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.
13 #include "OCReflect.h"
14 #include "OCPlatform.h"
18 using namespace OC::OCReflect;
20 /// This class represents a single resource named 'lightResource'. This resource has
21 /// two simple properties named 'state' and 'power' and they have respective setter
22 /// and getter methods.
27 /// Access this property from a TB client
33 : m_state(false), m_power(0)
37 /// Setter method for the setting the power of this light resource
38 void setPower(int powerValue)
43 /// Getter method for the getting the power of this light resource
49 /// Setter method for the setting the state of this light resource
50 void setState(bool state)
55 /// Getter method for the getting the state of this light resource
62 /* Note that this does not need to be a member function: for classes you do not have
63 access to, you can accomplish this with a free function: */
65 /// This function binds the properties and methods to the server.
66 void createResourceWithPropeties(OC::OCPlatform& platform)
68 using OC::OCReflect::property_type;
69 using OC::OCReflect::named_property_binding;
71 named_property_binding_vector properties {
72 named_property_binding("state", property_type::boolean),
73 named_property_binding("power", property_type::integer)
76 std::string resourceURI = "/a/light";
77 std::string resourceTypeName = "light";
79 // This will internally invoke registerResource method (which would eventually create
82 platform.registerResource(resourceURI, resourceTypeName, properties);
88 // Create PlatformConfig object
91 //cfg.ipAddress = "192.168.1.5";
92 cfg.ipAddress = "134.134.161.166";
94 cfg.mode = ModeType::Server;
95 cfg.serviceType = ServiceType::InProc;
97 // Create a OCPlatform instance.
98 // Note: Platform creation is synchronous call.
101 OCPlatform platform(cfg);
103 // Create the instance of the resource class (in this case instance of class 'LightResource').
104 // Invoke bindTo function of class light.
106 LightResource myLightResource;
107 myLightResource.createResourceWithPropeties(platform);
121 // No explicit call to stop the platform.
122 // When OCPlatform destructor is invoked, internally we do platform cleanup