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 demonstrates platform and device discovery feature
23 ///The server sets the platform and device related info. which can
24 ///be later retrieved by a client.
28 #include <condition_variable>
30 #include "OCPlatform.h"
32 #include "ocpayload.h"
36 // Set of strings for each of platform Info fields
37 std::string platformID = "0A3E0D6F-DBF5-404E-8719-D6880042463A";
38 std::string manufacturerName = "myName";
39 std::string manufacturerLink = "https://www.example.com";
40 std::string modelNumber = "myModelNumber";
41 std::string dateOfManufacture = "2016-01-15";
42 std::string platformVersion = "platformVersion";
43 std::string operatingSystemVersion = "myOS";
44 std::string hardwareVersion = "myHardwareVersion";
45 std::string firmwareVersion = "1.0";
46 std::string supportLink = "https://www.examplesupport.com";
47 std::string systemTime = "2016-01-15T11.01";
49 // Set of strings for each of device info fields
50 std::string deviceName = "Bill's Battlestar";
51 std::string specVersion = "core.1.1.0";
52 std::vector<std::string> dataModelVersions = {"res.1.1.0", "sh.1.1.0"};
53 std::string protocolIndependentID = "4cae60c1-48cb-47dc-882e-dedec114f45c";
56 std::string deviceType = "oic.d.tv";
58 // OCPlatformInfo Contains all the platform info to be stored
59 OCPlatformInfo platformInfo;
61 void DeletePlatformInfo()
63 delete[] platformInfo.platformID;
64 delete[] platformInfo.manufacturerName;
65 delete[] platformInfo.manufacturerUrl;
66 delete[] platformInfo.modelNumber;
67 delete[] platformInfo.dateOfManufacture;
68 delete[] platformInfo.platformVersion;
69 delete[] platformInfo.operatingSystemVersion;
70 delete[] platformInfo.hardwareVersion;
71 delete[] platformInfo.firmwareVersion;
72 delete[] platformInfo.supportUrl;
73 delete[] platformInfo.systemTime;
76 void DuplicateString(char ** targetString, std::string sourceString)
78 *targetString = new char[sourceString.length() + 1];
79 strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
82 OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
83 std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
84 std::string platformVersion, std::string operatingSystemVersion, std::string hardwareVersion,
85 std::string firmwareVersion, std::string supportUrl, std::string systemTime)
87 DuplicateString(&platformInfo.platformID, platformID);
88 DuplicateString(&platformInfo.manufacturerName, manufacturerName);
89 DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
90 DuplicateString(&platformInfo.modelNumber, modelNumber);
91 DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
92 DuplicateString(&platformInfo.platformVersion, platformVersion);
93 DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
94 DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
95 DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
96 DuplicateString(&platformInfo.supportUrl, supportUrl);
97 DuplicateString(&platformInfo.systemTime, systemTime);
102 OCStackResult SetDeviceInfo()
104 OCStackResult result = OC_STACK_ERROR;
106 OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
109 std::cout << "Failed to find resource " << OC_RSRVD_DEVICE_URI << std::endl;
113 result = OCBindResourceTypeToResource(handle, deviceType.c_str());
114 if (result != OC_STACK_OK)
116 std::cout << "Failed to add device type" << std::endl;
120 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, deviceName);
121 if (result != OC_STACK_OK)
123 std::cout << "Failed to set device name" << std::endl;
127 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
129 if (result != OC_STACK_OK)
131 std::cout << "Failed to set data model versions" << std::endl;
135 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion);
136 if (result != OC_STACK_OK)
138 std::cout << "Failed to set spec version" << std::endl;
142 result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
143 protocolIndependentID);
144 if (result != OC_STACK_OK)
146 std::cout << "Failed to set piid" << std::endl;
155 // Create PlatformConfig object
157 OC::ServiceType::InProc,
158 OC::ModeType::Server,
159 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
160 0, // Uses randomly available port
161 OC::QualityOfService::LowQos
164 OCPlatform::Configure(cfg);
166 std::cout<<"Starting server & setting platform info\n";
168 OCStackResult result = SetPlatformInfo(platformID, manufacturerName, manufacturerLink,
169 modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion,
170 hardwareVersion, firmwareVersion, supportLink, systemTime);
172 result = OCPlatform::registerPlatformInfo(platformInfo);
174 if (result != OC_STACK_OK)
176 std::cout << "Platform Registration failed\n";
180 result = SetDeviceInfo();
182 if (result != OC_STACK_OK)
184 std::cout << "Device Registration failed\n";
188 DeletePlatformInfo();
190 // A condition variable will free the mutex it is given, then do a non-
191 // intensive block until 'notify' is called on it. In this case, since we
192 // don't ever call cv.notify, this should be a non-processor intensive version
195 std::condition_variable cv;
196 std::unique_lock<std::mutex> lock(blocker);
197 cv.wait(lock, []{return false;});
199 // No explicit call to stop the platform.
200 // When OCPlatform::destructor is invoked, internally we do platform cleanup