[IOT-1538] Add support for Protocol-Independent ID
[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::vector<std::string> dataModelVersions = {"res.1.1.0", "sh.1.1.0"};
53 std::string  protocolIndependentID = "4cae60c1-48cb-47dc-882e-dedec114f45c";
54
55 // Device type
56 std::string  deviceType = "oic.d.tv";
57
58 // OCPlatformInfo Contains all the platform info to be stored
59 OCPlatformInfo platformInfo;
60
61 void DeletePlatformInfo()
62 {
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;
74 }
75
76 void DuplicateString(char ** targetString, std::string sourceString)
77 {
78     *targetString = new char[sourceString.length() + 1];
79     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
80 }
81
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)
86 {
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);
98
99     return OC_STACK_OK;
100 }
101
102 OCStackResult SetDeviceInfo()
103 {
104     OCStackResult result = OC_STACK_ERROR;
105
106     OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
107     if (handle == NULL)
108     {
109         std::cout << "Failed to find resource " << OC_RSRVD_DEVICE_URI << std::endl;
110         return result;
111     }
112
113     result = OCBindResourceTypeToResource(handle, deviceType.c_str());
114     if (result != OC_STACK_OK)
115     {
116         std::cout << "Failed to add device type" << std::endl;
117         return result;
118     }
119
120     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, deviceName);
121     if (result != OC_STACK_OK)
122     {
123         std::cout << "Failed to set device name" << std::endl;
124         return result;
125     }
126
127     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
128                                           dataModelVersions);
129     if (result != OC_STACK_OK)
130     {
131         std::cout << "Failed to set data model versions" << std::endl;
132         return result;
133     }
134
135     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion);
136     if (result != OC_STACK_OK)
137     {
138         std::cout << "Failed to set spec version" << std::endl;
139         return result;
140     }
141
142     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
143                                           protocolIndependentID);
144     if (result != OC_STACK_OK)
145     {
146         std::cout << "Failed to set piid" << std::endl;
147         return result;
148     }
149
150     return OC_STACK_OK;
151 }
152
153 int main()
154 {
155     // Create PlatformConfig object
156     PlatformConfig cfg {
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
162     };
163
164     OCPlatform::Configure(cfg);
165
166     std::cout<<"Starting server & setting platform info\n";
167
168     OCStackResult result = SetPlatformInfo(platformID, manufacturerName, manufacturerLink,
169             modelNumber, dateOfManufacture, platformVersion,  operatingSystemVersion,
170             hardwareVersion, firmwareVersion,  supportLink, systemTime);
171
172     result = OCPlatform::registerPlatformInfo(platformInfo);
173
174     if (result != OC_STACK_OK)
175     {
176         std::cout << "Platform Registration failed\n";
177         return -1;
178     }
179
180     result = SetDeviceInfo();
181
182     if (result != OC_STACK_OK)
183     {
184         std::cout << "Device Registration failed\n";
185         return -1;
186     }
187
188     DeletePlatformInfo();
189
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
193     // of while(true);
194     std::mutex blocker;
195     std::condition_variable cv;
196     std::unique_lock<std::mutex> lock(blocker);
197     cv.wait(lock, []{return false;});
198
199     // No explicit call to stop the platform.
200     // When OCPlatform::destructor is invoked, internally we do platform cleanup
201
202     return 0;
203
204 }