Fix Jira Difect IOT-505
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / mqtt-fan / src / fanserver.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics 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 #include <pthread.h>
28 #include <cpluff.h>
29
30 #include "OCPlatform.h"
31 #include "OCApi.h"
32 #include "../lib/mosquitto.h"
33 #include "fanserver.h"
34
35 #include <time.h>
36
37
38 using namespace OC;
39 using namespace std;
40 namespace PH = std::placeholders;
41
42 time_t timer;                // Define the timer
43 struct tm *tblock;           // Define a structure for time block
44
45
46 int gObservation = 0;
47 void *ChangeFanRepresentation (void *param);
48 void *handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest);
49
50 // Specifies where to notify all observers or list of observers
51 // false: notifies all observers
52 // true: notifies list of observers
53 bool isListOfObservers = false;
54
55 // Specifies secure or non-secure
56 // false: non-secure resource
57 // true: secure resource
58 bool isSecure = false;
59
60 /// Specifies whether Entity handler is going to do slow response or not
61 bool isSlowResponse = false;
62
63 // Forward declaring the entityHandler
64 /// This class represents a single resource named 'fanResource'. This resource has
65 /// two simple properties named 'state' and 'power'
66 typedef struct plugin_data_t plugin_data_t;
67
68 struct plugin_data_t
69 {
70     cp_context_t *ctx;
71     pthread_t m_thread;                                 // 2
72     void *str;
73     bool flag;
74 };
75
76 struct mosquitto *myMosquitto;
77 class FanResource
78 {
79     public:
80         /// Access this property from a TB client
81         std::string m_name;
82         bool m_state;
83         int m_power;
84         int m_health;
85         std::string m_fanUri;
86         OCResourceHandle m_resourceHandle;
87         OCRepresentation m_fanRep;
88         ObservationIds m_interestedObservers;
89
90     public:
91         /// Constructor
92         FanResource(): m_name("John's fan"), m_state(false), m_power(0), m_fanUri("/a/fan")
93         {
94             // Initialize representation
95             m_fanRep.setUri(m_fanUri);
96
97             m_fanRep.setValue("state", m_state);
98             m_fanRep.setValue("power", m_power);
99             m_fanRep.setValue("name", m_name);
100         }
101
102
103         /* Note that this does not need to be a member function: for classes you do not have
104         access to, you can accomplish this with a free function: */
105
106         /// This function internally calls registerResource API.
107         void createResource()
108         {
109             std::string resourceURI = m_fanUri; // URI of the resource
110             std::string resourceTypeName = "core.fan"; // resource type name. In this case, it is fan
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(&FanResource::entityHandler, this, PH::_1);
117
118             // This will internally create and register the resource.
119             OCStackResult result = OCPlatform::registerResource(
120                                        m_resourceHandle, resourceURI, resourceTypeName,
121                                        resourceInterface, cb, resourceProperty);
122
123             if (OC_STACK_OK != result)
124             {
125                 cout << "Resource creation was unsuccessful\n";
126             }
127         }
128
129         OCStackResult createResource1()
130         {
131             std::string resourceURI = "/a/fan1"; // URI of the resource
132             std::string resourceTypeName = "core.fan"; // resource type name. In this case, it is fan
133             std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
134
135             // OCResourceProperty is defined ocstack.h
136             uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
137
138             EntityHandler cb = std::bind(&FanResource::entityHandler, this, PH::_1);
139
140             OCResourceHandle resHandle;
141
142             // This will internally create and register the resource.
143             OCStackResult result = OCPlatform::registerResource(
144                                        resHandle, resourceURI, resourceTypeName,
145                                        resourceInterface, cb, resourceProperty);
146
147             if (OC_STACK_OK != result)
148             {
149                 cout << "Resource creation was unsuccessful\n";
150             }
151
152             return result;
153         }
154
155         OCResourceHandle getHandle()
156         {
157             return m_resourceHandle;
158         }
159
160         // Puts representation.
161         // Gets values from the representation and
162         // updates the internal state
163         void put(OCRepresentation &rep)
164         {
165             try
166             {
167                 if (rep.getValue("state", m_state))
168                 {
169                     cout << "\t\t\t\t" << "state: " << m_state << endl;
170                 }
171                 else
172                 {
173                     cout << "\t\t\t\t" << "state not found in the representation" << endl;
174                 }
175
176                 if (rep.getValue("power", m_power))
177                 {
178                     cout << "\t\t\t\t" << "power: " << m_power << endl;
179                     if (m_power == 1)
180                     {
181                         mosquitto_publish(myMosquitto, NULL, "actuators/fan", 32, "onfan", 0, true);
182                     }
183                     else
184                     {
185                         mosquitto_publish(myMosquitto, NULL, "actuators/fan", 32, "offfan", 0, true);
186                     }
187                 }
188                 else
189                 {
190                     cout << "\t\t\t\t" << "power not found in the representation" << endl;
191                 }
192             }
193             catch (exception &e)
194             {
195                 cout << e.what() << endl;
196             }
197
198         }
199
200         // Post representation.
201         // Post can create new resource or simply act like put.
202         // Gets values from the representation and
203         // updates the internal state
204         OCRepresentation post(OCRepresentation &rep)
205         {
206             static int first = 1;
207
208             // for the first time it tries to create a resource
209             if (first)
210             {
211                 first = 0;
212
213                 if (OC_STACK_OK == createResource1())
214                 {
215                     OCRepresentation rep1;
216                     rep1.setValue("createduri", std::string("/a/fan1"));
217
218                     return rep1;
219                 }
220             }
221
222             // from second time onwards it just puts
223             put(rep);
224             return get();
225         }
226
227
228         // gets the updated representation.
229         // Updates the representation with latest internal state before
230         // sending out.
231         OCRepresentation get()
232         {
233             m_fanRep.setValue("state", m_state);
234             m_fanRep.setValue("power", m_power);
235
236             return m_fanRep;
237         }
238
239
240         void addType(const std::string &type) const
241         {
242             OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
243             if (OC_STACK_OK != result)
244             {
245                 cout << "Binding TypeName to Resource was unsuccessful\n";
246             }
247         }
248
249         void addInterface(const std::string &interface) const
250         {
251             OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
252             if (OC_STACK_OK != result)
253             {
254                 cout << "Binding TypeName to Resource was unsuccessful\n";
255             }
256         }
257
258     private:
259         // This is just a sample implementation of entity handler.
260         // Entity handler can be implemented in several ways by the manufacturer
261         OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
262         {
263             cout << "\tIn Server CPP entity handler:\n";
264             OCEntityHandlerResult ehResult = OC_EH_ERROR;
265             if (request)
266             {
267                 // Get the request type and request flag
268                 std::string requestType = request->getRequestType();
269                 int requestFlag = request->getRequestHandlerFlag();
270
271                 if (requestFlag & RequestHandlerFlag::InitFlag)
272                 {
273                     cout << "\t\trequestFlag : Init\n";
274
275                     // entity handler to perform resource initialization operations
276                 }
277                 if (requestFlag & RequestHandlerFlag::RequestFlag)
278                 {
279                     cout << "\t\trequestFlag : Request   ===  Handle by FanServer\n";
280                     auto pResponse = std::make_shared<OC::OCResourceResponse>();
281                     pResponse->setRequestHandle(request->getRequestHandle());
282                     pResponse->setResourceHandle(request->getResourceHandle());
283
284                     // If the request type is GET
285                     if (requestType == "GET")
286                     {
287                         cout << "\t\t\trequestType : GET\n";
288                         if (isSlowResponse) // Slow response case
289                         {
290                             static int startedThread = 0;
291                             if (!startedThread)
292                             {
293                                 std::thread t(handleSlowResponse, (void *)this, request);
294                                 startedThread = 1;
295                                 t.detach();
296                             }
297                             ehResult = OC_EH_SLOW;
298                         }
299                         else // normal response case.
300                         {
301                             pResponse->setErrorCode(200);
302                             pResponse->setResponseResult(OC_EH_OK);
303                             pResponse->setResourceRepresentation(get());
304                             if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
305                             {
306                                 ehResult = OC_EH_OK;
307                             }
308                         }
309                     }
310                     else if (requestType == "PUT")
311                     {
312                         cout << "\t\t\trequestType : PUT\n";
313                         OCRepresentation rep = request->getResourceRepresentation();
314
315                         // Do related operations related to PUT request
316                         // Update the fanResource
317                         put(rep);
318                         pResponse->setErrorCode(200);
319                         pResponse->setResponseResult(OC_EH_OK);
320                         pResponse->setResourceRepresentation(get());
321                         if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
322                         {
323                             ehResult = OC_EH_OK;
324                         }
325                     }
326                     else if (requestType == "POST")
327                     {
328                         cout << "\t\t\trequestType : POST\n";
329
330                         OCRepresentation rep = request->getResourceRepresentation();
331
332                         // Do related operations related to POST request
333                         OCRepresentation rep_post = post(rep);
334                         pResponse->setResourceRepresentation(rep_post);
335                         pResponse->setErrorCode(200);
336                         if (rep_post.hasAttribute("createduri"))
337                         {
338                             pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
339                             pResponse->setNewResourceUri(rep_post.getValue<std::string>("createduri"));
340                         }
341
342                         if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
343                         {
344                             ehResult = OC_EH_OK;
345                         }
346                     }
347                     else if (requestType == "DELETE")
348                     {
349                         // DELETE request operations
350                     }
351                 }
352
353                 if (requestFlag & RequestHandlerFlag::ObserverFlag)
354                 {
355                     ObservationInfo observationInfo = request->getObservationInfo();
356                     if (ObserveAction::ObserveRegister == observationInfo.action)
357                     {
358                         m_interestedObservers.push_back(observationInfo.obsId);
359                     }
360                     else if (ObserveAction::ObserveUnregister == observationInfo.action)
361                     {
362                         m_interestedObservers.erase(std::remove(
363                                                         m_interestedObservers.begin(),
364                                                         m_interestedObservers.end(),
365                                                         observationInfo.obsId),
366                                                     m_interestedObservers.end());
367                     }
368
369                     pthread_t threadId;
370
371                     cout << "\t\trequestFlag : Observer\n";
372                     gObservation = 1;
373                     static int startedThread = 0;
374
375                     // Observation happens on a different thread in ChangeFanRepresentation function.
376                     // If we have not created the thread already, we will create one here.
377                     if (!startedThread)
378                     {
379                         pthread_create (&threadId, NULL, ChangeFanRepresentation, (void *)this);
380                         startedThread = 1;
381                     }
382                     ehResult = OC_EH_OK;
383                 }
384             }
385             else
386             {
387                 std::cout << "Request invalid" << std::endl;
388             }
389
390             return ehResult;
391         }
392 };
393
394 // Create the instance of the resource class (in this case instance of class 'FanResource').
395
396 // ChangeFanRepresentaion is an observation function,
397 // which notifies any changes to the resource to stack
398 // via notifyObservers
399 void *ChangeFanRepresentation (void *param)
400 {
401     FanResource *fanPtr = (FanResource *) param;
402
403     // This function continuously monitors for the changes
404     while (1)
405     {
406         sleep (5);
407
408         if (gObservation)
409         {
410             // If under observation if there are any changes to the fan resource
411             // we call notifyObservors
412             //
413             // For demostration we are changing the power value and notifying.
414             fanPtr->m_power += 10;
415
416             cout << "\nPower updated to : " << fanPtr->m_power << endl;
417             cout << "Notifying observers with resource handle: " << fanPtr->getHandle() << endl;
418
419             OCStackResult result = OC_STACK_OK;
420
421             if (isListOfObservers)
422             {
423                 std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
424
425                 resourceResponse->setErrorCode(200);
426                 resourceResponse->setResourceRepresentation(fanPtr->get(), DEFAULT_INTERFACE);
427
428                 result = OCPlatform::notifyListOfObservers(  fanPtr->getHandle(),
429                          fanPtr->m_interestedObservers,
430                          resourceResponse);
431             }
432             else
433             {
434                 result = OCPlatform::notifyAllObservers(fanPtr->getHandle());
435             }
436
437             if (OC_STACK_NO_OBSERVERS == result)
438             {
439                 cout << "No More observers, stopping notifications" << endl;
440                 gObservation = 0;
441             }
442         }
443     }
444
445     return NULL;
446 }
447
448 void *handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest)
449 {
450     // This function handles slow response case
451     FanResource *fanPtr = (FanResource *) param;
452     // Induce a case for slow response by using sleep
453     std::cout << "SLOW response" << std::endl;
454     sleep (10);
455
456     auto pResponse = std::make_shared<OC::OCResourceResponse>();
457     pResponse->setRequestHandle(pRequest->getRequestHandle());
458     pResponse->setResourceHandle(pRequest->getResourceHandle());
459     pResponse->setResourceRepresentation(fanPtr->get());
460     pResponse->setErrorCode(200);
461     pResponse->setResponseResult(OC_EH_OK);
462
463     // Set the slow response flag back to false
464     isSlowResponse = false;
465     OCPlatform::sendResponse(pResponse);
466     return NULL;
467 }
468
469
470 //int start_fanserver(void*)                // 1
471 void *start_fanserver(void *d)      // 2
472 {
473     // Create PlatformConfig object
474     PlatformConfig cfg
475     {
476         OC::ServiceType::InProc,
477         OC::ModeType::Both,
478         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
479         0,         // Uses randomly available port
480         OC::QualityOfService::LowQos
481     };
482
483     OCPlatform::Configure(cfg);
484
485     printf("start_fanserver [mosquitto] Null\n");
486     try
487     {
488         FanResource myFanResource;
489         mosquitto_lib_init();
490         myMosquitto = mosquitto_new("MQTT plug-in", true, NULL);
491         if (!myMosquitto)
492         {
493             printf("[mosquitto] Null\n");
494             printf("You need to install mqtt broker\n");
495         }
496         else
497         {
498             printf("Mosquitto is working\n");
499         }
500
501         if (mosquitto_connect(myMosquitto, "127.0.0.1", 1883, 60) != MOSQ_ERR_SUCCESS)
502         {
503             printf("Mosquitto Connection is failed.\n");
504             pthread_exit((void *)0);
505         }
506         printf("Mosquitto Connection is done.\n");
507         myFanResource.createResource();
508         // Get time of day
509         timer = time(NULL);
510         // Converts date/time to a structure
511         tblock = localtime(&timer);
512         // Output ASCII data/time
513         printf("FanReousrce reigishter time is: %s", asctime(tblock));
514         // Perform app tasks
515         while (true)
516         {
517             // some tasks
518         }
519     }
520     catch (OCException e)
521     {
522         //log(e.what());
523     }
524     // No explicit call to stop the platform.
525     // When OCPlatform destructor is invoked, internally we do platform cleanup
526     mosquitto_destroy(myMosquitto);
527
528     mosquitto_lib_cleanup();
529     printf("start_fanserver finish\n");
530     pthread_exit((void *)0);
531 }