CAResult_t CAEDRClientSendMulticastData(const uint8_t *data, uint32_t dataLength) { VERIFY_NON_NULL(data, TAG, "data is null"); OIC_LOG(DEBUG, TAG, "IN"); CAResult_t result = CAEDRSendMulticastMessage(data, dataLength); OIC_LOG(DEBUG, TAG, "OUT"); return result; }
CAResult_t u_linklist_add_head(u_linklist_t *linklist, void *data) { VERIFY_NON_NULL(linklist, TAG, "list is null"); VERIFY_NON_NULL(data, TAG, "data is null"); u_linklist_data_t *add_node = NULL; add_node = (u_linklist_data_t *) OICMalloc(sizeof(u_linklist_data_t)); if (NULL == add_node) { OIC_LOG(DEBUG, TAG, "LinklistAdd FAIL, memory allocation failed"); return CA_MEMORY_ALLOC_FAILED; } add_node->data = data; add_node->next = linklist->list; linklist->list = add_node; linklist->size += 1; return CA_STATUS_OK; }
static CAResult_t CAStartUnicastServer(const char *localAddress, uint16_t *port, bool isSecured, int *serverFD) { OIC_LOG(DEBUG, IP_SERVER_TAG, "IN"); VERIFY_NON_NULL(serverFD, IP_SERVER_TAG, "serverFD"); VERIFY_NON_NULL(localAddress, IP_SERVER_TAG, "localAddress"); VERIFY_NON_NULL(port, IP_SERVER_TAG, "port"); CAResult_t ret = CACreateSocket(serverFD, localAddress, port, isSecured); if (CA_STATUS_OK != ret) { OIC_LOG(ERROR, IP_SERVER_TAG, "Failed to create unicast socket"); } OIC_LOG(DEBUG, IP_SERVER_TAG, "OUT"); return ret; }
CAResult_t CAGetIPInterfaceInformation(CAEndpoint_t **info, uint32_t *size) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(info, TAG, "info is NULL"); VERIFY_NON_NULL(size, TAG, "size is NULL"); u_arraylist_t *iflist = CAIPGetInterfaceInformation(0); if (!iflist) { OIC_LOG(ERROR, TAG, "get interface info failed"); return CA_STATUS_FAILED; } uint32_t len = u_arraylist_length(iflist); CAEndpoint_t *eps = (CAEndpoint_t *)OICCalloc(len, sizeof (CAEndpoint_t)); if (!eps) { OIC_LOG(ERROR, TAG, "Malloc Failed"); u_arraylist_destroy(iflist); return CA_MEMORY_ALLOC_FAILED; } for (uint32_t i = 0, j = 0; i < len; i++) { CAInterface_t *ifitem = (CAInterface_t *)u_arraylist_get(iflist, i); OICStrcpy(eps[j].addr, CA_INTERFACE_NAME_SIZE, ifitem->name); eps[j].flags = CA_IPV4; eps[j].adapter = CA_ADAPTER_IP; eps[j].interface = 0; eps[j].port = 0; j++; } *info = eps; *size = len; u_arraylist_destroy(iflist); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAIPGetInterfaceSubnetMask(const char *ipAddress, char **subnetMask) { OIC_LOG(DEBUG, IP_MONITOR_TAG, "IN"); VERIFY_NON_NULL(subnetMask, IP_MONITOR_TAG, "subnet mask"); VERIFY_NON_NULL(ipAddress, IP_MONITOR_TAG, "ipAddress is null"); VERIFY_NON_NULL(g_networkMonitorContext, IP_MONITOR_TAG, "g_networkMonitorContext is null"); VERIFY_NON_NULL(g_networkMonitorContextMutex, IP_MONITOR_TAG, "g_networkMonitorContextMutex is null"); // Get the interface and ipaddress information from cache ca_mutex_lock(g_networkMonitorContextMutex); if (!g_networkMonitorContext->netInterfaceList || (0 == u_arraylist_length(g_networkMonitorContext->netInterfaceList))) { OIC_LOG(DEBUG, IP_MONITOR_TAG, "Network not enabled"); ca_mutex_unlock(g_networkMonitorContextMutex); return CA_ADAPTER_NOT_ENABLED; } uint32_t list_length = u_arraylist_length(g_networkMonitorContext->netInterfaceList); OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "list lenght [%d]", list_length); for (uint32_t list_index = 0; list_index < list_length; list_index++) { CANetInfo_t *info = (CANetInfo_t *) u_arraylist_get( g_networkMonitorContext->netInterfaceList, list_index); if (!info) { continue; } if (strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0) { OIC_LOG_V(DEBUG, IP_MONITOR_TAG, "CAIPGetInterfaceSubnetMask subnetmask is %s", info->subnetMask); *subnetMask = OICStrdup(info->subnetMask); break; } } ca_mutex_unlock(g_networkMonitorContextMutex); OIC_LOG(DEBUG, IP_MONITOR_TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue, uint32_t charValueLen) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(charValue, TAG, "charValue"); oc_mutex_lock(g_leCharacteristicMutex); if (!g_LEConnectedState) { OIC_LOG(ERROR, TAG, "g_LEConnectedState is false"); oc_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } if (NULL == g_gattReadCharPath) { OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL"); oc_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } int ret = bt_gatt_set_value(g_gattReadCharPath, (char *)charValue, charValueLen); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_set_value failed with return[%s]", CALEGetErrorMsg(ret)); oc_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } #ifdef BLE_TIZEN_30 ret = bt_gatt_server_notify_characteristic_changed_value(g_gattReadCharPath, CALEServerNotificationSentCB, NULL, NULL); #else ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB, NULL); #endif if (0 != ret) { OIC_LOG_V(ERROR, TAG, #ifdef BLE_TIZEN_30 "bt_gatt_server_notify_characteristic_changed_value failed with return[%s]", #else "bt_gatt_server_notify failed with return[%s]", #endif CALEGetErrorMsg(ret)); oc_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } oc_mutex_unlock(g_leCharacteristicMutex); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAUpdateCharacteristicsToGattClient(const char *address, const uint8_t *charValue, uint32_t charValueLen) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(charValue, TAG, "charValue"); VERIFY_NON_NULL(address, TAG, "address"); OIC_LOG_V(DEBUG, TAG, "Client's Unicast address for sending data [%s]", address); ca_mutex_lock(g_leCharacteristicMutex); if (NULL == g_gattReadCharPath) { OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL"); ca_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } int ret = bt_gatt_set_value(g_gattReadCharPath, (char *)charValue, charValueLen); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_set_value failed with return [%s]", CALEGetErrorMsg(ret)); ca_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB, address, NULL); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_notify failed with return [%s]", CALEGetErrorMsg(ret)); ca_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } ca_mutex_unlock(g_leCharacteristicMutex); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAManagerStartAutoConnection(JNIEnv *env, jstring remote_le_address) { VERIFY_NON_NULL(env, TAG, "env is null"); VERIFY_NON_NULL(remote_le_address, TAG, "remote_le_address is null"); OIC_LOG(DEBUG, TAG, "IN - CAManagerStartAutoConnection"); if (true == CAManagerGetAutoConnectionFlag(env, remote_le_address)) { OIC_LOG(INFO, TAG, "auto connecting."); return CA_STATUS_FAILED; } ca_mutex_lock(g_connectRetryMutex); CAResult_t res = CA_STATUS_OK; for (size_t retry_cnt = 0 ; retry_cnt < MAX_RETRY_COUNT ; retry_cnt++) { // there is retry logic 5 times when connectGatt call has failed // because BT adapter might be not ready yet. res = CAManagerConnectGatt(env, remote_le_address); if (CA_STATUS_OK != res) { OIC_LOG_V(INFO, TAG, "retry will be started at least %d times after delay 1sec", MAX_RETRY_COUNT - retry_cnt - 1); if (ca_cond_wait_for(g_connectRetryCond, g_connectRetryMutex, TIMEOUT) == 0) { OIC_LOG(INFO, TAG, "request to connect gatt was canceled"); ca_mutex_unlock(g_connectRetryMutex); return CA_STATUS_OK; } // time out. retry connection } else { OIC_LOG(INFO, TAG, "ConnectGatt has called successfully"); break; } } ca_mutex_unlock(g_connectRetryMutex); OIC_LOG(DEBUG, TAG, "OUT - CAManagerStartAutoConnection"); return res; }
CAResult_t CAInitializeIP(CARegisterConnectivityCallback registerCallback, CANetworkPacketReceivedCallback networkPacketCallback, CANetworkChangeCallback netCallback, CAErrorHandleCallback errorCallback, ca_thread_pool_t handle) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(registerCallback, TAG, "registerCallback"); VERIFY_NON_NULL(networkPacketCallback, TAG, "networkPacketCallback"); VERIFY_NON_NULL(netCallback, TAG, "netCallback"); #ifndef SINGLE_THREAD VERIFY_NON_NULL(handle, TAG, "thread pool handle"); #endif g_networkChangeCallback = netCallback; g_networkPacketCallback = networkPacketCallback; g_errorCallback = errorCallback; CAInitializeIPGlobals(); caglobals.ip.threadpool = handle; CAIPSetPacketReceiveCallback(CAIPPacketReceivedCB); #ifdef __WITH_DTLS__ CAAdapterNetDtlsInit(); CADTLSSetAdapterCallbacks(CAIPPacketReceivedCB, CAIPPacketSendCB, 0); #endif CAConnectivityHandler_t ipHandler; ipHandler.startAdapter = CAStartIP; ipHandler.startListenServer = CAStartIPListeningServer; ipHandler.stopListenServer = CAStopIPListeningServer; ipHandler.startDiscoveryServer = CAStartIPDiscoveryServer; ipHandler.sendData = CASendIPUnicastData; ipHandler.sendDataToAll = CASendIPMulticastData; ipHandler.GetnetInfo = CAGetIPInterfaceInformation; ipHandler.readData = CAReadIPData; ipHandler.stopAdapter = CAStopIP; ipHandler.terminate = CATerminateIP; registerCallback(ipHandler, CA_ADAPTER_IP); OIC_LOG(INFO, TAG, "OUT IntializeIP is Success"); return CA_STATUS_OK; }
CAResult_t CAEDRServerInitialize(ca_thread_pool_t handle) { OIC_LOG(DEBUG, TAG, "CAEDRServerInitialize"); VERIFY_NON_NULL(handle, TAG, "handle is NULL"); g_threadPoolHandle = handle; CAEDRServerJniInit(); return CAEDRServerCreateMutex(); }
CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint, CAErrorInfo_t *errorInfo) { VERIFY_NON_NULL(pdu, TAG, "pdu"); uint32_t code = 0; CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &errorInfo->info); return ret; }
/** * Callback handler for handling callback of provisioning device 1. * * @param[in] ctx ctx value passed to callback from calling function. * @param[in] UNUSED handle to an invocation * @param[in] clientResponse Response from queries to remote servers. * @return OC_STACK_DELETE_TRANSACTION to delete the transaction * and OC_STACK_KEEP_TRANSACTION to keep it. */ static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED, OCClientResponse *clientResponse) { VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION); (void)UNUSED; CredentialData_t* credData = (CredentialData_t*) ctx; OICFree(credData->credInfoFirst); const OCProvisionDev_t *deviceInfo = credData->deviceInfo2; OicSecCred_t *credInfo = credData->credInfo; const OCProvisionResultCB resultCallback = credData->resultCallback; if (clientResponse) { if (OC_STACK_RESOURCE_CREATED == clientResponse->result) { // send credentials to second device registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED,1); OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData, provisionCredentialCB2); DeleteCredList(credInfo); if (OC_STACK_OK != res) { registerResultForCredProvisioning(credData, res,2); ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults, credData->resArr, true); OICFree(credData->resArr); OICFree(credData); credData = NULL; } } else { registerResultForCredProvisioning(credData, OC_STACK_ERROR,1); ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults, credData->resArr, true); OICFree(credData->resArr); OICFree(credData); credData = NULL; } } else { OC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device"); registerResultForCredProvisioning(credData, OC_STACK_ERROR,1); ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults, credData->resArr, true); DeleteCredList(credInfo); OICFree(credData->resArr); OICFree(credData); credData = NULL; } return OC_STACK_DELETE_TRANSACTION; }
OCStackResult SendDisconnectMessage(const KeepAliveEntry_t *entry) { VERIFY_NON_NULL(entry, FATAL, OC_STACK_INVALID_PARAM); /* * Send empty message to disconnect a connection. * If CA get the empty message from RI, CA will disconnect a connection. */ CARequestInfo_t requestInfo = { .method = CA_PUT }; return CASendRequest(&entry->remoteAddr, &requestInfo); }
OCStackResult HandleKeepAliveResponse(const CAEndpoint_t *endPoint, OCStackResult responseCode, const OCRepPayload *respPayload) { VERIFY_NON_NULL(endPoint, FATAL, OC_STACK_INVALID_PARAM); OIC_LOG(DEBUG, TAG, "HandleKeepAliveResponse IN"); // Get entry from KeepAlive table. uint32_t index = 0; KeepAliveEntry_t *entry = GetEntryFromEndpoint(endPoint, &index); if (!entry) { // Receive response message about find /oic/ping request. OIC_LOG(ERROR, TAG, "There is no connection info in KeepAlive table"); if (OC_STACK_NO_RESOURCE == responseCode) { OIC_LOG(ERROR, TAG, "Server doesn't have a ping resource"); return OC_STACK_ERROR; } else if (OC_STACK_OK == responseCode) { int64_t *recvInterval = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; OCRepPayloadGetIntArray(respPayload, INTERVAL_ARRAY, &recvInterval, dimensions); size_t serverIntervalSize = calcDimTotal(dimensions); entry = AddKeepAliveEntry(endPoint, OC_CLIENT, recvInterval); if (!entry) { OIC_LOG(ERROR, TAG, "Failed to add new KeepAlive entry"); return OC_STACK_ERROR; } if (serverIntervalSize) { // update interval size with received size of server. entry->intervalSize = serverIntervalSize; } // Send first ping message return SendPingMessage(entry); } } else { // Set sentPingMsg values with false. entry->sentPingMsg = false; } OIC_LOG(DEBUG, TAG, "HandleKeepAliveResponse OUT"); return OC_STACK_OK; }
OCStackResult HandleKeepAliveRequest(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo) { VERIFY_NON_NULL(endPoint, FATAL, OC_STACK_INVALID_PARAM); VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM); OIC_LOG(DEBUG, TAG, "HandleKeepAliveRequest IN"); OCStackResult result = OC_STACK_OK; if (CA_PUT == requestInfo->method) { result = HandleKeepAlivePUTRequest(endPoint, requestInfo); } else if (CA_GET == requestInfo->method) { result = HandleKeepAliveGETRequest(endPoint, requestInfo); } OIC_LOG(DEBUG, TAG, "HandleKeepAliveRequest OUT"); return result; }
CAResult_t CAAddBLEServiceInfoToList(BLEServiceList **serviceList, BLEServiceInfo *bleServiceInfo) { OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN"); VERIFY_NON_NULL(serviceList, NULL, "Param serviceList is NULL"); VERIFY_NON_NULL(bleServiceInfo, NULL, "Param bleServiceInfo is NULL"); BLEServiceList *node = (BLEServiceList *) OICCalloc(1, sizeof(BLEServiceList)); if (NULL == node) { OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "Malloc failed!"); OICFree(bleServiceInfo->bdAddress); OICFree(bleServiceInfo); return CA_STATUS_FAILED; } node->serviceInfo = bleServiceInfo; node->next = NULL; if (*serviceList == NULL) // Empty list { *serviceList = node; } else // Add at front end { node->next = *serviceList; *serviceList = node; } CAIncrementRegisteredServiceCount(); OIC_LOG_V(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "Device [%s] added to list", bleServiceInfo->bdAddress); OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT"); return CA_STATUS_OK; }
OCStackResult SendAclReq(PEContext_t *context, OCDevAddr *devAddr, OCConnectivityType connType, uint16_t securedPort) { OCStackResult ret = OC_STACK_ERROR; const char GET_ACE_QUERY_FMT[] = "%s?%s=%s;%s=%s"; char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {}; uint32_t outLen = 0; char uri[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {}; OCCallbackData cbData = {.context=NULL}; OCDevAddr destAddr = {.adapter = OC_ADAPTER_IP}; B64Result b64Ret; VERIFY_NON_NULL(TAG, context, ERROR); VERIFY_NON_NULL(TAG, devAddr, ERROR); b64Ret = b64Encode(context->subject.id, sizeof(context->subject.id), base64Buff, sizeof(base64Buff), &outLen); VERIFY_SUCCESS(TAG, B64_OK == b64Ret, ERROR); snprintf(uri, sizeof(uri), GET_ACE_QUERY_FMT, OIC_RSRC_ACL_URI, OIC_JSON_SUBJECT_NAME, base64Buff, OIC_JSON_RESOURCES_NAME, context->resource); cbData.cb = &AmsMgrAclReqCallback; cbData.context = context; destAddr = *devAddr; //update port info destAddr.flags = (OCTransportFlags)(destAddr.flags | OC_FLAG_SECURE); destAddr.port = securedPort; OC_LOG_V(INFO, TAG, "AMS Manager Sending Unicast ACL request with URI = %s", uri); ret = OCDoResource(NULL, OC_REST_GET, uri, &destAddr, NULL, connType, OC_LOW_QOS, &cbData, NULL, 0); exit: OC_LOG_V(INFO, TAG, "%s returns %d ", __func__, ret); return ret; }
CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback, CANetworkPacketReceivedCallback reqRespCallback, CANetworkChangeCallback netCallback) { OCLog(DEBUG, CALEADAPTER_TAG, "IN"); //Input validation VERIFY_NON_NULL(registerCallback, NULL, "RegisterConnectivity callback is null"); VERIFY_NON_NULL(reqRespCallback, NULL, "PacketReceived Callback is null"); VERIFY_NON_NULL(netCallback, NULL, "NetworkChange Callback is null"); #ifdef __TIZEN__ int ret = bt_initialize(); if (0 != ret) { OCLog(ERROR, CALEADAPTER_TAG, "bt_initialize failed!"); return CA_STATUS_FAILED; } #endif //#ifdef __TIZEN__ CASetBLEReqRescallback(reqRespCallback); CALERegisterNetworkNotifications(netCallback); CAConnectivityHandler_t connHandler; connHandler.startListenServer = CAStartLEListeningServer; connHandler.startDiscoverServer = CAStartLEDiscoveryServer; connHandler.sendData = CASendLEUnicastData; connHandler.sendDataToAll = CASendLEMulticastData; connHandler.startNotifyServer = CAStartLENotifyServer; connHandler.sendNotification = CASendLENotification; connHandler.GetnetInfo = CAGetLEInterfaceInformation; connHandler.readData = CAReadLEData; connHandler.terminate = CATerminateLE; registerCallback(connHandler, CA_LE); OCLog(DEBUG, CALEADAPTER_TAG, "OUT"); return CA_STATUS_OK; }
OCStackResult HandleKeepAlivePUTRequest(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo) { VERIFY_NON_NULL(endPoint, FATAL, OC_STACK_INVALID_PARAM); VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM); // Get entry from KeepAlive table. uint32_t index = 0; KeepAliveEntry_t *entry = GetEntryFromEndpoint(endPoint, &index); if (!entry) { OIC_LOG(ERROR, TAG, "Received the first keepalive message from client"); entry = AddKeepAliveEntry(endPoint, OC_SERVER, NULL); if (!entry) { OIC_LOG(ERROR, TAG, "Failed to add new keepalive entry"); return OC_STACK_ERROR; } } OCPayload *ocPayload = NULL; OCParsePayload(&ocPayload, PAYLOAD_TYPE_REPRESENTATION, requestInfo->info.payload, requestInfo->info.payloadSize); OCRepPayload *repPayload = (OCRepPayload *)ocPayload; int64_t interval = 0; OCRepPayloadGetPropInt(repPayload, INTERVAL, &interval); entry->interval = interval; OIC_LOG_V(DEBUG, TAG, "Received interval is [%d]", entry->interval); entry->timeStamp = OICGetCurrentTime(TIME_IN_US); // Send response message. SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_CHANGED, requestInfo->info.type, requestInfo->info.numOptions, requestInfo->info.options, requestInfo->info.token, requestInfo->info.tokenLength, requestInfo->info.resourceUri); return OC_STACK_OK; }
CAResult_t u_linklist_add(u_linklist_t *linklist, void *data) { VERIFY_NON_NULL(linklist, TAG, "list is null"); VERIFY_NON_NULL(data, TAG, "data is null"); u_linklist_data_t *add_node = NULL; u_linklist_data_t *node = linklist->list; add_node = (u_linklist_data_t *) OICMalloc(sizeof(u_linklist_data_t)); if (NULL == add_node) { OIC_LOG(DEBUG, TAG, "LinklistAdd FAIL, memory allocation failed"); return CA_MEMORY_ALLOC_FAILED; } add_node->data = data; add_node->next = NULL; if (NULL == node) { linklist->list = add_node; linklist->size += 1; } else { //else loop through the list and find the last node, insert next to it while (true) { if(node->next == NULL) { node->next = add_node; linklist->size += 1; break; } node = node->next; }; } return CA_STATUS_OK; }
// TODO This functionality is replicated in all SVR's and therefore we need // to encapsulate it in a common method. However, this may not be the right // file for this method. OCStackResult AddUuidArray(const cJSON* jsonRoot, const char* arrayItem, size_t *numUuids, OicUuid_t** uuids) { size_t idxx = 0; cJSON* jsonObj = cJSON_GetObjectItem((cJSON *)jsonRoot, arrayItem); VERIFY_NON_NULL(TAG, jsonObj, ERROR); VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR); *numUuids = (size_t)cJSON_GetArraySize(jsonObj); VERIFY_SUCCESS(TAG, *numUuids > 0, ERROR); *uuids = (OicUuid_t*)OICCalloc(*numUuids, sizeof(OicUuid_t)); VERIFY_NON_NULL(TAG, *uuids, ERROR); do { unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {}; uint32_t outLen = 0; B64Result b64Ret = B64_OK; cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx); VERIFY_NON_NULL(TAG, jsonOwnr, ERROR); VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR); outLen = 0; b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff, sizeof(base64Buff), &outLen); VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof((*uuids)[idxx].id)), ERROR); memcpy((*uuids)[idxx].id, base64Buff, outLen); } while ( ++idxx < *numUuids); return OC_STACK_OK; exit: return OC_STACK_ERROR; }
CAResult_t CAVerifyOICServiceByUUID(const char* serviceUUID) { OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "IN"); VERIFY_NON_NULL(serviceUUID, NULL, "Param serviceHandle is NULL"); if (strcasecmp(serviceUUID, OIC_BLE_SERVICE_ID) != 0) { OIC_LOG(ERROR, TZ_BLE_CLIENT_UTIL_TAG, "It is not OIC service!"); return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TZ_BLE_CLIENT_UTIL_TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback, CANetworkPacketReceivedCallback networkPacketCallback, CANetworkChangeCallback netCallback, CAErrorHandleCallback errorCallback, ca_thread_pool_t handle) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(registerCallback, TAG, "registerCallback"); VERIFY_NON_NULL(networkPacketCallback, TAG, "networkPacketCallback"); VERIFY_NON_NULL(netCallback, TAG, "netCallback"); VERIFY_NON_NULL(handle, TAG, "thread pool handle"); g_networkChangeCallback = netCallback; g_networkPacketCallback = networkPacketCallback; g_errorCallback = errorCallback; CAInitializeTCPGlobals(); caglobals.tcp.threadpool = handle; CATCPSetPacketReceiveCallback(CATCPPacketReceivedCB); CATCPSetErrorHandler(CATCPErrorHandler); CAConnectivityHandler_t TCPHandler = { .startAdapter = CAStartTCP, .startListenServer = CAStartTCPListeningServer, .stopListenServer = CAStopTCPListeningServer, .startDiscoveryServer = CAStartTCPDiscoveryServer, .sendData = CASendTCPUnicastData, .sendDataToAll = CASendTCPMulticastData, .GetnetInfo = CAGetTCPInterfaceInformation, .readData = CAReadTCPData, .stopAdapter = CAStopTCP, .terminate = CATerminateTCP }; registerCallback(TCPHandler, CA_ADAPTER_TCP); OIC_LOG(INFO, TAG, "OUT IntializeTCP is Success"); return CA_STATUS_OK; }
CAResult_t CAAdapterSendData(const char *remoteAddress, const char *serviceUUID, const uint8_t *data, uint32_t dataLength, uint32_t *sentLength) { OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN - CAAdapterSendData"); if (false == g_adapterState) { OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Bluetooth adapter is disabled!"); *sentLength = 0; return CA_ADAPTER_NOT_ENABLED; } // Input validation VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "service UUID is null"); VERIFY_NON_NULL(data, EDR_ADAPTER_TAG, "Data is null"); VERIFY_NON_NULL(sentLength, EDR_ADAPTER_TAG, "Sent data length holder is null"); // Create remote endpoint CAEndpoint_t *remoteEndpoint = CACreateEndpointObject(CA_DEFAULT_FLAGS, CA_ADAPTER_RFCOMM_BTEDR, remoteAddress, 0); if (NULL == remoteEndpoint) { OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create remote endpoint !"); return CA_MEMORY_ALLOC_FAILED; } // Add message to data queue CAEDRData *edrData = CACreateEDRData(remoteEndpoint, data, dataLength); CAQueueingThreadAddData(g_sendQueueHandle, edrData, sizeof (CAEDRData)); *sentLength = dataLength; // Free remote endpoint CAFreeEndpoint(remoteEndpoint); OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT - CAAdapterSendData"); return CA_STATUS_OK; }
OCStackResult HandleKeepAliveGETRequest(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo) { VERIFY_NON_NULL(endPoint, FATAL, OC_STACK_INVALID_PARAM); VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM); OIC_LOG_V(DEBUG, TAG, "Find Ping resource [%s]", requestInfo->info.resourceUri); CAResponseResult_t result = CA_CONTENT; OCResource *resourcePtr = FindResourceByUri(requestInfo->info.resourceUri); if (!resourcePtr) { // Resource URL not specified OIC_LOG_V(DEBUG, TAG, "There is no Ping resource [%s]", requestInfo->info.resourceUri); result = CA_NOT_FOUND; } SendDirectStackResponse(endPoint, requestInfo->info.messageId, result, requestInfo->info.type, requestInfo->info.numOptions, requestInfo->info.options, requestInfo->info.token, requestInfo->info.tokenLength, requestInfo->info.resourceUri); return OC_STACK_OK; }
CAResult_t CAArduinoInitUdpSocket(uint16_t *port, int *socketID) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(port, TAG, "port"); VERIFY_NON_NULL(socketID, TAG, "socketID"); CAResult_t ret = CAArduinoGetAvailableSocket(socketID); if (ret != CA_STATUS_OK) { OIC_LOG(ERROR, TAG, "get sock fail"); return ret; } //Create a datagram socket on which to recv/send. if (!socket(*socketID, SnMR::UDP, *port, 0)) { OIC_LOG(ERROR, TAG, "sock fail"); return CA_STATUS_FAILED; } OIC_LOG_V(DEBUG, TAG, "socketId:%d", *socketID); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
OicSecCred_t * getCredList() { size_t sz = 0; OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred)); VERIFY_NON_NULL(TAG, cred, ERROR); cred->credId = 1234; // use |memcpy| for copying full-lengthed UUID without null termination memcpy(cred->subject.id, "1111111111111111", sizeof(cred->subject.id)); #if 0 cred->roleIdsLen = 2; cred->roleIds = (OicSecRole_t *)OICCalloc(cred->roleIdsLen, sizeof(OicSecRole_t)); VERIFY_NON_NULL(TAG, cred->roleIds, ERROR); OICStrcpy((char *)cred->roleIds[0].id, sizeof(cred->roleIds[0].id), "role11"); OICStrcpy((char *)cred->roleIds[1].id, sizeof(cred->roleIds[1].id), "role12"); #endif cred->credType = SYMMETRIC_PAIR_WISE_KEY; cred->privateData.data = (uint8_t *)OICCalloc(1, strlen("My private Key11") + 1); VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR); OICStrcpy((char *)cred->privateData.data, strlen("My private Key11")+1,"My private Key11"); // use |memcpy| for copying full-lengthed UUID without null termination memcpy(cred->rownerID.id, "aaaaaaaaaaaaaaaa", sizeof(cred->rownerID.id)); cred->next = (OicSecCred_t*)OICCalloc(1, sizeof(*cred->next)); VERIFY_NON_NULL(TAG, cred->next, ERROR); cred->next->credId = 5678; // use |memcpy| for copying full-lengthed UUID without null termination memcpy(cred->next->subject.id, "2222222222222222", sizeof(cred->next->subject.id)); #if 0 cred->next->roleIdsLen = 0; #endif cred->next->credType = SYMMETRIC_PAIR_WISE_KEY; sz = strlen("My private Key21") + 1; cred->next->privateData.data = (uint8_t *)OICCalloc(1, sz); VERIFY_NON_NULL(TAG, cred->next->privateData.data, ERROR); OICStrcpy((char *)cred->next->privateData.data, sz, "My private Key21"); #if 0 sz = strlen("My Public Key123") + 1; cred->next->publicData.data = (char *)OICCalloc(1, sz); VERIFY_NON_NULL(TAG, cred->next->publicData.data, ERROR); OICStrcpy(cred->next->publicData.data, sz,"My Public Key123"); #endif // use |memcpy| for copying full-lengthed UUID without null termination memcpy(cred->next->rownerID.id, "bbbbbbbbbbbbbbbb", sizeof(cred->next->rownerID.id)); return cred; exit: if(cred) { DeleteCredList(cred); cred = NULL; } return cred; }
CAResult_t CARemoveBleServiceFromGattServer(const char *svcPath) { OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "IN"); VERIFY_NON_NULL(svcPath, TZ_BLE_SERVER_TAG, "Param svcPath is NULL"); int ret = bt_gatt_remove_service(svcPath); if (0 != ret) { OIC_LOG_V(ERROR, TZ_BLE_SERVER_TAG, "bt_gatt_remove_service failed [%d]", ret); return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TZ_BLE_SERVER_TAG, "OUT"); return CA_STATUS_OK; }
uint32_t CASendLEMulticastData(void *data, uint32_t dataLen) { OCLog(DEBUG, CALEADAPTER_TAG, "IN"); //Input validation VERIFY_NON_NULL(data, NULL, "Data is null"); if (0 >= dataLen) { OCLog(ERROR, CALEADAPTER_TAG, "Invalid Parameter"); return 0; } CAResult_t result = CA_STATUS_FAILED; #ifdef __TIZEN__ pthread_mutex_lock(&gBleIsServerMutex); if (gIsServer) { result = CAUpdateCharacteristicsInGattServer(data, dataLen); if (CA_STATUS_OK != result) { OCLogv(ERROR, CALEADAPTER_TAG, "[CASendLEMulticastData] updating data in server is failed"); pthread_mutex_unlock(&gBleIsServerMutex); return 0; } } else { result = CAUpdateCharacteristicsToAllGattServers(data, dataLen); if (CA_STATUS_OK != result) { OCLogv(ERROR, CALEADAPTER_TAG, "[SendLEMulticastDataToAll] multicasting data to servers failed" ); pthread_mutex_unlock(&gBleIsServerMutex); return 0; } } pthread_mutex_unlock(&gBleIsServerMutex); #else char *tempPath = "temp_path"; updateCharacteristicsInGattServer(tempPath, (char *) data, dataLen); #endif //#ifdef __TIZEN__ OCLog(DEBUG, CALEADAPTER_TAG, "OUT"); return dataLen; }
CAResult_t CAManagerProcessRecovery(JNIEnv *env, uint16_t adapter_state) { VERIFY_NON_NULL(env, TAG, "env"); OIC_LOG(DEBUG, TAG, "IN - CAManagerProcessRecovery"); ca_mutex_lock(g_recoveryMutex); CAResult_t res = CA_STATUS_OK; switch(adapter_state) { case STATE_OFF: // adapter will be enabled automatically after WAITING_TIME. if (ca_cond_wait_for(g_recoveryCond, g_recoveryMutex, WAITING_TIME) == 0) { OIC_LOG(INFO, TAG, "BT recovery was canceled"); } else { // enabled if (!CAManagerControlAdapter(env, true)) { OIC_LOG(ERROR, TAG, "BT recovery(enable) failure"); res = CA_STATUS_FAILED; } } CAManagerSetBTRecovery(false); break; case START_RECOVERY: if (!CAManagerControlAdapter(env, false)) { OIC_LOG(ERROR, TAG, "BT recovery(disable) failure"); res = CA_STATUS_FAILED; } CAManagerSetBTRecovery(true); break; default: break; } ca_mutex_unlock(g_recoveryMutex); OIC_LOG(DEBUG, TAG, "OUT - CAManagerProcessRecovery"); return res; }