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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
31 #include "ocpayload.h"
33 const char *getResult(OCStackResult result);
35 #define TAG PCF("ocservercontainer")
37 volatile sig_atomic_t gQuitFlag = 0;
38 int gLightUnderObservation = 0;
39 void createResources();
40 typedef struct LIGHTRESOURCE{
41 OCResourceHandle handle;
46 static LightResource light;
48 char *gLightResourceUri= (char *)"/a/light";
49 char *gRoomResourceUri= (char *)"/a/room";
50 char *gFanResourceUri= (char *)"/a/fan";
62 OC_LOG(INFO, TAG, "Usage : ocservercoll -t <Test Case>");
64 "Test Case 1 : Create room resource with default collection entity handler.");
66 "Test Case 2 : Create room resource with application collection entity handler.");
69 unsigned static int TEST = TEST_INVALID;
72 PrintReceivedMsgInfo(OCEntityHandlerFlag flag, OCEntityHandlerRequest * ehRequest)
74 const char* typeOfMessage;
75 const char* typeOfMethod;
80 typeOfMessage = "OC_REQUEST_FLAG";
83 typeOfMessage = "OC_OBSERVE_FLAG";
86 typeOfMessage = "UNKNOWN";
89 if (ehRequest == NULL)
91 typeOfMethod = "UNKNOWN";
93 else if (ehRequest->method == OC_REST_GET)
95 typeOfMethod = "OC_REST_GET";
99 typeOfMethod = "OC_REST_PUT";
102 OC_LOG_V(INFO, TAG, "Receiving message type: %s, method %s", typeOfMessage,
106 //The only case when this entity handler is for a non-existing resource.
107 OCEntityHandlerResult
108 OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
109 OCEntityHandlerRequest *entityHandlerRequest, char* uri, void* /*callbackParam*/)
111 OC_LOG_V(INFO, TAG, "Inside device default entity handler - flags: 0x%x, uri: %s", flag, uri);
113 OCEntityHandlerResult ehResult = OC_EH_OK;
114 OCEntityHandlerResponse response;
116 if (!entityHandlerRequest)
118 OC_LOG(ERROR, TAG, "Invalid request pointer");
122 if (entityHandlerRequest->resource == NULL)
124 OC_LOG(INFO, TAG, "Received request from client to a non-existing resource");
125 ehResult = OC_EH_RESOURCE_NOT_FOUND;
129 OC_LOG_V(INFO, TAG, "Device Handler: Received unsupported request from client %d",
130 entityHandlerRequest->method);
131 ehResult = OC_EH_ERROR;
134 if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
136 // Format the response. Note this requires some info about the request
137 response.requestHandle = entityHandlerRequest->requestHandle;
138 response.resourceHandle = entityHandlerRequest->resource;
139 response.ehResult = ehResult;
140 response.payload = nullptr;
141 response.numSendVendorSpecificHeaderOptions = 0;
142 memset(response.sendVendorSpecificHeaderOptions,
143 0, sizeof response.sendVendorSpecificHeaderOptions);
144 // Indicate that response is NOT in a persistent buffer
145 response.persistentBufferFlag = 0;
148 if (OCDoResponse(&response) != OC_STACK_OK)
150 OC_LOG(ERROR, TAG, "Error sending response");
151 ehResult = OC_EH_ERROR;
157 OCEntityHandlerResult OCEntityHandlerRoomCb(OCEntityHandlerFlag flag,
158 OCEntityHandlerRequest * ehRequest,
161 OCEntityHandlerResult ret = OC_EH_OK;
162 OCEntityHandlerResponse response;
164 OC_LOG_V(INFO, TAG, "Callback for Room");
165 PrintReceivedMsgInfo(flag, ehRequest );
167 if(ehRequest && flag == OC_REQUEST_FLAG )
169 std::string query = (const char*)ehRequest->query;
170 OCRepPayload* payload = OCRepPayloadCreate();
172 if(OC_REST_GET == ehRequest->method)
174 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
176 OCRepPayloadSetUri(payload, gRoomResourceUri);
177 OCRepPayloadSetPropString(payload, "name", "John's Room");
179 OCRepPayload *tempPayload = OCRepPayloadCreate();
180 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
181 OCRepPayloadAppend(payload, tempPayload);
183 OCRepPayload *tempPayload2 = OCRepPayloadCreate();
184 OCRepPayloadSetUri(tempPayload2, gFanResourceUri);
185 OCRepPayloadAppend(payload, tempPayload2);
187 else if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
189 OCRepPayloadSetUri(payload, gRoomResourceUri);
191 OCRepPayload *tempPayload = OCRepPayloadCreate();
192 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
193 OCRepPayloadAppend(payload, tempPayload);
195 OCRepPayload *tempPayload2 = OCRepPayloadCreate();
196 OCRepPayloadSetUri(tempPayload2, gFanResourceUri);
197 OCRepPayloadAppend(payload, tempPayload2);
199 else if(query.find(OC_RSRVD_INTERFACE_BATCH) != std::string::npos)
202 OCRepPayloadSetUri(payload, gRoomResourceUri);
204 OCRepPayload *tempPayload = OCRepPayloadCreate();
205 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
206 OCRepPayloadSetPropBool(tempPayload, "state", false);
207 OCRepPayloadSetPropInt(tempPayload, "power", 0);
208 OCRepPayloadAppend(payload, tempPayload);
210 OCRepPayload *tempPayload2 = OCRepPayloadCreate();
211 OCRepPayloadSetUri(tempPayload2, gFanResourceUri);
212 OCRepPayloadSetPropBool(tempPayload2, "state", true);
213 OCRepPayloadSetPropInt(tempPayload2, "speed", 10);
214 OCRepPayloadAppend(payload, tempPayload2);
218 // Format the response. Note this requires some info about the request
219 response.requestHandle = ehRequest->requestHandle;
220 response.resourceHandle = ehRequest->resource;
221 response.ehResult = ret;
222 response.payload = reinterpret_cast<OCPayload*>(payload);
223 response.numSendVendorSpecificHeaderOptions = 0;
224 memset(response.sendVendorSpecificHeaderOptions,
225 0, sizeof response.sendVendorSpecificHeaderOptions);
226 memset(response.resourceUri, 0, sizeof response.resourceUri);
227 // Indicate that response is NOT in a persistent buffer
228 response.persistentBufferFlag = 0;
230 if (OCDoResponse(&response) != OC_STACK_OK)
232 OC_LOG(ERROR, TAG, "Error sending response");
237 else if(OC_REST_PUT == ehRequest->method)
239 if(query.find(OC_RSRVD_INTERFACE_DEFAULT) != std::string::npos)
241 if(ret != OC_EH_ERROR)
243 OCRepPayloadSetUri(payload, gRoomResourceUri);
244 OCRepPayloadSetPropString(payload, "name", "John's Room");
247 if(query.find(OC_RSRVD_INTERFACE_LL) != std::string::npos)
249 if(ret != OC_EH_ERROR)
251 OCRepPayloadSetUri(payload, gRoomResourceUri);
253 if(ret != OC_EH_ERROR)
255 OCRepPayload *tempPayload = OCRepPayloadCreate();
256 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
257 OCRepPayloadAppend(payload, tempPayload);
259 if(ret != OC_EH_ERROR)
261 OCRepPayload *tempPayload = OCRepPayloadCreate();
262 OCRepPayloadSetUri(tempPayload, gFanResourceUri);
263 OCRepPayloadAppend(payload, tempPayload);
266 if(query.find(OC_RSRVD_INTERFACE_BATCH ) != std::string::npos)
268 if(ret != OC_EH_ERROR)
270 OCRepPayloadSetUri(payload, gRoomResourceUri);
272 if(ret != OC_EH_ERROR)
274 OCRepPayload *tempPayload = OCRepPayloadCreate();
275 OCRepPayloadSetUri(tempPayload, gLightResourceUri);
276 OCRepPayloadSetPropBool(tempPayload, "state", true);
277 OCRepPayloadSetPropInt(tempPayload, "power", 0);
278 OCRepPayloadAppend(payload, tempPayload);
280 if(ret != OC_EH_ERROR)
282 OCRepPayload *tempPayload = OCRepPayloadCreate();
283 OCRepPayloadSetUri(tempPayload, gFanResourceUri);
284 OCRepPayloadSetPropBool(tempPayload, "state", false);
285 OCRepPayloadSetPropInt(tempPayload, "speed", 0);
286 OCRepPayloadAppend(payload, tempPayload);
291 // Format the response. Note this requires some info about the request
292 response.requestHandle = ehRequest->requestHandle;
293 response.resourceHandle = ehRequest->resource;
294 response.ehResult = ret;
295 response.payload = reinterpret_cast<OCPayload*>(payload);
296 response.numSendVendorSpecificHeaderOptions = 0;
297 memset(response.sendVendorSpecificHeaderOptions,
298 0, sizeof response.sendVendorSpecificHeaderOptions);
299 memset(response.resourceUri, 0, sizeof response.resourceUri);
300 // Indicate that response is NOT in a persistent buffer
301 response.persistentBufferFlag = 0;
303 if (OCDoResponse(&response) != OC_STACK_OK)
305 OC_LOG(ERROR, TAG, "Error sending response");
312 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
314 OCRepPayloadDestroy(payload);
318 else if (ehRequest && flag == OC_OBSERVE_FLAG)
320 gLightUnderObservation = 1;
325 OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
326 OCEntityHandlerRequest * ehRequest,void* /*callbackParam*/)
328 OCEntityHandlerResult ret = OC_EH_OK;
329 OCEntityHandlerResponse response;
331 OC_LOG_V(INFO, TAG, "Callback for Light");
332 PrintReceivedMsgInfo(flag, ehRequest );
334 if(ehRequest && flag == OC_REQUEST_FLAG)
336 OCRepPayload* payload = OCRepPayloadCreate();
337 if(OC_REST_GET == ehRequest->method)
339 OCRepPayloadSetUri(payload, gLightResourceUri);
340 OCRepPayloadSetPropBool(payload, "state", false);
341 OCRepPayloadSetPropInt(payload, "power", 0);
343 else if(OC_REST_PUT == ehRequest->method)
345 OCRepPayloadSetUri(payload, gLightResourceUri);
346 OCRepPayloadSetPropBool(payload, "state", true);
347 OCRepPayloadSetPropInt(payload, "power", 0);
351 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
358 // Format the response. Note this requires some info about the request
359 response.requestHandle = ehRequest->requestHandle;
360 response.resourceHandle = ehRequest->resource;
361 response.ehResult = ret;
362 response.payload = reinterpret_cast<OCPayload*>(payload);
363 response.numSendVendorSpecificHeaderOptions = 0;
364 memset(response.sendVendorSpecificHeaderOptions,
365 0, sizeof response.sendVendorSpecificHeaderOptions);
366 memset(response.resourceUri, 0, sizeof response.resourceUri);
367 // Indicate that response is NOT in a persistent buffer
368 response.persistentBufferFlag = 0;
371 if (OCDoResponse(&response) != OC_STACK_OK)
373 OC_LOG(ERROR, TAG, "Error sending response");
379 OCRepPayloadDestroy(payload);
382 else if (ehRequest && flag == OC_OBSERVE_FLAG)
384 gLightUnderObservation = 1;
390 OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag,
391 OCEntityHandlerRequest * ehRequest, void* /*callback*/)
393 OCEntityHandlerResult ret = OC_EH_OK;
394 OCEntityHandlerResponse response;
396 OC_LOG_V(INFO, TAG, "Callback for Fan");
397 PrintReceivedMsgInfo(flag, ehRequest );
399 if(ehRequest && flag == OC_REQUEST_FLAG)
401 OCRepPayload* payload = OCRepPayloadCreate();
403 if(OC_REST_GET == ehRequest->method)
405 OCRepPayloadSetUri(payload, gFanResourceUri);
406 OCRepPayloadSetPropBool(payload, "state", true);
407 OCRepPayloadSetPropInt(payload, "speed", 10);
409 else if(OC_REST_PUT == ehRequest->method)
411 OCRepPayloadSetUri(payload, gFanResourceUri);
412 OCRepPayloadSetPropBool(payload, "state", false);
413 OCRepPayloadSetPropInt(payload, "speed", 0);
417 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
424 // Format the response. Note this requires some info about the request
425 response.requestHandle = ehRequest->requestHandle;
426 response.resourceHandle = ehRequest->resource;
427 response.ehResult = ret;
428 response.payload = reinterpret_cast<OCPayload*>(payload);
429 response.numSendVendorSpecificHeaderOptions = 0;
430 memset(response.sendVendorSpecificHeaderOptions,
431 0, sizeof response.sendVendorSpecificHeaderOptions);
432 memset(response.resourceUri, 0, sizeof response.resourceUri);
433 // Indicate that response is NOT in a persistent buffer
434 response.persistentBufferFlag = 0;
437 if (OCDoResponse(&response) != OC_STACK_OK)
439 OC_LOG(ERROR, TAG, "Error sending response");
443 OCRepPayloadDestroy(payload);
446 else if (ehRequest && flag == OC_OBSERVE_FLAG)
448 gLightUnderObservation = 1;
454 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
455 void handleSigInt(int signum)
457 if (signum == SIGINT)
463 void *ChangeLightRepresentation (void *param)
466 OCStackResult result = OC_STACK_ERROR;
472 if (gLightUnderObservation)
475 " =====> Notifying stack of new power level %d\n", light.power);
476 result = OCNotifyAllObservers (light.handle, OC_NA_QOS);
477 if (OC_STACK_NO_OBSERVERS == result)
479 gLightUnderObservation = 0;
486 int main(int argc, char* argv[])
491 while ((opt = getopt(argc, argv, "t:")) != -1)
503 if(TEST <= TEST_INVALID || TEST >= MAX_TESTS)
509 OC_LOG(DEBUG, TAG, "OCServer is starting...");
511 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
513 OC_LOG(ERROR, TAG, "OCStack init error");
517 OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandlerCb, NULL);
520 * Declare and create the example resource: light
525 * Create a thread for changing the representation of the light
527 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
529 // Break from loop with Ctrl-C
530 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
531 signal(SIGINT, handleSigInt);
534 if (OCProcess() != OC_STACK_OK)
536 OC_LOG(ERROR, TAG, "OCStack process error");
543 * Cancel the light thread and wait for it to terminate
545 pthread_cancel(threadId);
546 pthread_join(threadId, NULL);
548 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
550 if (OCStop() != OC_STACK_OK)
552 OC_LOG(ERROR, TAG, "OCStack process error");
558 void createResources()
562 OCResourceHandle fan;
563 OCStackResult res = OCCreateResource(&fan,
565 OC_RSRVD_INTERFACE_DEFAULT,
567 OCEntityHandlerFanCb,
569 OC_DISCOVERABLE|OC_OBSERVABLE);
570 OC_LOG_V(INFO, TAG, "Created fan resource with result: %s", getResult(res));
572 OCResourceHandle light;
573 res = OCCreateResource(&light,
575 OC_RSRVD_INTERFACE_DEFAULT,
577 OCEntityHandlerLightCb,
579 OC_DISCOVERABLE|OC_OBSERVABLE);
580 OC_LOG_V(INFO, TAG, "Created light resource with result: %s", getResult(res));
582 OCResourceHandle room;
584 if(TEST == TEST_APP_COLL_EH)
586 res = OCCreateResource(&room,
588 OC_RSRVD_INTERFACE_BATCH,
590 OCEntityHandlerRoomCb,
596 res = OCCreateResource(&room,
598 OC_RSRVD_INTERFACE_BATCH,
605 OC_LOG_V(INFO, TAG, "Created room resource with result: %s", getResult(res));
606 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_LL);
607 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_DEFAULT);
609 res = OCBindResource(room, light);
610 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
612 res = OCBindResource(room, fan);
613 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));