1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 *****************************************************************/
27 #include "platform_features.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
31 #include "ocprovisioningmanager.h"
32 #include "oxmjustworks.h"
33 #include "oxmrandompin.h"
34 #include "securevirtualresourcetypes.h"
35 #include "srmutility.h"
43 // declaration(s) for provisioning client using C-level provisioning API
44 // user input definition for main loop on provisioning client
45 #define _10_DISCOV_ALL_DEVS_ 10
46 #define _11_DISCOV_UNOWN_DEVS_ 11
47 #define _12_DISCOV_OWN_DEVS_ 12
48 #define _20_REGIST_DEVS_ 20
49 #define _30_PROVIS_PAIR_DEVS_ 30
50 #define _31_PROVIS_CRED_ 31
51 #define _32_PROVIS_ACL_ 32
52 #define _33_PROVIS_DP_ 33
53 #define _34_CHECK_LINK_STATUS_ 34
54 #define _40_UNLINK_PAIR_DEVS_ 40
55 #define _50_REMOVE_SELEC_DEV_ 50
56 #define _99_EXIT_PRVN_CLT_ 99
58 #define ACL_RESRC_MAX_NUM 16
59 #define ACL_RESRC_MAX_LEN 128
60 #define ACL_PEMISN_CNT 5
61 #define DISCOVERY_TIMEOUT 10 // 10 sec
62 #define CALLBACK_TIMEOUT 60 // 1 min
63 #define TAG "provisioningclient"
65 static const char* ACL_PEMISN[5] = {"CREATE", "READ", "WRITE", "DELETE", "NOTIFY"};
66 static const char* SVR_DB_FILE_NAME = "oic_svr_db_client.dat";
67 // '_' for separaing from the same constant variable in |srmresourcestrings.c|
68 static const char* PRVN_DB_FILE_NAME = "oic_prvn_mng.db";
69 static const OicSecPrm_t SUPPORTED_PRMS[1] =
74 // |g_ctx| means provision manager application context and
75 // the following, includes |un/own_list|, could be variables, which |g_ctx| has,
76 // for accessing all function(s) for these, they are declared on global domain
77 static const char* g_ctx = "Provision Manager Client Application Context";
78 static char* g_svr_fname;
79 static char* g_prvn_fname;
80 static OCProvisionDev_t* g_own_list;
81 static OCProvisionDev_t* g_unown_list;
83 static int g_unown_cnt;
86 // function declaration(s) for calling them before implementing
87 static OicSecAcl_t* createAcl(const int);
88 static OicSecPdAcl_t* createPdAcl(const int);
89 static OCProvisionDev_t* getDevInst(const OCProvisionDev_t*, const int);
90 static int printDevList(const OCProvisionDev_t*);
91 static size_t printUuidList(const OCUuidList_t*);
92 static int printResultList(const OCProvisionResult_t*, const int);
93 static void printUuid(const OicUuid_t*);
94 static FILE* fopen_prvnMng(const char*, const char*);
95 static int waitCallbackRet(void);
96 static int selectTwoDiffNum(int*, int*, const int, const char*);
98 // callback function(s) for provisioning client using C-level provisioning API
99 static void ownershipTransferCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
103 OIC_LOG_V(INFO, TAG, "Ownership Transfer SUCCEEDED - ctx: %s", (char*) ctx);
107 OIC_LOG_V(ERROR, TAG, "Ownership Transfer FAILED - ctx: %s", (char*) ctx);
108 printResultList((const OCProvisionResult_t*) arr, nOfRes);
113 static void provisionPairwiseCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
117 OIC_LOG_V(INFO, TAG, "Provision Pairwise SUCCEEDED - ctx: %s", (char*) ctx);
121 OIC_LOG_V(ERROR, TAG, "Provision Pairwise FAILED - ctx: %s", (char*) ctx);
122 printResultList((const OCProvisionResult_t*) arr, nOfRes);
127 static void provisionCredCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
131 OIC_LOG_V(INFO, TAG, "Provision Credential SUCCEEDED - ctx: %s", (char*) ctx);
135 OIC_LOG_V(ERROR, TAG, "Provision Credential FAILED - ctx: %s", (char*) ctx);
136 printResultList((const OCProvisionResult_t*) arr, nOfRes);
141 static void provisionAclCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
145 OIC_LOG_V(INFO, TAG, "Provision ACL SUCCEEDED - ctx: %s", (char*) ctx);
149 OIC_LOG_V(ERROR, TAG, "Provision ACL FAILED - ctx: %s", (char*) ctx);
150 printResultList((const OCProvisionResult_t*) arr, nOfRes);
155 static void provisionDPCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
159 OIC_LOG_V(INFO, TAG, "Provision Direct-Pairing SUCCEEDED - ctx: %s", (char*) ctx);
163 OIC_LOG_V(ERROR, TAG, "Provision Direct-Pairing FAILED - ctx: %s", (char*) ctx);
164 printResultList((const OCProvisionResult_t*) arr, nOfRes);
169 static void unlinkDevicesCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
173 OIC_LOG_V(INFO, TAG, "Unlink Devices SUCCEEDED - ctx: %s", (char*) ctx);
177 OIC_LOG_V(ERROR, TAG, "Unlink Devices FAILED - ctx: %s", (char*) ctx);
178 printResultList((const OCProvisionResult_t*) arr, nOfRes);
183 static void removeDeviceCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
187 OIC_LOG_V(INFO, TAG, "Remove Device SUCCEEDED - ctx: %s", (char*) ctx);
191 OIC_LOG_V(ERROR, TAG, "Remove Device FAILED - ctx: %s", (char*) ctx);
192 printResultList((const OCProvisionResult_t*) arr, nOfRes);
197 static void inputPinCB(char* pin, size_t len)
199 if(!pin || OXM_RANDOM_PIN_SIZE>=len)
201 OIC_LOG(ERROR, TAG, "inputPinCB invalid parameters");
205 printf(" > INPUT PIN: ");
206 for(int ret=0; 1!=ret; )
208 ret = scanf("%8s", pin);
209 for( ; 0x20<=getchar(); ); // for removing overflow garbages
210 // '0x20<=code' is character region
214 // function(s) for provisioning client using C-level provisioning API
215 static int initProvisionClient(void)
217 // initialize persistent storage for SVR DB
218 static OCPersistentStorage pstStr =
220 .open = fopen_prvnMng,
226 if(OC_STACK_OK != OCRegisterPersistentStorageHandler(&pstStr))
228 OIC_LOG(ERROR, TAG, "OCRegisterPersistentStorageHandler error");
232 // initialize OC stack and provisioning manager
233 if(OC_STACK_OK != OCInit(NULL, 0, OC_CLIENT_SERVER))
235 OIC_LOG(ERROR, TAG, "OCStack init error");
239 if (access(PRVN_DB_FILE_NAME, F_OK) != -1)
241 printf("************************************************************\n");
242 printf("************Provisioning DB file already exists.************\n");
243 printf("************************************************************\n");
247 printf("*************************************************************\n");
248 printf("************No provisioning DB file, creating new************\n");
249 printf("*************************************************************\n");
252 if(OC_STACK_OK != OCInitPM(PRVN_DB_FILE_NAME))
254 OIC_LOG(ERROR, TAG, "OC_PM init error");
258 // register callback function(s) to each OxM
259 OTMCallbackData_t otmcb =
261 .loadSecretCB = LoadSecretJustWorksCallback,
262 .createSecureSessionCB = CreateSecureSessionJustWorksCallback,
263 .createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload,
264 .createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload
266 if(OC_STACK_OK != OCSetOwnerTransferCallbackData(OIC_JUST_WORKS, &otmcb))
268 OIC_LOG(ERROR, TAG, "OCSetOwnerTransferCallbackData error: OIC_JUST_WORKS");
271 otmcb.loadSecretCB = InputPinCodeCallback;
272 otmcb.createSecureSessionCB = CreateSecureSessionRandomPinCallback;
273 otmcb.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
274 otmcb.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
275 if(OC_STACK_OK != OCSetOwnerTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &otmcb))
277 OIC_LOG(ERROR, TAG, "OCSetOwnerTransferCallbackData error: OIC_RANDOM_DEVICE_PIN");
280 SetInputPinCB(inputPinCB);
285 static int discoverAllDevices(void)
287 // delete un/owned device lists before updating them
290 OCDeleteDiscoveredDevices(g_own_list);
295 OCDeleteDiscoveredDevices(g_unown_list);
299 // call |OCGetDevInfoFromNetwork| API actually
300 printf(" Discovering All Un/Owned Devices on Network..\n");
301 if(OC_STACK_OK != OCGetDevInfoFromNetwork(DISCOVERY_TIMEOUT, &g_own_list, &g_unown_list))
303 OIC_LOG(ERROR, TAG, "OCGetDevInfoFromNetwork API error");
307 // display the discovered un/owned lists
308 printf(" > Discovered Owned Devices\n");
309 g_own_cnt = printDevList(g_own_list);
310 printf(" > Discovered Unowned Devices\n");
311 g_unown_cnt = printDevList(g_unown_list);
317 static int discoverUnownedDevices(void)
319 // delete unowned device list before updating it
322 OCDeleteDiscoveredDevices(g_unown_list);
326 // call |OCDiscoverUnownedDevices| API actually
327 printf(" Discovering Only Unowned Devices on Network..\n");
328 if(OC_STACK_OK != OCDiscoverUnownedDevices(DISCOVERY_TIMEOUT, &g_unown_list))
330 OIC_LOG(ERROR, TAG, "OCDiscoverUnownedDevices API error");
334 // display the discovered unowned list
335 printf(" > Discovered Unowned Devices\n");
336 g_unown_cnt = printDevList(g_unown_list);
341 static int discoverOwnedDevices(void)
343 // delete owned device list before updating it
346 OCDeleteDiscoveredDevices(g_own_list);
350 // call |OCDiscoverOwnedDevices| API actually
351 printf(" Discovering Only Owned Devices on Network..\n");
352 if(OC_STACK_OK != OCDiscoverOwnedDevices(DISCOVERY_TIMEOUT, &g_own_list))
354 OIC_LOG(ERROR, TAG, "OCDiscoverOwnedDevices API error");
358 // display the discovered owned list
359 printf(" > Discovered Owned Devices\n");
360 g_own_cnt = printDevList(g_own_list);
365 static int registerDevices(void)
367 // check |unown_list| for registering devices
368 if(!g_unown_list || 0>=g_unown_cnt)
370 printf(" > Unowned Device List, to Register Devices, is Empty\n");
371 printf(" > Please Discover Unowned Devices first, with [10|11] Menu\n");
372 return 0; // normal case
375 // call |OCDoOwnershipTransfer| API actually
376 // calling this API with callback actually acts like blocking
377 // for error checking, the return value saved and printed
379 printf(" Registering All Discovered Unowned Devices..\n");
380 OCStackResult rst = OCDoOwnershipTransfer((void*) g_ctx, g_unown_list, ownershipTransferCB);
381 if(OC_STACK_OK != rst)
383 OIC_LOG_V(ERROR, TAG, "OCDoOwnershipTransfer API error: %d", rst);
386 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
388 OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
392 // display the registered result
393 printf(" > Registered Discovered Unowned Devices\n");
394 printf(" > Please Discover Owned Devices for the Registered Result, with [10|12] Menu\n");
399 static int provisionPairwise(void)
401 // check |own_list| for provisioning pairwise devices
402 if(!g_own_list || 2>g_own_cnt)
404 printf(" > Owned Device List, to Provision the Pairwise, is Empty\n");
405 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
406 return 0; // normal case
409 // select two devices for provisioning pairwise devices
410 int dev_num[2] = {0};
411 if(selectTwoDiffNum(&(dev_num[0]), &(dev_num[1]), g_own_cnt, "for Linking Devices"))
413 OIC_LOG(ERROR, TAG, "selectTwoDiffNum error return");
414 return -1; // not need to 'goto' |ERROR| before allocating |acl|
417 // create ACL(s) for each selected device
418 OicSecAcl_t* acl[2] = {0};
419 for(int i=0; 2>i; ++i)
421 acl[i] = createAcl(dev_num[i]);
424 OIC_LOG(ERROR, TAG, "createAcl error return");
429 // call |OCProvisionPairwiseDevices| API actually
430 // calling this API with callback actually acts like blocking
431 // for error checking, the return value saved and printed
433 printf(" Provisioning Selected Pairwise Devices..\n");
435 OCProvisionPairwiseDevices((void*) g_ctx,
436 SYMMETRIC_PAIR_WISE_KEY, OWNER_PSK_LENGTH_128,
437 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[0]), acl[0],
438 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[1]), acl[1],
439 provisionPairwiseCB);
440 if(OC_STACK_OK != rst)
442 OIC_LOG_V(ERROR, TAG, "OCProvisionPairwiseDevices API error: %d", rst);
445 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
447 OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
450 OCDeleteACLList(acl[0]);
451 OCDeleteACLList(acl[1]);
453 // display the pairwise-provisioned result
454 printf(" > Provisioned Selected Pairwise Devices\n");
455 printf(" > Please Check Device's Status for the Linked Result, with [33] Menu\n");
460 OCDeleteACLList(acl[0]);
461 OCDeleteACLList(acl[1]);
465 static int provisionCred(void)
467 // check |own_list| for provisioning pairwise credentials
468 if(!g_own_list || 2>g_own_cnt)
470 printf(" > Owned Device List, to Provision Credentials, is Empty\n");
471 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
472 return 0; // normal case
475 // select two devices for provisioning pairwise credentials
476 int dev_num[2] = {0};
477 if(selectTwoDiffNum(&(dev_num[0]), &(dev_num[1]), g_own_cnt, "for Linking CRED(s)"))
479 OIC_LOG(ERROR, TAG, "selectTwoDiffNum error return");
483 printf(" Select PSK length..\n");
484 printf(" 1 - 128bit(Default)\n");
485 printf(" 2 - 256bit\n");
488 for(int ret=0; 1!=ret; )
490 ret = scanf("%d",&sizeOption);
491 for( ; 0x20<=getchar(); ); // for removing overflow garbages
492 // '0x20<=code' is character region
500 size = OWNER_PSK_LENGTH_128;
505 size = OWNER_PSK_LENGTH_256;
510 size = OWNER_PSK_LENGTH_128;
516 // call |OCProvisionCredentials| API actually
517 // calling this API with callback actually acts like blocking
518 // for error checking, the return value saved and printed
520 printf(" Provisioning Selected Pairwise Credentials..\n");
522 OCProvisionCredentials((void*) g_ctx,
523 SYMMETRIC_PAIR_WISE_KEY, size,
524 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[0]),
525 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[1]),
527 if(OC_STACK_OK != rst)
529 OIC_LOG_V(ERROR, TAG, "OCProvisionCredentials API error: %d", rst);
532 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
534 OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
538 // display the CRED-provisioned result
539 printf(" > Provisioned Selected Pairwise Crendentials\n");
540 printf(" > Please Check Device's Status for the Linked Result, with [33] Menu\n");
545 static int provisionAcl(void)
547 // check |own_list| for provisioning access control list
548 if(!g_own_list || 1>g_own_cnt)
550 printf(" > Owned Device List, to Provision ACL, is Empty\n");
551 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
552 return 0; // normal case
555 // select device for provisioning access control list
559 printf(" > Enter Device Number, for Provisioning ACL: ");
560 for(int ret=0; 1!=ret; )
562 ret = scanf("%d", &dev_num);
563 for( ; 0x20<=getchar(); ); // for removing overflow garbages
564 // '0x20<=code' is character region
566 if(0<dev_num && g_own_cnt>=dev_num)
570 printf(" Entered Wrong Number. Please Enter Again\n");
573 // create ACL for selected device
574 OicSecAcl_t* acl = NULL;
575 acl = createAcl(dev_num);
578 OIC_LOG(ERROR, TAG, "createAcl error return");
582 // call |OCProvisionACL| API actually
583 // calling this API with callback actually acts like blocking
584 // for error checking, the return value saved and printed
586 printf(" Provisioning Selected ACL..\n");
587 OCProvisionDev_t* dev = getDevInst((const OCProvisionDev_t*) g_own_list, dev_num);
590 OIC_LOG(ERROR, TAG, "provisionAcl: device instance empty");
593 OCStackResult rst = OCProvisionACL((void*) g_ctx, dev, acl, provisionAclCB);
594 if(OC_STACK_OK != rst)
596 OIC_LOG_V(ERROR, TAG, "OCProvisionACL API error: %d", rst);
599 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
601 OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
604 OCDeleteACLList(acl); // after here |acl| points nothing
606 // display the ACL-provisioned result
607 printf(" > Provisioned Selected ACL\n");
612 OCDeleteACLList(acl); // after here |acl| points nothing
616 static int provisionDirectPairing(void)
618 // check |own_list| for provisioning direct-pairing
619 if(!g_own_list || 1>g_own_cnt)
621 printf(" > Owned Device List, to Provision ACL, is Empty\n");
622 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
623 return 0; // normal case
626 // select device for provisioning direct-pairing
630 printf(" > Enter Device Number, for Provisioning Direct-Pairing: ");
631 for(int ret=0; 1!=ret; )
633 ret = scanf("%d", &dev_num);
634 for( ; 0x20<=getchar(); ); // for removing overflow garbages
635 // '0x20<=code' is character region
637 if(0<dev_num && g_own_cnt>=dev_num)
641 printf(" Entered Wrong Number. Please Enter Again\n");
644 // create Direct-Pairing Configuration(PIN, PDACL) for selected device
645 // TODO: default acl -> input from user !
647 memset(&pconf, 0, sizeof(OicSecPconf_t));
652 // set default supported PRM types
653 pconf.prmLen = sizeof(SUPPORTED_PRMS)/sizeof(OicSecPrm_t);
654 pconf.prm = (OicSecPrm_t *)OICCalloc(pconf.prmLen, sizeof(OicSecPrm_t));
657 for (size_t i=0; i<pconf.prmLen; i++)
659 pconf.prm[i] = SUPPORTED_PRMS[i];
664 OIC_LOG(ERROR, TAG, "create prm error return");
669 const char DP_DEFAULT_PIN[] = "00000000";
670 memcpy(pconf.pin.val, DP_DEFAULT_PIN, DP_PIN_LENGTH);
673 pconf.pdacls = createPdAcl(dev_num);
676 OIC_LOG(ERROR, TAG, "createPdAcl error return");
680 // call |OCProvisionDirectPairing| API actually
681 // calling this API with callback actually acts like blocking
682 // for error checking, the return value saved and printed
684 printf(" Atempt Direct-Pairing Provisioning (PIN : [%s])..\n", (char*)pconf.pin.val);
685 OCStackResult rst = OCProvisionDirectPairing((void*) g_ctx,
686 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num),
687 &pconf, provisionDPCB);
688 if(OC_STACK_OK != rst)
690 OIC_LOG_V(ERROR, TAG, "OCProvisionDirectPairing API error: %d", rst);
691 if (OC_STACK_UNAUTHORIZED_REQ == rst)
693 OIC_LOG(ERROR, TAG, "Target Server NOT Support Direct-Pairing !!! (DPC == false)");
697 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
699 OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
702 OCDeletePdAclList(pconf.pdacls);
704 // display the PCONF-provisioned result
705 printf(" > SUCCESS to provision Direct-Pairing !!\n");
710 OCDeletePdAclList(pconf.pdacls); // after here |acl| points nothing
714 static int checkLinkedStatus(void)
716 // check |own_list| for checking selected link status on PRVN DB
717 if(!g_own_list || 1>g_own_cnt)
719 printf(" > Owned Device List, to Check Linked Status on PRVN DB, is Empty\n");
720 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
721 return 0; // normal case
724 // select device for checking selected link status on PRVN DB
728 printf(" > Enter Device Number, for Checking Linked Status on PRVN DB: ");
729 for(int ret=0; 1!=ret; )
731 ret = scanf("%d", &dev_num);
732 for( ; 0x20<=getchar(); ); // for removing overflow garbages
733 // '0x20<=code' is character region
735 if(0<dev_num && g_own_cnt>=dev_num)
739 printf(" Entered Wrong Number. Please Enter Again\n");
742 // call |OCGetLinkedStatus| API actually
743 printf(" Checking Selected Link Status on PRVN DB..\n");
744 OCUuidList_t* dvid_lst = NULL;
746 OCProvisionDev_t* dev = getDevInst((const OCProvisionDev_t*)g_own_list, dev_num);
747 if(!dev || !dev->doxm)
749 OIC_LOG(ERROR, TAG, "checkLinkedStatus: device instance empty");
755 &dev->doxm->deviceID,
756 &dvid_lst, &dvid_cnt)) // allow empty list
758 OIC_LOG(ERROR, TAG, "OCGetLinkedStatus API error");
762 // display the linked status result
763 printf(" > Checked Selected Link Status on PRVN DB\n");
764 if(!dvid_lst || !dvid_cnt) // |size_t| is unsigned
766 printf(" Linked Device List is Empty..\n");
767 return 0; // normal case
769 if(dvid_cnt != printUuidList((const OCUuidList_t*) dvid_lst))
771 OIC_LOG(ERROR, TAG, "printUuidList error return");
774 OCDeleteUuidList(dvid_lst);
779 OCDeleteUuidList(dvid_lst);
783 static int unlinkPairwise(void)
785 // check |own_list| for unlinking pairwise devices
786 if(!g_own_list || 2>g_own_cnt)
788 printf(" > Owned Device List, to Unlink the Pairwise, is Empty\n");
789 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
790 return 0; // normal case
793 // select two devices for unlinking pairwise devices
794 int dev_num[2] = {0};
795 if(selectTwoDiffNum(&(dev_num[0]), &(dev_num[1]), g_own_cnt, "for Unlinking Devices"))
797 OIC_LOG(ERROR, TAG, "selectTwoDiffNum error return");
801 // call |OCUnlinkDevices| API actually
802 // calling this API with callback actually acts like blocking
803 // for error checking, the return value saved and printed
805 printf(" Unlinking Selected Pairwise Devices..\n");
807 OCUnlinkDevices((void*) g_ctx,
808 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[0]),
809 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[1]),
811 if(OC_STACK_OK != rst)
813 OIC_LOG_V(ERROR, TAG, "OCUnlinkDevices API error: %d", rst);
816 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
818 OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
822 // display the pairwise-unlinked result
823 printf(" > Unlinked Selected Pairwise Devices\n");
824 printf(" > Please Check Device's Status for the Unlinked Result, with [33] Menu\n");
829 static int removeDevice(void)
831 // check |own_list| for removing device
832 if(!g_own_list || 1>g_own_cnt)
834 printf(" > Owned Device List, to Remove Device, is Empty\n");
835 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
836 return 0; // normal case
839 // select device for removing it
843 printf(" > Enter Device Number, for Removing Device: ");
844 for(int ret=0; 1!=ret; )
846 ret = scanf("%d", &dev_num);
847 for( ; 0x20<=getchar(); ); // for removing overflow garbages
848 // '0x20<=code' is character region
850 if(0<dev_num && g_own_cnt>=dev_num)
854 printf(" Entered Wrong Number. Please Enter Again\n");
857 // call |OCRemoveDevice| API actually
858 // calling this API with callback actually acts like blocking
859 // for error checking, the return value saved and printed
861 printf(" Removing Selected Owned Device..\n");
863 OCRemoveDevice((void*) g_ctx, DISCOVERY_TIMEOUT,
864 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num), removeDeviceCB);
865 if(OC_STACK_OK != rst)
867 OIC_LOG_V(ERROR, TAG, "OCRemoveDevice API error: %d", rst);
870 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
872 OIC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
876 // display the removed result
877 printf(" > Removed Selected Owned Device\n");
878 printf(" > Please Discover Owned Devices for the Registered Result, with [10|12] Menu\n");
883 static OicSecAcl_t* createAcl(const int dev_num)
885 if(0>=dev_num || g_own_cnt<dev_num)
887 OIC_LOG(ERROR, TAG, "createAcl invalid parameters");
888 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
891 // allocate memory for |acl| struct
892 printf(" **** Create ACL for the Selected Device[%d]\n", dev_num);
893 OicSecAcl_t* acl = (OicSecAcl_t*) OICCalloc(1, sizeof(OicSecAcl_t));
896 OIC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
897 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
900 // enter |subject| device number
904 printf(" > [A] Enter Subject Device Number: ");
905 for(int ret=0; 1!=ret; )
907 ret = scanf("%d", &num);
908 for( ; 0x20<=getchar(); ); // for removing overflow garbages
909 // '0x20<=code' is character region
911 if(0<num && g_own_cnt>=num && dev_num!=num)
915 printf(" Entered Wrong Number. Please Enter Again\n");
918 OCProvisionDev_t* dev = getDevInst((const OCProvisionDev_t*)g_own_list, num);
919 if(!dev || !dev->doxm)
921 OIC_LOG(ERROR, TAG, "createAcl: device instance empty");
924 memcpy(&acl->subject, &dev->doxm->deviceID, UUID_LENGTH);
926 // enter number of |resources| in 'accessed' device
929 printf(" > [B] Enter Number of Accessed Resources (under 16): ");
930 // '16' is |ACL_RESRC_MAX_NUM|
931 for(int ret=0; 1!=ret; )
933 ret = scanf("%d", &num);
934 for( ; 0x20<=getchar(); ); // for removing overflow garbages
935 // '0x20<=code' is character region
937 if(0<num && ACL_RESRC_MAX_NUM>=num)
941 printf(" Entered Wrong Number. Please Enter under 16 Again\n");
942 // '16' is |ACL_RESRC_MAX_NUM|
945 // enter actually each 'accessed' |resources| name
946 printf(" Enter Each Accessed Resource Name (each under 128 char)\n");
947 // '128' is ACL_RESRC_MAX_LEN
948 acl->resourcesLen = (unsigned) num;
949 acl->resources = (char**) OICCalloc(acl->resourcesLen, sizeof(char*));
952 OIC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
955 char rsrc_in[ACL_RESRC_MAX_LEN+1] = {0}; // '1' for null termination
956 for(int i=0; acl->resourcesLen>(unsigned)i; ++i)
958 printf(" Enter Accessed Resource[%d] Name: ", i+1);
959 for(int ret=0; 1!=ret; )
961 ret = scanf("%128s", rsrc_in); // '128' is ACL_RESRC_MAX_LEN
962 for( ; 0x20<=getchar(); ); // for removing overflow garbages
963 // '0x20<=code' is character region
965 size_t len = strlen(rsrc_in)+1; // '1' for null termination
966 char* rsrc = (char*) OICCalloc(len, sizeof(char));
969 OIC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
972 OICStrcpy(rsrc, len, rsrc_in);
973 acl->resources[i] = rsrc; // after here, |rsrc| points nothing
976 // enter |permission| for this access
977 printf(" > [C] Enter Permission for This Access\n");
978 uint16_t pmsn = PERMISSION_FULL_CONTROL; // default full permission
979 uint16_t pmsn_msk = PERMISSION_CREATE; // default permission mask
980 for(int i=0; ACL_PEMISN_CNT>i; ++i)
985 printf(" Enter %s Permission (y/n): ", ACL_PEMISN[i]);
986 for(int ret=0; 1!=ret; )
988 ret = scanf("%c", &ans);
989 for( ; 0x20<=getchar(); ); // for removing overflow garbages
990 // '0x20<=code' is character region
992 if('y'==ans || 'Y'==ans || 'n'==ans|| 'N'==ans)
994 ans &= ~0x20; // for masking lower case, 'y/n'
997 printf(" Entered Wrong Answer. Please Enter 'y/n' Again\n");
999 if('N' == ans) // masked lower case, 'n'
1005 acl->permission = pmsn;
1007 // enter |owner| device number
1011 printf(" > [D] Enter Owner Device Number: ");
1012 for(int ret=0; 1!=ret; )
1014 ret = scanf("%d", &own_num);
1015 for( ; 0x20<=getchar(); ); // for removing overflow garbages
1016 // '0x20<=code' is character region
1018 if(0<own_num && g_own_cnt>=own_num)
1022 printf(" Entered Wrong Number. Please Enter Again\n");
1025 dev = getDevInst((const OCProvisionDev_t*)g_own_list, own_num);
1026 if(!dev || !dev->doxm)
1028 OIC_LOG(ERROR, TAG, "createAcl: device instance empty");
1031 memcpy(&acl->rownerID, &dev->doxm->deviceID, sizeof(OicUuid_t));
1037 OCDeleteACLList(acl); // after here |acl| points nothing
1041 static OicSecPdAcl_t* createPdAcl(const int dev_num)
1043 if(0>=dev_num || g_own_cnt<dev_num)
1045 OIC_LOG(ERROR, TAG, "createAcl invalid parameters");
1046 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
1049 // allocate memory for |pdacl| struct
1050 printf(" **** Create PDACL for the Selected Device[%d]\n", dev_num);
1051 OicSecPdAcl_t* pdAcl = (OicSecPdAcl_t*) OICCalloc(1, sizeof(OicSecPdAcl_t));
1054 OIC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
1055 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
1059 // number of resources
1060 char rsrc_in[][ACL_RESRC_MAX_LEN+1] = {"*", "/rsrc/*"};
1061 pdAcl->resourcesLen = 1;
1064 int num = pdAcl->resourcesLen;
1065 pdAcl->resources = (char**) OICCalloc(num, sizeof(char*));
1066 if(!pdAcl->resources)
1068 OIC_LOG(ERROR, TAG, "createPdAcl: OICCalloc error return");
1071 for(int i=0; num>i; ++i)
1073 size_t len = strlen(rsrc_in[i])+1; // '1' for null termination
1074 char* rsrc = (char*) OICCalloc(len, sizeof(char));
1077 OIC_LOG(ERROR, TAG, "createPdAcl: OICCalloc error return");
1080 OICStrcpy(rsrc, len, rsrc_in[i]);
1081 pdAcl->resources[i] = rsrc; // after here, |rsrc| points nothing
1085 pdAcl->permission = PERMISSION_FULL_CONTROL;
1090 OCDeletePdAclList(pdAcl);
1094 static OCProvisionDev_t* getDevInst(const OCProvisionDev_t* dev_lst, const int dev_num)
1096 if(!dev_lst || 0>=dev_num)
1098 printf(" Device List is Empty..\n");
1102 OCProvisionDev_t* lst = (OCProvisionDev_t*) dev_lst;
1112 return NULL; // in here |lst| is always |NULL|
1115 static int printDevList(const OCProvisionDev_t* dev_lst)
1119 printf(" Device List is Empty..\n\n");
1123 OCProvisionDev_t* lst = (OCProvisionDev_t*) dev_lst;
1127 printf(" [%d] ", ++lst_cnt);
1128 printUuid((const OicUuid_t*) &lst->doxm->deviceID);
1137 static size_t printUuidList(const OCUuidList_t* uid_lst)
1141 printf(" Device List is Empty..\n\n");
1145 OCUuidList_t* lst = (OCUuidList_t*) uid_lst;
1149 printf(" [%zu] ", ++lst_cnt);
1150 printUuid((const OicUuid_t*) &lst->dev);
1159 static int printResultList(const OCProvisionResult_t* rslt_lst, const int rslt_cnt)
1161 if(!rslt_lst || 0>=rslt_cnt)
1163 printf(" Device List is Empty..\n\n");
1168 for( ; rslt_cnt>lst_cnt; ++lst_cnt)
1170 printf(" [%d] ", lst_cnt+1);
1171 printUuid((const OicUuid_t*) &rslt_lst[lst_cnt].deviceId);
1172 printf(" - result: %d\n", rslt_lst[lst_cnt].res);
1179 static void printUuid(const OicUuid_t* uid)
1181 for(int i=0; i<UUID_LENGTH; )
1183 printf("%02X", (*uid).id[i++]);
1184 if(i==4 || i==6 || i==8 || i==10) // canonical format for UUID has '8-4-4-4-12'
1191 static FILE* fopen_prvnMng(const char* path, const char* mode)
1193 (void)path; // unused |path| parameter
1195 // input |g_svr_db_fname| internally by force, not using |path| parameter
1196 // because |OCPersistentStorage::open| is called |OCPersistentStorage| internally
1197 // with its own |SVR_DB_FILE_NAME|
1198 return fopen(SVR_DB_FILE_NAME, mode);
1201 static int waitCallbackRet(void)
1203 for(int i=0; !g_doneCB && CALLBACK_TIMEOUT>i; ++i)
1206 if(OC_STACK_OK != OCProcess())
1208 OIC_LOG(ERROR, TAG, "OCStack process error");
1216 static int selectTwoDiffNum(int* a, int* b, const int max, const char* str)
1218 if(!a || !b || 2>=max || !str)
1225 for(int i=0; 2>i; ++i)
1227 int* num = 0==i?a:b;
1230 printf(" > Enter Device[%d] Number, %s: ", i+1, str);
1231 for(int ret=0; 1!=ret; )
1233 ret = scanf("%d", num);
1234 for( ; 0x20<=getchar(); ); // for removing overflow garbages
1235 // '0x20<=code' is character region
1237 if(0<*num && max>=*num)
1241 printf(" Entered Wrong Number. Please Enter Again\n");
1254 static void printMenu(void)
1256 printf("************************************************************\n");
1257 printf("****** OIC Provisioning Client with using C-level API ******\n");
1258 printf("************************************************************\n\n");
1260 printf("** [A] DISCOVER DEVICES ON NETWORK\n");
1261 printf("** 10. Discover All Un/Owned Devices on Network\n");
1262 printf("** 11. Discover Only Unowned Devices on Network\n");
1263 printf("** 12. Discover Only Owned Devices on Network\n\n");
1265 printf("** [B] REGISTER/OWN ALL DISCOVERED UNOWNED DEVICES\n");
1266 printf("** 20. Register/Own All Discovered Unowned Devices\n\n");
1268 printf("** [C] PROVISION/LINK PAIRWISE THINGS\n");
1269 printf("** 30. Provision/Link Pairwise Things\n");
1270 printf("** 31. Provision Credentials for Pairwise Things\n");
1271 printf("** 32. Provision the Selected Access Control List(ACL)\n");
1272 printf("** 33. Provision Direct-Pairing Configuration\n");
1273 printf("** 34. Check Linked Status of the Selected Device on PRVN DB\n\n");
1275 printf("** [D] UNLINK PAIRWISE THINGS\n");
1276 printf("** 40. Unlink Pairwise Things\n\n");
1278 printf("** [E] REMOVE THE SELECTED DEVICE\n");
1279 printf("** 50. Remove the Selected Device\n\n");
1281 printf("** [F] EXIT PROVISIONING CLIENT\n");
1282 printf("** 99. Exit Provisionong Client\n\n");
1284 printf("************************************************************\n\n");
1287 #if 0 // Code for enabling path configuration for PDB and SVR DBf
1288 static void printUsage(void)
1291 printf("OIC Provisioning Client with using C-level API\n");
1292 printf("Usage: provisioningclient [option]...\n");
1294 printf(" -h print help for this provisioning client\n");
1295 printf(" -p=[prvn_db_file_path/name] input PRVN DB file path and name\n");
1296 printf(" if not exists, will load default DB file\n");
1297 printf(" (default: |oic_prvn_mng.db| on working dir)\n");
1298 printf(" (ex. -p=oic_prvn_mng.db)\n");
1299 printf(" -s=[svr_db_file_path/name] input SVR DB file path and name\n");
1300 printf(" if not exists, will load default DB file\n");
1301 printf(" (default: |oic_svr_db_client.json| on working dir)\n");
1302 printf(" (ex. -s=oic_svr_db_client.json)\n");
1307 // main function for provisioning client using C-level provisioning API
1310 // initialize provisioning client
1311 if(initProvisionClient())
1313 OIC_LOG(ERROR, TAG, "ProvisionClient init error");
1317 // main loop for provisioning manager
1323 printf(">> Enter Menu Number: ");
1324 for(int ret=0; 1!=ret; )
1326 ret = scanf("%d", &mn_num);
1327 for( ; 0x20<=getchar(); ); // for removing overflow garbages
1328 // '0x20<=code' is character region
1333 case _10_DISCOV_ALL_DEVS_:
1334 if(discoverAllDevices())
1336 OIC_LOG(ERROR, TAG, "_10_DISCOV_ALL_DEVS_: error");
1339 case _11_DISCOV_UNOWN_DEVS_:
1340 if(discoverUnownedDevices())
1342 OIC_LOG(ERROR, TAG, "_11_DISCOV_UNOWN_DEVS_: error");
1345 case _12_DISCOV_OWN_DEVS_:
1346 if(discoverOwnedDevices())
1348 OIC_LOG(ERROR, TAG, "_12_DISCOV_OWN_DEVS_: error");
1351 case _20_REGIST_DEVS_:
1352 if(registerDevices())
1354 OIC_LOG(ERROR, TAG, "_20_REGIST_DEVS_: error");
1357 case _30_PROVIS_PAIR_DEVS_:
1358 if(provisionPairwise())
1360 OIC_LOG(ERROR, TAG, "_30_PROVIS_PAIR_DEVS_: error");
1363 case _31_PROVIS_CRED_:
1366 OIC_LOG(ERROR, TAG, "_31_PROVIS_CRED_: error");
1369 case _32_PROVIS_ACL_:
1372 OIC_LOG(ERROR, TAG, "_32_PROVIS_ACL_: error");
1375 case _33_PROVIS_DP_:
1376 if(provisionDirectPairing())
1378 OIC_LOG(ERROR, TAG, "_33_PROVIS_DP_: error");
1381 case _34_CHECK_LINK_STATUS_:
1382 if(checkLinkedStatus())
1384 OIC_LOG(ERROR, TAG, "_34_CHECK_LINK_STATUS_: error");
1387 case _40_UNLINK_PAIR_DEVS_:
1388 if(unlinkPairwise())
1390 OIC_LOG(ERROR, TAG, "_40_UNLINK_PAIR_DEVS_: error");
1393 case _50_REMOVE_SELEC_DEV_:
1396 OIC_LOG(ERROR, TAG, "_50_REMOVE_SELEC_DEV_: error");
1399 case _99_EXIT_PRVN_CLT_:
1402 printf(">> Entered Wrong Number. Please Enter Again\n\n");
1408 if(OC_STACK_OK != OCStop())
1410 OIC_LOG(ERROR, TAG, "OCStack stop error");
1412 OCDeleteDiscoveredDevices(g_own_list); // after here |g_own_list| points nothing
1413 OCDeleteDiscoveredDevices(g_unown_list); // after here |g_unown_list| points nothing
1417 OICFree(g_svr_fname); // after here |g_svr_fname| points nothing
1421 OICFree(g_prvn_fname); // after here |g_prvn_fname| points nothing
1423 return 0; // always return normal case
1428 #endif //__cplusplus