+ unsigned char *inBuffer = *data;
+ size_t inLen = *dataLength;
+ OIC_LOG_V(DEBUG, TAG, "before-datalength : %u", *dataLength);
+
+ if (NULL == svritem->data && inLen > 0)
+ {
+ // allocate memory for message header (CoAP header size because it is bigger)
+ svritem->data = (unsigned char *) OICCalloc(1, COAP_MAX_HEADER_SIZE);
+ if (NULL == svritem->data)
+ {
+ OIC_LOG(ERROR, TAG, "OICCalloc - out of memory");
+ return CA_MEMORY_ALLOC_FAILED;
+ }
+
+ // copy 1 byte to parse coap header length
+ memcpy(svritem->data, inBuffer, 1);
+ svritem->len = 1;
+ svritem->bufLen = COAP_MAX_HEADER_SIZE;
+ inBuffer++;
+ inLen--;
+ }
+
+ //if not enough data received - read them on next CAFillHeader() call
+ if (0 == inLen)
+ {
+ return CA_STATUS_OK;
+ }
+
+ //if enough data received - parse header
+ svritem->protocol = COAP;
+
+ //seems CoAP data received. read full coap header.
+ coap_transport_t transport = coap_get_tcp_header_type_from_initbyte(svritem->data[0] >> 4);
+ size_t headerLen = coap_get_tcp_header_length_for_transport(transport);
+ size_t copyLen = 0;
+
+ // HEADER
+ if (svritem->len < headerLen)
+ {
+ copyLen = headerLen - svritem->len;
+ if (inLen < copyLen)
+ {
+ copyLen = inLen;
+ }
+
+ //read required bytes to have full CoAP header
+ memcpy(svritem->data + svritem->len, inBuffer, copyLen);
+ svritem->len += copyLen;
+ inBuffer += copyLen;
+ inLen -= copyLen;