@@ -90,84 +90,78 @@ void NimBLEService::dump() const {
9090 */
9191bool NimBLEService::start () {
9292 NIMBLE_LOGD (LOG_TAG, " >> start(): Starting service: %s" , toString ().c_str ());
93+ // If started previously, clear everything and start over
94+ if (m_pSvcDef->characteristics ) {
95+ if (m_pSvcDef->characteristics ->descriptors ) {
96+ delete[] m_pSvcDef->characteristics ->descriptors ;
97+ }
98+ delete[] m_pSvcDef->characteristics ;
99+ }
93100
94- // Rebuild the service definition if the server attributes have changed.
95- if (getServer ()->m_svcChanged ) {
96- if (m_pSvcDef->characteristics ) {
97- if (m_pSvcDef->characteristics ->descriptors ) {
98- delete[] m_pSvcDef->characteristics ->descriptors ;
99- }
100- delete[] m_pSvcDef->characteristics ;
101+ size_t numChrs = 0 ;
102+ for (const auto & chr : m_vChars) {
103+ if (chr->getRemoved ()) {
104+ continue ;
101105 }
102- m_pSvcDef-> type = 0 ;
106+ ++numChrs ;
103107 }
104108
105- if (!m_pSvcDef->type ) {
106- m_pSvcDef->type = BLE_GATT_SVC_TYPE_PRIMARY;
107- size_t numChrs = 0 ;
109+ NIMBLE_LOGD (LOG_TAG, " Adding %d characteristics for service %s" , numChrs, toString ().c_str ());
110+ if (numChrs) {
111+ int i = 0 ;
112+
113+ // Nimble requires the last characteristic to have it's uuid = 0 to indicate the end
114+ // of the characteristics for the service. We create 1 extra and set it to null
115+ // for this purpose.
116+ ble_gatt_chr_def* pChrs = new ble_gatt_chr_def[numChrs + 1 ]{};
108117 for (const auto & chr : m_vChars) {
109118 if (chr->getRemoved ()) {
110119 continue ;
111120 }
112- ++numChrs;
113- }
114-
115- NIMBLE_LOGD (LOG_TAG, " Adding %d characteristics for service %s" , numChrs, toString ().c_str ());
116- if (numChrs) {
117- int i = 0 ;
118121
119- // Nimble requires the last characteristic to have it's uuid = 0 to indicate the end
120- // of the characteristics for the service. We create 1 extra and set it to null
121- // for this purpose.
122- ble_gatt_chr_def* pChrs = new ble_gatt_chr_def[numChrs + 1 ]{};
123- for (const auto & chr : m_vChars) {
124- if (chr->getRemoved ()) {
122+ size_t numDscs = 0 ;
123+ for (const auto & dsc : chr->m_vDescriptors ) {
124+ if (dsc->getRemoved ()) {
125125 continue ;
126126 }
127+ ++numDscs;
128+ }
129+
130+ if (numDscs) {
131+ int j = 0 ;
127132
128- size_t numDscs = 0 ;
133+ // Must have last descriptor uuid = 0 so we have to create 1 extra
134+ ble_gatt_dsc_def* pDscs = new ble_gatt_dsc_def[numDscs + 1 ]{};
129135 for (const auto & dsc : chr->m_vDescriptors ) {
130136 if (dsc->getRemoved ()) {
131137 continue ;
132138 }
133- ++numDscs;
134- }
135-
136- if (numDscs) {
137- int j = 0 ;
138-
139- // Must have last descriptor uuid = 0 so we have to create 1 extra
140- ble_gatt_dsc_def* pDscs = new ble_gatt_dsc_def[numDscs + 1 ]{};
141- for (const auto & dsc : chr->m_vDescriptors ) {
142- if (dsc->getRemoved ()) {
143- continue ;
144- }
145-
146- pDscs[j].uuid = dsc->getUUID ().getBase ();
147- pDscs[j].att_flags = dsc->getProperties ();
148- pDscs[j].min_key_size = 0 ;
149- pDscs[j].access_cb = NimBLEServer::handleGattEvent;
150- pDscs[j].arg = dsc;
151- ++j;
152- }
153139
154- pChrs[i].descriptors = pDscs;
140+ pDscs[j].uuid = dsc->getUUID ().getBase ();
141+ pDscs[j].att_flags = dsc->getProperties ();
142+ pDscs[j].min_key_size = 0 ;
143+ pDscs[j].access_cb = NimBLEServer::handleGattEvent;
144+ pDscs[j].arg = dsc;
145+ ++j;
155146 }
156147
157- pChrs[i].uuid = chr->getUUID ().getBase ();
158- pChrs[i].access_cb = NimBLEServer::handleGattEvent;
159- pChrs[i].arg = chr;
160- pChrs[i].flags = chr->getProperties ();
161- pChrs[i].min_key_size = 0 ;
162- pChrs[i].val_handle = &chr->m_handle ;
163- ++i;
148+ pChrs[i].descriptors = pDscs;
164149 }
165150
166- m_pSvcDef->characteristics = pChrs;
151+ pChrs[i].uuid = chr->getUUID ().getBase ();
152+ pChrs[i].access_cb = NimBLEServer::handleGattEvent;
153+ pChrs[i].arg = chr;
154+ pChrs[i].flags = chr->getProperties ();
155+ pChrs[i].min_key_size = 0 ;
156+ pChrs[i].val_handle = &chr->m_handle ;
157+ ++i;
167158 }
159+
160+ m_pSvcDef->characteristics = pChrs;
168161 }
169162
170- int rc = ble_gatts_count_cfg (m_pSvcDef);
163+ m_pSvcDef->type = BLE_GATT_SVC_TYPE_PRIMARY;
164+ int rc = ble_gatts_count_cfg (m_pSvcDef);
171165 if (rc != 0 ) {
172166 NIMBLE_LOGE (LOG_TAG, " ble_gatts_count_cfg failed, rc= %d, %s" , rc, NimBLEUtils::returnCodeToString (rc));
173167 return false ;
0 commit comments