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 the device discovery feature
23 ///The server sets the device related info. which can
24 ///be later retrieved by a client.
28 #include <condition_variable>
30 #include "OCPlatform.h"
35 //Set of strings for each of deviceInfo fields
36 std::string contentType = "myContentType";
37 std::string dateOfManufacture = "myDateOfManufacture";
38 std::string deviceName = "myDeviceName";
39 std::string deviceUUID = "myDeviceUUID";
40 std::string firmwareVersion = "myFirmwareVersion";
41 std::string hostName = "myHostName";
42 std::string manufacturerName = "myManufacturerNa";
43 std::string manufacturerUrl = "myManufacturerUrl";
44 std::string modelNumber = "myModelNumber";
45 std::string platformVersion = "myPlatformVersion";
46 std::string supportUrl = "mySupportUrl";
47 std::string version = "myVersion";
49 //OCDeviceInfo Contains all the device info to be stored
50 OCDeviceInfo deviceInfo;
52 void DeleteDeviceInfo()
54 delete deviceInfo.contentType;
55 delete deviceInfo.dateOfManufacture;
56 delete deviceInfo.deviceName;
57 delete deviceInfo.deviceUUID;
58 delete deviceInfo.firmwareVersion;
59 delete deviceInfo.hostName;
60 delete deviceInfo.manufacturerName;
61 delete deviceInfo.manufacturerUrl;
62 delete deviceInfo.modelNumber;
63 delete deviceInfo.platformVersion;
64 delete deviceInfo.supportUrl;
65 delete deviceInfo.version;
68 void DuplicateString(char ** targetString, std::string sourceString)
70 *targetString = new char[sourceString.length() + 1];
71 strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
74 OCStackResult SetDeviceInfo(std::string contentType, std::string dateOfManufacture,
75 std::string deviceName, std::string deviceUUID, std::string firmwareVersion,
76 std::string hostName, std::string manufacturerName, std::string manufacturerUrl,
77 std::string modelNumber, std::string platformVersion, std::string supportUrl,
80 if(manufacturerName.length() > MAX_MANUFACTURER_NAME_LENGTH)
82 return OC_STACK_INVALID_PARAM;
86 if(manufacturerUrl.length() > MAX_MANUFACTURER_URL_LENGTH)
88 return OC_STACK_INVALID_PARAM;
94 DuplicateString(&deviceInfo.contentType, contentType);
95 DuplicateString(&deviceInfo.dateOfManufacture, dateOfManufacture);
96 DuplicateString(&deviceInfo.deviceName, deviceName);
97 DuplicateString(&deviceInfo.deviceUUID, deviceUUID);
98 DuplicateString(&deviceInfo.firmwareVersion, firmwareVersion);
99 DuplicateString(&deviceInfo.hostName, hostName);
100 DuplicateString(&deviceInfo.manufacturerName, manufacturerName);
101 DuplicateString(&deviceInfo.manufacturerUrl, manufacturerUrl);
102 DuplicateString(&deviceInfo.modelNumber, modelNumber);
103 DuplicateString(&deviceInfo.platformVersion, platformVersion);
104 DuplicateString(&deviceInfo.supportUrl, supportUrl);
105 DuplicateString(&deviceInfo.version, version);
108 std::cout<<"String Copy failed!!\n";
109 return OC_STACK_ERROR;
120 // Create PlatformConfig object
122 OC::ServiceType::InProc,
123 OC::ModeType::Server,
124 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
125 0, // Uses randomly available port
126 OC::QualityOfService::LowQos
129 OCPlatform::Configure(cfg);
131 std::cout<<"Starting server & setting device info\n";
133 OCStackResult result = SetDeviceInfo(contentType, dateOfManufacture, deviceName,
134 deviceUUID, firmwareVersion, hostName, manufacturerName, manufacturerUrl,
135 modelNumber, platformVersion, supportUrl, version);
137 if(result != OC_STACK_OK)
139 std::cout << "Device Registration failed\n";
143 result = OCPlatform::registerDeviceInfo(deviceInfo);
145 if(result != OC_STACK_OK)
147 std::cout << "Device Registration failed\n";
153 // A condition variable will free the mutex it is given, then do a non-
154 // intensive block until 'notify' is called on it. In this case, since we
155 // don't ever call cv.notify, this should be a non-processor intensive version
158 std::condition_variable cv;
159 std::unique_lock<std::mutex> lock(blocker);
162 // No explicit call to stop the platform.
163 // When OCPlatform::destructor is invoked, internally we do platform cleanup