1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
39 #include "ocpayload.h"
41 #include "platform_features.h"
43 #define TAG PCF("ocservercontainer")
45 volatile sig_atomic_t gQuitFlag = 0;
46 int gLightUnderObservation = 0;
47 void createResources();
48 typedef struct LIGHTRESOURCE{
49 OCResourceHandle handle;
54 static LightResource light;
56 char *gLightResourceUri= (char *)"/a/light";
57 char *gRoomResourceUri= (char *)"/a/room";
58 char *gFanResourceUri= (char *)"/a/fan";
70 OIC_LOG(INFO, TAG, "Usage : ocservercoll -t <Test Case>");
72 "Test Case 1 : Create room resource with default collection entity handler.");
74 "Test Case 2 : Create room resource with application collection entity handler.");
77 unsigned static int TEST = TEST_INVALID;
80 PrintReceivedMsgInfo(OCEntityHandlerFlag flag, OCEntityHandlerRequest * ehRequest)
82 const char* typeOfMessage;
83 const char* typeOfMethod;
88 typeOfMessage = "OC_REQUEST_FLAG";
91 typeOfMessage = "OC_OBSERVE_FLAG";
94 typeOfMessage = "UNKNOWN";
97 if (ehRequest == NULL)
99 typeOfMethod = "UNKNOWN";
101 else if (ehRequest->method == OC_REST_GET)
103 typeOfMethod = "OC_REST_GET";
107 typeOfMethod = "OC_REST_PUT";
110 OIC_LOG_V(INFO, TAG, "Receiving message type: %s, method %s", typeOfMessage,
114 //The only case when this entity handler is for a non-existing resource.
115 OCEntityHandlerResult
116 OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
117 OCEntityHandlerRequest *entityHandlerRequest, char* uri, void* /*callbackParam*/)
119 OIC_LOG_V(INFO, TAG, "Inside device default entity handler - flags: 0x%x, uri: %s", flag, uri);
121 OCEntityHandlerResult ehResult = OC_EH_OK;
122 OCEntityHandlerResponse response;
124 if (!entityHandlerRequest)
126 OIC_LOG(ERROR, TAG, "Invalid request pointer");
130 if (entityHandlerRequest->resource == NULL)
132 OIC_LOG(INFO, TAG, "Received request from client to a non-existing resource");
133 ehResult = OC_EH_RESOURCE_NOT_FOUND;
137 OIC_LOG_V(INFO, TAG, "Device Handler: Received unsupported request from client %d",
138 entityHandlerRequest->method);
139 ehResult = OC_EH_ERROR;
142 if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
144 // Format the response. Note this requires some info about the request
145 response.requestHandle = entityHandlerRequest->requestHandle;
146 response.resourceHandle = entityHandlerRequest->resource;
147 response.ehResult = ehResult;
148 response.payload = nullptr;
149 response.numSendVendorSpecificHeaderOptions = 0;
150 memset(response.sendVendorSpecificHeaderOptions,
151 0, sizeof response.sendVendorSpecificHeaderOptions);
152 // Indicate that response is NOT in a persistent buffer
153 response.persistentBufferFlag = 0;
156 if (OCDoResponse(&response) != OC_STACK_OK)
158 OIC_LOG(ERROR, TAG, "Error sending response");
159 ehResult = OC_EH_ERROR;
165 OCEntityHandlerResult OCEntityHandlerRoomCb(OCEntityHandlerFlag flag,
166 OCEntityHandlerRequest * ehRequest,
169 OCEntityHandlerResult ret = OC_EH_OK;
170 OCEntityHandlerResponse response;
172 OIC_LOG_V(INFO, TAG, "Callback for Room");
173 PrintReceivedMsgInfo(flag, ehRequest );
175 if(ehRequest && flag == OC_REQUEST_FLAG )
177 std::string query = (const char*)ehRequest->query;
178 OCRepPayload* payload = OCRepPayloadCreate();
180 if(OC_REST_GET == ehRequest->method)
182 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
184 OCRepPayloadSetUri(payload, gRoomResourceUri);
185 OCRepPayloadSetPropString(payload, "name", "John's Room");
187 OCRepPayload *tempPayload = OCRepPayloadCreate();
188 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
189 OCRepPayloadAppend(payload, tempPayload);
191 OCRepPayload *tempPayload2 = OCRepPayloadCreate();
192 OCRepPayloadSetUri(tempPayload2, gFanResourceUri);
193 OCRepPayloadAppend(payload, tempPayload2);
195 else if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
197 OCRepPayloadSetUri(payload, gRoomResourceUri);
199 OCRepPayload *tempPayload = OCRepPayloadCreate();
200 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
201 OCRepPayloadAppend(payload, tempPayload);
203 OCRepPayload *tempPayload2 = OCRepPayloadCreate();
204 OCRepPayloadSetUri(tempPayload2, gFanResourceUri);
205 OCRepPayloadAppend(payload, tempPayload2);
207 else if(query.find(OC_RSRVD_INTERFACE_BATCH) != std::string::npos)
210 OCRepPayloadSetUri(payload, gRoomResourceUri);
212 OCRepPayload *tempPayload = OCRepPayloadCreate();
213 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
214 OCRepPayloadSetPropBool(tempPayload, "state", false);
215 OCRepPayloadSetPropInt(tempPayload, "power", 0);
216 OCRepPayloadAppend(payload, tempPayload);
218 OCRepPayload *tempPayload2 = OCRepPayloadCreate();
219 OCRepPayloadSetUri(tempPayload2, gFanResourceUri);
220 OCRepPayloadSetPropBool(tempPayload2, "state", true);
221 OCRepPayloadSetPropInt(tempPayload2, "speed", 10);
222 OCRepPayloadAppend(payload, tempPayload2);
226 // Format the response. Note this requires some info about the request
227 response.requestHandle = ehRequest->requestHandle;
228 response.resourceHandle = ehRequest->resource;
229 response.ehResult = ret;
230 response.payload = reinterpret_cast<OCPayload*>(payload);
231 response.numSendVendorSpecificHeaderOptions = 0;
232 memset(response.sendVendorSpecificHeaderOptions,
233 0, sizeof response.sendVendorSpecificHeaderOptions);
234 memset(response.resourceUri, 0, sizeof response.resourceUri);
235 // Indicate that response is NOT in a persistent buffer
236 response.persistentBufferFlag = 0;
238 if (OCDoResponse(&response) != OC_STACK_OK)
240 OIC_LOG(ERROR, TAG, "Error sending response");
245 else if(OC_REST_PUT == ehRequest->method)
247 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
249 if(ret != OC_EH_ERROR)
251 OCRepPayloadSetUri(payload, gRoomResourceUri);
252 OCRepPayloadSetPropString(payload, "name", "John's Room");
255 if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
257 if(ret != OC_EH_ERROR)
259 OCRepPayloadSetUri(payload, gRoomResourceUri);
261 if(ret != OC_EH_ERROR)
263 OCRepPayload *tempPayload = OCRepPayloadCreate();
264 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
265 OCRepPayloadAppend(payload, tempPayload);
267 if(ret != OC_EH_ERROR)
269 OCRepPayload *tempPayload = OCRepPayloadCreate();
270 OCRepPayloadSetUri(tempPayload, gFanResourceUri);
271 OCRepPayloadAppend(payload, tempPayload);
274 if(query.find(OC_RSRVD_INTERFACE_BATCH ) != std::string::npos)
276 if(ret != OC_EH_ERROR)
278 OCRepPayloadSetUri(payload, gRoomResourceUri);
280 if(ret != OC_EH_ERROR)
282 OCRepPayload *tempPayload = OCRepPayloadCreate();
283 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
284 OCRepPayloadSetPropBool(tempPayload, "state", true);
285 OCRepPayloadSetPropInt(tempPayload, "power", 0);
286 OCRepPayloadAppend(payload, tempPayload);
288 if(ret != OC_EH_ERROR)
290 OCRepPayload *tempPayload = OCRepPayloadCreate();
291 OCRepPayloadSetUri(tempPayload, gFanResourceUri);
292 OCRepPayloadSetPropBool(tempPayload, "state", false);
293 OCRepPayloadSetPropInt(tempPayload, "speed", 0);
294 OCRepPayloadAppend(payload, tempPayload);
299 // Format the response. Note this requires some info about the request
300 response.requestHandle = ehRequest->requestHandle;
301 response.resourceHandle = ehRequest->resource;
302 response.ehResult = ret;
303 response.payload = reinterpret_cast<OCPayload*>(payload);
304 response.numSendVendorSpecificHeaderOptions = 0;
305 memset(response.sendVendorSpecificHeaderOptions,
306 0, sizeof response.sendVendorSpecificHeaderOptions);
307 memset(response.resourceUri, 0, sizeof response.resourceUri);
308 // Indicate that response is NOT in a persistent buffer
309 response.persistentBufferFlag = 0;
311 if (OCDoResponse(&response) != OC_STACK_OK)
313 OIC_LOG(ERROR, TAG, "Error sending response");
320 OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
322 OCRepPayloadDestroy(payload);
326 else if (ehRequest && flag == OC_OBSERVE_FLAG)
328 gLightUnderObservation = 1;
333 OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
334 OCEntityHandlerRequest * ehRequest,void* /*callbackParam*/)
336 OCEntityHandlerResult ret = OC_EH_OK;
337 OCEntityHandlerResponse response;
339 OIC_LOG_V(INFO, TAG, "Callback for Light");
340 PrintReceivedMsgInfo(flag, ehRequest );
342 if(ehRequest && flag == OC_REQUEST_FLAG)
344 OCRepPayload* payload = OCRepPayloadCreate();
345 if(OC_REST_GET == ehRequest->method)
347 OCRepPayloadSetUri(payload, gLightResourceUri);
348 OCRepPayloadSetPropBool(payload, "state", false);
349 OCRepPayloadSetPropInt(payload, "power", 0);
351 else if(OC_REST_PUT == ehRequest->method)
353 OCRepPayloadSetUri(payload, gLightResourceUri);
354 OCRepPayloadSetPropBool(payload, "state", true);
355 OCRepPayloadSetPropInt(payload, "power", 0);
359 OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
366 // Format the response. Note this requires some info about the request
367 response.requestHandle = ehRequest->requestHandle;
368 response.resourceHandle = ehRequest->resource;
369 response.ehResult = ret;
370 response.payload = reinterpret_cast<OCPayload*>(payload);
371 response.numSendVendorSpecificHeaderOptions = 0;
372 memset(response.sendVendorSpecificHeaderOptions,
373 0, sizeof response.sendVendorSpecificHeaderOptions);
374 memset(response.resourceUri, 0, sizeof response.resourceUri);
375 // Indicate that response is NOT in a persistent buffer
376 response.persistentBufferFlag = 0;
379 if (OCDoResponse(&response) != OC_STACK_OK)
381 OIC_LOG(ERROR, TAG, "Error sending response");
387 OCRepPayloadDestroy(payload);
390 else if (ehRequest && flag == OC_OBSERVE_FLAG)
392 gLightUnderObservation = 1;
398 OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag,
399 OCEntityHandlerRequest * ehRequest, void* /*callback*/)
401 OCEntityHandlerResult ret = OC_EH_OK;
402 OCEntityHandlerResponse response;
404 OIC_LOG_V(INFO, TAG, "Callback for Fan");
405 PrintReceivedMsgInfo(flag, ehRequest );
407 if(ehRequest && flag == OC_REQUEST_FLAG)
409 OCRepPayload* payload = OCRepPayloadCreate();
411 if(OC_REST_GET == ehRequest->method)
413 OCRepPayloadSetUri(payload, gFanResourceUri);
414 OCRepPayloadSetPropBool(payload, "state", true);
415 OCRepPayloadSetPropInt(payload, "speed", 10);
417 else if(OC_REST_PUT == ehRequest->method)
419 OCRepPayloadSetUri(payload, gFanResourceUri);
420 OCRepPayloadSetPropBool(payload, "state", false);
421 OCRepPayloadSetPropInt(payload, "speed", 0);
425 OIC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
432 // Format the response. Note this requires some info about the request
433 response.requestHandle = ehRequest->requestHandle;
434 response.resourceHandle = ehRequest->resource;
435 response.ehResult = ret;
436 response.payload = reinterpret_cast<OCPayload*>(payload);
437 response.numSendVendorSpecificHeaderOptions = 0;
438 memset(response.sendVendorSpecificHeaderOptions,
439 0, sizeof response.sendVendorSpecificHeaderOptions);
440 memset(response.resourceUri, 0, sizeof response.resourceUri);
441 // Indicate that response is NOT in a persistent buffer
442 response.persistentBufferFlag = 0;
445 if (OCDoResponse(&response) != OC_STACK_OK)
447 OIC_LOG(ERROR, TAG, "Error sending response");
451 OCRepPayloadDestroy(payload);
454 else if (ehRequest && flag == OC_OBSERVE_FLAG)
456 gLightUnderObservation = 1;
462 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
463 void handleSigInt(int signum)
465 if (signum == SIGINT)
471 void *ChangeLightRepresentation (void *param)
474 OCStackResult result = OC_STACK_ERROR;
480 if (gLightUnderObservation)
483 " =====> Notifying stack of new power level %d\n", light.power);
484 result = OCNotifyAllObservers (light.handle, OC_NA_QOS);
485 if (OC_STACK_NO_OBSERVERS == result)
487 gLightUnderObservation = 0;
494 int main(int argc, char* argv[])
499 while ((opt = getopt(argc, argv, "t:")) != -1)
511 if(TEST <= TEST_INVALID || TEST >= MAX_TESTS)
517 OIC_LOG(DEBUG, TAG, "OCServer is starting...");
519 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
521 OIC_LOG(ERROR, TAG, "OCStack init error");
525 OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandlerCb, NULL);
528 * Declare and create the example resource: light
533 * Create a thread for changing the representation of the light
535 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
537 // Break from loop with Ctrl-C
538 OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
539 signal(SIGINT, handleSigInt);
542 if (OCProcess() != OC_STACK_OK)
544 OIC_LOG(ERROR, TAG, "OCStack process error");
551 * Cancel the light thread and wait for it to terminate
553 #ifdef HAVE_PTHREAD_H
554 pthread_cancel(threadId);
555 pthread_join(threadId, NULL);
558 OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");
560 if (OCStop() != OC_STACK_OK)
562 OIC_LOG(ERROR, TAG, "OCStack process error");
568 void createResources()
572 OCResourceHandle fan;
573 OCStackResult res = OCCreateResource(&fan,
575 OC_RSRVD_INTERFACE_DEFAULT,
577 OCEntityHandlerFanCb,
579 OC_DISCOVERABLE|OC_OBSERVABLE);
580 OIC_LOG_V(INFO, TAG, "Created fan resource with result: %s", getResult(res));
582 OCResourceHandle light;
583 res = OCCreateResource(&light,
585 OC_RSRVD_INTERFACE_DEFAULT,
587 OCEntityHandlerLightCb,
589 OC_DISCOVERABLE|OC_OBSERVABLE);
590 OIC_LOG_V(INFO, TAG, "Created light resource with result: %s", getResult(res));
592 OCResourceHandle room;
594 if(TEST == TEST_APP_COLL_EH)
596 res = OCCreateResource(&room,
598 OC_RSRVD_INTERFACE_BATCH,
600 OCEntityHandlerRoomCb,
606 res = OCCreateResource(&room,
608 OC_RSRVD_INTERFACE_BATCH,
615 OIC_LOG_V(INFO, TAG, "Created room resource with result: %s", getResult(res));
616 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_LL);
617 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_DEFAULT);
619 res = OCBindResource(room, light);
620 OIC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
622 res = OCBindResource(room, fan);
623 OIC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));