1 /******************************************************************
3 * Copyright 2014 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 ******************************************************************/
21 #include "camsgparser.h"
23 #include "caadapterutils.h"
27 #define CA_MSG_PARSER_TAG "CA_MSG_PARSER"
30 void printBinaryFormat(char *data)
32 OIC_LOG(DEBUG, CA_MSG_PARSER_TAG, "IN");
34 for (i = 0; i < 8; i++)
36 OIC_LOG_V(DEBUG, CA_MSG_PARSER_TAG, "c[%d]: %d", i, !!((*data << i) & 0x80));
39 OIC_LOG(DEBUG, CA_MSG_PARSER_TAG, "OUT");
43 CAResult_t CAGenerateHeader(char *header, uint32_t length)
45 OIC_LOG(DEBUG, CA_MSG_PARSER_TAG, "IN");
47 VERIFY_NON_NULL(header, NULL, "header is NULL");
51 memset(header, 0x0, sizeof(char) * 2);
53 // Set the Header in the first two bits of the data as "01".
57 // Set the NEXT TWO reserved bits to Zero.
62 for (i = 11; i >= 0; i--)
69 *header |= 1 << i - 8;
73 *(header + 1) |= 1 << i;
80 *header |= 0 << i - 8 ;
84 *(header + 1) |= 0 << i;
89 OIC_LOG(DEBUG, CA_MSG_PARSER_TAG, "OUT");
95 uint32_t CAParseHeader(const char *header)
97 OIC_LOG(DEBUG, CA_MSG_PARSER_TAG, "IN");
99 VERIFY_NON_NULL(header, NULL, "header is NULL");
103 for (i = 0; i < 8; i++)
106 int bit = !!((*header << i) & 0x80);
111 dataLen = pow (2, pos + 7) + dataLen;
116 for (j = 0; j < 8; j++)
119 int bit = !!((*(header + 1) << pos) & 0x80);
122 dataLen = pow (2, j) + dataLen;
130 uint32_t CAFragmentData(const char *data, char **dataSegment, uint32_t TotalLen, uint32_t offset)
132 OIC_LOG(DEBUG, CA_MSG_PARSER_TAG, "IN");
134 VERIFY_NON_NULL(*data, NULL, "Param data is NULL");
138 if (MAX_DATA_LENGTH_SUPPORTED < TotalLen)
140 OIC_LOG(ERROR, CA_MSG_PARSER_TAG, "dataLen is out of bound");
144 if ( CA_SUPPORTED_BLE_MTU_SIZE <= TotalLen)
146 length = CA_SUPPORTED_BLE_MTU_SIZE;
155 OIC_LOG_V(DEBUG, CA_MSG_PARSER_TAG, "generatingf the Header info");
157 char *header = (char *) OICMalloc(sizeof(char) * CA_HEADER_LENGTH);
158 VERIFY_NON_NULL_RET(*header, CA_MSG_PARSER_TAG, "Malloc failed", -1);
160 *dataSegment = (char *) OICMalloc(sizeof(char) * length);
161 VERIFY_NON_NULL_RET(*dataSegment, CA_MSG_PARSER_TAG, "Malloc failed", -1);
163 memset(header, 0x0, sizeof(char) * CA_HEADER_LENGTH );
164 memset(*dataSegment, 0x0, sizeof(char) * length );
166 CAResult_t result = CAGenerateHeader(header, TotalLen);
167 if (CA_STATUS_OK != result )
169 OIC_LOG(ERROR, CA_MSG_PARSER_TAG, "Generate header failed");
173 memcpy(*dataSegment, header, CA_HEADER_LENGTH);
176 memcpy(*dataSegment + CA_HEADER_LENGTH, data, length - CA_HEADER_LENGTH );
181 OIC_LOG_V(DEBUG, CA_MSG_PARSER_TAG, "Appending actual data of length [%d] and offset [%d]", length,
184 *dataSegment = (char *) OICMalloc(sizeof(char) * length);
185 memcpy(*dataSegment, data + offset, length);
192 uint32_t CADeFragmentData(const char *datasegment, char **data, uint32_t TotalLen, uint32_t offset)
194 OIC_LOG(DEBUG, CA_MSG_PARSER_TAG, "IN");
196 VERIFY_NON_NULL(*data, NULL, "Param data is NULL");
200 memcpy(*data + offset, datasegment, strlen(datasegment));
202 length = TotalLen - offset;
209 OIC_LOG(DEBUG, CA_MSG_PARSER_TAG, "OUT");