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_DTLS__) || 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 std::cout << " 15. Read trust cert chain"<<std::endl;
98 #endif // __WITH_DTLS__ || __WITH_TLS__
99 std::cout << " 99. Exit loop"<<std::endl;
102 void moveTransferredDevice()
104 pOwnedDevList.push_back(pUnownedDevList[transferDevIdx]);
105 pUnownedDevList.erase(pUnownedDevList.begin() + transferDevIdx);
108 void InputPinCB(char* pinBuf, size_t bufSize)
112 std::cout <<"INPUT PIN : ";
115 OICStrcpy(pinBuf, bufSize, ptr.c_str());
120 void printUuid(OicUuid_t uuid)
122 for (int i = 0; i < UUID_LENGTH; i++)
124 std::cout <<std::hex << uuid.id[i] << " ";
126 std::cout<<std::endl;
129 void ownershipTransferCB(PMResultList_t *result, int hasError)
133 std::cout << "Error!!! in OwnershipTransfer"<<std::endl;
137 std::cout<< "\nTransferred Ownership successfuly for device : ";
138 printUuid(result->at(0).deviceId);
141 moveTransferredDevice();
146 void printStatus(int status)
148 static std::map<int, std::string> devStatus = {{1<<0, DEV_STATUS_ON}, {1<<1, DEV_STATUS_OFF}};
150 std::cout <<devStatus[status] <<std::endl;
153 void printDevices(DeviceList_t &list)
155 for (unsigned int i = 0; i < list.size(); i++ )
157 std::cout << "Device "<< i+1 <<" ID: ";
158 std::cout << list[i]->getDeviceID() << " From IP: ";
159 std::cout << list[i]->getDevAddr() << std::endl;
164 * Callback function for provisioning ACL, Credentials.
166 * @param[in] result Result list
167 * @param[in] hasError indicates if the result has error
169 void provisionCB(PMResultList_t *result, int hasError)
173 std::cout << "Error in provisioning operation!"<<std::endl;
177 std::cout<< "\nReceived provisioning results: ";
178 for (unsigned int i = 0; i < result->size(); i++)
180 std::cout << "Result is = " << result->at(i).res <<" for device ";
181 printUuid(result->at(i).deviceId);
191 * Ask user with which devices it wants to make further actions.
192 * All possible error checks included.
193 * Default behavior in case if only one options leaves are included too.
194 * Expect that user count devices from 1, f.e. 1st, 2nd, 3rd, etc
195 * Use DeviceList_t instead of devicesCount because of print devices info
197 * @param[in] list owned devices list.
198 * @param[out] out device number array.
199 * @param[in] count how many device numbers need to read.
200 * @return 0 in case of success and other value otherwise.
202 int readDeviceNumber(DeviceList_t &list, int count, int *out)
204 if (out == NULL || count <= 0)
206 std::cout << "Error! Please put valid input parameters" << std::endl;
210 int devicesCount = list.size();
212 //Print current list of owned devices
213 std::cout <<"Owned devices, count = " << devicesCount << std::endl;
216 if (devicesCount < count)
218 std::cout << "You can't proceed with selected action because Owned devices count ( ";
219 std::cout << devicesCount << " ) are less then required devices ( " << count << " ).";
220 std::cout << "You may need to discover devices again" << std::endl;
224 std::cout << "Select " << count << " device(s) for provisioning" << std::endl;
226 for (int curr = 0; curr < count; curr++)
228 //select last device by default if only 1 option exist
229 //from user's point of view device counting starts from 1,
230 //so 1st, 2nd, 3rd, etc devices
231 if ((curr == count - 1) && (devicesCount == count))
234 for (int i = 0; i < curr; i++)
239 out[curr] = (count*(count+1))/2 - sum;
241 std::cout << "Device " << curr + 1 << " : " << out[curr];
242 std::cout << " - selected automatically (because no other options exist)" << std::endl;
247 std::cout << "Device " << curr + 1 << " : ";
250 if (choice < 1 || choice > devicesCount)
252 std::cout << "Error! You should enter valid device number!" << std::endl;
256 //check that user doesn't select the same device twice
257 for (int i = 0; i < curr; i++)
259 if (out[i] == choice)
261 std::cout << "Error! You cannot select the same device twice!" << std::endl;
269 //Users count devices from 1, so 1st, 2nd, 3rd, etc device
270 //But deviceList array start index is 0, so need to decrease all numbers to 1
271 for (int i = 0; i < count; i++) out[i] -= 1;
277 * Perform cleanup for ACL
280 static void deleteACL(OicSecAcl_t *acl)
287 * Calculate ACL permission from string to bit
289 * @param[in] temp_psm Input data of ACL permission string
290 * @param[in,out] pms The pointer of ACL permission value
291 * @return 0 on success otherwise -1.
293 static int CalculateAclPermission(const char *temp_pms, uint16_t *pms)
297 if (NULL == temp_pms || NULL == pms)
302 while (temp_pms[i] != '\0')
351 * Get the ACL property from user
353 * @param[in] ACL Datastructure to save user inputs
354 * @return 0 on success otherwise -1.
356 static int InputACL(OicSecAcl_t *acl)
359 char *temp_id, *temp_rsc, *temp_pms;
361 printf("******************************************************************************\n");
362 printf("-Set ACL policy for target device\n");
363 printf("******************************************************************************\n");
365 printf("-URN identifying the subject\n");
366 printf("ex) 1111-1111-1111-1111 (16 Numbers except to '-')\n");
367 printf("Subject : ");
368 ret = scanf("%19ms", &temp_id);
370 OicSecAce_t* ace = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
373 OIC_LOG(ERROR, TAG, "Error while memory allocation");
376 LL_APPEND(acl->aces, ace);
380 for (int i = 0, j = 0; temp_id[i] != '\0'; i++)
382 if (DASH != temp_id[i])
383 ace->subjectuuid.id[j++] = temp_id[i];
390 printf("Error while input\n");
395 size_t resourcesLen = 0;
396 printf("Num. of Resource : ");
397 ret = scanf("%zu", &resourcesLen);
398 if ((1 != ret) || (resourcesLen <= 0 || resourcesLen > 50))
401 printf("Error while input\n");
404 printf("-URI of resource\n");
405 printf("ex)/oic/sh/temp/0 (Max_URI_Length: %d Byte )\n", MAX_URI_LENGTH);
406 for(size_t i = 0; i < resourcesLen; i++)
408 OicSecRsrc_t* rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
412 OIC_LOG(ERROR, TAG, "Error while memory allocation");
416 LL_APPEND(ace->resources, rsrc);
418 printf("[%zu]Resource : ", i + 1);
419 ret = scanf("%64ms", &temp_rsc);
422 printf("Error while input\n");
426 rsrc->href = OICStrdup(temp_rsc);
429 char* rsrc_in = NULL;
433 printf(" Enter Number of resource type for [%s]: ", rsrc->href);
434 for(int ret=0; 1!=ret; )
436 ret = scanf("%d", &arrLen);
437 for( ; 0x20<=getchar(); ); // for removing overflow garbages
438 // '0x20<=code' is character region
440 if(ACL_RESRC_ARRAY_SIZE >= arrLen)
444 printf(" Entered Wrong Number. Please Enter under %d Again\n", ACL_RESRC_ARRAY_SIZE);
447 rsrc->typeLen = arrLen;
448 rsrc->types = (char**)OICCalloc(arrLen, sizeof(char*));
451 OIC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
455 for(int i = 0; i < arrLen; i++)
457 printf(" Enter ResourceType[%d] Name (e.g. core.led): ", i+1);
458 for(int ret=0; 1!=ret; )
460 ret = scanf("%64ms", &rsrc_in); // '128' is ACL_RESRC_MAX_LEN
461 for( ; 0x20<=getchar(); ); // for removing overflow garbages
462 // '0x20<=code' is character region
464 rsrc->types[i] = OICStrdup(rsrc_in);
468 OIC_LOG(ERROR, TAG, "createAcl: OICStrdup error return");
475 printf(" Enter Number of interface name for [%s]: ", rsrc->href);
476 for(int ret=0; 1!=ret; )
478 ret = scanf("%d", &arrLen);
479 for( ; 0x20<=getchar(); ); // for removing overflow garbages
480 // '0x20<=code' is character region
482 if(ACL_RESRC_ARRAY_SIZE >= arrLen)
486 printf(" Entered Wrong Number. Please Enter under %d Again\n", ACL_RESRC_ARRAY_SIZE);
489 rsrc->interfaceLen = arrLen;
490 rsrc->interfaces = (char**)OICCalloc(arrLen, sizeof(char*));
491 if(!rsrc->interfaces)
493 OIC_LOG(ERROR, TAG, "createAcl: OICCalloc error return");
497 for(int i = 0; i < arrLen; i++)
499 printf(" Enter interfnace[%d] Name (e.g. oic.if.baseline): ", i+1);
500 for(int ret=0; 1!=ret; )
502 ret = scanf("%64ms", &rsrc_in); // '128' is ACL_RESRC_MAX_LEN
503 for( ; 0x20<=getchar(); ); // for removing overflow garbages
504 // '0x20<=code' is character region
506 rsrc->interfaces[i] = OICStrdup(rsrc_in);
508 if(!rsrc->interfaces[i])
510 OIC_LOG(ERROR, TAG, "createAcl: OICStrdup error return");
520 printf("-Set the permission(C,R,U,D,N)\n");
521 printf("ex) CRUDN, CRU_N,..(5 Charaters)\n");
522 printf("Permission : ");
523 ret = scanf("%5ms", &temp_pms);
526 printf("Error while input\n");
529 ret = CalculateAclPermission(temp_pms, &(ace->permission));
534 printf("-URN identifying the rowner\n");
535 printf("ex) 1111-1111-1111-1111 (16 Numbers except to '-')\n");
538 ret = scanf("%19ms", &temp_id);
541 printf("Error while input\n");
545 for (int k = 0, j = 0; temp_id[k] != '\0'; k++)
547 if (DASH != temp_id[k])
549 acl->rownerID.id[j++] = temp_id[k];
561 static int InputCredentials(Credential &cred)
567 std::cout << "Select credential type from following values:" << std::endl;
568 std::cout << "1: symmetric pair-wise key" << std::endl;
569 std::cout << "2: symmetric group key" << std::endl;
570 std::cout << "4: asymmetric key" << std::endl;
571 std::cout << "8: signed asymmetric key (aka certificate)" << std::endl;
572 std::cout << "16: PIN /password" << std::endl;
573 std::cout << "Your choice: ";
579 cred.setCredentialType(static_cast<OicSecCredType_t>(choice));
580 choice = 0; //validation of the accepted choice.
586 std::cout << "selected type is not supported yet" << std::endl;
589 std::cout << "Error! Please select valid credential type" << std::endl;
592 } while(0 != choice);
594 std::cout << "Please enter key size (valid size is 128 or 256) :";
599 cred.setCredentialKeySize(OWNER_PSK_LENGTH_128);
601 else if(256 == choice)
603 cred.setCredentialKeySize(OWNER_PSK_LENGTH_256);
607 std::cout << "Error! Please enter valid key size!" << std::endl;
614 static void deletePconf()
616 OICFree(g_pconf.prm);
618 OicSecPdAcl_t* acl = g_pconf.pdacls;
621 /* Clean Resources */
622 for (unsigned int i = 0; i < (acl)->resourcesLen; i++)
624 OICFree((acl)->resources[i]);
626 OICFree((acl)->resources);
628 /* Clean ACL node itself */
629 /* Required only if acl was created in heap */
632 memset(&g_pconf, 0, sizeof(OicSecPconf_t));
635 static OicSecPdAcl_t* InputPdACL()
638 char *temp_rsc, *temp_pms;
640 printf("******************************************************************************\n");
641 printf("-Set ACL policy for target DP device\n");
642 printf("******************************************************************************\n");
644 OicSecPdAcl_t *acl = (OicSecPdAcl_t *)OICCalloc(1,sizeof(OicSecPdAcl_t));
647 OIC_LOG(ERROR, TAG, "Error while memory allocation");
652 printf("Num. of Resource : ");
653 ret = scanf("%zu", &acl->resourcesLen);
654 if ((1 != ret) || (acl->resourcesLen <= 0 || acl->resourcesLen > 50))
656 printf("Error while input\n");
660 printf("-URI of resource\n");
661 printf("ex)/oic/sh/temp/0 (Max_URI_Length: %d Byte )\n", MAX_URI_LENGTH);
662 acl->resources = (char **)OICCalloc(acl->resourcesLen, sizeof(char *));
663 if (NULL == acl->resources)
665 OIC_LOG(ERROR, TAG, "Error while memory allocation");
669 for (size_t i = 0; i < acl->resourcesLen; i++)
671 printf("[%zu]Resource : ", i + 1);
672 ret = scanf("%64ms", &temp_rsc);
675 printf("Error while input\n");
676 OICFree(acl->resources);
681 acl->resources[i] = OICStrdup(temp_rsc);
683 if (NULL == acl->resources[i])
685 OIC_LOG(ERROR, TAG, "Error while memory allocation");
686 OICFree(acl->resources);
695 printf("-Set the permission(C,R,U,D,N)\n");
696 printf("ex) CRUDN, CRU_N,..(5 Charaters)\n");
697 printf("Permission : ");
698 ret = scanf("%5ms", &temp_pms);
701 printf("Error while input\n");
702 OICFree(acl->resources);
706 ret = CalculateAclPermission(temp_pms, &(acl->permission));
713 void provisionDirectPairingCB(PMResultList_t *result, int hasError)
717 std::cout << "Error in provisioning operation!"<<std::endl;
721 std::cout<< "\nReceived provisioning results: Direct Pairing is successful ";
722 for (unsigned int i = 0; i < result->size(); i++)
724 std::cout << "Result is = " << result->at(i).res <<" for device ";
725 printUuid(result->at(i).deviceId);
735 static void provisionDP(int dev_num)
743 // set default supported PRM types
744 g_pconf.prmLen = sizeof(SUPPORTED_PRMS)/sizeof(OicSecPrm_t);
745 g_pconf.prm = (OicSecPrm_t *)OICCalloc(g_pconf.prmLen, sizeof(OicSecPrm_t));
748 for (size_t i=0; i < g_pconf.prmLen; i++)
750 g_pconf.prm[i] = SUPPORTED_PRMS[i];
755 OIC_LOG(ERROR, TAG, "create prm error return");
759 std::cout << "Enter PIN to be configured: ";
763 if (pin.length() == DP_PIN_LENGTH)
769 std::cout << "PIN length should be 8, Enter again: ";
773 memcpy(g_pconf.pin.val, pin.c_str(), DP_PIN_LENGTH);
777 g_pconf.pdacls = InputPdACL();
780 OIC_LOG(ERROR, TAG, "InputPdACL error return");
784 // call |OCProvisionDirectPairing| API actually
785 // calling this API with callback actually acts like blocking
786 // for error checking, the return value saved and printed
787 rst = pOwnedDevList[dev_num-1]->provisionDirectPairing(&g_pconf, provisionDirectPairingCB);
788 if(OC_STACK_OK != rst)
790 OIC_LOG_V(ERROR, TAG, "OCProvisionDirectPairing API error: %d", rst);
791 if (OC_STACK_UNAUTHORIZED_REQ == rst)
793 OIC_LOG(ERROR, TAG, "Target Server NOT Support Direct-Pairing !!! (DPC == false)");
800 deletePconf(); // after here |acl| points nothing
804 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
805 static int saveTrustCert(void)
808 // call |OCSaveTrustCertChainBin| API actually
809 printf(" Save Trust Cert. Chain into Cred of SVR.\n");
811 ByteArray trustCertChainArray = {0, 0};
813 FILE *fp = fopen("rootca.crt", "rb+");
818 if (fseeko(fp, 0, SEEK_END) == 0 && (fsize = ftello(fp)) >= 0)
820 trustCertChainArray.data = (uint8_t*)OICMalloc(fsize);
821 trustCertChainArray.len = fsize;
822 if (NULL == trustCertChainArray.data)
824 OIC_LOG(ERROR,TAG,"malloc");
829 fsize = fread(trustCertChainArray.data, 1, fsize, fp);
832 OIC_LOG(ERROR,TAG,"Read error");
837 OIC_LOG_BUFFER(DEBUG, TAG, trustCertChainArray.data, trustCertChainArray.len);
839 if(OC_STACK_OK != OCSecure::saveTrustCertChain(trustCertChainArray.data, trustCertChainArray.len,
840 OIC_ENCODING_PEM,&g_credId))
842 OIC_LOG(ERROR, TAG, "OCSaveTrustCertChainBin API error");
845 printf("CredId of Saved Trust Cert. Chain into Cred of SVR : %d.\n", g_credId);
849 #endif // __WITH_DTLS__ or __WITH_TLS__
853 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
855 // Create PlatformConfig object
857 OC::ServiceType::InProc,
861 OC::QualityOfService::LowQos,
865 OCPlatform::Configure(cfg);
870 OicSecAcl_t *acl1 = nullptr, *acl2 = nullptr;
871 if (OCSecure::provisionInit("") != OC_STACK_OK)
873 std::cout <<"PM Init failed"<< std::endl;
877 for (int out = 0; !out;)
901 //Secure resource discovery.
903 pUnownedDevList.clear();
904 std::cout << "Started discovery..." <<std::endl;
905 OCStackResult result = OCSecure::discoverUnownedDevices(DISCOVERY_TIMEOUT,
907 if (result != OC_STACK_OK)
909 std::cout<< "!!Error - UnOwned Discovery failed."<<std::endl;
911 else if (pUnownedDevList.size())
913 std::cout <<"Found secure devices, count = " <<
914 pUnownedDevList.size() << std::endl;
915 printDevices(pUnownedDevList);
919 std::cout <<"No Secure devices found"<<std::endl;
925 pOwnedDevList.clear();
926 std::cout << "Started discovery..." <<std::endl;
927 OCStackResult result = OCSecure::discoverOwnedDevices(DISCOVERY_TIMEOUT,
929 if (result != OC_STACK_OK)
931 std::cout<< "!!Error - Owned Discovery failed."<<std::endl;
933 else if (pOwnedDevList.size())
935 std::cout <<"Found owned devices, count = " <<
936 pOwnedDevList.size() << std::endl;
937 printDevices(pOwnedDevList);
941 std::cout <<"No Secure devices found"<<std::endl;
949 if (!pUnownedDevList.size())
951 std::cout <<"There are no more Unowned devices"<<std::endl;
955 for (unsigned int i = 0; i < pUnownedDevList.size(); i++ )
957 std::cout << i+1 << ": "<< pUnownedDevList[i]->getDeviceID();
958 std::cout << " From IP:" << pUnownedDevList[i]->getDevAddr() <<std::endl;
961 std::cout <<"Select device number: "<<std::endl;
963 if (devNum > pUnownedDevList.size())
965 std::cout <<"Invalid device number"<<std::endl;
968 transferDevIdx = devNum - 1;
970 //register callbacks for JUST WORKS and PIN methods
971 std::cout <<"Registering OTM Methods: 1. JUST WORKS and 2. PIN"<<std::endl;
974 OTMCallbackData_t justWorksCBData;
975 justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback;
976 justWorksCBData.createSecureSessionCB =
977 CreateSecureSessionJustWorksCallback;
978 justWorksCBData.createSelectOxmPayloadCB =
979 CreateJustWorksSelectOxmPayload;
980 justWorksCBData.createOwnerTransferPayloadCB =
981 CreateJustWorksOwnerTransferPayload;
982 OCSecure::setOwnerTransferCallbackData(OIC_JUST_WORKS,
983 &justWorksCBData, NULL);
987 OTMCallbackData_t pinBasedCBData;
988 pinBasedCBData.loadSecretCB = InputPinCodeCallback;
989 pinBasedCBData.createSecureSessionCB =
990 CreateSecureSessionRandomPinCallback;
991 pinBasedCBData.createSelectOxmPayloadCB =
992 CreatePinBasedSelectOxmPayload;
993 pinBasedCBData.createOwnerTransferPayloadCB =
994 CreatePinBasedOwnerTransferPayload;
995 OCSecure::setOwnerTransferCallbackData(OIC_RANDOM_DEVICE_PIN,
996 &pinBasedCBData, InputPinCB);
1000 std::cout << "Transfering ownership for : "<<
1001 pUnownedDevList[devNum-1]->getDeviceID()<<std::endl;
1002 if (pUnownedDevList[devNum-1]->doOwnershipTransfer(ownershipTransferCB)
1005 std::cout<<"OwnershipTransferCallback is failed"<<std::endl;
1010 case 4: //Provision ACL
1014 if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
1016 std::cout << "Provision ACL for : "<<
1017 pOwnedDevList[index]->getDeviceID()<< std::endl;
1019 acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
1022 OIC_LOG(ERROR, TAG, "Error while memory allocation");
1026 std::cout << "Please input ACL for selected device: " << std::endl;
1027 if (0 != InputACL(acl1))
1034 if (pOwnedDevList[index]->provisionACL(acl1, provisionCB) != OC_STACK_OK)
1037 std::cout <<"provisionACL is failed"<< std::endl;
1041 case 5: //Provision Credentials
1045 if (0 != readDeviceNumber(pOwnedDevList, 2, devices)) break;
1047 int first = devices[0];
1048 int second = devices[1];
1050 std::cout << "Provision Credentials to devices: "<<
1051 pOwnedDevList[first]->getDeviceID();
1052 std::cout << " and "<< pOwnedDevList[second]->getDeviceID() << std::endl;
1054 Credential cred( NO_SECURITY_MODE ,0);
1055 std::cout << "Please input credentials for selected devices: " << std::endl;
1056 if (0 != InputCredentials(cred))
1061 if (pOwnedDevList[first]->provisionCredentials(cred,
1062 *pOwnedDevList[second].get(), provisionCB) != OC_STACK_OK)
1065 std::cout <<"provisionCredentials is failed"<< std::endl;
1069 case 6: //Provision ACL & Creds b/w two devices.
1073 if (0 != readDeviceNumber(pOwnedDevList, 2, devices)) break;
1075 int first = devices[0];
1076 int second = devices[1];
1078 std::cout << "Provision pairwise devices: "<<
1079 pOwnedDevList[first]->getDeviceID();
1080 std::cout << " and "<< pOwnedDevList[second]->getDeviceID() << std::endl;
1082 Credential cred( NO_SECURITY_MODE, 0);
1083 std::cout << "Please input credentials for selected devices: " << std::endl;
1084 if (0 != InputCredentials(cred))
1087 acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
1090 OIC_LOG(ERROR, TAG, "Error while memory allocation");
1094 std::cout << "Please input ACL for selected device: " << std::endl;
1095 if (0 != InputACL(acl1))
1100 acl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
1103 OIC_LOG(ERROR, TAG, "Error while memory allocation");
1107 std::cout << "Please input ACL for selected device: " << std::endl;
1108 if (0 != InputACL(acl2))
1115 if (pOwnedDevList[first]->provisionPairwiseDevices(cred, acl1,
1116 *pOwnedDevList[second].get(), acl2, provisionCB) != OC_STACK_OK)
1119 std::cout <<"provisionPairwiseDevices is failed"<< std::endl;
1123 case 7: //Unlink Devices
1127 if (0 != readDeviceNumber(pOwnedDevList, 2, devices)) break;
1129 int first = devices[0];
1130 int second = devices[1];
1132 std::cout << "Unlink devices: "<< pOwnedDevList[first]->getDeviceID();
1133 std::cout << " and "<< pOwnedDevList[second]->getDeviceID() << std::endl;
1137 if (pOwnedDevList[first]->unlinkDevices(*pOwnedDevList[second].get(),
1138 provisionCB) != OC_STACK_OK)
1141 std::cout <<"unlinkDevice is failed"<< std::endl;
1145 case 8: //Remove Device
1149 if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
1151 std::cout << "Remove Device: "<< pOwnedDevList[index]->getDeviceID()<< std::endl;
1155 if (pOwnedDevList[index]->removeDevice(DISCOVERY_TIMEOUT, provisionCB)
1159 std::cout <<"removeDevice is failed"<< std::endl;
1163 case 9: //Remove Device using UUID
1167 if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
1169 std::cout << "Remove Device: "<< pOwnedDevList[index]->getDeviceID()<< std::endl;
1173 if (OCSecure::removeDeviceWithUuid(DISCOVERY_TIMEOUT,
1174 pOwnedDevList[index]->getDeviceID(),
1179 std::cout <<"removeDevice is failed"<< std::endl;
1184 case 10: //Get Linked devices
1186 UuidList_t linkedUuid;
1187 unsigned int devNum;
1189 if (!pOwnedDevList.size())
1191 std::cout <<"There are no Owned devices yet,"
1192 " may need to discover"<<std::endl;
1196 for (unsigned int i = 0; i < pOwnedDevList.size(); i++ )
1198 std::cout << i+1 << ": "<< pOwnedDevList[i]->getDeviceID() <<" From IP:";
1199 std::cout << pOwnedDevList[i]->getDevAddr() <<std::endl;
1202 std::cout <<"Select device number: "<<std::endl;
1204 if (devNum > pOwnedDevList.size())
1206 std::cout <<"Invalid device number"<<std::endl;
1210 if(pOwnedDevList[devNum -1]->getLinkedDevices(linkedUuid) == OC_STACK_OK)
1212 if (!linkedUuid.size())
1214 std::cout <<"No devices are linked to "<<
1215 pOwnedDevList[devNum -1]->getDeviceID() << std::endl;
1217 //display the Linked devices (UUIDs)
1218 for(unsigned int i = 0; i < linkedUuid.size(); i++)
1220 printUuid(linkedUuid[i]);
1225 std::cout <<"Error! in getLinkedDevices"<<std::endl;
1229 case 11: //Get device' status
1231 DeviceList_t unownedList, ownedList;
1233 if (OCSecure::getDevInfoFromNetwork(DISCOVERY_TIMEOUT, ownedList,
1234 unownedList) == OC_STACK_OK)
1236 std::cout <<"Owned Device' status for" <<std::endl;
1237 for (unsigned int i = 0; i < ownedList.size(); i++ )
1239 std::cout << "Device "<<i+1 <<" ID: '";
1240 std::cout << ownedList[i]->getDeviceID() << "' From IP: ";
1241 std::cout << ownedList[i]->getDevAddr() << " Status: ";
1242 printStatus(ownedList[i]->getDeviceStatus());
1244 std::cout <<"\nUnOwned Device' status for" <<std::endl;
1245 for (unsigned int i = 0; i < unownedList.size(); i++ )
1247 std::cout << "Device "<<i+1 <<" ID: '";
1248 std::cout << unownedList[i]->getDeviceID() << "' From IP: ";
1249 std::cout << unownedList[i]->getDevAddr() << " Status: ";
1250 printStatus(unownedList[i]->getDeviceStatus());
1259 unsigned int devNum;
1261 if (!pOwnedDevList.size())
1263 std::cout <<"There are no Owned devices yet,"
1264 " may need to discover"<<std::endl;
1268 for (unsigned int i = 0; i < pOwnedDevList.size(); i++ )
1270 std::cout << i+1 << ": "<< pOwnedDevList[i]->getDeviceID() <<" From IP:";
1271 std::cout << pOwnedDevList[i]->getDevAddr() <<std::endl;
1274 std::cout <<"Select device number: "<<std::endl;
1276 if (devNum > pOwnedDevList.size())
1278 std::cout <<"Invalid device number"<<std::endl;
1283 provisionDP(devNum);
1287 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1292 std::cout<<"Error in saving cert"<<std::endl;
1300 if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
1302 std::cout << "Provision cert for : "<<
1303 pOwnedDevList[index]->getDeviceID()<< std::endl;
1307 if (pOwnedDevList[index]->provisionTrustCertChain(SIGNED_ASYMMETRIC_KEY,
1308 g_credId,provisionCB ) != OC_STACK_OK)
1311 std::cout <<"provision cert is failed"<< std::endl;
1319 std::cout<<"please save cert using option 13.";
1323 uint8_t *trustCertChain = NULL;
1324 size_t chainSize = 0;
1325 if (OC_STACK_OK != OCSecure::readTrustCertChain(g_credId, &trustCertChain,&chainSize))
1327 std::cout <<"issue in read trust chain"<< std::endl;
1331 std::cout<<"size of cert : "<<chainSize<<std::endl;
1336 #endif //__WITH_DTLS__ || __WITH_TLS__
1344 catch(OCException& e)
1346 oclog() << "Exception in main: "<<e.what();