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"};
66 std::string protocolIndependentID = "41a75d38-83c3-4b83-a794-f2174990b30b";
68 // OCPlatformInfo Contains all the platform info to be stored
69 OCPlatformInfo platformInfo;
71 // Forward declaring the entityHandler
72 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request);
74 /// This class represents a single resource named 'lightResource'. This resource has
75 /// two simple properties named 'state' and 'power'
80 /// Access this property from a TB client
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;
92 LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
93 m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
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: */
98 /// This function internally calls registerResource API.
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.
105 // OCResourceProperty is defined ocstack.h
106 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
108 // This will internally create and register the resource.
109 OCStackResult result = OCPlatform::registerResource(
110 m_resourceHandle, resourceURI, resourceTypeName,
111 resourceInterface, &entityHandler, resourceProperty);
113 if (OC_STACK_OK != result)
115 cout << "Resource creation was unsuccessful\n";
119 /// This function internally calls registerResource API.
120 void createResource2()
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.
126 // OCResourceProperty is defined ocstack.h
127 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
129 // This will internally create and register the resource.
130 OCStackResult result = OCPlatform::registerResource(
131 m_resourceHandle2, resourceURI, resourceTypeName,
132 resourceInterface, &entityHandler, resourceProperty);
134 if (OC_STACK_OK != result)
136 cout << "Resource creation was unsuccessful\n";
140 void createResource3()
142 std::string resourceURI = m_lightUri3; // URI of the resource
143 std::string resourceTypeName = "core.light";
144 std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
146 // OCResourceProperty is defined ocstack.h
147 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
149 // This will internally create and register the resource.
150 OCStackResult result = OCPlatform::registerResource(
151 m_resourceHandle3, resourceURI, resourceTypeName,
152 resourceInterface, &entityHandler, resourceProperty);
154 if (OC_STACK_OK != result)
156 cout << "Resource creation was unsuccessful\n";
160 OCResourceHandle getHandle()
162 return m_resourceHandle;
165 void addType(const std::string& type) const
167 OCStackResult result = OC::OCPlatform::bindTypeToResource(m_resourceHandle, type);
168 if (OC_STACK_OK != result)
170 cout << "Binding TypeName to Resource was unsuccessful\n";
174 void addInterface(const std::string& iface) const
176 OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_resourceHandle, iface);
177 if (OC_STACK_OK != result)
179 cout << "Binding TypeName to Resource was unsuccessful\n";
185 void createPresenceResources()
187 std::array<std::string, numPresenceResources> resourceURI { {
190 std::array<std::string, numPresenceResources> resourceTypeName { {
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;
199 // This will internally create and register the resource.
200 OCStackResult result = OC_STACK_OK;
201 for(int i=0; i<numPresenceResources; i++)
203 result = OCPlatform::registerResource(handle,
204 resourceURI.at(i), resourceTypeName.at(i), resourceInterface,
205 &entityHandler, resourceProperty);
206 if (result != OC_STACK_OK)
208 cout << "Resource creation was unsuccessful with resource URI "
209 << resourceURI.at(i);
214 // Create the instance of the resource class (in this case instance of class 'LightResource').
215 LightResource myLightResource;
217 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> /*request*/)
219 cout << "\tIn Server CPP entity handler:\n";
223 void DeletePlatformInfo()
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;
238 void DuplicateString(char ** targetString, std::string sourceString)
240 *targetString = new char[sourceString.length() + 1];
241 strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
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)
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);
265 OCStackResult SetDeviceInfo()
267 OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME,
269 if (result != OC_STACK_OK)
271 cout << "Failed to set device name" << endl;
275 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
277 if (result != OC_STACK_OK)
279 cout << "Failed to set data model versions" << endl;
283 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion);
284 if (result != OC_STACK_OK)
286 cout << "Failed to set spec version" << endl;
290 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
291 protocolIndependentID);
292 if (result != OC_STACK_OK)
294 cout << "Failed to set piid" << endl;
303 // Create PlatformConfig object
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
312 OCPlatform::Configure(cfg);
313 std::cout << "Starting server & setting platform info\n";
315 OCStackResult result = SetPlatformInfo(platformId, manufacturerName, manufacturerLink,
316 modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion,
317 hardwareVersion, firmwareVersion, supportLink, systemTime);
319 result = OCPlatform::registerPlatformInfo(platformInfo);
321 if (result != OC_STACK_OK)
323 std::cout << "Platform Registration failed\n";
327 result = SetDeviceInfo();
329 if (result != OC_STACK_OK)
331 std::cout << "Device Registration failed\n";
337 using namespace OC::OCPlatform;
338 // Time to Live is 30 seconds
341 // Invoke createResource function of class light.
342 myLightResource.createResource();
343 std :: cout << "Creating first resource of type \"core.light\"" << std :: endl;
345 std :: cout << "Will start creating/deleting resources for presence in 10 seconds.\n";
349 std :: cout << "\nCreating the second resource of type \"core.light\"" << std :: endl;
352 myLightResource.createResource2();
354 std :: cout << "Stopping presence\n" << std :: endl;
358 std :: cout << "Restarting presence\n" << std :: endl;
363 std :: cout << "Creating a third resource of type \"core.light\"\n" << std :: endl;
366 myLightResource.createResource3();
368 std :: cout << "Creating two non-operational resources.\"\n" << std :: endl;
371 createPresenceResources();
373 DeletePlatformInfo();
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
380 std::condition_variable cv;
381 std::unique_lock<std::mutex> lock(blocker);
384 catch(OCException& e)
386 oclog() << "Exception in main: "<< e.what();
389 // No explicit call to stop the platform.
390 // When OCPlatform destructor is invoked, internally we do platform cleanup