+ 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;
+ 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;
+
+ //if not enough data received - read them on next CAFillHeader() call
+ if (svritem->len < headerLen)
+ {
+ *data = inBuffer;
+ *dataLength = inLen;
+ OIC_LOG(DEBUG, TAG, "CoAP header received partially. Wait for rest header data");
+ return CA_STATUS_OK;
+ }
+
+ //calculate CoAP message length
+ svritem->totalLen = CAGetTotalLengthFromHeader(svritem->data);
+
+ // allocate required memory
+ unsigned char *buffer = OICRealloc(svritem->data, svritem->totalLen);
+ if (NULL == buffer)
+ {
+ OIC_LOG(ERROR, TAG, "OICRealloc - out of memory");
+ return CA_MEMORY_ALLOC_FAILED;