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";
55 * @brief Variable to check if secure mode is enabled or not.
57 static bool gIsSecured = false;
61 cout<<"============"<<endl;
62 cout<<"I: Init easy setup"<<endl;
63 cout<<"S: Enabled Security"<<endl;
64 cout<<"P: start provisioning resources"<<endl;
65 cout<<"T: terminate"<<endl;
66 cout<<"Q: quit"<<endl;
67 cout<<"============"<<endl;
70 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
72 cout<<"Easy setup event callback"<<endl;
76 if(enrolleeState == ES_ON_BOARDED_STATE)
78 cout<<"Device is successfully OnBoared on Adhoc network"<<endl;
80 else if (enrolleeState == ES_PROVISIONED_STATE)
82 cout<<"Device is provisioned with target network's credentials"<<endl;
84 else if (enrolleeState == ES_ON_BOARDED_TARGET_NETWORK_STATE)
86 cout<<"Device is onboarded/connected with target network"<<endl;
90 cout<<"Wrong state !! Easy setup is failed at Enrollee state = "<<enrolleeState<<endl;
95 cout<<"Easy stup is failed at Enrollee state = "<<enrolleeState<<endl;
101 FILE* server_fopen(const char *path, const char *mode)
104 return fopen(CRED_FILE, mode);
107 void EnableSecurity()
109 cout << "Inside EnableSecurity API.." << endl;
113 // Initialize Persistent Storage for SVR database
114 OCPersistentStorage ps = { server_fopen, fread, fwrite, fclose, unlink };
115 OCRegisterPersistentStorageHandler(&ps);
118 void StartEasySetup()
120 cout<<"StartEasySetup and onboarding started.."<<endl;
122 if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
124 cout<<"StartEasySetup and onboarding Fail!!"<<endl;
128 pthread_t thread_handle;
129 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
131 cout<<"Thread creation failed"<<endl;
135 void StartOICStackAndStartResources()
137 cout<<"Starting Enrollee Provisioning"<<endl;
139 // Initialize the OC Stack in Server mode
140 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
142 cout<<"OCStack init error!!"<<endl;
146 if (InitProvisioning() == ES_ERROR)
148 cout<<"Init Provisioning Failed!!"<<endl;
152 cout<<"InitProvisioning Success"<<endl;
157 cout<<"StopEasySetup IN"<<endl;
159 if (TerminateEasySetup() == ES_ERROR)
161 cout<<"TerminateEasySetup Failed!!"<<endl;
166 if (OCStop() != OC_STACK_OK)
168 cout<<"OCStack stop failed!!"<<endl;
172 cout<<"StopEasySetup OUT"<<endl;
177 cout<<"#########################"<<endl;
178 cout<<"EasySetup Enrollee SAMPLE"<<endl;
179 cout<<"#########################"<<endl;
198 case 'I': // Init EasySetup
203 case 'S': // Enable Security
208 case 'P': // start provisioning
210 StartOICStackAndStartResources();
213 case 'T': // stop easy setup
219 cout<<"wrong option"<<endl;
222 if (option == 'Q' || option == 'q') break;
227 void *listeningFunc(void*)
229 OCStackResult result;
233 result = OCProcess();
234 if (result != OC_STACK_OK)
236 cout<<"OCStack stop error";