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 ******************************************************************/
24 * This file contains common utility functions to manage the CA msg
25 * fragmentation and reassemebly.
29 #ifndef CA_FRAGMENTATION_H_
30 #define CA_FRAGMENTATION_H_
36 * The MTU supported for BLE adapter
38 #define CA_SUPPORTED_BLE_MTU_SIZE 20
41 * The maximum port value for BLE packet format
43 #define CA_SUPPORTED_BLE_MAX_PORT 127
46 * The minimum port value for BLE packet format
48 #define CA_SUPPORTED_BLE_MIN_PORT 1
51 * The multicaset packet remote port value
53 #define CA_BLE_MULTICAST_PORT 0
56 * The header size for ble fragmentation.
57 * Specific header descriptions are below.
59 #define CA_BLE_HEADER_SIZE 2
62 * The length header size for ble fragmentation.
63 * Length header is embedded in first packet of entire CoAP PDU.
65 #define CA_BLE_LENGTH_HEADER_SIZE 4
68 * Current Header version.
70 #define HEADER_VERSION 1
78 * This enum value is used to make the CA BLE packet header.
79 * 1st bit is used to check whether the packet is start packet or not.
80 * Start packet should be marked ad CA_BLE_PACKET_START(1) and any other
81 * packet is marked as CA_BLE_PACKET_NOT_START(0).
84 CA_BLE_PACKET_NOT_START = 0,
85 CA_BLE_PACKET_START = 1
89 * This enum value is used to make the CA BLE packet header.
90 * 9th bit is uesd to check the packet use secure logic(dtls) or not.
91 * Secure packet should be marking CA_BLE_PACKET_SECURE(1) and other
92 * packet is makred CA_BLE_PACKET_NON_SECURE(0).
95 CA_BLE_PACKET_NON_SECURE = 0,
96 CA_BLE_PACKET_SECURE = 1
97 } CABLEPacketSecure_t;
100 /*****************************************************************
101 * @file The CA Header format
102 * CA Header will be defined by 2 bytes of Header.
103 * First one bit : Header type that is start packet or not.
104 * 2nd to 8th bit : Own unique port value.
105 * 9th bit: Secure type using dtls(1) or not(0).
106 * 10th to 16th bit : Remote endpoint unique port value.
108 * Start packet has additional 4 bytes size length header which
109 * represent total packet size.
110 *****************************************************************/
113 * This function is used to generate the CA BLE variable related to
114 * maintain the fragmentation logic. The variable is used in BLE send routine.
116 * @param[in] dataLength Original packet size about data to send.
117 * @param[out] midPacketCount Number of mid packet except first and last
119 * @param[out] remainingLen Size of last packet before adding header.
120 * @param[out] totalLengh The total length of the data.
122 * @return ::CA_STATUS_OK on success. One of the CA_STATUS_FAILED
123 * or other error values on error.
124 * @retval ::CA_STATUS_OK Successful
125 * @retval ::CA_STATUS_FAILED Operation failed
127 CAResult_t CAGenerateVariableForFragmentation(size_t dataLength,
128 uint32_t *midPacketCount,
129 size_t *remainingLen,
130 size_t *totalLength);
133 * This function is used to generate the CA BLE header to
134 * maintain the fragmentation logic. The header structure explained
135 * above will be formed and returned to the caller.
137 * @param[in,out] header Pointer to the octet array that will
138 * contain the generated header.
139 * @param[in] type Enum value to check start packet or not.
140 * it will be embedded in bit 1 of the header
141 * @param[in] sourcePort Source(own) port of the unique(in device)
142 * value. it will be embedded in bits 2~8 of
144 * @param[in] secure Enum value to check whether secure or not.
145 * it will be embedded in bit 9 of the header.
146 * @param[in] destPort Destination(remote endpoint) port of the
147 * unique(in device) value. it will be embedded
148 * in bits 10~16 of the header.
150 * @return ::CA_STATUS_OK on success. One of the CA_STATUS_FAILED
151 * or other error values on error.
152 * @retval ::CA_STATUS_OK Successful
153 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
154 * @retval ::CA_STATUS_FAILED Operation failed
156 CAResult_t CAGenerateHeader(uint8_t *header,
157 CABLEPacketStart_t type,
158 const uint8_t sourcePort,
159 CABLEPacketSecure_t secure,
160 const uint8_t destPort);
163 * This function is used to generate the CA BLE length header to
164 * maintain the fragmentation logic. The header structure explained
165 * above will be formed and returned to the caller.
167 * @param[in,out] header Pointer to the octet array that will
168 * contain the generated length header.
169 * @param[in] headerLength Length about header array. it should be
170 * same as CA_BLE_LENGTH_HEADER_SIZE.
171 * @param[in] dataLength The total length of data size. it will
172 * be embedded in 4 bytes of the length header.
174 * @return ::CA_STATUS_OK on success. One of the CA_STATUS_FAILED
175 * or other error values on error.
176 * @retval ::CA_STATUS_OK Successful
177 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
178 * @retval ::CA_STATUS_FAILED Operation failed
180 CAResult_t CAGenerateHeaderPayloadLength(uint8_t *header,
185 * This function is used to make the CA BLE first data segment to
186 * maintain the fragmentation logic. start data segment is included
187 * 2 bytes header, 4 bytes length header and transmit data.
189 * @param[out] dataSegment Pointer to the octet array that will
190 * contain the generated data packet.
191 * @param[in] data Data to the octet array that required
192 * transmittin to remote device. it will
193 * be embedded in 7th byte to data length.
194 * @param[in] dataLength The length of data size.
195 * @param[in] dataHeader Pointer to the octet array that contain
197 * @param[in] lengthHeader Pointer to the octet array that contain
200 * @return ::CA_STATUS_OK on success. One of the CA_STATUS_FAILED
201 * or other error values on error.
202 * @retval ::CA_STATUS_OK Successful
203 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
204 * @retval ::CA_STATUS_FAILED Operation failed
206 CAResult_t CAMakeFirstDataSegment(uint8_t *dataSegment,
208 const uint32_t dataLength,
209 const uint8_t *dataHeader,
210 const uint8_t *lengthHeader);
213 * This function is used to make the CA BLE second to end data segment
214 * to maintain the fragmentation logic. start data segment is included
215 * 2 bytes header and transmit data.
217 * @param[out] dataSegment Pointer to the octet array that will
218 * contain the generated data packet.
219 * @param[in] data Data to the octet array that required
220 * transmittin to remote device. it will
221 * be embedded in 7th byte to data length.
222 * @param[in] dataLength The length of data size.
223 * @param[in] index Index to determine whether some of the
225 * @param[in] dataHeader Pointer to the octet array that contain
228 * @return ::CA_STATUS_OK on success. One of the CA_STATUS_FAILED
229 * or other error values on error.
230 * @retval ::CA_STATUS_OK Successful
231 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
232 * @retval ::CA_STATUS_FAILED Operation failed
234 CAResult_t CAMakeRemainDataSegment(uint8_t *dataSegment,
236 const uint32_t dataLength,
237 const uint32_t index,
238 const uint8_t *dataHeader);
241 * This function is used to parse the header in the receiver end. This
242 * function will provide the information of the type of the packet, source(remote) /
243 * destination port info and secure infomation.
245 * @param[in] header Pointer to the octet array data which contains
246 * the header information. Note that pointer should
247 * point to two bytes of data header which needs to
249 * @param[out] type Enum value to check start packet or not.
250 * it will be embedded in bit 1 of the header
251 * @param[out] sourcePort Source(own) port of the unique(in device)
252 * value. it will be embedded in bits 2~8 of
254 * @param[out] secure Enum value to check whether secure or not.
255 * it will be embedded in bit 9 of the header.
256 * @param[out] destPort Destination(remote endpoint) port of the
257 * unique(in device) value. it will be embedded
258 * in bits 10~16 of the header.
260 * @return ::CA_STATUS_OK on success. One of the CA_STATUS_FAILED
261 * or other error values on error.
262 * @retval ::CA_STATUS_OK Successful
263 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
264 * @retval ::CA_STATUS_FAILED Operation failed
266 CAResult_t CAParseHeader(const uint8_t *header,
267 CABLEPacketStart_t *startFlag,
268 uint16_t *sourcePort,
269 CABLEPacketSecure_t *secureFlag,
273 * This function is used to parse the length header in the receiver end. This
274 * function will provide the total data length about defragmented data.
276 * @param[in] header Pointer to the octet array that will
277 * contain length infomation.
278 * @param[out] headerLength Length about header array. it should be
279 * same as CA_BLE_LENGTH_HEADER_SIZE.
280 * @param[out] dataLength The total length of data size. it will
281 * be embedded in 4 bytes of the length header.
283 * @return ::CA_STATUS_OK on success. One of the CA_STATUS_FAILED
284 * or other error values on error.
285 * @retval ::CA_STATUS_OK Successful
286 * @retval ::CA_STATUS_INVALID_PARAM Invalid input arguments
287 * @retval ::CA_STATUS_FAILED Operation failed
289 CAResult_t CAParseHeaderPayloadLength(uint8_t *header,
291 uint32_t *dataLength);
297 #endif /* CA_FRAGMENTATION_H_ */