1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "easysetup.h"
32 void *listeningFunc(void*);
36 * @brief Target SSID of the Soft Access point to which the device has to connect
38 static char ssid[] = "EasySetup123";
42 * @brief Password of the Soft Access point to which the device has to connect
44 static char passwd[] = "EasySetup123";
47 * Secure Virtual Resource database for Iotivity Server
48 * It contains Server's Identity and the PSK credentials
49 * of other devices which the server trusts
51 static char CRED_FILE[] = "oic_svr_db_server.json";
53 OCPersistentStorage ps ;
58 * @brief Variable to check if secure mode is enabled or not.
60 static bool gIsSecured = false;
64 cout<<"============"<<endl;
65 cout<<"S: Enabled Security"<<endl;
66 cout<<"I: Init easy setup"<<endl;
67 cout<<"P: start provisioning resources"<<endl;
68 cout<<"T: terminate"<<endl;
69 cout<<"Q: quit"<<endl;
70 cout<<"============"<<endl;
73 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
75 cout<<"Easy setup event callback"<<endl;
79 if(enrolleeState == ES_ON_BOARDED_STATE)
81 cout<<"Device is successfully OnBoared on Adhoc network"<<endl;
83 else if (enrolleeState == ES_PROVISIONED_STATE)
85 cout<<"Device is provisioned with target network's credentials"<<endl;
87 else if (enrolleeState == ES_ON_BOARDED_TARGET_NETWORK_STATE)
89 cout<<"Device is onboarded/connected with target network"<<endl;
93 cout<<"Wrong state !! Easy setup is failed at Enrollee state = "<<enrolleeState<<endl;
98 cout<<"Easy stup is failed at Enrollee state = "<<enrolleeState<<endl;
104 FILE* server_fopen(const char *path, const char *mode)
107 return fopen(CRED_FILE, mode);
110 void EnableSecurity()
112 cout << "Inside EnableSecurity API.." << endl;
116 // Initialize Persistent Storage for SVR database
117 ps = { server_fopen, fread, fwrite, fclose, unlink };
118 OCRegisterPersistentStorageHandler(&ps);
121 void StartEasySetup()
123 cout<<"StartEasySetup and onboarding started.."<<endl;
125 if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
127 cout<<"StartEasySetup and onboarding Fail!!"<<endl;
132 void StartOICStackAndStartResources()
134 cout<<"Starting Enrollee Provisioning"<<endl;
136 // Initialize the OC Stack in Server mode
137 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
139 cout<<"OCStack init error!!"<<endl;
143 if (InitProvisioning() == ES_ERROR)
145 cout<<"Init Provisioning Failed!!"<<endl;
149 pthread_t thread_handle;
150 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
152 cout<<"Thread creation failed"<<endl;
155 cout<<"InitProvisioning Success"<<endl;
160 cout<<"StopEasySetup IN"<<endl;
162 if (TerminateEasySetup() == ES_ERROR)
164 cout<<"TerminateEasySetup Failed!!"<<endl;
169 if (OCStop() != OC_STACK_OK)
171 cout<<"OCStack stop failed!!"<<endl;
175 cout<<"StopEasySetup OUT"<<endl;
180 cout<<"#########################"<<endl;
181 cout<<"EasySetup Enrollee SAMPLE"<<endl;
182 cout<<"#########################"<<endl;
201 case 'S': // Enable Security
206 case 'I': // Init EasySetup
211 case 'P': // start provisioning
213 StartOICStackAndStartResources();
216 case 'T': // stop easy setup
222 cout<<"wrong option"<<endl;
225 if (option == 'Q' || option == 'q') break;
230 void *listeningFunc(void*)
232 OCStackResult result;
236 result = OCProcess();
237 if (result != OC_STACK_OK)
239 cout<<"OCStack stop error";