[ENROLLEE] Tizen enrollee sample application build using scons command
[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     if(InitProvisioning()== ES_ERROR)
110     {
111         cout<<"Init Provisioning Failed"<<endl;
112         return;
113     }
114     cout<<"InitProvisioning:Success"<<endl;
115 }
116
117 void StopEasySetup()
118 {
119     cout<<"StopEasySetup IN"<<endl;
120     if(TerminateEasySetup()== ES_ERROR)
121     {
122         cout<<"return value is: ES_ERROR"<<endl;
123         return;
124     }
125     cout<<"StopEasySetup OUT"<<endl;
126 }
127
128 int main()
129 {
130     cout<<"#########################"<<endl;
131     cout<<"EasySetup Enrollee SAMPLE"<<endl;
132     cout<<"#########################"<<endl;
133     PrintMenu();
134     char option;
135
136     while(true)
137     {
138         cin>>option;
139         switch (option)
140         {
141             case 'H': // help
142             case 'h':
143                 PrintMenu();
144                 break;
145
146             case 'Q': // quit
147             case 'q':
148                 cout<<"quit";
149                 break;
150
151             case 'S': // start easy setup
152             case 's':
153                 StartEasySetup();
154                 break;
155
156             case 'P': // start provisioning
157             case 'p':
158                 StartProvisioning();
159                 break;
160
161             case 'T': // stop easy setup
162             case 't':
163                 StopEasySetup();
164                 break;
165
166             default:
167                 cout<<"wrong option"<<endl;
168                 break;
169         }
170         if(option=='Q') break;
171     }
172     return 0;
173 }
174
175 void *listeningFunc(void*)
176 {
177     OCStackResult result;
178
179     while (true)
180     {
181         result = OCProcess();
182         if (result != OC_STACK_OK)
183         {
184            cout<<"OCStack stop error";
185         }
186     }
187     return NULL;
188 }
189