728x90
반응형
SMALL
battery level 만 read하고 싶어
read할 수 있는 새 Characteristic을 추가 하였음
- blle_nus.h
/**@brief Nordic UART Service structure.
*
* @details This structure contains status information related to the service.
*/
struct ble_nus_s
{
uint8_t uuid_type;
uint16_t service_handle;
ble_gatts_char_handles_t tx_handles;
ble_gatts_char_handles_t rx_handles;
ble_gatts_char_handles_t bat_handles; //added
blcm_link_ctx_storage_t * const p_link_ctx_storage;
ble_nus_data_handler_t data_handler;
};
struct ble_nus_s에
ble_gatts_char_handles_t bat_handles; 추가
- ble_nus.c
#define BLE_UUID_NUS_BAT_CHARACTERISTIC 0x0004 //add
#define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0003 /**< The UUID of the TX Characteristic. */
#define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0002 /**< The UUID of the RX Characteristic. */
uint32_t ble_nus_init(ble_nus_t * p_nus, ble_nus_init_t const * p_nus_init)
{
ret_code_t err_code;
ble_uuid_t ble_uuid;
ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
ble_add_char_params_t add_char_params;
VERIFY_PARAM_NOT_NULL(p_nus);
VERIFY_PARAM_NOT_NULL(p_nus_init);
// Initialize the service structure.
p_nus->data_handler = p_nus_init->data_handler;
/**@snippet [Adding proprietary Service to the SoftDevice] */
// Add a custom base UUID.
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
VERIFY_SUCCESS(err_code);
ble_uuid.type = p_nus->uuid_type;
ble_uuid.uuid = BLE_UUID_NUS_SERVICE;
// Add the service.
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
&ble_uuid,
&p_nus->service_handle);
/**@snippet [Adding proprietary Service to the SoftDevice] */
VERIFY_SUCCESS(err_code);
// Add the RX Characteristic.
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC;
add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = BLE_NUS_MAX_RX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true;
add_char_params.char_props.write = 1;
add_char_params.char_props.write_wo_resp = 1;
add_char_params.read_access = SEC_OPEN;
add_char_params.write_access = SEC_OPEN;
err_code = characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->rx_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
// Add the TX Characteristic.
/**@snippet [Adding proprietary characteristic to the SoftDevice] */
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = BLE_UUID_NUS_TX_CHARACTERISTIC;
add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = BLE_NUS_MAX_TX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true;
add_char_params.char_props.notify = 1;
add_char_params.read_access = SEC_OPEN;
add_char_params.write_access = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;
err_code = characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->tx_handles);
if (err_code != NRF_SUCCESS)
{
return err_code;
}
// Add Battery Level Characteristic.
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = BLE_UUID_NUS_BAT_CHARACTERISTIC;
add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = BLE_NUS_MAX_TX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true;
add_char_params.char_props.read = 1;
add_char_params.is_defered_read = 1;
add_char_params.read_access = SEC_OPEN;
return characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->bat_handles);
/**@snippet [Adding proprietary characteristic to the SoftDevice] */
}
보면 마지막에 Characteristic을 추가하였다.
read property를 추가하여야 하기 때문에
add_char_params.char_props.read = 1;
add_char_params.is_defered_read = 1;
add_char_params.read_access = SEC_OPEN;
를 추가해주면 된다.
uuid랑 ble_gatts_char_handles_t(&p_nus->bat_handles) 를 다르게 추가함을 잊지 말아야 함.
- main.c
...
static void on_authorize_request(ble_evt_t const * p_ble_evt);
...
/**@brief Function for handling the Read/Write Authorization request event.
*
* @param[in] p_ble_evt Event received from the BLE stack.
*/
static void on_authorize_request(ble_evt_t const * p_ble_evt)
{
ble_gatts_rw_authorize_reply_params_t reply_params;
ble_gatts_evt_rw_authorize_request_t const * p_evt_rw_auth = &p_ble_evt->evt.gatts_evt.params.authorize_request;
memset (&reply_params, 0, sizeof(ble_gatts_rw_authorize_reply_params_t));
if (p_evt_rw_auth->type == BLE_GATTS_AUTHORIZE_TYPE_READ)
{
uint8_t bat_arr[3] = {0,};
bat_arr[0] = 0xa5;
bat_arr[1] = 0x06;
bat_arr[2] = bat_level;
reply_params.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
reply_params.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS;
reply_params.params.read.update = 1;
reply_params.params.read.len = 3;
reply_params.params.read.offset = p_evt_rw_auth->request.read.offset;
reply_params.params.read.p_data = bat_arr;
(void) sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.common_evt.conn_handle, &reply_params);
}
}
...
static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context)
{
...
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
NRF_LOG_DEBUG("BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST");
on_authorize_request(p_ble_evt);
break;
...
}
삽질끝에 uint8_t bat_level을 reply로 보내는데 성공하였다.
추가해아할 점만 포스팅 하였음.!
728x90
반응형
LIST
'Firmware > Nordic nRF52' 카테고리의 다른 글
[Nordic nRF52] 타이머 추가하기 (0) | 2021.12.29 |
---|---|
nRF52 long button click milliseconds time 수정 (0) | 2021.07.05 |