Add deviceid in device payload
[platform/upstream/iotivity.git] / resource / examples / devicediscoveryserver.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 ///
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.
25 ///
26
27 #include <mutex>
28 #include <condition_variable>
29
30 #include "OCPlatform.h"
31 #include "OCApi.h"
32 #include "ocpayload.h"
33
34 using namespace OC;
35
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";
48
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::string  dataModelVersions = "res.1.1.0,sh.1.2.0";
53
54 // OCPlatformInfo Contains all the platform info to be stored
55 OCPlatformInfo platformInfo;
56
57 // OCDeviceInfo Contains all the device info to be stored
58 OCDeviceInfo deviceInfo;
59
60 void DeletePlatformInfo()
61 {
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;
73 }
74
75
76 void DeleteDeviceInfo()
77 {
78     delete[] deviceInfo.deviceName;
79     delete[] deviceInfo.specVersion;
80     OCFreeOCStringLL(deviceInfo.dataModelVersions);
81 }
82
83 void DuplicateString(char ** targetString, std::string sourceString)
84 {
85     *targetString = new char[sourceString.length() + 1];
86     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
87 }
88
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)
93 {
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);
105
106     return OC_STACK_OK;
107 }
108
109 OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions)
110 {
111     DuplicateString(&deviceInfo.deviceName, deviceName);
112
113     if (!specVersion.empty())
114     {
115         DuplicateString(&deviceInfo.specVersion, specVersion);
116     }
117
118     if (!dataModelVersions.empty())
119     {
120         OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str());
121     }
122
123     return OC_STACK_OK;
124 }
125
126 int main()
127 {
128     // Create PlatformConfig object
129     PlatformConfig cfg {
130         OC::ServiceType::InProc,
131         OC::ModeType::Server,
132         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
133         0,         // Uses randomly available port
134         OC::QualityOfService::LowQos
135     };
136
137     OCPlatform::Configure(cfg);
138
139     std::cout<<"Starting server & setting platform info\n";
140
141     OCStackResult result = SetPlatformInfo(platformID, manufacturerName, manufacturerLink,
142             modelNumber, dateOfManufacture, platformVersion,  operatingSystemVersion,
143             hardwareVersion, firmwareVersion,  supportLink, systemTime);
144
145     result = OCPlatform::registerPlatformInfo(platformInfo);
146
147     if(result != OC_STACK_OK)
148     {
149         std::cout << "Platform Registration failed\n";
150         return -1;
151     }
152
153
154     result = SetDeviceInfo(deviceName, specVersion, dataModelVersions);
155     OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
156     OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
157
158     result = OCPlatform::registerDeviceInfo(deviceInfo);
159
160     if(result != OC_STACK_OK)
161     {
162         std::cout << "Device Registration failed\n";
163         return -1;
164     }
165
166     DeletePlatformInfo();
167     DeleteDeviceInfo();
168
169     // A condition variable will free the mutex it is given, then do a non-
170     // intensive block until 'notify' is called on it.  In this case, since we
171     // don't ever call cv.notify, this should be a non-processor intensive version
172     // of while(true);
173     std::mutex blocker;
174     std::condition_variable cv;
175     std::unique_lock<std::mutex> lock(blocker);
176     cv.wait(lock, []{return false;});
177
178     // No explicit call to stop the platform.
179     // When OCPlatform::destructor is invoked, internally we do platform cleanup
180
181     return 0;
182
183 }