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 * *****************************************************************/
29 #include <condition_variable>
32 #include "oic_malloc.h"
33 #include "oic_string.h"
34 #include "OCPlatform.h"
36 #include "OCProvisioningManager.h"
37 #include "oxmjustworks.h"
38 #include "oxmrandompin.h"
39 #include "aclresource.h"
42 #define MAX_PERMISSION_LENGTH (5)
43 #define ACL_RESRC_ARRAY_SIZE (3)
50 #define PREDEFINED_TIMEOUT (10)
51 #define MAX_OWNED_DEVICE (10)
52 #define TAG "provisioningclient"
54 #define JSON_DB_PATH "./oic_svr_db_client.json"
55 #define DAT_DB_PATH "./oic_svr_db_client.dat"
56 #define DEV_STATUS_ON "DEV_STATUS_ON"
57 #define DEV_STATUS_OFF "DEV_STATUS_OFF"
59 #define DISCOVERY_TIMEOUT 5
61 static const OicSecPrm_t SUPPORTED_PRMS[1] =
68 DeviceList_t pUnownedDevList, pOwnedDevList;
69 static int transferDevIdx, ask = 1;
70 static OicSecPconf_t g_pconf;
71 static uint16_t g_credId = 0;
73 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
76 return fopen(DAT_DB_PATH, mode);
81 std::cout << "\nChoose an option:"<<std::endl;
82 std::cout << " 1. UnOwned Device discovery"<<std::endl;
83 std::cout << " 2. Owned Device discovery"<<std::endl;
84 std::cout << " 3. Ownership transfer"<<std::endl;
85 std::cout << " 4. Provision ACL"<<std::endl;
86 std::cout << " 5. Provision Credentials"<<std::endl;
87 std::cout << " 6. Credential & ACL provisioning b/w two devices"<<std::endl;
88 std::cout << " 7. Unlink Devices"<<std::endl;
89 std::cout << " 8. Remove Device"<<std::endl;
90 std::cout << " 9. Remove Device using UUID"<<std::endl;
91 std::cout << " 10. Get Linked Devices"<<std::endl;
92 std::cout << " 11. Get Device Status"<<std::endl;
93 std::cout << " 12. Provision Direct-Pairing Configuration"<<std::endl;
94 #if defined(__WITH_X509__) || defined(__WITH_TLS__)
95 std::cout << " 13. Save the Trust Cert. Chain into Cred of SVR"<<std::endl;
96 std::cout << " 14. Provision the Trust Cert. Chain"<<std::endl;
97 #endif // __WITH_X509__ || __WITH_TLS__
98 std::cout << " 99. Exit loop"<<std::endl;
101 void moveTransferredDevice()
103 pOwnedDevList.push_back(pUnownedDevList[transferDevIdx]);
104 pUnownedDevList.erase(pUnownedDevList.begin() + transferDevIdx);
107 void InputPinCB(char* pinBuf, size_t bufSize)
111 std::cout <<"INPUT PIN : ";
114 OICStrcpy(pinBuf, bufSize, ptr.c_str());
119 void printUuid(OicUuid_t uuid)
121 for (int i = 0; i < UUID_LENGTH; i++)
123 std::cout <<std::hex << uuid.id[i] << " ";
125 std::cout<<std::endl;
128 void ownershipTransferCB(PMResultList_t *result, int hasError)
132 std::cout << "Error!!! in OwnershipTransfer"<<std::endl;
136 std::cout<< "\nTransferred Ownership successfuly for device : ";
137 printUuid(result->at(0).deviceId);
140 moveTransferredDevice();
145 void printStatus(int status)
147 static std::map<int, std::string> devStatus = {{1<<0, DEV_STATUS_ON}, {1<<1, DEV_STATUS_OFF}};
149 std::cout <<devStatus[status] <<std::endl;
152 void printDevices(DeviceList_t &list)
154 for (unsigned int i = 0; i < list.size(); i++ )
156 std::cout << "Device "<< i+1 <<" ID: ";
157 std::cout << list[i]->getDeviceID() << " From IP: ";
158 std::cout << list[i]->getDevAddr() << std::endl;
163 * Callback function for provisioning ACL, Credentials.
165 * @param[in] result Result list
166 * @param[in] hasError indicates if the result has error
168 void provisionCB(PMResultList_t *result, int hasError)
172 std::cout << "Error in provisioning operation!"<<std::endl;
176 std::cout<< "\nReceived provisioning results: ";
177 for (unsigned int i = 0; i < result->size(); i++)
179 std::cout << "Result is = " << result->at(i).res <<" for device ";
180 printUuid(result->at(i).deviceId);
190 * Ask user with which devices it wants to make further actions.
191 * All possible error checks included.
192 * Default behavior in case if only one options leaves are included too.
193 * Expect that user count devices from 1, f.e. 1st, 2nd, 3rd, etc
194 * Use DeviceList_t instead of devicesCount because of print devices info
196 * @param[in] list owned devices list.
197 * @param[out] out device number array.
198 * @param[in] count how many device numbers need to read.
199 * @return 0 in case of success and other value otherwise.
201 int readDeviceNumber(DeviceList_t &list, int count, int *out)
203 if (out == NULL || count <= 0)
205 std::cout << "Error! Please put valid input parameters" << std::endl;
209 int devicesCount = list.size();
211 //Print current list of owned devices
212 std::cout <<"Owned devices, count = " << devicesCount << std::endl;
215 if (devicesCount < count)
217 std::cout << "You can't proceed with selected action because Owned devices count ( ";
218 std::cout << devicesCount << " ) are less then required devices ( " << count << " ).";
219 std::cout << "You may need to discover devices again" << std::endl;
223 std::cout << "Select " << count << " device(s) for provisioning" << std::endl;
225 for (int curr = 0; curr < count; curr++)
227 //select last device by default if only 1 option exist
228 //from user's point of view device counting starts from 1,
229 //so 1st, 2nd, 3rd, etc devices
230 if ((curr == count - 1) && (devicesCount == count))
233 for (int i = 0; i < curr; i++)
238 out[curr] = (count*(count+1))/2 - sum;
240 std::cout << "Device " << curr + 1 << " : " << out[curr];
241 std::cout << " - selected automatically (because no other options exist)" << std::endl;
246 std::cout << "Device " << curr + 1 << " : ";
249 if (choice < 1 || choice > devicesCount)
251 std::cout << "Error! You should enter valid device number!" << std::endl;
255 //check that user doesn't select the same device twice
256 for (int i = 0; i < curr; i++)
258 if (out[i] == choice)
260 std::cout << "Error! You cannot select the same device twice!" << std::endl;
268 //Users count devices from 1, so 1st, 2nd, 3rd, etc device
269 //But deviceList array start index is 0, so need to decrease all numbers to 1
270 for (int i = 0; i < count; i++) out[i] -= 1;
276 * Perform cleanup for ACL
279 static void deleteACL(OicSecAcl_t *acl)
286 * Calculate ACL permission from string to bit
288 * @param[in] temp_psm Input data of ACL permission string
289 * @param[in,out] pms The pointer of ACL permission value
290 * @return 0 on success otherwise -1.
292 static int CalculateAclPermission(const char *temp_pms, uint16_t *pms)
296 if (NULL == temp_pms || NULL == pms)
301 while (temp_pms[i] != '\0')
350 * Get the ACL property from user
352 * @param[in] ACL Datastructure to save user inputs
353 * @return 0 on success otherwise -1.
355 static int InputACL(OicSecAcl_t *acl)
358 char *temp_id, *temp_rsc, *temp_pms;
360 printf("******************************************************************************\n");
361 printf("-Set ACL policy for target device\n");
362 printf("******************************************************************************\n");
364 printf("-URN identifying the subject\n");
365 printf("ex) 1111-1111-1111-1111 (16 Numbers except to '-')\n");
366 printf("Subject : ");
367 ret = scanf("%19ms", &temp_id);
369 OicSecAce_t* ace = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
372 OIC_LOG(ERROR, TAG, "Error while memory allocation");
375 LL_APPEND(acl->aces, ace);
379 for (int i = 0, j = 0; temp_id[i] != '\0'; i++)
381 if (DASH != temp_id[i])
382 ace->subjectuuid.id[j++] = temp_id[i];
389 printf("Error while input\n");
394 size_t resourcesLen = 0;
395 printf("Num. of Resource : ");
396 ret = scanf("%zu", &resourcesLen);
397 if ((1 != ret) || (resourcesLen <= 0 || resourcesLen > 50))
400 printf("Error while input\n");
403 printf("-URI of resource\n");
404 printf("ex)/oic/sh/temp/0 (Max_URI_Length: %d Byte )\n", MAX_URI_LENGTH);
405 for(size_t i = 0; i < resourcesLen; i++)
407 OicSecRsrc_t* rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
411 OIC_LOG(ERROR, TAG, "Error while memory allocation");
415 LL_APPEND(ace->resources, rsrc);
417 printf("[%zu]Resource : ", i + 1);
418 ret = scanf("%64ms", &temp_rsc);
421 printf("Error while input\n");
425 rsrc->href = OICStrdup(temp_rsc);
428 char* rsrc_in = NULL;
432 printf(" Enter Number of resource type for [%s]: ", rsrc->href);
433 for(int ret=0; 1!=ret; )
435 ret = scanf("%d", &arrLen);
436 for( ; 0x20<=getchar(); ); // for removing overflow garbages
437 // '0x20<=code' is character region
439 if(ACL_RESRC_ARRAY_SIZE >= arrLen)
443 printf(" Entered Wrong Number. Please Enter under %d Again\n", ACL_RESRC_ARRAY_SIZE);
446 rsrc->typeLen = arrLen;
447 rsrc->types = (char**)OICCalloc(arrLen, sizeof(char*));
450 OIC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
454 for(int i = 0; i < arrLen; i++)
456 printf(" Enter ResourceType[%d] Name (e.g. core.led): ", i+1);
457 for(int ret=0; 1!=ret; )
459 ret = scanf("%64ms", &rsrc_in); // '128' is ACL_RESRC_MAX_LEN
460 for( ; 0x20<=getchar(); ); // for removing overflow garbages
461 // '0x20<=code' is character region
463 rsrc->types[i] = OICStrdup(rsrc_in);
467 OIC_LOG(ERROR, TAG, "createAcl: OICStrdup error return");
474 printf(" Enter Number of interface name for [%s]: ", rsrc->href);
475 for(int ret=0; 1!=ret; )
477 ret = scanf("%d", &arrLen);
478 for( ; 0x20<=getchar(); ); // for removing overflow garbages
479 // '0x20<=code' is character region
481 if(ACL_RESRC_ARRAY_SIZE >= arrLen)
485 printf(" Entered Wrong Number. Please Enter under %d Again\n", ACL_RESRC_ARRAY_SIZE);
488 rsrc->interfaceLen = arrLen;
489 rsrc->interfaces = (char**)OICCalloc(arrLen, sizeof(char*));
490 if(!rsrc->interfaces)
492 OIC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
496 for(int i = 0; i < arrLen; i++)
498 printf(" Enter interfnace[%d] Name (e.g. oic.if.baseline): ", i+1);
499 for(int ret=0; 1!=ret; )
501 ret = scanf("%64ms", &rsrc_in); // '128' is ACL_RESRC_MAX_LEN
502 for( ; 0x20<=getchar(); ); // for removing overflow garbages
503 // '0x20<=code' is character region
505 rsrc->interfaces[i] = OICStrdup(rsrc_in);
507 if(!rsrc->interfaces[i])
509 OIC_LOG(ERROR, TAG, "createAcl: OICStrdup error return");
519 printf("-Set the permission(C,R,U,D,N)\n");
520 printf("ex) CRUDN, CRU_N,..(5 Charaters)\n");
521 printf("Permission : ");
522 ret = scanf("%5ms", &temp_pms);
525 printf("Error while input\n");
528 ret = CalculateAclPermission(temp_pms, &(ace->permission));
533 printf("-URN identifying the rowner\n");
534 printf("ex) 1111-1111-1111-1111 (16 Numbers except to '-')\n");
537 ret = scanf("%19ms", &temp_id);
540 printf("Error while input\n");
544 for (int k = 0, j = 0; temp_id[k] != '\0'; k++)
546 if (DASH != temp_id[k])
548 acl->rownerID.id[j++] = temp_id[k];
560 static int InputCredentials(Credential &cred)
566 std::cout << "Select credential type from following values:" << std::endl;
567 std::cout << "1: symmetric pair-wise key" << std::endl;
568 std::cout << "2: symmetric group key" << std::endl;
569 std::cout << "4: asymmetric key" << std::endl;
570 std::cout << "8: signed asymmetric key (aka certificate)" << std::endl;
571 std::cout << "16: PIN /password" << std::endl;
572 std::cout << "Your choice: ";
578 cred.setCredentialType(static_cast<OicSecCredType_t>(choice));
579 choice = 0; //validation of the accepted choice.
585 std::cout << "selected type is not supported yet" << std::endl;
588 std::cout << "Error! Please select valid credential type" << std::endl;
591 } while(0 != choice);
593 std::cout << "Please enter key size (valid size is 128 or 256) :";
598 cred.setCredentialKeySize(OWNER_PSK_LENGTH_128);
600 else if(256 == choice)
602 cred.setCredentialKeySize(OWNER_PSK_LENGTH_256);
606 std::cout << "Error! Please enter valid key size!" << std::endl;
613 static void deletePconf()
615 OICFree(g_pconf.prm);
617 OicSecPdAcl_t* acl = g_pconf.pdacls;
620 /* Clean Resources */
621 for (unsigned int i = 0; i < (acl)->resourcesLen; i++)
623 OICFree((acl)->resources[i]);
625 OICFree((acl)->resources);
627 /* Clean ACL node itself */
628 /* Required only if acl was created in heap */
631 memset(&g_pconf, 0, sizeof(OicSecPconf_t));
634 static OicSecPdAcl_t* InputPdACL()
637 char *temp_rsc, *temp_pms;
639 printf("******************************************************************************\n");
640 printf("-Set ACL policy for target DP device\n");
641 printf("******************************************************************************\n");
643 OicSecPdAcl_t *acl = (OicSecPdAcl_t *)OICCalloc(1,sizeof(OicSecPdAcl_t));
646 OIC_LOG(ERROR, TAG, "Error while memory allocation");
651 printf("Num. of Resource : ");
652 ret = scanf("%zu", &acl->resourcesLen);
653 if ((1 != ret) || (acl->resourcesLen <= 0 || acl->resourcesLen > 50))
655 printf("Error while input\n");
659 printf("-URI of resource\n");
660 printf("ex)/oic/sh/temp/0 (Max_URI_Length: %d Byte )\n", MAX_URI_LENGTH);
661 acl->resources = (char **)OICCalloc(acl->resourcesLen, sizeof(char *));
662 if (NULL == acl->resources)
664 OIC_LOG(ERROR, TAG, "Error while memory allocation");
668 for (size_t i = 0; i < acl->resourcesLen; i++)
670 printf("[%zu]Resource : ", i + 1);
671 ret = scanf("%64ms", &temp_rsc);
674 printf("Error while input\n");
675 OICFree(acl->resources);
680 acl->resources[i] = OICStrdup(temp_rsc);
682 if (NULL == acl->resources[i])
684 OIC_LOG(ERROR, TAG, "Error while memory allocation");
685 OICFree(acl->resources);
694 printf("-Set the permission(C,R,U,D,N)\n");
695 printf("ex) CRUDN, CRU_N,..(5 Charaters)\n");
696 printf("Permission : ");
697 ret = scanf("%5ms", &temp_pms);
700 printf("Error while input\n");
701 OICFree(acl->resources);
705 ret = CalculateAclPermission(temp_pms, &(acl->permission));
712 void provisionDirectPairingCB(PMResultList_t *result, int hasError)
716 std::cout << "Error in provisioning operation!"<<std::endl;
720 std::cout<< "\nReceived provisioning results: Direct Pairing is successful ";
721 for (unsigned int i = 0; i < result->size(); i++)
723 std::cout << "Result is = " << result->at(i).res <<" for device ";
724 printUuid(result->at(i).deviceId);
734 static void provisionDP(int dev_num)
742 // set default supported PRM types
743 g_pconf.prmLen = sizeof(SUPPORTED_PRMS)/sizeof(OicSecPrm_t);
744 g_pconf.prm = (OicSecPrm_t *)OICCalloc(g_pconf.prmLen, sizeof(OicSecPrm_t));
747 for (size_t i=0; i < g_pconf.prmLen; i++)
749 g_pconf.prm[i] = SUPPORTED_PRMS[i];
754 OIC_LOG(ERROR, TAG, "create prm error return");
758 std::cout << "Enter PIN to be configured: ";
762 if (pin.length() == DP_PIN_LENGTH)
768 std::cout << "PIN length should be 8, Enter again: ";
772 memcpy(g_pconf.pin.val, pin.c_str(), DP_PIN_LENGTH);
776 g_pconf.pdacls = InputPdACL();
779 OIC_LOG(ERROR, TAG, "InputPdACL error return");
783 // call |OCProvisionDirectPairing| API actually
784 // calling this API with callback actually acts like blocking
785 // for error checking, the return value saved and printed
786 rst = pOwnedDevList[dev_num-1]->provisionDirectPairing(&g_pconf, provisionDirectPairingCB);
787 if(OC_STACK_OK != rst)
789 OIC_LOG_V(ERROR, TAG, "OCProvisionDirectPairing API error: %d", rst);
790 if (OC_STACK_UNAUTHORIZED_REQ == rst)
792 OIC_LOG(ERROR, TAG, "Target Server NOT Support Direct-Pairing !!! (DPC == false)");
799 deletePconf(); // after here |acl| points nothing
804 static int saveTrustCert(void)
807 // call |OCSaveTrustCertChainBin| API actually
808 printf(" Save Trust Cert. Chain into Cred of SVR.\n");
810 ByteArray trustCertChainArray = {0, 0};
812 FILE *fp = fopen("rootca.crt", "rb+");
817 if (fseeko(fp, 0, SEEK_END) == 0 && (fsize = ftello(fp)) >= 0)
819 trustCertChainArray.data = (uint8_t*)OICMalloc(fsize);
820 trustCertChainArray.len = fsize;
821 if (NULL == trustCertChainArray.data)
823 OIC_LOG(ERROR,TAG,"malloc");
828 fsize = fread(trustCertChainArray.data, 1, fsize, fp);
831 OIC_LOG(ERROR,TAG,"Read error");
836 OIC_LOG_BUFFER(DEBUG, TAG, trustCertChainArray.data, trustCertChainArray.len);
838 if(OC_STACK_OK != OCSecure::saveTrustCertChain(trustCertChainArray.data, trustCertChainArray.len,
839 OIC_ENCODING_PEM,&g_credId))
841 OIC_LOG(ERROR, TAG, "OCSaveTrustCertChainBin API error");
844 printf("CredId of Saved Trust Cert. Chain into Cred of SVR : %d.\n", g_credId);
848 #endif //__WITH_TLS__
852 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
854 // Create PlatformConfig object
856 OC::ServiceType::InProc,
860 OC::QualityOfService::LowQos,
864 OCPlatform::Configure(cfg);
869 OicSecAcl_t *acl1 = nullptr, *acl2 = nullptr;
870 if (OCSecure::provisionInit("") != OC_STACK_OK)
872 std::cout <<"PM Init failed"<< std::endl;
876 for (int out = 0; !out;)
900 //Secure resource discovery.
902 pUnownedDevList.clear();
903 std::cout << "Started discovery..." <<std::endl;
904 OCStackResult result = OCSecure::discoverUnownedDevices(DISCOVERY_TIMEOUT,
906 if (result != OC_STACK_OK)
908 std::cout<< "!!Error - UnOwned Discovery failed."<<std::endl;
910 else if (pUnownedDevList.size())
912 std::cout <<"Found secure devices, count = " <<
913 pUnownedDevList.size() << std::endl;
914 printDevices(pUnownedDevList);
918 std::cout <<"No Secure devices found"<<std::endl;
924 pOwnedDevList.clear();
925 std::cout << "Started discovery..." <<std::endl;
926 OCStackResult result = OCSecure::discoverOwnedDevices(DISCOVERY_TIMEOUT,
928 if (result != OC_STACK_OK)
930 std::cout<< "!!Error - Owned Discovery failed."<<std::endl;
932 else if (pOwnedDevList.size())
934 std::cout <<"Found owned devices, count = " <<
935 pOwnedDevList.size() << std::endl;
936 printDevices(pOwnedDevList);
940 std::cout <<"No Secure devices found"<<std::endl;
948 if (!pUnownedDevList.size())
950 std::cout <<"There are no more Unowned devices"<<std::endl;
954 for (unsigned int i = 0; i < pUnownedDevList.size(); i++ )
956 std::cout << i+1 << ": "<< pUnownedDevList[i]->getDeviceID();
957 std::cout << " From IP:" << pUnownedDevList[i]->getDevAddr() <<std::endl;
960 std::cout <<"Select device number: "<<std::endl;
962 if (devNum > pUnownedDevList.size())
964 std::cout <<"Invalid device number"<<std::endl;
967 transferDevIdx = devNum - 1;
969 //register callbacks for JUST WORKS and PIN methods
970 std::cout <<"Registering OTM Methods: 1. JUST WORKS and 2. PIN"<<std::endl;
973 OTMCallbackData_t justWorksCBData;
974 justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback;
975 justWorksCBData.createSecureSessionCB =
976 CreateSecureSessionJustWorksCallback;
977 justWorksCBData.createSelectOxmPayloadCB =
978 CreateJustWorksSelectOxmPayload;
979 justWorksCBData.createOwnerTransferPayloadCB =
980 CreateJustWorksOwnerTransferPayload;
981 OCSecure::setOwnerTransferCallbackData(OIC_JUST_WORKS,
982 &justWorksCBData, NULL);
986 OTMCallbackData_t pinBasedCBData;
987 pinBasedCBData.loadSecretCB = InputPinCodeCallback;
988 pinBasedCBData.createSecureSessionCB =
989 CreateSecureSessionRandomPinCallback;
990 pinBasedCBData.createSelectOxmPayloadCB =
991 CreatePinBasedSelectOxmPayload;
992 pinBasedCBData.createOwnerTransferPayloadCB =
993 CreatePinBasedOwnerTransferPayload;
994 OCSecure::setOwnerTransferCallbackData(OIC_RANDOM_DEVICE_PIN,
995 &pinBasedCBData, InputPinCB);
999 std::cout << "Transfering ownership for : "<<
1000 pUnownedDevList[devNum-1]->getDeviceID()<<std::endl;
1001 if (pUnownedDevList[devNum-1]->doOwnershipTransfer(ownershipTransferCB)
1004 std::cout<<"OwnershipTransferCallback is failed"<<std::endl;
1009 case 4: //Provision ACL
1013 if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
1015 std::cout << "Provision ACL for : "<<
1016 pOwnedDevList[index]->getDeviceID()<< std::endl;
1018 acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
1021 OIC_LOG(ERROR, TAG, "Error while memory allocation");
1025 std::cout << "Please input ACL for selected device: " << std::endl;
1026 if (0 != InputACL(acl1))
1033 if (pOwnedDevList[index]->provisionACL(acl1, provisionCB) != OC_STACK_OK)
1036 std::cout <<"provisionACL is failed"<< std::endl;
1040 case 5: //Provision Credentials
1044 if (0 != readDeviceNumber(pOwnedDevList, 2, devices)) break;
1046 int first = devices[0];
1047 int second = devices[1];
1049 std::cout << "Provision Credentials to devices: "<<
1050 pOwnedDevList[first]->getDeviceID();
1051 std::cout << " and "<< pOwnedDevList[second]->getDeviceID() << std::endl;
1053 Credential cred( NO_SECURITY_MODE ,0);
1054 std::cout << "Please input credentials for selected devices: " << std::endl;
1055 if (0 != InputCredentials(cred))
1060 if (pOwnedDevList[first]->provisionCredentials(cred,
1061 *pOwnedDevList[second].get(), provisionCB) != OC_STACK_OK)
1064 std::cout <<"provisionCredentials is failed"<< std::endl;
1068 case 6: //Provision ACL & Creds b/w two devices.
1072 if (0 != readDeviceNumber(pOwnedDevList, 2, devices)) break;
1074 int first = devices[0];
1075 int second = devices[1];
1077 std::cout << "Provision pairwise devices: "<<
1078 pOwnedDevList[first]->getDeviceID();
1079 std::cout << " and "<< pOwnedDevList[second]->getDeviceID() << std::endl;
1081 Credential cred( NO_SECURITY_MODE, 0);
1082 std::cout << "Please input credentials for selected devices: " << std::endl;
1083 if (0 != InputCredentials(cred))
1086 acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
1089 OIC_LOG(ERROR, TAG, "Error while memory allocation");
1093 std::cout << "Please input ACL for selected device: " << std::endl;
1094 if (0 != InputACL(acl1))
1099 acl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
1102 OIC_LOG(ERROR, TAG, "Error while memory allocation");
1106 std::cout << "Please input ACL for selected device: " << std::endl;
1107 if (0 != InputACL(acl2))
1114 if (pOwnedDevList[first]->provisionPairwiseDevices(cred, acl1,
1115 *pOwnedDevList[second].get(), acl2, provisionCB) != OC_STACK_OK)
1118 std::cout <<"provisionPairwiseDevices is failed"<< std::endl;
1122 case 7: //Unlink Devices
1126 if (0 != readDeviceNumber(pOwnedDevList, 2, devices)) break;
1128 int first = devices[0];
1129 int second = devices[1];
1131 std::cout << "Unlink devices: "<< pOwnedDevList[first]->getDeviceID();
1132 std::cout << " and "<< pOwnedDevList[second]->getDeviceID() << std::endl;
1136 if (pOwnedDevList[first]->unlinkDevices(*pOwnedDevList[second].get(),
1137 provisionCB) != OC_STACK_OK)
1140 std::cout <<"unlinkDevice is failed"<< std::endl;
1144 case 8: //Remove Device
1148 if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
1150 std::cout << "Remove Device: "<< pOwnedDevList[index]->getDeviceID()<< std::endl;
1154 if (pOwnedDevList[index]->removeDevice(DISCOVERY_TIMEOUT, provisionCB)
1158 std::cout <<"removeDevice is failed"<< std::endl;
1162 case 9: //Remove Device using UUID
1166 if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
1168 std::cout << "Remove Device: "<< pOwnedDevList[index]->getDeviceID()<< std::endl;
1172 if (OCSecure::removeDeviceWithUuid(DISCOVERY_TIMEOUT,
1173 pOwnedDevList[index]->getDeviceID(),
1178 std::cout <<"removeDevice is failed"<< std::endl;
1183 case 10: //Get Linked devices
1185 UuidList_t linkedUuid;
1186 unsigned int devNum;
1188 if (!pOwnedDevList.size())
1190 std::cout <<"There are no Owned devices yet,"
1191 " may need to discover"<<std::endl;
1195 for (unsigned int i = 0; i < pOwnedDevList.size(); i++ )
1197 std::cout << i+1 << ": "<< pOwnedDevList[i]->getDeviceID() <<" From IP:";
1198 std::cout << pOwnedDevList[i]->getDevAddr() <<std::endl;
1201 std::cout <<"Select device number: "<<std::endl;
1203 if (devNum > pOwnedDevList.size())
1205 std::cout <<"Invalid device number"<<std::endl;
1209 if(pOwnedDevList[devNum -1]->getLinkedDevices(linkedUuid) == OC_STACK_OK)
1211 if (!linkedUuid.size())
1213 std::cout <<"No devices are linked to "<<
1214 pOwnedDevList[devNum -1]->getDeviceID() << std::endl;
1216 //display the Linked devices (UUIDs)
1217 for(unsigned int i = 0; i < linkedUuid.size(); i++)
1219 printUuid(linkedUuid[i]);
1224 std::cout <<"Error! in getLinkedDevices"<<std::endl;
1228 case 11: //Get device' status
1230 DeviceList_t unownedList, ownedList;
1232 if (OCSecure::getDevInfoFromNetwork(DISCOVERY_TIMEOUT, ownedList,
1233 unownedList) == OC_STACK_OK)
1235 std::cout <<"Owned Device' status for" <<std::endl;
1236 for (unsigned int i = 0; i < ownedList.size(); i++ )
1238 std::cout << "Device "<<i+1 <<" ID: '";
1239 std::cout << ownedList[i]->getDeviceID() << "' From IP: ";
1240 std::cout << ownedList[i]->getDevAddr() << " Status: ";
1241 printStatus(ownedList[i]->getDeviceStatus());
1243 std::cout <<"\nUnOwned Device' status for" <<std::endl;
1244 for (unsigned int i = 0; i < unownedList.size(); i++ )
1246 std::cout << "Device "<<i+1 <<" ID: '";
1247 std::cout << unownedList[i]->getDeviceID() << "' From IP: ";
1248 std::cout << unownedList[i]->getDevAddr() << " Status: ";
1249 printStatus(unownedList[i]->getDeviceStatus());
1258 unsigned int devNum;
1260 if (!pOwnedDevList.size())
1262 std::cout <<"There are no Owned devices yet,"
1263 " may need to discover"<<std::endl;
1267 for (unsigned int i = 0; i < pOwnedDevList.size(); i++ )
1269 std::cout << i+1 << ": "<< pOwnedDevList[i]->getDeviceID() <<" From IP:";
1270 std::cout << pOwnedDevList[i]->getDevAddr() <<std::endl;
1273 std::cout <<"Select device number: "<<std::endl;
1275 if (devNum > pOwnedDevList.size())
1277 std::cout <<"Invalid device number"<<std::endl;
1282 provisionDP(devNum);
1286 #if defined(__WITH_X509__) || defined(__WITH_TLS__)
1291 std::cout<<"Error in saving cert"<<std::endl;
1299 if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
1301 std::cout << "Provision cert for : "<<
1302 pOwnedDevList[index]->getDeviceID()<< std::endl;
1306 if (pOwnedDevList[index]->provisionTrustCertChain(SIGNED_ASYMMETRIC_KEY,
1307 g_credId,provisionCB ) != OC_STACK_OK)
1310 std::cout <<"provision cert is failed"<< std::endl;
1314 #endif //__WITH_X509__ || __WITH_TLS__
1322 catch(OCException& e)
1324 oclog() << "Exception in main: "<<e.what();