123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996 |
- #include "main.h"
- #include "bsp_time.h"
- #include "hal_ble_client.h"
- #include "hal_ble_host.h"
- static uint8_t isHostConnet = 0; //本机主机
- static uint8_t isClientConnet = 0; //本机从机
- //#if IS_HOST
- // char my_name[] = "long_S0";
- //#else
- // char my_name[] = "long_SS0";
- //#endif
- #define APP_BLE_CONN_CFG_TAG 1 /**< Tag that refers to the BLE stack configuration set with @ref sd_ble_cfg_set. The default tag is @ref BLE_CONN_CFG_TAG_DEFAULT. */
- #define APP_BLE_OBSERVER_PRIO 3 /**< BLE observer priority of the application. There is no need to modify this value. */
- #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
- #define ECHOBACK_BLE_UART_DATA 1 /**< Echo the UART data that is received over the Nordic UART Service (NUS) back to the sender. */
- BLE_NUS_C_DEF(m_ble_nus_c); /**< BLE Nordic UART Service (NUS) client instance. */
- NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
- BLE_DB_DISCOVERY_DEF(m_db_disc); /**< Database discovery module instance. */
- NRF_BLE_SCAN_DEF(m_scan); /**< Scanning Module instance. */
- NRF_BLE_GQ_DEF(m_ble_gatt_queue, /**< BLE GATT Queue instance. */
- NRF_SDH_BLE_CENTRAL_LINK_COUNT,
- NRF_BLE_GQ_QUEUE_SIZE);
- static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH; /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
- /**@brief Function for handling asserts in the SoftDevice.
- *
- * @details This function is called in case of an assert in the SoftDevice.
- *
- * @warning This handler is only an example and is not meant for the final product. You need to analyze
- * how your product is supposed to react in case of assert.
- * @warning On assert from the SoftDevice, the system can only recover on reset.
- *
- * @param[in] line_num Line number of the failing assert call.
- * @param[in] p_file_name File name of the failing assert call.
- */
- void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
- {
- app_error_handler(0xDEADBEEF, line_num, p_file_name);
- }
- /**@brief Function for handling the Nordic UART Service Client errors.
- *
- * @param[in] nrf_error Error code containing information about what went wrong.
- */
- static void nus_error_handler(uint32_t nrf_error)
- {
- //APP_ERROR_HANDLER(nrf_error);
- }
- /**@brief Function to start scanning. */
- static void scan_start(void)
- {
- ret_code_t ret;
- ret = nrf_ble_scan_start(&m_scan);
- APP_ERROR_CHECK(ret);
- ret = bsp_indication_set(BSP_INDICATE_SCANNING);
- APP_ERROR_CHECK(ret);
- // SEGGER_RTT_printf(0,"scan_start -> scan_name <%s> \n",HOST_NAME);
- }
- /**@brief Function to stop scanning. */
- //static void scan_stop(void)
- //{
- // nrf_ble_scan_stop();
- //}
- #define PRINT_MAC 0
- /**@brief Function for handling Scanning Module events.
- */
- static void scan_evt_handler(scan_evt_t const * p_scan_evt)
- {
- ret_code_t err_code;
- switch(p_scan_evt->scan_evt_id)
- {
- case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
- {
- err_code = p_scan_evt->params.connecting_err.err_code;
- APP_ERROR_CHECK(err_code);
- SEGGER_RTT_printf(0,"scan_evt_handler -> NRF_BLE_SCAN_EVT_CONNECTING_ERROR \n");
- } break;
- case NRF_BLE_SCAN_EVT_CONNECTED:
- {
- SEGGER_RTT_printf(0,"scan_evt_handler -> NRF_BLE_SCAN_EVT_CONNECTED\n");
-
- ble_gap_evt_connected_t const * p_connected = p_scan_evt->params.connected.p_connected;
- // Scan is automatically stopped by the connection.
- SEGGER_RTT_printf(0,"mac:%02x %02x %02x %02x %02x %02x\r\n",
- p_connected->peer_addr.addr[5],
- p_connected->peer_addr.addr[4],
- p_connected->peer_addr.addr[3],
- p_connected->peer_addr.addr[2],
- p_connected->peer_addr.addr[1],
- p_connected->peer_addr.addr[0]
- );
- } break;
- case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
- {
- SEGGER_RTT_printf(0,"scan_evt_handler -> NRF_BLE_SCAN_EVT_SCAN_TIMEOUT -> Scan timed out.\n");
- scan_start();
- } break;
-
- case NRF_BLE_SCAN_EVT_FILTER_MATCH:
- {
- SEGGER_RTT_printf(0,"scan_evt_handler -> NRF_BLE_SCAN_EVT_FILTER_MATCH\n");
- } break;
-
- case NRF_BLE_SCAN_EVT_WHITELIST_REQUEST:
- {
- SEGGER_RTT_printf(0,"scan_evt_handler -> NRF_BLE_SCAN_EVT_WHITELIST_REQUEST\n");
- } break;
-
- case NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT:
- {
- SEGGER_RTT_printf(0,"scan_evt_handler -> NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT\n");
- } break;
-
- case NRF_BLE_SCAN_EVT_NOT_FOUND:
- {
- // SEGGER_RTT_printf(0,"scan_evt_handler -> NRF_BLE_SCAN_EVT_NOT_FOUND");
- } break;
-
- default:SEGGER_RTT_printf(0,"scan_evt_handler -> default:%d \n",p_scan_evt->scan_evt_id);
- break;
- }
- }
- /**@brief Function for initializing the scanning and setting the filters.
- */
- static void scan_init(void)
- {
- ret_code_t err_code;
- nrf_ble_scan_init_t init_scan;
- memset(&init_scan, 0, sizeof(init_scan));
- init_scan.connect_if_match = true;
- init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
- err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
- APP_ERROR_CHECK(err_code);
- err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, HOST_NAME);
- APP_ERROR_CHECK(err_code);
- err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_NAME_FILTER, false);
- APP_ERROR_CHECK(err_code);
- }
- /**@brief Function for handling database discovery events.
- *
- * @details This function is a callback function to handle events from the database discovery module.
- * Depending on the UUIDs that are discovered, this function forwards the events
- * to their respective services.
- *
- * @param[in] p_event Pointer to the database discovery event.
- */
- static void db_disc_handler(ble_db_discovery_evt_t * p_evt)
- {
- ble_nus_c_on_db_disc_evt(&m_ble_nus_c, p_evt);
- }
- /**@brief Function for handling app_uart events.
- *
- * @details This function receives a single character from the app_uart module and appends it to
- * a string. The string is sent over BLE when the last character received is a
- * 'new line' '\n' (hex 0x0A) or if the string reaches the maximum data length.
- */
- //void uart_event_handle(app_uart_evt_t * p_event)
- //{
- // unsigned char byte;
- // switch (p_event->evt_type)
- // {
- // /**@snippet [Handling data from UART] */
- // case APP_UART_DATA_READY:
- // app_uart_get(&byte);
- // break;
- // /**@snippet [Handling data from UART] */
- // case APP_UART_COMMUNICATION_ERROR:
- // //APP_ERROR_HANDLER(p_event->data.error_communication);
- // break;
- // case APP_UART_FIFO_ERROR:
- // //APP_ERROR_HANDLER(p_event->data.error_code);
- // break;
- // default:
- // break;
- // }
- //}
- /**@brief Callback handling Nordic UART Service (NUS) client events.
- *
- * @details This function is called to notify the application of NUS client events.
- *
- * @param[in] p_ble_nus_c NUS client handle. This identifies the NUS client.
- * @param[in] p_ble_nus_evt Pointer to the NUS client event.
- */
- /**@snippet [Handling events from the ble_nus_c module] */
- static void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, ble_nus_c_evt_t const * p_ble_nus_evt)
- {
- ret_code_t err_code;
- switch (p_ble_nus_evt->evt_type)
- {
- case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
- printf("Discovery complete.\n");
- err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles);
- APP_ERROR_CHECK(err_code);
- err_code = ble_nus_c_tx_notif_enable(p_ble_nus_c);
- APP_ERROR_CHECK(err_code);
- printf("Connected to device with Nordic UART Service.\n");
- break;
- case BLE_NUS_C_EVT_NUS_TX_EVT://作为主机接收从机的数据
- BLE_Host_Push(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
- //ble_receive_from_server(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
- break;
- case BLE_NUS_C_EVT_DISCONNECTED:
- printf("Disconnected.\n");
- scan_start();
- break;
- }
- }
- /**@snippet [Handling events from the ble_nus_c module] */
- int send_bytes_server(uint8_t * bytes, uint16_t len)
- {
- if(isHostConnet==0) return -1;
- if(NRF_SUCCESS != ble_nus_c_string_send(&m_ble_nus_c,bytes,len)) {
- SEGGER_RTT_printf(0,"send_bytes_server -> fail\r\n");
- return -1;
- }
- return 0;
- }
- /**
- * @brief Function for handling shutdown events.
- *
- * @param[in] event Shutdown type.
- */
- static bool shutdown_handler(nrf_pwr_mgmt_evt_t event)
- {
- ret_code_t err_code;
- err_code = bsp_indication_set(BSP_INDICATE_IDLE);
- APP_ERROR_CHECK(err_code);
- switch (event)
- {
- case NRF_PWR_MGMT_EVT_PREPARE_WAKEUP:
- // Prepare wakeup buttons.
- err_code = bsp_btn_ble_sleep_mode_prepare();
- APP_ERROR_CHECK(err_code);
- break;
- default:
- break;
- }
- return true;
- }
- NRF_PWR_MGMT_HANDLER_REGISTER(shutdown_handler, APP_SHUTDOWN_HANDLER_PRIORITY);
- /**@brief Function for handling BLE events.
- *
- * @param[in] p_ble_evt Bluetooth stack event.
- * @param[in] p_context Unused.
- */
- static void on_ble_central_evt(ble_evt_t const * p_ble_evt, void * p_context)//作为主设备时的处理
- {
- ret_code_t err_code;
- ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
-
- switch (p_ble_evt->header.evt_id)
- {
- case BLE_GAP_EVT_CONNECTED:
- err_code = ble_nus_c_handles_assign(&m_ble_nus_c, p_ble_evt->evt.gap_evt.conn_handle, NULL);
- APP_ERROR_CHECK(err_code);
- err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
- APP_ERROR_CHECK(err_code);
- SEGGER_RTT_printf(0,"start discovery services\n");//添加开始发现服务提示
- APP_ERROR_CHECK(err_code);
-
- // start discovery of services. The NUS Client waits for a discovery result
- err_code = ble_db_discovery_start(&m_db_disc, p_ble_evt->evt.gap_evt.conn_handle);
-
- APP_ERROR_CHECK(err_code);
- isHostConnet = 1;
- break;
- case BLE_GAP_EVT_DISCONNECTED:
- isHostConnet = 0;
- SEGGER_RTT_printf(0,"Disconnected. conn_handle: 0x%x, reason: 0x%x",
- p_gap_evt->conn_handle,
- p_gap_evt->params.disconnected.reason);
- printf("Disconnected to Server.\n");
- break;
- case BLE_GAP_EVT_TIMEOUT:
- if (p_gap_evt->params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
- {
- SEGGER_RTT_printf(0,"Connection Request timed out.\n");
- }
- break;
- case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
- // Pairing not supported.
- err_code = sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
- APP_ERROR_CHECK(err_code);
- SEGGER_RTT_printf(0,"on_ble_central_evt -> BLE_GAP_EVT_SEC_PARAMS_REQUEST\n");
- break;
- case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
- // Accepting parameters requested by peer.
- err_code = sd_ble_gap_conn_param_update(p_gap_evt->conn_handle,
- &p_gap_evt->params.conn_param_update_request.conn_params);//主机接受从机连接参数更新连接参数
- APP_ERROR_CHECK(err_code);
- SEGGER_RTT_printf(0,"on_ble_central_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST\n");
- break;
-
- case BLE_GAP_EVT_CONN_PARAM_UPDATE:
- SEGGER_RTT_printf(0,"on_ble_central_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE\n");
- SEGGER_RTT_printf(0,"min_conn_interval : %d\n",p_gap_evt->params.conn_param_update_request.conn_params.min_conn_interval);
- SEGGER_RTT_printf(0,"max_conn_interval : %d\n",p_gap_evt->params.conn_param_update_request.conn_params.max_conn_interval);
- SEGGER_RTT_printf(0,"slave_latency : %d\n",p_gap_evt->params.conn_param_update_request.conn_params.slave_latency);
- SEGGER_RTT_printf(0,"conn_sup_timeout : %d\n",p_gap_evt->params.conn_param_update_request.conn_params.conn_sup_timeout);
- break;
-
- case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
- {
- NRF_LOG_DEBUG("PHY update request.\n");
- ble_gap_phys_t const phys =
- {
- .rx_phys = BLE_GAP_PHY_AUTO,
- .tx_phys = BLE_GAP_PHY_AUTO,
- };
- err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
- APP_ERROR_CHECK(err_code);
- } break;
- case BLE_GATTC_EVT_TIMEOUT:
- // Disconnect on GATT Client timeout event.
- NRF_LOG_DEBUG("GATT Client Timeout.\n");
- err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
- BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
- APP_ERROR_CHECK(err_code);
- break;
- case BLE_GATTS_EVT_TIMEOUT:
- // Disconnect on GATT Server timeout event.
- NRF_LOG_DEBUG("GATT Server Timeout.\n");
- err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
- BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
- APP_ERROR_CHECK(err_code);
- break;
-
- default:
- break;
- }
- }
- /**@brief Function for handling events from the GATT library. */
- void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
- {
- if (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED)
- {
- printf("ATT MTU exchange completed.\r\n");
- m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
- printf("Ble NUS max data length set to 0x%X(%d)\r\n", m_ble_nus_max_data_len, m_ble_nus_max_data_len);
- }
- }
- /**@brief Function for initializing the GATT library. */
- void gatt_init(void)
- {
- ret_code_t err_code;
- err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
- APP_ERROR_CHECK(err_code);
- err_code = nrf_ble_gatt_att_mtu_central_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
- APP_ERROR_CHECK(err_code);
- }
- /**@brief Function for handling events from the BSP module.
- *
- * @param[in] event Event generated by button press.
- */
- void bsp_event_handler(bsp_event_t event)
- {
- ret_code_t err_code;
- switch (event)
- {
- case BSP_EVENT_SLEEP:
- nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
- break;
- case BSP_EVENT_DISCONNECT:
- err_code = sd_ble_gap_disconnect(m_ble_nus_c.conn_handle,
- BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
- if (err_code != NRF_ERROR_INVALID_STATE)
- {
- APP_ERROR_CHECK(err_code);
- }
- break;
- default:
- break;
- }
- }
- /**@brief Function for initializing the UART. */
- //static void uart_init(void)
- //{
- // #ifdef USE_REGISTER_UART
- // bsp_uart_init(ble_receive_from_uart);
- // #else
- // ret_code_t err_code;
- // app_uart_comm_params_t const comm_params =
- // {
- // .rx_pin_no = PIN_RXD_BLE,
- // .tx_pin_no = PIN_TXD_BLE,
- // .rts_pin_no = RTS_PIN_NUMBER,
- // .cts_pin_no = CTS_PIN_NUMBER,
- // .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
- // .use_parity = false,
- // .baud_rate = UART_BAUDRATE_BAUDRATE_Baud115200
- // };
- // APP_UART_FIFO_INIT(&comm_params,
- // UART_RX_BUF_SIZE,
- // UART_TX_BUF_SIZE,
- // uart_event_handle,
- // APP_IRQ_PRIORITY_LOWEST,
- // err_code);
- // APP_ERROR_CHECK(err_code);
- // #endif
- //}
- /**@brief Function for initializing the Nordic UART Service (NUS) client. */
- static void nus_c_init(void)
- {
- ret_code_t err_code;
- ble_nus_c_init_t init;
- init.evt_handler = ble_nus_c_evt_handler;
- init.error_handler = nus_error_handler;
- init.p_gatt_queue = &m_ble_gatt_queue;
- err_code = ble_nus_c_init(&m_ble_nus_c, &init);
- APP_ERROR_CHECK(err_code);
- }
- /**@brief Function for initializing buttons and leds. */
- //static void buttons_leds_init(void)
- //{
- // ret_code_t err_code;
- // bsp_event_t startup_event;
- // err_code = bsp_init(BSP_INIT_LEDS, bsp_event_handler);
- // APP_ERROR_CHECK(err_code);
- // err_code = bsp_btn_ble_init(NULL, &startup_event);
- // APP_ERROR_CHECK(err_code);
- //}
- /**@brief Function for initializing the timer. */
- static void timer_init(void)
- { ret_code_t err_code = app_timer_init();
- APP_ERROR_CHECK(err_code);
- }
- /**@brief Function for initializing the nrf log module. */
- //static void log_init(void)
- //{
- // ret_code_t err_code = NRF_LOG_INIT(NULL);
- // APP_ERROR_CHECK(err_code);
- // NRF_LOG_DEFAULT_BACKENDS_INIT();
- //}
- /**@brief Function for initializing power management.
- */
- static void power_management_init(void)
- {
- ret_code_t err_code;
- err_code = nrf_pwr_mgmt_init();
- APP_ERROR_CHECK(err_code);
- }
- /** @brief Function for initializing the database discovery module. */
- static void db_discovery_init(void)
- {
- ble_db_discovery_init_t db_init;
- memset(&db_init, 0, sizeof(ble_db_discovery_init_t));
- db_init.evt_handler = db_disc_handler;
- db_init.p_gatt_queue = &m_ble_gatt_queue;
- ret_code_t err_code = ble_db_discovery_init(&db_init);
- APP_ERROR_CHECK(err_code);
- }
- /**@brief Function for handling the idle state (main loop).
- *
- * @details Handles any pending log operations, then sleeps until the next event occurs.
- */
- //static void idle_state_handle(void)
- //{
- // if (NRF_LOG_PROCESS() == false)
- // {
- // nrf_pwr_mgmt_run();
- // }
- //}
- //********************************* 从机初始化 *********************************//
- #include "ble_advertising.h"
- #include "ble_conn_params.h"
- #include "nrf_ble_qwr.h"
- #include "nrf_fstorage.h"
- #include "nrf_soc.h"
- #include "ble_nus.h"
- #define APP_ADV_INTERVAL 300 /**< The advertising interval (in units of 0.625 ms). This value corresponds to 187.5 ms. */
- #define APP_ADV_DURATION 18000 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
- #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(1000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
- #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
- #define MAX_CONN_PARAMS_UPDATE_COUNT 1
- //#if IS_HOST
- // #define DEVICE_NAME "00_test"
- //#else
- // #define DEVICE_NAME "long_S0"
- //#endif
-
- //#define DEVICE_NAME "00_test"
- #define MIN_CONN_INTERVAL MSEC_TO_UNITS(20, UNIT_1_25_MS) /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
- #define MAX_CONN_INTERVAL MSEC_TO_UNITS(75, UNIT_1_25_MS) /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
- #define SLAVE_LATENCY 0 /**< Slave latency. */
- #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)
- static ble_uuid_t m_adv_uuids[] = /**< Universally unique service identifier. */
- {
- {BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}
- };
- BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);
- BLE_ADVERTISING_DEF(m_advertising);
- NRF_BLE_QWRS_DEF(m_qwr, NRF_SDH_BLE_TOTAL_LINK_COUNT);
- uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID;
- static void on_adv_evt(ble_adv_evt_t ble_adv_evt)//广播事件
- {
- switch (ble_adv_evt)
- {
- case BLE_ADV_EVT_FAST:
- {
- SEGGER_RTT_printf(0,"Fast advertising.\n");
- } break;
- case BLE_ADV_EVT_IDLE:
- {
- SEGGER_RTT_printf(0,"on_adv_evt->BLE_ADV_EVT_IDLE\n");
- ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);//开始广播
- APP_ERROR_CHECK(err_code);
- } break;
- default:
- // No implementation needed.
- break;
- }
- }
- static void multi_qwr_conn_handle_assign(uint16_t conn_handle)
- {
- for (uint32_t i = 0; i < NRF_SDH_BLE_TOTAL_LINK_COUNT; i++)
- {
- if (m_qwr[i].conn_handle == BLE_CONN_HANDLE_INVALID)
- {
- ret_code_t err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr[i], conn_handle);
- APP_ERROR_CHECK(err_code);
- break;
- }
- }
- }
- static void on_ble_peripheral_evt(ble_evt_t const * p_ble_evt)//作为从设备的处理
- {
- ret_code_t err_code;
- ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
-
- switch (p_ble_evt->header.evt_id)
- {
- case BLE_GAP_EVT_CONNECTED:
- SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> BLE_GAP_EVT_CONNECTED\n");
- m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
- multi_qwr_conn_handle_assign(p_ble_evt->evt.gap_evt.conn_handle);//QWR句柄分配
- isClientConnet = 1;
- break;
- case BLE_GAP_EVT_DISCONNECTED:
- isClientConnet = 0;
- SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> BLE_GAP_EVT_DISCONNECTED\n");
- break;
- case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
- {
- SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> BLE_GAP_EVT_PHY_UPDATE_REQUEST\n");
- ble_gap_phys_t const phys =
- {
- .rx_phys = BLE_GAP_PHY_AUTO,
- .tx_phys = BLE_GAP_PHY_AUTO,
- };
- err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
- APP_ERROR_CHECK(err_code);
- } break;
- case BLE_GATTC_EVT_TIMEOUT:
- // Disconnect on GATT Client timeout event.
- SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> BLE_GATTC_EVT_TIMEOUT\n");
- err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
- BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
- APP_ERROR_CHECK(err_code);
- break;
- case BLE_GATTS_EVT_TIMEOUT:
- // Disconnect on GATT Server timeout event.
- SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> BLE_GATTS_EVT_TIMEOUT\n");
- err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
- BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
- APP_ERROR_CHECK(err_code);
- break;
-
- case BLE_GAP_EVT_CONN_PARAM_UPDATE:
- SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> BLE_GAP_EVT_CONN_PARAM_UPDATE\n");
- SEGGER_RTT_printf(0,"min_conn_interval : %d\n",p_gap_evt->params.conn_param_update_request.conn_params.min_conn_interval);
- SEGGER_RTT_printf(0,"max_conn_interval : %d\n",p_gap_evt->params.conn_param_update_request.conn_params.max_conn_interval);
- SEGGER_RTT_printf(0,"slave_latency : %d\n",p_gap_evt->params.conn_param_update_request.conn_params.slave_latency);
- SEGGER_RTT_printf(0,"conn_sup_timeout : %d\n",p_gap_evt->params.conn_param_update_request.conn_params.conn_sup_timeout);
- break;
-
- case BLE_GATTC_EVT_EXCHANGE_MTU_RSP:
- SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> BLE_GATTC_EVT_EXCHANGE_MTU_RSP\n");
- break;
- case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
- SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> BLE_GAP_EVT_DATA_LENGTH_UPDATE\n");
- break;
- case BLE_GAP_EVT_ADV_SET_TERMINATED:
- SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> BLE_GAP_EVT_ADV_SET_TERMINATED\n");
- break;
- case BLE_GATTS_EVT_HVN_TX_COMPLETE:
- NRF_LOG_DEBUG("on_ble_peripheral_evt -> BLE_GATTS_EVT_HVN_TX_COMPLETE\n");
- break;
- case BLE_GATTS_EVT_WRITE:
- NRF_LOG_DEBUG("on_ble_peripheral_evt -> BLE_GATTS_EVT_WRITE\n");
- break;
- default:SEGGER_RTT_printf(0,"on_ble_peripheral_evt -> default : 0x%2x\n",p_ble_evt->header.evt_id);
- // No implementation needed.
- break;
- }
- }
- static void conn_params_error_handler(uint32_t nrf_error)
- {
- APP_ERROR_HANDLER(nrf_error);
- }
- static void conn_params_init(void)
- {
- ret_code_t err_code;
- ble_conn_params_init_t cp_init;
- memset(&cp_init, 0, sizeof(cp_init));
- cp_init.p_conn_params = NULL;
- cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
- cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
- cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
- cp_init.start_on_notify_cccd_handle = BLE_CONN_HANDLE_INVALID; // Start upon connection.
- cp_init.disconnect_on_fail = true;
- cp_init.evt_handler = NULL; // Ignore events.
- cp_init.error_handler = conn_params_error_handler;
- err_code = ble_conn_params_init(&cp_init);
- APP_ERROR_CHECK(err_code);
- }
- static void nrf_qwr_error_handler(uint32_t nrf_error)//队列写错误操作
- {
- APP_ERROR_HANDLER(nrf_error);
- }
- //从 BLE 接受数据
- static void nus_data_handler(ble_nus_evt_t * p_evt)
- {
- if (p_evt->type == BLE_NUS_EVT_RX_DATA)
- {
- BLE_Client_Push((unsigned char*)(p_evt->params.rx_data.p_data), p_evt->params.rx_data.length);
- }
- }
- void ble_nus_send_buff(uint8_t *p,uint16_t len)
- {
- if(isClientConnet==0) return;
- ble_nus_data_send(&m_nus, p, &len, m_conn_handle);
- }
- static void services_init(void)//服务初始化
- {
- uint32_t err_code;
- ble_nus_init_t nus_init;
- nrf_ble_qwr_init_t qwr_init = {0};
- // Initialize Queued Write Module.
- qwr_init.error_handler = nrf_qwr_error_handler;
-
- for (uint32_t i = 0; i < NRF_SDH_BLE_TOTAL_LINK_COUNT; i++)
- {
- err_code = nrf_ble_qwr_init(&m_qwr[i], &qwr_init);
- APP_ERROR_CHECK(err_code);
- }
- // Initialize NUS.
- memset(&nus_init, 0, sizeof(nus_init));
- nus_init.data_handler = nus_data_handler;
- err_code = ble_nus_init(&m_nus, &nus_init);
- APP_ERROR_CHECK(err_code);
- }
- static void gap_params_init(void)//GAP初始化
- {
- uint32_t err_code;
- ble_gap_conn_params_t gap_conn_params;
- ble_gap_conn_sec_mode_t sec_mode;
- #if USE_LADDR == 1
- ble_gap_addr_t m_my_addr;
- char BleReallyName[30]={0};
- #endif
- BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
- #if USE_LADDR == 1
- err_code = sd_ble_gap_addr_get(&m_my_addr);
- APP_ERROR_CHECK(err_code);
- #if PROJECT_SLECT == 8
- sprintf(BleReallyName,"%s_L%02X%02X",DEVICE_NAME,m_my_addr.addr[1],m_my_addr.addr[0]);
- #endif
- #if PROJECT_SLECT == 9
- sprintf(BleReallyName,"%s_R%02X%02X",DEVICE_NAME,m_my_addr.addr[1],m_my_addr.addr[0]);
- #endif
- err_code = sd_ble_gap_device_name_set(&sec_mode,
- (const uint8_t *) BleReallyName,
- strlen(DEVICE_NAME)+7);
- #else
- err_code = sd_ble_gap_device_name_set(&sec_mode,
- (const uint8_t *) DEVICE_NAME,
- strlen(DEVICE_NAME));
- #endif
- APP_ERROR_CHECK(err_code);
- memset(&gap_conn_params, 0, sizeof(gap_conn_params));
- gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
- gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
- gap_conn_params.slave_latency = SLAVE_LATENCY;
- gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
- err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
- APP_ERROR_CHECK(err_code);
- }
- static void advertising_init(void)
- {
- uint32_t err_code;
- ble_advertising_init_t init;
- memset(&init, 0, sizeof(init));
- init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
- init.advdata.include_appearance = false;
- init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
- init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
- init.srdata.uuids_complete.p_uuids = m_adv_uuids;
- init.config.ble_adv_fast_enabled = true;
- init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
- init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
- init.evt_handler = on_adv_evt;
- err_code = ble_advertising_init(&m_advertising, &init);
- APP_ERROR_CHECK(err_code);
- ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
- }
- //static void adv_scan_start(void)
- //{
- //#if BLESLAVE
- // ret_code_t err_code;
- //#endif
- // //check if there are no flash operations in progress
- // if (!nrf_fstorage_is_busy(NULL))
- // {
- // // Start scanning for peripherals and initiate connection to devices which
- // // advertise Heart Rate or Running speed and cadence UUIDs.
- //#if BLEHOST
- // scan_start();//开始扫描
- //#endif
- // // Start advertising.
- //#if BLESLAVE
- // err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);//同时开始广播
- // APP_ERROR_CHECK(err_code);
- //#endif
- // }
- //}
- void advertising_start(void)
- {
- #if BLESLAVE
- ret_code_t err_code;
- err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);//同时开始广播
- APP_ERROR_CHECK(err_code);
- #endif
- }
- void senduart(unsigned char* bytes,int len)
- {
- for(int i=0;i<len;i++) app_uart_put(bytes[i]); //串口输出数据
- }
- void Advertising_Config(void)
- {
- gap_params_init();//添加的GAP初始化
- conn_params_init();//添加的连接参数初始化
- services_init();//添加从机服务初始化
- advertising_init();//添加广播初始化
- }
- static bool ble_evt_is_advertising_timeout(ble_evt_t const * p_ble_evt)
- {
- return (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_SET_TERMINATED);
- }
- /**@brief Function for handling BLE events.
- *
- * @param[in] p_ble_evt Bluetooth stack event.
- * @param[in] p_context Unused.
- */
- static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
- {
- 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_is_advertising_timeout(p_ble_evt))
- {
- // ble_nus_on_ble_evt(p_ble_evt, &m_nus);
- on_ble_peripheral_evt(p_ble_evt);
- }
- else if ((role == BLE_GAP_ROLE_CENTRAL) || (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_REPORT))
- {
- // ble_nus_c_on_ble_evt(p_ble_evt, &m_ble_nus_c);
- on_ble_central_evt(p_ble_evt,NULL);
- }
- else
- {
- SEGGER_RTT_printf(0,"ble_evt_handler -> other\n");
- }
- }
- /**@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;
- err_code = nrf_sdh_enable_request();
- APP_ERROR_CHECK(err_code);
- // Configure the BLE stack using the default settings.
- // Fetch the start address of the application RAM.
- uint32_t ram_start = 0;
- err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
- APP_ERROR_CHECK(err_code);
- // Enable BLE stack.
- err_code = nrf_sdh_ble_enable(&ram_start);
- APP_ERROR_CHECK(err_code);
- // Register a handler for BLE events.
- NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
- }
- uint8_t BLE_Client_Isconneted(void)
- {
- return isClientConnet;
- }
- uint8_t BLE_Host_Isconneted(void)
- {
- return isHostConnet;
- }
- int8_t sd_ble_gap_rssi_get_user(void)
- {
- int8_t rssi =0;
- unsigned char pch_chan=0;
- sd_ble_gap_rssi_get(m_ble_nus_c.conn_handle, &rssi,&pch_chan);
- return rssi;
- }
- extern void PRE_Init(void);
- extern void USR_Init(void);
- extern void USR_Process(void);
- int main(void)
- {
- // log_init();
- PRE_Init(); //用户初始化
- timer_init();
- TIME_Init();
- // uart_init();
- db_discovery_init();
- power_management_init();
- ble_stack_init();
- gatt_init();
- nus_c_init();
- scan_init();
- scan_start();//开始扫描
- SEGGER_RTT_printf(0,"Uart BLE_Host started.\r\n");
- Advertising_Config();
- ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
- SEGGER_RTT_printf(0,"Uart BLE_Slave started.\r\n");
-
- USR_Init(); //用户初始化
- while (1)
- {
- USR_Process();
- }
- }
|