Merge branch 'master' into easysetup & CBOR changes
[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         OCRepPayload *getResp = constructResponse(ehRequest);
103         if(!getResp)
104         {
105                 OC_LOG(ERROR, TAG, "constructResponse failed");
106                 return OC_EH_ERROR;
107         }
108
109         *payload = getResp;
110         ehResult = OC_EH_OK;
111
112         return ehResult;
113 }
114
115 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
116                 OCRepPayload** payload)
117 {
118
119         OCEntityHandlerResult ehResult=OC_EH_ERROR;
120         if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
121         {
122                 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
123                 return ehResult;
124         }
125
126         OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
127         if(!input)
128         {
129                 OC_LOG_V(ERROR, TAG, "Failed to parse");
130                 return ehResult;
131         }
132
133         const char* tnn;
134         if(OCRepPayloadGetPropString(input,OC_RSRVD_ES_TNN, &tnn))
135         {
136                 sprintf(g_prov.tnn, "%s", tnn);
137         }
138
139         const char* cd;
140         if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
141         {
142                 sprintf(g_prov.cd, "%s", cd);
143         }
144
145         g_flag = 1;
146
147         OCRepPayload *getResp = constructResponse(ehRequest);
148         if(!getResp)
149         {
150                 OC_LOG(ERROR, TAG, "constructResponse failed");
151                 return OC_EH_ERROR;
152         }
153
154         *payload = getResp;
155         ehResult = OC_EH_OK;
156
157
158
159         return ehResult;
160 }
161
162
163 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
164 {
165         OCEntityHandlerResult ehResult = OC_EH_ERROR;
166         if(!ehRequest)
167         {
168                 OC_LOG(ERROR, TAG, PCF("Request is Null"));
169                 return ehResult;
170         }
171         if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
172         {
173                 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
174                 return ehResult;
175         }
176
177         OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
178         if(!input)
179         {
180                 OC_LOG_V(ERROR, TAG, "Failed to parse" );
181                 return ehResult;
182         }
183         const char* tr;
184         if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
185         {
186
187                 // Triggering
188                 ehResult = OC_EH_OK;
189         }
190
191         g_flag = 1;
192
193         return ehResult;
194 }
195
196 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
197 {
198
199         OCRepPayload* payload = OCRepPayloadCreate();
200         if(!payload)
201         {
202                 OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
203                 return NULL;
204         }
205
206         if (g_prov.handle != NULL && ehRequest->resource == g_prov.handle)
207         {
208
209                 
210                 OCRepPayloadSetUri(payload,OC_RSRVD_ES_URI_PROV);
211
212                 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS,g_prov.ps);
213                 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, g_prov.tnt);
214                 OCRepPayloadSetPropString(payload,OC_RSRVD_ES_TNN, g_prov.tnn);
215                 OCRepPayloadSetPropString(payload,OC_RSRVD_ES_CD, g_prov.cd);
216         }
217         else if (g_net.handle != NULL && ehRequest->requestHandle == g_net.handle)
218         {
219                 
220                 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
221                 OCRepPayloadSetPropInt(payload, "ant", g_net.ant[0]);
222         }
223         return payload;
224 }
225
226 // This is the entity handler for the registered resource.
227 // This is invoked by OCStack whenever it recevies a request for this resource.
228
229 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
230                 OCEntityHandlerRequest* entityHandlerRequest,void *callback)
231 {
232         (void)callback;
233         OCEntityHandlerResult ehRet = OC_EH_OK;
234         OCEntityHandlerResponse response =
235         { 0 };
236         OCRepPayload* payload = NULL;
237         if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
238         {
239                 if (OC_REST_GET == entityHandlerRequest->method)
240                 {
241                         OC_LOG_V(INFO, TAG, "Received GET request");
242                         ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
243                 }
244                 else if (OC_REST_PUT == entityHandlerRequest->method)
245                 {
246                         OC_LOG_V(INFO, TAG, "Received PUT request");
247
248                         if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle)
249                         {
250                                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
251                         }
252                         else
253                         {
254                                 ehRet = OC_EH_ERROR;
255                         }
256                 }
257                 else if (OC_REST_POST == entityHandlerRequest->method)
258                 {
259                         // TODO: As of now, POST request will be not received.
260                         OC_LOG(INFO, TAG, "Received OC_REST_POST from client");
261                         //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
262                 }
263
264                 if (ehRet == OC_EH_OK)
265                 {
266                         // Format the response.  Note this requires some info about the request
267                         response.requestHandle = entityHandlerRequest->requestHandle;
268                         response.resourceHandle = entityHandlerRequest->resource;
269                         response.ehResult = ehRet;
270                         response.payload = (OCPayload*)(&payload);  //response uses OCPaylod while all get,put methodes use OCRepPayload 
271                         response.numSendVendorSpecificHeaderOptions = 0;
272                         memset(response.sendVendorSpecificHeaderOptions, 0,
273                                         sizeof response.sendVendorSpecificHeaderOptions);
274                         memset(response.resourceUri, 0, sizeof response.resourceUri);
275                         // Indicate that response is NOT in a persistent buffer
276                         response.persistentBufferFlag = 0;
277
278                         // Send the response
279                         if (OCDoResponse(&response) != OC_STACK_OK)
280                         {
281                                 OC_LOG(ERROR, TAG, "Error sending response");
282                                 ehRet = OC_EH_ERROR;
283                         }
284                 }
285         }
286
287         if (g_flag == 1)
288         {
289                 g_cbForResEvent(ES_RECVTRIGGEROFPROVRES);
290                 g_flag = 0;
291         }
292
293         return ehRet;
294 }
295
296 const char *getResult(OCStackResult result)
297 {
298         switch (result)
299         {
300         case OC_STACK_OK:
301                 return "OC_STACK_OK";
302         case OC_STACK_INVALID_URI:
303                 return "OC_STACK_INVALID_URI";
304         case OC_STACK_INVALID_QUERY:
305                 return "OC_STACK_INVALID_QUERY";
306         case OC_STACK_INVALID_IP:
307                 return "OC_STACK_INVALID_IP";
308         case OC_STACK_INVALID_PORT:
309                 return "OC_STACK_INVALID_PORT";
310         case OC_STACK_INVALID_CALLBACK:
311                 return "OC_STACK_INVALID_CALLBACK";
312         case OC_STACK_INVALID_METHOD:
313                 return "OC_STACK_INVALID_METHOD";
314         case OC_STACK_NO_MEMORY:
315                 return "OC_STACK_NO_MEMORY";
316         case OC_STACK_COMM_ERROR:
317                 return "OC_STACK_COMM_ERROR";
318         case OC_STACK_INVALID_PARAM:
319                 return "OC_STACK_INVALID_PARAM";
320         case OC_STACK_NOTIMPL:
321                 return "OC_STACK_NOTIMPL";
322         case OC_STACK_NO_RESOURCE:
323                 return "OC_STACK_NO_RESOURCE";
324         case OC_STACK_RESOURCE_ERROR:
325                 return "OC_STACK_RESOURCE_ERROR";
326         case OC_STACK_SLOW_RESOURCE:
327                 return "OC_STACK_SLOW_RESOURCE";
328         case OC_STACK_NO_OBSERVERS:
329                 return "OC_STACK_NO_OBSERVERS";
330         case OC_STACK_ERROR:
331                 return "OC_STACK_ERROR";
332         default:
333                 return "UNKNOWN";
334         }
335 }
336