1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 /// This sample provides steps to define an interface for multiple resources
8 /// (properties and methods) and host these resources on the server.
13 #include "OCReflect.h"
15 using namespace OC::OCReflect;
24 bool m_powered; // This is an observable property
25 std::string manufacturer; // This is a non-observable property
32 manufacturer("Integrated Electronics")
36 /// Setter method for the setting the power state of this dimmer resource
37 void setPowered(const bool newState)
42 /// Getter method for the setting the power state of this dimmer resource
43 bool getPowered() const
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)
57 /// Getter method for getting the intensity level of the dimmer resoruce
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:
66 void bindTo(OC::OCServer& server, const std::string& base_URI)
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;
75 using OC::OCReflect::property_type;*/
77 // Register the URI with the server
78 server.registerResource(this, base_URI);
80 // Steps to map the property
82 // The canonical way to bind a property to a server in one step:
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
91 // The canonical way to bind a property to a server in one step:
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
100 // Steps to map the methods
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
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
117 // Example to map setLevel method using the canonical way in one step:
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)
127 // Example to map getLevel method using the canonical way in one step:
128 bind_service(server, this, &dimmer::getLevel, "getLevel", property_type::integer);
137 std::string manufacturer;
142 manufacturer("Integrated Electronics")
146 /// Setter method for the setting the lock state of this door resource
147 void setLockState(const bool lockState)
149 m_locked = lockState;
152 /// Getter method for the setting the lock state of this door resource
153 bool getLockState() const
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: */
161 void bindTo(OC::OCServer& server, const std::string& base_URI)
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;
170 using OC::OCReflect::property_type;*/
172 // Register the URI with the server
173 server.registerResource(this, base_URI);
175 // Steps to map the property
177 // The canonical way to bind a property to a server in one step:
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
186 // Steps to map the methods
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
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
212 void bindTo(OC::OCServer& server, const std::string& base_URI)
214 // Register the URI with the server
215 server.registerResource(this, base_URI);
217 // Bind child resource 'dimmer' to the server
218 myDimmer.bindTo(server, base_URI + "/dimmer/a");
220 // Bind child resource 'door' to the server
221 myDoor.bindTo(server, base_URI + "/door/a");
227 //Step1: create a server
228 OC::OCSecurityModel securityModel;
230 server.setSecurityModel(securityModel);
232 //Step2: Create a Garage resource
235 //Step3: Bind garage resource to the server
236 //Note: Garage resource has child resources -- dimmer and door.
237 myGarage.bindTo(server, "/garage/");
239 //Step4: Start the server
247 //Step4: Stop the server