Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / examples / server / single_resource.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 ///
7 /// This sample provides steps to define an interface for a resource
8 /// (properties and methods) and host this resource on the server. 
9 ///
10
11 #include <functional>
12
13 #include "OCReflect.h"
14 #include "OCPlatform.h"
15 #include "OCApi.h"
16
17 using namespace OC;
18 using namespace OC::OCReflect;
19
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.
23
24 class LightResource
25 {
26 private:
27         /// Access this property from a TB client 
28         bool m_state;
29         int m_power;
30
31 public:
32         LightResource()
33                 : m_state(false), m_power(0)
34         {}
35
36 public:
37         /// Setter method for the setting the power of this light resource
38         void setPower(int powerValue)             
39         { 
40                 m_power = powerValue; 
41         }
42
43         /// Getter method for the getting the power of this light resource
44         int getPower() const                           
45         { 
46                 return m_power; 
47         }
48
49         /// Setter method for the setting the state of this light resource
50         void setState(bool state)             
51         { 
52                 m_state = state; 
53         }
54
55         /// Getter method for the getting the state of this light resource
56         bool getState() const                           
57         { 
58                 return m_state; 
59         }
60         
61 public:
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: */
64         
65         /// This function binds the properties and methods to the server. 
66         void createResourceWithPropeties(OC::OCPlatform& platform)
67         {
68                 using OC::OCReflect::property_type;
69                 using OC::OCReflect::named_property_binding;
70
71                 named_property_binding_vector properties {
72                         named_property_binding("state", property_type::boolean),
73                         named_property_binding("power", property_type::integer)
74                 };
75
76                 std::string resourceURI = "/a/light";
77                 std::string resourceTypeName = "light";
78
79                 // This will internally invoke registerResource method (which would eventually create 
80                 // resource). 
81
82                 platform.registerResource(resourceURI, resourceTypeName, properties);
83         }
84 };
85
86 int main()
87 {
88         // Create PlatformConfig object
89
90         PlatformConfig cfg;
91         //cfg.ipAddress = "192.168.1.5";
92         cfg.ipAddress = "134.134.161.166";
93         cfg.port = 5683;
94         cfg.mode = ModeType::Server;
95         cfg.serviceType = ServiceType::InProc;
96         
97         // Create a OCPlatform instance. 
98         // Note: Platform creation is synchronous call. 
99         try
100         {
101                 OCPlatform platform(cfg);
102
103                 // Create the instance of the resource class (in this case instance of class 'LightResource'). 
104                 // Invoke bindTo function of class light. 
105
106                 LightResource myLightResource;
107                 myLightResource.createResourceWithPropeties(platform);
108                 
109                 // Perform app tasks
110                 while(true)
111                 {
112                         // some tasks
113                 }
114         }
115         catch(OCException e)
116         {
117                 //log(e.what());
118         }
119
120                 
121         // No explicit call to stop the platform. 
122         // When OCPlatform destructor is invoked, internally we do platform cleanup
123 }