+static CAResult_t CALEAdapterGattServerStart()
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
+
+ if (caglobals.bleFlags & CA_LE_SERVER_DISABLE)
+ {
+ OIC_LOG_V(INFO, CALEADAPTER_TAG, "server flag of configure is disable [%d]",
+ caglobals.bleFlags);
+ return CA_STATUS_OK;
+ }
+
+ CAResult_t result = CAStartLEGattServer();
+
+#ifndef SINGLE_THREAD
+ /*
+ Don't start the server side sending queue thread until the
+ server itself has actually started.
+ */
+ if (CA_STATUS_OK == result)
+ {
+ oc_mutex_lock(g_bleServerSendDataMutex);
+ result = CAQueueingThreadStart(g_bleServerSendQueueHandle);
+ oc_mutex_unlock(g_bleServerSendDataMutex);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG_V(ERROR,
+ CALEADAPTER_TAG,
+ "Unable to start server queuing thread (%d)",
+ result);
+ }
+ }
+#endif
+
+ return result;
+}
+
+static CAResult_t CALEAdapterGattServerStop()
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
+
+ if (caglobals.bleFlags & CA_LE_SERVER_DISABLE)
+ {
+ OIC_LOG_V(INFO, CALEADAPTER_TAG, "server flag of configure is disable [%d]",
+ caglobals.bleFlags);
+ return CA_STATUS_OK;
+ }
+
+#ifndef SINGLE_THREAD
+
+ oc_mutex_lock(g_bleServerSendDataMutex);
+ CAResult_t res = CAQueueingThreadStop(g_bleServerSendQueueHandle);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAQueueingThreadStop has failed");
+ }
+ oc_mutex_unlock(g_bleServerSendDataMutex);
+
+ res = CAStopLEGattServer();
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "CAStopLEGattServer has failed");
+ }
+
+ return res;
+#else
+ return CAStopLEGattServer();
+#endif
+}
+
+static CAResult_t CALEAdapterGattClientStart()
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
+
+ CAResult_t result = CAStartLEGattClient();
+
+#ifndef SINGLE_THREAD
+ /*
+ Don't start the client side sending queue thread until the
+ client itself has actually started.
+ */
+ if (CA_STATUS_OK == result)
+ {
+ oc_mutex_lock(g_bleClientSendDataMutex);
+ result = CAQueueingThreadStart(g_bleClientSendQueueHandle);
+ oc_mutex_unlock(g_bleClientSendDataMutex);
+
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR,
+ CALEADAPTER_TAG,
+ "Unable to start client queuing thread");
+ }
+ }
+#endif
+
+ return result;
+}
+
+static CAResult_t CALEAdapterGattClientStop()
+{
+#ifndef SINGLE_THREAD
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
+ CAStopLEGattClient();
+
+ oc_mutex_lock(g_bleClientSendDataMutex);
+ CAResult_t result = CAQueueingThreadStop(g_bleClientSendQueueHandle);
+ oc_mutex_unlock(g_bleClientSendDataMutex);
+
+ return result;
+#else
+ CAStopLEGattClient();
+
+ return CA_STATUS_OK;
+#endif
+}
+
+#ifdef __WITH_DTLS__
+static ssize_t CALESecureSendDataCB(CAEndpoint_t *endpoint, const void *data, size_t dataLen)
+{
+ VERIFY_NON_NULL(endpoint, CALEADAPTER_TAG, "endpoint is NULL");
+ VERIFY_NON_NULL(data, CALEADAPTER_TAG, "data is NULL");
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "IN %s", __func__);
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "encrypted datalen = %d", dataLen);
+
+ CAResult_t result;
+ CADataType_t dataType = g_dataType;
+ ssize_t ret = 0;
+
+ if (ADAPTER_SERVER == g_adapterType ||
+ (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_DATA == dataType))
+ {
+ result = CALEAdapterServerSendData(endpoint, data, dataLen);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for server failed" );
+
+ if (g_errorHandler)
+ {
+ g_errorHandler(endpoint, data, dataLen, result);
+ }
+ return ret;
+ }
+ ret = (ssize_t)dataLen;
+ }
+ else if (ADAPTER_CLIENT == g_adapterType ||
+ (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_REQUEST_DATA == dataType) ||
+ (ADAPTER_BOTH_CLIENT_SERVER == g_adapterType && CA_RESPONSE_FOR_RES == dataType))
+ {
+ result = CALEAdapterClientSendData(endpoint, data, dataLen);
+ if (CA_STATUS_OK != result)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "Send unicast data for client failed" );
+
+ if (g_errorHandler)
+ {
+ g_errorHandler(endpoint, data, dataLen, result);
+ }
+ return ret;
+ }
+ ret = (ssize_t)dataLen;
+ }
+ else
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG,
+ "Can't Send Message adapterType = %d, dataType = %d", g_adapterType, dataType);
+ return ret;
+ }
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "OUT %s", __func__);
+ return ret;
+}
+
+CAResult_t CALESecureReceiveDataCB(const CASecureEndpoint_t *sep, const void *data,
+ size_t dataLen)
+{
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "%s", __func__);
+
+ VERIFY_NON_NULL(sep, CALEADAPTER_TAG, "sep is NULL");
+ VERIFY_NON_NULL(data, CALEADAPTER_TAG, "data is NULL");
+
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
+ "Secure Data Receive - decrypted datalen = %d", dataLen);
+
+ if (dataLen <= 0)
+ {
+ OIC_LOG(ERROR, CALEADAPTER_TAG, "incorrect dataLen, derecypt fail !");
+ return CA_STATUS_INVALID_PARAM;
+ }
+
+ OIC_LOG_BUFFER(DEBUG, CALEADAPTER_TAG, data, dataLen);
+
+ CAResult_t res = CA_STATUS_OK;
+ if (g_networkPacketReceivedCallback)
+ {
+ OIC_LOG_V(DEBUG, CALEADAPTER_TAG,
+ "[CALESecureReceiveDataCB] Secure flags = %d, %x",
+ sep->endpoint.flags, sep->endpoint.flags);
+ OIC_LOG(DEBUG, CALEADAPTER_TAG,
+ "[CALESecureReceiveDataCB] Received data up !");
+ res = g_networkPacketReceivedCallback(sep, data, dataLen);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Error parsing CoAP data, res = %d", res);
+ }
+ }
+ return res;
+}
+#endif
+