Modify permissions for files in Things Manager
[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 #include "ocstack.h"
23
24 #define TAG PCF("ES")
25
26 int g_eventflag = 0;
27 int g_cnt = 0;
28 char *targetSsid;
29 char *targetPass;
30
31 EventCallback g_cbForProvisioning = NULL;
32 EventCallback g_cbForOnboarding = NULL;
33
34 void EventCallbackInOnboarding(ESResult event);
35 void EventCallbackInProvisioning(ESResult event);
36 void EventCallbackAfterProvisioning(ESResult event);
37
38 void EventCallbackInOnboarding(ESResult event)
39 {
40     if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
41     {
42         if (g_cbForOnboarding != NULL)
43         {
44             g_cbForOnboarding(event);
45         }
46     }
47 }
48
49 void EventCallbackInProvisioning(ESResult event)
50 {
51     ESResult res = ES_OK;
52
53     if (event == ES_RECVTRIGGEROFPROVRES)
54     {
55         targetSsid = (char *) malloc(MAXSSIDLEN);
56         targetPass = (char *) malloc(MAXNETCREDLEN);
57
58         if(TerminateEasySetup() != OC_STACK_OK)
59         {
60             OC_LOG(ERROR, TAG, PCF("Terminating stack failed"));
61             return;
62         }
63
64         GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
65
66         res = ConnectToWiFiNetwork(targetSsid, targetPass, EventCallbackAfterProvisioning);
67
68         if (g_cbForProvisioning != NULL)
69         {
70             g_cbForProvisioning(res);
71         }
72     }
73 }
74
75 void EventCallbackAfterProvisioning(ESResult event)
76 {
77     if (event == ES_NETWORKFOUND || event == ES_NETWORKCONNECTED)
78     {
79         if (g_cbForProvisioning != NULL)
80         {
81             g_cbForProvisioning(event);
82         }
83     }
84 }
85
86 ESResult FindNetworkForOnboarding(NetworkType networkType,
87                                            const char *ssid,
88                                            const char *passwd,
89                                            EventCallback cb)
90 {
91     if (!ssid || !passwd)
92     {
93         return ES_ERROR;
94     }
95
96     if (networkType == ES_WIFI)
97     {
98         if (g_cbForOnboarding == NULL)
99         {
100             g_cbForOnboarding = cb;
101         }
102
103         if(ConnectToWiFiNetwork(ssid, passwd, EventCallbackInOnboarding) != ES_NETWORKCONNECTED)
104         {
105             OC_LOG(ERROR, TAG, PCF("ConnectToWiFiNetwork Failed"));
106             return ES_ERROR;
107         }
108         else{
109             OC_LOG(INFO, TAG, PCF("ConnectToWiFiNetwork Success"));
110             return ES_OK;
111         }
112     }
113 }
114
115
116 ESResult InitEasySetup(NetworkType networkType, const char *ssid, const char *passwd,
117               EventCallback cb)
118 {
119     if(FindNetworkForOnboarding(networkType, ssid, passwd, cb) != ES_OK)
120     {
121         OC_LOG(ERROR, TAG, PCF("OnBoarding Failed"));
122         return ES_ERROR;
123     }
124
125     // Initialize the OC Stack in Server mode
126     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
127     {
128         OC_LOG(ERROR, TAG, PCF("OCStack init error"));
129         return ES_ERROR;
130     }
131     else
132     {
133         OC_LOG(DEBUG, TAG, PCF("OCStack init success"));
134         return ES_OK;
135     }
136 }
137
138 ESResult TerminateEasySetup()
139 {
140     if(OCStop() != OC_STACK_OK)
141     {
142         OC_LOG(ERROR, TAG, PCF("OCStack stop failed"));
143         return ES_ERROR;
144     }
145     else
146     {
147         OC_LOG(ERROR, TAG, PCF("OCStack stop success"));
148         return ES_OK;
149     }
150 }
151
152 ESResult InitProvisioning(EventCallback cb)
153 {
154     if (cb == NULL)
155     {
156         return ES_ERROR;
157     }
158     else
159     {
160         g_cbForProvisioning = cb;
161     }
162
163     if (CreateProvisioningResource() != OC_STACK_OK)
164     {
165         return ES_ERROR;
166     }
167
168     if (CreateNetworkResource() != OC_STACK_OK)
169     {
170         return ES_ERROR;
171     }
172
173     RegisterResourceEventCallBack(EventCallbackInProvisioning);
174
175     return ES_RESOURCECREATED;
176 }
177