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;
102 OCRepPayload *getResp = constructResponse(ehRequest);
105 OC_LOG(ERROR, TAG, "constructResponse failed");
115 OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
116 OCRepPayload** payload)
119 OCEntityHandlerResult ehResult=OC_EH_ERROR;
120 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
122 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
126 OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
129 OC_LOG_V(ERROR, TAG, "Failed to parse");
134 if(OCRepPayloadGetPropString(input,OC_RSRVD_ES_TNN, &tnn))
136 sprintf(g_prov.tnn, "%s", tnn);
140 if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
142 sprintf(g_prov.cd, "%s", cd);
147 OCRepPayload *getResp = constructResponse(ehRequest);
150 OC_LOG(ERROR, TAG, "constructResponse failed");
163 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
165 OCEntityHandlerResult ehResult = OC_EH_ERROR;
168 OC_LOG(ERROR, TAG, PCF("Request is Null"));
171 if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
173 OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
177 OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);
180 OC_LOG_V(ERROR, TAG, "Failed to parse" );
184 if(OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
196 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
199 OCRepPayload* payload = OCRepPayloadCreate();
202 OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
206 if (g_prov.handle != NULL && ehRequest->resource == g_prov.handle)
210 OCRepPayloadSetUri(payload,OC_RSRVD_ES_URI_PROV);
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);
217 else if (g_net.handle != NULL && ehRequest->requestHandle == g_net.handle)
220 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
221 OCRepPayloadSetPropInt(payload, "ant", g_net.ant[0]);
226 // This is the entity handler for the registered resource.
227 // This is invoked by OCStack whenever it recevies a request for this resource.
229 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
230 OCEntityHandlerRequest* entityHandlerRequest,void *callback)
233 OCEntityHandlerResult ehRet = OC_EH_OK;
234 OCEntityHandlerResponse response =
236 OCRepPayload* payload = NULL;
237 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
239 if (OC_REST_GET == entityHandlerRequest->method)
241 OC_LOG_V(INFO, TAG, "Received GET request");
242 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
244 else if (OC_REST_PUT == entityHandlerRequest->method)
246 OC_LOG_V(INFO, TAG, "Received PUT request");
248 if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle)
250 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
257 else if (OC_REST_POST == entityHandlerRequest->method)
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);
264 if (ehRet == OC_EH_OK)
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;
279 if (OCDoResponse(&response) != OC_STACK_OK)
281 OC_LOG(ERROR, TAG, "Error sending response");
289 g_cbForResEvent(ES_RECVTRIGGEROFPROVRES);
296 const char *getResult(OCStackResult result)
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";
331 return "OC_STACK_ERROR";