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::vector<std::string> dataModelVersions = {"res.1.1.0"};
67 // OCPlatformInfo Contains all the platform info to be stored
68 OCPlatformInfo platformInfo;
70 // Forward declaring the entityHandler
71 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
73 /// This class represents a single resource named 'lightResource'. This resource has
74 /// two simple properties named 'state' and 'power'
79 /// Access this property from a TB client
82 std::string m_lightUri;
83 std::string m_lightUri2;
84 std::string m_lightUri3;
85 OCResourceHandle m_resourceHandle;
86 OCResourceHandle m_resourceHandle2;
87 OCResourceHandle m_resourceHandle3;
91 LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
92 m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
94 /* Note that this does not need to be a member function: for classes you do not have
95 access to, you can accomplish this with a free function: */
97 /// This function internally calls registerResource API.
100 std::string resourceURI = m_lightUri; // URI of the resource
101 std::string resourceTypeName = "core.light"; // resource type name.
102 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
104 // OCResourceProperty is defined ocstack.h
105 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
107 // This will internally create and register the resource.
108 OCStackResult result = OCPlatform::registerResource(
109 m_resourceHandle, resourceURI, resourceTypeName,
110 resourceInterface, &entityHandler, resourceProperty);
112 if (OC_STACK_OK != result)
114 cout << "Resource creation was unsuccessful\n";
118 /// This function internally calls registerResource API.
119 void createResource2()
121 std::string resourceURI = m_lightUri2; // URI of the resource
122 std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
123 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
125 // OCResourceProperty is defined ocstack.h
126 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
128 // This will internally create and register the resource.
129 OCStackResult result = OCPlatform::registerResource(
130 m_resourceHandle2, resourceURI, resourceTypeName,
131 resourceInterface, &entityHandler, resourceProperty);
133 if (OC_STACK_OK != result)
135 cout << "Resource creation was unsuccessful\n";
139 void createResource3()
141 std::string resourceURI = m_lightUri3; // URI of the resource
142 std::string resourceTypeName = "core.light";
143 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
145 // OCResourceProperty is defined ocstack.h
146 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
148 // This will internally create and register the resource.
149 OCStackResult result = OCPlatform::registerResource(
150 m_resourceHandle3, resourceURI, resourceTypeName,
151 resourceInterface, &entityHandler, resourceProperty);
153 if (OC_STACK_OK != result)
155 cout << "Resource creation was unsuccessful\n";
159 OCResourceHandle getHandle()
161 return m_resourceHandle;
164 void addType(const std::string& type) const
166 OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
167 if (OC_STACK_OK != result)
169 cout << "Binding TypeName to Resource was unsuccessful\n";
173 void addInterface(const std::string& iface) const
175 OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
176 if (OC_STACK_OK != result)
178 cout << "Binding TypeName to Resource was unsuccessful\n";
184 void createPresenceResources()
186 std::array<std::string, numPresenceResources> resourceURI { {
189 std::array<std::string, numPresenceResources> resourceTypeName { {
193 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
194 OCResourceHandle handle;
195 // OCResourceProperty is defined ocstack.h
196 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
198 // This will internally create and register the resource.
199 OCStackResult result = OC_STACK_OK;
200 for(int i=0; i<numPresenceResources; i++)
202 result = OCPlatform::registerResource(handle,
203 resourceURI.at(i), resourceTypeName.at(i), resourceInterface,
204 &entityHandler, resourceProperty);
205 if (result != OC_STACK_OK)
207 cout << "Resource creation was unsuccessful with resource URI "
208 << resourceURI.at(i);
213 // Create the instance of the resource class (in this case instance of class 'LightResource').
214 LightResource myLightResource;
216 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
218 cout << "\tIn Server CPP entity handler:\n";
222 void DeletePlatformInfo()
224 delete[] platformInfo.platformID;
225 delete[] platformInfo.manufacturerName;
226 delete[] platformInfo.manufacturerUrl;
227 delete[] platformInfo.modelNumber;
228 delete[] platformInfo.dateOfManufacture;
229 delete[] platformInfo.platformVersion;
230 delete[] platformInfo.operatingSystemVersion;
231 delete[] platformInfo.hardwareVersion;
232 delete[] platformInfo.firmwareVersion;
233 delete[] platformInfo.supportUrl;
234 delete[] platformInfo.systemTime;
237 void DuplicateString(char ** targetString, std::string sourceString)
239 *targetString = new char[sourceString.length() + 1];
240 strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
243 OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
244 std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
245 std::string platformVersion, std::string operatingSystemVersion,
246 std::string hardwareVersion, std::string firmwareVersion, std::string supportUrl,
247 std::string systemTime)
249 DuplicateString(&platformInfo.platformID, platformID);
250 DuplicateString(&platformInfo.manufacturerName, manufacturerName);
251 DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
252 DuplicateString(&platformInfo.modelNumber, modelNumber);
253 DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
254 DuplicateString(&platformInfo.platformVersion, platformVersion);
255 DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
256 DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
257 DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
258 DuplicateString(&platformInfo.supportUrl, supportUrl);
259 DuplicateString(&platformInfo.systemTime, systemTime);
264 OCStackResult SetDeviceInfo()
266 OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME,
268 if (result != OC_STACK_OK)
270 cout << "Failed to set device name" << endl;
274 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
276 if (result != OC_STACK_OK)
278 cout << "Failed to set data model versions" << endl;
282 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion);
283 if (result != OC_STACK_OK)
285 cout << "Failed to set spec version" << endl;
294 // Create PlatformConfig object
296 OC::ServiceType::InProc,
297 OC::ModeType::Server,
298 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
299 0, // Uses randomly available port
300 OC::QualityOfService::LowQos
303 OCPlatform::Configure(cfg);
304 std::cout << "Starting server & setting platform info\n";
306 OCStackResult result = SetPlatformInfo(platformId, manufacturerName, manufacturerLink,
307 modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion,
308 hardwareVersion, firmwareVersion, supportLink, systemTime);
310 result = OCPlatform::registerPlatformInfo(platformInfo);
312 if (result != OC_STACK_OK)
314 std::cout << "Platform Registration failed\n";
318 result = SetDeviceInfo();
319 if (result != OC_STACK_OK)
321 std::cout << "Device Registration failed\n";
327 using namespace OC::OCPlatform;
328 // Time to Live is 30 seconds
331 // Invoke createResource function of class light.
332 myLightResource.createResource();
333 std :: cout << "Creating first resource of type \"core.light\"" << std :: endl;
335 std :: cout << "Will start creating/deleting resources for presence in 10 seconds.\n";
339 std :: cout << "\nCreating the second resource of type \"core.light\"" << std :: endl;
342 myLightResource.createResource2();
344 std :: cout << "Stopping presence\n" << std :: endl;
348 std :: cout << "Restarting presence\n" << std :: endl;
353 std :: cout << "Creating a third resource of type \"core.light\"\n" << std :: endl;
356 myLightResource.createResource3();
358 std :: cout << "Creating two non-operational resources.\"\n" << std :: endl;
361 createPresenceResources();
363 DeletePlatformInfo();
365 // A condition variable will free the mutex it is given, then do a non-
366 // intensive block until 'notify' is called on it. In this case, since we
367 // don't ever call cv.notify, this should be a non-processor intensive version
370 std::condition_variable cv;
371 std::unique_lock<std::mutex> lock(blocker);
374 catch(OCException& e)
376 oclog() << "Exception in main: "<< e.what();
379 // No explicit call to stop the platform.
380 // When OCPlatform destructor is invoked, internally we do platform cleanup