Updated Makefiles and CMakeLists.txt to point to resource, not oic-resource
[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 OCEntityHandlerResult entityHandlerRoom(std::shared_ptr<OCResourceRequest> request,
38                                         std::shared_ptr<OCResourceResponse> response);
39 OCEntityHandlerResult entityHandlerLight(std::shared_ptr<OCResourceRequest> request,
40                                          std::shared_ptr<OCResourceResponse> response);
41 OCEntityHandlerResult entityHandlerFan(std::shared_ptr<OCResourceRequest> request,
42                                        std::shared_ptr<OCResourceResponse> response);
43
44 class RoomResource
45 {
46 public:
47
48     // Room members
49     std::string m_roomUri;
50     std::string m_roomName;
51     std::vector<std::string> m_roomTypes;
52     std::vector<std::string> m_roomInterfaces;
53     OCResourceHandle m_roomHandle;
54     OCRepresentation m_roomRep;
55
56     // light members
57     bool m_lightState;
58     int m_lightColor;
59     std::string m_lightUri;
60     std::vector<std::string> m_lightTypes;
61     std::vector<std::string> m_lightInterfaces;
62     OCResourceHandle m_lightHandle;
63     OCRepresentation m_lightRep;
64
65     // fan members
66     bool m_fanState;
67     int m_fanSpeed;
68     std::string m_fanUri;
69     std::vector<std::string> m_fanTypes;
70     std::vector<std::string> m_fanInterfaces;
71     OCResourceHandle m_fanHandle;
72     OCRepresentation m_fanRep;
73
74 public:
75     /// Constructor
76     RoomResource(): m_roomName("John's Room"), m_lightState(false),
77                     m_lightColor(0), m_fanState(false), m_fanSpeed(0)
78     {
79         m_lightUri = "/a/light"; // URI of the resource
80         m_lightTypes.push_back("core.light"); // resource type name. In this case, it is light
81         m_lightInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
82
83         m_lightRep.setUri(m_lightUri);
84         m_lightRep.setResourceTypes(m_lightTypes);
85         m_lightRep.setResourceInterfaces(m_lightInterfaces);
86         m_lightRep.setValue("state", m_lightState);
87         m_lightRep.setValue("color", m_lightColor);
88
89         m_fanUri = "/a/fan"; // URI of the resource
90         m_fanTypes.push_back("core.fan"); // resource type name. In this case, it is light
91         m_fanInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
92
93         m_fanRep.setUri(m_fanUri);
94         m_fanRep.setResourceTypes(m_fanTypes);
95         m_fanRep.setResourceInterfaces(m_fanInterfaces);
96         m_fanRep.setValue("state", m_fanState);
97         m_fanRep.setValue("speed", m_fanSpeed);
98
99         m_roomUri = "/a/room"; // URI of the resource
100         m_roomTypes.push_back("core.room"); // resource type name. In this case, it is light
101         m_roomInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
102         m_roomInterfaces.push_back(BATCH_INTERFACE); // resource interface.
103         m_roomInterfaces.push_back(LINK_INTERFACE); // resource interface.
104         m_roomRep.setValue("name", m_roomName);
105         m_roomRep.setUri(m_roomUri);
106         m_roomRep.setResourceTypes(m_roomTypes);
107         m_roomRep.setResourceInterfaces(m_roomInterfaces);
108     }
109
110     /// This function internally calls registerResource API.
111     void createResources()
112     {
113         using namespace OC::OCPlatform;
114         // This will internally create and register the resource.
115         OCStackResult result = registerResource(
116                                     m_roomHandle, m_roomUri, m_roomTypes[0],
117                                     m_roomInterfaces[0], entityHandlerRoom,
118                                     OC_DISCOVERABLE | OC_OBSERVABLE
119                                   );
120
121         if (OC_STACK_OK != result)
122         {
123             cout << "Resource creation (room) was unsuccessful\n";
124         }
125
126         result = bindInterfaceToResource(m_roomHandle, m_roomInterfaces[1]);
127         if (OC_STACK_OK != result)
128         {
129             cout << "Binding TypeName to Resource was unsuccessful\n";
130         }
131
132         result = bindInterfaceToResource(m_roomHandle, m_roomInterfaces[2]);
133         if (OC_STACK_OK != result)
134         {
135             cout << "Binding TypeName to Resource was unsuccessful\n";
136         }
137
138         result = registerResource(
139                                     m_lightHandle, m_lightUri, m_lightTypes[0],
140                                     m_lightInterfaces[0], entityHandlerLight,
141                                     OC_DISCOVERABLE | OC_OBSERVABLE
142                                    );
143
144         if (OC_STACK_OK != result)
145         {
146             cout << "Resource creation (light) was unsuccessful\n";
147         }
148
149         result = registerResource(
150                                     m_fanHandle, m_fanUri, m_fanTypes[0],
151                                     m_fanInterfaces[0], entityHandlerFan,
152                                     OC_DISCOVERABLE | OC_OBSERVABLE
153                                    );
154
155         if (OC_STACK_OK != result)
156         {
157             cout << "Resource creation (fan) was unsuccessful\n";
158         }
159
160         result = bindResource(m_roomHandle, m_lightHandle);
161         if (OC_STACK_OK != result)
162         {
163             cout << "Binding fan resource to room was unsuccessful\n";
164         }
165
166         result = bindResource(m_roomHandle, m_fanHandle);
167         if (OC_STACK_OK != result)
168         {
169             cout << "Binding light resource to room was unsuccessful\n";
170         }
171
172     }
173
174     void setLightRepresentation(OCRepresentation& rep)
175     {
176         bool tempState = false;
177         int tempColor = 0;
178
179         // If both entries exist
180         if(rep.getValue("state", tempState) && rep.getValue("color", tempColor))
181         {
182             m_lightState = tempState;
183             m_lightColor= tempColor;
184
185             cout << "\t\t\t\t" << "state: " << m_lightState << endl;
186             cout << "\t\t\t\t" << "color: " << m_lightColor << endl;
187         }
188     }
189
190     void setFanRepresentation(OCRepresentation& rep)
191     {
192         bool tempState = false;
193         int tempSpeed = 0;
194
195         // If both entries exist
196         if(rep.getValue("state", tempState) && rep.getValue("speed", tempSpeed))
197         {
198             m_fanState = tempState;
199             m_fanSpeed = tempSpeed;
200
201             cout << "\t\t\t\t" << "state: " << m_fanState << endl;
202             cout << "\t\t\t\t" << "speed: " << m_fanSpeed << endl;
203         }
204     }
205
206
207     OCRepresentation getLightRepresentation()
208     {
209         m_lightRep.setValue("state", m_lightState);
210         m_lightRep.setValue("color", m_lightColor);
211
212         return m_lightRep;
213     }
214
215     OCRepresentation getFanRepresentation()
216     {
217         m_fanRep.setValue("state", m_fanState);
218         m_fanRep.setValue("speed", m_fanSpeed);
219         return m_fanRep;
220     }
221
222     OCRepresentation getRoomRepresentation(void)
223     {
224         std::vector<OCRepresentation> children;
225
226         OCRepresentation light = getLightRepresentation();
227         children.push_back(light);
228
229         OCRepresentation fan = getFanRepresentation();
230         children.push_back(fan);
231
232         m_roomRep.setChildren(children);
233
234         return m_roomRep;
235     }
236
237 };
238
239 // Create the instance of the resource class (in this case instance of class 'RoomResource').
240 RoomResource myRoomResource;
241
242 OCEntityHandlerResult entityHandlerRoom(std::shared_ptr<OCResourceRequest> request,
243                        std::shared_ptr<OCResourceResponse> response)
244 {
245     cout << "\tIn Server CPP entity handler:\n";
246
247     if(request)
248     {
249         // Get the request type and request flag
250         std::string requestType = request->getRequestType();
251         int requestFlag = request->getRequestHandlerFlag();
252
253         if(requestFlag == RequestHandlerFlag::InitFlag)
254         {
255             cout << "\t\trequestFlag : Init\n";
256
257             // entity handler to perform resource initialization operations
258         }
259         else if(requestFlag == RequestHandlerFlag::RequestFlag)
260         {
261             cout << "\t\trequestFlag : Request\n";
262
263             // If the request type is GET
264             if(requestType == "GET")
265             {
266                 cout << "\t\t\trequestType : GET\n";
267
268                 // Check for query params (if any)
269                 QueryParamsMap queryParamsMap = request->getQueryParameters();
270
271                 cout << "\t\t\tquery params: \n";
272                 for(auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
273                 {
274                     cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
275                 }
276
277                 OCRepresentation rep;
278                 rep = myRoomResource.getRoomRepresentation();
279
280                 if(response)
281                 {
282                     // TODO Error Code
283                     response->setErrorCode(200);
284
285                     auto findRes = queryParamsMap.find("if");
286
287                     if(findRes != queryParamsMap.end())
288                     {
289                         response->setResourceRepresentation(rep, findRes->second);
290                     }
291                     else
292                     {
293                         response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
294                     }
295                 }
296
297             }
298             else if(requestType == "PUT")
299             {
300                 cout << "\t\t\trequestType : PUT\n";
301
302                 QueryParamsMap queryParamsMap = request->getQueryParameters();
303
304                 entityHandlerLight(request, response);
305                 entityHandlerFan(request, response);
306
307                 OCRepresentation rep;
308                 rep = myRoomResource.getRoomRepresentation();
309
310                 if(response)
311                 {
312                     response->setErrorCode(200);
313
314                     auto findRes = queryParamsMap.find("if");
315
316                     if(findRes != queryParamsMap.end())
317                     {
318                         response->setResourceRepresentation(rep, findRes->second);
319                     }
320                     else
321                     {
322                         response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
323                     }
324                 }
325             }
326             else if(requestType == "POST")
327             {
328                 // POST request operations
329             }
330             else if(requestType == "DELETE")
331             {
332                 // DELETE request operations
333             }
334         }
335         else if(requestFlag == RequestHandlerFlag::ObserverFlag)
336         {
337             cout << "\t\trequestFlag : Observer\n";
338         }
339     }
340     else
341     {
342         std::cout << "Request invalid" << std::endl;
343     }
344
345     return OC_EH_OK;
346 }
347
348 OCEntityHandlerResult entityHandlerLight(std::shared_ptr<OCResourceRequest> request,
349                                          std::shared_ptr<OCResourceResponse> response)
350 {
351     cout << "\tIn Server CPP (Light) entity handler:\n";
352
353     if(request)
354     {
355         // Get the request type and request flag
356         std::string requestType = request->getRequestType();
357         int requestFlag = request->getRequestHandlerFlag();
358
359         if(requestFlag == RequestHandlerFlag::InitFlag)
360         {
361             cout << "\t\trequestFlag : Init\n";
362
363             // entity handler to perform resource initialization operations
364         }
365         else if(requestFlag == RequestHandlerFlag::RequestFlag)
366         {
367             cout << "\t\trequestFlag : Request\n";
368
369             // If the request type is GET
370             if(requestType == "GET")
371             {
372                 cout << "\t\t\trequestType : GET\n";
373
374                 if(response)
375                 {
376                     // TODO Error Code
377                     response->setErrorCode(200);
378                     response->setResourceRepresentation(myRoomResource.getLightRepresentation());
379                 }
380
381             }
382             else if(requestType == "PUT")
383             {
384                 cout << "\t\t\trequestType : PUT\n";
385
386                 OCRepresentation rep = request->getResourceRepresentation();
387
388                 // Do related operations related to PUT request
389                 myRoomResource.setLightRepresentation(rep);
390
391                 if(response)
392                 {
393                     // TODO Error Code
394                     response->setErrorCode(200);
395                     response->setResourceRepresentation(myRoomResource.getLightRepresentation());
396                 }
397
398             }
399             else if(requestType == "POST")
400             {
401                 // POST request operations
402             }
403             else if(requestType == "DELETE")
404             {
405                 // DELETE request operations
406             }
407         }
408         else if(requestFlag == RequestHandlerFlag::ObserverFlag)
409         {
410             cout << "\t\trequestFlag : Observer\n";
411         }
412     }
413     else
414     {
415         std::cout << "Request invalid" << std::endl;
416     }
417
418     return OC_EH_OK;
419 }
420
421 OCEntityHandlerResult entityHandlerFan(std::shared_ptr<OCResourceRequest> request,
422                                        std::shared_ptr<OCResourceResponse> response)
423 {
424     cout << "\tIn Server CPP (Fan) entity handler:\n";
425
426     if(request)
427     {
428         // Get the request type and request flag
429         std::string requestType = request->getRequestType();
430         int requestFlag = request->getRequestHandlerFlag();
431
432         if(requestFlag == RequestHandlerFlag::InitFlag)
433         {
434             cout << "\t\trequestFlag : Init\n";
435
436             // entity handler to perform resource initialization operations
437         }
438         else if(requestFlag == RequestHandlerFlag::RequestFlag)
439         {
440             cout << "\t\trequestFlag : Request\n";
441
442             // If the request type is GET
443             if(requestType == "GET")
444             {
445                 cout << "\t\t\trequestType : GET\n";
446
447                 if(response)
448                 {
449                     // TODO Error Code
450                     response->setErrorCode(200);
451
452                     response->setResourceRepresentation(myRoomResource.getFanRepresentation());
453                 }
454
455             }
456             else if(requestType == "PUT")
457             {
458                 cout << "\t\t\trequestType : PUT\n";
459
460                 OCRepresentation rep = request->getResourceRepresentation();
461
462                 // Do related operations related to PUT request
463                 myRoomResource.setFanRepresentation(rep);
464
465                 if(response)
466                 {
467                     // TODO Error Code
468                     response->setErrorCode(200);
469                     response->setResourceRepresentation(myRoomResource.getFanRepresentation());
470                 }
471             }
472             else if(requestType == "POST")
473             {
474                 // POST request operations
475             }
476             else if(requestType == "DELETE")
477             {
478                 // DELETE request operations
479             }
480         }
481         else if(requestFlag == RequestHandlerFlag::ObserverFlag)
482         {
483             cout << "\t\trequestFlag : Observer\n";
484         }
485     }
486     else
487     {
488         std::cout << "Request invalid" << std::endl;
489     }
490
491     return OC_EH_OK;
492 }
493
494 int main()
495 {
496     // Create PlatformConfig object
497     PlatformConfig cfg {
498         OC::ServiceType::InProc,
499         OC::ModeType::Server,
500         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
501         0,         // Uses randomly available port
502         OC::QualityOfService::LowQos
503     };
504
505     OCPlatform::Configure(cfg);
506     try
507     {
508
509         myRoomResource.createResources();
510
511         // Perform app tasks
512         while(true)
513         {
514             // some tasks
515         }
516     }
517     catch(OCException e)
518     {
519         std::cout << "Exception in main: " << e.what();
520     }
521
522     // No explicit call to stop the platform.
523     // When OCPlatform destructor is invoked, internally we do platform cleanup
524 }