Merge "Implementation of connectivity abstraction feature Release v0.5" into connecti...
[platform/upstream/iotivity.git] / resource / examples / presenceserver.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 ///
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
24 ///
25
26 #include <functional>
27
28 #include <pthread.h>
29 #include <mutex>
30 #include <condition_variable>
31
32 #include "OCPlatform.h"
33 #include "OCApi.h"
34
35 using namespace OC;
36 using namespace std;
37
38 // Forward declaring the entityHandler
39 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
40
41 /// This class represents a single resource named 'lightResource'. This resource has
42 /// two simple properties named 'state' and 'power'
43
44 class LightResource
45 {
46 public:
47     /// Access this property from a TB client
48     bool m_state;
49     int m_power;
50     std::string m_lightUri;
51     std::string m_lightUri2;
52     std::string m_lightUri3;
53     OCResourceHandle m_resourceHandle;
54     OCResourceHandle m_resourceHandle2;
55     OCResourceHandle m_resourceHandle3;
56
57 public:
58     /// Constructor
59     LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
60                      m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
61
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 internally calls registerResource API.
66     void createResource()
67     {
68         std::string resourceURI = m_lightUri; // URI of the resource
69         std::string resourceTypeName = "core.light"; // resource type name.
70         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
71
72         // OCResourceProperty is defined ocstack.h
73         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
74
75         // This will internally create and register the resource.
76         OCStackResult result = OCPlatform::registerResource(
77                                     m_resourceHandle, resourceURI, resourceTypeName,
78                                     resourceInterface, &entityHandler, resourceProperty);
79
80         if (OC_STACK_OK != result)
81         {
82             cout << "Resource creation was unsuccessful\n";
83         }
84     }
85
86     /// This function internally calls registerResource API.
87     void createResource2()
88     {
89         std::string resourceURI = m_lightUri2; // URI of the resource
90         std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
91         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
92
93         // OCResourceProperty is defined ocstack.h
94         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
95
96         // This will internally create and register the resource.
97         OCStackResult result = OCPlatform::registerResource(
98                                     m_resourceHandle2, resourceURI, resourceTypeName,
99                                     resourceInterface, &entityHandler, resourceProperty);
100
101         if (OC_STACK_OK != result)
102         {
103             cout << "Resource creation was unsuccessful\n";
104         }
105     }
106
107     void createResource3()
108     {
109         std::string resourceURI = m_lightUri3; // URI of the resource
110         std::string resourceTypeName = "core.light";
111         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
112
113         // OCResourceProperty is defined ocstack.h
114         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
115
116         // This will internally create and register the resource.
117         OCStackResult result = OCPlatform::registerResource(
118                                     m_resourceHandle3, resourceURI, resourceTypeName,
119                                     resourceInterface, &entityHandler, resourceProperty);
120
121         if (OC_STACK_OK != result)
122         {
123             cout << "Resource creation was unsuccessful\n";
124         }
125     }
126
127     OCResourceHandle getHandle()
128     {
129         return m_resourceHandle;
130     }
131
132     void addType(const std::string& type) const
133     {
134         OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
135         if (OC_STACK_OK != result)
136         {
137             cout << "Binding TypeName to Resource was unsuccessful\n";
138         }
139     }
140
141     void addInterface(const std::string& interface) const
142     {
143         OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
144         if (OC_STACK_OK != result)
145         {
146             cout << "Binding TypeName to Resource was unsuccessful\n";
147         }
148     }
149
150 };
151
152 // Create the instance of the resource class (in this case instance of class 'LightResource').
153 LightResource myLightResource;
154
155 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
156 {
157     cout << "\tIn Server CPP entity handler:\n";
158     return OC_EH_OK;
159 }
160
161 int main()
162 {
163     // Create PlatformConfig object
164     PlatformConfig cfg {
165         OC::ServiceType::InProc,
166         OC::ModeType::Server,
167         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
168         0,         // Uses randomly available port
169         OC::QualityOfService::LowQos
170     };
171
172     OCPlatform::Configure(cfg);
173     try
174     {
175         using namespace OC::OCPlatform;
176         // Time to Live is 30 seconds
177         startPresence(30);
178
179         // Invoke createResource function of class light.
180         myLightResource.createResource();
181
182         printf("\nEnter a key to create the second resource\n");
183         getchar();
184
185         myLightResource.createResource2();
186
187         printf("\nEnter a key to stop the presence\n");
188         getchar();
189         stopPresence();
190
191         printf("\nEnter a key to restart the presence\n");
192         getchar();
193
194         startPresence(30);
195
196         printf("\nEnter a key to create the third resource\n");
197         getchar();
198
199         myLightResource.createResource3();
200
201         // A condition variable will free the mutex it is given, then do a non-
202         // intensive block until 'notify' is called on it.  In this case, since we
203         // don't ever call cv.notify, this should be a non-processor intensive version
204         // of while(true);
205         std::mutex blocker;
206         std::condition_variable cv;
207         std::unique_lock<std::mutex> lock(blocker);
208         cv.wait(lock);
209     }
210     catch(OCException e)
211     {
212         //log(e.what());
213     }
214
215     // No explicit call to stop the platform.
216     // When OCPlatform destructor is invoked, internally we do platform cleanup
217
218     return 0;
219 }