CAResult_t CASendMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length) { u_arraylist_t *list = CAGetSelectedNetworkList(); if (!list) { OIC_LOG(DEBUG, TAG, "No selected network"); return CA_SEND_FAILED; } CATransportFlags_t requestedAdapter = endpoint->adapter ? endpoint->adapter : CA_ALL_ADAPTERS; size_t selectedLength = u_arraylist_length(list); for (size_t i = 0; i < selectedLength; i++) { void* ptrType = u_arraylist_get(list, i); if(ptrType == NULL) { continue; } CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType; if ((connType & requestedAdapter) == 0) { continue; } int index = CAGetAdapterIndex(connType); if (0 > index) { OIC_LOG(DEBUG, TAG, "unknown connectivity type!"); continue; } uint32_t sentDataLen = 0; if (g_adapterHandler[index].sendDataToAll != NULL) { void *payload = (void *) OICMalloc(length); if (!payload) { OIC_LOG(ERROR, TAG, "Out of memory!"); return CA_MEMORY_ALLOC_FAILED; } memcpy(payload, data, length); sentDataLen = g_adapterHandler[index].sendDataToAll(endpoint, payload, length); OICFree(payload); } if (sentDataLen != length) { OIC_LOG(ERROR, TAG, "sendDataToAll failed! Error will be reported from adapter"); #ifdef SINGLE_THREAD //in case of single thread, no error handler. Report error immediately return CA_SEND_FAILED; #endif } } return CA_STATUS_OK; }
CAResult_t CASendUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t length) { if (endpoint == NULL) { OIC_LOG(DEBUG, TAG, "Invalid endpoint"); return CA_STATUS_INVALID_PARAM; } CATransportAdapter_t type = endpoint->adapter; int index = CAGetAdapterIndex(type); if (0 > index) { OIC_LOG(ERROR, TAG, "unknown transport type!"); return CA_STATUS_INVALID_PARAM; } int32_t sentDataLen = 0; if (g_adapterHandler[index].sendData != NULL) { sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length); } if (sentDataLen != (int)length) { OIC_LOG(ERROR, TAG, "error in sending data. Error will be reported in adapter"); #ifdef SINGLE_THREAD //in case of single thread, no error handler. Report error immediately return CA_SEND_FAILED; #endif } return CA_STATUS_OK; }
static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportAdapter_t cType) { if(handler.startAdapter == NULL || handler.startListenServer == NULL || handler.stopListenServer == NULL || handler.startDiscoveryServer == NULL || handler.sendData == NULL || handler.sendDataToAll == NULL || handler.GetnetInfo == NULL || handler.readData == NULL || handler.stopAdapter == NULL || handler.terminate == NULL) { OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!"); return; } int index = CAGetAdapterIndex(cType); if (0 > index) { OIC_LOG(ERROR, TAG, "unknown connectivity type!"); return; } g_adapterHandler[index] = handler; OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType); }
CAResult_t CAStartDiscoveryServerAdapters() { OIC_LOG(DEBUG, TAG, "IN"); CAResult_t result = CA_STATUS_FAILED; u_arraylist_t *list = CAGetSelectedNetworkList(); if (!list) { OIC_LOG(ERROR, TAG, "No selected network"); return result; } size_t length = u_arraylist_length(list); for (size_t i = 0; i < length; i++) { void* ptrType = u_arraylist_get(list, i); if(ptrType == NULL) { continue; } CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType; int index = CAGetAdapterIndex(connType); if (0 > index) { OIC_LOG(DEBUG, TAG, "unknown connectivity type!"); continue; } if (g_adapterHandler[index].startDiscoveryServer != NULL) { const CAResult_t tmp = g_adapterHandler[index].startDiscoveryServer(); // Successful discovery if at least one adapter started. if (CA_STATUS_OK == tmp) { result = tmp; } } } OIC_LOG(DEBUG, TAG, "OUT"); return result; }
void CAStopAdapter(CATransportAdapter_t transportType) { OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType); int index = CAGetAdapterIndex(transportType); if (0 > index) { OIC_LOG(ERROR, TAG, "unknown transport type!"); return; } if (g_adapterHandler[index].stopAdapter != NULL) { g_adapterHandler[index].stopAdapter(); } }
CAResult_t CAStartAdapter(CATransportAdapter_t transportType) { OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType); int index = CAGetAdapterIndex(transportType); if (index == -1) { OIC_LOG(ERROR, TAG, "unknown connectivity type!"); return CA_STATUS_FAILED; } if (g_adapterHandler[index].startAdapter != NULL) { g_adapterHandler[index].startAdapter(); } return CA_STATUS_OK; }
CAResult_t CAStartDiscoveryServerAdapters() { OIC_LOG(DEBUG, TAG, "IN"); u_arraylist_t *list = CAGetSelectedNetworkList(); if (!list) { OIC_LOG(ERROR, TAG, "No selected network"); return CA_STATUS_FAILED; } for (uint32_t i = 0; i < u_arraylist_length(list); i++) { void* ptrType = u_arraylist_get(list, i); if(ptrType == NULL) { continue; } CATransportAdapter_t connType = *(CATransportAdapter_t *)ptrType; int index = CAGetAdapterIndex(connType); if (index == -1) { OIC_LOG(DEBUG, TAG, "unknown connectivity type!"); continue; } if (g_adapterHandler[index].startDiscoveryServer != NULL) { g_adapterHandler[index].startDiscoveryServer(); } } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAReadData() { OIC_LOG(DEBUG, TAG, "IN"); u_arraylist_t *list = CAGetSelectedNetworkList(); if (!list) { return CA_STATUS_FAILED; } uint8_t i = 0; for (i = 0; i < u_arraylist_length(list); i++) { void *ptrType = u_arraylist_get(list, i); if (NULL == ptrType) { OIC_LOG(ERROR, TAG, "get list fail"); return CA_STATUS_FAILED; } CATransportAdapter_t connType = *(CATransportAdapter_t *) ptrType; int index = CAGetAdapterIndex(connType); if (-1 == index) { OIC_LOG(DEBUG, TAG, "unknown connectivity type!"); continue; } if (g_adapterHandler[index].readData != NULL) { g_adapterHandler[index].readData(); } } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }