/** * This function deletes memory allocated to linked list created by OCDiscover_XXX_Devices API. * * @param[in] pList Pointer to OCProvisionDev_t which should be deleted. */ void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList) { PMDeleteDeviceList(pList); }
OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime, OCProvisionDev_t** pOwnedDevList, OCProvisionDev_t** pUnownedDevList) { //TODO will be replaced by more efficient logic if (pOwnedDevList == NULL || *pOwnedDevList != NULL || pUnownedDevList == NULL || *pUnownedDevList != NULL || 0 == waittime) { return OC_STACK_INVALID_PARAM; } // Code for unowned discovery OCProvisionDev_t *unownedDevice = NULL; OCStackResult res = OCDiscoverUnownedDevices(waittime/2, &unownedDevice); if (OC_STACK_OK != res) { OIC_LOG(ERROR,TAG, "Error in unowned discovery"); return res; } // Code for owned discovery OCProvisionDev_t *ownedDevice = NULL; res = OCDiscoverOwnedDevices(waittime/2, &ownedDevice); if (OC_STACK_OK != res) { OIC_LOG(ERROR,TAG, "Error in owned discovery"); PMDeleteDeviceList(unownedDevice); return res; } // Code to get list of all the owned devices. OCUuidList_t *uuidList = NULL; size_t numOfDevices = 0; res = PDMGetOwnedDevices(&uuidList, &numOfDevices); if (OC_STACK_OK != res) { OIC_LOG(ERROR, TAG, "Error while getting info from DB"); PMDeleteDeviceList(unownedDevice); PMDeleteDeviceList(ownedDevice); return res; } // Code to compare devices in owned list and deviceid from DB. OCProvisionDev_t* pCurDev = ownedDevice; size_t deleteCnt = 0; while (pCurDev) { if(true == PMDeleteFromUUIDList(uuidList, &pCurDev->doxm->deviceID)) { deleteCnt++; } pCurDev = pCurDev->next; } // If there is no remaind device in uuidList, we have to assign NULL to prevent free. if (deleteCnt == numOfDevices) { uuidList = NULL; } // Code to add information of the devices which are currently off in owned list. OCUuidList_t *powerOffDeviceList = uuidList; while (powerOffDeviceList) { OCProvisionDev_t *ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t)); if (NULL == ptr) { OIC_LOG(ERROR,TAG,"Fail to allocate memory"); PMDeleteDeviceList(unownedDevice); PMDeleteDeviceList(ownedDevice); OCDeleteUuidList(uuidList); return OC_STACK_NO_MEMORY; } ptr->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t)); if (NULL == ptr->doxm) { OIC_LOG(ERROR,TAG,"Fail to allocate memory"); PMDeleteDeviceList(unownedDevice); PMDeleteDeviceList(ownedDevice); OCDeleteUuidList(uuidList); OICFree(ptr); return OC_STACK_NO_MEMORY; } memcpy(ptr->doxm->deviceID.id, powerOffDeviceList->dev.id, sizeof(ptr->doxm->deviceID.id)); ptr->devStatus = DEV_STATUS_OFF; LL_PREPEND(ownedDevice, ptr); powerOffDeviceList = powerOffDeviceList->next; } OCDeleteUuidList(uuidList); *pOwnedDevList = ownedDevice; *pUnownedDevList = unownedDevice; return OC_STACK_OK; }
/* * Function to device revocation * This function will remove credential of target device from all devices in subnet. * * @param[in] ctx Application context would be returned in result callback * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds) * @param[in] pTargetDev Device information to be revoked. * @param[in] resultCallback callback provided by API user, callback will be called when * credential revocation is finished. * @return OC_STACK_OK in case of success and other value otherwise. */ OCStackResult OCRemoveDeviceWithUuid(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery, const OicUuid_t* pTargetUuid, OCProvisionResultCB resultCallback) { OIC_LOG(INFO, TAG, "IN OCRemoveDeviceWithUuid"); OCStackResult res = OC_STACK_ERROR; if (!pTargetUuid || 0 == waitTimeForOwnedDeviceDiscovery) { OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : Invalied parameters"); return OC_STACK_INVALID_PARAM; } if (!resultCallback) { OIC_LOG(INFO, TAG, "OCRemoveDeviceWithUuid : NULL Callback"); return OC_STACK_INVALID_CALLBACK; } OCProvisionDev_t* pOwnedDevList = NULL; //2. Find owned device from the network res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList); if (OC_STACK_OK != res) { OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to PMDeviceDiscovery"); goto error; } OCProvisionDev_t* pTargetDev = NULL; LL_FOREACH(pOwnedDevList, pTargetDev) { if(memcmp(&pTargetDev->doxm->deviceID.id, pTargetUuid->id, sizeof(pTargetUuid->id)) == 0) { break; } } char* strUuid = NULL; if(OC_STACK_OK != ConvertUuidToStr(pTargetUuid, &strUuid)) { OIC_LOG(WARNING, TAG, "Failed to covert UUID to String."); goto error; } if(pTargetDev) { OIC_LOG_V(INFO, TAG, "[%s] is dectected on the network.", strUuid); OIC_LOG_V(INFO, TAG, "Trying [%s] revocation.", strUuid); // Send DELETE requests to linked devices OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not. resReq = SRPRemoveDeviceWithoutDiscovery(ctx, pOwnedDevList, pTargetDev, resultCallback); if (OC_STACK_OK != resReq) { if (OC_STACK_CONTINUE == resReq) { OIC_LOG(DEBUG, TAG, "OCRemoveDeviceWithUuid : Revoked device has no linked device except PT."); } else { OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Failed to invoke SRPRemoveDevice"); res = resReq; OICFree(strUuid); goto error; } } res = RemoveDeviceInfoFromLocal(pTargetDev); if(OC_STACK_OK != res) { OIC_LOG(ERROR, TAG, "OCRemoveDeviceWithUuid : Filed to remove the device information from local."); OICFree(strUuid); goto error; } if(OC_STACK_CONTINUE == resReq) { /** * If there is no linked device, PM does not send any request. * So we should directly invoke the result callback to inform the result of OCRemoveDevice. */ if(resultCallback) { resultCallback(ctx, 0, NULL, false); } res = OC_STACK_OK; } } else { OIC_LOG_V(WARNING, TAG, "OCRemoveDeviceWithUuid : Failed to find the [%s] on the network.", strUuid); res = OC_STACK_ERROR; } error: OICFree(strUuid); PMDeleteDeviceList(pOwnedDevList); OIC_LOG(INFO, TAG, "OUT OCRemoveDeviceWithUuid"); return res; }