[IOT-1538] Add support for Protocol-Independent ID
[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 "iotivity_config.h"
27 #include <functional>
28
29 #ifdef HAVE_PTHREAD_H
30 #include <pthread.h>
31 #endif
32 #include <array>
33 #include <mutex>
34 #include <condition_variable>
35
36 #include "OCPlatform.h"
37 #include "OCApi.h"
38 #include "ocpayload.h"
39
40 #ifdef HAVE_WINDOWS_H
41 #include <windows.h>
42 #endif
43
44 using namespace OC;
45 using namespace std;
46
47 #define numPresenceResources (2)
48
49 // Set of strings for each of platform Info fields
50 std::string  platformId = "0A3E0D6F-DBF5-404E-8719-D6880042463A";
51 std::string  manufacturerName = "OCF";
52 std::string  manufacturerLink = "https://www.iotivity.org";
53 std::string  modelNumber = "myModelNumber";
54 std::string  dateOfManufacture = "2016-01-15";
55 std::string  platformVersion = "myPlatformVersion";
56 std::string  operatingSystemVersion = "myOS";
57 std::string  hardwareVersion = "myHardwareVersion";
58 std::string  firmwareVersion = "1.0";
59 std::string  supportLink = "https://www.iotivity.org";
60 std::string  systemTime = "2016-01-15T11.01";
61
62 // Set of strings for each of device info fields
63 std::string  deviceName = "IoTivity Presence Server";
64 std::string  specVersion = "core.1.1.0";
65 std::vector<std::string> dataModelVersions = {"res.1.1.0"};
66 std::string  protocolIndependentID = "41a75d38-83c3-4b83-a794-f2174990b30b";
67
68 // OCPlatformInfo Contains all the platform info to be stored
69 OCPlatformInfo platformInfo;
70
71 // Forward declaring the entityHandler
72 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
73
74 /// This class represents a single resource named 'lightResource'. This resource has
75 /// two simple properties named 'state' and 'power'
76
77 class LightResource
78 {
79 public:
80     /// Access this property from a TB client
81     bool m_state;
82     int m_power;
83     std::string m_lightUri;
84     std::string m_lightUri2;
85     std::string m_lightUri3;
86     OCResourceHandle m_resourceHandle;
87     OCResourceHandle m_resourceHandle2;
88     OCResourceHandle m_resourceHandle3;
89
90 public:
91     /// Constructor
92     LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
93                      m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
94
95     /* Note that this does not need to be a member function: for classes you do not have
96     access to, you can accomplish this with a free function: */
97
98     /// This function internally calls registerResource API.
99     void createResource()
100     {
101         std::string resourceURI = m_lightUri; // URI of the resource
102         std::string resourceTypeName = "core.light"; // resource type name.
103         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
104
105         // OCResourceProperty is defined ocstack.h
106         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
107
108         // This will internally create and register the resource.
109         OCStackResult result = OCPlatform::registerResource(
110                                     m_resourceHandle, resourceURI, resourceTypeName,
111                                     resourceInterface, &entityHandler, resourceProperty);
112
113         if (OC_STACK_OK != result)
114         {
115             cout << "Resource creation was unsuccessful\n";
116         }
117     }
118
119     /// This function internally calls registerResource API.
120     void createResource2()
121     {
122         std::string resourceURI = m_lightUri2; // URI of the resource
123         std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
124         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
125
126         // OCResourceProperty is defined ocstack.h
127         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
128
129         // This will internally create and register the resource.
130         OCStackResult result = OCPlatform::registerResource(
131                                     m_resourceHandle2, resourceURI, resourceTypeName,
132                                     resourceInterface, &entityHandler, resourceProperty);
133
134         if (OC_STACK_OK != result)
135         {
136             cout << "Resource creation was unsuccessful\n";
137         }
138     }
139
140     void createResource3()
141     {
142         std::string resourceURI = m_lightUri3; // URI of the resource
143         std::string resourceTypeName = "core.light";
144         std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
145
146         // OCResourceProperty is defined ocstack.h
147         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
148
149         // This will internally create and register the resource.
150         OCStackResult result = OCPlatform::registerResource(
151                                     m_resourceHandle3, resourceURI, resourceTypeName,
152                                     resourceInterface, &entityHandler, resourceProperty);
153
154         if (OC_STACK_OK != result)
155         {
156             cout << "Resource creation was unsuccessful\n";
157         }
158     }
159
160     OCResourceHandle getHandle()
161     {
162         return m_resourceHandle;
163     }
164
165     void addType(const std::string& type) const
166     {
167         OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
168         if (OC_STACK_OK != result)
169         {
170             cout << "Binding TypeName to Resource was unsuccessful\n";
171         }
172     }
173
174     void addInterface(const std::string& iface) const
175     {
176         OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
177         if (OC_STACK_OK != result)
178         {
179             cout << "Binding TypeName to Resource was unsuccessful\n";
180         }
181     }
182
183 };
184
185 void createPresenceResources()
186 {
187     std::array<std::string, numPresenceResources> resourceURI { {
188         "/a/fan",
189         "/a/led" } };
190     std::array<std::string, numPresenceResources> resourceTypeName { {
191         "core.fan",
192         "core.led" } };
193
194     std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
195     OCResourceHandle handle;
196     // OCResourceProperty is defined ocstack.h
197     uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
198
199     // This will internally create and register the resource.
200     OCStackResult result = OC_STACK_OK;
201     for(int i=0; i<numPresenceResources; i++)
202     {
203         result = OCPlatform::registerResource(handle,
204                 resourceURI.at(i), resourceTypeName.at(i), resourceInterface,
205                 &entityHandler, resourceProperty);
206         if (result != OC_STACK_OK)
207         {
208             cout << "Resource creation was unsuccessful with resource URI "
209                     << resourceURI.at(i);
210         }
211     }
212 }
213
214 // Create the instance of the resource class (in this case instance of class 'LightResource').
215 LightResource myLightResource;
216
217 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
218 {
219     cout << "\tIn Server CPP entity handler:\n";
220     return OC_EH_OK;
221 }
222
223 void DeletePlatformInfo()
224 {
225     delete[] platformInfo.platformID;
226     delete[] platformInfo.manufacturerName;
227     delete[] platformInfo.manufacturerUrl;
228     delete[] platformInfo.modelNumber;
229     delete[] platformInfo.dateOfManufacture;
230     delete[] platformInfo.platformVersion;
231     delete[] platformInfo.operatingSystemVersion;
232     delete[] platformInfo.hardwareVersion;
233     delete[] platformInfo.firmwareVersion;
234     delete[] platformInfo.supportUrl;
235     delete[] platformInfo.systemTime;
236 }
237
238 void DuplicateString(char ** targetString, std::string sourceString)
239 {
240     *targetString = new char[sourceString.length() + 1];
241     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
242 }
243
244 OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
245         std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
246         std::string platformVersion, std::string operatingSystemVersion,
247         std::string hardwareVersion, std::string firmwareVersion, std::string supportUrl,
248         std::string systemTime)
249 {
250     DuplicateString(&platformInfo.platformID, platformID);
251     DuplicateString(&platformInfo.manufacturerName, manufacturerName);
252     DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
253     DuplicateString(&platformInfo.modelNumber, modelNumber);
254     DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
255     DuplicateString(&platformInfo.platformVersion, platformVersion);
256     DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
257     DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
258     DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
259     DuplicateString(&platformInfo.supportUrl, supportUrl);
260     DuplicateString(&platformInfo.systemTime, systemTime);
261
262     return OC_STACK_OK;
263 }
264
265 OCStackResult SetDeviceInfo()
266 {
267     OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME,
268                                                         deviceName);
269     if (result != OC_STACK_OK)
270     {
271         cout << "Failed to set device name" << endl;
272         return result;
273     }
274
275     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
276                                           dataModelVersions);
277     if (result != OC_STACK_OK)
278     {
279         cout << "Failed to set data model versions" << endl;
280         return result;
281     }
282
283     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion);
284     if (result != OC_STACK_OK)
285     {
286         cout << "Failed to set spec version" << endl;
287         return result;
288     }
289
290     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
291                                           protocolIndependentID);
292     if (result != OC_STACK_OK)
293     {
294         cout << "Failed to set piid" << endl;
295         return result;
296     }
297
298     return OC_STACK_OK;
299 }
300
301 int main()
302 {
303     // Create PlatformConfig object
304     PlatformConfig cfg {
305         OC::ServiceType::InProc,
306         OC::ModeType::Server,
307         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
308         0,         // Uses randomly available port
309         OC::QualityOfService::LowQos
310     };
311
312     OCPlatform::Configure(cfg);
313     std::cout << "Starting server & setting platform info\n";
314
315     OCStackResult result = SetPlatformInfo(platformId, manufacturerName, manufacturerLink,
316             modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion,
317             hardwareVersion, firmwareVersion, supportLink, systemTime);
318
319     result = OCPlatform::registerPlatformInfo(platformInfo);
320
321     if (result != OC_STACK_OK)
322     {
323         std::cout << "Platform Registration failed\n";
324         return -1;
325     }
326
327     result = SetDeviceInfo();
328
329     if (result != OC_STACK_OK)
330     {
331         std::cout << "Device Registration failed\n";
332         return -1;
333     }
334
335     try
336     {
337         using namespace OC::OCPlatform;
338         // Time to Live is 30 seconds
339         startPresence(30);
340
341         // Invoke createResource function of class light.
342         myLightResource.createResource();
343         std :: cout << "Creating first resource of type \"core.light\"" << std :: endl;
344
345         std :: cout << "Will start creating/deleting resources for presence in 10 seconds.\n";
346
347         sleep(10);
348
349         std :: cout << "\nCreating the second resource of type \"core.light\"" <<  std :: endl;
350         sleep(1);
351
352         myLightResource.createResource2();
353
354         std :: cout << "Stopping presence\n" << std :: endl;
355         sleep(1);
356         stopPresence();
357
358         std :: cout << "Restarting presence\n" << std :: endl;
359         sleep(1);
360
361         startPresence(30);
362
363         std :: cout << "Creating a third resource of type \"core.light\"\n" << std :: endl;
364         sleep(1);
365
366         myLightResource.createResource3();
367
368         std :: cout << "Creating two non-operational resources.\"\n" << std :: endl;
369         sleep(1);
370
371         createPresenceResources();
372
373         DeletePlatformInfo();
374
375         // A condition variable will free the mutex it is given, then do a non-
376         // intensive block until 'notify' is called on it.  In this case, since we
377         // don't ever call cv.notify, this should be a non-processor intensive version
378         // of while(true);
379         std::mutex blocker;
380         std::condition_variable cv;
381         std::unique_lock<std::mutex> lock(blocker);
382         cv.wait(lock);
383     }
384     catch(OCException& e)
385     {
386         oclog() << "Exception in main: "<< e.what();
387     }
388
389     // No explicit call to stop the platform.
390     // When OCPlatform destructor is invoked, internally we do platform cleanup
391
392     return 0;
393 }
394