+ memcpy(dataSegment, header, CA_HEADER_LENGTH);
+ OICFree(header);
+
+ uint32_t length = 0;
+ if (CA_SUPPORTED_BLE_MTU_SIZE > totalLength)
+ {
+ length = totalLength;
+ memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data, bleData->dataLen);
+ }
+ else
+ {
+ length = CA_SUPPORTED_BLE_MTU_SIZE;
+ memcpy(dataSegment + CA_HEADER_LENGTH, bleData->data,
+ CA_SUPPORTED_BLE_MTU_SIZE - CA_HEADER_LENGTH);
+ }
+
+ uint32_t iter = totalLength / CA_SUPPORTED_BLE_MTU_SIZE;
+ uint32_t index = 0;
+
+ // Send the first segment with the header.
+ if (NULL != bleData->remoteEndpoint) // Sending Unicast Data
+ {
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Server Sending Unicast Data");
+
+ result = CAUpdateCharacteristicsToGattClient(
+ bleData->remoteEndpoint->addr, dataSegment, length);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR,
+ CALEADAPTER_TAG,
+ "Update characteristics failed, result [%d]",
+ result);
+
+ g_errorHandler(bleData->remoteEndpoint,
+ bleData->data,
+ bleData->dataLen,
+ result);
+ OICFree(dataSegment);
+ return;
+ }
+
+ OIC_LOG_V(DEBUG,
+ CALEADAPTER_TAG,
+ "Server Sent data length [%u]",
+ length);
+ for (index = 1; index < iter; index++)
+ {
+ // Send the remaining header.
+ OIC_LOG_V(DEBUG,
+ CALEADAPTER_TAG,
+ "Sending the chunk number [%u]",
+ index);
+
+ result =
+ CAUpdateCharacteristicsToGattClient(
+ bleData->remoteEndpoint->addr,
+ bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
+ CA_SUPPORTED_BLE_MTU_SIZE);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "Update characteristics failed, result [%d]", result);
+ g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]",
+ CA_SUPPORTED_BLE_MTU_SIZE);
+ }
+
+ const uint32_t remainingLen =
+ totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
+
+ if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
+ {
+ // send the last segment of the data (Ex: 22 bytes of 622
+ // bytes of data when MTU is 200)
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
+ result = CAUpdateCharacteristicsToGattClient(
+ bleData->remoteEndpoint->addr,
+ bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
+ remainingLen);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR,
+ CALEADAPTER_TAG,
+ "Update characteristics failed, result [%d]",
+ result);
+ g_errorHandler(bleData->remoteEndpoint,
+ bleData->data,
+ bleData->dataLen,
+ result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
+ }
+ }
+ else
+ {
+ 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);
+ CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", length);
+ for (index = 1; index < iter; index++)
+ {
+ // Send the remaining header.
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Sending the chunk number [%d]", index);
+ result = CAUpdateCharacteristicsToAllGattClients(
+ bleData->data + ((index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH),
+ CA_SUPPORTED_BLE_MTU_SIZE);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+ result);
+ CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%u]", CA_SUPPORTED_BLE_MTU_SIZE);
+ }
+
+ const uint32_t remainingLen = totalLength % CA_SUPPORTED_BLE_MTU_SIZE;
+ if (remainingLen && (totalLength > CA_SUPPORTED_BLE_MTU_SIZE))
+ {
+ // send the last segment of the data (Ex: 22 bytes of 622 bytes of data when MTU is 200)
+ OIC_LOG(DEBUG, CALEADAPTER_TAG, "Sending the last chunk");
+ result = CAUpdateCharacteristicsToAllGattClients(
+ bleData->data + (index * CA_SUPPORTED_BLE_MTU_SIZE) - CA_HEADER_LENGTH,
+ remainingLen);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]",
+ result);
+ CALEErrorHandler(NULL, bleData->data, bleData->dataLen, result);
+ OICFree(dataSegment);
+ return;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", remainingLen);
+ }
+ }
+ OICFree(dataSegment);
+