esp-nimble-cpp 2.3.2
Loading...
Searching...
No Matches
NimBLEService.h
1/*
2 * Copyright 2020-2025 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NIMBLE_CPP_SERVICE_H_
19#define NIMBLE_CPP_SERVICE_H_
20
21#include "syscfg/syscfg.h"
22#if CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
23
24class NimBLEService;
25
26# include "NimBLEAttribute.h"
27# include "NimBLEServer.h"
28# include "NimBLECharacteristic.h"
29
34class NimBLEService : public NimBLELocalAttribute {
35 public:
36 NimBLEService(const char* uuid);
37 NimBLEService(const NimBLEUUID& uuid);
38 ~NimBLEService();
39
40 NimBLEServer* getServer() const;
41 std::string toString() const;
42 void dump() const;
43 bool isStarted() const;
44 bool start();
45 NimBLECharacteristic* createCharacteristic(const char* uuid,
46 uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
47 uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
48
49 NimBLECharacteristic* createCharacteristic(const NimBLEUUID& uuid,
50 uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
51 uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
52 void addCharacteristic(NimBLECharacteristic* pCharacteristic);
53 void removeCharacteristic(NimBLECharacteristic* pCharacteristic, bool deleteChr = false);
54 NimBLECharacteristic* getCharacteristic(const char* uuid, uint16_t instanceId = 0) const;
55 NimBLECharacteristic* getCharacteristic(const NimBLEUUID& uuid, uint16_t instanceId = 0) const;
56 NimBLECharacteristic* getCharacteristicByHandle(uint16_t handle) const;
57
58 const std::vector<NimBLECharacteristic*>& getCharacteristics() const;
59 std::vector<NimBLECharacteristic*> getCharacteristics(const char* uuid) const;
60 std::vector<NimBLECharacteristic*> getCharacteristics(const NimBLEUUID& uuid) const;
61
62 private:
63 friend class NimBLEServer;
64
65 std::vector<NimBLECharacteristic*> m_vChars{};
66 // Nimble requires an array of services to be sent to the api
67 // Since we are adding 1 at a time we create an array of 2 and set the type
68 // of the second service to 0 to indicate the end of the array.
69 ble_gatt_svc_def m_pSvcDef[2]{};
70}; // NimBLEService
71
72#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
73#endif // NIMBLE_CPP_SERVICE_H_
A model of a BLE UUID.
Definition NimBLEUUID.h:41