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 dateOfManufacture = "myDateOfManufacture";
38 std::string firmwareVersion = "my.Firmware.Version";
39 std::string manufacturerName = "myName";
40 std::string operatingSystemVersion = "myOS";
41 std::string hardwareVersion = "myHardwareVersion";
42 std::string platformID = "myPlatformID";
43 std::string manufacturerUrl = "www.myurl.com";
44 std::string modelNumber = "myModelNumber";
45 std::string platformVersion = "platformVersion";
46 std::string supportUrl = "www.mysupporturl.com";
47 std::string systemTime = "mySystemTime";
49 //Set of strings for each of device info fields
50 std::string deviceName = "Bill's Battlestar";
51 std::string specVersion = "myDeviceSpecVersion";
52 std::string dataModelVersions = "myDeviceModelVersion";
54 //OCPlatformInfo Contains all the platform info to be stored
55 OCPlatformInfo platformInfo;
57 //OCDeviceInfo Contains all the device info to be stored
58 OCDeviceInfo deviceInfo;
60 void DeletePlatformInfo()
62 delete[] platformInfo.platformID;
63 delete[] platformInfo.manufacturerName;
64 delete[] platformInfo.manufacturerUrl;
65 delete[] platformInfo.modelNumber;
66 delete[] platformInfo.dateOfManufacture;
67 delete[] platformInfo.platformVersion;
68 delete[] platformInfo.operatingSystemVersion;
69 delete[] platformInfo.hardwareVersion;
70 delete[] platformInfo.firmwareVersion;
71 delete[] platformInfo.supportUrl;
72 delete[] platformInfo.systemTime;
76 void DeleteDeviceInfo()
78 delete[] deviceInfo.deviceName;
79 delete[] deviceInfo.specVersion;
80 OCFreeOCStringLL(deviceInfo.dataModelVersions);
83 void DuplicateString(char ** targetString, std::string sourceString)
85 *targetString = new char[sourceString.length() + 1];
86 strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
89 OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
90 std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
91 std::string platformVersion, std::string operatingSystemVersion, std::string hardwareVersion,
92 std::string firmwareVersion, std::string supportUrl, std::string systemTime)
94 DuplicateString(&platformInfo.platformID, platformID);
95 DuplicateString(&platformInfo.manufacturerName, manufacturerName);
96 DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
97 DuplicateString(&platformInfo.modelNumber, modelNumber);
98 DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
99 DuplicateString(&platformInfo.platformVersion, platformVersion);
100 DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
101 DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
102 DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
103 DuplicateString(&platformInfo.supportUrl, supportUrl);
104 DuplicateString(&platformInfo.systemTime, systemTime);
109 OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions)
111 DuplicateString(&deviceInfo.deviceName, deviceName);
113 if (!specVersion.empty())
114 DuplicateString(&deviceInfo.specVersion, specVersion);
116 if (!dataModelVersions.empty())
117 OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str());
125 // Create PlatformConfig object
127 OC::ServiceType::InProc,
128 OC::ModeType::Server,
129 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
130 0, // Uses randomly available port
131 OC::QualityOfService::LowQos
134 OCPlatform::Configure(cfg);
136 std::cout<<"Starting server & setting platform info\n";
138 OCStackResult result = SetPlatformInfo(platformID, manufacturerName, manufacturerUrl,
139 modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion,
140 hardwareVersion, firmwareVersion, supportUrl, systemTime);
142 result = OCPlatform::registerPlatformInfo(platformInfo);
144 if(result != OC_STACK_OK)
146 std::cout << "Platform Registration failed\n";
151 result = SetDeviceInfo(deviceName, specVersion, dataModelVersions);
152 OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
153 OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
155 result = OCPlatform::registerDeviceInfo(deviceInfo);
157 if(result != OC_STACK_OK)
159 std::cout << "Device Registration failed\n";
163 DeletePlatformInfo();
166 // A condition variable will free the mutex it is given, then do a non-
167 // intensive block until 'notify' is called on it. In this case, since we
168 // don't ever call cv.notify, this should be a non-processor intensive version
171 std::condition_variable cv;
172 std::unique_lock<std::mutex> lock(blocker);
173 cv.wait(lock, []{return false;});
175 // No explicit call to stop the platform.
176 // When OCPlatform::destructor is invoked, internally we do platform cleanup