@@ -52,13 +52,8 @@ bool BLEPeripheral::begin()
5252 }
5353
5454 pr_info (LOG_MODULE_BLE, " %s: %d" , __FUNCTION__, 2 );
55-
56- /* Populate advertising data
57- */
58- _advDataInit ();
59- pr_info (LOG_MODULE_BLE, " %s: %d" , __FUNCTION__, 3 );
6055
61- return (_startAdvertising () == BLE_STATUS_SUCCESS);
56+ return (startAdvertising () == BLE_STATUS_SUCCESS);
6257}
6358
6459void
@@ -154,26 +149,28 @@ void BLEPeripheral::addAttribute(BLEAttribute& attribute)
154149}
155150
156151
157- void
152+ BleStatus
158153BLEPeripheral::_advDataInit (void )
159154{
160- uint8_t lengthTotal = 0 ;
155+ uint8_t lengthTotal = 2 ; // Flags data length
156+ _adv_data_idx = 0 ;
157+
161158 /* Add flags */
162159 _adv_type = (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR);
163- _adv_data[_adv_data_idx].type = BT_DATA_FLAGS;
164- _adv_data[_adv_data_idx].data = &_adv_type;
165- _adv_data[_adv_data_idx].data_len = 1 ;
166- _adv_data_idx++;
160+ _adv_data[_adv_data_idx].type = BT_DATA_FLAGS;
161+ _adv_data[_adv_data_idx].data = &_adv_type;
162+ _adv_data[_adv_data_idx].data_len = 1 ;
163+ _adv_data_idx++;
167164
168165 if (_advertise_service_uuid)
169166 {
170- uint8_t type;
167+ uint8_t type;
171168 uint8_t length;
172169 uint8_t *data = NULL ;
173170
174- pr_info (LOG_MODULE_BLE, " ADV Type-%d" , _advertise_service_uuid->type );
171+ pr_info (LOG_MODULE_BLE, " ADV Type-%d" , _advertise_service_uuid->type );
175172 if (BT_UUID_TYPE_16 == _advertise_service_uuid->type )
176- {
173+ {
177174 // UINT16_TO_LESTREAM(adv_tmp, uuid.uuid16);
178175 data = (uint8_t *)&(((struct bt_uuid_16 *)_advertise_service_uuid)->val );
179176 length = sizeof (uint16_t );
@@ -187,22 +184,23 @@ BLEPeripheral::_advDataInit(void)
187184 }
188185 if (NULL != data)
189186 {
190- _adv_data[_adv_data_idx].type = type;
191- _adv_data[_adv_data_idx].data = data;
192- _adv_data[_adv_data_idx].data_len = length;
193- _adv_data_idx++;
187+ _adv_data[_adv_data_idx].type = type;
188+ _adv_data[_adv_data_idx].data = data;
189+ _adv_data[_adv_data_idx].data_len = length;
190+ _adv_data_idx++;
194191 lengthTotal += length;
195192
196- pr_info (LOG_MODULE_BLE, " Service UUID Len -%d" , length);
193+ pr_info (LOG_MODULE_BLE, " Service UUID Len -%d" , length);
197194 }
198195 }
199196
200- if (_local_name) {
197+ if (_local_name)
198+ {
201199 /* Add device name (truncated if too long) */
202- _adv_data[_adv_data_idx].type = BT_DATA_NAME_COMPLETE;
203- _adv_data[_adv_data_idx].data = (const uint8_t *)_local_name;
204- _adv_data[_adv_data_idx].data_len = strlen (_local_name);
205- _adv_data_idx++;
200+ _adv_data[_adv_data_idx].type = BT_DATA_NAME_COMPLETE;
201+ _adv_data[_adv_data_idx].data = (const uint8_t *)_local_name;
202+ _adv_data[_adv_data_idx].data_len = strlen (_local_name);
203+ _adv_data_idx++;
206204
207205 lengthTotal += strlen (_local_name);
208206 pr_info (LOG_MODULE_BLE, " Local Name -%s" , _local_name);
@@ -216,39 +214,61 @@ BLEPeripheral::_advDataInit(void)
216214 /* A 128-bit Service Data UUID won't fit in an Advertising packet */
217215 if (BT_UUID_TYPE_16 != _service_data_uuid->type )
218216 {
219- return ; /* We support service data only for 16-bit service UUID */
217+ /* We support service data only for 16-bit service UUID */
218+ return BLE_STATUS_NOT_SUPPORTED;
220219 }
221220
222221 uint8_t block_len = sizeof (uint16_t ) + _service_data_length;
223222 if (1 + block_len > BLE_MAX_ADV_SIZE)
224223 {
225- return ; // Service data block is too large.
224+ // Service data block is too large.
225+ return BLE_STATUS_ERROR_PARAMETER;
226226 }
227- _adv_data[_adv_data_idx].type = BT_DATA_SVC_DATA16;
228- _adv_data[_adv_data_idx].data = _service_data_buf;
229- _adv_data[_adv_data_idx].data_len = block_len;
230- _adv_data_idx++;
227+
228+ _adv_data[_adv_data_idx].type = BT_DATA_SVC_DATA16;
229+ _adv_data[_adv_data_idx].data = _service_data_buf;
230+ _adv_data[_adv_data_idx].data_len = block_len;
231+ _adv_data_idx++;
231232
232233 uint8_t *adv_tmp = _service_data_buf;
233234
234235 UINT16_TO_LESTREAM (adv_tmp, (((struct bt_uuid_16 *)_service_data_uuid)->val ));
235236 memcpy (adv_tmp, _service_data, _service_data_length);
236-
237+
237238 lengthTotal += block_len;
238239 pr_info (LOG_MODULE_BLE, " SVC Len -%d" , block_len);
239240 }
240- pr_info (LOG_MODULE_BLE, " ADV lengthTotal -%d" , lengthTotal);
241+ if (lengthTotal > BLE_MAX_ADV_SIZE)
242+ {
243+ pr_error (LOG_MODULE_BLE, " ADV Total length-%d" , lengthTotal);
244+ // Service data block is too large.
245+ return BLE_STATUS_ERROR_PARAMETER;
246+ }
247+ return BLE_STATUS_SUCCESS;
241248}
242249
243250BleStatus
244- BLEPeripheral::_startAdvertising ()
251+ BLEPeripheral::startAdvertising ()
245252{
246253 BleStatus status = BLE_STATUS_SUCCESS;
247-
254+ status = _advDataInit ();
255+ if (BLE_STATUS_SUCCESS != status)
256+ {
257+ return status;
258+ }
248259 status = BLEPeripheralRole::instance ()->startAdvertising (_adv_data,
249- _adv_data_idx,
250- NULL ,
251- 0 );
260+ _adv_data_idx,
261+ NULL ,
262+ 0 );
263+ return status;
264+ }
265+
266+ BleStatus
267+ BLEPeripheral::stopAdvertising ()
268+ {
269+ BleStatus status = BLE_STATUS_SUCCESS;
270+
271+ status = BLEPeripheralRole::instance ()->stopAdvertising ();
252272 return status;
253273}
254274
0 commit comments