- //read required bytes to have full CoAP header
- memcpy(svritem->data + svritem->len, inBuffer, copyLen);
- svritem->len += copyLen;
- inBuffer += copyLen;
- inLen -= copyLen;
- }
-
- *data = inBuffer;
- *dataLength = inLen;
-
- OIC_LOG_V(DEBUG, TAG, "after-datalength : %u", *dataLength);
- OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
- return CA_STATUS_OK;
-}
-
-/**
- * Read message header from socket item->fd
- *
- * @param[in/out] item - used socket, buffer, current received message length and protocol
- * @return - CA_STATUS_OK or appropriate error code
- */
-static CAResult_t CAReadHeader(CATCPSessionInfo_t *svritem)
-{
- CAResult_t res = CA_STATUS_OK;
-
- if (NULL == svritem)
- {
- return CA_STATUS_INVALID_PARAM;
- }
-
- if (NULL == svritem->data)
- {
- // 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;
- }
- }
-
- //read data (assume TLS header) from remote device.
- //use TLS_HEADER_SIZE - svritem->len because even header can be read partially
- res = CARecv(svritem, TLS_HEADER_SIZE - svritem->len, 0);
-
- //return if any error occurs
- if (CA_STATUS_OK != res)
- {
- return res;
- }
-
- //if not enough data received - read them on next CAReceiveMessage() call
- if (svritem->len < TLS_HEADER_SIZE)
- {
- OIC_LOG(DEBUG, TAG, "Header received partially. Wait for rest header data");
- return CA_STATUS_OK;
- }
-
- //if enough data received - parse header
-#ifdef __WITH_TLS__
- if (CAIsTlsMessage(svritem->data, svritem->len))
- {
- svritem->protocol = TLS;
-
- //[3][4] bytes in tls header are tls payload length
- unsigned int message_length = (unsigned int)((svritem->data[3] << 8) | svritem->data[4]);
- OIC_LOG_V(DEBUG, TAG, "%s: message_length = %d", __func__, message_length);
-
- svritem->totalLen = message_length + TLS_HEADER_SIZE;
- }
- else
-#endif
- {
- 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);
-
- if (svritem->len < headerLen)