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