DrObscure

ble_app_beacon - main.c

Mar 25th, 2017
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12.  
  13. /** @file
  14. *
  15. * @defgroup ble_sdk_app_beacon_main main.c
  16. * @{
  17. * @ingroup ble_sdk_app_beacon
  18. * @brief Beacon Transmitter Sample Application main file.
  19. *
  20. * This file contains the source code for an Beacon transmitter sample application.
  21. */
  22.  
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include "nrf_delay.h"
  26. #include "ble_advdata.h"
  27. #include "boards.h"
  28. #include "nordic_common.h"
  29. #include "softdevice_handler.h"
  30.  
  31. #define IS_SRVC_CHANGED_CHARACT_PRESENT 0 /**< Include or not the service_changed characteristic. if not enabled, the server's database cannot be changed for the lifetime of the device*/
  32.  
  33. #define ADVERTISING_LED_PIN_NO LED_0 /**< Is on when device is advertising. */
  34.  
  35. #define APP_CFG_NON_CONN_ADV_TIMEOUT 0 /**< Time for which the device must be advertising in non-connectable mode (in seconds). 0 disables timeout. */
  36. #define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS) /**< The advertising interval for non-connectable advertisement (100 ms). This value can vary between 100ms to 10.24s). */
  37.  
  38. #define APP_BEACON_INFO_LENGTH 0x17 /**< Total length of information advertised by the Beacon. */
  39. #define APP_ADV_DATA_LENGTH 0x15 /**< Length of manufacturer specific data in the advertisement. */
  40. #define APP_DEVICE_TYPE 0x02 /**< 0x02 refers to Beacon. */
  41. #define APP_MEASURED_RSSI 0xC3 /**< The Beacon's measured RSSI at 1 meter distance in dBm. */
  42. #define APP_COMPANY_IDENTIFIER 0x004C /**< Company identifier for Apple Inc. as per www.bluetooth.org. */
  43. #define APP_MAJOR_VALUE 0x01, 0x02 /**< Major value used to identify Beacons. */
  44. #define APP_MINOR_VALUE 0x03, 0x04 /**< Minor value used to identify Beacons. */
  45. #define APP_BEACON_UUID 0x01, 0x12, 0x23, 0x34, \
  46. 0x45, 0x56, 0x67, 0x78, \
  47. 0x89, 0x9a, 0xab, 0xbc, \
  48. 0xcd, 0xde, 0xef, 0xf0 /**< Proprietary UUID for Beacon. */
  49.  
  50. #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
  51.  
  52. #if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
  53. #define MAJ_VAL_OFFSET_IN_BEACON_INFO 18 /**< Position of the MSB of the Major Value in m_beacon_info array. */
  54. #define UICR_ADDRESS 0x10001080 /**< Address of the UICR register used by this example. The major and minor versions to be encoded into the advertising data will be picked up from this location. */
  55. #endif
  56.  
  57. static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the stack when starting advertising. */
  58. static uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] = /**< Information advertised by the Beacon. */
  59. {
  60. APP_DEVICE_TYPE, // Manufacturer specific information. Specifies the device type in this
  61. // implementation.
  62. APP_ADV_DATA_LENGTH, // Manufacturer specific information. Specifies the length of the
  63. // manufacturer specific data in this implementation.
  64. APP_BEACON_UUID, // 128 bit UUID value.
  65. APP_MAJOR_VALUE, // Major arbitrary value that can be used to distinguish between Beacons.
  66. APP_MINOR_VALUE, // Minor arbitrary value that can be used to distinguish between Beacons.
  67. APP_MEASURED_RSSI // Manufacturer specific information. The Beacon's measured TX power in
  68. // this implementation.
  69. };
  70.  
  71.  
  72. /**@brief Function for error handling, which is called when an error has occurred.
  73. *
  74. * @warning This handler is an example only and does not fit a final product. You need to analyze
  75. * how your product is supposed to react in case of error.
  76. *
  77. * @param[in] error_code Error code supplied to the handler.
  78. * @param[in] line_num Line number where the handler is called.
  79. * @param[in] p_file_name Pointer to the file name.
  80. */
  81. void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
  82. {
  83. // This call can be used for debug purposes during application development.
  84. // @note CAUTION: Activating this code will write the stack to flash on an error.
  85. // This function should NOT be used in a final product.
  86. // It is intended STRICTLY for development/debugging purposes.
  87. // The flash write will happen EVEN if the radio is active, thus interrupting
  88. // any communication.
  89. // Use with care. Un-comment the line below to use.
  90. // ble_debug_assert_handler(error_code, line_num, p_file_name);
  91.  
  92. // On assert, the system can only recover on reset.
  93. NVIC_SystemReset();
  94. }
  95.  
  96.  
  97. /**@brief Callback function for asserts in the SoftDevice.
  98. *
  99. * @details This function will be called in case of an assert in the SoftDevice.
  100. *
  101. * @warning This handler is an example only and does not fit a final product. You need to analyze
  102. * how your product is supposed to react in case of Assert.
  103. * @warning On assert from the SoftDevice, the system can only recover on reset.
  104. *
  105. * @param[in] line_num Line number of the failing ASSERT call.
  106. * @param[in] file_name File name of the failing ASSERT call.
  107. */
  108. void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
  109. {
  110. app_error_handler(DEAD_BEEF, line_num, p_file_name);
  111. }
  112.  
  113.  
  114. /**@brief Function for the LEDs initialization.
  115. *
  116. * @details Initializes all LEDs used by this application.
  117. */
  118. static void leds_init(void)
  119. {
  120. nrf_gpio_cfg_output(LED_0);
  121. nrf_gpio_cfg_output(LED_1);
  122. nrf_gpio_cfg_output(LED_2);
  123. nrf_gpio_cfg_output(LED_3);
  124. }
  125.  
  126.  
  127. /**@brief Function for initializing the Advertising functionality.
  128. *
  129. * @details Encodes the required advertising data and passes it to the stack.
  130. * Also builds a structure to be passed to the stack when starting advertising.
  131. */
  132. static void advertising_init(void)
  133. {
  134. uint32_t err_code;
  135. ble_advdata_t advdata;
  136. uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
  137.  
  138. ble_advdata_manuf_data_t manuf_specific_data;
  139. manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
  140.  
  141. #if defined(USE_UICR_FOR_MAJ_MIN_VALUES)
  142. // If USE_UICR_FOR_MAJ_MIN_VALUES is defined, the major and minor values will be read from the
  143. // UICR instead of using the default values. The major and minor values obtained from the UICR
  144. // are encoded into advertising data in big endian order (MSB First).
  145. // To set the UICR used by this example to a desired value, write to the address 0x10001080
  146. // using the nrfjprog tool. The command to be used is as follows.
  147. // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val <your major/minor value>
  148. // For example, for a major value and minor value of 0xabcd and 0x0102 respectively, the
  149. // the following command should be used.
  150. // nrfjprog --snr <Segger-chip-Serial-Number> --memwr 0x10001080 --val 0xabcd0102
  151. uint16_t major_value = ((*(uint32_t *)UICR_ADDRESS) & 0xFFFF0000) >> 16;
  152. uint16_t minor_value = ((*(uint32_t *)UICR_ADDRESS) & 0x0000FFFF);
  153.  
  154. uint8_t index = MAJ_VAL_OFFSET_IN_BEACON_INFO;
  155.  
  156. m_beacon_info[index++] = MSB(major_value);
  157. m_beacon_info[index++] = LSB(major_value);
  158.  
  159. m_beacon_info[index++] = MSB(minor_value);
  160. m_beacon_info[index++] = LSB(minor_value);
  161. #endif
  162.  
  163. manuf_specific_data.data.p_data = (uint8_t *) m_beacon_info;
  164. manuf_specific_data.data.size = APP_BEACON_INFO_LENGTH;
  165.  
  166. // Build and set advertising data.
  167. memset(&advdata, 0, sizeof(advdata));
  168.  
  169. advdata.name_type = BLE_ADVDATA_NO_NAME;
  170. advdata.flags.size = sizeof(flags);
  171. advdata.flags.p_data = &flags;
  172. advdata.p_manuf_specific_data = &manuf_specific_data;
  173.  
  174. err_code = ble_advdata_set(&advdata, NULL);
  175. APP_ERROR_CHECK(err_code);
  176.  
  177. // Initialize advertising parameters (used when starting advertising).
  178. memset(&m_adv_params, 0, sizeof(m_adv_params));
  179.  
  180. m_adv_params.type = BLE_GAP_ADV_TYPE_ADV_NONCONN_IND;
  181. m_adv_params.p_peer_addr = NULL; // Undirected advertisement.
  182. m_adv_params.fp = BLE_GAP_ADV_FP_ANY;
  183. m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
  184. m_adv_params.timeout = APP_CFG_NON_CONN_ADV_TIMEOUT;
  185. }
  186.  
  187.  
  188. /**@brief Function for starting advertising.
  189. */
  190. static void advertising_start(void)
  191. {
  192. uint32_t err_code;
  193.  
  194. err_code = sd_ble_gap_adv_start(&m_adv_params);
  195. APP_ERROR_CHECK(err_code);
  196.  
  197. nrf_gpio_pin_set(ADVERTISING_LED_PIN_NO);
  198. }
  199.  
  200.  
  201. /**@brief Function for initializing the BLE stack.
  202. *
  203. * @details Initializes the SoftDevice and the BLE event interrupt.
  204. */
  205. static void ble_stack_init(void)
  206. {
  207. uint32_t err_code;
  208.  
  209. // Initialize the SoftDevice handler module.
  210. SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_SYNTH_250_PPM, false);
  211.  
  212. // Enable BLE stack
  213. ble_enable_params_t ble_enable_params;
  214.  
  215. memset(&ble_enable_params, 0, sizeof(ble_enable_params));
  216. ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
  217. err_code = sd_ble_enable(&ble_enable_params);
  218. APP_ERROR_CHECK(err_code);
  219. }
  220.  
  221.  
  222. /**@brief Function for doing power management.
  223. */
  224. static void power_manage(void)
  225. {
  226. uint32_t err_code = sd_app_evt_wait();
  227. APP_ERROR_CHECK(err_code);
  228. }
  229.  
  230.  
  231. int main(void)
  232. {
  233. nrf_delay_ms(500);
  234.  
  235. // Initialize.
  236. leds_init();
  237. nrf_gpio_pin_set(LED_3);
  238. nrf_delay_ms(500);
  239. nrf_gpio_pin_clear(LED_3);
  240.  
  241. nrf_gpio_pin_set(LED_2);
  242. ble_stack_init();
  243. nrf_delay_ms(500);
  244. nrf_gpio_pin_clear(LED_2);
  245.  
  246. nrf_gpio_pin_set(LED_1);
  247. advertising_init();
  248. nrf_delay_ms(500);
  249. nrf_gpio_pin_clear(LED_1);
  250.  
  251. // Start execution.
  252. advertising_start();
  253.  
  254. nrf_delay_ms(500);
  255. nrf_gpio_pin_set(LED_3);
  256. nrf_delay_ms(500);
  257.  
  258. uint32_t flag = 1;
  259. // Enter main loop.
  260. for (;;)
  261. {
  262. power_manage();
  263. if(flag)
  264. { flag =0; nrf_gpio_pin_clear(LED_3);}
  265. else
  266. { flag = 1; nrf_gpio_pin_set(LED_3); }
  267. nrf_delay_ms(100);
  268. }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment