simplify error check

This commit is contained in:
Shuanglei Tao
2025-03-05 15:01:52 +08:00
parent b6934e2ce2
commit 96fcb26deb
4 changed files with 67 additions and 117 deletions

View File

@@ -23,7 +23,7 @@
#if defined(S112) #if defined(S112)
#define EPD_CFG_DEFAULT {0x14, 0x13, 0x06, 0x05, 0x04, 0x03, 0x02, 0x03, 0xFF, 0x12, 0x07} #define EPD_CFG_DEFAULT {0x14, 0x13, 0x06, 0x05, 0x04, 0x03, 0x02, 0x03, 0xFF, 0x12, 0x07}
#else #else
//#define EPD_CFG_DEFAULT {0x05, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x07} #define EPD_CFG_DEFAULT {0x05, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x01, 0x07}
#endif #endif
#ifndef EPD_CFG_DEFAULT #ifndef EPD_CFG_DEFAULT
@@ -184,13 +184,13 @@ static void on_write(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
{ {
if (ble_srv_is_notification_enabled(p_evt_write->data)) if (ble_srv_is_notification_enabled(p_evt_write->data))
{ {
NRF_LOG_DEBUG("notification enabled\n");
p_epd->is_notification_enabled = true; p_epd->is_notification_enabled = true;
static uint16_t length = sizeof(epd_config_t); static uint16_t length = sizeof(epd_config_t);
NRF_LOG_DEBUG("send epd config\n");
err_code = ble_epd_string_send(p_epd, (uint8_t *)&p_epd->config, length); err_code = ble_epd_string_send(p_epd, (uint8_t *)&p_epd->config, length);
if (err_code != NRF_ERROR_INVALID_STATE) if (err_code != NRF_ERROR_INVALID_STATE)
{
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
}
} }
else else
{ {
@@ -247,20 +247,17 @@ void ble_epd_on_ble_evt(ble_epd_t * p_epd, ble_evt_t * p_ble_evt)
static uint32_t epd_service_init(ble_epd_t * p_epd) static uint32_t epd_service_init(ble_epd_t * p_epd)
{ {
uint32_t err_code;
ble_uuid_t ble_uuid; ble_uuid_t ble_uuid;
ble_uuid128_t base_uuid = BLE_EPD_BASE_UUID; ble_uuid128_t base_uuid = BLE_EPD_BASE_UUID;
ble_add_char_params_t add_char_params; ble_add_char_params_t add_char_params;
err_code = sd_ble_uuid_vs_add(&base_uuid, &p_epd->uuid_type); VERIFY_SUCCESS(sd_ble_uuid_vs_add(&base_uuid, &p_epd->uuid_type));
VERIFY_SUCCESS(err_code);
ble_uuid.type = p_epd->uuid_type; ble_uuid.type = p_epd->uuid_type;
ble_uuid.uuid = BLE_UUID_EPD_SERVICE; ble_uuid.uuid = BLE_UUID_EPD_SERVICE;
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, VERIFY_SUCCESS(sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
&ble_uuid, &ble_uuid,
&p_epd->service_handle); &p_epd->service_handle));
VERIFY_SUCCESS(err_code);
memset(&add_char_params, 0, sizeof(add_char_params)); memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = BLE_UUID_EPD_CHARACTERISTIC; add_char_params.uuid = BLE_UUID_EPD_CHARACTERISTIC;
@@ -330,6 +327,7 @@ uint32_t ble_epd_init(ble_epd_t * p_epd, epd_callback_t cmd_cb)
p_epd->epd_cmd_cb = cmd_cb; p_epd->epd_cmd_cb = cmd_cb;
// Initialize the service structure. // Initialize the service structure.
p_epd->max_data_len = BLE_EPD_MAX_DATA_LEN;
p_epd->conn_handle = BLE_CONN_HANDLE_INVALID; p_epd->conn_handle = BLE_CONN_HANDLE_INVALID;
p_epd->is_notification_enabled = false; p_epd->is_notification_enabled = false;
@@ -364,7 +362,7 @@ uint32_t ble_epd_string_send(ble_epd_t * p_epd, uint8_t * p_string, uint16_t len
return NRF_ERROR_INVALID_STATE; return NRF_ERROR_INVALID_STATE;
} }
if (length > BLE_EPD_MAX_DATA_LEN) if (length > p_epd->max_data_len)
{ {
return NRF_ERROR_INVALID_PARAM; return NRF_ERROR_INVALID_PARAM;
} }

View File

@@ -45,9 +45,7 @@ void ble_epd_evt_handler(ble_evt_t const * p_ble_evt, void * p_context);
#define BLE_UUID_EPD_SERVICE 0x0001 #define BLE_UUID_EPD_SERVICE 0x0001
#define EPD_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN #define EPD_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN
#if defined(S112) #if defined(S112)
#define OPCODE_LENGTH 1 #define BLE_EPD_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - 3)
#define HANDLE_LENGTH 2
#define BLE_EPD_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH)
#else #else
#define BLE_EPD_MAX_DATA_LEN (GATT_MTU_SIZE_DEFAULT - 3) /**< Maximum length of data (in bytes) that can be transmitted to the peer. */ #define BLE_EPD_MAX_DATA_LEN (GATT_MTU_SIZE_DEFAULT - 3) /**< Maximum length of data (in bytes) that can be transmitted to the peer. */
#endif #endif
@@ -80,8 +78,9 @@ typedef struct
{ {
uint8_t uuid_type; /**< UUID type for EPD Service Base UUID. */ uint8_t uuid_type; /**< UUID type for EPD Service Base UUID. */
uint16_t service_handle; /**< Handle of EPD Service (as provided by the S110 SoftDevice). */ uint16_t service_handle; /**< Handle of EPD Service (as provided by the S110 SoftDevice). */
ble_gatts_char_handles_t char_handles; /**< Handles related to the EPD characteristic (as provided by the S110 SoftDevice). */ ble_gatts_char_handles_t char_handles; /**< Handles related to the EPD characteristic (as provided by the SoftDevice). */
uint16_t conn_handle; /**< Handle of the current connection (as provided by the S110 SoftDevice). BLE_CONN_HANDLE_INVALID if not in a connection. */ uint16_t conn_handle; /**< Handle of the current connection (as provided by the SoftDevice). BLE_CONN_HANDLE_INVALID if not in a connection. */
uint16_t max_data_len; /**< Maximum length of data (in bytes) that can be transmitted to the peer */
bool is_notification_enabled; /**< Variable to indicate if the peer has enabled notification of the RX characteristic.*/ bool is_notification_enabled; /**< Variable to indicate if the peer has enabled notification of the RX characteristic.*/
epd_driver_t *driver; /**< current EPD driver */ epd_driver_t *driver; /**< current EPD driver */
epd_config_t config; /**< EPD config */ epd_config_t config; /**< EPD config */

View File

@@ -9976,7 +9976,7 @@
#ifndef NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED #ifndef NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED
#define NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED 1 #define NRF_LOG_STR_FORMATTER_TIMESTAMP_FORMAT_ENABLED 0
#endif #endif
// </h> // </h>

153
main.c
View File

@@ -29,6 +29,7 @@
#include "fstorage.h" #include "fstorage.h"
#include "softdevice_handler.h" #include "softdevice_handler.h"
#endif #endif
#include "nrf_power.h"
#include "nrf_soc.h" #include "nrf_soc.h"
#include "app_error.h" #include "app_error.h"
#include "app_timer.h" #include "app_timer.h"
@@ -57,6 +58,11 @@
#define TIMER_TICKS(MS) APP_TIMER_TICKS(MS) #define TIMER_TICKS(MS) APP_TIMER_TICKS(MS)
#else #else
#define TIMER_TICKS(MS) APP_TIMER_TICKS(MS, APP_TIMER_PRESCALER) #define TIMER_TICKS(MS) APP_TIMER_TICKS(MS, APP_TIMER_PRESCALER)
// Low frequency clock source to be used by the SoftDevice
#define NRF_CLOCK_LFCLKSRC {.source = NRF_CLOCK_LF_SRC_SYNTH, \
.rc_ctiv = 0, \
.rc_temp_ctiv = 0, \
.xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_20_PPM}
#endif #endif
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Minimum connection interval (7.5 ms) */ #define MIN_CONN_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Minimum connection interval (7.5 ms) */
@@ -77,7 +83,6 @@
#if defined(S112) #if defined(S112)
NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */ NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */ BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
static uint16_t m_ble_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - 3; /**< Maximum length of data (in bytes) that can be transmitted to the peer. */
#endif #endif
static uint16_t m_driver_refs = 0; static uint16_t m_driver_refs = 0;
static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */ static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
@@ -162,20 +167,16 @@ static void scheduler_init(void)
*/ */
static void timers_init(void) static void timers_init(void)
{ {
uint32_t err_code;
// Initialize timer module. // Initialize timer module.
#if defined(S112) #if defined(S112)
err_code = app_timer_init(); APP_ERROR_CHECK(app_timer_init());
APP_ERROR_CHECK(err_code);
#else #else
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false); APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
#endif #endif
// Create timers. // Create timers.
err_code = app_timer_create(&m_clock_timer_id, APP_ERROR_CHECK(app_timer_create(&m_clock_timer_id,
APP_TIMER_MODE_REPEATED, APP_TIMER_MODE_REPEATED,
clock_timer_timeout_handler); clock_timer_timeout_handler));
APP_ERROR_CHECK(err_code);
} }
/**@brief Function for starting application timers. /**@brief Function for starting application timers.
@@ -183,8 +184,7 @@ static void timers_init(void)
static void application_timers_start(void) static void application_timers_start(void)
{ {
// Start application timers. // Start application timers.
uint32_t err_code = app_timer_start(m_clock_timer_id, CLOCK_TIMER_INTERVAL, NULL); APP_ERROR_CHECK(app_timer_start(m_clock_timer_id, CLOCK_TIMER_INTERVAL, NULL));
APP_ERROR_CHECK(err_code);
} }
bool epd_cmd_callback(uint8_t cmd, uint8_t *data, uint16_t len) bool epd_cmd_callback(uint8_t cmd, uint8_t *data, uint16_t len)
@@ -222,11 +222,8 @@ bool epd_cmd_callback(uint8_t cmd, uint8_t *data, uint16_t len)
*/ */
static void services_init(void) static void services_init(void)
{ {
uint32_t err_code;
memset(&m_epd, 0, sizeof(ble_epd_t)); memset(&m_epd, 0, sizeof(ble_epd_t));
err_code = ble_epd_init(&m_epd, epd_cmd_callback); APP_ERROR_CHECK(ble_epd_init(&m_epd, epd_cmd_callback));
APP_ERROR_CHECK(err_code);
} }
/**@brief Function for the GAP initialization. /**@brief Function for the GAP initialization.
@@ -236,7 +233,6 @@ static void services_init(void)
*/ */
static void gap_params_init(void) static void gap_params_init(void)
{ {
uint32_t err_code;
char device_name[20]; char device_name[20];
ble_gap_addr_t addr; ble_gap_addr_t addr;
ble_gap_conn_params_t gap_conn_params; ble_gap_conn_params_t gap_conn_params;
@@ -244,21 +240,19 @@ static void gap_params_init(void)
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
#if defined(S112) #if defined(S112)
err_code = sd_ble_gap_addr_get(&addr); APP_ERROR_CHECK(sd_ble_gap_addr_get(&addr));
#else #else
err_code = sd_ble_gap_address_get(&addr); APP_ERROR_CHECK(sd_ble_gap_address_get(&addr));
#endif #endif
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("Bluetooth MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n", NRF_LOG_INFO("Bluetooth MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n",
addr.addr[5], addr.addr[4], addr.addr[3], addr.addr[5], addr.addr[4], addr.addr[3],
addr.addr[2], addr.addr[1], addr.addr[0]); addr.addr[2], addr.addr[1], addr.addr[0]);
snprintf(device_name, 20, "%s_%02X%02X", DEVICE_NAME, addr.addr[1],addr.addr[0]); snprintf(device_name, 20, "%s_%02X%02X", DEVICE_NAME, addr.addr[1],addr.addr[0]);
err_code = sd_ble_gap_device_name_set(&sec_mode, APP_ERROR_CHECK(sd_ble_gap_device_name_set(&sec_mode,
(const uint8_t *)device_name, (const uint8_t *)device_name,
strlen(device_name)); strlen(device_name)));
APP_ERROR_CHECK(err_code);
memset(&gap_conn_params, 0, sizeof(gap_conn_params)); memset(&gap_conn_params, 0, sizeof(gap_conn_params));
@@ -267,8 +261,7 @@ static void gap_params_init(void)
gap_conn_params.slave_latency = SLAVE_LATENCY; gap_conn_params.slave_latency = SLAVE_LATENCY;
gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
err_code = sd_ble_gap_ppcp_set(&gap_conn_params); APP_ERROR_CHECK(sd_ble_gap_ppcp_set(&gap_conn_params));
APP_ERROR_CHECK(err_code);
} }
/**@brief Function for handling an event from the Connection Parameters Module. /**@brief Function for handling an event from the Connection Parameters Module.
@@ -284,12 +277,9 @@ static void gap_params_init(void)
*/ */
static void on_conn_params_evt(ble_conn_params_evt_t * p_evt) static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
{ {
uint32_t err_code;
if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
{ {
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); APP_ERROR_CHECK(sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE));
APP_ERROR_CHECK(err_code);
} }
} }
@@ -308,7 +298,6 @@ static void conn_params_error_handler(uint32_t nrf_error)
*/ */
static void conn_params_init(void) static void conn_params_init(void)
{ {
uint32_t err_code;
ble_conn_params_init_t cp_init; ble_conn_params_init_t cp_init;
memset(&cp_init, 0, sizeof(cp_init)); memset(&cp_init, 0, sizeof(cp_init));
@@ -322,8 +311,7 @@ static void conn_params_init(void)
cp_init.evt_handler = on_conn_params_evt; cp_init.evt_handler = on_conn_params_evt;
cp_init.error_handler = conn_params_error_handler; cp_init.error_handler = conn_params_error_handler;
err_code = ble_conn_params_init(&cp_init); APP_ERROR_CHECK(ble_conn_params_init(&cp_init));
APP_ERROR_CHECK(err_code);
} }
@@ -331,11 +319,10 @@ static void advertising_start(void)
{ {
NRF_LOG_INFO("advertising start\n"); NRF_LOG_INFO("advertising start\n");
#if defined(S112) #if defined(S112)
uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST); APP_ERROR_CHECK(ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST));
#else #else
uint32_t err_code = ble_advertising_start(BLE_ADV_MODE_FAST); APP_ERROR_CHECK(ble_advertising_start(BLE_ADV_MODE_FAST));
#endif #endif
APP_ERROR_CHECK(err_code);
} }
/**@brief Function for putting the chip into sleep mode. /**@brief Function for putting the chip into sleep mode.
@@ -350,8 +337,9 @@ static void sleep_mode_enter(void)
ble_epd_sleep_prepare(&m_epd); ble_epd_sleep_prepare(&m_epd);
// Go to system-off mode (this function will not return; wakeup will cause a reset). // Go to system-off mode (this function will not return; wakeup will cause a reset).
uint32_t err_code = sd_power_system_off(); APP_ERROR_CHECK(sd_power_system_off());
APP_ERROR_CHECK(err_code);
nrf_power_system_off();
} }
void gpiote_evt_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { void gpiote_evt_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
@@ -367,14 +355,9 @@ void gpiote_evt_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
static void setup_wakeup_pin(nrf_drv_gpiote_pin_t pin) { static void setup_wakeup_pin(nrf_drv_gpiote_pin_t pin) {
NRF_LOG_DEBUG("Setting up wakeup pin\n"); NRF_LOG_DEBUG("Setting up wakeup pin\n");
ret_code_t err_code = nrf_drv_gpiote_init(); APP_ERROR_CHECK(nrf_drv_gpiote_init());
APP_ERROR_CHECK(err_code);
nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(false); nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(false);
APP_ERROR_CHECK(nrf_drv_gpiote_in_init(pin, &config, gpiote_evt_handler));
err_code = nrf_drv_gpiote_in_init(pin, &config, gpiote_evt_handler);
APP_ERROR_CHECK(err_code);
nrf_drv_gpiote_in_event_enable(pin, true); nrf_drv_gpiote_in_event_enable(pin, true);
} }
@@ -410,8 +393,6 @@ static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
*/ */
static void on_ble_evt(ble_evt_t * p_ble_evt) static void on_ble_evt(ble_evt_t * p_ble_evt)
{ {
uint32_t err_code;
switch (p_ble_evt->header.evt_id) switch (p_ble_evt->header.evt_id)
{ {
case BLE_GAP_EVT_CONNECTED: case BLE_GAP_EVT_CONNECTED:
@@ -437,35 +418,30 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
.rx_phys = BLE_GAP_PHY_AUTO, .rx_phys = BLE_GAP_PHY_AUTO,
.tx_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(sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys));
APP_ERROR_CHECK(err_code);
} break; } break;
#endif #endif
case BLE_GAP_EVT_SEC_PARAMS_REQUEST: case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
// Pairing not supported // Pairing not supported
err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL); APP_ERROR_CHECK(sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL));
APP_ERROR_CHECK(err_code);
break; break;
case BLE_GATTS_EVT_SYS_ATTR_MISSING: case BLE_GATTS_EVT_SYS_ATTR_MISSING:
// No system attributes have been stored. // No system attributes have been stored.
err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0); APP_ERROR_CHECK(sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0));
APP_ERROR_CHECK(err_code);
break; break;
case BLE_GATTC_EVT_TIMEOUT: case BLE_GATTC_EVT_TIMEOUT:
// Disconnect on GATT Client timeout event. // Disconnect on GATT Client timeout event.
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle, APP_ERROR_CHECK(sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
APP_ERROR_CHECK(err_code);
break; break;
case BLE_GATTS_EVT_TIMEOUT: case BLE_GATTS_EVT_TIMEOUT:
// Disconnect on GATT Server timeout event. // Disconnect on GATT Server timeout event.
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle, APP_ERROR_CHECK(sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
APP_ERROR_CHECK(err_code);
break; break;
default: default:
@@ -532,54 +508,41 @@ static void sys_evt_dispatch(uint32_t sys_evt)
*/ */
static void ble_stack_init(void) static void ble_stack_init(void)
{ {
uint32_t err_code;
#if defined(S112) #if defined(S112)
err_code = nrf_sdh_enable_request(); APP_ERROR_CHECK(nrf_sdh_enable_request());
APP_ERROR_CHECK(err_code);
// Configure the BLE stack using the default settings. // Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM. // Fetch the start address of the application RAM.
uint32_t ram_start = 0; uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start); APP_ERROR_CHECK(nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start));
APP_ERROR_CHECK(err_code);
// Enable BLE stack. // Enable BLE stack.
err_code = nrf_sdh_ble_enable(&ram_start); APP_ERROR_CHECK(nrf_sdh_ble_enable(&ram_start));
APP_ERROR_CHECK(err_code);
// Register a handler for BLE events. // Register a handler for BLE events.
NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL); NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
#else #else
nrf_clock_lf_cfg_t clock_lf_cfg = { nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
.source = NRF_CLOCK_LF_SRC_SYNTH,
.rc_ctiv = 0,
.rc_temp_ctiv = 0,
.xtal_accuracy = NRF_CLOCK_LF_XTAL_ACCURACY_20_PPM,
};
// Initialize the SoftDevice handler module. // Initialize the SoftDevice handler module.
SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL); SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
ble_enable_params_t ble_enable_params; ble_enable_params_t ble_enable_params;
err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT, APP_ERROR_CHECK(softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
PERIPHERAL_LINK_COUNT, PERIPHERAL_LINK_COUNT,
&ble_enable_params); &ble_enable_params));
APP_ERROR_CHECK(err_code);
// Check the ram settings against the used number of links // Check the ram settings against the used number of links
CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT); CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT);
// Enable BLE stack. // Enable BLE stack.
err_code = softdevice_enable(&ble_enable_params); APP_ERROR_CHECK(softdevice_enable(&ble_enable_params));
APP_ERROR_CHECK(err_code);
// Subscribe for BLE events. // Subscribe for BLE events.
err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch); APP_ERROR_CHECK(softdevice_ble_evt_handler_set(ble_evt_dispatch));
APP_ERROR_CHECK(err_code);
// Subscribe for System events. // Subscribe for System events.
err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch); APP_ERROR_CHECK(softdevice_sys_evt_handler_set(sys_evt_dispatch));
APP_ERROR_CHECK(err_code);
#endif #endif
} }
@@ -589,8 +552,8 @@ void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
{ {
if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED)) if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
{ {
m_ble_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH; m_epd.max_data_len = p_evt->params.att_mtu_effective - 3;
NRF_LOG_INFO("Data len is set to 0x%X(%d)", m_ble_max_data_len, m_ble_max_data_len); NRF_LOG_INFO("Data len is set to 0x%X(%d)", m_epd.max_data_len, m_epd.max_data_len);
} }
NRF_LOG_DEBUG("ATT MTU exchange completed. central 0x%x peripheral 0x%x", NRF_LOG_DEBUG("ATT MTU exchange completed. central 0x%x peripheral 0x%x",
p_gatt->att_mtu_desired_central, p_gatt->att_mtu_desired_central,
@@ -601,28 +564,22 @@ void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
/**@brief Function for initializing the GATT library. */ /**@brief Function for initializing the GATT library. */
void gatt_init(void) void gatt_init(void)
{ {
ret_code_t err_code; APP_ERROR_CHECK(nrf_ble_gatt_init(&m_gatt, gatt_evt_handler));
APP_ERROR_CHECK(nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE));
err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
APP_ERROR_CHECK(err_code);
err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
APP_ERROR_CHECK(err_code);
} }
#else #else
// Set BW Config to HIGH. // Set BW Config to HIGH.
static void ble_options_set(void) static void ble_options_set(void)
{ {
uint32_t err_code;
ble_opt_t ble_opt; ble_opt_t ble_opt;
memset(&ble_opt, 0, sizeof(ble_opt)); memset(&ble_opt, 0, sizeof(ble_opt));
ble_opt.common_opt.conn_bw.role = BLE_GAP_ROLE_PERIPH; ble_opt.common_opt.conn_bw.role = BLE_GAP_ROLE_PERIPH;
ble_opt.common_opt.conn_bw.conn_bw.conn_bw_rx = BLE_CONN_BW_HIGH; ble_opt.common_opt.conn_bw.conn_bw.conn_bw_rx = BLE_CONN_BW_HIGH;
ble_opt.common_opt.conn_bw.conn_bw.conn_bw_tx = BLE_CONN_BW_HIGH; ble_opt.common_opt.conn_bw.conn_bw.conn_bw_tx = BLE_CONN_BW_HIGH;
err_code = sd_ble_opt_set(BLE_COMMON_OPT_CONN_BW, &ble_opt); APP_ERROR_CHECK(sd_ble_opt_set(BLE_COMMON_OPT_CONN_BW, &ble_opt));
APP_ERROR_CHECK(err_code);
} }
#endif #endif
@@ -630,7 +587,6 @@ static void ble_options_set(void)
*/ */
static void advertising_init(void) static void advertising_init(void)
{ {
uint32_t err_code;
#if defined(S112) #if defined(S112)
ble_advertising_init_t init; ble_advertising_init_t init;
@@ -648,8 +604,7 @@ static void advertising_init(void)
init.config.ble_adv_fast_timeout = APP_ADV_TIMEOUT_IN_SECONDS * 100; init.config.ble_adv_fast_timeout = APP_ADV_TIMEOUT_IN_SECONDS * 100;
init.evt_handler = on_adv_evt; init.evt_handler = on_adv_evt;
err_code = ble_advertising_init(&m_advertising, &init); APP_ERROR_CHECK(ble_advertising_init(&m_advertising, &init));
APP_ERROR_CHECK(err_code);
ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG); ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
#else #else
@@ -672,8 +627,7 @@ static void advertising_init(void)
options.ble_adv_fast_interval = APP_ADV_INTERVAL; options.ble_adv_fast_interval = APP_ADV_INTERVAL;
options.ble_adv_fast_timeout = APP_ADV_TIMEOUT_IN_SECONDS; options.ble_adv_fast_timeout = APP_ADV_TIMEOUT_IN_SECONDS;
err_code = ble_advertising_init(&advdata, &scanrsp, &options, on_adv_evt, NULL); APP_ERROR_CHECK(ble_advertising_init(&advdata, &scanrsp, &options, on_adv_evt, NULL));
APP_ERROR_CHECK(err_code);
#endif #endif
} }
@@ -688,8 +642,7 @@ static uint32_t timestamp_func(void)
*/ */
static void log_init(void) static void log_init(void)
{ {
ret_code_t err_code = NRF_LOG_INIT(timestamp_func); APP_ERROR_CHECK(NRF_LOG_INIT(timestamp_func));
APP_ERROR_CHECK(err_code);
#if defined(S112) #if defined(S112)
NRF_LOG_DEFAULT_BACKENDS_INIT(); NRF_LOG_DEFAULT_BACKENDS_INIT();
#endif #endif