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;
163 OCRepPayload* payload = OCRepPayloadCreate();
165 OC_LOG_V(INFO, TAG, "Callback for Room");
166 PrintReceivedMsgInfo(flag, ehRequest );
168 if(ehRequest && flag == OC_REQUEST_FLAG )
170 std::string query = (const char*)ehRequest->query;
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",
317 else if (ehRequest && flag == OC_OBSERVE_FLAG)
319 gLightUnderObservation = 1;
324 OCEntityHandlerResult OCEntityHandlerLightCb(OCEntityHandlerFlag flag,
325 OCEntityHandlerRequest * ehRequest,void* callbackParam)
327 OCEntityHandlerResult ret = OC_EH_OK;
328 OCEntityHandlerResponse response;
329 OCRepPayload* payload = OCRepPayloadCreate();
331 OC_LOG_V(INFO, TAG, "Callback for Light");
332 PrintReceivedMsgInfo(flag, ehRequest );
334 if(ehRequest && flag == OC_REQUEST_FLAG)
336 if(OC_REST_GET == ehRequest->method)
338 OCRepPayloadSetUri(payload, gLightResourceUri);
339 OCRepPayloadSetPropBool(payload, "state", false);
340 OCRepPayloadSetPropInt(payload, "power", 0);
342 else if(OC_REST_PUT == ehRequest->method)
344 OCRepPayloadSetUri(payload, gLightResourceUri);
345 OCRepPayloadSetPropBool(payload, "state", true);
346 OCRepPayloadSetPropInt(payload, "power", 0);
350 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
357 // Format the response. Note this requires some info about the request
358 response.requestHandle = ehRequest->requestHandle;
359 response.resourceHandle = ehRequest->resource;
360 response.ehResult = ret;
361 response.payload = reinterpret_cast<OCPayload*>(payload);
362 response.numSendVendorSpecificHeaderOptions = 0;
363 memset(response.sendVendorSpecificHeaderOptions,
364 0, sizeof response.sendVendorSpecificHeaderOptions);
365 memset(response.resourceUri, 0, sizeof response.resourceUri);
366 // Indicate that response is NOT in a persistent buffer
367 response.persistentBufferFlag = 0;
370 if (OCDoResponse(&response) != OC_STACK_OK)
372 OC_LOG(ERROR, TAG, "Error sending response");
377 else if (ehRequest && flag == OC_OBSERVE_FLAG)
379 gLightUnderObservation = 1;
385 OCEntityHandlerResult OCEntityHandlerFanCb(OCEntityHandlerFlag flag,
386 OCEntityHandlerRequest * ehRequest, void* callback)
388 OCEntityHandlerResult ret = OC_EH_OK;
389 OCEntityHandlerResponse response;
390 OCRepPayload* payload = OCRepPayloadCreate();
392 OC_LOG_V(INFO, TAG, "Callback for Fan");
393 PrintReceivedMsgInfo(flag, ehRequest );
395 if(ehRequest && flag == OC_REQUEST_FLAG)
397 if(OC_REST_GET == ehRequest->method)
399 OCRepPayloadSetUri(payload, gFanResourceUri);
400 OCRepPayloadSetPropBool(payload, "state", true);
401 OCRepPayloadSetPropInt(payload, "speed", 10);
403 else if(OC_REST_PUT == ehRequest->method)
405 OCRepPayloadSetUri(payload, gFanResourceUri);
406 OCRepPayloadSetPropBool(payload, "state", false);
407 OCRepPayloadSetPropInt(payload, "speed", 0);
411 OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
418 // Format the response. Note this requires some info about the request
419 response.requestHandle = ehRequest->requestHandle;
420 response.resourceHandle = ehRequest->resource;
421 response.ehResult = ret;
422 response.payload = reinterpret_cast<OCPayload*>(payload);
423 response.numSendVendorSpecificHeaderOptions = 0;
424 memset(response.sendVendorSpecificHeaderOptions,
425 0, sizeof response.sendVendorSpecificHeaderOptions);
426 memset(response.resourceUri, 0, sizeof response.resourceUri);
427 // Indicate that response is NOT in a persistent buffer
428 response.persistentBufferFlag = 0;
431 if (OCDoResponse(&response) != OC_STACK_OK)
433 OC_LOG(ERROR, TAG, "Error sending response");
439 else if (ehRequest && flag == OC_OBSERVE_FLAG)
441 gLightUnderObservation = 1;
447 /* SIGINT handler: set gQuitFlag to 1 for graceful termination */
448 void handleSigInt(int signum)
450 if (signum == SIGINT)
456 void *ChangeLightRepresentation (void *param)
459 OCStackResult result = OC_STACK_ERROR;
465 if (gLightUnderObservation)
468 " =====> Notifying stack of new power level %d\n", light.power);
469 result = OCNotifyAllObservers (light.handle, OC_NA_QOS);
470 if (OC_STACK_NO_OBSERVERS == result)
472 gLightUnderObservation = 0;
479 int main(int argc, char* argv[])
484 while ((opt = getopt(argc, argv, "t:")) != -1)
496 if(TEST <= TEST_INVALID || TEST >= MAX_TESTS)
502 OC_LOG(DEBUG, TAG, "OCServer is starting...");
504 if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
506 OC_LOG(ERROR, TAG, "OCStack init error");
510 OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandlerCb, NULL);
513 * Declare and create the example resource: light
518 * Create a thread for changing the representation of the light
520 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
522 // Break from loop with Ctrl-C
523 OC_LOG(INFO, TAG, "Entering ocserver main loop...");
524 signal(SIGINT, handleSigInt);
527 if (OCProcess() != OC_STACK_OK)
529 OC_LOG(ERROR, TAG, "OCStack process error");
536 * Cancel the light thread and wait for it to terminate
538 pthread_cancel(threadId);
539 pthread_join(threadId, NULL);
541 OC_LOG(INFO, TAG, "Exiting ocserver main loop...");
543 if (OCStop() != OC_STACK_OK)
545 OC_LOG(ERROR, TAG, "OCStack process error");
551 void createResources()
555 OCResourceHandle fan;
556 OCStackResult res = OCCreateResource(&fan,
558 OC_RSRVD_INTERFACE_DEFAULT,
560 OCEntityHandlerFanCb,
562 OC_DISCOVERABLE|OC_OBSERVABLE);
563 OC_LOG_V(INFO, TAG, "Created fan resource with result: %s", getResult(res));
565 OCResourceHandle light;
566 res = OCCreateResource(&light,
568 OC_RSRVD_INTERFACE_DEFAULT,
570 OCEntityHandlerLightCb,
572 OC_DISCOVERABLE|OC_OBSERVABLE);
573 OC_LOG_V(INFO, TAG, "Created light resource with result: %s", getResult(res));
575 OCResourceHandle room;
577 if(TEST == TEST_APP_COLL_EH)
579 res = OCCreateResource(&room,
581 OC_RSRVD_INTERFACE_BATCH,
583 OCEntityHandlerRoomCb,
589 res = OCCreateResource(&room,
591 OC_RSRVD_INTERFACE_BATCH,
598 OC_LOG_V(INFO, TAG, "Created room resource with result: %s", getResult(res));
599 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_LL);
600 OCBindResourceInterfaceToResource(room, OC_RSRVD_INTERFACE_DEFAULT);
602 res = OCBindResource(room, light);
603 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));
605 res = OCBindResource(room, fan);
606 OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res));