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