#include <boards.h>
#include <RBL_nRF8001.h>
+#include "pdu.h"
#include "caleinterface.h"
#include "oic_malloc.h"
#include "caadapterutils.h"
#include "cafragmentation.h"
#define TAG "LES"
+/**
+ * Maximum TCP header length.
+ */
+#define TCP_MAX_HEADER_LENGTH 6
/**
- * @var g_bleServerDataReceivedCallback
- * @brief Maintains the callback to be notified on receival of network packets from other
- * BLE devices
+ * Maintains the callback to be notified on receival of network packets from other
+ * BLE devices
*/
static CABLEDataReceivedCallback g_bleServerDataReceivedCallback = NULL;
/**
- * @def MAX_EVENT_COUNT
- * @brief Maximum number of tries to get the event on BLE Shield address.
+ * Maximum number of tries to get the event on BLE Shield address.
*/
#define MAX_EVENT_COUNT 20
static uint8_t *g_coapBuffer = NULL;
/**
- * @var g_receivedDataLen
- * @brief Actual length of data received.
+ * Actual length of data received.
*/
-static uint32_t g_receivedDataLen = 0;
+static size_t g_receivedDataLen = 0;
/**
- * @var g_packetDataLen
- * @brief Total Length of data that is being fragmented.
+ * Total Length of data that is being fragmented.
*/
-static uint32_t g_packetDataLen = 0;
+static size_t g_packetDataLen = 0;
+
+void CAGetTCPHeaderDetails(unsigned char* recvBuffer, size_t *headerlen)
+{
+ coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
+ ((unsigned char *)recvBuffer)[0] >> 4);
+ *headerlen = coap_get_tcp_header_length_for_transport(transport);
+}
void CACheckLEDataInternal()
{
if (NULL == g_coapBuffer)
{
OIC_LOG(DEBUG, TAG, "IN");
- uint8_t headerArray[CA_HEADER_LENGTH];
- while (CAIsLEDataAvailable() && g_receivedDataLen < CA_HEADER_LENGTH)
+ size_t bufSize = TCP_MAX_HEADER_LENGTH;
+ g_coapBuffer = (uint8_t *)OICCalloc(bufSize, 1);
+ if (NULL == g_coapBuffer)
{
- headerArray[g_receivedDataLen++] = CALEReadData();
+ OIC_LOG(ERROR, TAG, "malloc");
+ return;
}
- g_packetDataLen = CAParseHeader(headerArray, CA_HEADER_LENGTH);
+ g_coapBuffer[g_receivedDataLen++] = CALEReadData();
+ size_t headerLen;
+ CAGetTCPHeaderDetails(g_coapBuffer, &headerLen);
+ OIC_LOG_V(INFO, TAG, "hdr len %d", headerLen);
+ while (CAIsLEDataAvailable() && g_receivedDataLen < headerLen)
+ {
+ g_coapBuffer[g_receivedDataLen++] = CALEReadData();
+ }
+ g_packetDataLen = coap_get_total_message_length(g_coapBuffer, g_receivedDataLen);
+ OIC_LOG_V(INFO, TAG, "pkt len %d", g_packetDataLen);
if (g_packetDataLen > COAP_MAX_PDU_SIZE)
{
OIC_LOG(ERROR, TAG, "len > pdu_size");
return;
}
- g_coapBuffer = (uint8_t *)OICCalloc((size_t)g_packetDataLen, 1);
- if (NULL == g_coapBuffer)
+ bufSize = g_packetDataLen;
+ uint8_t *newBuf = (uint8_t *)OICRealloc(g_coapBuffer, bufSize);
+ if (NULL == newBuf)
{
OIC_LOG(ERROR, TAG, "malloc");
+ OICFree(g_coapBuffer);
+ g_coapBuffer = NULL;
return;
}
-
- OIC_LOG(DEBUG, TAG, "OUT");
- g_receivedDataLen = 0;
+ g_coapBuffer = newBuf;
}
- OIC_LOG(DEBUG, TAG, "IN");
while (CAIsLEDataAvailable())
{
OIC_LOG(DEBUG, TAG, "In While loop");
}
g_receivedDataLen = 0;
+ OICFree(g_coapBuffer);
g_coapBuffer = NULL;
break;
}
CALEClearSenderInfoImpl(&g_bleClientSenderInfo);
}
-static size_t CAGetMessageLengthFromData(const unsigned char *recvBuffer)
-{
- coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
- ((unsigned char *)recvBuffer)[0] >> 4);
- size_t optPaylaodLen = coap_get_length_from_header((unsigned char *)recvBuffer,
- transport);
- size_t headerLen = coap_get_tcp_header_length((unsigned char *)recvBuffer);
-
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "option/paylaod length [%d]", optPaylaodLen);
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "header length [%d]", headerLen);
- OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "total data length [%d]", headerLen + optPaylaodLen);
-
- return headerLen + optPaylaodLen;
-}
-
static CAResult_t CAInitLEClientSenderQueue()
{
OIC_LOG(DEBUG, CALEADAPTER_TAG, "IN - CAInitLEClientSenderQueue");
OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
- newSender->totalDataLen = CAGetMessageLengthFromData(bleData->data);
+ newSender->totalDataLen = coap_get_total_message_length(bleData->data, bleData->dataLen);
if(!(newSender->totalDataLen))
{
VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
#ifdef SINGLE_THREAD
- uint8_t header[CA_HEADER_LENGTH] = { 0 };
-
- CAResult_t result =
- CAGenerateHeader(header, CA_HEADER_LENGTH, dataLen);
-
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Generate header failed");
- return CA_STATUS_FAILED;
- }
-
if (!CAIsLEConnected())
{
OIC_LOG(ERROR, CALEADAPTER_TAG, "le not conn");
return CA_STATUS_FAILED;
}
- result = CAUpdateCharacteristicsToAllGattClients(header, CA_HEADER_LENGTH);
- if (CA_STATUS_OK != result)
- {
- OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
- return CA_STATUS_FAILED;
- }
-
+ CAResult_t result = CA_STATUS_OK;
const uint32_t dataLimit = dataLen / CA_SUPPORTED_BLE_MTU_SIZE;
for (uint32_t iter = 0; iter < dataLimit; iter++)
{
CAFreeEndpoint(rep);
OIC_LOG(DEBUG, CALEADAPTER_TAG, "CALEErrorHandler OUT");
-}
+}
\ No newline at end of file