Merge "Merge branch 'master' into extended-easysetup" into extended-easysetup
[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 <array>
30 #include <mutex>
31 #include <condition_variable>
32
33 #include "OCPlatform.h"
34 #include "OCApi.h"
35
36 #ifdef HAVE_WINDOWS_H
37 #include <windows.h>
38 #endif
39 #include "platform_features.h"
40
41 using namespace OC;
42 using namespace std;
43
44 #define numPresenceResources (2)
45
46 // Forward declaring the entityHandler
47 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
48
49 /// This class represents a single resource named 'lightResource'. This resource has
50 /// two simple properties named 'state' and 'power'
51
52 class LightResource
53 {
54 public:
55     /// Access this property from a TB client
56     bool m_state;
57     int m_power;
58     std::string m_lightUri;
59     std::string m_lightUri2;
60     std::string m_lightUri3;
61     OCResourceHandle m_resourceHandle;
62     OCResourceHandle m_resourceHandle2;
63     OCResourceHandle m_resourceHandle3;
64
65 public:
66     /// Constructor
67     LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
68                      m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
69
70     /* Note that this does not need to be a member function: for classes you do not have
71     access to, you can accomplish this with a free function: */
72
73     /// This function internally calls registerResource API.
74     void createResource()
75     {
76         std::string resourceURI = m_lightUri; // URI of the resource
77         std::string resourceTypeName = "core.light"; // resource type name.
78         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
79
80         // OCResourceProperty is defined ocstack.h
81         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
82
83         // This will internally create and register the resource.
84         OCStackResult result = OCPlatform::registerResource(
85                                     m_resourceHandle, resourceURI, resourceTypeName,
86                                     resourceInterface, &entityHandler, resourceProperty);
87
88         if (OC_STACK_OK != result)
89         {
90             cout << "Resource creation was unsuccessful\n";
91         }
92     }
93
94     /// This function internally calls registerResource API.
95     void createResource2()
96     {
97         std::string resourceURI = m_lightUri2; // URI of the resource
98         std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
99         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
100
101         // OCResourceProperty is defined ocstack.h
102         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
103
104         // This will internally create and register the resource.
105         OCStackResult result = OCPlatform::registerResource(
106                                     m_resourceHandle2, resourceURI, resourceTypeName,
107                                     resourceInterface, &entityHandler, resourceProperty);
108
109         if (OC_STACK_OK != result)
110         {
111             cout << "Resource creation was unsuccessful\n";
112         }
113     }
114
115     void createResource3()
116     {
117         std::string resourceURI = m_lightUri3; // URI of the resource
118         std::string resourceTypeName = "core.light";
119         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
120
121         // OCResourceProperty is defined ocstack.h
122         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
123
124         // This will internally create and register the resource.
125         OCStackResult result = OCPlatform::registerResource(
126                                     m_resourceHandle3, resourceURI, resourceTypeName,
127                                     resourceInterface, &entityHandler, resourceProperty);
128
129         if (OC_STACK_OK != result)
130         {
131             cout << "Resource creation was unsuccessful\n";
132         }
133     }
134
135     OCResourceHandle getHandle()
136     {
137         return m_resourceHandle;
138     }
139
140     void addType(const std::string& type) const
141     {
142         OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
143         if (OC_STACK_OK != result)
144         {
145             cout << "Binding TypeName to Resource was unsuccessful\n";
146         }
147     }
148
149     void addInterface(const std::string& iface) const
150     {
151         OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
152         if (OC_STACK_OK != result)
153         {
154             cout << "Binding TypeName to Resource was unsuccessful\n";
155         }
156     }
157
158 };
159
160 void createPresenceResources()
161 {
162     std::array<std::string, numPresenceResources> resourceURI { {
163         "/a/fan",
164         "/a/led" } };
165     std::array<std::string, numPresenceResources> resourceTypeName { {
166         "core.fan",
167         "core.led" } };
168
169     std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
170     OCResourceHandle handle;
171     // OCResourceProperty is defined ocstack.h
172     uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
173
174     // This will internally create and register the resource.
175     OCStackResult result = OC_STACK_OK;
176     for(int i=0; i<numPresenceResources; i++)
177     {
178         result = OCPlatform::registerResource(handle,
179                 resourceURI.at(i), resourceTypeName.at(i), resourceInterface,
180                 &entityHandler, resourceProperty);
181         if (result != OC_STACK_OK)
182         {
183             cout << "Resource creation was unsuccessful with resource URI "
184                     << resourceURI.at(i);
185         }
186     }
187 }
188
189 // Create the instance of the resource class (in this case instance of class 'LightResource').
190 LightResource myLightResource;
191
192 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
193 {
194     cout << "\tIn Server CPP entity handler:\n";
195     return OC_EH_OK;
196 }
197
198 int main()
199 {
200     // Create PlatformConfig object
201     PlatformConfig cfg {
202         OC::ServiceType::InProc,
203         OC::ModeType::Server,
204         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
205         0,         // Uses randomly available port
206         OC::QualityOfService::LowQos
207     };
208
209     OCPlatform::Configure(cfg);
210     try
211     {
212         using namespace OC::OCPlatform;
213         // Time to Live is 30 seconds
214         startPresence(30);
215
216         // Invoke createResource function of class light.
217         myLightResource.createResource();
218         std :: cout << "Creating first resource of type \"core.light\"" << std :: endl;
219
220         std :: cout << "Will start creating/deleting resources for presence in 10 seconds.\n";
221
222         sleep(10);
223
224         std :: cout << "\nCreating the second resource of type \"core.light\"" <<  std :: endl;
225         sleep(1);
226
227         myLightResource.createResource2();
228
229         std :: cout << "Stopping presence\n" << std :: endl;
230         sleep(1);
231         stopPresence();
232
233         std :: cout << "Restarting presence\n" << std :: endl;
234         sleep(1);
235
236         startPresence(30);
237
238         std :: cout << "Creating a third resource of type \"core.light\"\n" << std :: endl;
239         sleep(1);
240
241         myLightResource.createResource3();
242
243         std :: cout << "Creating two non-operational resources.\"\n" << std :: endl;
244         sleep(1);
245
246         createPresenceResources();
247
248         // A condition variable will free the mutex it is given, then do a non-
249         // intensive block until 'notify' is called on it.  In this case, since we
250         // don't ever call cv.notify, this should be a non-processor intensive version
251         // of while(true);
252         std::mutex blocker;
253         std::condition_variable cv;
254         std::unique_lock<std::mutex> lock(blocker);
255         cv.wait(lock);
256     }
257     catch(OCException& e)
258     {
259         oclog() << "Exception in main: "<< e.what();
260     }
261
262     // No explicit call to stop the platform.
263     // When OCPlatform destructor is invoked, internally we do platform cleanup
264
265     return 0;
266 }
267