Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / easy-setup / sdk / enrollee / src / easysetup.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  * @file
23  *
24  * This file contains the implementation for EasySetup Enrollee device
25  */
26
27 #include "easysetup.h"
28
29 #include "logger.h"
30 #include "resourceHandler.h"
31
32 /**
33  * @var ES_ENROLLEE_TAG
34  * @brief Logging tag for module name.
35  */
36 #define ES_ENROLLEE_TAG "ES"
37
38 //-----------------------------------------------------------------------------
39 // Private variables
40 //-----------------------------------------------------------------------------
41
42 /**
43  * @var targetSsid
44  * @brief Target SSID of the Soft Access point to which the device has to connect
45  */
46 static char *targetSsid;
47
48 /**
49  * @var targetPass
50  * @brief Password of the target access point to which the device has to connect
51  */
52 static char *targetPass;
53
54 /**
55  * @var g_cbForEnrolleeStatus
56  * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
57  */
58 static EventCallback g_cbForEnrolleeStatus = NULL;
59
60 //-----------------------------------------------------------------------------
61 // Private internal function prototypes
62 //-----------------------------------------------------------------------------
63
64 void EventCallbackInOnboarding(ESResult esResult);
65 void EventCallbackInProvisioning(ESResult esResult);
66 void EventCallbackAfterProvisioning(ESResult esResult);
67
68 void EventCallbackInOnboarding(ESResult esResult)
69 {
70     if (g_cbForEnrolleeStatus != NULL){
71         OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with esResult = %d", esResult);
72         if(esResult == ES_OK){
73             OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
74                                                 ES_ON_BOARDED_STATE);
75             g_cbForEnrolleeStatus(esResult, ES_ON_BOARDED_STATE);
76         }
77         else{
78             OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
79                                                 ES_INIT_STATE);
80             g_cbForEnrolleeStatus(esResult, ES_INIT_STATE);
81         }
82     }
83     else{
84         OC_LOG(ERROR, ES_ENROLLEE_TAG, "g_cbForEnrolleeStatus is NULL");
85     }
86 }
87
88 void EventCallbackInProvisioning(ESResult esResult)
89 {
90     ESResult res = ES_OK;
91
92     if (esResult == ES_RECVTRIGGEROFPROVRES)
93     {
94         targetSsid = (char *) malloc(MAXSSIDLEN);
95         targetPass = (char *) malloc(MAXNETCREDLEN);
96
97         if(TerminateEasySetup() != OC_STACK_OK)
98         {
99             OC_LOG(ERROR, ES_ENROLLEE_TAG, "Terminating stack failed");
100             return;
101         }
102
103         GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
104
105         res = ConnectToWiFiNetwork(targetSsid, targetPass, EventCallbackAfterProvisioning);
106
107         if (g_cbForEnrolleeStatus != NULL)
108         {
109             if(res == ES_NETWORKCONNECTED){
110                 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
111                                                     ES_PROVISIONED_STATE);
112                 g_cbForEnrolleeStatus(ES_OK, ES_PROVISIONED_STATE);
113             }
114             else{
115                 OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
116                                                     ES_PROVISIONING_STATE);
117                 g_cbForEnrolleeStatus(ES_OK, ES_PROVISIONING_STATE);
118             }
119
120         }
121     }
122 }
123
124 void EventCallbackAfterProvisioning(ESResult esResult)
125 {
126     if (g_cbForEnrolleeStatus != NULL){
127         if(esResult == ES_OK){
128             OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
129                                                    ES_PROVISIONED_STATE);
130             g_cbForEnrolleeStatus(esResult, ES_PROVISIONED_STATE);
131         }
132         else{
133             OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Calling the application with enrolleestate = %d",
134                                                    ES_PROVISIONING_STATE);
135             g_cbForEnrolleeStatus(esResult, ES_PROVISIONING_STATE);
136         }
137     }
138     else{
139         OC_LOG(ERROR, ES_ENROLLEE_TAG, "g_cbForEnrolleeStatus is NULL");
140     }
141 }
142
143 ESResult FindNetworkForOnboarding(OCConnectivityType networkType,
144                                            const char *ssid,
145                                            const char *passwd,
146                                            EventCallback cb)
147 {
148     if (!ssid || !passwd)
149     {
150         return ES_ERROR;
151     }
152
153     if (networkType == CT_ADAPTER_IP)
154     {
155         if (g_cbForEnrolleeStatus == NULL)
156         {
157             g_cbForEnrolleeStatus = cb;
158         }
159
160         if(ConnectToWiFiNetwork(ssid, passwd, EventCallbackInOnboarding) != ES_NETWORKCONNECTED)
161         {
162             OC_LOG(ERROR, ES_ENROLLEE_TAG, "ConnectToWiFiNetwork Failed");
163             cb(ES_ERROR, ES_ON_BOARDING_STATE);
164             return ES_ERROR;
165         }
166         else{
167             OC_LOG(INFO, ES_ENROLLEE_TAG, "ConnectToWiFiNetwork Success");
168             cb(ES_OK, ES_ON_BOARDED_STATE);
169             return ES_OK;
170         }
171     }
172     return ES_ERROR;
173 }
174
175
176 ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
177               EventCallback cb)
178 {
179     if(FindNetworkForOnboarding(networkType, ssid, passwd, cb) != ES_OK)
180     {
181         OC_LOG(ERROR, ES_ENROLLEE_TAG, "OnBoarding Failed");
182         return ES_ERROR;
183     }
184
185     // Initialize the OC Stack in Server mode
186     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
187     {
188         OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack init error");
189         return ES_ERROR;
190     }
191     else
192     {
193         OC_LOG(DEBUG, ES_ENROLLEE_TAG, "OCStack init success");
194         return ES_OK;
195     }
196 }
197
198 ESResult TerminateEasySetup()
199 {
200     if(OCStop() != OC_STACK_OK)
201     {
202         OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack stop failed");
203         return ES_ERROR;
204     }
205     else
206     {
207         OC_LOG(ERROR, ES_ENROLLEE_TAG, "OCStack stop success");
208         return ES_OK;
209     }
210 }
211
212 ESResult InitProvisioning()
213 {
214     if (CreateProvisioningResource() != OC_STACK_OK)
215     {
216         return ES_ERROR;
217     }
218
219     if (CreateNetworkResource() != OC_STACK_OK)
220     {
221         return ES_ERROR;
222     }
223
224     RegisterResourceEventCallBack(EventCallbackInProvisioning);
225
226     return ES_RESOURCECREATED;
227 }
228