|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <sdkconfig.h> |
| 4 | + |
| 5 | +#include <string> |
| 6 | + |
| 7 | +#if CONFIG_BT_NIMBLE_ENABLED || defined(_DOXYGEN_) |
| 8 | + |
| 9 | +#include "NimBLEDevice.h" |
| 10 | + |
| 11 | +#include "base_component.hpp" |
| 12 | + |
| 13 | +namespace espp { |
| 14 | +/// Generic Access Service |
| 15 | +/// This class is responsible for creating and managing the Generic Access Service. |
| 16 | +/// |
| 17 | +/// The service is created with the following characteristics: |
| 18 | +/// - Name (read) |
| 19 | +/// - Appearance (read) |
| 20 | +/// |
| 21 | +/// The Generic Access Service is a required Bluetooth service that provides |
| 22 | +/// information about the device. This information can be used by a client to |
| 23 | +/// identify the device and determine its capabilities. The Generic Access |
| 24 | +/// Service is defined by the Bluetooth SIG and is intended to be used with any |
| 25 | +/// device. |
| 26 | +/// |
| 27 | +/// NOTE: as a developer, you should not need to actually use this service |
| 28 | +/// directly as the NimBLE stack will automatically include it in the device |
| 29 | +/// information. This class is provided for completeness and for developers who |
| 30 | +/// want to customize the service. I'm not really sure why I created this file, |
| 31 | +/// but I have so here we are. |
| 32 | +class GenericAccessService : public BaseComponent { |
| 33 | +public: |
| 34 | + static constexpr uint16_t SERVICE_UUID = 0x1800; |
| 35 | + static constexpr uint16_t NAME_CHAR_UUID = 0x2A00; |
| 36 | + static constexpr uint16_t APPEARANCE_CHAR_UUID = 0x2A01; |
| 37 | + |
| 38 | + /// Parse the appearance value from raw bytes |
| 39 | + /// \param bytes The characteristic value |
| 40 | + /// \return The appearance value |
| 41 | + static uint16_t parse_appearance(const std::vector<uint8_t> &bytes) { |
| 42 | + return parse_appearance(bytes.data(), bytes.size()); |
| 43 | + } |
| 44 | + |
| 45 | + /// Parse the appearance value from raw bytes |
| 46 | + /// \param bytes The characteristic value |
| 47 | + /// \param size The size of the characteristic value |
| 48 | + /// \return The appearance value |
| 49 | + static uint16_t parse_appearance(const uint8_t *bytes, size_t size) { |
| 50 | + if (size != 2) { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + return (bytes[1] << 8) | bytes[0]; |
| 54 | + } |
| 55 | + |
| 56 | + /// Constructor |
| 57 | + /// \param log_level The log level for the component |
| 58 | + explicit GenericAccessService(espp::Logger::Verbosity log_level = espp::Logger::Verbosity::WARN) |
| 59 | + : BaseComponent("GenericAccessService", log_level) {} |
| 60 | + |
| 61 | + /// Initialize the Service |
| 62 | + /// \param server The BLE server to add the service to |
| 63 | + void init(NimBLEServer *server) { make_service(server); } |
| 64 | + |
| 65 | + /// Deinitialize the Service |
| 66 | + /// \note This should only be called after NimBLEDevice::deinit(true) has been |
| 67 | + /// called, since that will free the memory used by the service |
| 68 | + void deinit() { |
| 69 | + service_ = nullptr; |
| 70 | + name_ = nullptr; |
| 71 | + appearance_ = nullptr; |
| 72 | + } |
| 73 | + |
| 74 | + /// Start the service |
| 75 | + /// \note This must be called after the service has been initialized |
| 76 | + void start() { |
| 77 | + if (!service_) { |
| 78 | + logger_.error("Service not created"); |
| 79 | + return; |
| 80 | + } |
| 81 | + if (!service_->start()) { |
| 82 | + logger_.error("Failed to start service"); |
| 83 | + return; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// Get the service object |
| 88 | + /// \return The service object |
| 89 | + NimBLEService *get_service() { return service_; } |
| 90 | + |
| 91 | + /// Get the UUID of the service |
| 92 | + /// \return The service UUID |
| 93 | + NimBLEUUID uuid() { |
| 94 | + if (service_) { |
| 95 | + return service_->getUUID(); |
| 96 | + } |
| 97 | + return NimBLEUUID(SERVICE_UUID); |
| 98 | + } |
| 99 | + |
| 100 | + /// Set the device name |
| 101 | + /// \param name The device name |
| 102 | + void set_name(const std::string &name) { |
| 103 | + if (!name_) { |
| 104 | + logger_.error("Characteristic not created"); |
| 105 | + return; |
| 106 | + } |
| 107 | + name_->setValue(name); |
| 108 | + } |
| 109 | + |
| 110 | + /// Set the device appearance |
| 111 | + /// \param appearance The appearance value |
| 112 | + void set_appearance(uint16_t appearance) { |
| 113 | + if (!appearance_) { |
| 114 | + logger_.error("Characteristic not created"); |
| 115 | + return; |
| 116 | + } |
| 117 | + appearance_->setValue(appearance); |
| 118 | + } |
| 119 | + |
| 120 | + /// Get the device name |
| 121 | + /// \return The device name |
| 122 | + std::string get_name() { |
| 123 | + if (!name_) { |
| 124 | + logger_.error("Characteristic not created"); |
| 125 | + return ""; |
| 126 | + } |
| 127 | + return name_->getValue(); |
| 128 | + } |
| 129 | + |
| 130 | + /// Get the device appearance |
| 131 | + /// \return The appearance value |
| 132 | + uint16_t get_appearance() { |
| 133 | + if (!appearance_) { |
| 134 | + logger_.error("Characteristic not created"); |
| 135 | + return 0; |
| 136 | + } |
| 137 | + return parse_appearance(appearance_->getValue()); |
| 138 | + } |
| 139 | + |
| 140 | +protected: |
| 141 | + void make_service(NimBLEServer *server) { |
| 142 | + service_ = server->createService(NimBLEUUID(SERVICE_UUID)); |
| 143 | + if (!service_) { |
| 144 | + logger_.error("Failed to create service"); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + name_ = service_->createCharacteristic(NimBLEUUID(NAME_CHAR_UUID), NIMBLE_PROPERTY::READ); |
| 149 | + |
| 150 | + appearance_ = |
| 151 | + service_->createCharacteristic(NimBLEUUID(APPEARANCE_CHAR_UUID), NIMBLE_PROPERTY::READ); |
| 152 | + } |
| 153 | + |
| 154 | + NimBLEService *service_{nullptr}; |
| 155 | + NimBLECharacteristic *name_{nullptr}; |
| 156 | + NimBLECharacteristic *appearance_{nullptr}; |
| 157 | +}; |
| 158 | +} // namespace espp |
| 159 | + |
| 160 | +#endif // CONFIG_BT_NIMBLE_ENABLED || defined(_DOXYGEN_) |
0 commit comments