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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
30 #include <condition_variable>
32 #include "OCPlatform.h"
37 namespace PH = std::placeholders;
40 void * ChangeLightRepresentation (void *param);
41 void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest);
43 // Specifies where to notify all observers or list of observers
44 // false: notifies all observers
45 // true: notifies list of observers
46 bool isListOfObservers = false;
48 // Specifies secure or non-secure
49 // false: non-secure resource
50 // true: secure resource
51 bool isSecure = false;
53 /// Specifies whether Entity handler is going to do slow response or not
54 bool isSlowResponse = false;
56 // Forward declaring the entityHandler
58 /// This class represents a single resource named 'lightResource'. This resource has
59 /// two simple properties named 'state' and 'power'
65 /// Access this property from a TB client
69 std::string m_lightUri;
70 OCResourceHandle m_resourceHandle;
71 OCRepresentation m_lightRep;
72 ObservationIds m_interestedObservers;
77 :m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light"),
78 m_resourceHandle(nullptr) {
79 // Initialize representation
80 m_lightRep.setUri(m_lightUri);
82 m_lightRep.setValue("state", m_state);
83 m_lightRep.setValue("power", m_power);
84 m_lightRep.setValue("name", m_name);
87 /* Note that this does not need to be a member function: for classes you do not have
88 access to, you can accomplish this with a free function: */
90 /// This function internally calls registerResource API.
94 std::string resourceURI = m_lightUri;
95 //resource type name. In this case, it is light
96 std::string resourceTypeName = "core.light";
97 // resource interface.
98 std::string resourceInterface = DEFAULT_INTERFACE;
100 // OCResourceProperty is defined ocstack.h
101 uint8_t resourceProperty;
104 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
108 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
110 EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
112 // This will internally create and register the resource.
113 OCStackResult result = OCPlatform::registerResource(
114 m_resourceHandle, resourceURI, resourceTypeName,
115 resourceInterface, cb, resourceProperty);
117 if (OC_STACK_OK != result)
119 cout << "Resource creation was unsuccessful\n";
123 OCStackResult createResource1()
125 // URI of the resource
126 std::string resourceURI = "/a/light1";
127 // resource type name. In this case, it is light
128 std::string resourceTypeName = "core.light";
129 // resource interface.
130 std::string resourceInterface = DEFAULT_INTERFACE;
132 // OCResourceProperty is defined ocstack.h
133 uint8_t resourceProperty;
136 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
140 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
142 EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
144 OCResourceHandle resHandle;
146 // This will internally create and register the resource.
147 OCStackResult result = OCPlatform::registerResource(
148 resHandle, resourceURI, resourceTypeName,
149 resourceInterface, cb, resourceProperty);
151 if (OC_STACK_OK != result)
153 cout << "Resource creation was unsuccessful\n";
159 OCResourceHandle getHandle()
161 return m_resourceHandle;
164 // Puts representation.
165 // Gets values from the representation and
166 // updates the internal state
167 void put(OCRepresentation& rep)
170 if (rep.getValue("state", m_state))
172 cout << "\t\t\t\t" << "state: " << m_state << endl;
176 cout << "\t\t\t\t" << "state not found in the representation" << endl;
179 if (rep.getValue("power", m_power))
181 cout << "\t\t\t\t" << "power: " << m_power << endl;
185 cout << "\t\t\t\t" << "power not found in the representation" << endl;
190 cout << e.what() << endl;
195 // Post representation.
196 // Post can create new resource or simply act like put.
197 // Gets values from the representation and
198 // updates the internal state
199 OCRepresentation post(OCRepresentation& rep)
201 static int first = 1;
203 // for the first time it tries to create a resource
208 if(OC_STACK_OK == createResource1())
210 OCRepresentation rep1;
211 rep1.setValue("createduri", std::string("/a/light1"));
217 // from second time onwards it just puts
223 // gets the updated representation.
224 // Updates the representation with latest internal state before
226 OCRepresentation get()
228 m_lightRep.setValue("state", m_state);
229 m_lightRep.setValue("power", m_power);
234 void addType(const std::string& type) const
236 OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
237 if (OC_STACK_OK != result)
239 cout << "Binding TypeName to Resource was unsuccessful\n";
243 void addInterface(const std::string& interface) const
245 OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
246 if (OC_STACK_OK != result)
248 cout << "Binding TypeName to Resource was unsuccessful\n";
253 // This is just a sample implementation of entity handler.
254 // Entity handler can be implemented in several ways by the manufacturer
255 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
257 cout << "\tIn Server CPP entity handler:\n";
258 OCEntityHandlerResult ehResult = OC_EH_ERROR;
261 // Get the request type and request flag
262 std::string requestType = request->getRequestType();
263 int requestFlag = request->getRequestHandlerFlag();
265 if(requestFlag & RequestHandlerFlag::RequestFlag)
267 cout << "\t\trequestFlag : Request\n";
268 auto pResponse = std::make_shared<OC::OCResourceResponse>();
269 pResponse->setRequestHandle(request->getRequestHandle());
270 pResponse->setResourceHandle(request->getResourceHandle());
272 // Check for query params (if any)
273 QueryParamsMap queries = request->getQueryParameters();
275 if (!queries.empty())
277 std::cout << "\nQuery processing upto entityHandler" << std::endl;
279 for (auto it : queries)
281 std::cout << "Query key: " << it.first << " value : " << it.second
285 // If the request type is GET
286 if(requestType == "GET")
288 cout << "\t\t\trequestType : GET\n";
289 if(isSlowResponse) // Slow response case
291 static int startedThread = 0;
294 std::thread t(handleSlowResponse, (void *)this, request);
298 ehResult = OC_EH_SLOW;
300 else // normal response case.
302 pResponse->setErrorCode(200);
303 pResponse->setResponseResult(OC_EH_OK);
304 pResponse->setResourceRepresentation(get());
305 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
311 else if(requestType == "PUT")
313 cout << "\t\t\trequestType : PUT\n";
314 OCRepresentation rep = request->getResourceRepresentation();
316 // Do related operations related to PUT request
317 // Update the lightResource
319 pResponse->setErrorCode(200);
320 pResponse->setResponseResult(OC_EH_OK);
321 pResponse->setResourceRepresentation(get());
322 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
327 else if(requestType == "POST")
329 cout << "\t\t\trequestType : POST\n";
331 OCRepresentation rep = request->getResourceRepresentation();
333 // Do related operations related to POST request
334 OCRepresentation rep_post = post(rep);
335 pResponse->setResourceRepresentation(rep_post);
336 pResponse->setErrorCode(200);
337 if(rep_post.hasAttribute("createduri"))
339 pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
340 pResponse->setNewResourceUri(rep_post.getValue<std::string>("createduri"));
344 pResponse->setResponseResult(OC_EH_OK);
347 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
352 else if(requestType == "DELETE")
354 cout << "Delete request received" << endl;
358 if(requestFlag & RequestHandlerFlag::ObserverFlag)
360 ObservationInfo observationInfo = request->getObservationInfo();
361 if(ObserveAction::ObserveRegister == observationInfo.action)
363 m_interestedObservers.push_back(observationInfo.obsId);
365 else if(ObserveAction::ObserveUnregister == observationInfo.action)
367 m_interestedObservers.erase(std::remove(
368 m_interestedObservers.begin(),
369 m_interestedObservers.end(),
370 observationInfo.obsId),
371 m_interestedObservers.end());
376 cout << "\t\trequestFlag : Observer\n";
378 static int startedThread = 0;
380 // Observation happens on a different thread in ChangeLightRepresentation function.
381 // If we have not created the thread already, we will create one here.
384 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)this);
392 std::cout << "Request invalid" << std::endl;
400 // ChangeLightRepresentaion is an observation function,
401 // which notifies any changes to the resource to stack
402 // via notifyObservers
403 void * ChangeLightRepresentation (void *param)
405 LightResource* lightPtr = (LightResource*) param;
407 // This function continuously monitors for the changes
414 // If under observation if there are any changes to the light resource
415 // we call notifyObservors
417 // For demostration we are changing the power value and notifying.
418 lightPtr->m_power += 10;
420 cout << "\nPower updated to : " << lightPtr->m_power << endl;
421 cout << "Notifying observers with resource handle: " << lightPtr->getHandle() << endl;
423 OCStackResult result = OC_STACK_OK;
425 if(isListOfObservers)
427 std::shared_ptr<OCResourceResponse> resourceResponse =
428 {std::make_shared<OCResourceResponse>()};
430 resourceResponse->setErrorCode(200);
431 resourceResponse->setResourceRepresentation(lightPtr->get(), DEFAULT_INTERFACE);
433 result = OCPlatform::notifyListOfObservers( lightPtr->getHandle(),
434 lightPtr->m_interestedObservers,
439 result = OCPlatform::notifyAllObservers(lightPtr->getHandle());
442 if(OC_STACK_NO_OBSERVERS == result)
444 cout << "No More observers, stopping notifications" << endl;
453 void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest)
455 // This function handles slow response case
456 LightResource* lightPtr = (LightResource*) param;
457 // Induce a case for slow response by using sleep
458 std::cout << "SLOW response" << std::endl;
461 auto pResponse = std::make_shared<OC::OCResourceResponse>();
462 pResponse->setRequestHandle(pRequest->getRequestHandle());
463 pResponse->setResourceHandle(pRequest->getResourceHandle());
464 pResponse->setResourceRepresentation(lightPtr->get());
465 pResponse->setErrorCode(200);
466 pResponse->setResponseResult(OC_EH_OK);
468 // Set the slow response flag back to false
469 isSlowResponse = false;
470 OCPlatform::sendResponse(pResponse);
476 std::cout << std::endl;
477 std::cout << "Usage : simpleserver <value>\n";
478 std::cout << " Default - Non-secure resource and notify all observers\n";
479 std::cout << " 1 - Non-secure resource and notify list of observers\n\n";
480 std::cout << " 2 - Secure resource and notify all observers\n";
481 std::cout << " 3 - Secure resource and notify list of observers\n\n";
482 std::cout << " 4 - Non-secure resource, GET slow response, notify all observers\n";
485 static FILE* client_open(const char* /*path*/, const char *mode)
487 return fopen("./oic_svr_db_server.json", mode);
490 int main(int argc, char* argv[])
493 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
497 isListOfObservers = false;
502 int value = atoi(argv[1]);
506 isListOfObservers = true;
510 isListOfObservers = false;
514 isListOfObservers = true;
518 isSlowResponse = true;
529 // Create PlatformConfig object
531 OC::ServiceType::InProc,
532 OC::ModeType::Server,
533 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
534 0, // Uses randomly available port
535 OC::QualityOfService::LowQos,
539 OCPlatform::Configure(cfg);
542 // Create the instance of the resource class
543 // (in this case instance of class 'LightResource').
544 LightResource myLight;
546 // Invoke createResource function of class light.
547 myLight.createResource();
548 std::cout << "Created resource." << std::endl;
550 myLight.addType(std::string("core.brightlight"));
551 myLight.addInterface(std::string(LINK_INTERFACE));
552 std::cout << "Added Interface and Type" << std::endl;
555 // A condition variable will free the mutex it is given, then do a non-
556 // intensive block until 'notify' is called on it. In this case, since we
557 // don't ever call cv.notify, this should be a non-processor intensive version
560 std::condition_variable cv;
561 std::unique_lock<std::mutex> lock(blocker);
562 std::cout <<"Waiting" << std::endl;
563 cv.wait(lock, []{return false;});
565 catch(OCException &e)
567 std::cout << "OCException in main : " << e.what() << endl;
570 // No explicit call to stop the platform.
571 // When OCPlatform::destructor is invoked, internally we do platform cleanup