int main() { struct timespec timeout; OIC_LOG(DEBUG, TAG, "OCServer is starting..."); // Set callbacks for verification SetDisplayNumCB(NULL, displayNumCB); SetUserConfirmCB(NULL, confirmNumCB); // Set Verification Option for ownership transfer // Currently, BOTH display AND confirm SetVerifyOption((VerifyOptionBitmask_t)(DISPLAY_NUM | USER_CONFIRM)); // Initialize Persistent Storage for SVR database OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink}; OCRegisterPersistentStorageHandler(&ps); if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack init error"); return 0; } /* * Declare and create the example resource: LED */ createLEDResource(gResourceUri, &LED, false, 0); timeout.tv_sec = 0; timeout.tv_nsec = 100000000L; // Break from loop with Ctrl-C OIC_LOG(INFO, TAG, "Entering ocserver main loop..."); signal(SIGINT, handleSigInt); while (!gQuitFlag) { if (OCProcess() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack process error"); return 0; } nanosleep(&timeout, NULL); } OIC_LOG(INFO, TAG, "Exiting ocserver main loop..."); if (OCStop() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack process error"); } return 0; }
int main(void) { struct timespec timeout; // Initialize Persistent Storage for SVR database OCPersistentStorage ps = { client_fopen, fread, fwrite, fclose, unlink }; OCRegisterPersistentStorageHandler(&ps); /* Initialize OCStack*/ if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack init error"); return 0; } // DeviceDiscovery(); timeout.tv_sec = 0; timeout.tv_nsec = 100000000L; // Break from loop with Ctrl+C OIC_LOG(INFO, TAG, "Entering occlient main loop..."); signal(SIGINT, handleSigInt); // CLI int thr_id; pthread_t p_thread; thr_id = pthread_create(&p_thread, NULL, CLInterface, (void *)NULL); if (thr_id < 0) { OIC_LOG(ERROR, TAG, "create CLI Thread error"); return 0; } // loop while (!gQuitFlag) { if (OCProcess() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack process error"); return 0; } nanosleep(&timeout, NULL); } OIC_LOG(INFO, TAG, "Exiting occlient main loop..."); if (OCStop() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack stop error"); } return 0; }
int main() { struct timespec timeout; OIC_LOG(DEBUG, TAG, "OCServer is starting..."); // Initialize Persistent Storage for SVR database OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink}; OCRegisterPersistentStorageHandler(&ps); if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack init error"); return 0; } /** * If server supported random pin based ownership transfer, * callback of print PIN should be registered before runing server. */ SetGeneratePinCB(&GeneratePinCB); /* * Declare and create the example resource: LED */ createLEDResource(gResourceUri, &LED, false, 0); timeout.tv_sec = 0; timeout.tv_nsec = 100000000L; // Break from loop with Ctrl-C OIC_LOG(INFO, TAG, "Entering ocserver main loop..."); signal(SIGINT, handleSigInt); while (!gQuitFlag) { if (OCProcess() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack process error"); return 0; } nanosleep(&timeout, NULL); } OIC_LOG(INFO, TAG, "Exiting ocserver main loop..."); if (OCStop() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack process error"); } return 0; }
int main(int argc, char* argv[]) { struct timespec timeout; OC_LOG(DEBUG, TAG, "OCServer is starting..."); // Initialize Persistent Storage for SVR database OCPersistentStorage ps = {}; ps.open = server_fopen; ps.read = fread; ps.write = fwrite; ps.close = fclose; ps.unlink = unlink; OCRegisterPersistentStorageHandler(&ps); if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack init error"); return 0; } /* * Declare and create the example resource: LED */ createLEDResource(gResourceUri, &LED, false, 0); timeout.tv_sec = 0; timeout.tv_nsec = 100000000L; // Break from loop with Ctrl-C OC_LOG(INFO, TAG, "Entering ocserver main loop..."); signal(SIGINT, handleSigInt); while (!gQuitFlag) { if (OCProcess() != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack process error"); return 0; } nanosleep(&timeout, NULL); } OC_LOG(INFO, TAG, "Exiting ocserver main loop..."); if (OCStop() != OC_STACK_OK) { OC_LOG(ERROR, TAG, "OCStack process error"); } return 0; }
void SetPersistentHandler(OCPersistentStorage *ps, bool set) { if (set) { ps->open = fopen; ps->read = fread; ps->write = fwrite; ps->close = fclose; ps->unlink = unlink; } else { memset(ps, 0, sizeof(OCPersistentStorage)); } EXPECT_EQ(OC_STACK_OK, OCRegisterPersistentStorageHandler(ps)); }
void SetPersistentHandler(OCPersistentStorage *ps) { if (ps) { ps->open = server_fopen; ps->read = fread; ps->write = fwrite; ps->close = fclose; ps->unlink = unlink; OCRegisterPersistentStorageHandler(ps); } }
/** * 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 void SetUpTestCase() { SetPersistentHandler(&ps); OCStackResult res = OCRegisterPersistentStorageHandler(&ps); ASSERT_TRUE(res == OC_STACK_OK); }
int main(int argc, char* argv[]) { int opt; struct timespec timeout; OCPersistentStorage ps; while ((opt = getopt(argc, argv, "u:t:c:d:p:")) != -1) { switch(opt) { case 'u': UnicastDiscovery = atoi(optarg); break; case 't': TestCase = atoi(optarg); break; case 'c': ConnType = atoi(optarg); break; case 'd': DevOwner = atoi(optarg); break; case 'p': { WithTcp = atoi(optarg); if(WithTcp > 1) { PrintUsage(); return -1; } } break; default: PrintUsage(); return -1; } } if ((UnicastDiscovery != 0 && UnicastDiscovery != 1) || (TestCase < TEST_DISCOVER_REQ || TestCase >= MAX_TESTS)|| (ConnType < CT_ADAPTER_DEFAULT || ConnType >= MAX_CT)) { PrintUsage(); return -1; } if(ConnType == CT_ADAPTER_DEFAULT || ConnType == CT_IP) { discoveryReqConnType = CT_DEFAULT; } else { OIC_LOG(INFO, TAG, "Using Default Connectivity type"); PrintUsage(); } // Initialize Persistent Storage for SVR database if (DevOwner) ps = { client_fopen_devowner, fread, fwrite, fclose, unlink }; else ps = { client_fopen_nondevowner, fread, fwrite, fclose, unlink }; OCRegisterPersistentStorageHandler(&ps); /* Initialize OCStack*/ if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack init error"); return 0; } InitDiscovery(); timeout.tv_sec = 0; timeout.tv_nsec = 100000000L; // Break from loop with Ctrl+C OIC_LOG(INFO, TAG, "Entering occlient main loop..."); signal(SIGINT, handleSigInt); while (!gQuitFlag) { if (OCProcess() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack process error"); return 0; } nanosleep(&timeout, NULL); } OIC_LOG(INFO, TAG, "Exiting occlient main loop..."); if (OCStop() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack stop error"); } return 0; }
// function(s) for provisioning client using C-level provisioning API static int initProvisionClient(void) { // initialize persistent storage for SVR DB static OCPersistentStorage pstStr = { .open = fopen_prvnMng, .read = fread, .write = fwrite, .close = fclose, .unlink = unlink }; if(OC_STACK_OK != OCRegisterPersistentStorageHandler(&pstStr)) { OIC_LOG(ERROR, TAG, "OCRegisterPersistentStorageHandler error"); return -1; } // initialize OC stack and provisioning manager if(OC_STACK_OK != OCInit(NULL, 0, OC_CLIENT_SERVER)) { OIC_LOG(ERROR, TAG, "OCStack init error"); return -1; } if (access(PRVN_DB_FILE_NAME, F_OK) != -1) { printf("************************************************************\n"); printf("************Provisioning DB file already exists.************\n"); printf("************************************************************\n"); } else { printf("*************************************************************\n"); printf("************No provisioning DB file, creating new************\n"); printf("*************************************************************\n"); } if(OC_STACK_OK != OCInitPM(PRVN_DB_FILE_NAME)) { OIC_LOG(ERROR, TAG, "OC_PM init error"); return -1; } // register callback function(s) to each OxM OTMCallbackData_t otmcb = { .loadSecretCB = LoadSecretJustWorksCallback, .createSecureSessionCB = CreateSecureSessionJustWorksCallback, .createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload, .createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload }; if(OC_STACK_OK != OCSetOwnerTransferCallbackData(OIC_JUST_WORKS, &otmcb)) { OIC_LOG(ERROR, TAG, "OCSetOwnerTransferCallbackData error: OIC_JUST_WORKS"); return -1; } otmcb.loadSecretCB = InputPinCodeCallback; otmcb.createSecureSessionCB = CreateSecureSessionRandomPinCallback; otmcb.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload; otmcb.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload; if(OC_STACK_OK != OCSetOwnerTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &otmcb)) { OIC_LOG(ERROR, TAG, "OCSetOwnerTransferCallbackData error: OIC_RANDOM_DEVICE_PIN"); return -1; } SetInputPinCB(inputPinCB); return 0; }