[Easy-setup] Commented check for Intended resource
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / enrollee / linux / enrolleewifi.c
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 #include "easysetup.h"
23
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <pthread.h>
28
29 #define TAG "TS"
30
31 void *listeningFunc(void *);
32
33 /**
34  * @var ssid
35  * @brief Target SSID of the Soft Access point to which the device has to connect
36  */
37 static char ssid[] = "EasySetup123";
38
39 /**
40  * @var passwd
41  * @brief Password of the Soft Access point to which the device has to connect
42  */
43 static char passwd[] = "EasySetup123";
44
45 /**
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
49  */
50 static char CRED_FILE[] = "oic_svr_db_server.json";
51
52 OCPersistentStorage ps ;
53
54
55 /**
56  * @var gIsSecured
57  * @brief Variable to check if secure mode is enabled or not.
58  */
59 static bool gIsSecured = false;
60
61 void PrintMenu()
62 {
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");
68     printf("Q: quit\n");
69     printf("============\n");
70 }
71
72 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
73 {
74     printf("Easy setup event callback\n");
75
76     if(esResult == ES_OK)
77     {
78         if(enrolleeState == ES_ON_BOARDED_STATE)
79         {
80             printf("Device is successfully OnBoared on Adhoc network\n");
81         }
82         else if (enrolleeState == ES_PROVISIONED_STATE)
83         {
84             printf("Device is provisioned with target network's credentials\n");
85         }
86         else if (enrolleeState == ES_ON_BOARDED_TARGET_NETWORK_STATE)
87         {
88             printf("Device is onboarded/connected with target network\n");
89         }
90         else
91         {
92             printf("Wrong state !! Easy setup is failed at Enrollee state = %d\n",enrolleeState);
93         }
94     }
95     else
96     {
97         printf("Easy stup is failed at Enrollee state = %d\n",enrolleeState);;
98     }
99
100     PrintMenu();
101 }
102
103 FILE* server_fopen(const char *path, const char *mode)
104 {
105     (void) path;
106     return fopen(CRED_FILE, mode);
107 }
108
109 void EnableSecurity()
110 {
111     printf("Inside EnableSecurity API..\n");
112
113     gIsSecured = true;
114
115     // Initialize Persistent Storage for SVR database
116     ps = (OCPersistentStorage){ server_fopen, fread, fwrite, fclose, unlink };
117     OCRegisterPersistentStorageHandler(&ps);
118 }
119
120 void StartEasySetup()
121 {
122     printf("StartEasySetup and onboarding started..\n");
123
124     if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
125     {
126         printf("StartEasySetup and onboarding Fail!!\n");
127         return;
128     }
129 }
130
131 void StartOICStackAndStartResources()
132 {
133     printf("Starting Enrollee Provisioning\n");
134
135     // Initialize the OC Stack in Server mode
136     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
137     {
138         printf("OCStack init error!!\n");
139         return;
140     }
141
142     if (InitProvisioning() == ES_ERROR)
143     {
144         printf("Init Provisioning Failed!!\n");
145         return;
146     }
147
148     pthread_t thread_handle;
149     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
150     {
151         printf("Thread creation failed\n");
152     }
153
154     printf("InitProvisioning Success\n");
155 }
156
157 void StopEasySetup()
158 {
159     printf("StopEasySetup IN\n");
160
161     if (TerminateEasySetup() == ES_ERROR)
162     {
163         printf("TerminateEasySetup Failed!!\n");
164         return;
165     }
166
167     //stop OC Stack
168     if (OCStop() != OC_STACK_OK)
169     {
170         printf("OCStack stop failed!!\n");
171         return;
172     }
173
174     printf("StopEasySetup OUT\n");
175 }
176
177 int main()
178 {
179     printf("#########################\n");
180     printf("EasySetup Enrollee SAMPLE\n");
181     printf("#########################\n");
182     PrintMenu();
183     char option = 'T'; 
184
185     while(true)
186     {
187         scanf("%c",&option);
188
189        if(option!= '\n')
190       {
191             switch (option)
192             {
193                 case 'H': // help
194                 case 'h':
195                     PrintMenu();
196                     break;
197
198                 case 'Q': // quit
199                 case 'q':
200                     printf("quit");
201                     break;
202
203                 case 'S': // Enable Security
204                 case 's':
205                     EnableSecurity();
206                     break;
207
208                 case 'I': // Init EasySetup
209                 case 'i':
210                     StartEasySetup();
211                     break;
212
213                 case 'P': // start provisioning
214                 case 'p':
215                     StartOICStackAndStartResources();
216                     break;
217
218                 case 'T': // stop easy setup
219                 case 't':
220                     StopEasySetup();
221                     break;
222
223                 default:
224                     printf("wrong option\n");
225                     break;
226             }
227             if (option == 'Q' || option == 'q') break;
228         }
229     }
230     return 0;
231 }
232
233 void *listeningFunc(void * a)
234 {
235     OCStackResult result;
236
237     while (true)
238     {
239         result = OCProcess();
240         if (result != OC_STACK_OK)
241         {
242            printf("OCStack stop error");
243         }
244     }
245     return NULL;
246 }
247