Committer:
jinu
Date:
Thu Feb 09 06:08:17 2017 +0000
Revision:
0:6ba9b94b8997
NRF51 serialization libraries for mDot

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jinu 0:6ba9b94b8997 1 /* Copyright (c) Nordic Semiconductor ASA
jinu 0:6ba9b94b8997 2 * All rights reserved.
jinu 0:6ba9b94b8997 3 *
jinu 0:6ba9b94b8997 4 * Redistribution and use in source and binary forms, with or without modification,
jinu 0:6ba9b94b8997 5 * are permitted provided that the following conditions are met:
jinu 0:6ba9b94b8997 6 *
jinu 0:6ba9b94b8997 7 * 1. Redistributions of source code must retain the above copyright notice, this
jinu 0:6ba9b94b8997 8 * list of conditions and the following disclaimer.
jinu 0:6ba9b94b8997 9 *
jinu 0:6ba9b94b8997 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this
jinu 0:6ba9b94b8997 11 * list of conditions and the following disclaimer in the documentation and/or
jinu 0:6ba9b94b8997 12 * other materials provided with the distribution.
jinu 0:6ba9b94b8997 13 *
jinu 0:6ba9b94b8997 14 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
jinu 0:6ba9b94b8997 15 * contributors to this software may be used to endorse or promote products
jinu 0:6ba9b94b8997 16 * derived from this software without specific prior written permission.
jinu 0:6ba9b94b8997 17 *
jinu 0:6ba9b94b8997 18 * 4. This software must only be used in a processor manufactured by Nordic
jinu 0:6ba9b94b8997 19 * Semiconductor ASA, or in a processor manufactured by a third party that
jinu 0:6ba9b94b8997 20 * is used in combination with a processor manufactured by Nordic Semiconductor.
jinu 0:6ba9b94b8997 21 *
jinu 0:6ba9b94b8997 22 *
jinu 0:6ba9b94b8997 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
jinu 0:6ba9b94b8997 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
jinu 0:6ba9b94b8997 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
jinu 0:6ba9b94b8997 26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
jinu 0:6ba9b94b8997 27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jinu 0:6ba9b94b8997 28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
jinu 0:6ba9b94b8997 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
jinu 0:6ba9b94b8997 30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
jinu 0:6ba9b94b8997 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
jinu 0:6ba9b94b8997 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jinu 0:6ba9b94b8997 33 */
jinu 0:6ba9b94b8997 34
jinu 0:6ba9b94b8997 35 #include "ble_gatts_app.h"
jinu 0:6ba9b94b8997 36 #include <string.h>
jinu 0:6ba9b94b8997 37 #include "ble_serialization.h"
jinu 0:6ba9b94b8997 38 #include "cond_field_serialization.h"
jinu 0:6ba9b94b8997 39 #include "ble_gap.h"
jinu 0:6ba9b94b8997 40 #include "ble_rpc_defines.h"
jinu 0:6ba9b94b8997 41 #include "app_util.h"
jinu 0:6ba9b94b8997 42
jinu 0:6ba9b94b8997 43
jinu 0:6ba9b94b8997 44 uint32_t ble_gatts_hvx_req_enc(uint16_t conn_handle,
jinu 0:6ba9b94b8997 45 ble_gatts_hvx_params_t const * const p_hvx_params,
jinu 0:6ba9b94b8997 46 uint8_t * const p_buf,
jinu 0:6ba9b94b8997 47 uint32_t * const p_buf_len)
jinu 0:6ba9b94b8997 48 {
jinu 0:6ba9b94b8997 49 uint32_t index = 0;
jinu 0:6ba9b94b8997 50
jinu 0:6ba9b94b8997 51 SER_ASSERT_NOT_NULL(p_buf);
jinu 0:6ba9b94b8997 52 SER_ASSERT_NOT_NULL(p_buf_len);
jinu 0:6ba9b94b8997 53
jinu 0:6ba9b94b8997 54 SER_ASSERT(p_hvx_params == NULL ||
jinu 0:6ba9b94b8997 55 !(p_hvx_params->p_len == NULL && p_hvx_params->p_data != NULL), NRF_ERROR_NULL);
jinu 0:6ba9b94b8997 56
jinu 0:6ba9b94b8997 57 SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1 + 1, *p_buf_len);
jinu 0:6ba9b94b8997 58
jinu 0:6ba9b94b8997 59 p_buf[index++] = SD_BLE_GATTS_HVX;
jinu 0:6ba9b94b8997 60 index += uint16_encode(conn_handle, &p_buf[index]);
jinu 0:6ba9b94b8997 61
jinu 0:6ba9b94b8997 62 p_buf[index++] = (p_hvx_params != NULL) ? RPC_BLE_FIELD_PRESENT : RPC_BLE_FIELD_NOT_PRESENT;
jinu 0:6ba9b94b8997 63
jinu 0:6ba9b94b8997 64 if (p_hvx_params != NULL)
jinu 0:6ba9b94b8997 65 {
jinu 0:6ba9b94b8997 66 SER_ASSERT_LENGTH_LEQ(index + 2 + 1 + 2 + 2, *p_buf_len);
jinu 0:6ba9b94b8997 67 index += uint16_encode(p_hvx_params->handle, &p_buf[index]);
jinu 0:6ba9b94b8997 68 p_buf[index++] = p_hvx_params->type;
jinu 0:6ba9b94b8997 69 index += uint16_encode(p_hvx_params->offset, &p_buf[index]);
jinu 0:6ba9b94b8997 70
jinu 0:6ba9b94b8997 71 if (p_hvx_params->p_len != NULL)
jinu 0:6ba9b94b8997 72 {
jinu 0:6ba9b94b8997 73 SER_ASSERT_LENGTH_LEQ(index + 1 + 2 + 1, *p_buf_len);
jinu 0:6ba9b94b8997 74
jinu 0:6ba9b94b8997 75 SER_ERROR_CHECK(*p_hvx_params->p_len <= BLE_GATTS_VAR_ATTR_LEN_MAX,
jinu 0:6ba9b94b8997 76 NRF_ERROR_INVALID_PARAM);
jinu 0:6ba9b94b8997 77 p_buf[index++] = RPC_BLE_FIELD_PRESENT;
jinu 0:6ba9b94b8997 78 index += uint16_encode(*(p_hvx_params->p_len), &p_buf[index]);
jinu 0:6ba9b94b8997 79
jinu 0:6ba9b94b8997 80 if (p_hvx_params->p_data != NULL)
jinu 0:6ba9b94b8997 81 {
jinu 0:6ba9b94b8997 82 SER_ASSERT_LENGTH_LEQ(index + 1 + *(p_hvx_params->p_len), *p_buf_len);
jinu 0:6ba9b94b8997 83 p_buf[index++] = RPC_BLE_FIELD_PRESENT;
jinu 0:6ba9b94b8997 84 memcpy(&(p_buf[index]), p_hvx_params->p_data, *(p_hvx_params->p_len));
jinu 0:6ba9b94b8997 85 index += *(p_hvx_params->p_len);
jinu 0:6ba9b94b8997 86 }
jinu 0:6ba9b94b8997 87 else
jinu 0:6ba9b94b8997 88 {
jinu 0:6ba9b94b8997 89 p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT;
jinu 0:6ba9b94b8997 90 }
jinu 0:6ba9b94b8997 91 }
jinu 0:6ba9b94b8997 92 else
jinu 0:6ba9b94b8997 93 {
jinu 0:6ba9b94b8997 94 p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT;
jinu 0:6ba9b94b8997 95 p_buf[index++] = RPC_BLE_FIELD_NOT_PRESENT;
jinu 0:6ba9b94b8997 96 }
jinu 0:6ba9b94b8997 97 }
jinu 0:6ba9b94b8997 98
jinu 0:6ba9b94b8997 99 *p_buf_len = index;
jinu 0:6ba9b94b8997 100
jinu 0:6ba9b94b8997 101 return NRF_SUCCESS;
jinu 0:6ba9b94b8997 102 }
jinu 0:6ba9b94b8997 103
jinu 0:6ba9b94b8997 104
jinu 0:6ba9b94b8997 105 uint32_t ble_gatts_hvx_rsp_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 106 uint32_t packet_len,
jinu 0:6ba9b94b8997 107 uint32_t * const p_result_code,
jinu 0:6ba9b94b8997 108 uint16_t * * const pp_bytes_written)
jinu 0:6ba9b94b8997 109 {
jinu 0:6ba9b94b8997 110 SER_ASSERT_NOT_NULL(p_buf);
jinu 0:6ba9b94b8997 111 SER_ASSERT_NOT_NULL(p_result_code);
jinu 0:6ba9b94b8997 112
jinu 0:6ba9b94b8997 113 uint32_t err_code;
jinu 0:6ba9b94b8997 114 uint32_t index = 0;
jinu 0:6ba9b94b8997 115 uint32_t decode_result = ser_ble_cmd_rsp_result_code_dec(p_buf,
jinu 0:6ba9b94b8997 116 &index,
jinu 0:6ba9b94b8997 117 packet_len,
jinu 0:6ba9b94b8997 118 SD_BLE_GATTS_HVX,
jinu 0:6ba9b94b8997 119 p_result_code);
jinu 0:6ba9b94b8997 120
jinu 0:6ba9b94b8997 121 if (decode_result != NRF_SUCCESS)
jinu 0:6ba9b94b8997 122 {
jinu 0:6ba9b94b8997 123 return decode_result;
jinu 0:6ba9b94b8997 124 }
jinu 0:6ba9b94b8997 125
jinu 0:6ba9b94b8997 126 if (*p_result_code != NRF_SUCCESS)
jinu 0:6ba9b94b8997 127 {
jinu 0:6ba9b94b8997 128 SER_ASSERT_LENGTH_EQ(index, packet_len);
jinu 0:6ba9b94b8997 129 return NRF_SUCCESS;
jinu 0:6ba9b94b8997 130 }
jinu 0:6ba9b94b8997 131
jinu 0:6ba9b94b8997 132 err_code = cond_field_dec(p_buf, packet_len, &index, (void * *)pp_bytes_written, uint16_t_dec);
jinu 0:6ba9b94b8997 133 SER_ASSERT(err_code == NRF_SUCCESS, err_code);
jinu 0:6ba9b94b8997 134
jinu 0:6ba9b94b8997 135 SER_ASSERT_LENGTH_EQ(index, packet_len);
jinu 0:6ba9b94b8997 136
jinu 0:6ba9b94b8997 137 return NRF_SUCCESS;
jinu 0:6ba9b94b8997 138 }
jinu 0:6ba9b94b8997 139