1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 /// This sample provides steps to define an interface for a resource
8 /// (properties and methods) and host this resource on the server.
14 #include <OCReflect.h>
15 #include <OCPlatform.h>
22 using namespace OC::OCReflect;
24 /// This class represents a single resource named 'light'. This resource has
25 /// two simple properties named 'state' and 'power' and they have respective setter
26 /// and getter methods.
31 /// Access this property from a TB client
42 /// Setter method for the setting the power of this light resource
43 void setPower(int powerValue)
48 /// Getter method for the getting the power of this light resource
54 /// Setter method for the setting the state of this light resource
55 void setState(bool state)
60 /// Getter method for the getting the state of this light resource
67 /* Note that this does not need to be a member function: for classes you do not have
68 access to, you can accomplish this with a free function: */
70 /// This function binds the properties and methods to the server.
71 void bindTo(OC::OCPlatform& platform)
73 using OC::OCReflect::property_type;
74 using OC::OCReflect::named_property_binding;
76 named_property_binding_vector properties {
77 named_property_binding("state", property_type::boolean),
78 named_property_binding("power", property_type::integer)
81 std::string resourceURI = "/a/light";
82 std::string resourceTypeName = "light";
83 platform.registerResource(resourceURI, resourceTypeName, properties);
89 // Step1: Create a OCPlatform instance.
90 // Step1.1: The constructor of OCPlatform class takes PlatformConfig object.
91 // Note: Platform creation is synchronous call.
94 cfg.ipAddress = "192.168.1.5";
96 cfg.mode = ModeType::Server;
97 cfg.serviceType = ServiceType::InProc;
99 cout << "Creating OCPlatform .. \n";
101 OCPlatform platform(cfg);
103 // Step2: Create the instance of the resource class (in this case instance of class 'light').
104 // Step2.1: Invoke bindTo function of class light.
107 myLight.bindTo(platform);
114 // No explicit call to stop the platform.
115 // When OCPlatform destructor is invoked, internally we do platform cleanup