OCStackResult OCSecureResource::provisionPairwiseDevices(const Credential &cred, const OicSecAcl_t* acl1, const OCSecureResource &device2, const OicSecAcl_t* acl2, ResultCallBack resultCallback) { if(!resultCallback) { oclog() << "Result calback can't be null"; return OC_STACK_INVALID_PARAM; } OCStackResult result; auto cLock = m_csdkLock.lock(); if(cLock) { ProvisionContext* context = new ProvisionContext(resultCallback); std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCProvisionPairwiseDevices(static_cast<void*>(context), cred.getCredentialType(), cred.getCredentialKeySize(), devPtr, const_cast<OicSecAcl_t*>(acl1), device2.getDevPtr(), const_cast<OicSecAcl_t*>(acl2), &OCSecureResource::callbackWrapper); } else { oclog() <<"Mutex not found"; result = OC_STACK_ERROR; } return result; }
/** * 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; }
static int provisionPairwise(void) { // check |own_list| for provisioning pairwise devices if(!g_own_list || 2>g_own_cnt) { printf(" > Owned Device List, to Provision the Pairwise, is Empty\n"); printf(" > Please Register Unowned Devices first, with [20] Menu\n"); return 0; // normal case } // select two devices for provisioning pairwise devices int dev_num[2] = {0}; if(selectTwoDiffNum(&(dev_num[0]), &(dev_num[1]), g_own_cnt, "for Linking Devices")) { OIC_LOG(ERROR, TAG, "selectTwoDiffNum error return"); return -1; // not need to 'goto' |ERROR| before allocating |acl| } // create ACL(s) for each selected device OicSecAcl_t* acl[2] = {0}; for(int i=0; 2>i; ++i) { acl[i] = createAcl(dev_num[i]); if(!acl[i]) { OIC_LOG(ERROR, TAG, "createAcl error return"); goto PVPWS_ERROR; } } // call |OCProvisionPairwiseDevices| API actually // calling this API with callback actually acts like blocking // for error checking, the return value saved and printed g_doneCB = false; printf(" Provisioning Selected Pairwise Devices..\n"); OCStackResult rst = OCProvisionPairwiseDevices((void*) g_ctx, SYMMETRIC_PAIR_WISE_KEY, OWNER_PSK_LENGTH_128, getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[0]), acl[0], getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[1]), acl[1], provisionPairwiseCB); if(OC_STACK_OK != rst) { OIC_LOG_V(ERROR, TAG, "OCProvisionPairwiseDevices API error: %d", rst); goto PVPWS_ERROR; } if(waitCallbackRet()) // input |g_doneCB| flag implicitly { OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error"); goto PVPWS_ERROR; } OCDeleteACLList(acl[0]); OCDeleteACLList(acl[1]); // display the pairwise-provisioned result printf(" > Provisioned Selected Pairwise Devices\n"); printf(" > Please Check Device's Status for the Linked Result, with [33] Menu\n"); return 0; PVPWS_ERROR: OCDeleteACLList(acl[0]); OCDeleteACLList(acl[1]); return -1; }