Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / arduino / 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 #include "softap.h"
29 #include "onboarding.h"
30 #include "logger.h"
31 #include "resourcehandler.h"
32
33 /**
34  * @var ES_ENROLLEE_TAG
35  * @brief Logging tag for module name.
36  */
37 #define ES_ENROLLEE_TAG "ES"
38
39 //-----------------------------------------------------------------------------
40 // Private variables
41 //-----------------------------------------------------------------------------
42
43 /**
44  * @var gTargetSsid
45  * @brief Target SSID of the Soft Access point to which the device has to connect
46  */
47 static char gTargetSsid[MAXSSIDLEN];
48
49 /**
50  * @var gTargetPass
51  * @brief Password of the target access point to which the device has to connect
52  */
53 static char gTargetPass[MAXNETCREDLEN];
54
55 /**
56  * @var gEnrolleeStatusCb
57  * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
58  */
59 static ESEnrolleeEventCallback gEnrolleeStatusCb = NULL;
60
61 /**
62  * @var gIsSecured
63  * @brief Variable to check if secure mode is enabled or not.
64  */
65 static bool gIsSecured = false;
66
67 //-----------------------------------------------------------------------------
68 // Private internal function prototypes
69 //-----------------------------------------------------------------------------
70 void ESOnboardingCallback(ESResult esResult);
71 void ESProvisioningCallback(ESResult esResult);
72 void ESOnboardingCallbackTargetNet(ESResult esResult);
73 static bool ESEnrolleeValidateParam(OCConnectivityType networkType, const char *ssid,
74                                                 const char *passwd, ESEnrolleeEventCallback cb);
75
76
77 void ESOnboardingCallback(ESResult esResult)
78 {
79         OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESOnboardingCallback with  result = %d", esResult);
80         if(esResult == ES_OK)
81         {
82             gEnrolleeStatusCb(esResult, ES_ON_BOARDED_STATE);
83         }
84         else
85         {
86             OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
87                         "Onboarding is failed callback result is = %d", esResult);
88             gEnrolleeStatusCb(esResult, ES_INIT_STATE);
89         }
90 }
91
92 void ESProvisioningCallback(ESResult esResult)
93 {
94     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESProvisioningCallback with  result = %d", esResult);
95
96     if (esResult == ES_RECVTRIGGEROFPROVRES)
97     {
98         GetTargetNetworkInfoFromProvResource(gTargetSsid, gTargetPass);
99         gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
100         OIC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
101
102         // Connecting/onboarding to target network
103         ConnectToWiFiNetwork(gTargetSsid, gTargetPass, ESOnboardingCallbackTargetNet);
104     }
105     else
106     {
107        OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Provisioning is failed callback result is = %d", esResult);
108        // Resetting Enrollee to ONBOARDED_STATE as Enrollee is alreday onboarded in previous step
109        gEnrolleeStatusCb(ES_OK, ES_ON_BOARDED_STATE);
110     }
111 }
112
113 void ESOnboardingCallbackTargetNet(ESResult esResult)
114 {
115     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESOnboardingCallback on target network with result = %d",
116                                                                                         esResult);
117     if(esResult == ES_OK)
118     {
119         gEnrolleeStatusCb(esResult, ES_ON_BOARDED_TARGET_NETWORK_STATE);
120     }
121     else
122     {
123         OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG,
124                     "Onboarding is failed on target network and callback result is = %d", esResult);
125         // Resetting Enrollee state to the ES_PROVISIONED_STATE
126         // as device is already being provisioned with target network creds.
127         gEnrolleeStatusCb(esResult, ES_PROVISIONED_STATE);
128     }
129 }
130
131 ESResult ESInitEnrollee(OCConnectivityType networkType, const char *ssid, const char *passwd,
132         bool isSecured,
133         ESEnrolleeEventCallback cb)
134 {
135     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee IN");
136     if(!ESEnrolleeValidateParam(networkType,ssid,passwd,cb))
137     {
138         OIC_LOG(ERROR, ES_ENROLLEE_TAG,
139                             "ESInitEnrollee::Stopping Easy setup due to invalid parameters");
140         return ES_ERROR;
141     }
142
143     //Init callback
144     gEnrolleeStatusCb = cb;
145
146     gIsSecured = isSecured;
147
148     // TODO : This onboarding state has to be set by lower layer, as they better
149     // knows when actually on-boarding started.
150     cb(ES_ERROR,ES_ON_BOARDING_STATE);
151
152     OIC_LOG(INFO, ES_ENROLLEE_TAG, "received callback");
153     OIC_LOG(INFO, ES_ENROLLEE_TAG, "onboarding now..");
154
155     if(!ESOnboard(ssid, passwd, ESOnboardingCallback))
156     {
157         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESInitEnrollee::On-boarding failed");
158         cb(ES_ERROR, ES_INIT_STATE);
159         return ES_ERROR;
160     }
161
162     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
163     return ES_OK;
164 }
165
166 ESResult ESTerminateEnrollee()
167 {
168     UnRegisterResourceEventCallBack();
169
170     //Delete Prov resource
171     if (DeleteProvisioningResource() != OC_STACK_OK)
172     {
173         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
174         return ES_ERROR;
175     }
176
177     OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
178     return ES_OK;
179 }
180
181 ESResult ESInitProvisioning()
182 {
183     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitProvisioning <<IN>>");
184
185     if (CreateProvisioningResource(gIsSecured) != OC_STACK_OK)
186     {
187         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
188         return ES_ERROR;
189     }
190
191     RegisterResourceEventCallBack(ESProvisioningCallback);
192
193     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitProvisioning <<OUT>>");
194     return ES_RESOURCECREATED;
195 }
196
197 static bool ESEnrolleeValidateParam(OCConnectivityType /*networkType*/, const char *ssid,
198                                                 const char *passwd, ESEnrolleeEventCallback cb)
199 {
200     if (!ssid || !passwd || !cb)
201     {
202         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESEnrolleeValidateParam - Invalid parameters");
203         return false;
204     }
205     return true;
206 }
207