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 *****************************************************************/
26 #include "oic_malloc.h"
27 #include "oic_string.h"
28 #include "ocprovisioningmanager.h"
29 #include "oxmjustworks.h"
30 #include "oxmrandompin.h"
31 #include "securevirtualresourcetypes.h"
32 #include "srmutility.h"
40 // declaration(s) for provisioning client using C-level provisioning API
41 // user input definition for main loop on provisioning client
42 #define _10_DISCOV_ALL_DEVS_ 10
43 #define _11_DISCOV_UNOWN_DEVS_ 11
44 #define _12_DISCOV_OWN_DEVS_ 12
45 #define _20_REGIST_DEVS_ 20
46 #define _30_PROVIS_PAIR_DEVS_ 30
47 #define _31_PROVIS_CRED_ 31
48 #define _32_PROVIS_ACL_ 32
49 #define _33_CHECK_LINK_STATUS_ 33
50 #define _40_UNLINK_PAIR_DEVS_ 40
51 #define _50_REMOVE_SELEC_DEV_ 50
52 #define _99_EXIT_PRVN_CLT_ 99
54 #define ACL_RESRC_MAX_NUM 16
55 #define ACL_RESRC_MAX_LEN 128
56 #define ACL_PEMISN_CNT 5
57 #define DISCOVERY_TIMEOUT 10 // 10 sec
58 #define CALLBACK_TIMEOUT 60 // 1 min
59 #define TAG "provisioningclient"
61 static const char* ACL_PEMISN[5] = {"CREATE", "READ", "WRITE", "DELETE", "NOTIFY"};
62 static const char* SVR_DB_FILE_NAME = "oic_svr_db_client.json";
63 // '_' for separaing from the same constant variable in |srmresourcestrings.c|
64 static const char* PRVN_DB_FILE_NAME = "oic_prvn_mng.db";
65 // |g_ctx| means provision manager application context and
66 // the following, includes |un/own_list|, could be variables, which |g_ctx| has,
67 // for accessing all function(s) for these, they are declared on global domain
68 static const char* g_ctx = "Provision Manager Client Application Context";
69 static char* g_svr_fname;
70 static char* g_prvn_fname;
71 static OCProvisionDev_t* g_own_list;
72 static OCProvisionDev_t* g_unown_list;
74 static int g_unown_cnt;
77 // function declaration(s) for calling them before implementing
78 static OicSecAcl_t* createAcl(const int);
79 static OCProvisionDev_t* getDevInst(const OCProvisionDev_t*, const int);
80 static int printDevList(const OCProvisionDev_t*);
81 static size_t printUuidList(const OCUuidList_t*);
82 static int printResultList(const OCProvisionResult_t*, const int);
83 static void printUuid(const OicUuid_t*);
84 static FILE* fopen_prvnMng(const char*, const char*);
85 static int waitCallbackRet(void);
86 static int selectTwoDiffNum(int*, int*, const int, const char*);
88 // callback function(s) for provisioning client using C-level provisioning API
89 static void ownershipTransferCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
93 OC_LOG_V(INFO, TAG, "Ownership Transfer SUCCEEDED - ctx: %s", (char*) ctx);
97 OC_LOG_V(ERROR, TAG, "Ownership Transfer FAILED - ctx: %s", (char*) ctx);
98 printResultList((const OCProvisionResult_t*) arr, nOfRes);
103 static void provisionPairwiseCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
107 OC_LOG_V(INFO, TAG, "Provision Pairwise SUCCEEDED - ctx: %s", (char*) ctx);
111 OC_LOG_V(ERROR, TAG, "Provision Pairwise FAILED - ctx: %s", (char*) ctx);
112 printResultList((const OCProvisionResult_t*) arr, nOfRes);
117 static void provisionCredCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
121 OC_LOG_V(INFO, TAG, "Provision Credential SUCCEEDED - ctx: %s", (char*) ctx);
125 OC_LOG_V(ERROR, TAG, "Provision Credential FAILED - ctx: %s", (char*) ctx);
126 printResultList((const OCProvisionResult_t*) arr, nOfRes);
131 static void provisionAclCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
135 OC_LOG_V(INFO, TAG, "Provision ACL SUCCEEDED - ctx: %s", (char*) ctx);
139 OC_LOG_V(ERROR, TAG, "Provision ACL FAILED - ctx: %s", (char*) ctx);
140 printResultList((const OCProvisionResult_t*) arr, nOfRes);
145 static void unlinkDevicesCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
149 OC_LOG_V(INFO, TAG, "Unlink Devices SUCCEEDED - ctx: %s", (char*) ctx);
153 OC_LOG_V(ERROR, TAG, "Unlink Devices FAILED - ctx: %s", (char*) ctx);
154 printResultList((const OCProvisionResult_t*) arr, nOfRes);
159 static void removeDeviceCB(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool hasError)
163 OC_LOG_V(INFO, TAG, "Remove Device SUCCEEDED - ctx: %s", (char*) ctx);
167 OC_LOG_V(ERROR, TAG, "Remove Device FAILED - ctx: %s", (char*) ctx);
168 printResultList((const OCProvisionResult_t*) arr, nOfRes);
173 static void inputPinCB(char* pin, size_t len)
175 if(!pin || OXM_RANDOM_PIN_SIZE>=len)
177 OC_LOG(ERROR, TAG, "inputPinCB invalid parameters");
181 printf(" > INPUT PIN: ");
182 for(int ret=0; 1!=ret; )
184 ret = scanf("%8s", pin);
185 for( ; 0x20<=getchar(); ); // for removing overflow garbages
186 // '0x20<=code' is character region
190 // function(s) for provisioning client using C-level provisioning API
191 static int initProvisionClient(void)
193 // initialize persistent storage for SVR DB
194 static OCPersistentStorage pstStr =
196 .open = fopen_prvnMng,
202 if(OC_STACK_OK != OCRegisterPersistentStorageHandler(&pstStr))
204 OC_LOG(ERROR, TAG, "OCRegisterPersistentStorageHandler error");
208 // initialize OC stack and provisioning manager
209 if(OC_STACK_OK != OCInit(NULL, 0, OC_CLIENT_SERVER))
211 OC_LOG(ERROR, TAG, "OCStack init error");
215 if (access(PRVN_DB_FILE_NAME, F_OK) != -1)
217 printf("************************************************************\n");
218 printf("************Provisioning DB file already exists.************\n");
219 printf("************************************************************\n");
223 printf("*************************************************************\n");
224 printf("************No provisioning DB file, creating new************\n");
225 printf("*************************************************************\n");
228 if(OC_STACK_OK != OCInitPM(PRVN_DB_FILE_NAME))
230 OC_LOG(ERROR, TAG, "OC_PM init error");
234 // register callback function(s) to each OxM
235 OTMCallbackData_t otmcb =
237 .loadSecretCB = LoadSecretJustWorksCallback,
238 .createSecureSessionCB = CreateSecureSessionJustWorksCallback,
239 .createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload,
240 .createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload
242 if(OC_STACK_OK != OCSetOwnerTransferCallbackData(OIC_JUST_WORKS, &otmcb))
244 OC_LOG(ERROR, TAG, "OCSetOwnerTransferCallbackData error: OIC_JUST_WORKS");
247 otmcb.loadSecretCB = InputPinCodeCallback;
248 otmcb.createSecureSessionCB = CreateSecureSessionRandomPinCallbak;
249 otmcb.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
250 otmcb.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
251 if(OC_STACK_OK != OCSetOwnerTransferCallbackData(OIC_RANDOM_DEVICE_PIN, &otmcb))
253 OC_LOG(ERROR, TAG, "OCSetOwnerTransferCallbackData error: OIC_RANDOM_DEVICE_PIN");
256 SetInputPinCB(inputPinCB);
261 static int discoverAllDevices(void)
263 // delete un/owned device lists before updating them
266 OCDeleteDiscoveredDevices(g_own_list);
271 OCDeleteDiscoveredDevices(g_unown_list);
275 // call |OCGetDevInfoFromNetwork| API actually
276 printf(" Discovering All Un/Owned Devices on Network..\n");
277 if(OC_STACK_OK != OCGetDevInfoFromNetwork(DISCOVERY_TIMEOUT, &g_own_list, &g_unown_list))
279 OC_LOG(ERROR, TAG, "OCGetDevInfoFromNetwork API error");
283 // display the discovered un/owned lists
284 printf(" > Discovered Owned Devices\n");
285 g_own_cnt = printDevList(g_own_list);
286 printf(" > Discovered Unowned Devices\n");
287 g_unown_cnt = printDevList(g_unown_list);
293 static int discoverUnownedDevices(void)
295 // delete unowned device list before updating it
298 OCDeleteDiscoveredDevices(g_unown_list);
302 // call |OCDiscoverUnownedDevices| API actually
303 printf(" Discovering Only Unowned Devices on Network..\n");
304 if(OC_STACK_OK != OCDiscoverUnownedDevices(DISCOVERY_TIMEOUT, &g_unown_list))
306 OC_LOG(ERROR, TAG, "OCDiscoverUnownedDevices API error");
310 // display the discovered unowned list
311 printf(" > Discovered Unowned Devices\n");
312 g_unown_cnt = printDevList(g_unown_list);
317 static int discoverOwnedDevices(void)
319 // delete owned device list before updating it
322 OCDeleteDiscoveredDevices(g_own_list);
326 // call |OCDiscoverOwnedDevices| API actually
327 printf(" Discovering Only Owned Devices on Network..\n");
328 if(OC_STACK_OK != OCDiscoverOwnedDevices(DISCOVERY_TIMEOUT, &g_own_list))
330 OC_LOG(ERROR, TAG, "OCDiscoverOwnedDevices API error");
334 // display the discovered owned list
335 printf(" > Discovered Owned Devices\n");
336 g_own_cnt = printDevList(g_own_list);
341 static int registerDevices(void)
343 // check |unown_list| for registering devices
344 if(!g_unown_list || 0>=g_unown_cnt)
346 printf(" > Unowned Device List, to Register Devices, is Empty\n");
347 printf(" > Please Discover Unowned Devices first, with [10|11] Menu\n");
348 return 0; // normal case
351 // call |OCDoOwnershipTransfer| API actually
352 // calling this API with callback actually acts like blocking
353 // for error checking, the return value saved and printed
355 printf(" Registering All Discovered Unowned Devices..\n");
356 OCStackResult rst = OCDoOwnershipTransfer((void*) g_ctx, g_unown_list, ownershipTransferCB);
357 if(OC_STACK_OK != rst)
359 OC_LOG_V(ERROR, TAG, "OCDoOwnershipTransfer API error: %d", rst);
362 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
364 OC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
368 // display the registered result
369 printf(" > Registered Discovered Unowned Devices\n");
370 printf(" > Please Discover Owned Devices for the Registered Result, with [10|12] Menu\n");
375 static int provisionPairwise(void)
377 // check |own_list| for provisioning pairwise devices
378 if(!g_own_list || 2>g_own_cnt)
380 printf(" > Owned Device List, to Provision the Pairwise, is Empty\n");
381 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
382 return 0; // normal case
385 // select two devices for provisioning pairwise devices
386 int dev_num[2] = {0};
387 if(selectTwoDiffNum(&(dev_num[0]), &(dev_num[1]), g_own_cnt, "for Linking Devices"))
389 OC_LOG(ERROR, TAG, "selectTwoDiffNum error return");
390 return -1; // not need to 'goto' |ERROR| before allocating |acl|
393 // create ACL(s) for each selected device
394 OicSecAcl_t* acl[2] = {0};
395 for(int i=0; 2>i; ++i)
397 acl[i] = createAcl(dev_num[i]);
400 OC_LOG(ERROR, TAG, "createAcl error return");
405 // call |OCProvisionPairwiseDevices| API actually
406 // calling this API with callback actually acts like blocking
407 // for error checking, the return value saved and printed
409 printf(" Provisioning Selected Pairwise Devices..\n");
411 OCProvisionPairwiseDevices((void*) g_ctx,
412 SYMMETRIC_PAIR_WISE_KEY, OWNER_PSK_LENGTH_128,
413 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[0]), acl[0],
414 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[1]), acl[1],
415 provisionPairwiseCB);
416 if(OC_STACK_OK != rst)
418 OC_LOG_V(ERROR, TAG, "OCProvisionPairwiseDevices API error: %d", rst);
421 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
423 OC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
426 OCDeleteACLList(acl[0]);
427 OCDeleteACLList(acl[1]);
429 // display the pairwise-provisioned result
430 printf(" > Provisioned Selected Pairwise Devices\n");
431 printf(" > Please Check Device's Status for the Linked Result, with [33] Menu\n");
436 OCDeleteACLList(acl[0]);
437 OCDeleteACLList(acl[1]);
441 static int provisionCred(void)
443 // check |own_list| for provisioning pairwise credentials
444 if(!g_own_list || 2>g_own_cnt)
446 printf(" > Owned Device List, to Provision Credentials, is Empty\n");
447 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
448 return 0; // normal case
451 // select two devices for provisioning pairwise credentials
452 int dev_num[2] = {0};
453 if(selectTwoDiffNum(&(dev_num[0]), &(dev_num[1]), g_own_cnt, "for Linking CRED(s)"))
455 OC_LOG(ERROR, TAG, "selectTwoDiffNum error return");
459 printf(" Select PSK length..\n");
460 printf(" 1 - 128bit(Default)\n");
461 printf(" 2 - 256bit\n");
464 for(int ret=0; 1!=ret; )
466 ret = scanf("%d",&sizeOption);
467 for( ; 0x20<=getchar(); ); // for removing overflow garbages
468 // '0x20<=code' is character region
476 size = OWNER_PSK_LENGTH_128;
481 size = OWNER_PSK_LENGTH_256;
486 size = OWNER_PSK_LENGTH_128;
492 // call |OCProvisionCredentials| API actually
493 // calling this API with callback actually acts like blocking
494 // for error checking, the return value saved and printed
496 printf(" Provisioning Selected Pairwise Credentials..\n");
498 OCProvisionCredentials((void*) g_ctx,
499 SYMMETRIC_PAIR_WISE_KEY, size,
500 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[0]),
501 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[1]),
503 if(OC_STACK_OK != rst)
505 OC_LOG_V(ERROR, TAG, "OCProvisionCredentials API error: %d", rst);
508 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
510 OC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
514 // display the CRED-provisioned result
515 printf(" > Provisioned Selected Pairwise Crendentials\n");
516 printf(" > Please Check Device's Status for the Linked Result, with [33] Menu\n");
521 static int provisionAcl(void)
523 // check |own_list| for provisioning access control list
524 if(!g_own_list || 1>g_own_cnt)
526 printf(" > Owned Device List, to Provision ACL, is Empty\n");
527 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
528 return 0; // normal case
531 // select device for provisioning access control list
535 printf(" > Enter Device Number, for Provisioning ACL: ");
536 for(int ret=0; 1!=ret; )
538 ret = scanf("%d", &dev_num);
539 for( ; 0x20<=getchar(); ); // for removing overflow garbages
540 // '0x20<=code' is character region
542 if(0<dev_num && g_own_cnt>=dev_num)
546 printf(" Entered Wrong Number. Please Enter Again\n");
549 // create ACL for selected device
550 OicSecAcl_t* acl = NULL;
551 acl = createAcl(dev_num);
554 OC_LOG(ERROR, TAG, "createAcl error return");
558 // call |OCProvisionACL| API actually
559 // calling this API with callback actually acts like blocking
560 // for error checking, the return value saved and printed
562 printf(" Provisioning Selected ACL..\n");
564 OCProvisionACL((void*) g_ctx,
565 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num),
566 acl, provisionAclCB);
567 if(OC_STACK_OK != rst)
569 OC_LOG_V(ERROR, TAG, "OCProvisionACL API error: %d", rst);
572 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
574 OC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
577 OCDeleteACLList(acl); // after here |acl| points nothing
579 // display the ACL-provisioned result
580 printf(" > Provisioned Selected ACL\n");
585 OCDeleteACLList(acl); // after here |acl| points nothing
589 static int checkLinkedStatus(void)
591 // check |own_list| for checking selected link status on PRVN DB
592 if(!g_own_list || 1>g_own_cnt)
594 printf(" > Owned Device List, to Check Linked Status on PRVN DB, is Empty\n");
595 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
596 return 0; // normal case
599 // select device for checking selected link status on PRVN DB
603 printf(" > Enter Device Number, for Checking Linked Status on PRVN DB: ");
604 for(int ret=0; 1!=ret; )
606 ret = scanf("%d", &dev_num);
607 for( ; 0x20<=getchar(); ); // for removing overflow garbages
608 // '0x20<=code' is character region
610 if(0<dev_num && g_own_cnt>=dev_num)
614 printf(" Entered Wrong Number. Please Enter Again\n");
617 // call |OCGetLinkedStatus| API actually
618 printf(" Checking Selected Link Status on PRVN DB..\n");
619 OCUuidList_t* dvid_lst = NULL;
623 &getDevInst((const OCProvisionDev_t*) g_own_list, dev_num)->doxm->deviceID,
624 &dvid_lst, &dvid_cnt)) // allow empty list
626 OC_LOG(ERROR, TAG, "OCGetLinkedStatus API error");
630 // display the linked status result
631 printf(" > Checked Selected Link Status on PRVN DB\n");
632 if(!dvid_lst || !dvid_cnt) // |size_t| is unsigned
634 printf(" Linked Device List is Empty..\n");
635 return 0; // normal case
637 if(dvid_cnt != printUuidList((const OCUuidList_t*) dvid_lst))
639 OC_LOG(ERROR, TAG, "printUuidList error return");
642 OCDeleteUuidList(dvid_lst);
647 OCDeleteUuidList(dvid_lst);
651 static int unlinkPairwise(void)
653 // check |own_list| for unlinking pairwise devices
654 if(!g_own_list || 2>g_own_cnt)
656 printf(" > Owned Device List, to Unlink the Pairwise, is Empty\n");
657 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
658 return 0; // normal case
661 // select two devices for unlinking pairwise devices
662 int dev_num[2] = {0};
663 if(selectTwoDiffNum(&(dev_num[0]), &(dev_num[1]), g_own_cnt, "for Unlinking Devices"))
665 OC_LOG(ERROR, TAG, "selectTwoDiffNum error return");
669 // call |OCUnlinkDevices| API actually
670 // calling this API with callback actually acts like blocking
671 // for error checking, the return value saved and printed
673 printf(" Unlinking Selected Pairwise Devices..\n");
675 OCUnlinkDevices((void*) g_ctx,
676 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[0]),
677 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num[1]),
679 if(OC_STACK_OK != rst)
681 OC_LOG_V(ERROR, TAG, "OCUnlinkDevices API error: %d", rst);
684 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
686 OC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
690 // display the pairwise-unlinked result
691 printf(" > Unlinked Selected Pairwise Devices\n");
692 printf(" > Please Check Device's Status for the Unlinked Result, with [33] Menu\n");
697 static int removeDevice(void)
699 // check |own_list| for removing device
700 if(!g_own_list || 1>g_own_cnt)
702 printf(" > Owned Device List, to Remove Device, is Empty\n");
703 printf(" > Please Register Unowned Devices first, with [20] Menu\n");
704 return 0; // normal case
707 // select device for removing it
711 printf(" > Enter Device Number, for Removing Device: ");
712 for(int ret=0; 1!=ret; )
714 ret = scanf("%d", &dev_num);
715 for( ; 0x20<=getchar(); ); // for removing overflow garbages
716 // '0x20<=code' is character region
718 if(0<dev_num && g_own_cnt>=dev_num)
722 printf(" Entered Wrong Number. Please Enter Again\n");
725 // call |OCRemoveDevice| API actually
726 // calling this API with callback actually acts like blocking
727 // for error checking, the return value saved and printed
729 printf(" Removing Selected Owned Device..\n");
731 OCRemoveDevice((void*) g_ctx, DISCOVERY_TIMEOUT,
732 getDevInst((const OCProvisionDev_t*) g_own_list, dev_num), removeDeviceCB);
733 if(OC_STACK_OK != rst)
735 OC_LOG_V(ERROR, TAG, "OCRemoveDevice API error: %d", rst);
738 if(waitCallbackRet()) // input |g_doneCB| flag implicitly
740 OC_LOG(ERROR, TAG, "OCProvisionCredentials callback error");
744 // display the removed result
745 printf(" > Removed Selected Owned Device\n");
746 printf(" > Please Discover Owned Devices for the Registered Result, with [10|12] Menu\n");
751 static OicSecAcl_t* createAcl(const int dev_num)
753 if(0>=dev_num || g_own_cnt<dev_num)
755 OC_LOG(ERROR, TAG, "createAcl invalid parameters");
756 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
759 // allocate memory for |acl| struct
760 printf(" **** Create ACL for the Selected Device[%d]\n", dev_num);
761 OicSecAcl_t* acl = (OicSecAcl_t*) OICCalloc(1, sizeof(OicSecAcl_t));
764 OC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
765 return NULL; // not need to 'goto' |ERROR| before allocating |acl|
768 // enter |subject| device number
772 printf(" > [A] Enter Subject Device Number: ");
773 for(int ret=0; 1!=ret; )
775 ret = scanf("%d", &num);
776 for( ; 0x20<=getchar(); ); // for removing overflow garbages
777 // '0x20<=code' is character region
779 if(num && g_own_cnt>=num && dev_num!=num)
783 printf(" Entered Wrong Number. Please Enter Again\n");
785 memcpy(&acl->subject,
786 &getDevInst((const OCProvisionDev_t*) g_own_list, num)->doxm->deviceID,
787 UUID_LENGTH); // not need |*sizeof(uint8_t)|
789 // enter number of |resources| in 'accessed' device
792 printf(" > [B] Enter Number of Accessed Resources (under 16): ");
793 // '16' is |ACL_RESRC_MAX_NUM|
794 for(int ret=0; 1!=ret; )
796 ret = scanf("%zu", &acl->resourcesLen);
797 for( ; 0x20<=getchar(); ); // for removing overflow garbages
798 // '0x20<=code' is character region
800 if(acl->resourcesLen && ACL_RESRC_MAX_NUM>=acl->resourcesLen)
801 // |acl->resourcesLen| is unsigned
805 printf(" Entered Wrong Number. Please Enter under 16 Again\n");
806 // '16' is |ACL_RESRC_MAX_NUM|
809 // enter actually each 'accessed' |resources| name
810 printf(" Enter Each Accessed Resource Name (each under 128 char)\n");
811 // '128' is ACL_RESRC_MAX_LEN
812 num = acl->resourcesLen;
813 acl->resources = (char**) OICCalloc(num, sizeof(char*));
816 OC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
819 char rsrc_in[ACL_RESRC_MAX_LEN+1] = {0}; // '1' for null termination
820 for(int i=0; num>i; ++i)
822 printf(" Enter Accessed Resource[%d] Name: ", i+1);
823 for(int ret=0; 1!=ret; )
825 ret = scanf("%128s", rsrc_in); // '128' is ACL_RESRC_MAX_LEN
826 for( ; 0x20<=getchar(); ); // for removing overflow garbages
827 // '0x20<=code' is character region
829 size_t len = strlen(rsrc_in)+1; // '1' for null termination
830 char* rsrc = (char*) OICCalloc(len, sizeof(char));
833 OC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
836 OICStrcpy(rsrc, len, rsrc_in);
837 acl->resources[i] = rsrc; // after here, |rsrc| points nothing
840 // enter |permission| for this access
841 printf(" > [C] Enter Permission for This Access\n");
842 uint16_t pmsn = PERMISSION_FULL_CONTROL; // default full permission
843 uint16_t pmsn_msk = PERMISSION_CREATE; // default permission mask
844 for(int i=0; ACL_PEMISN_CNT>i; ++i)
849 printf(" Enter %s Permission (y/n): ", ACL_PEMISN[i]);
850 for(int ret=0; 1!=ret; )
852 ret = scanf("%c", &ans);
853 for( ; 0x20<=getchar(); ); // for removing overflow garbages
854 // '0x20<=code' is character region
856 if('y'==ans || 'Y'==ans || 'n'==ans|| 'N'==ans)
858 ans &= ~0x20; // for masking lower case, 'y/n'
861 printf(" Entered Wrong Answer. Please Enter 'y/n' Again\n");
863 if('N' == ans) // masked lower case, 'n'
869 acl->permission = pmsn;
871 // enter |owner| device number
874 printf(" > [D] Enter Owner Device Number: ");
875 for(int ret=0; 1!=ret; )
877 ret = scanf("%d", &num);
878 for( ; 0x20<=getchar(); ); // for removing overflow garbages
879 // '0x20<=code' is character region
881 if(num && g_own_cnt>=num)
885 printf(" Entered Wrong Number. Please Enter Again\n");
888 acl->owners = (OicUuid_t*) OICCalloc(1, sizeof(OicUuid_t));
891 OC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
895 &getDevInst((const OCProvisionDev_t*) g_own_list, num)->doxm->deviceID,
896 UUID_LENGTH); // not need |*sizeof(uint8_t)|
902 OCDeleteACLList(acl); // after here |acl| points nothing
906 static OCProvisionDev_t* getDevInst(const OCProvisionDev_t* dev_lst, const int dev_num)
908 if(!dev_lst || 0>=dev_num)
910 printf(" Device List is Empty..\n");
914 OCProvisionDev_t* lst = (OCProvisionDev_t*) dev_lst;
924 return NULL; // in here |lst| is always |NULL|
927 static int printDevList(const OCProvisionDev_t* dev_lst)
931 printf(" Device List is Empty..\n\n");
935 OCProvisionDev_t* lst = (OCProvisionDev_t*) dev_lst;
939 printf(" [%d] ", ++lst_cnt);
940 printUuid((const OicUuid_t*) &lst->doxm->deviceID);
949 static size_t printUuidList(const OCUuidList_t* uid_lst)
953 printf(" Device List is Empty..\n\n");
957 OCUuidList_t* lst = (OCUuidList_t*) uid_lst;
961 printf(" [%zu] ", ++lst_cnt);
962 printUuid((const OicUuid_t*) &lst->dev);
971 static int printResultList(const OCProvisionResult_t* rslt_lst, const int rslt_cnt)
973 if(!rslt_lst || 0>=rslt_cnt)
975 printf(" Device List is Empty..\n\n");
980 for( ; rslt_cnt>lst_cnt; ++lst_cnt)
982 printf(" [%d] ", lst_cnt+1);
983 printUuid((const OicUuid_t*) &rslt_lst[lst_cnt].deviceId);
984 printf(" - result: %d\n", rslt_lst[lst_cnt].res);
991 static void printUuid(const OicUuid_t* uid)
993 for(int i=0; i<UUID_LENGTH; )
995 printf("%02X", (*uid).id[i++]);
996 if(i==4 || i==6 || i==8 || i==10) // canonical format for UUID has '8-4-4-4-12'
1003 static FILE* fopen_prvnMng(const char* path, const char* mode)
1005 (void)path; // unused |path| parameter
1007 // input |g_svr_db_fname| internally by force, not using |path| parameter
1008 // because |OCPersistentStorage::open| is called |OCPersistentStorage| internally
1009 // with its own |SVR_DB_FILE_NAME|
1010 return fopen(SVR_DB_FILE_NAME, mode);
1013 static int waitCallbackRet(void)
1015 for(int i=0; !g_doneCB && CALLBACK_TIMEOUT>i; ++i)
1018 if(OC_STACK_OK != OCProcess())
1020 OC_LOG(ERROR, TAG, "OCStack process error");
1028 static int selectTwoDiffNum(int* a, int* b, const int max, const char* str)
1030 if(!a || !b || 2>=max || !str)
1037 for(int i=0; 2>i; ++i)
1039 int* num = 0==i?a:b;
1042 printf(" > Enter Device[%d] Number, %s: ", i+1, str);
1043 for(int ret=0; 1!=ret; )
1045 ret = scanf("%d", num);
1046 for( ; 0x20<=getchar(); ); // for removing overflow garbages
1047 // '0x20<=code' is character region
1049 if(0<*num && max>=*num)
1053 printf(" Entered Wrong Number. Please Enter Again\n");
1066 static void printMenu(void)
1068 printf("************************************************************\n");
1069 printf("****** OIC Provisioning Client with using C-level API ******\n");
1070 printf("************************************************************\n\n");
1072 printf("** [A] DISCOVER DEVICES ON NETWORK\n");
1073 printf("** 10. Discover All Un/Owned Devices on Network\n");
1074 printf("** 11. Discover Only Unowned Devices on Network\n");
1075 printf("** 12. Discover Only Owned Devices on Network\n\n");
1077 printf("** [B] REGISTER/OWN ALL DISCOVERED UNOWNED DEVICES\n");
1078 printf("** 20. Register/Own All Discovered Unowned Devices\n\n");
1080 printf("** [C] PROVISION/LINK PAIRWISE THINGS\n");
1081 printf("** 30. Provision/Link Pairwise Things\n");
1082 printf("** 31. Provision Credentials for Pairwise Things\n");
1083 printf("** 32. Provision the Selected Access Control List(ACL)\n");
1084 printf("** 33. Check Linked Status of the Selected Device on PRVN DB\n\n");
1086 printf("** [D] UNLINK PAIRWISE THINGS\n");
1087 printf("** 40. Unlink Pairwise Things\n\n");
1089 printf("** [E] REMOVE THE SELECTED DEVICE\n");
1090 printf("** 50. Remove the Selected Device\n\n");
1092 printf("** [F] EXIT PROVISIONING CLIENT\n");
1093 printf("** 99. Exit Provisionong Client\n\n");
1095 printf("************************************************************\n\n");
1098 #if 0 // Code for enabling path configuration for PDB and SVR DBf
1099 static void printUsage(void)
1102 printf("OIC Provisioning Client with using C-level API\n");
1103 printf("Usage: provisioningclient [option]...\n");
1105 printf(" -h print help for this provisioning client\n");
1106 printf(" -p=[prvn_db_file_path/name] input PRVN DB file path and name\n");
1107 printf(" if not exists, will load default DB file\n");
1108 printf(" (default: |oic_prvn_mng.db| on working dir)\n");
1109 printf(" (ex. -p=oic_prvn_mng.db)\n");
1110 printf(" -s=[svr_db_file_path/name] input SVR DB file path and name\n");
1111 printf(" if not exists, will load default DB file\n");
1112 printf(" (default: |oic_svr_db_client.json| on working dir)\n");
1113 printf(" (ex. -s=oic_svr_db_client.json)\n");
1118 // main function for provisioning client using C-level provisioning API
1121 // initialize provisioning client
1122 if(initProvisionClient())
1124 OC_LOG(ERROR, TAG, "ProvisionClient init error");
1128 // main loop for provisioning manager
1134 printf(">> Enter Menu Number: ");
1135 for(int ret=0; 1!=ret; )
1137 ret = scanf("%d", &mn_num);
1138 for( ; 0x20<=getchar(); ); // for removing overflow garbages
1139 // '0x20<=code' is character region
1144 case _10_DISCOV_ALL_DEVS_:
1145 if(discoverAllDevices())
1147 OC_LOG(ERROR, TAG, "_10_DISCOV_ALL_DEVS_: error");
1150 case _11_DISCOV_UNOWN_DEVS_:
1151 if(discoverUnownedDevices())
1153 OC_LOG(ERROR, TAG, "_11_DISCOV_UNOWN_DEVS_: error");
1156 case _12_DISCOV_OWN_DEVS_:
1157 if(discoverOwnedDevices())
1159 OC_LOG(ERROR, TAG, "_12_DISCOV_OWN_DEVS_: error");
1162 case _20_REGIST_DEVS_:
1163 if(registerDevices())
1165 OC_LOG(ERROR, TAG, "_20_REGIST_DEVS_: error");
1168 case _30_PROVIS_PAIR_DEVS_:
1169 if(provisionPairwise())
1171 OC_LOG(ERROR, TAG, "_30_PROVIS_PAIR_DEVS_: error");
1174 case _31_PROVIS_CRED_:
1177 OC_LOG(ERROR, TAG, "_31_PROVIS_CRED_: error");
1180 case _32_PROVIS_ACL_:
1183 OC_LOG(ERROR, TAG, "_32_PROVIS_ACL_: error");
1186 case _33_CHECK_LINK_STATUS_:
1187 if(checkLinkedStatus())
1189 OC_LOG(ERROR, TAG, "_33_CHECK_LINK_STATUS_: error");
1192 case _40_UNLINK_PAIR_DEVS_:
1193 if(unlinkPairwise())
1195 OC_LOG(ERROR, TAG, "_40_UNLINK_PAIR_DEVS_: error");
1198 case _50_REMOVE_SELEC_DEV_:
1201 OC_LOG(ERROR, TAG, "_50_REMOVE_SELEC_DEV_: error");
1204 case _99_EXIT_PRVN_CLT_:
1207 printf(">> Entered Wrong Number. Please Enter Again\n\n");
1213 if(OC_STACK_OK != OCStop())
1215 OC_LOG(ERROR, TAG, "OCStack stop error");
1217 OCDeleteDiscoveredDevices(g_own_list); // after here |g_own_list| points nothing
1218 OCDeleteDiscoveredDevices(g_unown_list); // after here |g_unown_list| points nothing
1222 OICFree(g_svr_fname); // after here |g_svr_fname| points nothing
1226 OICFree(g_prvn_fname); // after here |g_prvn_fname| points nothing
1228 return 0; // always return normal case
1233 #endif //__cplusplus