ble_user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #include "ble_user.h"
  2. #include "ble_peripheral.h"
  3. #if NRF_MODULE_ENABLED(OWNED_C_SERVER)
  4. #include "ble_central.h"
  5. #endif
  6. //DFU需要引用的头文件
  7. #if NRF_MODULE_ENABLED(BLE_DFU)
  8. #include "nrf_power.h"
  9. #include "nrf_bootloader_info.h"
  10. #include "ble_dfu.h"
  11. #include "nrf_dfu_ble_svci_bond_sharing.h"
  12. #include "nrf_svci_async_function.h"
  13. #include "nrf_svci_async_handler.h"
  14. #endif
  15. #if NRF_MODULE_ENABLED(BLE_MY_UARTS)
  16. #include "my_ble_uarts.h"
  17. BLE_UARTS_DEF(m_uarts); //定义名称为m_uarts的串口透传服务实例
  18. #endif
  19. NRF_BLE_GATT_DEF(m_gatt); /**< ************* 定义名称为m_gatt的GATT模块实例 *************/
  20. NRF_BLE_QWR_DEF(m_qwr); /**< *********** 定义一个名称为m_qwr的排队写入实例 ************/
  21. #if WECHAT_SERVER_ENABLED==1
  22. BLE_WECHAT_DEF(m_wechat); /**< ******************** WECHAT instance. ********************/
  23. #endif
  24. static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context);
  25. #if OWNED_SERVER_ENABLED==1
  26. BLE_OWNED_DEF(m_owned); /**< ***************** Owned server instance. *****************/
  27. #endif
  28. static uint16_t *peripheral_conn_handle; /**< ************ Handle of the current connection. ***********/
  29. static ble_user_func_s ble_user_func={
  30. .recieve_data=NULL,
  31. .dfu_act=NULL,
  32. .ble_stat=NULL,
  33. };
  34. uint8_t m_addl_adv_manuf_data[BLE_GAP_ADDR_LEN]={0};
  35. APP_TIMER_DEF(ble_timer_id);
  36. static bool ble_timers_stat=false;
  37. //启动毫秒计时应用定时器。启动后,即开始产生毫秒计时,也就是提供了一个以毫秒为单位的计时基准
  38. static void ble_timers_stop()
  39. {
  40. if(ble_timers_stat==true)
  41. {
  42. //启动APP定时器
  43. ret_code_t err_code = app_timer_stop(ble_timer_id);
  44. APP_ERROR_CHECK(err_code);
  45. ble_timers_stat=false;
  46. }
  47. }
  48. //启动毫秒计时应用定时器。启动后,即开始产生毫秒计时,也就是提供了一个以毫秒为单位的计时基准
  49. static void ble_timers_start(uint32_t ms)
  50. {
  51. ble_timers_stop();
  52. //启动APP定时器
  53. ret_code_t err_code = app_timer_start(ble_timer_id, APP_TIMER_TICKS(ms), NULL);
  54. APP_ERROR_CHECK(err_code);
  55. ble_timers_stat=true;
  56. }
  57. static void ble_timers_handler(void * p_context)
  58. {
  59. UNUSED_PARAMETER(p_context);
  60. ble_timers_stat=false;
  61. // ret_code_t err_code ;
  62. // APP_ERROR_CHECK(err_code);
  63. }
  64. void ble_timers_init(void)
  65. {
  66. ret_code_t err_code = app_timer_create(&ble_timer_id,
  67. APP_TIMER_MODE_SINGLE_SHOT,
  68. ble_timers_handler);
  69. APP_ERROR_CHECK(err_code);
  70. }
  71. /**@brief 获取蓝牙Mac地址
  72. *
  73. * @details
  74. *
  75. */
  76. static void get_mac_addr(uint8_t *p_mac_addr)
  77. {
  78. uint32_t error_code;
  79. ble_gap_addr_t *p_mac_addr_t = (ble_gap_addr_t*)malloc(sizeof(ble_gap_addr_t));
  80. error_code = sd_ble_gap_addr_get(p_mac_addr_t);
  81. APP_ERROR_CHECK(error_code);
  82. uint8_t *d = p_mac_addr_t->addr;
  83. for ( uint8_t i = 6; i >0;)
  84. {
  85. i--;
  86. p_mac_addr[5-i]= d[i];
  87. }
  88. free(p_mac_addr_t);
  89. p_mac_addr_t = NULL;
  90. }
  91. //static void get_mac_addr(uint8_t *p_mac_addr)
  92. //{
  93. // uint32_t error_code;
  94. // ble_gap_addr_t *p_mac_addr_t = (ble_gap_addr_t*)malloc(sizeof(ble_gap_addr_t));
  95. // error_code = sd_ble_gap_addr_get(p_mac_addr_t);
  96. // APP_ERROR_CHECK(error_code);
  97. // p_mac_addr[0]=0xE6;
  98. // p_mac_addr[1]=0xAA;
  99. // p_mac_addr[2]=0xA8;
  100. // p_mac_addr[3]=0x38;
  101. // p_mac_addr[4]=0xD8;
  102. // p_mac_addr[5]=0x24;
  103. //
  104. // uint8_t *d = p_mac_addr_t->addr;
  105. // for ( uint8_t i = 6; i >0;)
  106. // {
  107. // i--;
  108. // d[i]=p_mac_addr[5-i];
  109. // }
  110. //
  111. // error_code = sd_ble_gap_addr_set(p_mac_addr_t);
  112. // APP_ERROR_CHECK(error_code);
  113. // free(p_mac_addr_t);
  114. // p_mac_addr_t = NULL;
  115. //}
  116. /**@brief Function for handling Queued Write Module errors.
  117. *
  118. * @details A pointer to this function will be passed to each service which may need to inform the
  119. * application about an error.
  120. *
  121. * @param[in] nrf_error Error code containing information about what went wrong.
  122. */
  123. static void nrf_qwr_error_handler(uint32_t nrf_error)
  124. {
  125. APP_ERROR_HANDLER(nrf_error);
  126. }
  127. /**@brief Function for initializing the GATT module.
  128. */
  129. static void gatt_init(void)
  130. {
  131. ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL);
  132. APP_ERROR_CHECK(err_code);
  133. }
  134. static void set_ble_stat_act(ble_stat_e e)
  135. {
  136. if(ble_user_func.ble_stat!=NULL)
  137. {
  138. ble_user_func.ble_stat(e);
  139. }
  140. }
  141. /**@brief 处理主机询问数据
  142. */
  143. static bool ble_owned_data_pro(ble_owned_t * p_owned, ble_evt_t const * p_ble_evt)
  144. {
  145. uint8_t len=0;
  146. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  147. NRF_LOG_HEXDUMP_INFO(p_evt_write->data,p_evt_write->len);
  148. if(ble_user_func.recieve_data!=NULL && p_evt_write->len>0)
  149. {
  150. bool stat=ble_user_func.recieve_data((uint8_t *)p_evt_write->data,p_evt_write->len);
  151. if(stat==true)
  152. {
  153. set_ble_stat_act(BLE_STAT_DATA_OK);
  154. }
  155. else{
  156. set_ble_stat_act(BLE_STAT_DATA_NOK);
  157. }
  158. return stat;
  159. }
  160. return false;
  161. }
  162. void ble_owned_data_send(uint8_t *data,uint16_t len)
  163. {
  164. if(m_owned.conn_handle!=BLE_CONN_HANDLE_INVALID)
  165. {
  166. ble_owned_indicate_data_chunk(&m_owned,data,len);
  167. }
  168. }
  169. //获取连接状态
  170. bool get_m_owned_conn_stat(void)
  171. {
  172. if(m_owned.conn_handle!=BLE_CONN_HANDLE_INVALID)
  173. {
  174. return true;
  175. }
  176. return false;
  177. }
  178. //蓝牙事件处理函数
  179. void ble_user_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  180. {
  181. ret_code_t err_code;
  182. switch (p_ble_evt->header.evt_id)
  183. {
  184. case BLE_GAP_EVT_CONNECTED:
  185. err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, p_ble_evt->evt.gattc_evt.conn_handle);
  186. APP_ERROR_CHECK(err_code);
  187. set_ble_stat_act(BLE_STAT_CONNECTED);
  188. break;
  189. case BLE_GAP_EVT_DISCONNECTED:
  190. set_ble_stat_act(BLE_STAT_DISCONNECTED);
  191. if(ble_user_func.ble_disconnect!=NULL)
  192. {
  193. ble_user_func.ble_disconnect();
  194. }
  195. break;
  196. }
  197. }
  198. // 蓝牙事件处理函数
  199. static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
  200. {
  201. if ((p_context == NULL) || (p_ble_evt == NULL))
  202. {
  203. return;
  204. }
  205. uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  206. uint16_t role = ble_conn_state_role(conn_handle);
  207. // Based on the role this device plays in the connection, dispatch to the right handler.
  208. if (role == BLE_GAP_ROLE_PERIPH)
  209. {
  210. ble_evt_t * evt=(ble_evt_t *)p_ble_evt;
  211. ble_owned_on_ble_evt(evt,p_context);
  212. ble_peripheral_on_ble_evt(evt,p_context);
  213. ble_user_on_ble_evt(evt,p_context);
  214. }
  215. #if NRF_MODULE_ENABLED(OWNED_C_SERVER)
  216. else if ((role == BLE_GAP_ROLE_CENTRAL) || (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_REPORT))
  217. {
  218. on_ble_central_evt(p_ble_evt);
  219. }
  220. #endif
  221. }
  222. /**@brief Function for initializing the BLE stack.
  223. *
  224. * @details Initializes the SoftDevice and the BLE event interrupt.
  225. */
  226. static void ble_stack_init(void)
  227. {
  228. ret_code_t err_code;
  229. //请求使能SoftDevice,该函数中会根据sdk_config.h文件中低频时钟的设置来配置低频时钟
  230. err_code = nrf_sdh_enable_request();
  231. APP_ERROR_CHECK(err_code);
  232. //定义保存应用程序RAM起始地址的变量
  233. uint32_t ram_start = 0;
  234. //使用sdk_config.h文件的默认参数配置协议栈,获取应用程序RAM起始地址,保存到变量ram_start
  235. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  236. APP_ERROR_CHECK(err_code);
  237. //使能BLE协议栈
  238. err_code = nrf_sdh_ble_enable(&ram_start);
  239. APP_ERROR_CHECK(err_code);
  240. //注册BLE事件回调函数
  241. NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  242. }
  243. #if NRF_MODULE_ENABLED(BLE_MY_UARTS)
  244. static void ble_uart_rx_data_pro(ble_uarts_evt_t * p_evt)
  245. {
  246. uint32_t err_code;
  247. // for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
  248. // {
  249. do
  250. {
  251. err_code = ble_uarts_data_send(&m_uarts, (uint8_t *)p_evt->params.rx_data.p_data, &p_evt->params.rx_data.length, *peripheral_conn_handle);
  252. if ((err_code != NRF_ERROR_INVALID_STATE) &&
  253. (err_code != NRF_ERROR_RESOURCES) &&
  254. (err_code != NRF_ERROR_NOT_FOUND))
  255. {
  256. APP_ERROR_CHECK(err_code);
  257. }
  258. }
  259. while (err_code == NRF_ERROR_RESOURCES);while (err_code == NRF_ERROR_BUSY);
  260. // }
  261. }
  262. //串口透传事件回调函数,串口透出服务初始化时注册
  263. static void uarts_data_handler(ble_uarts_evt_t * p_evt)
  264. {
  265. //判断事件类型:接收到新数据事件
  266. if (p_evt->type == BLE_UARTS_EVT_RX_DATA)
  267. {
  268. ble_uart_rx_data_pro(p_evt);
  269. }
  270. //判断事件类型:发送就绪事件,该事件在后面的试验会用到,当前我们在该事件中翻转指示灯D4的状态,指示该事件的产生
  271. if (p_evt->type == BLE_UARTS_EVT_TX_RDY)
  272. {
  273. }
  274. }
  275. #endif
  276. /**@brief Function for initializing services that will be used by the application.
  277. */
  278. static void services_init(void)
  279. {
  280. ret_code_t err_code;
  281. nrf_ble_qwr_init_t qwr_init = {0};
  282. // Initialize Queued Write Module.
  283. qwr_init.error_handler = nrf_qwr_error_handler;
  284. err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
  285. APP_ERROR_CHECK(err_code);
  286. #if NRF_MODULE_ENABLED(BLE_DFU)
  287. dfu_service_init();
  288. #endif
  289. get_mac_addr(m_addl_adv_manuf_data);
  290. #if WECHAT_SERVER_ENABLED==1
  291. err_code = ble_wechat_init(&m_wechat);
  292. APP_ERROR_CHECK(err_code);
  293. #endif
  294. #if OWNED_SERVER_ENABLED==1
  295. ble_owned_init_t p_owned_init;
  296. p_owned_init.owned_indicate_handler=ble_owned_data_pro;
  297. err_code = ble_owned_init(&m_owned,&p_owned_init);
  298. APP_ERROR_CHECK(err_code);
  299. #endif
  300. #if NRF_MODULE_ENABLED(BLE_MY_UARTS)
  301. //定义串口透传初始化结构体
  302. ble_uarts_init_t uarts_init;
  303. //清零串口透传服务初始化结构体
  304. memset(&uarts_init, 0, sizeof(uarts_init));
  305. //设置串口透传事件回调函数
  306. uarts_init.data_handler = uarts_data_handler;
  307. //初始化串口透传服务
  308. err_code = ble_uarts_init(&m_uarts, &uarts_init);
  309. APP_ERROR_CHECK(err_code);
  310. #endif
  311. }
  312. #if NRF_MODULE_ENABLED(BLE_DFU)
  313. static void set_dfu_act(bool stat)
  314. {
  315. if(stat==true)
  316. {
  317. if(ble_user_func.dfu_act!=NULL)
  318. {
  319. m_ready_for_reset=false;
  320. ble_user_func.dfu_act();
  321. }
  322. }
  323. }
  324. void return_dfu_act(bool stat)
  325. {
  326. m_ready_for_reset=true;
  327. NVIC_SystemReset();
  328. }
  329. #endif
  330. #if NRF_MODULE_ENABLED(OWNED_C_SERVER)
  331. //#include "ble_upload.h"
  332. #include "action.h"
  333. void ble_central_wr_data_pro(uint8_t *data,uint16_t len)
  334. {
  335. Recieve_Dev_Data(data,len);
  336. }
  337. bool ble_scan_adv_data_pro(const ble_gap_evt_adv_report_t * p_adv_report)
  338. {
  339. Set_Connect_MacAddr(p_adv_report);
  340. }
  341. void ble_scan_timeout_pro(void)
  342. {
  343. Set_Scan_Timeout();
  344. }
  345. void ble_scan_connect_pro(bool stat)
  346. {
  347. Set_Connected_Act(stat);
  348. }
  349. #endif
  350. void set_ble_recieve_data_evtfunc(ble_recieve_data_func func)
  351. {
  352. ble_user_func.recieve_data=func;
  353. }
  354. void set_ble_dfu_act_evtfunc(ble_dfu_act_func func)
  355. {
  356. ble_user_func.dfu_act=func;
  357. }
  358. void set_ble_stat_evtfunc(ble_stat_func func)
  359. {
  360. ble_user_func.ble_stat=func;
  361. }
  362. void set_ble_disconnect_evtfunc(ble_disconnect_func func)
  363. {
  364. ble_user_func.ble_disconnect=func;
  365. }
  366. void Ble_User_Init(void)
  367. {
  368. ret_code_t err_code;
  369. ble_stack_init();
  370. gatt_init();
  371. services_init();
  372. #if NRF_MODULE_ENABLED(BLE_DFU)
  373. Ble_Peripheral_Init(set_dfu_act,&peripheral_conn_handle);
  374. #else
  375. Ble_Peripheral_Init(NULL,&peripheral_conn_handle);
  376. #endif
  377. #if NRF_MODULE_ENABLED(OWNED_C_SERVER)
  378. Ble_Central_Set_Wr_Evt_Handler(ble_central_wr_data_pro);
  379. Ble_Central_Set_Scan_Adv_Evt_Handler(ble_scan_adv_data_pro);
  380. Ble_Central_Set_Scan_Timeout_Evt_Handler(ble_scan_timeout_pro);
  381. Ble_Central_Set_Scan_Connect_Evt_Handler(ble_scan_connect_pro);
  382. Ble_Central_Init();
  383. #endif
  384. }