uint32_t ANT_ENABLE_dec(uint8_t const * const p_buf,
                        uint32_t              buf_len,
                        uint32_t * const      p_index,
                        void * const          p_void_enable_params)

{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_index);
    SER_ASSERT_NOT_NULL(p_void_enable_params);

    ANT_ENABLE * p_enable_params = (ANT_ENABLE *)p_void_enable_params;
    uint32_t err_code = NRF_SUCCESS;

    err_code = uint8_t_dec(p_buf, buf_len, p_index, &p_enable_params->ucTotalNumberOfChannels);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, buf_len, p_index, &p_enable_params->ucNumberOfEncryptedChannels);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_dec(p_buf, buf_len, p_index, &p_enable_params->usNumberOfEvents);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint32_t_dec(p_buf, buf_len, p_index, &p_enable_params->pucMemoryBlockStartLocation);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_dec(p_buf, buf_len, p_index, &p_enable_params->usMemoryBlockByteSize);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    return err_code;
}
uint32_t ble_gattc_write_params_t_dec(uint8_t const * const p_buf,
                                      uint32_t              buf_len,
                                      uint32_t * const      p_index,
                                      void * const          p_void_write)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_index);
    SER_ASSERT_NOT_NULL(p_void_write);

    uint32_t err_code = NRF_SUCCESS;

    ble_gattc_write_params_t * p_write = (ble_gattc_write_params_t *)p_void_write;

    err_code = uint8_t_dec(p_buf, buf_len, p_index, &(p_write->write_op));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, buf_len, p_index, &(p_write->flags));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_write->handle));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_write->offset));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = len16data_dec(p_buf, buf_len, p_index, &(p_write->p_value), &(p_write->len));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    return err_code;
}
uint32_t ble_gatts_evt_write_t_dec(uint8_t const * const p_buf,
                                   uint32_t              buf_len,
                                   uint32_t * const      p_index,
                                   uint32_t * const      p_struct_len,
                                   void * const          p_void_write)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_index);
    SER_ASSERT_NOT_NULL(p_struct_len);

    uint32_t err_code      = NRF_SUCCESS;
    uint32_t in_struct_len = *p_struct_len;

    *p_struct_len = offsetof(ble_gatts_evt_write_t, data);

    uint16_t handle;
    err_code = uint16_t_dec(p_buf, buf_len, p_index, &handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    uint8_t op;
    err_code = uint8_t_dec(p_buf, buf_len, p_index, &op);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    ble_gatts_attr_context_t context;
    err_code = ble_gatts_attr_context_t_dec(p_buf, buf_len, p_index, &context);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    uint16_t offset;
    err_code = uint16_t_dec(p_buf, buf_len, p_index, &offset);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    uint16_t len;
    err_code = uint16_t_dec(p_buf, buf_len, p_index, &len);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    *p_struct_len += len;

    if (p_void_write != NULL)
    {
        ble_gatts_evt_write_t * p_write = (ble_gatts_evt_write_t *)p_void_write;

        SER_ASSERT_LENGTH_LEQ(*p_struct_len, in_struct_len);

        p_write->handle = handle;
        p_write->op     = op;

        memcpy(&(p_write->context), &context, sizeof (ble_gatts_attr_context_t));

        p_write->offset = offset;
        p_write->len    = len;

        SER_ASSERT_LENGTH_LEQ(p_write->len, buf_len - *p_index);
        memcpy(p_write->data, &p_buf[*p_index], p_write->len);
    }

    *p_index += len;

    return err_code;
}
uint32_t ble_evt_user_mem_release_dec(uint8_t const * const p_buf,
                                      uint32_t              packet_len,
                                      ble_evt_t * const     p_event,
                                      uint32_t * const      p_event_len)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event);
    SER_ASSERT_NOT_NULL(p_event_len);

    uint32_t index        = 0;
    uint32_t err_code     = NRF_SUCCESS;

    uint32_t event_len = (uint16_t) (offsetof(ble_evt_t, evt.common_evt.params.user_mem_release)) +
                         sizeof (ble_evt_user_mem_release_t) -
                         sizeof (ble_evt_hdr_t);

    if (p_event == NULL)
    {
        *p_event_len = event_len;
        return NRF_SUCCESS;
    }

    p_event->header.evt_id  = BLE_EVT_USER_MEM_RELEASE;
    p_event->header.evt_len = event_len;
    ble_evt_user_mem_release_t * p_user_mem_rel = &(p_event->evt.common_evt.params.user_mem_release);

    err_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.common_evt.conn_handle));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, packet_len, &index, &(p_user_mem_rel->type));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    // Decoding order of mem block is different than structure elements order - length is decoded first
    err_code = uint16_t_dec(p_buf, packet_len, &index, &(p_user_mem_rel->mem_block.len));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    if (p_buf[index++] == SER_FIELD_PRESENT)
    {
        // Using connection handle find which mem block to release in Application Processor
        uint32_t user_mem_table_index;
        err_code = app_ble_user_mem_context_find(p_event->evt.common_evt.conn_handle, &user_mem_table_index);
        SER_ASSERT(err_code == NRF_SUCCESS, err_code);
        p_user_mem_rel->mem_block.p_mem = m_app_user_mem_table[user_mem_table_index].mem_block.p_mem;
    }
    else
    {
        p_user_mem_rel->mem_block.p_mem = NULL;
    }

    // Now user memory context can be released
    err_code = app_ble_user_mem_context_destroy(p_event->evt.common_evt.conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);
    *p_event_len = event_len;

    return err_code;
}
uint32_t ble_gap_disconnect_req_dec(uint8_t const * const p_buf,
                                    uint32_t              packet_len,
                                    uint16_t * const      p_conn_handle,
                                    uint8_t * const       p_hci_status)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_conn_handle);
    SER_ASSERT_NOT_NULL(p_hci_status);

    uint8_t  opcode;
    uint32_t index    = 0;
    uint32_t err_code = NRF_SUCCESS;

    err_code = uint8_t_dec(p_buf, packet_len, &index, &opcode);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);
    SER_ASSERT(opcode == SD_BLE_GAP_DISCONNECT, NRF_ERROR_INVALID_PARAM);

    err_code = uint16_t_dec(p_buf, packet_len, &index, p_conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, packet_len, &index, p_hci_status);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return err_code;
}
uint32_t ant_channel_id_get_rsp_dec(uint8_t const * const p_buf,
                                    uint32_t              packet_len,
                                    void * const          p_device_number,
                                    void * const          p_device_type,
                                    void * const          p_transmit_type,
                                    uint32_t * const      p_result_code)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_result_code);

    uint32_t index    = 0;
    uint32_t err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len,
                                                        SVC_ANT_CHANNEL_ID_GET, p_result_code);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    if (*p_result_code != NRF_SUCCESS)
    {
        SER_ASSERT_LENGTH_EQ(index, packet_len);

        return NRF_SUCCESS;
    }

    err_code = uint16_t_dec(p_buf, packet_len, &index, p_device_number);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, packet_len, &index, p_device_type);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, packet_len, &index, p_transmit_type);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return NRF_SUCCESS;
}
uint32_t ble_gattc_evt_rel_disc_rsp_t_dec(uint8_t const * const p_buf,
                                          uint32_t              buf_len,
                                          uint32_t * const      p_index,
                                          void * const          p_void_struct)
{
    uint32_t                       error_code = NRF_SUCCESS;
    ble_gattc_evt_rel_disc_rsp_t * p_rsp      = (ble_gattc_evt_rel_disc_rsp_t *) p_void_struct;
    uint16_t                       include_count;
    uint32_t                       i;

    error_code = uint16_t_dec(p_buf, buf_len, p_index, &include_count);
    SER_ASSERT(error_code == NRF_SUCCESS, error_code);
    p_rsp->count = include_count;

    ble_gattc_include_t * p_include = (ble_gattc_include_t *) p_rsp->includes;

    for (i = 0; i < include_count; i++)
    {
        error_code = ble_gattc_include_t_dec(p_buf, buf_len, p_index, p_include);
        SER_ASSERT(error_code == NRF_SUCCESS, error_code);

        p_include++;
    }

    return error_code;
}
uint32_t ble_gap_authenticate_req_dec(uint8_t const * const          p_buf,
                                      uint32_t                       packet_len,
                                      uint16_t * const               p_conn_handle,
                                      ble_gap_sec_params_t * * const pp_sec_params)
{
    uint32_t index = SER_CMD_DATA_POS;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_conn_handle);
    SER_ASSERT_NOT_NULL(pp_sec_params);
    SER_ASSERT_NOT_NULL(*pp_sec_params);

    uint32_t err_code = NRF_SUCCESS;

    err_code = uint16_t_dec(p_buf, packet_len, &index, p_conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_dec(p_buf,
                              packet_len,
                              &index,
                              (void * *)pp_sec_params,
                              ble_gap_sec_params_t_dec);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return err_code;
}
uint32_t ble_gap_lesc_oob_data_get_req_dec(uint8_t const * const         p_buf,
                                          uint32_t                       packet_len,
                                          uint16_t *                     p_conn_handle,
                                          ble_gap_lesc_p256_pk_t * *     pp_pk_own,
                                          ble_gap_lesc_oob_data_t * *    pp_oobd_own)
{
    uint32_t index    = SER_CMD_HEADER_SIZE;
    uint32_t err_code = NRF_SUCCESS;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_conn_handle);
    SER_ASSERT_NOT_NULL(pp_oobd_own);
    SER_ASSERT_NOT_NULL(pp_pk_own);

    err_code = uint16_t_dec(p_buf, packet_len, &index, p_conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_dec(p_buf, packet_len, &index, (void **)pp_pk_own, ble_gap_lesc_p256_pk_t_dec);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_dec(p_buf, packet_len, &index, (void **)pp_oobd_own, NULL);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return err_code;
}
Beispiel #10
0
uint32_t ble_gatts_value_get_req_dec(uint8_t const * const       p_buf,
                                     uint16_t                    packet_len,
                                     uint16_t * const            p_conn_handle,
                                     uint16_t * const            p_handle,
                                     ble_gatts_value_t * * const pp_value)
{
    uint32_t index    = 0;
    uint32_t err_code = NRF_SUCCESS;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_conn_handle);
    SER_ASSERT_NOT_NULL(p_handle);
    SER_ASSERT_NOT_NULL(pp_value);
    SER_ASSERT_NOT_NULL(*pp_value);
    SER_ASSERT_NOT_NULL((*pp_value)->p_value);
    SER_ASSERT_LENGTH_LEQ(SER_CMD_HEADER_SIZE + 5, packet_len);

    SER_ASSERT(p_buf[index] == SD_BLE_GATTS_VALUE_GET, NRF_ERROR_INVALID_PARAM);
    index++;

    err_code = uint16_t_dec(p_buf, packet_len, &index, p_conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_dec(p_buf, packet_len, &index, p_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    if (p_buf[index++] == SER_FIELD_PRESENT)
    {
        err_code = uint16_t_dec(p_buf, packet_len, &index, &((*pp_value)->len));
        SER_ASSERT(err_code == NRF_SUCCESS, err_code);

        err_code = uint16_t_dec(p_buf, packet_len, &index, &((*pp_value)->offset));
        SER_ASSERT(err_code == NRF_SUCCESS, err_code);

        SER_ASSERT_LENGTH_LEQ(1, packet_len - index);
        if (p_buf[index++] == SER_FIELD_NOT_PRESENT)
        {
            (*pp_value)->p_value = NULL;
        }
    }
    else
    {
        *pp_value = NULL;
    }

    return err_code;
}
uint32_t ble_gatts_evt_rw_authorize_request_dec(uint8_t const * const p_buf,
                                                uint32_t              packet_len,
                                                ble_evt_t * const     p_event,
                                                uint32_t * const      p_event_len)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event_len);

    uint32_t index    = 0;
    uint32_t err_code = NRF_SUCCESS;

    uint32_t in_event_len = *p_event_len;

    *p_event_len = offsetof(ble_evt_t, evt.gatts_evt.params) - sizeof (ble_evt_hdr_t);

    uint16_t conn_handle;
    err_code = uint16_t_dec(p_buf, packet_len, &index, &conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    void * p_void_authorize_request = NULL;

    if (p_event != NULL)
    {
        SER_ASSERT_LENGTH_LEQ(*p_event_len, in_event_len);

        p_event->header.evt_id             = BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST;
        p_event->evt.gatts_evt.conn_handle = conn_handle;

        p_void_authorize_request = &(p_event->evt.gatts_evt.params.authorize_request);
    }
    uint32_t tmp_event_len = in_event_len - *p_event_len;
    err_code = ble_gatts_evt_rw_authorize_request_t_dec(p_buf,
                                                        packet_len,
                                                        &index,
                                                        &tmp_event_len,
                                                        p_void_authorize_request);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    *p_event_len += tmp_event_len;
    
    if(p_event != NULL)
    {
        if((p_event->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) && (p_event->evt.gatts_evt.params.authorize_request.request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW))
        {
            uint32_t conn_index;
        
            if(app_ble_user_mem_context_find(p_event->evt.gatts_evt.conn_handle, &conn_index) != NRF_ERROR_NOT_FOUND)
            {      
                err_code = len16data_dec(p_buf, packet_len, &index, &m_app_user_mem_table[conn_index].mem_block.p_mem, &m_app_user_mem_table[conn_index].mem_block.len);
                SER_ASSERT(err_code == NRF_SUCCESS, err_code);
            }
        }
    }

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return err_code;
}
uint32_t ble_gattc_evt_rel_disc_rsp_dec(uint8_t const * const p_buf,
                                        uint32_t              packet_len,
                                        ble_evt_t * const     p_event,
                                        uint32_t * const      p_event_len)
{
    uint32_t index         = 0;
    uint32_t event_len     = 0;
    uint16_t include_count = 0;

    uint32_t error_code = NRF_SUCCESS;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event_len);

    SER_ASSERT_LENGTH_LEQ(8, packet_len);

    include_count = uint16_decode(&p_buf[BLE_GATTC_EVT_REL_DISC_RSP_COUNT_POSITION]);
    event_len     = (uint16_t) (offsetof(ble_evt_t, evt.gattc_evt.params.rel_disc_rsp.includes)) -
                    sizeof (ble_evt_hdr_t) + (include_count * sizeof (ble_gattc_include_t));

    if (p_event == NULL)
    {
        *p_event_len = event_len;
        return NRF_SUCCESS;
    }

    SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE);

    p_event->header.evt_id = BLE_GATTC_EVT_REL_DISC_RSP;
    error_code             =
        uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.conn_handle));
    SER_ASSERT(error_code == NRF_SUCCESS, error_code);
    error_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.gatt_status));
    SER_ASSERT(error_code == NRF_SUCCESS, error_code);
    error_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gattc_evt.error_handle));
    SER_ASSERT(error_code == NRF_SUCCESS, error_code);
    error_code =
        ble_gattc_evt_rel_disc_rsp_t_dec(p_buf, packet_len, &index,
                                         &(p_event->evt.gattc_evt.params.rel_disc_rsp));
    SER_ASSERT(error_code == NRF_SUCCESS, error_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return error_code;
}
uint32_t ble_gap_evt_adv_report_dec(uint8_t const * const p_buf,
                                    uint32_t              packet_len,
                                    ble_evt_t * const     p_event,
                                    uint32_t * const      p_event_len)
{
    uint32_t index = 0;
    uint32_t err_code = NRF_SUCCESS;
    uint8_t  byte;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event_len);

    SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE+7+1+1, packet_len); // assumed size(data) = 0

    uint32_t event_len = (uint16_t) (offsetof(ble_evt_t, evt.gap_evt.params.adv_report)) +
                         sizeof (ble_gap_evt_adv_report_t) -
                         sizeof (ble_evt_hdr_t);

    if (p_event == NULL)
    {
        *p_event_len = event_len;
        return NRF_SUCCESS;
    }

    SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE);

    p_event->header.evt_id  = BLE_GAP_EVT_ADV_REPORT;
    p_event->header.evt_len = event_len;

    err_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.conn_handle));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = ble_gap_addr_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.params.adv_report.peer_addr));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.params.adv_report.rssi));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, packet_len, &index, &byte);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    p_event->evt.gap_evt.params.adv_report.scan_rsp = byte & 0x01;
    p_event->evt.gap_evt.params.adv_report.type = (byte >> 1) & 0x03;
    p_event->evt.gap_evt.params.adv_report.dlen = (byte >> 3) & 0x1F;

    err_code = uint8_vector_dec(p_buf, packet_len, &index,
        p_event->evt.gap_evt.params.adv_report.data, (uint16_t)(p_event->evt.gap_evt.params.adv_report.dlen));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);
    *p_event_len = event_len;

    return err_code;
}
Beispiel #14
0
uint32_t ble_l2cap_header_t_dec(uint8_t const * const p_buf,
                                uint32_t              buf_len,
                                uint32_t * const      p_index,
                                void * const          p_void_header)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_index);
    SER_ASSERT_NOT_NULL(p_void_header);

    ble_l2cap_header_t * p_header = (ble_l2cap_header_t *)p_void_header;
    uint32_t             err_code = NRF_SUCCESS;

    err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_header->len));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_header->cid));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    return err_code;
}
uint32_t ble_gatts_value_t_dec(uint8_t const * const p_buf,
                               uint32_t              buf_len,
                               uint32_t * const      p_index,
                               void * const          p_void_struct)
{
    ble_gatts_value_t * p_value  = (ble_gatts_value_t *)p_void_struct;
    uint32_t            err_code = NRF_SUCCESS;
    uint16_t            total_len = p_value->len;

    err_code = uint16_t_dec(p_buf, buf_len, p_index, (void *) &(p_value->len));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);
    
    err_code = uint16_t_dec(p_buf, buf_len, p_index, (void *) &(p_value->offset));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);
    
    err_code = buf_dec(p_buf, buf_len, p_index, &(p_value->p_value), total_len, p_value->len);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    return err_code;
}
uint32_t ble_gatts_evt_read_t_dec(uint8_t const * const p_buf,
                                  uint32_t              buf_len,
                                  uint32_t * const      p_index,
                                  uint32_t * const      p_struct_len,
                                  void * const          p_void_read)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_index);

    uint32_t err_code      = NRF_SUCCESS;
    uint32_t in_struct_len = *p_struct_len;

    *p_struct_len = sizeof (ble_gatts_evt_read_t);

    uint16_t handle;
    err_code = uint16_t_dec(p_buf, buf_len, p_index, &handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    ble_gatts_attr_context_t context;
    err_code = ble_gatts_attr_context_t_dec(p_buf, buf_len, p_index, &context);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    uint16_t offset;
    err_code = uint16_t_dec(p_buf, buf_len, p_index, &offset);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    if (p_void_read != NULL)
    {
        ble_gatts_evt_read_t * p_read = (ble_gatts_evt_read_t *)p_void_read;

        SER_ASSERT_LENGTH_LEQ(*p_struct_len, in_struct_len);

        p_read->handle = handle;
        memcpy(&(p_read->context), &context, sizeof (context));
        p_read->offset = offset;
    }

    return err_code;
}
uint32_t ble_gap_evt_lesc_dhkey_request_dec(uint8_t const * const p_buf,
                                            uint32_t              packet_len,
                                            ble_evt_t * const     p_event,
                                            uint32_t * const      p_event_len)
{
    uint32_t index    = 0;
    uint32_t err_code = NRF_SUCCESS;
    uint32_t conn_index;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event_len);
    // [minimal packet is: 'conn_handle' + 'p_pk_peer' SER_FIELD_NOT_PRESENT +
    //  'oobd_req']
    SER_ASSERT_LENGTH_LEQ(sizeof(uint16_t) + sizeof(uint8_t) + sizeof(uint8_t),
        packet_len);

    uint32_t event_len = offsetof (ble_gap_evt_t, params) + sizeof(uint8_t) + sizeof(uint8_t);

    if (p_event == NULL)
    {
        *p_event_len = event_len;
        return NRF_SUCCESS;
    }

    SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE);

    p_event->header.evt_len = event_len;

    err_code = uint16_t_dec(p_buf, packet_len, &index, &p_event->evt.gap_evt.conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    // keyset is an extension of standard event data - used to synchronize keys at application
    err_code = app_ble_gap_sec_context_find(p_event->evt.gap_evt.conn_handle, &conn_index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    p_event->evt.gap_evt.params.lesc_dhkey_request.p_pk_peer = m_app_keys_table[conn_index].keyset.keys_peer.p_pk;

    err_code = cond_field_dec(p_buf, packet_len, &index,
        (void **)&p_event->evt.gap_evt.params.lesc_dhkey_request.p_pk_peer, ble_gap_lesc_p256_pk_t_dec);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    uint8_t data;
    err_code = uint8_t_dec(p_buf, packet_len, &index, &data);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);
    p_event->evt.gap_evt.params.lesc_dhkey_request.oobd_req = data & 0x01;

    SER_ASSERT_LENGTH_EQ(index, packet_len);
    *p_event_len = event_len;

    return err_code;
}
uint32_t ble_gattc_include_t_dec(uint8_t const * const p_buf,
                                 uint32_t              buf_len,
                                 uint32_t * const      p_index,
                                 void * const          p_void_struct)
{
    uint32_t              error_code = NRF_SUCCESS;
    ble_gattc_include_t * p_include  = (ble_gattc_include_t *) p_void_struct;

    error_code = uint16_t_dec(p_buf, buf_len, p_index, &(p_include->handle));
    SER_ASSERT(error_code == NRF_SUCCESS, error_code);
    error_code = ble_gattc_service_t_dec(p_buf, buf_len, p_index, &(p_include->included_srvc));
    SER_ASSERT(error_code == NRF_SUCCESS, error_code);

    return error_code;
}
uint32_t ble_gap_evt_auth_status_dec(uint8_t const * const p_buf,
                                     uint32_t              packet_len,
                                     ble_evt_t * const     p_event,
                                     uint32_t * const      p_event_len)
{
    uint32_t index = 0;
    uint32_t err_code = NRF_SUCCESS;
    uint32_t conn_index;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event_len);

    SER_ASSERT_LENGTH_LEQ(SER_EVT_CONN_HANDLE_SIZE + 6, packet_len);

    uint32_t event_len = (uint16_t) (offsetof(ble_evt_t, evt.gap_evt.params.auth_status)) +
                         sizeof (ble_gap_evt_adv_report_t) -
                         sizeof (ble_evt_hdr_t);

    if (p_event == NULL)
    {
        *p_event_len = event_len;
        return NRF_SUCCESS;
    }

    p_event->header.evt_id  = BLE_GAP_EVT_AUTH_STATUS;
    p_event->header.evt_len = event_len;

    err_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.conn_handle));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = ble_gap_evt_auth_status_t_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.params.auth_status));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    // keyset is an extension of standard event data - used to synchronize keys at application
    err_code = app_ble_gap_sec_context_find(p_event->evt.gap_evt.conn_handle, &conn_index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = ble_gap_sec_keyset_t_dec(p_buf, packet_len, &index, &(m_app_keys_table[conn_index].keyset));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = app_ble_gap_sec_context_destroy(p_event->evt.gap_evt.conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);
    *p_event_len = event_len;

    return err_code;
}
uint32_t len16data_dec(uint8_t const * const p_buf,
                       uint32_t              buf_len,
                       uint32_t * const      p_index,
                       uint8_t * * const     pp_data,
                       uint16_t * const      p_dlen)
{
    uint32_t err_code    = NRF_SUCCESS;
    uint16_t out_buf_len = *p_dlen;

    err_code = uint16_t_dec(p_buf, buf_len, p_index, p_dlen);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = buf_dec(p_buf, buf_len, p_index, pp_data, out_buf_len, *p_dlen);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    return err_code;
}
uint32_t ble_gatts_evt_rw_authorize_request_dec(uint8_t const * const p_buf,
                                                uint32_t              packet_len,
                                                ble_evt_t * const     p_event,
                                                uint32_t * const      p_event_len)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event_len);

    uint32_t index    = 0;
    uint32_t err_code = NRF_SUCCESS;

    uint32_t in_event_len = *p_event_len;

    *p_event_len = offsetof(ble_evt_t, evt.gatts_evt.params) - sizeof (ble_evt_hdr_t);

    uint16_t conn_handle;
    err_code = uint16_t_dec(p_buf, packet_len, &index, &conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    void * p_void_authorize_request = NULL;

    if (p_event != NULL)
    {
        SER_ASSERT_LENGTH_LEQ(*p_event_len, in_event_len);

        p_event->header.evt_id             = BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST;
        p_event->evt.gatts_evt.conn_handle = conn_handle;

        p_void_authorize_request = &(p_event->evt.gatts_evt.params.authorize_request);
    }
    uint32_t tmp_event_len = in_event_len - *p_event_len;
    err_code = ble_gatts_evt_rw_authorize_request_t_dec(p_buf,
                                                        packet_len,
                                                        &index,
                                                        &tmp_event_len,
                                                        p_void_authorize_request);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    *p_event_len += tmp_event_len;

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return err_code;
}
uint32_t ble_gatts_evt_write_dec(uint8_t const * const p_buf,
                                 uint32_t              packet_len,
                                 ble_evt_t * const     p_event,
                                 uint32_t * const      p_event_len)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event_len);

    uint32_t err_code = NRF_SUCCESS;
    uint32_t index    = 0;

    uint32_t in_event_len = *p_event_len;

    *p_event_len = offsetof(ble_evt_t, evt.gatts_evt.params) - sizeof (ble_evt_hdr_t);

    uint16_t conn_handle;
    err_code = uint16_t_dec(p_buf, packet_len, &index, &conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    void * p_void_write = NULL;

    if (p_event != NULL)
    {
        SER_ASSERT_LENGTH_LEQ(*p_event_len, in_event_len);
        p_event->evt.gatts_evt.conn_handle = conn_handle;

        p_void_write = &(p_event->evt.gatts_evt.params.write);
    }

    uint32_t tmp_struct_len = in_event_len - *p_event_len;
    err_code = ble_gatts_evt_write_t_dec(p_buf,
                                         packet_len,
                                         &index,
                                         &tmp_struct_len,
                                         p_void_write);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    *p_event_len += tmp_struct_len;

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return err_code;
}
uint32_t ble_l2cap_evt_rx_t_dec(uint8_t const * const p_buf,
                                uint32_t              buf_len,
                                uint32_t * const      p_index,
                                uint32_t * const      p_struct_len,
                                void * const          p_void_evt_rx)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_index);
    SER_ASSERT_NOT_NULL(p_struct_len);

    ble_l2cap_evt_rx_t * p_evt_rx = (ble_l2cap_evt_rx_t *)p_void_evt_rx;
    uint32_t             err_code = NRF_SUCCESS;

    uint32_t total_struct_len = *p_struct_len;

    /* Get data length */
    uint32_t tmp_index = *p_index;
    uint16_t len       = 0;

    err_code = uint16_t_dec(p_buf, buf_len, &tmp_index, &len);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    /* Update struct length */
    *p_struct_len = offsetof(ble_l2cap_evt_rx_t, data[0]);
	*p_struct_len += (uint8_t*)&(p_evt_rx->data[len]) - (uint8_t*)&(p_evt_rx->data[0]);


    /* Decode header and copy data */
    if (p_void_evt_rx != NULL)
    {
        SER_ASSERT_LENGTH_LEQ(*p_struct_len, total_struct_len);

        err_code = ble_l2cap_header_t_dec(p_buf, buf_len, p_index, &(p_evt_rx->header));
        SER_ASSERT(err_code == NRF_SUCCESS, err_code);

        SER_ASSERT_LENGTH_LEQ(p_evt_rx->header.len, buf_len - *p_index);
        memcpy(p_evt_rx->data, &p_buf[*p_index], p_evt_rx->header.len);
        *p_index += p_evt_rx->header.len;
    }

    return err_code;
}
uint32_t ble_l2cap_cid_register_req_dec(uint8_t const * const p_buf,
                                        uint32_t              buf_len,
                                        uint16_t *            p_cid)
{
    uint32_t index    = 0;
    uint32_t err_code = NRF_SUCCESS;

    SER_ASSERT_NOT_NULL(p_cid);
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_LENGTH_EQ(3, buf_len);
    SER_ASSERT(p_buf[index] == SD_BLE_L2CAP_CID_REGISTER, NRF_ERROR_INVALID_PARAM);

    index++;
    err_code = uint16_t_dec(p_buf, buf_len, &index, p_cid);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, buf_len);

    return err_code;
}
uint32_t ant_channel_open_with_offset_req_dec(uint8_t const * const p_buf,
                                              uint32_t              packet_len,
                                              uint8_t * const       p_channel,
                                              uint16_t * const      p_usOffset)
{
    uint32_t index = SER_CMD_DATA_POS;
    uint32_t err_code;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_channel);

    err_code = uint8_t_dec(p_buf, packet_len, &index, p_channel);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_dec(p_buf, packet_len, &index, p_usOffset);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return err_code;
}
Beispiel #26
0
uint32_t ble_common_enable_params_t_dec(uint8_t const * const p_buf,
                                        uint32_t              buf_len,
                                        uint32_t * const      p_index,
                                        void * const          p_void_common_enable_params)
{
    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_index);
    SER_ASSERT_NOT_NULL(p_void_common_enable_params);

    ble_common_enable_params_t * p_common_enable_params = (ble_common_enable_params_t *)p_void_common_enable_params;
    uint32_t              err_code        = NRF_SUCCESS;

    err_code = uint16_t_dec(p_buf, buf_len, p_index, &p_common_enable_params->vs_uuid_count);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_dec(p_buf, buf_len, p_index, (void **) &(p_common_enable_params->p_conn_bw_counts),
            ble_conn_bw_counts_t_dec);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    return err_code;
}
uint32_t ble_gap_evt_scan_req_report_dec(uint8_t const * const p_buf,
                                         uint32_t              packet_len,
                                         ble_evt_t * const     p_event,
                                         uint32_t * const      p_event_len)
{
    uint32_t index = 0;
    uint32_t err_code = NRF_SUCCESS;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event_len);

    uint32_t event_len = (uint16_t) (offsetof(ble_evt_t, evt.gap_evt.params.scan_req_report)) +
                         sizeof (ble_gap_evt_scan_req_report_t) -
                         sizeof (ble_evt_hdr_t);

    if (p_event == NULL)
    {
        *p_event_len = event_len;
        return NRF_SUCCESS;
    }

    SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE);

    p_event->header.evt_id  = BLE_GAP_EVT_SCAN_REQ_REPORT;
    p_event->header.evt_len = event_len;

    err_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.conn_handle));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = ble_gap_addr_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.params.scan_req_report.peer_addr));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.params.scan_req_report.rssi));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);
    *p_event_len = event_len;

    return err_code;
}
Beispiel #28
0
uint32_t ble_gap_keypress_notify_req_dec(uint8_t const * const          p_buf,
                                          uint32_t                       packet_len,
                                          uint16_t *                     p_conn_handle,
                                          uint8_t *                      p_kp_not)
{
    uint32_t index    = SER_CMD_HEADER_SIZE;
    uint32_t err_code = NRF_SUCCESS;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_conn_handle);
    SER_ASSERT_NOT_NULL(p_kp_not);

    err_code = uint16_t_dec(p_buf, packet_len, &index, p_conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint8_t_dec(p_buf, packet_len, &index, p_kp_not);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return err_code;
}
Beispiel #29
0
uint32_t ble_gap_lesc_dhkey_reply_req_dec(uint8_t const * const          p_buf,
                                          uint32_t                       packet_len,
                                          uint16_t *                     p_conn_handle,
                                          ble_gap_lesc_dhkey_t * *       pp_dhkey)
{
    uint32_t index    = SER_CMD_HEADER_SIZE;
    uint32_t err_code = NRF_SUCCESS;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_conn_handle);
    SER_ASSERT_NOT_NULL(pp_dhkey);

    err_code = uint16_t_dec(p_buf, packet_len, &index, p_conn_handle);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_dec(p_buf, packet_len, &index, (void **)pp_dhkey, ble_gap_lesc_dhkey_t_dec);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    return err_code;
}
uint32_t ble_gap_evt_sec_params_request_dec(uint8_t const * const p_buf,
                                            uint32_t              packet_len,
                                            ble_evt_t * const     p_event,
                                            uint32_t * const      p_event_len)
{
    uint32_t index = 0;
    uint32_t event_len;
    uint32_t err_code = NRF_SUCCESS;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_event_len);

    SER_ASSERT_LENGTH_LEQ(7, packet_len);

    event_len = SER_EVT_CONN_HANDLE_SIZE + sizeof (ble_gap_evt_sec_params_request_t);

    if (p_event == NULL)
    {
        *p_event_len = event_len;
        return NRF_SUCCESS;
    }

    SER_ASSERT(event_len <= *p_event_len, NRF_ERROR_DATA_SIZE);
    p_event->header.evt_id  = BLE_GAP_EVT_SEC_PARAMS_REQUEST;
    p_event->header.evt_len = event_len;

    err_code = uint16_t_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.conn_handle));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = ble_gap_evt_sec_params_request_t_dec(p_buf, packet_len, &index, &(p_event->evt.gap_evt.params.sec_params_request));
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ASSERT_LENGTH_EQ(index, packet_len);

    *p_event_len = event_len;

    return err_code;
}