uint32_t ble_gatts_attr_enc(void const * const p_void_gatts_attr,
                            uint8_t * const    p_buf,
                            uint32_t           buf_len,
                            uint32_t * const   p_index)
{
    uint32_t           err_code     = NRF_SUCCESS;
    ble_gatts_attr_t * p_gatts_attr = (ble_gatts_attr_t *)p_void_gatts_attr;

    err_code = cond_field_enc((void *)p_gatts_attr->p_uuid, p_buf, buf_len, p_index, ble_uuid_t_enc);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_enc((void *)p_gatts_attr->p_attr_md,
                              p_buf,
                              buf_len,
                              p_index,
                              ble_gatts_attr_md_enc);
    SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_enc(&p_gatts_attr->init_offs, p_buf, buf_len, p_index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_enc(&p_gatts_attr->max_len, p_buf, buf_len, p_index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ERROR_CHECK(p_gatts_attr->init_len <= BLE_GATTS_VAR_ATTR_LEN_MAX, NRF_ERROR_INVALID_PARAM);
    //init len move just before p_data to be able to use len16data decoder.
    err_code = len16data_enc(p_gatts_attr->p_value, p_gatts_attr->init_len, p_buf, buf_len, p_index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    return err_code;
}
uint32_t ble_gatts_char_md_enc(void const * const p_void_char_md,
                               uint8_t * const    p_buf,
                               uint32_t           buf_len,
                               uint32_t * const   p_index)
{
    uint32_t err_code = NRF_SUCCESS;

    ble_gatts_char_md_t * p_char_md = (ble_gatts_char_md_t *)p_void_char_md;
    uint8_t               temp8;

    temp8 = p_char_md->char_props.broadcast |
            (p_char_md->char_props.read << 1) |
            (p_char_md->char_props.write_wo_resp << 2) |
            (p_char_md->char_props.write << 3) |
            (p_char_md->char_props.notify << 4) |
            (p_char_md->char_props.indicate << 5) |
            (p_char_md->char_props.auth_signed_wr << 6);

    err_code = uint8_t_enc(&temp8, p_buf, buf_len, p_index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    temp8 = p_char_md->char_ext_props.reliable_wr |
            (p_char_md->char_ext_props.wr_aux << 1);

    err_code = uint8_t_enc(&temp8, p_buf, buf_len, p_index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = uint16_t_enc(&p_char_md->char_user_desc_max_size, p_buf, buf_len, p_index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    SER_ERROR_CHECK(p_char_md->char_user_desc_size <= BLE_GATTS_VAR_ATTR_LEN_MAX,
                    NRF_ERROR_INVALID_PARAM);
    err_code = len16data_enc(p_char_md->p_char_user_desc, p_char_md->char_user_desc_size, p_buf,
                             buf_len, p_index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_enc(p_char_md->p_char_pf,
                              p_buf,
                              buf_len,
                              p_index,
                              ser_ble_gatts_char_pf_enc);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_enc(p_char_md->p_user_desc_md,
                              p_buf,
                              buf_len,
                              p_index,
                              ble_gatts_attr_md_enc);
    SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_enc(p_char_md->p_cccd_md, p_buf, buf_len, p_index, ble_gatts_attr_md_enc);
    SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code);

    err_code = cond_field_enc(p_char_md->p_sccd_md, p_buf, buf_len, p_index, ble_gatts_attr_md_enc);
    SER_ERROR_CHECK(err_code == NRF_SUCCESS, err_code);

    return err_code;
}
uint32_t ble_gatts_hvx_req_enc(uint16_t                             conn_handle,
                               ble_gatts_hvx_params_t const * const p_hvx_params,
                               uint8_t * const                      p_buf,
                               uint32_t * const                     p_buf_len)
{
    uint32_t index = 0;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_buf_len);

    SER_ASSERT(p_hvx_params == NULL ||
               !(p_hvx_params->p_len == NULL && p_hvx_params->p_data != NULL), NRF_ERROR_NULL);

    SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1 + 1, *p_buf_len);

    p_buf[index++] = SD_BLE_GATTS_HVX;
    index         += uint16_encode(conn_handle, &p_buf[index]);

    p_buf[index++] = (p_hvx_params != NULL) ? SER_FIELD_PRESENT : SER_FIELD_NOT_PRESENT;

    if (p_hvx_params != NULL)
    {
        SER_ASSERT_LENGTH_LEQ(index + 2 + 1 + 2 + 2, *p_buf_len);
        index         += uint16_encode(p_hvx_params->handle, &p_buf[index]);
        p_buf[index++] = p_hvx_params->type;
        index         += uint16_encode(p_hvx_params->offset, &p_buf[index]);

        if (p_hvx_params->p_len != NULL)
        {
            SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1, *p_buf_len);

            SER_ERROR_CHECK(*p_hvx_params->p_len <= BLE_GATTS_VAR_ATTR_LEN_MAX,
                            NRF_ERROR_INVALID_PARAM);
            p_buf[index++] = SER_FIELD_PRESENT;
            index         += uint16_encode(*(p_hvx_params->p_len), &p_buf[index]);

            if (p_hvx_params->p_data != NULL)
            {
                SER_ASSERT_LENGTH_LEQ(index + 1 + *(p_hvx_params->p_len), *p_buf_len);
                p_buf[index++] = SER_FIELD_PRESENT;
                memcpy(&(p_buf[index]), p_hvx_params->p_data, *(p_hvx_params->p_len));
                index += *(p_hvx_params->p_len);
            }
            else
            {
                p_buf[index++] = SER_FIELD_NOT_PRESENT;
            }
        }
        else
        {
            p_buf[index++] = SER_FIELD_NOT_PRESENT;
            p_buf[index++] = SER_FIELD_NOT_PRESENT;
        }
    }

    *p_buf_len = index;

    return NRF_SUCCESS;
}
示例#4
0
uint32_t ble_gap_device_name_set_req_enc(ble_gap_conn_sec_mode_t const * const p_write_perm,
                                         uint8_t const * const                 p_dev_name,
                                         uint16_t                              len,
                                         uint8_t * const                       p_buf,
                                         uint32_t * const                      p_buf_len)
{
    SER_REQ_ENC_BEGIN(SD_BLE_GAP_DEVICE_NAME_SET);
    
    SER_ERROR_CHECK(len <= BLE_GAP_DEVNAME_MAX_LEN, NRF_ERROR_INVALID_PARAM);

    SER_PUSH_COND(p_write_perm, ble_gap_conn_sec_mode_t_enc);
    SER_PUSH_len16data(p_dev_name, len);

    SER_REQ_ENC_END;
}
uint32_t ble_gatts_attr_t_enc(void const * const p_void_struct,
                              uint8_t * const    p_buf,
                              uint32_t           buf_len,
                              uint32_t * const   p_index)
{
    SER_STRUCT_ENC_BEGIN(ble_gatts_attr_t);

    SER_PUSH_COND(p_struct->p_uuid, ble_uuid_t_enc);
    SER_PUSH_COND(p_struct->p_attr_md, ble_gatts_attr_md_t_enc);
    SER_PUSH_uint16(&p_struct->init_offs);
    SER_PUSH_uint16(&p_struct->max_len);
    SER_ERROR_CHECK(p_struct->init_len <= BLE_GATTS_VAR_ATTR_LEN_MAX, NRF_ERROR_INVALID_PARAM);
    SER_PUSH_len16data(p_struct->p_value, p_struct->init_len);

    SER_STRUCT_ENC_END;
}
uint32_t ble_gattc_write_req_enc(uint16_t                               conn_handle,
                                 ble_gattc_write_params_t const * const p_write_params,
                                 uint8_t * const                        p_buf,
                                 uint32_t *                             p_buf_len)
{
    uint32_t index = 0;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_buf_len);

    SER_ASSERT_LENGTH_LEQ(index + 4, *p_buf_len);

    p_buf[index++] = SD_BLE_GATTC_WRITE;
    index         += uint16_encode(conn_handle, &p_buf[index]);
    p_buf[index++] = (p_write_params == NULL) ? SER_FIELD_NOT_PRESENT : SER_FIELD_PRESENT;

    if (p_write_params != NULL)
    {
        SER_ASSERT_LENGTH_LEQ(index + 9, *p_buf_len);
        p_buf[index++] = p_write_params->write_op;
        p_buf[index++] = p_write_params->flags;
        index         += uint16_encode(p_write_params->handle, &p_buf[index]);
        index         += uint16_encode(p_write_params->offset, &p_buf[index]);
        index         += uint16_encode(p_write_params->len, &p_buf[index]);

        SER_ERROR_CHECK(p_write_params->len <= BLE_GATTC_WRITE_P_VALUE_LEN_MAX,
                        NRF_ERROR_INVALID_PARAM);

        p_buf[index++] = (p_write_params->p_value == NULL) ?
                         SER_FIELD_NOT_PRESENT : SER_FIELD_PRESENT;

        if (p_write_params->p_value != NULL)
        {
            SER_ASSERT_LENGTH_LEQ(index + p_write_params->len, *p_buf_len);
            memcpy(&p_buf[index], p_write_params->p_value, p_write_params->len);
            index += p_write_params->len;
        }
    }

    *p_buf_len = index;

    return NRF_SUCCESS;
}
uint32_t ble_gatts_char_md_t_enc(void const * const p_void_struct,
                                 uint8_t * const    p_buf,
                                 uint32_t           buf_len,
                                 uint32_t * const   p_index)
{
    SER_STRUCT_ENC_BEGIN(ble_gatts_char_md_t);

    SER_PUSH_FIELD(&p_struct->char_props, ble_gatt_char_props_t_enc);
    SER_PUSH_FIELD(&p_struct->char_ext_props, ble_gatt_char_ext_props_t_enc);
    SER_PUSH_uint16(&p_struct->char_user_desc_max_size);
    SER_ERROR_CHECK(p_struct->char_user_desc_size <= BLE_GATTS_VAR_ATTR_LEN_MAX,
                    NRF_ERROR_INVALID_PARAM);
    SER_PUSH_len16data(p_struct->p_char_user_desc, p_struct->char_user_desc_size);
    SER_PUSH_COND(p_struct->p_char_pf, ble_gatts_char_pf_t_enc);
    SER_PUSH_COND(p_struct->p_user_desc_md, ble_gatts_attr_md_t_enc);
    SER_PUSH_COND(p_struct->p_cccd_md, ble_gatts_attr_md_t_enc);
    SER_PUSH_COND(p_struct->p_sccd_md, ble_gatts_attr_md_t_enc);

    SER_STRUCT_ENC_END;
}
uint32_t ble_gatts_sys_attr_set_req_enc(uint16_t              conn_handle,
                                        uint8_t const * const p_sys_attr_data,
                                        uint16_t              sys_attr_data_len,
                                        uint32_t              flags,
                                        uint8_t * const       p_buf,
                                        uint32_t * const      p_buf_len)
{
    uint32_t index = 0;
    uint32_t err_code = NRF_SUCCESS;

    SER_ASSERT_NOT_NULL(p_buf);
    SER_ASSERT_NOT_NULL(p_buf_len);

    SER_ASSERT_LENGTH_LEQ(index + 8, *p_buf_len);

    p_buf[index++] = SD_BLE_GATTS_SYS_ATTR_SET;
    index         += uint16_encode(conn_handle, &p_buf[index]);

    p_buf[index++] = (p_sys_attr_data != NULL) ? SER_FIELD_PRESENT : SER_FIELD_NOT_PRESENT;

    if (p_sys_attr_data != NULL)
    {
        //lint -save -esym(670,memcpy)
        SER_ERROR_CHECK(sys_attr_data_len <= BLE_GATTS_VAR_ATTR_LEN_MAX, NRF_ERROR_INVALID_PARAM);
        SER_ASSERT_LENGTH_LEQ(index + 2 + sys_attr_data_len + 4, *p_buf_len);
        index += uint16_encode(sys_attr_data_len, &p_buf[index]);
        memcpy(&(p_buf[index]), p_sys_attr_data, sys_attr_data_len);
        //lint -restore
        index += sys_attr_data_len;
    }
    
    err_code = uint32_t_enc(&flags, p_buf, *p_buf_len, &index);
    SER_ASSERT(err_code == NRF_SUCCESS, err_code);
    
    *p_buf_len = index;

    return NRF_SUCCESS;
}