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"
31 void *listeningFunc(void *);
35 * @brief Target SSID of the Soft Access point to which the device has to connect
37 static char ssid[] = "EasySetup123";
41 * @brief Password of the Soft Access point to which the device has to connect
43 static char passwd[] = "EasySetup123";
46 * Secure Virtual Resource database for Iotivity Server
47 * It contains Server's Identity and the PSK credentials
48 * of other devices which the server trusts
50 static char CRED_FILE[] = "oic_svr_db_server.json";
52 OCPersistentStorage ps ;
57 * @brief Variable to check if secure mode is enabled or not.
59 static bool gIsSecured = false;
63 printf("============\n");
64 printf("S: Enabled Security\n");
65 printf("I: Init easy setup\n");
66 printf("P: start provisioning resources\n");
67 printf("T: terminate\n");
69 printf("============\n");
72 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
74 printf("Easy setup event callback\n");
78 if(enrolleeState == ES_ON_BOARDED_STATE)
80 printf("Device is successfully OnBoared on Adhoc network\n");
82 else if (enrolleeState == ES_PROVISIONED_STATE)
84 printf("Device is provisioned with target network's credentials\n");
86 else if (enrolleeState == ES_ON_BOARDED_TARGET_NETWORK_STATE)
88 printf("Device is onboarded/connected with target network\n");
92 printf("Wrong state !! Easy setup is failed at Enrollee state = %d\n",enrolleeState);
97 printf("Easy stup is failed at Enrollee state = %d\n",enrolleeState);;
103 FILE* server_fopen(const char *path, const char *mode)
106 return fopen(CRED_FILE, mode);
109 void EnableSecurity()
111 printf("Inside EnableSecurity API..\n");
115 // Initialize Persistent Storage for SVR database
116 ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
117 OCRegisterPersistentStorageHandler(&ps);
120 void StartEasySetup()
122 printf("StartEasySetup and onboarding started..\n");
124 if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
126 printf("StartEasySetup and onboarding Fail!!\n");
131 void StartOICStackAndStartResources()
133 printf("Starting Enrollee Provisioning\n");
135 // Initialize the OC Stack in Server mode
136 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
138 printf("OCStack init error!!\n");
142 if (InitProvisioning() == ES_ERROR)
144 printf("Init Provisioning Failed!!\n");
148 pthread_t thread_handle;
149 if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
151 printf("Thread creation failed\n");
154 printf("InitProvisioning Success\n");
159 printf("StopEasySetup IN\n");
161 if (TerminateEasySetup() == ES_ERROR)
163 printf("TerminateEasySetup Failed!!\n");
168 if (OCStop() != OC_STACK_OK)
170 printf("OCStack stop failed!!\n");
174 printf("StopEasySetup OUT\n");
179 printf("#########################\n");
180 printf("EasySetup Enrollee SAMPLE\n");
181 printf("#########################\n");
203 case 'S': // Enable Security
208 case 'I': // Init EasySetup
213 case 'P': // start provisioning
215 StartOICStackAndStartResources();
218 case 'T': // stop easy setup
224 printf("wrong option\n");
227 if (option == 'Q' || option == 'q') break;
233 void *listeningFunc(void * a)
235 OCStackResult result;
239 result = OCProcess();
240 if (result != OC_STACK_OK)
242 printf("OCStack stop error");