NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
NimBLEService.h
1/*
2 * Copyright 2020-2024 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 "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
23
24class NimBLEService;
25
26# include "NimBLEAttribute.h"
27# include "NimBLEServer.h"
28# include "NimBLECharacteristic.h"
29
35 public:
36 NimBLEService(const char* uuid);
37 NimBLEService(const NimBLEUUID& uuid);
39
40 NimBLEServer* getServer() const;
41 std::string toString() const;
42 void dump() const;
43 bool isStarted() const;
44 bool start();
46 uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
47 uint16_t max_len = BLE_ATT_ATTR_MAX_LEN);
48
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_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
73#endif /* NIMBLE_CPP_SERVICE_H_ */
The model of a BLE Characteristic.
Definition NimBLECharacteristic.h:40
A base class for local BLE attributes.
Definition NimBLELocalAttribute.h:29
The model of a BLE server.
Definition NimBLEServer.h:60
The model of a BLE service.
Definition NimBLEService.h:34
NimBLECharacteristic * createCharacteristic(const char *uuid, uint32_t properties=NIMBLE_PROPERTY::READ|NIMBLE_PROPERTY::WRITE, uint16_t max_len=BLE_ATT_ATTR_MAX_LEN)
Create a new BLE Characteristic associated with this service.
Definition NimBLEService.cpp:195
NimBLECharacteristic * getCharacteristicByHandle(uint16_t handle) const
Get a pointer to the characteristic object with the specified handle.
Definition NimBLEService.cpp:309
std::string toString() const
Return a string representation of this service. A service is defined by:
Definition NimBLEService.cpp:354
const std::vector< NimBLECharacteristic * > & getCharacteristics() const
Definition NimBLEService.cpp:322
~NimBLEService()
Destructor, make sure we release the resources allocated for the service.
Definition NimBLEService.cpp:51
void dump() const
Dump details of this BLE GATT service.
Definition NimBLEService.cpp:67
bool isStarted() const
Checks if the service has been started.
Definition NimBLEService.cpp:375
NimBLEServer * getServer() const
Get the BLE server associated with this service.
Definition NimBLEService.cpp:367
bool start()
Builds the database of characteristics/descriptors for the service and registers it with the NimBLE s...
Definition NimBLEService.cpp:92
NimBLECharacteristic * getCharacteristic(const char *uuid, uint16_t instanceId=0) const
Get a pointer to the characteristic object with the specified UUID.
Definition NimBLEService.cpp:280
void addCharacteristic(NimBLECharacteristic *pCharacteristic)
Add a characteristic to the service.
Definition NimBLEService.cpp:220
void removeCharacteristic(NimBLECharacteristic *pCharacteristic, bool deleteChr=false)
Remove a characteristic from the service.
Definition NimBLEService.cpp:252
A model of a BLE UUID.
Definition NimBLEUUID.h:41