Merge branch 'master' into easysetup & CBOR changes
[platform/upstream/iotivity.git] / service / easy-setup / sdk / enrollee / common / src / easysetup.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 #include "easysetup.h"
22
23 /// WiFi network info and credentials
24 char defaultSsid[] = "EasyConnect";
25 char defaultPass[] = "EasyConnect";
26
27 int g_eventflag = 0;
28 int g_cnt = 0;
29 char *targetSsid;
30 char *targetPass;
31
32 EventCallback g_cbForProvisioning = NULL;
33 EventCallback g_cbForOnboarding = NULL;
34
35 void EventCallbackInOnboarding(ES_RESULT event);
36 void EventCallbackInProvisioning(ES_RESULT event);
37 void EventCallbackAfterProvisioning(ES_RESULT event);
38
39 void EventCallbackInOnboarding(ES_RESULT event)
40 {
41     if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
42     {
43         if (g_cbForOnboarding != NULL)
44         {
45             g_cbForOnboarding(event);
46         }
47     }
48 }
49
50 void EventCallbackInProvisioning(ES_RESULT event)
51 {
52     ES_RESULT res = ES_OK;
53
54     if (event == ES_RECVTRIGGEROFPROVRES)
55     {
56         targetSsid = (char *) malloc(MAXSSIDLEN);
57         targetPass = (char *) malloc(MAXNETCREDLEN);
58
59         GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
60
61         res = ConnectToWiFiNetwork(targetSsid, targetPass, EventCallbackAfterProvisioning);
62
63         if (g_cbForProvisioning != NULL)
64         {
65             g_cbForProvisioning(res);
66         }
67     }
68 }
69
70 void EventCallbackAfterProvisioning(ES_RESULT event)
71 {
72     if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
73     {
74         if (g_cbForProvisioning != NULL)
75         {
76             g_cbForProvisioning(event);
77         }
78     }
79 }
80
81 ES_RESULT FindNetworkForOnboarding(NetworkType networkType, EventCallback cb)
82 {
83     if (networkType == ES_WIFI)
84     {
85         if (g_cbForOnboarding == NULL)
86         {
87             g_cbForOnboarding = cb;
88         }
89
90         return ConnectToWiFiNetwork(defaultSsid, defaultPass, EventCallbackInOnboarding);
91     }
92 }
93
94 ES_RESULT FindNetworkForOnboarding(NetworkType networkType, const char *ssid, const char *passwd,
95         EventCallback cb)
96 {
97     if (!ssid || !passwd)
98     {
99         return ES_ERROR;
100     }
101
102     if (networkType == ES_WIFI)
103     {
104         if (g_cbForOnboarding == NULL)
105         {
106             g_cbForOnboarding = cb;
107         }
108
109         return ConnectToWiFiNetwork(ssid, passwd, EventCallbackInOnboarding);
110     }
111 }
112
113 ES_RESULT InitializeProvisioning(EventCallback cb)
114 {
115     if (cb == NULL)
116     {
117         return ES_ERROR;
118     }
119     else
120     {
121         g_cbForProvisioning = cb;
122     }
123
124     if (CreateProvisioningResource() != OC_STACK_OK)
125     {
126         return ES_ERROR;
127     }
128
129     if (CreateNetworkResource() != OC_STACK_OK)
130     {
131         return ES_ERROR;
132     }
133
134     RegisterResourceEventCallBack(EventCallbackInProvisioning);
135
136     return ES_RESOURCECREATED;
137 }
138