161 lines
4.2 KiB
C
161 lines
4.2 KiB
C
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C"
|
|
{
|
|
#endif
|
|
|
|
#include "app_config.h"
|
|
#include "apply_old_config.h"
|
|
#include "sdk_config.h"
|
|
|
|
#include "nrf_log.h"
|
|
#include "nrf_log_ctrl.h"
|
|
#include "nrf_log_default_backends.h"
|
|
|
|
#include "nrf_delay.h"
|
|
#include "nrf_gpio.h"
|
|
|
|
#include "nrf_sdh.h"
|
|
#include "nrf_sdh_ble.h"
|
|
#include "nrf_sdh_freertos.h"
|
|
|
|
#include "FreeRTOS.h"
|
|
#include "task.h"
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
|
|
|
|
extern void le_scan_start(void);
|
|
extern void le_scan_stop(void);
|
|
extern void le_gap_phy_update(uint16_t conn_handle);
|
|
extern void le_gap_conn_param_update(uint16_t conn_handle);
|
|
extern void le_db_discovery_start(uint16_t conn_handle);
|
|
static void le_evt_handler(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_ADV_REPORT: {
|
|
extern void le_scan_handler(ble_evt_t const *p_ble_evt, void *p_context);
|
|
le_scan_handler(p_ble_evt, p_context);
|
|
}
|
|
break;
|
|
case BLE_GAP_EVT_PHY_UPDATE:
|
|
NRF_LOG_INFO("PHY update procedure is complete.");
|
|
break;
|
|
case BLE_GAP_EVT_CONN_PARAM_UPDATE:
|
|
NRF_LOG_INFO("Connection parameters updated.");
|
|
break;
|
|
case BLE_GAP_EVT_CONNECTED:
|
|
NRF_LOG_INFO("Connected to peer.");
|
|
le_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle);
|
|
le_gap_conn_param_update(p_ble_evt->evt.gap_evt.conn_handle);
|
|
le_db_discovery_start(p_ble_evt->evt.gap_evt.conn_handle);
|
|
break;
|
|
case BLE_GAP_EVT_DISCONNECTED:
|
|
NRF_LOG_INFO("Disconnected from peer.");
|
|
break;
|
|
case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
|
|
le_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle);
|
|
break;
|
|
case BLE_GATTS_EVT_SYS_ATTR_MISSING: {
|
|
ret_code_t err_code = sd_ble_gatts_sys_attr_set(
|
|
p_ble_evt->evt.gatts_evt.conn_handle, NULL, 0, 0);
|
|
APP_ERROR_CHECK(err_code);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void le_stack_Init(void)
|
|
{
|
|
ret_code_t err_code;
|
|
uint32_t ram_start = 0;
|
|
ble_cfg_t ble_cfg;
|
|
memset(&ble_cfg, 0x00, sizeof(ble_cfg));
|
|
ble_cfg.conn_cfg.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
|
|
ble_cfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = 12;
|
|
|
|
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.
|
|
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
|
|
APP_ERROR_CHECK(err_code);
|
|
|
|
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &ble_cfg, 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, le_evt_handler, NULL);
|
|
}
|
|
|
|
static void initialize(void *p_context)
|
|
{
|
|
extern void uart_drv_init(void);
|
|
uart_drv_init();
|
|
|
|
extern void mem_board_init(void);
|
|
mem_board_init();
|
|
|
|
extern void le_db_discovery_init(void);
|
|
le_db_discovery_init();
|
|
|
|
extern void le_dis_c_init(void);
|
|
le_dis_c_init();
|
|
|
|
extern void le_gatt_c_init(void);
|
|
le_gatt_c_init();
|
|
|
|
extern void le_uart_c_init(void);
|
|
le_uart_c_init();
|
|
|
|
extern void le_gap_init(void);
|
|
le_gap_init();
|
|
|
|
extern void le_gatt_init(void);
|
|
le_gatt_init();
|
|
|
|
extern void le_scan_init(void);
|
|
le_scan_init();
|
|
|
|
extern void host_tasks_init(void);
|
|
host_tasks_init();
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
NRF_LOG_INIT(xTaskGetTickCount, configTICK_RATE_HZ);
|
|
NRF_LOG_DEFAULT_BACKENDS_INIT();
|
|
NRF_LOG_INFO("%s Build: %s %s", LE_DEVICE_NAME, __TIME__, __DATE__);
|
|
|
|
le_stack_Init();
|
|
|
|
nrf_sdh_freertos_init(initialize, NULL);
|
|
|
|
vTaskStartScheduler();
|
|
|
|
for (;;)
|
|
{
|
|
// Will not get here unless there is insufficient RAM.
|
|
__BKPT(255);
|
|
}
|
|
}
|
|
|
|
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
|
|
{
|
|
__BKPT(255);
|
|
}
|