1 /******************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
25 #include "caadapterutils.h"
26 #include "cafragmentation.h"
29 * Debugging tag for fragmentation module.
31 #define TAG "OIC_CA_FRAG"
33 //packet format define value
34 #define CA_BLE_START_POS 7
35 #define CA_BLE_START_LEN 1
36 #define CA_BLE_SOURCE_PORT_POS 6
37 #define CA_BLE_SOURCE_PORT_LEN 7
38 #define CA_BLE_SECURE_POS 7
39 #define CA_BLE_SECURE_LEN 1
40 #define CA_BLE_DESTINATION_PORT_POS 6
41 #define CA_BLE_DESTINATION_PORT_LEN 7
44 * This function is used to set value in specific bit position.
46 * @param[out] x Pointer to the octet variable that will contain
47 * value in specific bit position.
48 * @param[in] p Position which need to embed specific bits(0~7).
49 * @param[in] n Length to fill several bits from the position.
50 * @param[in] v Value to contain in specific bit position.
52 static void CASetBits(uint8_t *x, unsigned p, unsigned n, unsigned v)
56 OIC_LOG(ERROR, TAG, "set bits - lower err");
59 else if(~(unsigned)(~0u<<n) < v)
61 OIC_LOG(ERROR, TAG, "set bits - upper err");
64 *x = (*x & (~(~0u << (p-n+1)))) | (*x & (~0u << (p+1))) | ((v & ~(~0u << n)) << (p-n+1));
68 * This function is used to get value in specific bit position.
70 * @param[in] x Pointer to the octet variable that have infomation
72 * @param[in] p Position which need to get specific bits(0~7).
73 * @param[in] n Length to get several bits from the position.
75 * @return @c Extracted value from specific position.
77 static uint8_t CAGetBits(uint8_t x, unsigned p, unsigned n)
79 return (x >> (p + 1 - n)) & ~(~0u << n);
82 CAResult_t CAGenerateVariableForFragmentation(size_t dataLength,
83 uint32_t *midPacketCount,
87 OIC_LOG_V(DEBUG, TAG, "IN, dataLength = %d", dataLength);
89 size_t remainDataSize = 0;
91 CA_SUPPORTED_BLE_MTU_SIZE - (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE);
92 //total data size is smaller than 14 byte case.
93 if (dataLength < dataOnlyLen)
99 remainDataSize = dataLength - dataOnlyLen;
102 if (CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE <= 0)
104 OIC_LOG_V(ERROR, TAG, "BLE header size shouldn't be bigger than BLE MTU size.");
105 return CA_STATUS_FAILED;
108 *midPacketCount = (uint32_t)remainDataSize / (CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE);
109 *remainingLen = (uint32_t)remainDataSize % (CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE);
110 uint32_t remainHeaderSize = CA_BLE_HEADER_SIZE * (*midPacketCount + (*remainingLen == 0 ? 0:1));
111 *totalLength = dataLength + (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE) + remainHeaderSize;
113 OIC_LOG(DEBUG, TAG, "OUT");
118 CAResult_t CAGenerateHeader(uint8_t *header,
119 CABLEPacketStart_t type,
120 const uint8_t sourcePort,
121 CABLEPacketSecure_t secure,
122 const uint8_t destPort)
124 OIC_LOG(DEBUG, TAG, "IN");
126 VERIFY_NON_NULL(header, TAG, "header is NULL");
128 if (sourcePort > CA_SUPPORTED_BLE_MAX_PORT ||
129 sourcePort < CA_SUPPORTED_BLE_MIN_PORT ||
130 destPort > CA_SUPPORTED_BLE_MAX_PORT)
132 OIC_LOG_V(ERROR, TAG, "source port(%d) or destination port(%d) is invalid number!!",
133 sourcePort, destPort);
134 return CA_STATUS_FAILED;
137 CASetBits(&header[0], CA_BLE_START_POS, CA_BLE_START_LEN, type);
138 CASetBits(&header[0], CA_BLE_SOURCE_PORT_POS, CA_BLE_SOURCE_PORT_LEN, sourcePort);
139 CASetBits(&header[1], CA_BLE_SECURE_POS, CA_BLE_SECURE_LEN, secure);
140 CASetBits(&header[1], CA_BLE_DESTINATION_PORT_POS, CA_BLE_DESTINATION_PORT_LEN, destPort);
145 CAResult_t CAGenerateHeaderPayloadLength(uint8_t *header,
149 VERIFY_NON_NULL(header, TAG, "header is NULL");
151 if (headerLength != CA_BLE_LENGTH_HEADER_SIZE)
153 return CA_STATUS_FAILED;
156 for(size_t idx = 1; idx < CA_BLE_LENGTH_HEADER_SIZE; idx++)
158 header[CA_BLE_LENGTH_HEADER_SIZE - idx] = dataLength & 0xFF;
161 header[0] = dataLength & 0xFF;
166 CAResult_t CAMakeFirstDataSegment(uint8_t *dataSegment,
168 const uint32_t dataLength,
169 const uint8_t *dataHeader,
170 const uint8_t *lengthHeader)
172 OIC_LOG(DEBUG, TAG, "IN");
174 VERIFY_NON_NULL(dataSegment, TAG, "dataSegment is NULL");
175 VERIFY_NON_NULL(dataHeader, TAG, "dataHeader is NULL");
176 VERIFY_NON_NULL(lengthHeader, TAG, "lengthHeader is NULL");
178 memcpy(dataSegment, dataHeader, CA_BLE_HEADER_SIZE);
179 memcpy(dataSegment + CA_BLE_HEADER_SIZE, lengthHeader, CA_BLE_LENGTH_HEADER_SIZE);
180 memcpy(dataSegment + CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE, data, dataLength);
182 OIC_LOG(DEBUG, TAG, "OUT");
187 CAResult_t CAMakeRemainDataSegment(uint8_t *dataSegment,
189 const uint32_t dataLength,
190 const uint32_t index,
191 const uint8_t *dataHeader)
193 OIC_LOG(DEBUG, TAG, "IN");
195 VERIFY_NON_NULL(dataSegment, TAG, "dataSegment is NULL");
196 VERIFY_NON_NULL(dataHeader, TAG, "dataHeader is NULL");
198 const uint8_t *cur_pos = data +
199 (CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE +
200 (index * (CA_SUPPORTED_BLE_MTU_SIZE - CA_BLE_HEADER_SIZE)));
203 OIC_LOG(ERROR, TAG, "data is NULL");
204 return CA_STATUS_FAILED;
207 memcpy(dataSegment, dataHeader, CA_BLE_HEADER_SIZE);
208 memcpy(dataSegment + CA_BLE_HEADER_SIZE, cur_pos, dataLength);
210 OIC_LOG(DEBUG, TAG, "OUT");
215 CAResult_t CAParseHeader(const uint8_t *header,
216 CABLEPacketStart_t *startFlag,
217 uint16_t *sourcePort,
218 CABLEPacketSecure_t *secureFlag,
221 OIC_LOG(DEBUG, TAG, "IN");
223 VERIFY_NON_NULL(header, TAG, "header is NULL");
225 *startFlag = CAGetBits(header[0], CA_BLE_START_POS, CA_BLE_START_LEN);
226 *sourcePort = CAGetBits(header[0], CA_BLE_SOURCE_PORT_POS, CA_BLE_SOURCE_PORT_LEN);
227 *secureFlag = CAGetBits(header[1], CA_BLE_SECURE_POS, CA_BLE_SECURE_LEN);
228 *destPort = CAGetBits(header[1], CA_BLE_DESTINATION_PORT_POS, CA_BLE_DESTINATION_PORT_LEN);
230 OIC_LOG(DEBUG, TAG, "OUT");
235 CAResult_t CAParseHeaderPayloadLength(uint8_t *header,
237 uint32_t *dataLength)
239 OIC_LOG(DEBUG, TAG, "IN");
240 VERIFY_NON_NULL(header, TAG, "header is NULL");
242 if (headerLength != CA_BLE_LENGTH_HEADER_SIZE)
244 return CA_STATUS_FAILED;
247 for(size_t idx = 0; idx < headerLength; idx++)
250 *dataLength |= header[CA_BLE_HEADER_SIZE+idx];
253 OIC_LOG(DEBUG, TAG, "OUT");