Merge branch 'master' into extended-easysetup
[platform/upstream/iotivity.git] / resource / examples / simpleserverHQ.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 ///
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
24 ///
25
26 #include <functional>
27
28 #include <pthread.h>
29 #include <mutex>
30 #include <condition_variable>
31
32 #include "OCPlatform.h"
33 #include "OCApi.h"
34
35 #ifdef HAVE_WINDOWS_H
36 #include <windows.h>
37 #endif
38 #include "platform_features.h"
39
40 using namespace OC;
41 using namespace std;
42 namespace PH = std::placeholders;
43
44 int gObservation = 0;
45 void * ChangeLightRepresentation (void *param);
46
47 // Specifies where to notify all observers or list of observers
48 // 0 - notifies all observers
49 // 1 - notifies list of observers
50 int isListOfObservers = 0;
51
52 /// This class represents a single resource named 'lightResource'. This resource has
53 /// two simple properties named 'state' and 'power'
54
55 class LightResource
56 {
57
58 public:
59     /// Access this property from a TB client
60     std::string m_name;
61     bool m_state;
62     int m_power;
63     std::string m_lightUri;
64     OCResourceHandle m_resourceHandle;
65     OCRepresentation m_lightRep;
66     ObservationIds m_interestedObservers;
67
68 public:
69     /// Constructor
70     LightResource(PlatformConfig& /*cfg*/)
71         :m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") {
72         // Initialize representation
73         m_lightRep.setUri(m_lightUri);
74
75         m_lightRep.setValue("state", m_state);
76         m_lightRep.setValue("power", m_power);
77         m_lightRep.setValue("name", m_name);
78     }
79
80     /* Note that this does not need to be a member function: for classes you do not have
81     access to, you can accomplish this with a free function: */
82
83     /// This function internally calls registerResource API.
84     void createResource()
85     {
86         std::string resourceURI = m_lightUri; // URI of the resource
87         // resource type name. In this case, it is light
88         std::string resourceTypeName = "core.light";
89         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
90
91         // OCResourceProperty is defined ocstack.h
92         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
93
94         EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
95
96         // This will internally create and register the resource.
97         OCStackResult result = OCPlatform::registerResource(
98                                     m_resourceHandle, resourceURI, resourceTypeName,
99                                     resourceInterface, cb, resourceProperty);
100
101         if (OC_STACK_OK != result)
102         {
103             cout << "Resource creation was unsuccessful\n";
104         }
105     }
106
107     OCStackResult createResource1()
108     {
109         std::string resourceURI = "/a/light1"; // URI of the resource
110         std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
111         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
112
113         // OCResourceProperty is defined ocstack.h
114         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
115
116         EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1);
117
118         OCResourceHandle resHandle;
119
120         // This will internally create and register the resource.
121         OCStackResult result = OCPlatform::registerResource(
122                                     resHandle, resourceURI, resourceTypeName,
123                                     resourceInterface, cb, resourceProperty);
124
125         if (OC_STACK_OK != result)
126         {
127             cout << "Resource creation was unsuccessful\n";
128         }
129
130         return result;
131     }
132
133     OCResourceHandle getHandle()
134     {
135         return m_resourceHandle;
136     }
137
138     // Puts representation.
139     // Gets values from the representation and
140     // updates the internal state
141     void put(OCRepresentation& rep)
142     {
143         try {
144             if (rep.getValue("state", m_state))
145             {
146                 cout << "\t\t\t\t" << "state: " << m_state << endl;
147             }
148             else
149             {
150                 cout << "\t\t\t\t" << "state not found in the representation" << endl;
151             }
152
153             if (rep.getValue("power", m_power))
154             {
155                 cout << "\t\t\t\t" << "power: " << m_power << endl;
156             }
157             else
158             {
159                 cout << "\t\t\t\t" << "power not found in the representation" << endl;
160             }
161         }
162         catch (exception& e)
163         {
164             cout << e.what() << endl;
165         }
166
167     }
168
169     // Post representation.
170     // Post can create new resource or simply act like put.
171     // Gets values from the representation and
172     // updates the internal state
173     OCRepresentation post(OCRepresentation& rep)
174     {
175         static int first = 1;
176
177         std::cout << "In POST\n";
178
179         // for the first time it tries to create a resource
180         if(first)
181         {
182             std::cout << "In POST/First\n";
183
184             first = 0;
185
186             if(OC_STACK_OK == createResource1())
187             {
188                 std::cout << "Created a new resource\n";
189                 OCRepresentation rep1;
190                 rep1.setValue("createduri", std::string("/a/light1"));
191
192                 return rep1;
193             }
194         }
195
196         // from second time onwards it just puts
197         put(rep);
198         return get();
199     }
200
201
202     // gets the updated representation.
203     // Updates the representation with latest internal state before
204     // sending out.
205     OCRepresentation get()
206     {
207         m_lightRep.setValue("state", m_state);
208         m_lightRep.setValue("power", m_power);
209
210         return m_lightRep;
211     }
212
213     void addType(const std::string& type) const
214     {
215         OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
216         if (OC_STACK_OK != result)
217         {
218             cout << "Binding TypeName to Resource was unsuccessful\n";
219         }
220     }
221
222     void addInterface(const std::string& iface) const
223     {
224         OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
225         if (OC_STACK_OK != result)
226         {
227             cout << "Binding TypeName to Resource was unsuccessful\n";
228         }
229     }
230
231 private:
232
233 OCStackResult sendResponse(std::shared_ptr<OCResourceRequest> pRequest)
234 {
235     auto pResponse = std::make_shared<OC::OCResourceResponse>();
236     pResponse->setRequestHandle(pRequest->getRequestHandle());
237     pResponse->setResourceHandle(pRequest->getResourceHandle());
238     pResponse->setResourceRepresentation(get());
239     pResponse->setErrorCode(200);
240     pResponse->setResponseResult(OC_EH_OK);
241
242     return OCPlatform::sendResponse(pResponse);
243 }
244
245 OCStackResult sendPostResponse(std::shared_ptr<OCResourceRequest> pRequest)
246 {
247     auto pResponse = std::make_shared<OC::OCResourceResponse>();
248     pResponse->setRequestHandle(pRequest->getRequestHandle());
249     pResponse->setResourceHandle(pRequest->getResourceHandle());
250
251     OCRepresentation rep = pRequest->getResourceRepresentation();
252     OCRepresentation rep_post = post(rep);
253
254     pResponse->setResourceRepresentation(rep_post);
255     pResponse->setErrorCode(200);
256     pResponse->setResponseResult(OC_EH_OK);
257
258     return OCPlatform::sendResponse(pResponse);
259 }
260
261 // This is just a sample implementation of entity handler.
262 // Entity handler can be implemented in several ways by the manufacturer
263 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
264 {
265     cout << "\tIn Server CPP entity handler:\n";
266     OCEntityHandlerResult ehResult = OC_EH_ERROR;
267
268     if(request)
269     {
270         // Get the request type and request flag
271         std::string requestType = request->getRequestType();
272         int requestFlag = request->getRequestHandlerFlag();
273
274         if(requestFlag & RequestHandlerFlag::RequestFlag)
275         {
276             cout << "\t\trequestFlag : Request\n";
277
278             // If the request type is GET
279             if(requestType == "GET")
280             {
281                 cout << "\t\t\trequestType : GET\n";
282                 if(OC_STACK_OK == sendResponse(request))
283                 {
284                     ehResult = OC_EH_OK;
285                 }
286             }
287             else if(requestType == "PUT")
288             {
289                 cout << "\t\t\trequestType : PUT\n";
290
291                 OCRepresentation rep = request->getResourceRepresentation();
292                 // Do related operations related to PUT request
293                 // Update the lightResource
294                 put(rep);
295                 if(OC_STACK_OK == sendResponse(request))
296                 {
297                     ehResult = OC_EH_OK;
298                 }
299             }
300             else if(requestType == "POST")
301             {
302                 cout << "\t\t\trequestType : POST\n";
303                 if(OC_STACK_OK == sendPostResponse(request))
304                 {
305                     ehResult = OC_EH_OK;
306                 }
307             }
308             else if(requestType == "DELETE")
309             {
310                 // DELETE request operations
311             }
312         }
313
314         if(requestFlag & RequestHandlerFlag::ObserverFlag)
315         {
316             ObservationInfo observationInfo = request->getObservationInfo();
317             if(ObserveAction::ObserveRegister == observationInfo.action)
318             {
319                 m_interestedObservers.push_back(observationInfo.obsId);
320             }
321             else if(ObserveAction::ObserveUnregister == observationInfo.action)
322             {
323                 m_interestedObservers.erase(std::remove(
324                                                             m_interestedObservers.begin(),
325                                                             m_interestedObservers.end(),
326                                                             observationInfo.obsId),
327                                                             m_interestedObservers.end());
328             }
329
330             pthread_t threadId;
331
332             cout << "\t\trequestFlag : Observer\n";
333             gObservation = 1;
334             static int startedThread = 0;
335
336             // Observation happens on a different thread in ChangeLightRepresentation function.
337             // If we have not created the thread already, we will create one here.
338             if(!startedThread)
339             {
340                 pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)this);
341                 startedThread = 1;
342             }
343             ehResult = OC_EH_OK;
344         }
345     }
346     else
347     {
348         std::cout << "Request invalid" << std::endl;
349     }
350
351     return ehResult;
352 }
353
354 };
355
356 // ChangeLightRepresentaion is an observation function,
357 // which notifies any changes to the resource to stack
358 // via notifyObservers
359 void * ChangeLightRepresentation (void *param)
360 {
361     LightResource* lightPtr = (LightResource*) param;
362
363     // This function continuously monitors for the changes
364     while (1)
365     {
366         sleep (3);
367
368         if (gObservation)
369         {
370             // If under observation if there are any changes to the light resource
371             // we call notifyObservors
372             //
373             // For demostration we are changing the power value and notifying.
374             lightPtr->m_power += 10;
375
376             cout << "\nPower updated to : " << lightPtr->m_power << endl;
377             cout << "Notifying observers with resource handle: " << lightPtr->getHandle() << endl;
378
379             OCStackResult result = OC_STACK_OK;
380
381             if(isListOfObservers)
382             {
383                 std::shared_ptr<OCResourceResponse> resourceResponse =
384                             std::make_shared<OCResourceResponse>();
385
386                 resourceResponse->setErrorCode(200);
387                 resourceResponse->setResourceRepresentation(lightPtr->get(), DEFAULT_INTERFACE);
388
389                 result = OCPlatform::notifyListOfObservers(
390                                                             lightPtr->getHandle(),
391                                                             lightPtr->m_interestedObservers,
392                                                             resourceResponse,
393                                                             OC::QualityOfService::HighQos);
394             }
395             else
396             {
397                 result = OCPlatform::notifyAllObservers(lightPtr->getHandle(),
398                                                             OC::QualityOfService::HighQos);
399             }
400
401             if(OC_STACK_NO_OBSERVERS == result)
402             {
403                 cout << "No More observers, stopping notifications" << endl;
404                 gObservation = 0;
405             }
406         }
407     }
408
409     return NULL;
410 }
411
412 void PrintUsage()
413 {
414     std::cout << std::endl;
415     std::cout << "Usage : simpleserverHQ <ObserveType>\n";
416     std::cout << "   ObserveType : 0 - Observe All\n";
417     std::cout << "   ObserveType : 1 - Observe List of observers\n\n";
418 }
419
420
421 int main(int argc, char* argv[])
422 {
423     PrintUsage();
424
425     if (argc == 1)
426     {
427         isListOfObservers = 0;
428     }
429     else if (argc == 2)
430     {
431         int value = atoi(argv[1]);
432         if (value == 1)
433             isListOfObservers = 1;
434         else
435             isListOfObservers = 0;
436     }
437     else
438     {
439         return -1;
440     }
441
442     // Create PlatformConfig object
443     PlatformConfig cfg {
444         OC::ServiceType::InProc,
445         OC::ModeType::Server,
446         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
447         0,         // Uses randomly available port
448         OC::QualityOfService::LowQos
449     };
450
451     OCPlatform::Configure(cfg);
452
453     try
454     {
455         // Create the instance of the resource class
456         // (in this case instance of class 'LightResource').
457         LightResource myLight(cfg);
458
459         // Invoke createResource function of class light.
460         myLight.createResource();
461
462         myLight.addType(std::string("core.brightlight"));
463         myLight.addInterface(std::string(LINK_INTERFACE));
464
465         // A condition variable will free the mutex it is given, then do a non-
466         // intensive block until 'notify' is called on it.  In this case, since we
467         // don't ever call cv.notify, this should be a non-processor intensive version
468         // of while(true);
469         std::mutex blocker;
470         std::condition_variable cv;
471         std::unique_lock<std::mutex> lock(blocker);
472         cv.wait(lock, []{return false;});
473     }
474     catch(OCException& e)
475     {
476         oclog() << "Exception in main: "<< e.what();
477     }
478
479     // No explicit call to stop the platform.
480     // When OCPlatform destructor is invoked, internally we do platform cleanup
481
482     return 0;
483 }
484