Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / examples / server / multiple_resources.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 ///
7 /// This sample provides steps to define an interface for multiple resources
8 /// (properties and methods) and host these resources on the server. 
9 ///
10 #include <functional>
11
12 #include "OCServer.h"
13 #include "OCReflect.h"
14
15 using namespace OC::OCReflect;
16
17 class dimmer
18 {
19 private:
20         int m_level;
21         int m_color;
22
23 public:
24         bool m_powered;                         // This is an observable property
25         std::string manufacturer;       // This is a non-observable property
26
27 public:
28         dimmer()
29                 : m_level(0),
30                 m_color(0),
31                 m_powered(false),
32                 manufacturer("Integrated Electronics")
33         {}
34
35 public:
36         /// Setter method for the setting the power state of this dimmer resource
37         void setPowered(const bool newState)             
38         { 
39                 m_powered = newState; 
40         }
41
42         /// Getter method for the setting the power state of this dimmer resource
43         bool getPowered() const                           
44         { 
45                 return m_powered; 
46         }
47
48         /// Setter method for the setting the intensity level of this dimmer resource
49         /// Along with above basic method, this method takes two arguments
50         /// with first argument as intensity level and second level as color.
51         void setLevel(const int newLevel, int color)     
52         { 
53                 m_level = newLevel; 
54                 m_color = color; 
55         }
56
57         /// Getter method for getting the intensity level of the dimmer resoruce
58         int getLevel() const                              
59         { 
60                 return m_level; 
61         }
62
63         /// Note that this does not need to be a member function: for classes you do not have
64         /// access to, you can accomplish this with a free function:
65 public:
66         void bindTo(OC::OCServer& server, const std::string& base_URI)
67         {
68                 /*using OC::OCReflect::method_signature;
69                 using OC::OCReflect::method_binding;
70                 using OC::OCReflect::property_signature;
71                 using OC::OCReflect::property_binding;
72                 using OC::OCReflect::bind_service;
73                 using OC::OCReflect::bind_property;
74
75                 using OC::OCReflect::property_type;*/
76
77                 // Register the URI with the server
78                 server.registerResource(this, base_URI);
79
80                 // Steps to map the property
81
82                 // The canonical way to bind a property to a server in one step:
83                 bind_property(
84                         server,                                      // server to bind to
85                         this->manufacturer,                          // pointer to property
86                         "manufacturer",                              // property binding name
87                         OC::OCReflect::property_type::string,        // property
88                         OC::OCReflect::property_attribute::r         // type decoration
89                         );
90
91                 // The canonical way to bind a property to a server in one step:
92                 bind_property(
93                         server,                                      // server to bind to
94                         this->m_powered,                             // pointer to property
95                         "powerState",                                // property binding name
96                         OC::OCReflect::property_type::boolean,       // property
97                         OC::OCReflect::property_attribute::r         // type decoration
98                         );
99
100                 // Steps to map the methods
101
102                 // Example to map getPowered method using the canonical way in one step:
103                 bind_service(server,                    // server to bind to
104                         this,                                           // instance (ourself) to bind 
105                         &dimmer::getPowered,                    // address of the method to bind
106                         "getPowered",                           // name to bind with service URL
107                         property_type::boolean);        // return type of the method
108
109                 // Example to map setPowered method using the canonical way in one step:
110                 bind_service(server,                    // server to bind to
111                         this,                                           // instance (ourself) to bind 
112                         &dimmer::setPowered,                    // address of the method to bind
113                         "setPowered",                           // name to bind with service URL
114                         property_type::nil,                     // return type of the method
115                         property_type::boolean);        // parameter type for setPowered method
116
117                 // Example to map setLevel method using the canonical way in one step:
118                 bind_service(
119                         server,                         // server to bind with
120                         this,                           // instance to bind
121                         &dimmer::setLevel,              // method to bind
122                         "setLevel",                     // service name
123                         property_type::nil,             // input type
124                         property_type::integer,         // parameter list starts (parameter type for level)
125                         property_type::integer);        // parameter list (parameter type for color) 
126
127                 // Example to map getLevel method using the canonical way in one step:
128                 bind_service(server, this, &dimmer::getLevel, "getLevel", property_type::integer);
129         }
130 };
131
132 class door
133 {
134  bool m_locked;
135
136 public:
137         std::string manufacturer;
138
139 public:
140         door()
141                 : m_locked(true),
142                 manufacturer("Integrated Electronics")
143         {}
144
145 public:
146         /// Setter method for the setting the lock state of this door resource
147         void setLockState(const bool lockState)             
148         { 
149                 m_locked = lockState; 
150         }
151
152         /// Getter method for the setting the lock state of this door resource
153         bool getLockState() const                           
154         { 
155                 return m_locked; 
156         }
157
158         /* Note that this does not need to be a member function: for classes you do not have
159         access to, you can accomplish this with a free function: */
160 public:
161         void bindTo(OC::OCServer& server, const std::string& base_URI)
162         {
163                 /*using OC::OCReflect::method_signature;
164                 using OC::OCReflect::method_binding;
165                 using OC::OCReflect::property_signature;
166                 using OC::OCReflect::property_binding;
167                 using OC::OCReflect::bind_service;
168                 using OC::OCReflect::bind_property;
169
170                 using OC::OCReflect::property_type;*/
171
172                 // Register the URI with the server
173                 server.registerResource(this, base_URI);
174
175                 // Steps to map the property
176
177                 // The canonical way to bind a property to a server in one step:
178                 bind_property(
179                         server,                                      // server to bind to
180                         this->manufacturer,                          // pointer to property
181                         "manufacturer",                              // property binding name
182                         OC::OCReflect::property_type::string,        // property
183                         OC::OCReflect::property_attribute::r         // type decoration
184                         );
185
186                 // Steps to map the methods
187
188                 // Example to map getPowered method using the canonical way in one step:
189                 bind_service(server,                    // server to bind to
190                         this,                                           // instance (ourself) to bind 
191                         &door::getLockState,            // address of the method to bind
192                         "getLockState",                         // name to bind with service URL
193                         property_type::boolean);        // return type of the method
194
195                 // Example to map setPowered method using the canonical way in one step:
196                 bind_service(server,                    // server to bind to
197                         this,                                           // instance (ourself) to bind 
198                         &door::setLockState,            // address of the method to bind
199                         "setLockState",                         // name to bind with service URL
200                         property_type::nil,                     // return type of the method
201                         property_type::boolean);        // parameter type for setPowered method
202         }
203 };
204
205 class garage 
206 {
207 private:
208         dimmer myDimmer;
209         door myDoor;
210
211 public:
212         void bindTo(OC::OCServer& server, const std::string& base_URI)
213         {
214                 // Register the URI with the server
215                 server.registerResource(this, base_URI);
216
217                 // Bind child resource 'dimmer' to the server 
218                 myDimmer.bindTo(server, base_URI + "/dimmer/a");
219
220                 // Bind child resource 'door' to the server
221                 myDoor.bindTo(server, base_URI + "/door/a");
222         }
223 };
224
225 int main()
226 {
227         //Step1: create a server
228         OC::OCSecurityModel securityModel;
229         OC::OCServer server;
230         server.setSecurityModel(securityModel);
231
232         //Step2: Create a Garage resource
233         garage myGarage;
234
235         //Step3: Bind garage resource to the server
236         //Note: Garage resource has child resources -- dimmer and door.
237         myGarage.bindTo(server, "/garage/");
238
239         //Step4: Start the server
240         server.start();
241
242         while(true)
243         {
244                 //do something here
245         }
246
247         //Step4: Stop the server
248         server.stop();
249 }