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