1 //******************************************************************
3 // Copyright 2015 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"
26 * @brief Logging tag for module name.
28 #define ES_RH_TAG "ES_RH"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
36 * @brief Structure for holding the Provisioning status and target information required to connect to the target network
38 static ProvResource g_prov;
42 * @brief Structure forr holding the Provisioning status of network information
44 static NetResource g_net;
46 //-----------------------------------------------------------------------------
47 // Private internal function prototypes
48 //-----------------------------------------------------------------------------
49 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
50 OCEntityHandlerRequest *ehRequest, void *callback);
51 const char *getResult(OCStackResult result);
53 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
54 OCRepPayload** payload);
55 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
56 OCRepPayload** payload);
57 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
58 OCRepPayload** payload);
59 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);
61 static int g_flag = 0;
63 ResourceEventCallback g_cbForResEvent = NULL;
65 void RegisterResourceEventCallBack(ResourceEventCallback cb)
70 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
72 if (name != NULL && pass != NULL)
74 sprintf(name, "%s", g_prov.tnn);
75 sprintf(pass, "%s", g_prov.cd);
79 OCStackResult CreateProvisioningResource()
81 g_prov.ps = 1; // need to provisioning
82 g_prov.tnt = CT_ADAPTER_IP;
83 sprintf(g_prov.tnn, "Unknown");
84 sprintf(g_prov.cd, "Unknown");
86 OCStackResult res = OCCreateResource(&g_prov.handle, "oic.r.prov", OC_RSRVD_INTERFACE_DEFAULT,
87 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb, NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
89 OC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
94 OCStackResult CreateNetworkResource()
98 if (getCurrentNetworkInfo(CT_ADAPTER_IP, &netInfo) != ES_OK)
100 return OC_STACK_ERROR;
103 if (netInfo.type != CT_ADAPTER_IP)
105 return OC_STACK_ERROR;
108 g_net.cnt = (int) netInfo.type;
109 g_net.ant[0] = (int) CT_ADAPTER_IP;
110 sprintf(g_net.ipaddr, "%d.%d.%d.%d", netInfo.ipaddr[0], netInfo.ipaddr[1], netInfo.ipaddr[2],
112 sprintf(g_net.cnn, "%s", netInfo.ssid);
114 OC_LOG_V(INFO, ES_RH_TAG, "SSID: %s", g_net.cnn);
115 OC_LOG_V(INFO, ES_RH_TAG, "IP Address: %s", g_net.ipaddr);
117 OCStackResult res = OCCreateResource(&g_net.handle, "oic.r.net", OC_RSRVD_INTERFACE_DEFAULT,
118 OC_RSRVD_ES_URI_NET, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
119 OC_LOG_V(INFO, ES_RH_TAG, "Created Net resource with result: %s", getResult(res));
124 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
125 OCRepPayload **payload)
127 OCEntityHandlerResult ehResult = OC_EH_ERROR;
130 OC_LOG(ERROR, ES_RH_TAG, "Request is Null");
133 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
135 OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
139 OCRepPayload *getResp = constructResponse(ehRequest);
142 OC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
152 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
153 OCRepPayload** payload)
156 OCEntityHandlerResult ehResult = OC_EH_ERROR;
157 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
159 OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
163 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
166 OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
171 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
173 sprintf(g_prov.tnn, "%s", tnn);
177 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
179 sprintf(g_prov.cd, "%s", cd);
184 OCRepPayload *getResp = constructResponse(ehRequest);
187 OC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
197 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
198 OCRepPayload** payload)
200 OCEntityHandlerResult ehResult = OC_EH_ERROR;
203 OC_LOG(ERROR, ES_RH_TAG, "Request is Null");
206 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
208 OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
212 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
215 OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
219 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
231 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
233 OCRepPayload* payload = OCRepPayloadCreate();
236 OC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
240 if (ehRequest->resource == g_prov.handle)
242 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
243 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, g_prov.ps);
244 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, g_prov.tnt);
245 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, g_prov.tnn);
246 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, g_prov.cd);
248 else if (ehRequest->requestHandle == g_net.handle)
251 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
252 OCRepPayloadSetPropInt(payload, "ant", g_net.ant[0]);
257 // This is the entity handler for the registered resource.
258 // This is invoked by OCStack whenever it recevies a request for this resource.
259 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
260 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
263 OCEntityHandlerResult ehRet = OC_EH_OK;
264 OCEntityHandlerResponse response =
266 OCRepPayload* payload = NULL;
267 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
269 if (OC_REST_GET == entityHandlerRequest->method)
271 OC_LOG(INFO, ES_RH_TAG, "Received GET request");
272 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
274 else if (OC_REST_PUT == entityHandlerRequest->method)
276 OC_LOG(INFO, ES_RH_TAG, "Received PUT request");
278 if (g_prov.handle != NULL && entityHandlerRequest->resource == g_prov.handle)
280 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
287 else if (OC_REST_POST == entityHandlerRequest->method)
289 // TODO: As of now, POST request will be not received.
290 OC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
291 //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
294 if (ehRet == OC_EH_OK)
296 // Format the response. Note this requires some info about the request
297 response.requestHandle = entityHandlerRequest->requestHandle;
298 response.resourceHandle = entityHandlerRequest->resource;
299 response.ehResult = ehRet;
300 //response uses OCPaylod while all get,put methodes use OCRepPayload
301 response.payload = (OCPayload*) (payload);
302 response.numSendVendorSpecificHeaderOptions = 0;
303 memset(response.sendVendorSpecificHeaderOptions, 0,
304 sizeof response.sendVendorSpecificHeaderOptions);
305 memset(response.resourceUri, 0, sizeof response.resourceUri);
306 // Indicate that response is NOT in a persistent buffer
307 response.persistentBufferFlag = 0;
310 if (OCDoResponse(&response) != OC_STACK_OK)
312 OC_LOG(ERROR, ES_RH_TAG, "Error sending response");
320 g_cbForResEvent(ES_RECVTRIGGEROFPROVRES);
327 const char *getResult(OCStackResult result)
332 return "OC_STACK_OK";
333 case OC_STACK_INVALID_URI:
334 return "OC_STACK_INVALID_URI";
335 case OC_STACK_INVALID_QUERY:
336 return "OC_STACK_INVALID_QUERY";
337 case OC_STACK_INVALID_IP:
338 return "OC_STACK_INVALID_IP";
339 case OC_STACK_INVALID_PORT:
340 return "OC_STACK_INVALID_PORT";
341 case OC_STACK_INVALID_CALLBACK:
342 return "OC_STACK_INVALID_CALLBACK";
343 case OC_STACK_INVALID_METHOD:
344 return "OC_STACK_INVALID_METHOD";
345 case OC_STACK_NO_MEMORY:
346 return "OC_STACK_NO_MEMORY";
347 case OC_STACK_COMM_ERROR:
348 return "OC_STACK_COMM_ERROR";
349 case OC_STACK_INVALID_PARAM:
350 return "OC_STACK_INVALID_PARAM";
351 case OC_STACK_NOTIMPL:
352 return "OC_STACK_NOTIMPL";
353 case OC_STACK_NO_RESOURCE:
354 return "OC_STACK_NO_RESOURCE";
355 case OC_STACK_RESOURCE_ERROR:
356 return "OC_STACK_RESOURCE_ERROR";
357 case OC_STACK_SLOW_RESOURCE:
358 return "OC_STACK_SLOW_RESOURCE";
359 case OC_STACK_NO_OBSERVERS:
360 return "OC_STACK_NO_OBSERVERS";
362 return "OC_STACK_ERROR";