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"
27 * @brief Logging tag for module name.
29 #define ES_RH_TAG "ES_RH"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
37 * @brief Structure for holding the Provisioning status and target information required to connect to the target network
39 static ProvResource gProvResource;
43 * @brief Structure forr holding the Provisioning status of network information
45 static NetResource gNetResource;
47 //-----------------------------------------------------------------------------
48 // Private internal function prototypes
49 //-----------------------------------------------------------------------------
50 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
51 OCEntityHandlerRequest *ehRequest, void *callback);
52 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 gNetworkInfoProvEventCb = NULL;
65 void RegisterResourceEventCallBack(ResourceEventCallback cb)
67 gNetworkInfoProvEventCb = cb;
70 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
72 if (name != NULL && pass != NULL)
74 sprintf(name, "%s", gProvResource.tnn);
75 sprintf(pass, "%s", gProvResource.cd);
79 OCStackResult CreateProvisioningResource()
81 gProvResource.ps = 1; // need to do provisioning
82 gProvResource.tnt = CT_ADAPTER_IP;
83 sprintf(gProvResource.tnn, "Unknown");
84 sprintf(gProvResource.cd, "Unknown");
86 OCStackResult res = OCCreateResource(&gProvResource.handle, "oic.r.prov", OC_RSRVD_INTERFACE_DEFAULT,
87 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb, NULL,
88 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 gNetResource.cnt = (int) netInfo.type;
109 gNetResource.ant[0] = (int) CT_ADAPTER_IP;
111 if(netInfo.ipaddr != NULL)
113 sprintf(gNetResource.ipaddr, "%d.%d.%d.%d", netInfo.ipaddr[0], netInfo.ipaddr[1],
114 netInfo.ipaddr[2], netInfo.ipaddr[3]);
116 sprintf(gNetResource.cnn, "%s", netInfo.ssid);
118 OC_LOG_V(INFO, ES_RH_TAG, "SSID: %s", gNetResource.cnn);
119 OC_LOG_V(INFO, ES_RH_TAG, "IP Address: %s", gNetResource.ipaddr);
121 OCStackResult res = OCCreateResource(&gNetResource.handle, "oic.r.net", OC_RSRVD_INTERFACE_DEFAULT,
122 OC_RSRVD_ES_URI_NET, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
124 OC_LOG_V(INFO, ES_RH_TAG, "Created Net resource with result: %s", getResult(res));
129 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
130 OCRepPayload **payload)
132 OCEntityHandlerResult ehResult = OC_EH_ERROR;
135 OC_LOG(ERROR, ES_RH_TAG, "Request is Null");
138 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
140 OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
144 OCRepPayload *getResp = constructResponse(ehRequest);
147 OC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
157 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
158 OCRepPayload** payload)
160 OC_LOG(INFO, ES_RH_TAG, "ProcessPutRequest enter");
161 OCEntityHandlerResult ehResult = OC_EH_ERROR;
162 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
164 OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
168 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
171 OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
176 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
178 sprintf(gProvResource.tnn, "%s", tnn);
179 OC_LOG(INFO, ES_RH_TAG, "got ssid");
183 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
185 sprintf(gProvResource.cd, "%s", cd);
186 OC_LOG(INFO, ES_RH_TAG, "got password");
188 gProvResource.ps = 2;
189 OC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %d", gProvResource.ps);
192 OCRepPayload *getResp = constructResponse(ehRequest);
195 OC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
205 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
206 OCRepPayload** payload)
208 OCEntityHandlerResult ehResult = OC_EH_ERROR;
211 OC_LOG(ERROR, ES_RH_TAG, "Request is Null");
214 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
216 OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
220 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
223 OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
227 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
237 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
239 OCRepPayload* payload = OCRepPayloadCreate();
242 OC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
246 if (ehRequest->resource == gProvResource.handle)
248 OC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
249 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
250 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, gProvResource.ps);
251 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, gProvResource.tnt);
252 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, gProvResource.tnn);
253 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, gProvResource.cd);
255 else if (ehRequest->requestHandle == gNetResource.handle)
258 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
259 OCRepPayloadSetPropInt(payload, "ant", gNetResource.ant[0]);
265 * This is the entity handler for the registered resource.
266 * This is invoked by OCStack whenever it recevies a request for this resource.
268 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
269 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
272 OCEntityHandlerResult ehRet = OC_EH_OK;
273 OCEntityHandlerResponse response = { 0 };
274 OCRepPayload* payload = NULL;
275 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
277 if (OC_REST_GET == entityHandlerRequest->method)
279 OC_LOG(INFO, ES_RH_TAG, "Received GET request");
280 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
282 else if (OC_REST_PUT == entityHandlerRequest->method)
284 OC_LOG(INFO, ES_RH_TAG, "Received PUT request");
286 if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle)
288 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
292 OC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
296 else if (OC_REST_POST == entityHandlerRequest->method)
298 // TODO: As of now, POST request will be not received.
299 OC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
300 //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
303 if (ehRet == OC_EH_OK)
305 // Format the response. Note this requires some info about the request
306 response.requestHandle = entityHandlerRequest->requestHandle;
307 response.resourceHandle = entityHandlerRequest->resource;
308 response.ehResult = ehRet;
309 //response uses OCPaylod while all get,put methodes use OCRepPayload
310 response.payload = (OCPayload*) (payload);
311 response.numSendVendorSpecificHeaderOptions = 0;
312 memset(response.sendVendorSpecificHeaderOptions, 0,
313 sizeof response.sendVendorSpecificHeaderOptions);
314 memset(response.resourceUri, 0, sizeof response.resourceUri);
315 // Indicate that response is NOT in a persistent buffer
316 response.persistentBufferFlag = 0;
319 if (OCDoResponse(&response) != OC_STACK_OK)
321 OC_LOG(ERROR, ES_RH_TAG, "Error sending response");
329 gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
336 const char *getResult(OCStackResult result)
341 return "OC_STACK_OK";
342 case OC_STACK_INVALID_URI:
343 return "OC_STACK_INVALID_URI";
344 case OC_STACK_INVALID_QUERY:
345 return "OC_STACK_INVALID_QUERY";
346 case OC_STACK_INVALID_IP:
347 return "OC_STACK_INVALID_IP";
348 case OC_STACK_INVALID_PORT:
349 return "OC_STACK_INVALID_PORT";
350 case OC_STACK_INVALID_CALLBACK:
351 return "OC_STACK_INVALID_CALLBACK";
352 case OC_STACK_INVALID_METHOD:
353 return "OC_STACK_INVALID_METHOD";
354 case OC_STACK_NO_MEMORY:
355 return "OC_STACK_NO_MEMORY";
356 case OC_STACK_COMM_ERROR:
357 return "OC_STACK_COMM_ERROR";
358 case OC_STACK_INVALID_PARAM:
359 return "OC_STACK_INVALID_PARAM";
360 case OC_STACK_NOTIMPL:
361 return "OC_STACK_NOTIMPL";
362 case OC_STACK_NO_RESOURCE:
363 return "OC_STACK_NO_RESOURCE";
364 case OC_STACK_RESOURCE_ERROR:
365 return "OC_STACK_RESOURCE_ERROR";
366 case OC_STACK_SLOW_RESOURCE:
367 return "OC_STACK_SLOW_RESOURCE";
368 case OC_STACK_NO_OBSERVERS:
369 return "OC_STACK_NO_OBSERVERS";
371 return "OC_STACK_ERROR";