this article present a simple customized BLE service, with a characteristic that you can read, write , and notify.
example function description:
press button 3 to turn on LED_2, and press button 4 to turn off LED_2, recent status of LED_2 can be notify. Write "1" on the same characteristic can turn on LED_2, and write "0" can turn it off .
This is adapt from ble_app_template in SDK14
locate in :
...\BLE\nRF5_SDK_14.2.0_17b948a\nRF5_SDK_14.2.0_17b948a\examples\ble_peripheral\ble_app_template
I use nRF52840 PDK (pca10056)
STEP 0. add these two files near by main.c
ble_rls.c
/* author: Agatha Kuan 20180223 ble remote-controled light service */ #include "sdk_common.h" #include "ble_rls.h" #include <string.h> #include "ble_srv_common.h" #include "nrf_log.h" #include "nrf_log_ctrl.h" #include "nrf_log_default_backends.h" static void on_connect(ble_rls_t * p_rls, ble_evt_t const * p_ble_evt) { p_rls->conn_handle = p_ble_evt->evt.gap_evt.conn_handle; } static void on_disconnect(ble_rls_t * p_rls, ble_evt_t const * p_ble_evt) { UNUSED_PARAMETER(p_ble_evt); p_rls->conn_handle = BLE_CONN_HANDLE_INVALID; } static void on_write(ble_rls_t * p_rls, ble_evt_t const * p_ble_evt) { ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; if ((p_evt_write->handle == p_rls->status_char_handles.value_handle) && (p_evt_write->len == 1) && (p_rls->control_write_handler != NULL)) { p_rls->control_write_handler(p_ble_evt->evt.gap_evt.conn_handle, p_rls, p_evt_write->data[0]); } } void ble_rls_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context) { if ((p_context == NULL) || (p_ble_evt == NULL)) return; ble_rls_t * p_rls = (ble_rls_t *)p_context; switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: on_connect(p_rls, p_ble_evt); break; case BLE_GAP_EVT_DISCONNECTED: on_disconnect(p_rls, p_ble_evt); break; case BLE_GATTS_EVT_WRITE: on_write(p_rls, p_ble_evt); break; default: // No implementation needed. break; } } uint32_t status_char_add(ble_rls_t * p_rls, const ble_rls_init_t * p_rls_init) { uint32_t err_code; ble_gatts_char_md_t char_md; ble_gatts_attr_md_t cccd_md; ble_gatts_attr_t attr_char_value; ble_uuid_t ble_uuid; ble_gatts_attr_md_t attr_md; uint8_t encoded_report_ref[BLE_SRV_ENCODED_REPORT_REF_LEN]; uint8_t init_len; uint8_t init_status = CONTROLED_LED_OFF; if (p_rls->is_notification_supported) { memset(&cccd_md, 0, sizeof(cccd_md)); // According to BAS_SPEC_V10, the read operation on cccd should be possible without // authentication. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); cccd_md.write_perm = p_rls_init->status_char_attr_md.cccd_write_perm; cccd_md.vloc = BLE_GATTS_VLOC_STACK; } memset(&char_md, 0, sizeof(char_md)); char_md.char_props.read = 1; char_md.char_props.notify = (p_rls->is_notification_supported) ? 1 : 0; char_md.char_props.write = 1; char_md.p_char_user_desc = NULL; char_md.p_char_pf = NULL; char_md.p_user_desc_md = NULL; char_md.p_cccd_md = (p_rls->is_notification_supported) ? &cccd_md : NULL; char_md.p_sccd_md = NULL; BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_STATUS_CHAR); memset(&attr_md, 0, sizeof(attr_md)); attr_md.read_perm = p_rls_init->status_char_attr_md.read_perm; attr_md.write_perm = p_rls_init->status_char_attr_md.write_perm; attr_md.vloc = BLE_GATTS_VLOC_STACK; attr_md.rd_auth = 0; attr_md.wr_auth = 0; attr_md.vlen = 0; memset(&attr_char_value, 0, sizeof(attr_char_value)); attr_char_value.p_uuid = &ble_uuid; attr_char_value.p_attr_md = &attr_md; attr_char_value.init_len = sizeof(uint8_t); attr_char_value.init_offs = 0; attr_char_value.max_len = sizeof(uint8_t); attr_char_value.p_value = &init_status; err_code = sd_ble_gatts_characteristic_add(p_rls->service_handle, &char_md, &attr_char_value, &p_rls->status_char_handles); if (err_code != NRF_SUCCESS) return err_code; if (p_rls_init->p_report_ref != NULL) { // Add Report Reference descriptor BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_REPORT_REF_DESCR); memset(&attr_md, 0, sizeof(attr_md)); attr_md.read_perm = p_rls_init->status_report_read_perm; BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm); attr_md.vloc = BLE_GATTS_VLOC_STACK; attr_md.rd_auth = 0; attr_md.wr_auth = 0; attr_md.vlen = 0; init_len = ble_srv_report_ref_encode(encoded_report_ref, p_rls_init->p_report_ref); memset(&attr_char_value, 0, sizeof(attr_char_value)); attr_char_value.p_uuid = &ble_uuid; attr_char_value.p_attr_md = &attr_md; attr_char_value.init_len = init_len; attr_char_value.init_offs = 0; attr_char_value.max_len = attr_char_value.init_len; attr_char_value.p_value = encoded_report_ref; err_code = sd_ble_gatts_descriptor_add(p_rls->status_char_handles.value_handle, &attr_char_value, &p_rls->report_ref_handle); if (err_code != NRF_SUCCESS) return err_code; } else p_rls->report_ref_handle = BLE_GATT_HANDLE_INVALID; return NRF_SUCCESS; } uint32_t ble_rls_init(ble_rls_t * p_rls, const ble_rls_init_t * p_rls_init) { if (p_rls == NULL || p_rls_init == NULL) return NRF_ERROR_NULL; uint32_t err_code; ble_uuid_t ble_uuid; p_rls->control_write_handler = p_rls_init->control_write_handler; p_rls->conn_handle = BLE_CONN_HANDLE_INVALID; p_rls->is_notification_supported = p_rls_init->support_notification; BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_RLS_SERVICE); err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_rls->service_handle); if (err_code != NRF_SUCCESS) return err_code; err_code = status_char_add(p_rls, p_rls_init); VERIFY_SUCCESS(err_code); return NRF_SUCCESS; } uint32_t ble_rls_light_status_update(ble_rls_t* p_rls,uint8_t recent_status) { if (p_rls == NULL) return NRF_ERROR_NULL; uint32_t err_code = NRF_SUCCESS; ble_gatts_value_t gatts_value; if(recent_status != p_rls->light_status) { memset(&gatts_value, 0, sizeof(gatts_value)); gatts_value.len = sizeof(uint8_t); gatts_value.offset = 0; gatts_value.p_value = &recent_status; err_code = sd_ble_gatts_value_set(p_rls->conn_handle, p_rls->status_char_handles.value_handle, &gatts_value); if (err_code == NRF_SUCCESS) p_rls->light_status = recent_status; else return err_code; if ((p_rls->conn_handle != BLE_CONN_HANDLE_INVALID) && p_rls->is_notification_supported) { ble_gatts_hvx_params_t hvx_params; memset(&hvx_params, 0, sizeof(hvx_params)); hvx_params.handle = p_rls->status_char_handles.value_handle; hvx_params.type = BLE_GATT_HVX_NOTIFICATION; hvx_params.offset = gatts_value.offset; hvx_params.p_len = &gatts_value.len; hvx_params.p_data = gatts_value.p_value; err_code = sd_ble_gatts_hvx(p_rls->conn_handle, &hvx_params); } else err_code = NRF_ERROR_INVALID_STATE; } return err_code; }
ble_rls.h
/* author: Agatha Kuan 20180223 ble remote-controled light service */ #ifndef BLE_RLS_H_ #define BLE_RLS_H_ #include <stdint.h> #include <stdbool.h> #include "ble.h" #include "ble_srv_common.h" #include "nrf_sdh_ble.h" #include "nrf_ble_gatt.h" #ifdef __cplusplus extern "C" { #endif #ifndef BLE_RLS_OBSERVER_PRIO #define BLE_RLS_OBSERVER_PRIO 2 #endif #define BLE_RLS_DEF(_name) \ static ble_rls_t _name; \ NRF_SDH_BLE_OBSERVER(_name ## _obs, \ BLE_RLS_OBSERVER_PRIO, \ ble_rls_on_ble_evt, &_name); #define BLE_UUID_RLS_SERVICE 0x0330 #define BLE_UUID_STATUS_CHAR 0x0331 /*read,notify,write*/ #define CONTROLED_LED_ON 1 #define CONTROLED_LED_OFF 0 typedef enum { BLE_RLS_EVT_NOTIFICATION_ENABLED, BLE_RLS_EVT_NOTIFICATION_DISABLED } ble_rls_evt_type_t; typedef struct { ble_rls_evt_type_t evt_type; /**< Type of event. */ } ble_rls_evt_t; typedef struct ble_rls_s ble_rls_t; typedef void (*ble_rls_write_handler_t) (uint16_t conn_handle, ble_rls_t * p_rls, uint8_t new_state); typedef struct { ble_rls_write_handler_t control_write_handler; bool support_notification; ble_srv_report_ref_t* p_report_ref; uint8_t init_status; ble_srv_cccd_security_mode_t status_char_attr_md; ble_gap_conn_sec_mode_t status_report_read_perm; }ble_rls_init_t; struct ble_rls_s { uint16_t service_handle; ble_gatts_char_handles_t status_char_handles; uint16_t report_ref_handle; uint16_t conn_handle; bool is_notification_supported; uint8_t light_status; ble_rls_write_handler_t control_write_handler; }; uint32_t ble_rls_init(ble_rls_t* p_rls, const ble_rls_init_t* p_rls_init); void ble_rls_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context); uint32_t ble_rls_on_status_change(uint16_t conn_handle, ble_rls_t * p_rls, uint8_t recent_state); uint32_t ble_rls_light_status_update(ble_rls_t* p_rls,uint8_t recent_status); #ifdef __cplusplus } #endif #endif
STEP 1. add in main.c
#include "ble_rls.h"
BLE_RLS_DEF(m_rls);
about line 346
static void remote_control_light_handler(uint16_t conn_handle, ble_rls_t * p_rls, uint8_t new_state) { if (new_state) { NRF_LOG_INFO("light off\r"); bsp_board_led_off(BSP_BOARD_LED_1); ble_rls_light_status_update(&m_rls,CONTROLED_LED_OFF); } else { NRF_LOG_INFO("light on\r"); bsp_board_led_on(BSP_BOARD_LED_1); ble_rls_light_status_update(&m_rls,CONTROLED_LED_ON); } } static void services_init(void) { ret_code_t err_code; ble_rls_init_t rls_init; memset(&rls_init, 0, sizeof(rls_init)); rls_init.support_notification = true; rls_init.control_write_handler = remote_control_light_handler; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&rls_init.status_char_attr_md.cccd_write_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&rls_init.status_char_attr_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&rls_init.status_char_attr_md.write_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&rls_init.status_report_read_perm); err_code = ble_rls_init(&m_rls, &rls_init); APP_ERROR_CHECK(err_code); }
in bsp_event_handler
static void bsp_event_handler(bsp_event_t event) { ret_code_t err_code; switch (event) { case BSP_EVENT_SLEEP: NRF_LOG_INFO("bsp_event_handler sleep_mode_enter \r\n"); sleep_mode_enter(); break; // BSP_EVENT_SLEEP case BSP_EVENT_DISCONNECT: err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } break; // BSP_EVENT_DISCONNECT case BSP_EVENT_WHITELIST_OFF: if (m_conn_handle == BLE_CONN_HANDLE_INVALID) { err_code = ble_advertising_restart_without_whitelist(&m_advertising); if (err_code != NRF_ERROR_INVALID_STATE) { APP_ERROR_CHECK(err_code); } } break; // BSP_EVENT_KEY_0 case BSP_EVENT_WAKEUP: NRF_LOG_INFO("bsp_event_handler sleep_mode WAKE UP \r\n"); break; case BSP_EVENT_KEY_2: NRF_LOG_INFO("light on\r"); bsp_board_led_on(BSP_BOARD_LED_1); ble_rls_light_status_update(&m_rls,CONTROLED_LED_ON); break; case BSP_EVENT_KEY_3: NRF_LOG_INFO("light off\r"); bsp_board_led_off(BSP_BOARD_LED_1); ble_rls_light_status_update(&m_rls,CONTROLED_LED_OFF); break; default: break; } }
in main function
// Start execution. NRF_LOG_INFO("Template example started."); application_timers_start(); advertising_start(erase_bonds); bsp_board_led_on(BSP_BOARD_LED_2);
STEP.2 program and flash into your device
you can
press button 3 to turn on LED_2
press button 4 to turn off LED_2
on mobile phone, you can see
write 00 can turn off LED_2
write 01 can turn on it.
Note:
1.the example in this article is not the same as ble_app_blinky
2. the format and API I used are more similar to other common ble_services, like battery service or uart services.