| BlueNRG-1 DK: BLE examples

- Main Page
- Modules
- Files

- File List

BLE_Beacon_main.c
Go to the documentation of this file.
1
2 /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
3 * File Name : BLE_Beacon_main.c
4 * Author : VMA DIVISION
5 * Version : 1.1.0
6 * Date : 15-January-2016
7 * Description : Code demostrating the BLE Beacon application
8 ********************************************************************************
9 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
10 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
11 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
12 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
13 * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
14 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
15 *******************************************************************************/
16
196 /* Includes ------------------------------------------------------------------*/
197 #include <stdio.h>
198 #include <string.h>
199 #include "BlueNRG1_it.h"
200 #include "BlueNRG1_conf.h"
201 #include "ble_const.h"
202 #include "bluenrg1_stack.h"
203 #include "sleep.h"
204 #include "SDK_EVAL_Config.h"
205 #include "Beacon_config.h"
206 #include "OTA_btl.h"
207
208 /* Private typedef -----------------------------------------------------------*/
209 /* Private define ------------------------------------------------------------*/
210 #define BLE_BEACON_VERSION_STRING "1.1.0"
211
212 /* Set to 1 for enabling Flags AD Type position at the beginning
213 of the advertising packet */
214 #define ENABLE_FLAGS_AD_TYPE_AT_BEGINNING 1
215
216 /* Private macro -------------------------------------------------------------*/
217 /* Private variables ---------------------------------------------------------*/
218 /* Private function prototypes -----------------------------------------------*/
219 /* Private functions ---------------------------------------------------------*/
220
221 void Device_Init(void)
222 {
223 uint8_t ret;
224 uint16_t service_handle;
225 uint16_t dev_name_char_handle;
226 uint16_t appearance_char_handle;
227
228 /* Set the TX Power to -2 dBm */
229 ret = aci_hal_set_tx_power_level(1,4);
230 if(ret != 0) {
231 printf ("Error in aci_hal_set_tx_power_level() 0x%04xr\n", ret);
232 while(1);
233 }
234
235 /* Init the GATT */
236 ret = aci_gatt_init();
237 if (ret != 0)
238 printf ("Error in aci_gatt_init() 0x%04xr\n", ret);
239 else
240 printf ("aci_gatt_init() --> SUCCESS\r\n");
241
242 /* Init the GAP */
243 ret = aci_gap_init(0x01, 0x00, 0x08, &service_handle,
244 &dev_name_char_handle, &appearance_char_handle);
245 if (ret != 0)
246 printf ("Error in aci_gap_init() 0x%04x\r\n", ret);
247 else
248 printf ("aci_gap_init() --> SUCCESS\r\n");
249 }
250
251
257 static void Start_Beaconing(void)
258 {
259 uint8_t ret = BLE_STATUS_SUCCESS;
260
261 #if ENABLE_FLAGS_AD_TYPE_AT_BEGINNING
262 /* Set AD Type Flags at beginning on Advertising packet */
263 uint8_t adv_data[] = {
264 /* Advertising data: Flags AD Type */
265 0x02,
266 0x01,
267 0x06,
268 /* Advertising data: manufacturer specific data */
269 26, //len
270 AD_TYPE_MANUFACTURER_SPECIFIC_DATA, //manufacturer type
271 0x30, 0x00, //Company identifier code (Default is 0x0030 - STMicroelectronics: To be customized for specific identifier)
272 0x02, // ID
273 0x15, //Length of the remaining payload
274 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, //Location UUID
275 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61,
276 0x00, 0x00, // Major number
277 0x00, 0x00, // Minor number
278 0xC8 //2's complement of the Tx power (-56dB)};
279 };
280 #else
281 uint8_t manuf_data[] = {
282 26, //len
283 AD_TYPE_MANUFACTURER_SPECIFIC_DATA, //manufacturer type
284 0x30, 0x00, //Company identifier code (Default is 0x0030 - STMicroelectronics: To be customized for specific identifier)
285 0x02, // ID
286 0x15, //Length of the remaining payload
287 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, //Location UUID
288 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61,
289 0x00, 0x00, // Major number
290 0x00, 0x00, // Minor number
291 0xC8 //2's complement of the Tx power (-56dB)};
292 };
293 #endif
294
295 /* disable scan response */
296 ret = hci_le_set_scan_response_data(0,NULL);
297 if (ret != BLE_STATUS_SUCCESS)
298 {
299 printf ("Error in hci_le_set_scan_resp_data() 0x%04x\r\n", ret);
300 return;
301 }
302 else
303 printf ("hci_le_set_scan_resp_data() --> SUCCESS\r\n");
304
305 /* put device in non connectable mode */
306 ret = aci_gap_set_discoverable(ADV_NONCONN_IND, 160, 160, PUBLIC_ADDR, NO_WHITE_LIST_USE,
307 0, NULL, 0, NULL, 0, 0);
308 if (ret != BLE_STATUS_SUCCESS)
309 {
310 printf ("Error in aci_gap_set_discoverable() 0x%04x\r\n", ret);
311 return;
312 }
313 else
314 printf ("aci_gap_set_discoverable() --> SUCCESS\r\n");
315
316 #if ENABLE_FLAGS_AD_TYPE_AT_BEGINNING
317 /* Set the ADV data with the Flags AD Type at beginning of the
318 advertsing packet, followed by the beacon manufacturer specific data */
319 ret = hci_le_set_advertising_data (sizeof(adv_data), adv_data);
320 if (ret != BLE_STATUS_SUCCESS)
321 {
322 printf ("Error in hci_le_set_advertising_data() 0x%04x\r\n", ret);
323 return;
324 }
325 else
326 printf ("hci_le_set_advertising_data() --> SUCCESS\r\n");
327 #else
328 /* Delete the TX power level information */
329 ret = aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL);
330 if (ret != BLE_STATUS_SUCCESS)
331 {
332 printf ("Error in aci_gap_delete_ad_type() 0x%04x\r\n", ret);
333 return;
334 }
335 else
336 printf ("aci_gap_delete_ad_type() --> SUCCESS\r\n");
337
338 /* Update the ADV data with the BEACON manufacturing data */
339 ret = aci_gap_update_adv_data(27, manuf_data);
340 if (ret != BLE_STATUS_SUCCESS)
341 {
342 printf ("Error in aci_gap_update_adv_data() 0x%04x\r\n", ret);
343 return;
344 }
345 else
346 printf ("aci_gap_update_adv_data() --> SUCCESS\r\n");
347 #endif
348 }
349
350 int main(void)
351 {
352 uint8_t ret;
353
354 /* System Init */
355 SystemInit();
356
357 /* Identify BlueNRG-1 platform */
358 SdkEvalIdentification();
359
360 /* Init the UART peripheral */
361 SdkEvalComUartInit(UART_BAUDRATE);
362
363 /* BlueNRG-1 stack init */
364 ret = BlueNRG_Stack_Initialization(&BlueNRG_Stack_Init_params);
365 if (ret != BLE_STATUS_SUCCESS) {
366 printf("Error in BlueNRG_Stack_Initialization() 0x%02x\r\n", ret);
367 while(1);
368 }
369
370 /* Init the BlueNRG-1 device */
371 Device_Init();
372
373 #if ST_USE_OTA_SERVICE_MANAGER_APPLICATION
374 /* Initialize the button */
375 SdkEvalPushButtonInit(BUTTON_1);
376 #endif /* ST_USE_OTA_SERVICE_MANAGER_APPLICATION */
377
378 /* Start Beacon Non Connectable Mode*/
379 Start_Beaconing();
380
381 printf("BlueNRG-1 BLE Beacon Application (version: %s)\r\n", BLE_BEACON_VERSION_STRING);
382
383 while(1)
384 {
385 /* BlueNRG-1 stack tick */
386 BTLE_StackTick();
387
388 /* Enable Power Save according the Advertising Interval */
389 BlueNRG_Sleep(SLEEPMODE_WAKETIMER, 0, 0, 0);
390
391 #if ST_USE_OTA_SERVICE_MANAGER_APPLICATION
392 if (SdkEvalPushButtonGetState(BUTTON_1) == RESET)
393 {
394 OTA_Jump_To_Service_Manager_Application();
395 }
396 #endif /* ST_USE_OTA_SERVICE_MANAGER_APPLICATION */
397 }
398 }
399
400
401 #ifdef USE_FULL_ASSERT
402
409 void assert_failed(uint8_t* file, uint32_t line)
410 {
411 /* User can add his own implementation to report the file name and line number,
412 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
413
414 /* Infinite loop */
415 while (1)
416 {
417 }
418 }
419
420 #endif
421
422 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/