-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNimBLEDis.cpp
More file actions
145 lines (123 loc) · 4.9 KB
/
Copy pathNimBLEDis.cpp
File metadata and controls
145 lines (123 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2025 Ryan Powell and NimBLEOta contributors
// Sponsored by Theengs https://www.theengs.io
// MIT License
#include "NimBLEDevice.h"
#include "NimBLEDis.h"
#include "NimBLELog.h"
#define BLE_DIS_SERVICE_UUID (uint16_t)0x180a
#define BLE_DIS_SYSTEM_ID_CHR_UUID (uint16_t)0x2A23
#define BLE_DIS_MODEL_NUMBER_CHR_UUID (uint16_t)0x2A24
#define BLE_DIS_SERIAL_NUMBER_CHR_UUID (uint16_t)0x2A25
#define BLE_DIS_FIRMWARE_REVISION_CHR_UUID (uint16_t)0x2A26
#define BLE_DIS_HARDWARE_REVISION_CHR_UUID (uint16_t)0x2A27
#define BLE_DIS_SOFTWARE_REVISION_CHR_UUID (uint16_t)0x2A28
#define BLE_DIS_MANUFACTURER_NAME_CHR_UUID (uint16_t)0x2A29
#define BLE_DIS_REG_CERT_CHR_UUID (uint16_t)0x2A2A
#define BLE_DIS_PNP_ID_CHR_UUID (uint16_t)0x2A50
static const char* LOG_TAG = "NimBLEDis";
bool NimBLEDis::createDisChar(const NimBLEUUID& uuid, const uint8_t* value, uint16_t length) {
if (!m_pDisService) {
NIMBLE_LOGE(LOG_TAG, "Device Information Service not initialized");
return false;
}
if (m_pDisService->getCharacteristic(uuid)) {
NIMBLE_LOGE(LOG_TAG,
"%s - Characteristic value already set",
NimBLEUUID(BLE_DIS_MODEL_NUMBER_CHR_UUID).toString().c_str());
return false;
}
NimBLECharacteristic* pChar = m_pDisService->createCharacteristic(uuid, NIMBLE_PROPERTY::READ, length);
if (!pChar) {
NIMBLE_LOGE(LOG_TAG, "Failed to create characteristic");
return false;
}
pChar->setValue(value, length);
if (memcmp(pChar->getValue(), value, length) != 0) {
NIMBLE_LOGE(LOG_TAG, "Failed to set characteristic value");
return false;
}
return true;
}
bool NimBLEDis::setModelNumber(const char* value) {
if (createDisChar(BLE_DIS_MODEL_NUMBER_CHR_UUID, reinterpret_cast<const uint8_t*>(value), strlen(value))) {
NIMBLE_LOGI(LOG_TAG, "Model Number set to: %s", value);
return true;
}
return false;
}
bool NimBLEDis::setSerialNumber(const char* value) {
if (createDisChar(BLE_DIS_SERIAL_NUMBER_CHR_UUID, reinterpret_cast<const uint8_t*>(value), strlen(value))) {
NIMBLE_LOGI(LOG_TAG, "Serial Number set to: %s", value);
}
return false;
}
bool NimBLEDis::setFirmwareRevision(const char* value) {
if (createDisChar(BLE_DIS_FIRMWARE_REVISION_CHR_UUID, reinterpret_cast<const uint8_t*>(value), strlen(value))) {
NIMBLE_LOGI(LOG_TAG, "Firmware Revision set to: %s", value);
return true;
}
return false;
}
bool NimBLEDis::setHardwareRevision(const char* value) {
if (createDisChar(BLE_DIS_HARDWARE_REVISION_CHR_UUID, reinterpret_cast<const uint8_t*>(value), strlen(value))) {
NIMBLE_LOGI(LOG_TAG, "Hardware Revision set to: %s", value);
return true;
}
return false;
}
bool NimBLEDis::setSoftwareRevision(const char* value) {
if (createDisChar(BLE_DIS_SOFTWARE_REVISION_CHR_UUID, reinterpret_cast<const uint8_t*>(value), strlen(value))) {
NIMBLE_LOGI(LOG_TAG, "Software Revision set to: %s", value);
return true;
}
return false;
}
bool NimBLEDis::setManufacturerName(const char* value) {
if (createDisChar(BLE_DIS_MANUFACTURER_NAME_CHR_UUID, reinterpret_cast<const uint8_t*>(value), strlen(value))) {
NIMBLE_LOGI(LOG_TAG, "Manufacturer Name set to: %s", value);
return true;
}
return false;
}
bool NimBLEDis::setSystemId(const char* value) {
if (createDisChar(BLE_DIS_SYSTEM_ID_CHR_UUID, reinterpret_cast<const uint8_t*>(value), strlen(value))) {
NIMBLE_LOGI(LOG_TAG, "System ID set to: %s", value);
return true;
}
return false;
}
bool NimBLEDis::setPnp(uint8_t src, uint16_t vid, uint16_t pid, uint16_t ver) {
uint8_t pnp[] = {src,
static_cast<uint8_t>(vid & 0xFF),
static_cast<uint8_t>((vid >> 8) & 0xFF),
static_cast<uint8_t>(pid & 0xFF),
static_cast<uint8_t>((pid >> 8) & 0xFF),
static_cast<uint8_t>(ver & 0xFF),
static_cast<uint8_t>((ver >> 8) & 0xFF)};
if (createDisChar(BLE_DIS_PNP_ID_CHR_UUID, pnp, sizeof(pnp))) {
NIMBLE_LOGI(LOG_TAG, "PNP ID set to: %02x:%04x:%04x:%04x", src, vid, pid, ver);
return true;
}
return false;
}
bool NimBLEDis::init() {
NimBLEServer* pServer = NimBLEDevice::createServer();
if (!pServer) {
NIMBLE_LOGE(LOG_TAG, "Failed to get server");
return false;
}
m_pDisService = pServer->createService(BLE_DIS_SERVICE_UUID);
if (!m_pDisService) {
NIMBLE_LOGE(LOG_TAG, "Failed to create service");
return false;
}
NIMBLE_LOGD(LOG_TAG, "Device Information Service created");
return true;
}
bool NimBLEDis::start() {
if (m_pDisService != nullptr) {
return m_pDisService->start();
}
NIMBLE_LOGE(LOG_TAG, "Device Information Service not initialized");
return false;
}