123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- #include "ble_user.h"
- #include "ble_peripheral.h"
- #if NRF_MODULE_ENABLED(OWNED_C_SERVER)
- #include "ble_central.h"
- #endif
- //DFU需要引用的头文件
- #if NRF_MODULE_ENABLED(BLE_DFU)
- #include "nrf_power.h"
- #include "nrf_bootloader_info.h"
- #include "ble_dfu.h"
- #include "nrf_dfu_ble_svci_bond_sharing.h"
- #include "nrf_svci_async_function.h"
- #include "nrf_svci_async_handler.h"
- #endif
- #if NRF_MODULE_ENABLED(BLE_MY_UARTS)
- #include "my_ble_uarts.h"
- BLE_UARTS_DEF(m_uarts); //定义名称为m_uarts的串口透传服务实例
- #endif
- NRF_BLE_GATT_DEF(m_gatt); /**< ************* 定义名称为m_gatt的GATT模块实例 *************/
- NRF_BLE_QWR_DEF(m_qwr); /**< *********** 定义一个名称为m_qwr的排队写入实例 ************/
- #if WECHAT_SERVER_ENABLED==1
- BLE_WECHAT_DEF(m_wechat); /**< ******************** WECHAT instance. ********************/
- #endif
- static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context);
- #if OWNED_SERVER_ENABLED==1
- BLE_OWNED_DEF(m_owned); /**< ***************** Owned server instance. *****************/
- #endif
- static uint16_t *peripheral_conn_handle; /**< ************ Handle of the current connection. ***********/
- static ble_user_func_s ble_user_func={
- .recieve_data=NULL,
- .dfu_act=NULL,
- .ble_stat=NULL,
- };
- uint8_t m_addl_adv_manuf_data[BLE_GAP_ADDR_LEN]={0};
- APP_TIMER_DEF(ble_timer_id);
- static bool ble_timers_stat=false;
- //启动毫秒计时应用定时器。启动后,即开始产生毫秒计时,也就是提供了一个以毫秒为单位的计时基准
- static void ble_timers_stop()
- {
- if(ble_timers_stat==true)
- {
- //启动APP定时器
- ret_code_t err_code = app_timer_stop(ble_timer_id);
- APP_ERROR_CHECK(err_code);
- ble_timers_stat=false;
- }
- }
- //启动毫秒计时应用定时器。启动后,即开始产生毫秒计时,也就是提供了一个以毫秒为单位的计时基准
- static void ble_timers_start(uint32_t ms)
- {
- ble_timers_stop();
- //启动APP定时器
- ret_code_t err_code = app_timer_start(ble_timer_id, APP_TIMER_TICKS(ms), NULL);
- APP_ERROR_CHECK(err_code);
- ble_timers_stat=true;
- }
- static void ble_timers_handler(void * p_context)
- {
- UNUSED_PARAMETER(p_context);
- ble_timers_stat=false;
- // ret_code_t err_code ;
- // APP_ERROR_CHECK(err_code);
- }
- void ble_timers_init(void)
- {
- ret_code_t err_code = app_timer_create(&ble_timer_id,
- APP_TIMER_MODE_SINGLE_SHOT,
- ble_timers_handler);
- APP_ERROR_CHECK(err_code);
- }
- /**@brief 获取蓝牙Mac地址
- *
- * @details
- *
- */
- static void get_mac_addr(uint8_t *p_mac_addr)
- {
- uint32_t error_code;
- ble_gap_addr_t *p_mac_addr_t = (ble_gap_addr_t*)malloc(sizeof(ble_gap_addr_t));
- error_code = sd_ble_gap_addr_get(p_mac_addr_t);
- APP_ERROR_CHECK(error_code);
- uint8_t *d = p_mac_addr_t->addr;
- for ( uint8_t i = 6; i >0;)
- {
- i--;
- p_mac_addr[5-i]= d[i];
- }
- free(p_mac_addr_t);
- p_mac_addr_t = NULL;
- }
- //static void get_mac_addr(uint8_t *p_mac_addr)
- //{
- // uint32_t error_code;
- // ble_gap_addr_t *p_mac_addr_t = (ble_gap_addr_t*)malloc(sizeof(ble_gap_addr_t));
- // error_code = sd_ble_gap_addr_get(p_mac_addr_t);
- // APP_ERROR_CHECK(error_code);
- // p_mac_addr[0]=0xE6;
- // p_mac_addr[1]=0xAA;
- // p_mac_addr[2]=0xA8;
- // p_mac_addr[3]=0x38;
- // p_mac_addr[4]=0xD8;
- // p_mac_addr[5]=0x24;
- //
- // uint8_t *d = p_mac_addr_t->addr;
- // for ( uint8_t i = 6; i >0;)
- // {
- // i--;
- // d[i]=p_mac_addr[5-i];
- // }
- //
- // error_code = sd_ble_gap_addr_set(p_mac_addr_t);
- // APP_ERROR_CHECK(error_code);
- // free(p_mac_addr_t);
- // p_mac_addr_t = NULL;
- //}
- /**@brief Function for handling Queued Write Module errors.
- *
- * @details A pointer to this function will be passed to each service which may need to inform the
- * application about an error.
- *
- * @param[in] nrf_error Error code containing information about what went wrong.
- */
- static void nrf_qwr_error_handler(uint32_t nrf_error)
- {
- APP_ERROR_HANDLER(nrf_error);
- }
- /**@brief Function for initializing the GATT module.
- */
- static void gatt_init(void)
- {
- ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL);
- APP_ERROR_CHECK(err_code);
- }
- static void set_ble_stat_act(ble_stat_e e)
- {
- if(ble_user_func.ble_stat!=NULL)
- {
- ble_user_func.ble_stat(e);
- }
- }
- /**@brief 处理主机询问数据
- */
- static bool ble_owned_data_pro(ble_owned_t * p_owned, ble_evt_t const * p_ble_evt)
- {
- uint8_t len=0;
- ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
- NRF_LOG_HEXDUMP_INFO(p_evt_write->data,p_evt_write->len);
- if(ble_user_func.recieve_data!=NULL && p_evt_write->len>0)
- {
- bool stat=ble_user_func.recieve_data((uint8_t *)p_evt_write->data,p_evt_write->len);
- if(stat==true)
- {
- set_ble_stat_act(BLE_STAT_DATA_OK);
- }
- else{
- set_ble_stat_act(BLE_STAT_DATA_NOK);
- }
- return stat;
- }
- return false;
- }
- void ble_owned_data_send(uint8_t *data,uint16_t len)
- {
- if(m_owned.conn_handle!=BLE_CONN_HANDLE_INVALID)
- {
- ble_owned_indicate_data_chunk(&m_owned,data,len);
- }
- }
- //获取连接状态
- bool get_m_owned_conn_stat(void)
- {
- if(m_owned.conn_handle!=BLE_CONN_HANDLE_INVALID)
- {
- return true;
- }
- return false;
- }
- //蓝牙事件处理函数
- void ble_user_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
- {
- ret_code_t err_code;
-
- switch (p_ble_evt->header.evt_id)
- {
- case BLE_GAP_EVT_CONNECTED:
-
- err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, p_ble_evt->evt.gattc_evt.conn_handle);
- APP_ERROR_CHECK(err_code);
- set_ble_stat_act(BLE_STAT_CONNECTED);
- break;
- case BLE_GAP_EVT_DISCONNECTED:
- set_ble_stat_act(BLE_STAT_DISCONNECTED);
- if(ble_user_func.ble_disconnect!=NULL)
- {
- ble_user_func.ble_disconnect();
- }
- break;
- }
- }
- // 蓝牙事件处理函数
- static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
- {
- if ((p_context == NULL) || (p_ble_evt == NULL))
- {
- return;
- }
-
- uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
- uint16_t role = ble_conn_state_role(conn_handle);
-
- // Based on the role this device plays in the connection, dispatch to the right handler.
- if (role == BLE_GAP_ROLE_PERIPH)
- {
- ble_evt_t * evt=(ble_evt_t *)p_ble_evt;
- ble_owned_on_ble_evt(evt,p_context);
- ble_peripheral_on_ble_evt(evt,p_context);
- ble_user_on_ble_evt(evt,p_context);
- }
- #if NRF_MODULE_ENABLED(OWNED_C_SERVER)
- else if ((role == BLE_GAP_ROLE_CENTRAL) || (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_REPORT))
- {
- on_ble_central_evt(p_ble_evt);
- }
- #endif
- }
- /**@brief Function for initializing the BLE stack.
- *
- * @details Initializes the SoftDevice and the BLE event interrupt.
- */
- static void ble_stack_init(void)
- {
- ret_code_t err_code;
- //请求使能SoftDevice,该函数中会根据sdk_config.h文件中低频时钟的设置来配置低频时钟
- err_code = nrf_sdh_enable_request();
- APP_ERROR_CHECK(err_code);
-
- //定义保存应用程序RAM起始地址的变量
- uint32_t ram_start = 0;
- //使用sdk_config.h文件的默认参数配置协议栈,获取应用程序RAM起始地址,保存到变量ram_start
- err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
- APP_ERROR_CHECK(err_code);
- //使能BLE协议栈
- err_code = nrf_sdh_ble_enable(&ram_start);
- APP_ERROR_CHECK(err_code);
- //注册BLE事件回调函数
- NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
- }
- #if NRF_MODULE_ENABLED(BLE_MY_UARTS)
- static void ble_uart_rx_data_pro(ble_uarts_evt_t * p_evt)
- {
- uint32_t err_code;
- // for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
- // {
- do
- {
- 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);
- if ((err_code != NRF_ERROR_INVALID_STATE) &&
- (err_code != NRF_ERROR_RESOURCES) &&
- (err_code != NRF_ERROR_NOT_FOUND))
- {
- APP_ERROR_CHECK(err_code);
- }
- }
- while (err_code == NRF_ERROR_RESOURCES);while (err_code == NRF_ERROR_BUSY);
- // }
- }
- //串口透传事件回调函数,串口透出服务初始化时注册
- static void uarts_data_handler(ble_uarts_evt_t * p_evt)
- {
- //判断事件类型:接收到新数据事件
- if (p_evt->type == BLE_UARTS_EVT_RX_DATA)
- {
- ble_uart_rx_data_pro(p_evt);
- }
- //判断事件类型:发送就绪事件,该事件在后面的试验会用到,当前我们在该事件中翻转指示灯D4的状态,指示该事件的产生
- if (p_evt->type == BLE_UARTS_EVT_TX_RDY)
- {
-
- }
- }
- #endif
- /**@brief Function for initializing services that will be used by the application.
- */
- static void services_init(void)
- {
- ret_code_t err_code;
- nrf_ble_qwr_init_t qwr_init = {0};
- // Initialize Queued Write Module.
- qwr_init.error_handler = nrf_qwr_error_handler;
- err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
- APP_ERROR_CHECK(err_code);
-
- #if NRF_MODULE_ENABLED(BLE_DFU)
- dfu_service_init();
- #endif
- get_mac_addr(m_addl_adv_manuf_data);
-
- #if WECHAT_SERVER_ENABLED==1
- err_code = ble_wechat_init(&m_wechat);
- APP_ERROR_CHECK(err_code);
- #endif
-
- #if OWNED_SERVER_ENABLED==1
- ble_owned_init_t p_owned_init;
- p_owned_init.owned_indicate_handler=ble_owned_data_pro;
- err_code = ble_owned_init(&m_owned,&p_owned_init);
- APP_ERROR_CHECK(err_code);
- #endif
- #if NRF_MODULE_ENABLED(BLE_MY_UARTS)
- //定义串口透传初始化结构体
- ble_uarts_init_t uarts_init;
- //清零串口透传服务初始化结构体
- memset(&uarts_init, 0, sizeof(uarts_init));
- //设置串口透传事件回调函数
- uarts_init.data_handler = uarts_data_handler;
- //初始化串口透传服务
- err_code = ble_uarts_init(&m_uarts, &uarts_init);
- APP_ERROR_CHECK(err_code);
- #endif
- }
- #if NRF_MODULE_ENABLED(BLE_DFU)
- static void set_dfu_act(bool stat)
- {
- if(stat==true)
- {
- if(ble_user_func.dfu_act!=NULL)
- {
- m_ready_for_reset=false;
- ble_user_func.dfu_act();
- }
- }
- }
- void return_dfu_act(bool stat)
- {
- m_ready_for_reset=true;
- NVIC_SystemReset();
- }
- #endif
- #if NRF_MODULE_ENABLED(OWNED_C_SERVER)
- //#include "ble_upload.h"
- #include "action.h"
- void ble_central_wr_data_pro(uint8_t *data,uint16_t len)
- {
- Recieve_Dev_Data(data,len);
- }
- bool ble_scan_adv_data_pro(const ble_gap_evt_adv_report_t * p_adv_report)
- {
- Set_Connect_MacAddr(p_adv_report);
- }
- void ble_scan_timeout_pro(void)
- {
- Set_Scan_Timeout();
- }
- void ble_scan_connect_pro(bool stat)
- {
- Set_Connected_Act(stat);
- }
- #endif
- void set_ble_recieve_data_evtfunc(ble_recieve_data_func func)
- {
- ble_user_func.recieve_data=func;
- }
- void set_ble_dfu_act_evtfunc(ble_dfu_act_func func)
- {
- ble_user_func.dfu_act=func;
- }
- void set_ble_stat_evtfunc(ble_stat_func func)
- {
- ble_user_func.ble_stat=func;
- }
- void set_ble_disconnect_evtfunc(ble_disconnect_func func)
- {
- ble_user_func.ble_disconnect=func;
- }
- void Ble_User_Init(void)
- {
- ret_code_t err_code;
-
- ble_stack_init();
- gatt_init();
- services_init();
- #if NRF_MODULE_ENABLED(BLE_DFU)
- Ble_Peripheral_Init(set_dfu_act,&peripheral_conn_handle);
- #else
- Ble_Peripheral_Init(NULL,&peripheral_conn_handle);
- #endif
-
- #if NRF_MODULE_ENABLED(OWNED_C_SERVER)
- Ble_Central_Set_Wr_Evt_Handler(ble_central_wr_data_pro);
- Ble_Central_Set_Scan_Adv_Evt_Handler(ble_scan_adv_data_pro);
- Ble_Central_Set_Scan_Timeout_Evt_Handler(ble_scan_timeout_pro);
- Ble_Central_Set_Scan_Connect_Evt_Handler(ble_scan_connect_pro);
- Ble_Central_Init();
- #endif
- }
|