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"
38 // Specifies where to notify all observers or list of observers
39 // 0 - notifies all observers
40 // 1 - notifies list of observers
41 int isListOfObservers = 0;
43 // Forward declaring the entityHandler
44 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
46 /// This class represents a single resource named 'lightResource'. This resource has
47 /// 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(): m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
64 // Initialize representation
65 m_lightRep.setUri(m_lightUri);
67 m_lightRep.setValue("state", m_state);
68 m_lightRep.setValue("power", m_power);
69 m_lightRep.setValue("name", m_name);
72 /* Note that this does not need to be a member function: for classes you do not have
73 access to, you can accomplish this with a free function: */
75 /// This function internally calls registerResource API.
76 void createResource(OC::OCPlatform& platform)
78 std::string resourceURI = m_lightUri; // URI of the resource
79 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
80 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
82 // OCResourceProperty is defined ocstack.h
83 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
85 // This will internally create and register the resource.
86 OCStackResult result = platform.registerResource(
87 m_resourceHandle, resourceURI, resourceTypeName,
88 resourceInterface, &entityHandler, resourceProperty);
90 if (OC_STACK_OK != result)
92 cout << "Resource creation was unsuccessful\n";
96 OCResourceHandle getHandle()
98 return m_resourceHandle;
101 // Puts representation.
102 // Gets values from the representation and
103 // updates the internal state
104 void put(OCRepresentation& rep)
107 if (rep.getValue("state", m_state))
109 cout << "\t\t\t\t" << "state: " << m_state << endl;
113 cout << "\t\t\t\t" << "state not found in the representation" << endl;
116 if (rep.getValue("power", m_power))
118 cout << "\t\t\t\t" << "power: " << m_power << endl;
122 cout << "\t\t\t\t" << "power not found in the representation" << endl;
127 cout << e.what() << endl;
132 // gets the updated representation.
133 // Updates the representation with latest internal state before
135 OCRepresentation get()
137 m_lightRep.setValue("state", m_state);
138 m_lightRep.setValue("power", m_power);
143 void addType(const OC::OCPlatform& platform, const std::string& type) const
145 OCStackResult result = platform.bindTypeToResource(m_resourceHandle, type);
146 if (OC_STACK_OK != result)
148 cout << "Binding TypeName to Resource was unsuccessful\n";
152 void addInterface(const OC::OCPlatform& platform, const std::string& interface) const
154 OCStackResult result = platform.bindInterfaceToResource(m_resourceHandle, interface);
155 if (OC_STACK_OK != result)
157 cout << "Binding TypeName to Resource was unsuccessful\n";
162 // Create the instance of the resource class (in this case instance of class 'LightResource').
163 LightResource myLight;
165 // ChangeLightRepresentaion is an observation function,
166 // which notifies any changes to the resource to stack
167 // via notifyObservers
168 void * ChangeLightRepresentation (void *param)
170 // This function continuously monitors for the changes
177 // If under observation if there are any changes to the light resource
178 // we call notifyObservors
180 // For demostration we are changing the power value and notifying.
181 myLight.m_power += 10;
183 cout << "\nPower updated to : " << myLight.m_power << endl;
184 cout << "Notifying observers with resource handle: " << myLight.getHandle() << endl;
186 OCStackResult result = OC_STACK_OK;
188 if(isListOfObservers)
190 std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
192 resourceResponse->setErrorCode(200);
193 resourceResponse->setResourceRepresentation(myLight.get(), DEFAULT_INTERFACE);
195 result = OCPlatform::notifyListOfObservers( myLight.getHandle(),
196 myLight.m_interestedObservers,
201 result = OCPlatform::notifyAllObservers(myLight.getHandle());
204 if(OC_STACK_NO_OBSERVERS == result)
206 cout << "No More observers, stopping notifications" << endl;
215 // This is just a sample implementation of entity handler.
216 // Entity handler can be implemented in several ways by the manufacturer
217 void entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
219 cout << "\tIn Server CPP entity handler:\n";
223 // Get the request type and request flag
224 std::string requestType = request->getRequestType();
225 int requestFlag = request->getRequestHandlerFlag();
227 if(requestFlag & RequestHandlerFlag::InitFlag)
229 cout << "\t\trequestFlag : Init\n";
231 // entity handler to perform resource initialization operations
233 if(requestFlag & RequestHandlerFlag::RequestFlag)
235 cout << "\t\trequestFlag : Request\n";
237 // If the request type is GET
238 if(requestType == "GET")
240 cout << "\t\t\trequestType : GET\n";
245 response->setErrorCode(200);
247 response->setResourceRepresentation(myLight.get());
250 else if(requestType == "PUT")
252 cout << "\t\t\trequestType : PUT\n";
254 OCRepresentation rep = request->getResourceRepresentation();
256 // Do related operations related to PUT request
258 // Update the lightResource
264 response->setErrorCode(200);
266 response->setResourceRepresentation(myLight.get());
270 else if(requestType == "POST")
272 // POST request operations
274 else if(requestType == "DELETE")
276 // DELETE request operations
279 if(requestFlag & RequestHandlerFlag::ObserverFlag)
281 ObservationInfo observationInfo = request->getObservationInfo();
282 if(ObserveAction::ObserveRegister == observationInfo.action)
284 myLight.m_interestedObservers.push_back(observationInfo.obsId);
286 else if(ObserveAction::ObserveUnregister == observationInfo.action)
288 myLight.m_interestedObservers.erase(std::remove(
289 myLight.m_interestedObservers.begin(),
290 myLight.m_interestedObservers.end(),
291 observationInfo.obsId),
292 myLight.m_interestedObservers.end());
297 cout << "\t\trequestFlag : Observer\n";
300 static int startedThread = 0;
302 // Observation happens on a different thread in ChangeLightRepresentation function.
303 // If we have not created the thread already, we will create one here.
306 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
313 std::cout << "Request invalid" << std::endl;
319 std::cout << std::endl;
320 std::cout << "Usage : simplserver <isListOfObservers>\n";
321 std::cout << " ObserveType : 0 - Observe All\n";
322 std::cout << " ObserveType : 1 - Observe List of observers\n\n";
326 int main(int argc, char* argv[1])
332 isListOfObservers = 0;
336 int value = atoi(argv[1]);
338 isListOfObservers = 1;
340 isListOfObservers = 0;
347 // Create PlatformConfig object
349 OC::ServiceType::InProc,
350 OC::ModeType::Server,
351 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
352 0, // Uses randomly available port
353 OC::QualityOfService::NonConfirmable
356 // Create a OCPlatform instance.
357 // Note: Platform creation is synchronous call.
360 OCPlatform platform(cfg);
362 // Invoke createResource function of class light.
363 myLight.createResource(platform);
365 myLight.addType(platform, std::string("core.brightlight"));
366 myLight.addInterface(platform, std::string("oc.mi.ll"));
378 // No explicit call to stop the platform.
379 // When OCPlatform destructor is invoked, internally we do platform cleanup