1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "resourceHandler.h"
22 #include "ocpayload.h"
24 PROGMEM const char TAG[] = "resourceHandler";
29 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag, OCEntityHandlerRequest *, void *callback);
30 const char *getResult(OCStackResult result);
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);
38 ResourceEventCallback g_cbForResEvent = NULL;
40 void RegisterResourceEventCallBack(ResourceEventCallback cb)
45 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
47 if (name != NULL && pass != NULL)
49 sprintf(name, "%s", g_prov.tnn);
50 sprintf(pass, "%s", g_prov.cd);
54 OCStackResult CreateProvisioningResource()
56 g_prov.ps = 1; // need to provisioning
58 sprintf(g_prov.tnn, "Unknown");
59 sprintf(g_prov.cd, "Unknown");
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);
64 OC_LOG_V(INFO, TAG, "Created Prov resource with result: %s", getResult(res));
69 OCStackResult CreateNetworkResource()
73 if (getCurrentNetworkInfo(ES_WIFI, &netInfo) != 0)
75 return OC_STACK_ERROR;
78 if (netInfo.type != ES_WIFI)
80 return OC_STACK_ERROR;
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],
87 sprintf(g_net.cnn, "%s", netInfo.ssid);
89 OC_LOG_V(INFO, TAG, "SSID: %s", g_net.cnn);
90 OC_LOG_V(INFO, TAG, "IP Address: %s", g_net.ipaddr);
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));
99 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
101 OCEntityHandlerResult ehResult = OC_EH_ERROR;
104 OC_LOG(ERROR, TAG, PCF("Request is Null"));
107 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
109 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
113 OCRepPayload *getResp = constructResponse(ehRequest);
116 OC_LOG(ERROR, TAG, "constructResponse failed");
126 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
127 OCRepPayload** payload)
130 OCEntityHandlerResult ehResult=OC_EH_ERROR;
131 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
133 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
137 OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
140 OC_LOG_V(ERROR, TAG, "Failed to parse");
145 if(OCRepPayloadGetPropString(input,OC_RSRVD_ES_TNN, &tnn))
147 sprintf(g_prov.tnn, "%s", tnn);
151 if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
153 sprintf(g_prov.cd, "%s", cd);
158 OCRepPayload *getResp = constructResponse(ehRequest);
161 OC_LOG(ERROR, TAG, "constructResponse failed");
174 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
176 OCEntityHandlerResult ehResult = OC_EH_ERROR;
179 OC_LOG(ERROR, TAG, PCF("Request is Null"));
182 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
184 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
188 OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
191 OC_LOG_V(ERROR, TAG, "Failed to parse" );
195 if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
207 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
209 OCRepPayload* payload = OCRepPayloadCreate();
212 OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
216 if (ehRequest->resource == g_prov.handle)
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);
224 else if (ehRequest->requestHandle == g_net.handle)
227 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
228 OCRepPayloadSetPropInt(payload, "ant", g_net.ant[0]);
233 // This is the entity handler for the registered resource.
234 // This is invoked by OCStack whenever it recevies a request for this resource.
236 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
237 OCEntityHandlerRequest* entityHandlerRequest,void *callback)
240 OCEntityHandlerResult ehRet = OC_EH_OK;
241 OCEntityHandlerResponse response =
243 OCRepPayload* payload = NULL;
244 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
246 if (OC_REST_GET == entityHandlerRequest->method)
248 OC_LOG_V(INFO, TAG, "Received GET request");
249 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
251 else if (OC_REST_PUT == entityHandlerRequest->method)
253 OC_LOG_V(INFO, TAG, "Received PUT request");
255 if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle)
257 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
264 else if (OC_REST_POST == entityHandlerRequest->method)
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);
271 if (ehRet == OC_EH_OK)
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;
286 if (OCDoResponse(&response) != OC_STACK_OK)
288 OC_LOG(ERROR, TAG, "Error sending response");
296 g_cbForResEvent(ES_RECVTRIGGEROFPROVRES);
303 const char *getResult(OCStackResult result)
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";
338 return "OC_STACK_ERROR";