1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
29 #include <OCReflect.h>
30 #include <OCPlatform.h>
37 using namespace OC::OCReflect;
39 /// This class represents a single resource named 'light'. This resource has
40 /// two simple properties named 'state' and 'power' and they have respective setter
41 /// and getter methods.
46 /// Access this property from a TB client
57 /// Setter method for the setting the power of this light resource
58 void setPower(int powerValue)
63 /// Getter method for the getting the power of this light resource
69 /// Setter method for the setting the state of this light resource
70 void setState(bool state)
75 /// Getter method for the getting the state of this light resource
82 /* Note that this does not need to be a member function: for classes you do not have
83 access to, you can accomplish this with a free function: */
85 /// This function binds the properties and methods to the server.
86 void bindTo(OC::OCPlatform& platform)
88 using OC::OCReflect::property_type;
89 using OC::OCReflect::property_binding;
91 property_binding_vector properties {
92 property_binding("state", property_type::boolean),
93 property_binding("power", property_type::integer)
96 std::string resourceURI = "/a/light";
97 std::string resourceTypeName = "light";
98 platform.registerResource(resourceURI, resourceTypeName, properties);
104 // Step1: Create a OCPlatform instance.
105 // Step1.1: The constructor of OCPlatform class takes PlatformConfig object.
106 // Note: Platform creation is synchronous call.
109 cfg.ipAddress = "192.168.1.5";
111 cfg.mode = ModeType::Server;
112 cfg.serviceType = ServiceType::InProc;
114 cout << "Creating OCPlatform .. \n";
116 OCPlatform platform(cfg);
118 // Step2: Create the instance of the resource class (in this case instance of class 'light').
119 // Step2.1: Invoke bindTo function of class light.
122 myLight.bindTo(platform);
129 // No explicit call to stop the platform.
130 // When OCPlatform destructor is invoked, internally we do platform cleanup