Merge "Updated Makefile to support different build targets. Removed delegating ctors...
[platform/upstream/iotivity.git] / examples / roomserver.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 shows how one could create a resource (collection) with children.
23 ///
24
25 #include <functional>
26
27 #include <pthread.h>
28
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31
32 using namespace OC;
33 using namespace std;
34
35
36 // Forward declaring the entityHandler (room)
37 void entityHandlerRoom(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
38 void entityHandlerLight(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
39 void entityHandlerFan(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response);
40
41 class RoomResource
42 {
43 public:
44
45     // Room members
46     std::string m_roomUri;
47     std::string m_roomType;
48     std::string m_roomInterface1;
49     std::string m_roomInterface2;
50     std::string m_roomInterface3;
51     OCResourceHandle m_roomHandle;
52
53     // light members
54     bool m_lightState;
55     int m_lightColor;
56     std::string m_lightInterface;
57     std::string m_lightUri;
58     std::string m_lightType;
59     OCResourceHandle m_lightHandle;
60
61     // fan members
62     bool m_fanState;
63     int m_fanSpeed;
64     std::string m_fanInterface;
65     std::string m_fanUri;
66     std::string m_fanType;
67     OCResourceHandle m_fanHandle;
68
69 public:
70     /// Constructor
71     RoomResource(): m_lightState(false), m_lightColor(0), m_fanState(false), m_fanSpeed(0) 
72     {
73         m_roomUri = "/a/room"; // URI of the resource
74         m_roomType = "core.room"; // resource type name. In this case, it is light
75         m_roomInterface1 = DEFAULT_INTERFACE; // resource interface.
76         m_roomInterface2 = BATCH_INTERFACE; // resource interface.
77         m_roomInterface3 = LINK_INTERFACE; // resource interface.
78
79         m_lightUri = "/a/light"; // URI of the resource
80         m_lightType = "core.light"; // resource type name. In this case, it is light
81         m_lightInterface = DEFAULT_INTERFACE; // resource interface.
82
83         m_fanUri = "/a/fan"; // URI of the resource
84         m_fanType = "core.fan"; // resource type name. In this case, it is light
85         m_fanInterface = DEFAULT_INTERFACE; // resource interface.
86     }
87
88     /// This function internally calls registerResource API.
89     void createResources(OC::OCPlatform& platform)
90     {
91         // This will internally create and register the resource.
92         OCStackResult result = platform.registerResource(
93                                     m_roomHandle, m_roomUri, m_roomType,
94                                     m_roomInterface1, NULL, //entityHandlerRoom, 
95                                     OC_DISCOVERABLE | OC_OBSERVABLE
96                                   );
97
98         if (OC_STACK_OK != result)
99         {
100             cout << "Resource creation (room) was unsuccessful\n";
101         }
102
103         result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterface2);
104         if (OC_STACK_OK != result)
105         {
106             cout << "Binding TypeName to Resource was unsuccessful\n";
107         }
108
109         result = platform.bindInterfaceToResource(m_roomHandle, m_roomInterface3);
110         if (OC_STACK_OK != result)
111         {
112             cout << "Binding TypeName to Resource was unsuccessful\n";
113         }
114
115         result = platform.registerResource(
116                                     m_lightHandle, m_lightUri, m_lightType,
117                                     m_lightInterface, entityHandlerLight, 
118                                     OC_DISCOVERABLE | OC_OBSERVABLE
119                                    );
120
121         if (OC_STACK_OK != result)
122         {
123             cout << "Resource creation (light) was unsuccessful\n";
124         }
125
126         result = platform.registerResource(
127                                     m_fanHandle, m_fanUri, m_fanType,
128                                     m_fanInterface, entityHandlerFan, 
129                                     OC_DISCOVERABLE | OC_OBSERVABLE
130                                    );
131
132         if (OC_STACK_OK != result)
133         {
134             cout << "Resource creation (fan) was unsuccessful\n";
135         }
136
137         result = platform.bindResource(m_roomHandle, m_lightHandle);
138         if (OC_STACK_OK != result)
139         {
140             cout << "Binding fan resource to room was unsuccessful\n";
141         }
142
143         result = platform.bindResource(m_roomHandle, m_fanHandle);
144         if (OC_STACK_OK != result)
145         {
146             cout << "Binding light resource to room was unsuccessful\n";
147         }
148
149     }
150
151     void setRoomRepresentation(OCRepresentation& rep)
152     {
153         setLightRepresentation(rep);
154         setFanRepresentation(rep);
155     }
156
157     void setLightRepresentation(OCRepresentation& light)
158     {
159         AttributeMap attributeMap = light.getAttributeMap();
160
161         if(attributeMap.find("state") != attributeMap.end() && attributeMap.find("color") != attributeMap.end())
162         {
163             m_lightState = attributeMap["state"][0].compare("true") == 0;
164             m_lightColor= std::stoi(attributeMap["color"][0]);
165         }
166     }
167
168     void setFanRepresentation(OCRepresentation& fan)
169     {
170         AttributeMap attributeMap = fan.getAttributeMap();
171
172         if(attributeMap.find("state") != attributeMap.end() && attributeMap.find("speed") != attributeMap.end())
173         {
174             m_fanState = attributeMap["state"][0].compare("true") == 0;
175             m_fanSpeed = std::stoi(attributeMap["speed"][0]);
176         }
177     }
178
179
180     OCRepresentation getLightRepresentation() const
181     {
182         OCRepresentation light;
183
184         light.setUri(m_lightUri);
185
186         std::vector<std::string> interfaces;
187         interfaces.push_back(m_lightInterface);
188
189         light.setResourceInterfaces(interfaces);
190
191         std::vector<std::string> types;
192         types.push_back(m_lightType);
193
194         light.setResourceTypes(types);
195
196         AttributeMap attributeMap;
197         AttributeValues stateVal;
198         if(m_lightState)
199         {
200             stateVal.push_back("true");
201         }
202         else
203         {
204             stateVal.push_back("false");
205         }
206
207         AttributeValues colorVal;
208         colorVal.push_back(to_string(m_lightColor));
209
210         attributeMap["state"] = stateVal;
211         attributeMap["color"] = colorVal;
212
213         light.setAttributeMap(attributeMap);
214
215         return light;
216     }
217
218     OCRepresentation getFanRepresentation() const
219     {
220         OCRepresentation fan;
221         fan.setUri(m_fanUri);
222
223         std::vector<std::string> interfaces;
224         interfaces.push_back(m_fanInterface);
225
226         fan.setResourceInterfaces(interfaces);
227
228         std::vector<std::string> types;
229         types.push_back(m_fanType);
230
231         fan.setResourceTypes(types);
232
233         AttributeMap attributeMap;
234         AttributeValues stateVal;
235         if(m_fanState)
236         {
237             stateVal.push_back("true");
238         }
239         else
240         {
241             stateVal.push_back("false");
242         }
243
244         AttributeValues speedVal;
245         speedVal.push_back(to_string(m_fanSpeed));
246
247         attributeMap["state"] = stateVal;
248         attributeMap["speed"] = speedVal;
249
250         fan.setAttributeMap(attributeMap);
251         
252         return fan;
253     }
254
255     OCRepresentation getRoomRepresentation(void) const
256     {
257         OCRepresentation room;
258
259         room.setUri(m_roomUri);
260
261         std::vector<std::string> interfaces;
262         interfaces.push_back(m_roomInterface1);
263         interfaces.push_back(m_roomInterface2);
264         interfaces.push_back(m_roomInterface3);
265
266         room.setResourceInterfaces(interfaces);
267
268         std::vector<std::string> types;
269         types.push_back(m_roomType);
270
271         room.setResourceTypes(types);
272
273         std::vector<OCRepresentation> children;
274
275         OCRepresentation light = getLightRepresentation();
276         children.push_back(light);
277
278         OCRepresentation fan = getFanRepresentation();
279         children.push_back(fan);
280         room.setChildren(children);
281
282         return room;
283     }
284
285 };
286
287 // Create the instance of the resource class (in this case instance of class 'RoomResource').
288 RoomResource myRoomResource;
289
290 void entityHandlerRoom(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
291 {
292     cout << "\tIn Server CPP entity handler:\n";
293
294     if(request)
295     {
296         // Get the request type and request flag
297         std::string requestType = request->getRequestType();
298         RequestHandlerFlag requestFlag = request->getRequestHandlerFlag();
299
300         if(requestFlag == RequestHandlerFlag::InitFlag)
301         {
302             cout << "\t\trequestFlag : Init\n";
303
304             // entity handler to perform resource initialization operations
305         }
306         else if(requestFlag == RequestHandlerFlag::RequestFlag)
307         {
308             cout << "\t\trequestFlag : Request\n";
309
310             // If the request type is GET
311             if(requestType == "GET")
312             {
313                 cout << "\t\t\trequestType : GET\n";
314
315                 // Check for query params (if any)
316                 QueryParamsMap queryParamsMap = request->getQueryParameters();
317
318                 cout << "\t\t\tquery params: \n";
319                 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
320                 {
321                     cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
322                 }
323
324                 OCRepresentation rep;
325                 rep = myRoomResource.getRoomRepresentation();
326
327                 if(response)
328                 {
329                     // TODO Error Code
330                     response->setErrorCode(200);
331
332                     auto findRes = queryParamsMap.find("if");
333
334                     if(findRes != queryParamsMap.end())
335                     {
336                         response->setResourceRepresentation(rep, findRes->second);
337                     }
338                     else
339                     {
340                         response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
341                     }
342                 }
343
344             }
345             else if(requestType == "PUT")
346             {
347                 cout << "\t\t\trequestType : PUT\n";
348
349                  // Check for query params (if any)
350                 QueryParamsMap queryParamsMap = request->getQueryParameters();
351
352                 cout << "\t\t\tquery params: \n";
353                 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
354                 {
355                     cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
356                 }
357
358                 // Get the representation from the request
359                 OCRepresentation rep = request->getResourceRepresentation();
360
361                 myRoomResource.setRoomRepresentation(rep);
362
363                 // Do related operations related to PUT request
364                 rep = myRoomResource.getRoomRepresentation();
365
366                 if(response)
367                 {
368                     // TODO Error Code
369                     response->setErrorCode(200);
370
371                     auto findRes = queryParamsMap.find("if");
372
373                     if(findRes != queryParamsMap.end())
374                     {
375                         response->setResourceRepresentation(rep, findRes->second);
376                     }
377                     else
378                     {
379                         response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
380                     }
381                 }
382
383             }
384             else if(requestType == "POST")
385             {
386                 // POST request operations
387             }
388             else if(requestType == "DELETE")
389             {
390                 // DELETE request operations
391             }
392         }
393         else if(requestFlag == RequestHandlerFlag::ObserverFlag)
394         {
395             cout << "\t\trequestFlag : Observer\n";
396         }
397     }
398     else
399     {
400         std::cout << "Request invalid" << std::endl;
401     }
402 }
403
404 void entityHandlerLight(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
405 {
406     cout << "\tIn Server CPP (Light) entity handler:\n";
407
408     if(request)
409     {
410         // Get the request type and request flag
411         std::string requestType = request->getRequestType();
412         RequestHandlerFlag requestFlag = request->getRequestHandlerFlag();
413
414         if(requestFlag == RequestHandlerFlag::InitFlag)
415         {
416             cout << "\t\trequestFlag : Init\n";
417
418             // entity handler to perform resource initialization operations
419         }
420         else if(requestFlag == RequestHandlerFlag::RequestFlag)
421         {
422             cout << "\t\trequestFlag : Request\n";
423
424             // If the request type is GET
425             if(requestType == "GET")
426             {
427                 cout << "\t\t\trequestType : GET\n";
428
429                 // Check for query params (if any)
430                 QueryParamsMap queryParamsMap = request->getQueryParameters();
431
432                 cout << "\t\t\tquery params: \n";
433                 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
434                 {
435                     cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
436                 }
437
438                 OCRepresentation rep = myRoomResource.getLightRepresentation();
439
440                 if(response)
441                 {
442                     // TODO Error Code
443                     response->setErrorCode(200);
444
445                     auto findRes = queryParamsMap.find("if");
446
447                     if(findRes != queryParamsMap.end())
448                     {
449                         response->setResourceRepresentation(rep, findRes->second);
450                     }
451                     else
452                     {
453                         response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
454                     }
455                 }
456
457             }
458             else if(requestType == "PUT")
459             {
460                 cout << "\t\t\trequestType : PUT\n";
461
462                  // Check for query params (if any)
463                 QueryParamsMap queryParamsMap = request->getQueryParameters();
464
465                 cout << "\t\t\tquery params: \n";
466                 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
467                 {
468                     cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
469                 }
470
471                 // Get the representation from the request
472                 OCRepresentation rep = request->getResourceRepresentation();
473
474                 myRoomResource.setLightRepresentation(rep);
475
476                 // Do related operations related to PUT request
477                 rep = myRoomResource.getLightRepresentation();
478
479                 if(response)
480                 {
481                     // TODO Error Code
482                     response->setErrorCode(200);
483
484                     auto findRes = queryParamsMap.find("if");
485
486                     if(findRes != queryParamsMap.end())
487                     {
488                         response->setResourceRepresentation(rep, findRes->second);
489                     }
490                     else
491                     {
492                         response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
493                     }
494                 }
495
496             }
497             else if(requestType == "POST")
498             {
499                 // POST request operations
500             }
501             else if(requestType == "DELETE")
502             {
503                 // DELETE request operations
504             }
505         }
506         else if(requestFlag == RequestHandlerFlag::ObserverFlag)
507         {
508             cout << "\t\trequestFlag : Observer\n";
509         }
510     }
511     else
512     {
513         std::cout << "Request invalid" << std::endl;
514     }
515 }
516
517 void entityHandlerFan(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
518 {
519     cout << "\tIn Server CPP (Fan) entity handler:\n";
520
521     if(request)
522     {
523         // Get the request type and request flag
524         std::string requestType = request->getRequestType();
525         RequestHandlerFlag requestFlag = request->getRequestHandlerFlag();
526
527         if(requestFlag == RequestHandlerFlag::InitFlag)
528         {
529             cout << "\t\trequestFlag : Init\n";
530
531             // entity handler to perform resource initialization operations
532         }
533         else if(requestFlag == RequestHandlerFlag::RequestFlag)
534         {
535             cout << "\t\trequestFlag : Request\n";
536
537             // If the request type is GET
538             if(requestType == "GET")
539             {
540                 cout << "\t\t\trequestType : GET\n";
541
542                 // Check for query params (if any)
543                 QueryParamsMap queryParamsMap = request->getQueryParameters();
544
545                 cout << "\t\t\tquery params: \n";
546                 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
547                 {
548                     cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
549                 }
550
551                 OCRepresentation rep = myRoomResource.getFanRepresentation();
552
553                 if(response)
554                 {
555                     // TODO Error Code
556                     response->setErrorCode(200);
557
558                     auto findRes = queryParamsMap.find("if");
559
560                     if(findRes != queryParamsMap.end())
561                     {
562                         response->setResourceRepresentation(rep, findRes->second);
563                     }
564                     else
565                     {
566                         response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
567                     }
568                 }
569
570             }
571             else if(requestType == "PUT")
572             {
573                 cout << "\t\t\trequestType : PUT\n";
574
575                  // Check for query params (if any)
576                 QueryParamsMap queryParamsMap = request->getQueryParameters();
577
578                 cout << "\t\t\tquery params: \n";
579                 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
580                 {
581                     cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
582                 }
583
584                 // Get the representation from the request
585                 OCRepresentation rep = request->getResourceRepresentation();
586
587                 myRoomResource.setFanRepresentation(rep);
588
589                 // Do related operations related to PUT request
590                 rep = myRoomResource.getFanRepresentation();
591
592                 if(response)
593                 {
594                     // TODO Error Code
595                     response->setErrorCode(200);
596
597                     auto findRes = queryParamsMap.find("if");
598
599                     if(findRes != queryParamsMap.end())
600                     {
601                         response->setResourceRepresentation(rep, findRes->second);
602                     }
603                     else
604                     {
605                         response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
606                     }
607                 }
608
609             }
610             else if(requestType == "POST")
611             {
612                 // POST request operations
613             }
614             else if(requestType == "DELETE")
615             {
616                 // DELETE request operations
617             }
618         }
619         else if(requestFlag == RequestHandlerFlag::ObserverFlag)
620         {
621             cout << "\t\trequestFlag : Observer\n";
622         }
623     }
624     else
625     {
626         std::cout << "Request invalid" << std::endl;
627     }
628 }
629
630 int main()
631 {
632     // Create PlatformConfig object
633     PlatformConfig cfg {
634         OC::ServiceType::InProc,
635         OC::ModeType::Server,
636         "192.168.1.10",
637         56832,
638         OC::QualityOfService::NonConfirmable
639     };
640
641     // Create a OCPlatform instance.
642     // Note: Platform creation is synchronous call.
643     try
644     {
645         OCPlatform platform(cfg);
646
647         // Invoke createResource function of class light.
648
649         myRoomResource.createResources(platform);
650
651         // Perform app tasks
652         while(true)
653         {
654             // some tasks
655         }
656     }
657     catch(OCException e)
658     {
659         std::cout << "Exception in main: " << e.what();
660     }
661
662     // No explicit call to stop the platform.
663     // When OCPlatform destructor is invoked, internally we do platform cleanup
664 }