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 "OCPlatform.h"
35 namespace PH = std::placeholders;
38 void * ChangeLightRepresentation (void *param);
40 // Specifies where to notify all observers or list of observers
41 // 0 - notifies all observers
42 // 1 - notifies list of observers
43 int isListOfObservers = 0;
45 // Forward declaring the entityHandler
47 /// This class represents a single resource named 'lightResource'. This resource has
48 /// two simple properties named 'state' and 'power'
54 /// Access this property from a TB client
58 std::string m_lightUri;
59 OCResourceHandle m_resourceHandle;
60 OCRepresentation m_lightRep;
61 ObservationIds m_interestedObservers;
65 LightResource(PlatformConfig& cfg)
66 :m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
67 // Initialize representation
68 m_lightRep.setUri(m_lightUri);
70 m_lightRep.setValue("state", m_state);
71 m_lightRep.setValue("power", m_power);
72 m_lightRep.setValue("name", m_name);
75 /* Note that this does not need to be a member function: for classes you do not have
76 access to, you can accomplish this with a free function: */
78 /// This function internally calls registerResource API.
81 std::string resourceURI = m_lightUri; // URI of the resource
82 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
83 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
85 // OCResourceProperty is defined ocstack.h
86 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
88 EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1, PH::_2);
90 // This will internally create and register the resource.
91 OCStackResult result = OCPlatform::registerResource(
92 m_resourceHandle, resourceURI, resourceTypeName,
93 resourceInterface, cb, resourceProperty);
95 if (OC_STACK_OK != result)
97 cout << "Resource creation was unsuccessful\n";
101 OCStackResult createResource1()
103 std::string resourceURI = "/a/light1"; // URI of the resource
104 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
105 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
107 // OCResourceProperty is defined ocstack.h
108 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
110 EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1, PH::_2);
112 OCResourceHandle resHandle;
114 // This will internally create and register the resource.
115 OCStackResult result = OCPlatform::registerResource(
116 resHandle, resourceURI, resourceTypeName,
117 resourceInterface, cb, resourceProperty);
119 if (OC_STACK_OK != result)
121 cout << "Resource creation was unsuccessful\n";
127 OCResourceHandle getHandle()
129 return m_resourceHandle;
132 // Puts representation.
133 // Gets values from the representation and
134 // updates the internal state
135 void put(OCRepresentation& rep)
138 if (rep.getValue("state", m_state))
140 cout << "\t\t\t\t" << "state: " << m_state << endl;
144 cout << "\t\t\t\t" << "state not found in the representation" << endl;
147 if (rep.getValue("power", m_power))
149 cout << "\t\t\t\t" << "power: " << m_power << endl;
153 cout << "\t\t\t\t" << "power not found in the representation" << endl;
158 cout << e.what() << endl;
163 // Post representation.
164 // Post can create new resource or simply act like put.
165 // Gets values from the representation and
166 // updates the internal state
167 OCRepresentation post(OCRepresentation& rep)
169 static int first = 1;
171 std::cout << "In POST\n";
173 // for the first time it tries to create a resource
176 std::cout << "In POST/First\n";
180 if(OC_STACK_OK == createResource1())
182 std::cout << "Created a new resource\n";
184 OCRepresentation rep1;
185 rep1.setValue("createduri", std::string("/a/light1"));
191 // from second time onwards it just puts
197 // gets the updated representation.
198 // Updates the representation with latest internal state before
200 OCRepresentation get()
202 m_lightRep.setValue("state", m_state);
203 m_lightRep.setValue("power", m_power);
208 void addType(const std::string& type) const
210 OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
211 if (OC_STACK_OK != result)
213 cout << "Binding TypeName to Resource was unsuccessful\n";
217 void addInterface(const std::string& interface) const
219 OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
220 if (OC_STACK_OK != result)
222 cout << "Binding TypeName to Resource was unsuccessful\n";
227 // This is just a sample implementation of entity handler.
228 // Entity handler can be implemented in several ways by the manufacturer
229 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
231 cout << "\tIn Server CPP entity handler:\n";
235 // Get the request type and request flag
236 std::string requestType = request->getRequestType();
237 int requestFlag = request->getRequestHandlerFlag();
239 if(requestFlag & RequestHandlerFlag::InitFlag)
241 cout << "\t\trequestFlag : Init\n";
243 // entity handler to perform resource initialization operations
245 if(requestFlag & RequestHandlerFlag::RequestFlag)
247 cout << "\t\trequestFlag : Request\n";
249 // If the request type is GET
250 if(requestType == "GET")
252 cout << "\t\t\trequestType : GET\n";
257 response->setErrorCode(200);
259 response->setResourceRepresentation(get());
262 else if(requestType == "PUT")
264 cout << "\t\t\trequestType : PUT\n";
266 OCRepresentation rep = request->getResourceRepresentation();
268 // Do related operations related to PUT request
270 // Update the lightResource
276 response->setErrorCode(200);
278 response->setResourceRepresentation(get());
282 else if(requestType == "POST")
284 cout << "\t\t\trequestType : POST\n";
286 OCRepresentation rep = request->getResourceRepresentation();
288 // Do related operations related to POST request
290 OCRepresentation rep_post = post(rep);
295 response->setErrorCode(200);
297 response->setResourceRepresentation(rep_post);
300 // POST request operations
302 else if(requestType == "DELETE")
304 // DELETE request operations
308 if(requestFlag & RequestHandlerFlag::ObserverFlag)
310 ObservationInfo observationInfo = request->getObservationInfo();
311 if(ObserveAction::ObserveRegister == observationInfo.action)
313 m_interestedObservers.push_back(observationInfo.obsId);
315 else if(ObserveAction::ObserveUnregister == observationInfo.action)
317 m_interestedObservers.erase(std::remove(
318 m_interestedObservers.begin(),
319 m_interestedObservers.end(),
320 observationInfo.obsId),
321 m_interestedObservers.end());
326 cout << "\t\trequestFlag : Observer\n";
328 static int startedThread = 0;
330 // Observation happens on a different thread in ChangeLightRepresentation function.
331 // If we have not created the thread already, we will create one here.
334 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)this);
341 std::cout << "Request invalid" << std::endl;
349 // ChangeLightRepresentaion is an observation function,
350 // which notifies any changes to the resource to stack
351 // via notifyObservers
352 void * ChangeLightRepresentation (void *param)
354 LightResource* lightPtr = (LightResource*) param;
356 // This function continuously monitors for the changes
363 // If under observation if there are any changes to the light resource
364 // we call notifyObservors
366 // For demostration we are changing the power value and notifying.
367 lightPtr->m_power += 10;
369 cout << "\nPower updated to : " << lightPtr->m_power << endl;
370 cout << "Notifying observers with resource handle: " << lightPtr->getHandle() << endl;
372 OCStackResult result = OC_STACK_OK;
374 if(isListOfObservers)
376 std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
378 resourceResponse->setErrorCode(200);
379 resourceResponse->setResourceRepresentation(lightPtr->get(), DEFAULT_INTERFACE);
381 result = OCPlatform::notifyListOfObservers(
382 lightPtr->getHandle(),
383 lightPtr->m_interestedObservers,
385 OC::QualityOfService::HighQos);
389 result = OCPlatform::notifyAllObservers(lightPtr->getHandle(),
390 OC::QualityOfService::HighQos);
393 if(OC_STACK_NO_OBSERVERS == result)
395 cout << "No More observers, stopping notifications" << endl;
406 std::cout << std::endl;
407 std::cout << "Usage : simplserver <isListOfObservers>\n";
408 std::cout << " ObserveType : 0 - Observe All\n";
409 std::cout << " ObserveType : 1 - Observe List of observers\n\n";
413 int main(int argc, char* argv[1])
419 isListOfObservers = 0;
423 int value = atoi(argv[1]);
425 isListOfObservers = 1;
427 isListOfObservers = 0;
434 // Create PlatformConfig object
436 OC::ServiceType::InProc,
437 OC::ModeType::Server,
438 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
439 0, // Uses randomly available port
440 OC::QualityOfService::LowQos
443 OCPlatform::Configure(cfg);
447 // Create the instance of the resource class (in this case instance of class 'LightResource').
448 LightResource myLight(cfg);
450 // Invoke createResource function of class light.
451 myLight.createResource();
453 myLight.addType(std::string("core.brightlight"));
454 myLight.addInterface(std::string("oc.mi.ll"));
466 // No explicit call to stop the platform.
467 // When OCPlatform destructor is invoked, internally we do platform cleanup