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 /// This class represents a single resource named 'lightResource'. This resource has
46 /// two simple properties named 'state' and 'power'
52 /// Access this property from a TB client
56 std::string m_lightUri;
57 OCResourceHandle m_resourceHandle;
58 OCRepresentation m_lightRep;
59 ObservationIds m_interestedObservers;
63 LightResource(PlatformConfig& cfg)
64 :m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
65 // Initialize representation
66 m_lightRep.setUri(m_lightUri);
68 m_lightRep.setValue("state", m_state);
69 m_lightRep.setValue("power", m_power);
70 m_lightRep.setValue("name", m_name);
73 /* Note that this does not need to be a member function: for classes you do not have
74 access to, you can accomplish this with a free function: */
76 /// This function internally calls registerResource API.
79 std::string resourceURI = m_lightUri; // URI of the resource
80 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
81 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
83 // OCResourceProperty is defined ocstack.h
84 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
86 EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
88 // This will internally create and register the resource.
89 OCStackResult result = OCPlatform::registerResource(
90 m_resourceHandle, resourceURI, resourceTypeName,
91 resourceInterface, cb, resourceProperty);
93 if (OC_STACK_OK != result)
95 cout << "Resource creation was unsuccessful\n";
99 OCStackResult createResource1()
101 std::string resourceURI = "/a/light1"; // URI of the resource
102 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
103 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
105 // OCResourceProperty is defined ocstack.h
106 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
108 EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
110 OCResourceHandle resHandle;
112 // This will internally create and register the resource.
113 OCStackResult result = OCPlatform::registerResource(
114 resHandle, resourceURI, resourceTypeName,
115 resourceInterface, cb, resourceProperty);
117 if (OC_STACK_OK != result)
119 cout << "Resource creation was unsuccessful\n";
125 OCResourceHandle getHandle()
127 return m_resourceHandle;
130 // Puts representation.
131 // Gets values from the representation and
132 // updates the internal state
133 void put(OCRepresentation& rep)
136 if (rep.getValue("state", m_state))
138 cout << "\t\t\t\t" << "state: " << m_state << endl;
142 cout << "\t\t\t\t" << "state not found in the representation" << endl;
145 if (rep.getValue("power", m_power))
147 cout << "\t\t\t\t" << "power: " << m_power << endl;
151 cout << "\t\t\t\t" << "power not found in the representation" << endl;
156 cout << e.what() << endl;
161 // Post representation.
162 // Post can create new resource or simply act like put.
163 // Gets values from the representation and
164 // updates the internal state
165 OCRepresentation post(OCRepresentation& rep)
167 static int first = 1;
169 std::cout << "In POST\n";
171 // for the first time it tries to create a resource
174 std::cout << "In POST/First\n";
178 if(OC_STACK_OK == createResource1())
180 std::cout << "Created a new resource\n";
181 OCRepresentation rep1;
182 rep1.setValue("createduri", std::string("/a/light1"));
188 // from second time onwards it just puts
194 // gets the updated representation.
195 // Updates the representation with latest internal state before
197 OCRepresentation get()
199 m_lightRep.setValue("state", m_state);
200 m_lightRep.setValue("power", m_power);
205 void addType(const std::string& type) const
207 OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
208 if (OC_STACK_OK != result)
210 cout << "Binding TypeName to Resource was unsuccessful\n";
214 void addInterface(const std::string& interface) const
216 OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
217 if (OC_STACK_OK != result)
219 cout << "Binding TypeName to Resource was unsuccessful\n";
225 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
227 auto pResponse = std::make_shared<OC::OCResourceResponse>();
228 pResponse->setRequestHandle(pRequest->getRequestHandle());
229 pResponse->setResourceHandle(pRequest->getResourceHandle());
230 pResponse->setResourceRepresentation(get());
231 pResponse->setErrorCode(200);
232 pResponse->setResponseResult(OC_EH_OK);
234 return OCPlatform::sendResponse(pResponse);
237 OCStackResult sendPostResponse(std::shared_ptr<OCResourceRequest> pRequest)
239 auto pResponse = std::make_shared<OC::OCResourceResponse>();
240 pResponse->setRequestHandle(pRequest->getRequestHandle());
241 pResponse->setResourceHandle(pRequest->getResourceHandle());
243 OCRepresentation rep = pRequest->getResourceRepresentation();
244 OCRepresentation rep_post = post(rep);
246 pResponse->setResourceRepresentation(rep_post);
247 pResponse->setErrorCode(200);
248 pResponse->setResponseResult(OC_EH_OK);
250 return OCPlatform::sendResponse(pResponse);
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;
262 // Get the request type and request flag
263 std::string requestType = request->getRequestType();
264 int requestFlag = request->getRequestHandlerFlag();
266 if(requestFlag & RequestHandlerFlag::InitFlag)
268 cout << "\t\trequestFlag : Init\n";
269 // entity handler to perform resource initialization operations
271 if(requestFlag & RequestHandlerFlag::RequestFlag)
273 cout << "\t\trequestFlag : Request\n";
275 // If the request type is GET
276 if(requestType == "GET")
278 cout << "\t\t\trequestType : GET\n";
279 if(OC_STACK_OK == sendResponse(request))
284 else if(requestType == "PUT")
286 cout << "\t\t\trequestType : PUT\n";
288 OCRepresentation rep = request->getResourceRepresentation();
289 // Do related operations related to PUT request
290 // Update the lightResource
292 if(OC_STACK_OK == sendResponse(request))
297 else if(requestType == "POST")
299 cout << "\t\t\trequestType : POST\n";
300 if(OC_STACK_OK == sendPostResponse(request))
305 else if(requestType == "DELETE")
307 // DELETE request operations
311 if(requestFlag & RequestHandlerFlag::ObserverFlag)
313 ObservationInfo observationInfo = request->getObservationInfo();
314 if(ObserveAction::ObserveRegister == observationInfo.action)
316 m_interestedObservers.push_back(observationInfo.obsId);
318 else if(ObserveAction::ObserveUnregister == observationInfo.action)
320 m_interestedObservers.erase(std::remove(
321 m_interestedObservers.begin(),
322 m_interestedObservers.end(),
323 observationInfo.obsId),
324 m_interestedObservers.end());
329 cout << "\t\trequestFlag : Observer\n";
331 static int startedThread = 0;
333 // Observation happens on a different thread in ChangeLightRepresentation function.
334 // If we have not created the thread already, we will create one here.
337 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)this);
345 std::cout << "Request invalid" << std::endl;
353 // ChangeLightRepresentaion is an observation function,
354 // which notifies any changes to the resource to stack
355 // via notifyObservers
356 void * ChangeLightRepresentation (void *param)
358 LightResource* lightPtr = (LightResource*) param;
360 // This function continuously monitors for the changes
367 // If under observation if there are any changes to the light resource
368 // we call notifyObservors
370 // For demostration we are changing the power value and notifying.
371 lightPtr->m_power += 10;
373 cout << "\nPower updated to : " << lightPtr->m_power << endl;
374 cout << "Notifying observers with resource handle: " << lightPtr->getHandle() << endl;
376 OCStackResult result = OC_STACK_OK;
378 if(isListOfObservers)
380 std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
382 resourceResponse->setErrorCode(200);
383 resourceResponse->setResourceRepresentation(lightPtr->get(), DEFAULT_INTERFACE);
385 result = OCPlatform::notifyListOfObservers(
386 lightPtr->getHandle(),
387 lightPtr->m_interestedObservers,
389 OC::QualityOfService::HighQos);
393 result = OCPlatform::notifyAllObservers(lightPtr->getHandle(),
394 OC::QualityOfService::HighQos);
397 if(OC_STACK_NO_OBSERVERS == result)
399 cout << "No More observers, stopping notifications" << endl;
410 std::cout << std::endl;
411 std::cout << "Usage : simplserver <isListOfObservers>\n";
412 std::cout << " ObserveType : 0 - Observe All\n";
413 std::cout << " ObserveType : 1 - Observe List of observers\n\n";
417 int main(int argc, char* argv[1])
423 isListOfObservers = 0;
427 int value = atoi(argv[1]);
429 isListOfObservers = 1;
431 isListOfObservers = 0;
438 // Create PlatformConfig object
440 OC::ServiceType::InProc,
441 OC::ModeType::Server,
442 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
443 0, // Uses randomly available port
444 OC::QualityOfService::LowQos
447 OCPlatform::Configure(cfg);
451 // Create the instance of the resource class (in this case instance of class 'LightResource').
452 LightResource myLight(cfg);
454 // Invoke createResource function of class light.
455 myLight.createResource();
457 myLight.addType(std::string("core.brightlight"));
458 myLight.addInterface(std::string("oc.mi.ll"));
470 // No explicit call to stop the platform.
471 // When OCPlatform destructor is invoked, internally we do platform cleanup