Merge remote-tracking branch 'origin/master' into easysetup, Provisioning Handler...
[platform/upstream/iotivity.git] / service / easy-setup / sdk / enrollee / common / src / resourceHandler.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 "resourceHandler.h"
22 #include "ocpayload.h"
23
24 PROGMEM const char TAG[] = "resourceHandler";
25
26 ProvResource g_prov;
27 NetResource g_net;
28
29 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag, OCEntityHandlerRequest *, void *callback);
30 const char *getResult(OCStackResult result);
31
32 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,OCRepPayload** payload);
33 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,OCRepPayload** payload);
34 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,OCRepPayload** payload);
35 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);
36 int g_flag = 0;
37
38 ResourceEventCallback g_cbForResEvent = NULL;
39
40 void RegisterResourceEventCallBack(ResourceEventCallback cb)
41 {
42     g_cbForResEvent = cb;
43 }
44
45 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
46 {
47     if (name != NULL && pass != NULL)
48     {
49         sprintf(name, "%s", g_prov.tnn);
50         sprintf(pass, "%s", g_prov.cd);
51     }
52 }
53
54 OCStackResult CreateProvisioningResource()
55 {
56     g_prov.ps = 1; // need to provisioning
57     g_prov.tnt = ES_WIFI;
58     sprintf(g_prov.tnn, "Unknown");
59     sprintf(g_prov.cd, "Unknown");
60
61     OCStackResult res = OCCreateResource(&g_prov.handle, "oic.prov", OC_RSRVD_INTERFACE_DEFAULT,
62             OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
63
64     OC_LOG_V(INFO, TAG, "Created Prov resource with result: %s", getResult(res));
65
66     return res;
67 }
68
69 OCStackResult CreateNetworkResource()
70 {
71     NetworkInfo netInfo;
72
73     if (getCurrentNetworkInfo(ES_WIFI, &netInfo) != 0)
74     {
75         return OC_STACK_ERROR;
76     }
77
78     if (netInfo.type != ES_WIFI)
79     {
80         return OC_STACK_ERROR;
81     }
82
83     g_net.cnt = (int) netInfo.type;
84     g_net.ant[0] = (int) ES_WIFI;
85     sprintf(g_net.ipaddr, "%d.%d.%d.%d", netInfo.ipaddr[0], netInfo.ipaddr[1], netInfo.ipaddr[2],
86             netInfo.ipaddr[3]);
87     sprintf(g_net.cnn, "%s", netInfo.ssid);
88
89     OC_LOG_V(INFO, TAG, "SSID: %s", g_net.cnn);
90     OC_LOG_V(INFO, TAG, "IP Address: %s", g_net.ipaddr);
91
92     OCStackResult res = OCCreateResource(&g_net.handle, "oic.net", OC_RSRVD_INTERFACE_DEFAULT,
93             OC_RSRVD_ES_URI_NET, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
94     OC_LOG_V(INFO, TAG, "Created Net resource with result: %s", getResult(res));
95
96     return res;
97 }
98
99 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
100 {
101     OCEntityHandlerResult ehResult = OC_EH_ERROR;
102     if(!ehRequest)
103     {
104         OC_LOG(ERROR, TAG, PCF("Request is Null"));
105         return ehResult;
106     }
107     if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
108     {
109         OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
110         return ehResult;
111     }
112
113     OCRepPayload *getResp = constructResponse(ehRequest);
114     if(!getResp)
115     {
116         OC_LOG(ERROR, TAG, "constructResponse failed");
117         return OC_EH_ERROR;
118     }
119
120     *payload = getResp;
121     ehResult = OC_EH_OK;
122
123     return ehResult;
124 }
125
126 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
127         OCRepPayload** payload)
128 {
129
130     OCEntityHandlerResult ehResult=OC_EH_ERROR;
131     if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
132     {
133         OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
134         return ehResult;
135     }
136
137     OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
138     if(!input)
139     {
140         OC_LOG_V(ERROR, TAG, "Failed to parse");
141         return ehResult;
142     }
143
144     char* tnn;
145     if(OCRepPayloadGetPropString(input,OC_RSRVD_ES_TNN, &tnn))
146     {
147         sprintf(g_prov.tnn, "%s", tnn);
148     }
149
150     char* cd;
151     if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
152     {
153         sprintf(g_prov.cd, "%s", cd);
154     }
155
156     g_flag = 1;
157
158     OCRepPayload *getResp = constructResponse(ehRequest);
159     if(!getResp)
160     {
161         OC_LOG(ERROR, TAG, "constructResponse failed");
162         return OC_EH_ERROR;
163     }
164
165     *payload = getResp;
166     ehResult = OC_EH_OK;
167
168
169
170     return ehResult;
171 }
172
173
174 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
175 {
176     OCEntityHandlerResult ehResult = OC_EH_ERROR;
177     if(!ehRequest)
178     {
179         OC_LOG(ERROR, TAG, PCF("Request is Null"));
180         return ehResult;
181     }
182     if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
183     {
184         OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
185         return ehResult;
186     }
187
188     OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
189     if(!input)
190     {
191         OC_LOG_V(ERROR, TAG, "Failed to parse" );
192         return ehResult;
193     }
194     char* tr;
195     if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
196     {
197
198         // Triggering
199         ehResult = OC_EH_OK;
200     }
201
202     g_flag = 1;
203
204     return ehResult;
205 }
206
207 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
208 {
209     OCRepPayload* payload = OCRepPayloadCreate();
210     if(!payload)
211     {
212         OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
213         return NULL;
214     }
215
216     if (ehRequest->resource == g_prov.handle)
217     {
218         OCRepPayloadSetUri(payload,OC_RSRVD_ES_URI_PROV);
219         OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS,g_prov.ps);
220         OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, g_prov.tnt);
221         OCRepPayloadSetPropString(payload,OC_RSRVD_ES_TNN, g_prov.tnn);
222         OCRepPayloadSetPropString(payload,OC_RSRVD_ES_CD, g_prov.cd);
223     }
224     else if (ehRequest->requestHandle == g_net.handle)
225     {
226
227         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
228         OCRepPayloadSetPropInt(payload, "ant", g_net.ant[0]);
229     }
230     return payload;
231 }
232
233 // This is the entity handler for the registered resource.
234 // This is invoked by OCStack whenever it recevies a request for this resource.
235
236 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
237         OCEntityHandlerRequest* entityHandlerRequest,void *callback)
238 {
239     (void)callback;
240     OCEntityHandlerResult ehRet = OC_EH_OK;
241     OCEntityHandlerResponse response =
242     { 0 };
243     OCRepPayload* payload = NULL;
244     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
245     {
246         if (OC_REST_GET == entityHandlerRequest->method)
247         {
248             OC_LOG_V(INFO, TAG, "Received GET request");
249             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
250         }
251         else if (OC_REST_PUT == entityHandlerRequest->method)
252         {
253             OC_LOG_V(INFO, TAG, "Received PUT request");
254
255             if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle)
256             {
257                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
258             }
259             else
260             {
261                 ehRet = OC_EH_ERROR;
262             }
263         }
264         else if (OC_REST_POST == entityHandlerRequest->method)
265         {
266             // TODO: As of now, POST request will be not received.
267             OC_LOG(INFO, TAG, "Received OC_REST_POST from client");
268             //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
269         }
270
271         if (ehRet == OC_EH_OK)
272         {
273             // Format the response.  Note this requires some info about the request
274             response.requestHandle = entityHandlerRequest->requestHandle;
275             response.resourceHandle = entityHandlerRequest->resource;
276             response.ehResult = ehRet;
277             response.payload = (OCPayload*)(payload);  //response uses OCPaylod while all get,put methodes use OCRepPayload
278             response.numSendVendorSpecificHeaderOptions = 0;
279             memset(response.sendVendorSpecificHeaderOptions, 0,
280                     sizeof response.sendVendorSpecificHeaderOptions);
281             memset(response.resourceUri, 0, sizeof response.resourceUri);
282             // Indicate that response is NOT in a persistent buffer
283             response.persistentBufferFlag = 0;
284
285             // Send the response
286             if (OCDoResponse(&response) != OC_STACK_OK)
287             {
288                 OC_LOG(ERROR, TAG, "Error sending response");
289                 ehRet = OC_EH_ERROR;
290             }
291         }
292     }
293
294     if (g_flag == 1)
295     {
296         g_cbForResEvent(ES_RECVTRIGGEROFPROVRES);
297         g_flag = 0;
298     }
299
300     return ehRet;
301 }
302
303 const char *getResult(OCStackResult result)
304 {
305     switch (result)
306     {
307     case OC_STACK_OK:
308         return "OC_STACK_OK";
309     case OC_STACK_INVALID_URI:
310         return "OC_STACK_INVALID_URI";
311     case OC_STACK_INVALID_QUERY:
312         return "OC_STACK_INVALID_QUERY";
313     case OC_STACK_INVALID_IP:
314         return "OC_STACK_INVALID_IP";
315     case OC_STACK_INVALID_PORT:
316         return "OC_STACK_INVALID_PORT";
317     case OC_STACK_INVALID_CALLBACK:
318         return "OC_STACK_INVALID_CALLBACK";
319     case OC_STACK_INVALID_METHOD:
320         return "OC_STACK_INVALID_METHOD";
321     case OC_STACK_NO_MEMORY:
322         return "OC_STACK_NO_MEMORY";
323     case OC_STACK_COMM_ERROR:
324         return "OC_STACK_COMM_ERROR";
325     case OC_STACK_INVALID_PARAM:
326         return "OC_STACK_INVALID_PARAM";
327     case OC_STACK_NOTIMPL:
328         return "OC_STACK_NOTIMPL";
329     case OC_STACK_NO_RESOURCE:
330         return "OC_STACK_NO_RESOURCE";
331     case OC_STACK_RESOURCE_ERROR:
332         return "OC_STACK_RESOURCE_ERROR";
333     case OC_STACK_SLOW_RESOURCE:
334         return "OC_STACK_SLOW_RESOURCE";
335     case OC_STACK_NO_OBSERVERS:
336         return "OC_STACK_NO_OBSERVERS";
337     case OC_STACK_ERROR:
338         return "OC_STACK_ERROR";
339     default:
340         return "UNKNOWN";
341     }
342 }
343