+#ifdef SINGLE_THREAD
+static void CALEDataReceiverHandlerSingleThread(const uint8_t *data,
+ uint32_t dataLen)
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__);
+
+ VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
+
+ //packet parsing
+ CABLEPacketStart_t startFlag = CA_BLE_PACKET_NOT_START;
+ CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
+ uint16_t sourcePort = 0;
+ uint16_t destPort = 0;
+
+ CAParseHeader(data, &startFlag, &sourcePort, &secureFlag, &destPort);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
+ "header info: startFlag[%X] sourcePort[%d] secureFlag[%X] destPort[%d]",
+ startFlag, sourcePort, secureFlag, destPort);
+
+ if (destPort != g_localBLESourcePort && destPort != CA_BLE_MULTICAST_PORT)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "this packet is not valid for this app(port mismatch[mine:%d, packet:%d])",
+ g_localBLESourcePort, destPort);
+ return;
+ }
+
+ if (startFlag)
+ {
+ if (g_singleThreadReceiveData)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG,
+ "This packet is start packet but exist senderInfo. Remove senderInfo");
+ OICFree(g_singleThreadReceiveData->defragData);
+ OICFree(g_singleThreadReceiveData);
+ g_singleThreadReceiveData = NULL;
+ }
+
+ uint32_t totalLength = 0;
+ CAParseHeaderPayloadLength(data, CA_BLE_LENGTH_HEADER_SIZE, &totalLength);
+
+ g_singleThreadReceiveData = OICMalloc(sizeof(CABLESenderInfo_t));
+
+ if (!g_singleThreadReceiveData)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Memory allocation failed for new sender");
+ return;
+ }
+ g_singleThreadReceiveData->recvDataLen = 0;
+ g_singleThreadReceiveData->totalDataLen = 0;
+ g_singleThreadReceiveData->defragData = NULL;
+ g_singleThreadReceiveData->remoteEndpoint = NULL;
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Parsing the header");
+
+ g_singleThreadReceiveData->totalDataLen = totalLength;
+
+ if (!(g_singleThreadReceiveData->totalDataLen))
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Total Data Length is parsed as 0!!!");
+ OICFree(g_singleThreadReceiveData);
+ return;
+ }
+
+ size_t dataOnlyLen = dataLen - (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Total data to be accumulated [%u] bytes",
+ g_singleThreadReceiveData->totalDataLen);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "data received in the first packet [%u] bytes",
+ dataOnlyLen);
+
+ g_singleThreadReceiveData->defragData =
+ OICCalloc(g_singleThreadReceiveData->totalDataLen + 1,
+ sizeof(*g_singleThreadReceiveData->defragData));
+
+ if (NULL == g_singleThreadReceiveData->defragData)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "defragData is NULL!");
+ OICFree(g_singleThreadReceiveData);
+ return;
+ }
+
+ if (g_singleThreadReceiveData->recvDataLen + dataOnlyLen
+ > g_singleThreadReceiveData->totalDataLen)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "buffer is smaller than received data");
+ OICFree(g_singleThreadReceiveData->defragData);
+ OICFree(g_singleThreadReceiveData);
+ return;
+ }
+ memcpy(g_singleThreadReceiveData->defragData,
+ data + (CA_BLE_HEADER_SIZE + CA_BLE_LENGTH_HEADER_SIZE),
+ dataOnlyLen);
+ g_singleThreadReceiveData->recvDataLen += dataOnlyLen;
+ }
+ else
+ {
+ size_t dataOnlyLen = dataLen - CA_BLE_HEADER_SIZE;
+ if (g_singleThreadReceiveData->recvDataLen + dataOnlyLen
+ > g_singleThreadReceiveData->totalDataLen)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "Data Length exceeding error!! Receiving [%d] total length [%d]",
+ g_singleThreadReceiveData->recvDataLen + dataOnlyLen,
+ g_singleThreadReceiveData->totalDataLen);
+ OICFree(g_singleThreadReceiveData->defragData);
+ OICFree(g_singleThreadReceiveData);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Copying the data of length [%d]",
+ dataOnlyLen);
+ memcpy(g_singleThreadReceiveData->defragData + g_singleThreadReceiveData->recvDataLen,
+ data + CA_BLE_HEADER_SIZE,
+ dataOnlyLen);
+ g_singleThreadReceiveData->recvDataLen += dataOnlyLen;
+ OIC_LOG_V(INFO, CALEADAPTER_TAG, "totalDatalength [%d] received Datalen [%d]",
+ g_singleThreadReceiveData->totalDataLen, g_singleThreadReceiveData->recvDataLen);
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__);
+}
+
+static CAResult_t CALEServerSendDataSingleThread(const uint8_t *data,
+ uint32_t dataLen)
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__););
+
+ VERIFY_NON_NULL(data, CALEADAPTER_TAG, "Param data is NULL");
+
+ uint32_t midPacketCount = 0;
+ size_t remainingLen = 0;
+ size_t totalLength = 0;
+ CABLEPacketSecure_t secureFlag = CA_BLE_PACKET_NON_SECURE;
+
+ CAResult_t result = CAGenerateVariableForFragmentation(dataLen,
+ &midPacketCount,
+ &remainingLen,
+ &totalLength,
+ g_mtuSize);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "CAGenerateVariableForFragmentation failed, result [%d]", result);
+ return result;
+ }
+
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
+ "Packet info: data size[%d] midPacketCount[%d] remainingLen[%d] totalLength[%d]",
+ dataLen, midPacketCount, remainingLen, totalLength);
+
+ OIC_LOG_V(DEBUG,
+ CALEADAPTER_TAG,
+ "Server total Data length with header is [%u]",
+ totalLength);
+
+ uint8_t dataSegment[CA_SUPPORTED_BLE_MTU_SIZE] = {0};
+ uint8_t dataHeader[CA_BLE_HEADER_SIZE] = {0};
+
+ result = CAGenerateHeader(dataHeader,
+ CA_BLE_PACKET_START,
+ g_localBLESourcePort,
+ CA_BLE_PACKET_NON_SECURE,
+ CA_BLE_MULTICAST_PORT);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "CAGenerateHeader failed, result [%d]", result);
+ return result;
+ }
+
+ uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0};
+ result = CAGenerateHeaderPayloadLength(lengthHeader,
+ CA_BLE_LENGTH_HEADER_SIZE,
+ dataLen);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "CAGenerateHeaderPayloadLength failed, result [%d]", result);
+ return result;
+ }
+
+ uint32_t length = 0;
+ uint32_t dataOnlyLen = 0;
+ if (g_mtuSize > totalLength)
+ {
+ length = totalLength;
+ dataOnlyLen = dataLen;
+ }
+ else
+ {
+ length = g_mtuSize;
+ dataOnlyLen = g_mtuSize - CA_BLE_HEADER_SIZE - CA_BLE_LENGTH_HEADER_SIZE;
+ }
+
+ result = CAMakeFirstDataSegment(dataSegment,
+ data, dataOnlyLen,
+ dataHeader, lengthHeader);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "Making data segment failed, result [%d]", result);
+ return result;
+ }
+
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Multicast data");
+ result = CAUpdateCharacteristicsToAllGattClients(dataSegment, length);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+ result);
+ return result;
+ }
+
+ CALEDoEvents();
+
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
+
+ result = CAGenerateHeader(dataHeader,
+ CA_BLE_PACKET_NOT_START,
+ g_localBLESourcePort,
+ CA_BLE_PACKET_NON_SECURE,
+ CA_BLE_MULTICAST_PORT);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "CAGenerateHeader failed, result [%d]", result);
+ return result;
+ }
+
+ const uint32_t dataLimit = midPacketCount;
+ for (uint32_t iter = 0; iter < dataLimit; iter++)
+ {
+ result = CAMakeRemainDataSegment(dataSegment,
+ g_mtuSize - CA_BLE_HEADER_SIZE,
+ data,
+ dataLen,
+ iter,
+ dataHeader,
+ g_mtuSize);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "Making data segment failed, result [%d]", result);
+ return result;
+ }
+
+ result = CAUpdateCharacteristicsToAllGattClients(
+ dataSegment,
+ g_mtuSize);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
+ return result;
+ }
+
+ CALEDoEvents();
+ }
+
+ if (remainingLen && (totalLength > g_mtuSize))
+ {
+ // send the last segment of the data
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
+
+ result = CAMakeRemainDataSegment(dataSegment,
+ remainingLen,
+ data,
+ dataLen,
+ dataLimit,
+ dataHeader,
+ g_mtuSize);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "Making data segment failed, result [%d]", result);
+ return result;
+ }
+
+ result = CAUpdateCharacteristicsToAllGattClients(
+ dataSegment,
+ remainingLen + CA_BLE_HEADER_SIZE);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Update characteristics failed");
+ return result;
+ }
+ CALEDoEvents();
+ }
+
+ return result;
+}
+#endif
+