OCStackResult OCSecure::discoverUnownedDevices(unsigned short timeout, DeviceList_t &list) { OCStackResult result; OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr; auto csdkLock = OCPlatform_impl::Instance().csdkLock(); auto cLock = csdkLock.lock(); if(cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCDiscoverUnownedDevices(timeout, &pDevList); if (result == OC_STACK_OK) { // Create DeviceList of OCSecureResource's pCurDev = pDevList; while (pCurDev) { tmp = pCurDev; list.push_back(std::shared_ptr<OCSecureResource>( new OCSecureResource(csdkLock, pCurDev))); pCurDev = pCurDev->next; tmp->next = nullptr; } } else { oclog() <<"Unowned device discovery failed!"; } } else { oclog() <<"Mutex not found"; result = OC_STACK_ERROR; } return result; }
static int discoverUnownedDevices(void) { // delete unowned device list before updating it if(g_unown_list) { OCDeleteDiscoveredDevices(g_unown_list); g_unown_list = NULL; } // call |OCDiscoverUnownedDevices| API actually printf(" Discovering Only Unowned Devices on Network..\n"); if(OC_STACK_OK != OCDiscoverUnownedDevices(DISCOVERY_TIMEOUT, &g_unown_list)) { OIC_LOG(ERROR, TAG, "OCDiscoverUnownedDevices API error"); return -1; } // display the discovered unowned list printf(" > Discovered Unowned Devices\n"); g_unown_cnt = printDevList(g_unown_list); return 0; }
/** * Provisioning client sample using ProvisioningAPI */ int main() { OCStackResult res = OC_STACK_OK; int unused; (void)unused; // Initialize Persistent Storage for SVR database OCPersistentStorage ps = { .open = NULL, .read = NULL, .write = NULL, .close = NULL, .unlink = NULL }; ps.open = client_fopen; ps.read = fread; ps.write = fwrite; ps.close = fclose; ps.unlink = unlink; OCRegisterPersistentStorageHandler(&ps); if (OC_STACK_OK != OCInit(NULL, 0, OC_CLIENT_SERVER)) { OC_LOG(ERROR, TAG, "OCStack init error"); goto error; } OCProvisionDev_t* pDeviceList = NULL; res = OCDiscoverUnownedDevices(PREDEFINED_TIMEOUT, &pDeviceList); if(OC_STACK_OK != res) { OC_LOG_V(ERROR, TAG, "Failed to PMDeviceDiscovery : %d", res); goto error; } OCProvisionDev_t* pCurDev = pDeviceList; int i; while(pCurDev !=NULL) { for(i = 0; i < UUID_LENGTH; i++) printf("%c", pCurDev->doxm->deviceID.id[i]); printf("\n"); pCurDev = pCurDev->next; } //Register callback function to each OxM OTMCallbackData_t justWorksCBData = {.loadSecretCB=NULL, .createSecureSessionCB=NULL, .createSelectOxmPayloadCB=NULL, .createOwnerTransferPayloadCB=NULL}; justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback; justWorksCBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback; justWorksCBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload; justWorksCBData.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload; OTMSetOwnershipTransferCallbackData(OIC_JUST_WORKS, &justWorksCBData); OTMCallbackData_t pinBasedCBData = {.loadSecretCB=NULL, .createSecureSessionCB=NULL, .createSelectOxmPayloadCB=NULL, .createOwnerTransferPayloadCB=NULL}; pinBasedCBData.loadSecretCB = InputPinCodeCallback; pinBasedCBData.createSecureSessionCB = CreateSecureSessionRandomPinCallbak; pinBasedCBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload; pinBasedCBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload; OTMSetOwnershipTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &pinBasedCBData); SetInputPinCB(&InputPinCB); char* myContext = "OTM Context"; //Perform ownership transfer res = OCDoOwnershipTransfer((void*)myContext, pDeviceList, OwnershipTransferCB); if(OC_STACK_OK == res) { OC_LOG(INFO, TAG, "Request for ownership transfer is sent successfully."); } else { OC_LOG_V(ERROR, TAG, "Failed to OCDoOwnershipTransfer : %d", res); } gOwnershipState = 0; while ( gOwnershipState == 0 ) { if (OCProcess() != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack process error"); goto error; } sleep(1); } // Credential & ACL provisioning between two devices. OCProvisionDev_t *pOwnedList = NULL; OCProvisionDev_t *pOwnedDevices [MAX_OWNED_DEVICE] = {0,}; int nOwnedDevice = 0; res = OCDiscoverOwnedDevices(PREDEFINED_TIMEOUT, &pOwnedList); if (OC_STACK_OK == res) { printf("################## Owned Device List #######################\n"); while (pOwnedList != NULL) { nOwnedDevice ++; printf(" %d : ", nOwnedDevice); for (int i = 0; i < UUID_LENGTH; i++) { printf("%c", pOwnedList->doxm->deviceID.id[i]); } printf("\n"); pOwnedDevices[nOwnedDevice] = pOwnedList; pOwnedList = pOwnedList->next; } } else { OC_LOG(ERROR, TAG, "Error while Owned Device Discovery"); } int Device1 = 0; int Device2 = 0; printf("Select 2 devices for Credential & ACL provisioning\n"); printf("Device 1: "); unused = scanf("%d", &Device1); printf("Device 2: "); unused = scanf("%d", &Device2); gAcl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t)); if (NULL == gAcl1) { OC_LOG(ERROR, TAG, "Error while memory allocation"); goto error; } gAcl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t)); if (NULL == gAcl2) { OC_LOG(ERROR, TAG, "Error while memory allocation"); goto error; } printf("Input ACL for Device1\n"); if ( 0 == InputACL(gAcl1)) { printf("Success Input ACL\n"); } else { OC_LOG(ERROR, TAG, "InputACL error"); goto error; } printf("Input ACL for Device2\n"); if (0 == InputACL(gAcl2)) { printf("Success Input ACL\n"); } else { OC_LOG(ERROR, TAG, "InputACL error"); goto error; } char *ctx = "DUMMY"; OCProvisionPairwiseDevices(ctx,SYMMETRIC_PAIR_WISE_KEY, OWNER_PSK_LENGTH_128, pOwnedDevices[Device1], gAcl1, pOwnedDevices[Device2], gAcl2, ProvisionPairwiseCB); gOwnershipState = 0; while ( gOwnershipState == 0 ) { if (OCProcess() != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack process error"); goto error; } sleep(1); } if (OCStop() != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack process error"); goto error; } error: deleteACL(gAcl1); deleteACL(gAcl2); OCDeleteDiscoveredDevices(&pDeviceList); OCDeleteDiscoveredDevices(&pOwnedList); return 0; }
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; }