I am trying to implement write long characteristic between master control panel and PCA10001 board. here is my code
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
uint32_t err_code;
static ble_gap_evt_auth_status_t m_auth_status;
ble_gap_enc_info_t * p_enc_info;
ble_gatts_evt_write_t * p_evt_write;
uint8_t inData;
uint16_t inDataLen;
uint8_t buffer[30];
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
nrf_gpio_pin_set(CONNECTED_LED_PIN_NO);
nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
break;
case BLE_GAP_EVT_DISCONNECTED:
nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);
m_conn_handle = BLE_CONN_HANDLE_INVALID;
advertising_start();
break;
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
err_code = sd_ble_gap_sec_params_reply(m_conn_handle,
BLE_GAP_SEC_STATUS_SUCCESS,
&m_sec_params);
APP_ERROR_CHECK(err_code);
break;
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0);
APP_ERROR_CHECK(err_code);
break;
case BLE_GAP_EVT_AUTH_STATUS:
m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status;
break;
case BLE_GAP_EVT_SEC_INFO_REQUEST:
p_enc_info = &m_auth_status.periph_keys.enc_info;
if (p_enc_info->div == p_ble_evt->evt.gap_evt.params.sec_info_request.div)
{
err_code = sd_ble_gap_sec_info_reply(m_conn_handle, p_enc_info, NULL);
APP_ERROR_CHECK(err_code);
}
else
{
// No keys found for this device
err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL);
APP_ERROR_CHECK(err_code);
}
break;
case BLE_GAP_EVT_TIMEOUT:
if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT)
{
nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
// Configure buttons with sense level low as wakeup source.
nrf_gpio_cfg_sense_input(WAKEUP_BUTTON_PIN,
BUTTON_PULL,
NRF_GPIO_PIN_SENSE_LOW);
// Go to system-off mode (this function will not return; wakeup will cause a reset)
err_code = sd_power_system_off();
APP_ERROR_CHECK(err_code);
}
break;
case BLE_EVT_USER_MEM_REQUEST:
sprintf((char *)buffer, "memory requested, 0x%d\r\n", p_ble_evt->evt.common_evt.params.user_mem_request.type);
simple_uart_putstring(buffer);
if(p_ble_evt->evt.common_evt.params.user_mem_request.type ==BLE_USER_MEM_TYPE_GATTS_QUEUED_WRITES)
{
//m_user_mem_release.mem_block !! Has to be set
m_mem_block.p_mem = &m_mem_queue[0];
m_mem_block.len = MEM_BLOCK_SIZE;
err_code = sd_ble_user_mem_reply(m_conn_handle , &m_mem_block);
APP_ERROR_CHECK(err_code);
}
break;
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
simple_uart_putstring("authorize\r\n");
m_rw_authorize_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
m_rw_authorize_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
if(p_ble_evt->evt.gatts_evt.params.write.op == BLE_GATTS_OP_PREP_WRITE_REQ)
{
err_code = sd_ble_gatts_rw_authorize_reply(m_conn_handle,&m_rw_authorize_reply);
APP_ERROR_CHECK(err_code);
} else if ( p_ble_evt->evt.gatts_evt.params.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW)
{
err_code = sd_ble_gatts_rw_authorize_reply(m_conn_handle,&m_rw_authorize_reply);
APP_ERROR_CHECK(err_code);
}
break;
case BLE_EVT_USER_MEM_RELEASE:
simple_uart_putstring("release\r\n");
if (p_ble_evt->evt.common_evt.params.user_mem_release.mem_block.p_mem == m_mem_block.p_mem)
// will fail if mem block is not set previously
{
// Do something
}
break;
case BLE_GATTS_EVT_WRITE:
//sprintf((char *)buffer, "event write\r\n");
//sprintf((char *)buffer, "<<0x%lx 0x%lx>>\r\n", p_ble_evt->evt.gatts_evt.params.write.len, *(uint8_t *)p_ble_evt->evt.gatts_evt.params.write.data);
simple_uart_putstring(buffer);
p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
if(p_evt_write->op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW)
{
if(*((uint16_t *)m_mem_block.p_mem) == m_nus.tx_handles.value_handle)
{
inData = 0;
inDataLen = sizeof(uint8_t);
sd_ble_gatts_value_get(m_conn_handle, 0, &inDataLen, &inData );
sprintf((char *)buffer, "||0x%lx||", inData);
simple_uart_putstring(buffer);
simple_uart_putstring("\r\n");
}
}
break;
default:
// No implementation needed.
break;
}
simple_uart_put(m_mem_queue[0]);
simple_uart_put(m_mem_queue[1]);
simple_uart_put(m_mem_queue[2]);
simple_uart_put(m_mem_queue[3]);
sprintf((char *)buffer, "\r\n Event:0x%lx\r\n", p_ble_evt->header.evt_id);
simple_uart_putstring(buffer);
}
I am using MCP to write long a text string. but at the PCA10001 side when I parse m_mem_queue to check the data there is noting inside. futhermore I notice that I never get into the BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST here is the output of my console
Start...
Event:0x10
Event:0x52
Event:0x50
memory requested, 0x1
Event:0x2
||0x0||
Event:0x50
release
Event:0x3