1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample provides steps to define an interface for a resource
23 /// (properties and methods) and host this resource on the server.
26 #include "iotivity_config.h"
34 #include <condition_variable>
36 #include "OCPlatform.h"
38 #include "ocpayload.h"
47 #define numPresenceResources (2)
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";
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::string dataModelVersions = "res.1.1.0";
67 // OCPlatformInfo Contains all the platform info to be stored
68 OCPlatformInfo platformInfo;
70 // OCDeviceInfo Contains all the device info to be stored
71 OCDeviceInfo deviceInfo;
73 // Forward declaring the entityHandler
74 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
76 /// This class represents a single resource named 'lightResource'. This resource has
77 /// two simple properties named 'state' and 'power'
82 /// Access this property from a TB client
85 std::string m_lightUri;
86 std::string m_lightUri2;
87 std::string m_lightUri3;
88 OCResourceHandle m_resourceHandle;
89 OCResourceHandle m_resourceHandle2;
90 OCResourceHandle m_resourceHandle3;
94 LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
95 m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
97 /* Note that this does not need to be a member function: for classes you do not have
98 access to, you can accomplish this with a free function: */
100 /// This function internally calls registerResource API.
101 void createResource()
103 std::string resourceURI = m_lightUri; // URI of the resource
104 std::string resourceTypeName = "core.light"; // resource type name.
105 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
107 // OCResourceProperty is defined ocstack.h
108 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
110 // This will internally create and register the resource.
111 OCStackResult result = OCPlatform::registerResource(
112 m_resourceHandle, resourceURI, resourceTypeName,
113 resourceInterface, &entityHandler, resourceProperty);
115 if (OC_STACK_OK != result)
117 cout << "Resource creation was unsuccessful\n";
121 /// This function internally calls registerResource API.
122 void createResource2()
124 std::string resourceURI = m_lightUri2; // URI of the resource
125 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
126 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
128 // OCResourceProperty is defined ocstack.h
129 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
131 // This will internally create and register the resource.
132 OCStackResult result = OCPlatform::registerResource(
133 m_resourceHandle2, resourceURI, resourceTypeName,
134 resourceInterface, &entityHandler, resourceProperty);
136 if (OC_STACK_OK != result)
138 cout << "Resource creation was unsuccessful\n";
142 void createResource3()
144 std::string resourceURI = m_lightUri3; // URI of the resource
145 std::string resourceTypeName = "core.light";
146 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
148 // OCResourceProperty is defined ocstack.h
149 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
151 // This will internally create and register the resource.
152 OCStackResult result = OCPlatform::registerResource(
153 m_resourceHandle3, resourceURI, resourceTypeName,
154 resourceInterface, &entityHandler, resourceProperty);
156 if (OC_STACK_OK != result)
158 cout << "Resource creation was unsuccessful\n";
162 OCResourceHandle getHandle()
164 return m_resourceHandle;
167 void addType(const std::string& type) const
169 OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
170 if (OC_STACK_OK != result)
172 cout << "Binding TypeName to Resource was unsuccessful\n";
176 void addInterface(const std::string& iface) const
178 OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
179 if (OC_STACK_OK != result)
181 cout << "Binding TypeName to Resource was unsuccessful\n";
187 void createPresenceResources()
189 std::array<std::string, numPresenceResources> resourceURI { {
192 std::array<std::string, numPresenceResources> resourceTypeName { {
196 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
197 OCResourceHandle handle;
198 // OCResourceProperty is defined ocstack.h
199 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
201 // This will internally create and register the resource.
202 OCStackResult result = OC_STACK_OK;
203 for(int i=0; i<numPresenceResources; i++)
205 result = OCPlatform::registerResource(handle,
206 resourceURI.at(i), resourceTypeName.at(i), resourceInterface,
207 &entityHandler, resourceProperty);
208 if (result != OC_STACK_OK)
210 cout << "Resource creation was unsuccessful with resource URI "
211 << resourceURI.at(i);
216 // Create the instance of the resource class (in this case instance of class 'LightResource').
217 LightResource myLightResource;
219 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
221 cout << "\tIn Server CPP entity handler:\n";
225 void DeletePlatformInfo()
227 delete[] platformInfo.platformID;
228 delete[] platformInfo.manufacturerName;
229 delete[] platformInfo.manufacturerUrl;
230 delete[] platformInfo.modelNumber;
231 delete[] platformInfo.dateOfManufacture;
232 delete[] platformInfo.platformVersion;
233 delete[] platformInfo.operatingSystemVersion;
234 delete[] platformInfo.hardwareVersion;
235 delete[] platformInfo.firmwareVersion;
236 delete[] platformInfo.supportUrl;
237 delete[] platformInfo.systemTime;
240 void DeleteDeviceInfo()
242 delete[] deviceInfo.deviceName;
243 delete[] deviceInfo.specVersion;
244 OCFreeOCStringLL(deviceInfo.dataModelVersions);
247 void DuplicateString(char ** targetString, std::string sourceString)
249 *targetString = new char[sourceString.length() + 1];
250 strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
253 OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
254 std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
255 std::string platformVersion, std::string operatingSystemVersion,
256 std::string hardwareVersion, std::string firmwareVersion, std::string supportUrl,
257 std::string systemTime)
259 DuplicateString(&platformInfo.platformID, platformID);
260 DuplicateString(&platformInfo.manufacturerName, manufacturerName);
261 DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
262 DuplicateString(&platformInfo.modelNumber, modelNumber);
263 DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
264 DuplicateString(&platformInfo.platformVersion, platformVersion);
265 DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
266 DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
267 DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
268 DuplicateString(&platformInfo.supportUrl, supportUrl);
269 DuplicateString(&platformInfo.systemTime, systemTime);
274 OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions)
276 DuplicateString(&deviceInfo.deviceName, deviceName);
278 if (!specVersion.empty())
280 DuplicateString(&deviceInfo.specVersion, specVersion);
283 if (!dataModelVersions.empty())
285 OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str());
293 // Create PlatformConfig object
295 OC::ServiceType::InProc,
296 OC::ModeType::Server,
297 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
298 0, // Uses randomly available port
299 OC::QualityOfService::LowQos
302 OCPlatform::Configure(cfg);
303 std::cout << "Starting server & setting platform info\n";
305 OCStackResult result = SetPlatformInfo(platformId, manufacturerName, manufacturerLink,
306 modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion,
307 hardwareVersion, firmwareVersion, supportLink, systemTime);
309 result = OCPlatform::registerPlatformInfo(platformInfo);
311 if (result != OC_STACK_OK)
313 std::cout << "Platform Registration failed\n";
317 result = SetDeviceInfo(deviceName, specVersion, dataModelVersions);
318 OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
320 result = OCPlatform::registerDeviceInfo(deviceInfo);
322 if (result != OC_STACK_OK)
324 std::cout << "Device Registration failed\n";
329 using namespace OC::OCPlatform;
330 // Time to Live is 30 seconds
333 // Invoke createResource function of class light.
334 myLightResource.createResource();
335 std :: cout << "Creating first resource of type \"core.light\"" << std :: endl;
337 std :: cout << "Will start creating/deleting resources for presence in 10 seconds.\n";
341 std :: cout << "\nCreating the second resource of type \"core.light\"" << std :: endl;
344 myLightResource.createResource2();
346 std :: cout << "Stopping presence\n" << std :: endl;
350 std :: cout << "Restarting presence\n" << std :: endl;
355 std :: cout << "Creating a third resource of type \"core.light\"\n" << std :: endl;
358 myLightResource.createResource3();
360 std :: cout << "Creating two non-operational resources.\"\n" << std :: endl;
363 createPresenceResources();
365 DeletePlatformInfo();
368 // A condition variable will free the mutex it is given, then do a non-
369 // intensive block until 'notify' is called on it. In this case, since we
370 // don't ever call cv.notify, this should be a non-processor intensive version
373 std::condition_variable cv;
374 std::unique_lock<std::mutex> lock(blocker);
377 catch(OCException& e)
379 oclog() << "Exception in main: "<< e.what();
382 // No explicit call to stop the platform.
383 // When OCPlatform destructor is invoked, internally we do platform cleanup