Restructuring of the easy-setup service.
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / enrollee / tizen-sdb / EnrolleeSample / enrolleewifi.cpp
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 <string.h>
25 #include <iostream>
26 #include <pthread.h>
27
28 #define TAG "TS"
29
30 using namespace std;
31
32 void *listeningFunc(void*);
33
34 /**
35  * @var ssid
36  * @brief Target SSID of the Soft Access point to which the device has to connect
37  */
38 static char ssid[] = "EasySetup123";
39
40 /**
41  * @var passwd
42  * @brief Password of the Soft Access point to which the device has to connect
43  */
44 static char passwd[] = "EasySetup123";
45
46 void PrintMenu()
47 {
48     cout<<"============"<<endl;
49     cout<<"S: start easy setup"<<endl;
50     cout<<"P: start provisioning resources"<<endl;
51     cout<<"T: terminate"<<endl;
52     cout<<"Q: quit"<<endl;
53     cout<<"============"<<endl;
54 }
55
56 void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
57 {
58     cout<<"Easy setup event callback"<<endl;
59
60     if(esResult == ES_OK)
61     {
62         if(enrolleeState == ES_ON_BOARDED_STATE)
63         {
64             cout<<"Device is successfully OnBoared on Adhoc network"<<endl;
65         }
66         else if (enrolleeState == ES_PROVISIONED_STATE)
67         {
68             cout<<"Device is provisioned with target network's credentials"<<endl;
69         }
70         else if (enrolleeState == ES_ON_BOARDED_TARGET_NETWORK_STATE)
71         {
72             cout<<"Device is onboarded/connected with target network"<<endl;
73         }
74         else
75         {
76             cout<<"Wrong state !! Easy setup is failed at Enrollee state = "<<enrolleeState<<endl;
77         }
78     }
79     else
80     {
81         cout<<"Easy stup is failed at Enrollee state = "<<enrolleeState<<endl;
82     }
83
84     PrintMenu();
85 }
86
87
88 void StartEasySetup()
89 {
90     cout<<"StartEasySetup and onboarding started.."<<endl;
91
92     if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
93     {
94         cout<<"StartEasySetup and onboarding Fail!!"<<endl;
95         return;
96     }
97
98     pthread_t thread_handle;
99     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
100     {
101         cout<<"Thread creation failed"<<endl;
102     }
103 }
104
105 void StartProvisioning()
106 {
107     cout<<"Starting Enrollee Provisioning"<<endl;
108
109     // Initialize the OC Stack in Server mode
110     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
111     {
112         cout<<"OCStack init error!!"<<endl;
113         return;
114     }
115
116     if (InitProvisioning() == ES_ERROR)
117     {
118         cout<<"Init Provisioning Failed!!"<<endl;
119         return;
120     }
121
122     cout<<"InitProvisioning Success"<<endl;
123 }
124
125 void StopEasySetup()
126 {
127     cout<<"StopEasySetup IN"<<endl;
128
129     if (TerminateEasySetup() == ES_ERROR)
130     {
131         cout<<"TerminateEasySetup Failed!!"<<endl;
132         return;
133     }
134
135     //stop OC Stack
136     if (OCStop() != OC_STACK_OK)
137     {
138         cout<<"OCStack stop failed!!"<<endl;
139         return;
140     }
141
142     cout<<"StopEasySetup OUT"<<endl;
143 }
144
145 int main()
146 {
147     cout<<"#########################"<<endl;
148     cout<<"EasySetup Enrollee SAMPLE"<<endl;
149     cout<<"#########################"<<endl;
150     PrintMenu();
151     char option;
152
153     while(true)
154     {
155         cin>>option;
156         switch (option)
157         {
158             case 'H': // help
159             case 'h':
160                 PrintMenu();
161                 break;
162
163             case 'Q': // quit
164             case 'q':
165                 cout<<"quit";
166                 break;
167
168             case 'S': // start easy setup
169             case 's':
170                 StartEasySetup();
171                 break;
172
173             case 'P': // start provisioning
174             case 'p':
175                 StartProvisioning();
176                 break;
177
178             case 'T': // stop easy setup
179             case 't':
180                 StopEasySetup();
181                 break;
182
183             default:
184                 cout<<"wrong option"<<endl;
185                 break;
186         }
187         if (option == 'Q' || option == 'q') break;
188     }
189     return 0;
190 }
191
192 void *listeningFunc(void*)
193 {
194     OCStackResult result;
195
196     while (true)
197     {
198         result = OCProcess();
199         if (result != OC_STACK_OK)
200         {
201            cout<<"OCStack stop error";
202         }
203     }
204     return NULL;
205 }
206