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"
33 void *listeningFunc(void*);
37 * @brief Target SSID of the Soft Access point to which the device has to connect
39 static char ssid[] = "EasySetup123";
43 * @brief Password of the Soft Access point to which the device has to connect
45 static char passwd[] = "EasySetup123";
48 * Secure Virtual Resource database for Iotivity Server
49 * It contains Server's Identity and the PSK credentials
50 * of other devices which the server trusts
52 static char CRED_FILE[] = "oic_svr_db_server.json";
54 OCPersistentStorage ps ;
59 * @brief Variable to check if secure mode is enabled or not.
61 static bool gIsSecured = false;
65 cout<<"============"<<endl;
66 cout<<"S: Enabled Security"<<endl;
67 cout<<"I: Init easy setup"<<endl;
68 cout<<"P: start provisioning resources"<<endl;
69 cout<<"T: terminate"<<endl;
70 cout<<"Q: quit"<<endl;
71 cout<<"============"<<endl;
74 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
76 cout<<"Easy setup event callback"<<endl;
80 if(enrolleeState == ES_ON_BOARDED_STATE)
82 cout<<"Device is successfully OnBoared on Adhoc network"<<endl;
84 else if (enrolleeState == ES_PROVISIONED_STATE)
86 cout<<"Device is provisioned with target network's credentials"<<endl;
88 else if (enrolleeState == ES_ON_BOARDED_TARGET_NETWORK_STATE)
90 cout<<"Device is onboarded/connected with target network"<<endl;
94 cout<<"Wrong state !! Easy setup is failed at Enrollee state = "<<enrolleeState<<endl;
99 cout<<"Easy stup is failed at Enrollee state = "<<enrolleeState<<endl;
105 FILE* server_fopen(const char *path, const char *mode)
108 return fopen(CRED_FILE, mode);
111 void EnableSecurity()
113 cout << "Inside EnableSecurity API.." << endl;
117 // Initialize Persistent Storage for SVR database
118 ps = { server_fopen, fread, fwrite, fclose, unlink };
119 OCRegisterPersistentStorageHandler(&ps);
122 void StartEasySetup()
124 cout<<"StartEasySetup and onboarding started.."<<endl;
126 if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
128 cout<<"StartEasySetup and onboarding Fail!!"<<endl;
133 void StartOICStackAndStartResources()
135 cout<<"Starting Enrollee Provisioning"<<endl;
137 // Initialize the OC Stack in Server mode
138 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
140 cout<<"OCStack init error!!"<<endl;
144 if (InitProvisioning() == ES_ERROR)
146 cout<<"Init Provisioning Failed!!"<<endl;
150 pthread_t thread_handle;
151 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
153 cout<<"Thread creation failed"<<endl;
156 cout<<"InitProvisioning Success"<<endl;
161 cout<<"StopEasySetup IN"<<endl;
163 if (TerminateEasySetup() == ES_ERROR)
165 cout<<"TerminateEasySetup Failed!!"<<endl;
170 if (OCStop() != OC_STACK_OK)
172 cout<<"OCStack stop failed!!"<<endl;
176 cout<<"StopEasySetup OUT"<<endl;
181 cout<<"#########################"<<endl;
182 cout<<"EasySetup Enrollee SAMPLE"<<endl;
183 cout<<"#########################"<<endl;
202 case 'S': // Enable Security
207 case 'I': // Init EasySetup
212 case 'P': // start provisioning
214 StartOICStackAndStartResources();
217 case 'T': // stop easy setup
223 cout<<"wrong option"<<endl;
226 if (option == 'Q' || option == 'q') break;
231 void *listeningFunc(void*)
233 OCStackResult result;
237 result = OCProcess();
238 if (result != OC_STACK_OK)
240 cout<<"OCStack stop error";