Deprecate OCSetDeviceInfo and registerDeviceInfo
[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
54 // Device type
55 std::string  deviceType = "oic.d.tv";
56
57 // OCPlatformInfo Contains all the platform info to be stored
58 OCPlatformInfo platformInfo;
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 void DuplicateString(char ** targetString, std::string sourceString)
76 {
77     *targetString = new char[sourceString.length() + 1];
78     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
79 }
80
81 OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
82     std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
83     std::string platformVersion, std::string operatingSystemVersion, std::string hardwareVersion,
84     std::string firmwareVersion, std::string supportUrl, std::string systemTime)
85 {
86     DuplicateString(&platformInfo.platformID, platformID);
87     DuplicateString(&platformInfo.manufacturerName, manufacturerName);
88     DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
89     DuplicateString(&platformInfo.modelNumber, modelNumber);
90     DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
91     DuplicateString(&platformInfo.platformVersion, platformVersion);
92     DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
93     DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
94     DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
95     DuplicateString(&platformInfo.supportUrl, supportUrl);
96     DuplicateString(&platformInfo.systemTime, systemTime);
97
98     return OC_STACK_OK;
99 }
100
101 OCStackResult SetDeviceInfo()
102 {
103     OCStackResult result = OC_STACK_ERROR;
104
105     OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
106     if (handle == NULL)
107     {
108         std::cout << "Failed to find resource " << OC_RSRVD_DEVICE_URI << std::endl;
109         return result;
110     }
111
112     result = OCBindResourceTypeToResource(handle, deviceType.c_str());
113     if (result != OC_STACK_OK)
114     {
115         std::cout << "Failed to add device type" << std::endl;
116         return result;
117     }
118
119     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, deviceName);
120     if (result != OC_STACK_OK)
121     {
122         std::cout << "Failed to set device name" << std::endl;
123         return result;
124     }
125
126     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
127                                           dataModelVersions);
128     if (result != OC_STACK_OK)
129     {
130         std::cout << "Failed to set data model versions" << std::endl;
131         return result;
132     }
133
134     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion);
135     if (result != OC_STACK_OK)
136     {
137         std::cout << "Failed to set spec version" << std::endl;
138         return result;
139     }
140
141     return OC_STACK_OK;
142 }
143
144 int main()
145 {
146     // Create PlatformConfig object
147     PlatformConfig cfg {
148         OC::ServiceType::InProc,
149         OC::ModeType::Server,
150         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
151         0,         // Uses randomly available port
152         OC::QualityOfService::LowQos
153     };
154
155     OCPlatform::Configure(cfg);
156
157     std::cout<<"Starting server & setting platform info\n";
158
159     OCStackResult result = SetPlatformInfo(platformID, manufacturerName, manufacturerLink,
160             modelNumber, dateOfManufacture, platformVersion,  operatingSystemVersion,
161             hardwareVersion, firmwareVersion,  supportLink, systemTime);
162
163     result = OCPlatform::registerPlatformInfo(platformInfo);
164
165     if (result != OC_STACK_OK)
166     {
167         std::cout << "Platform Registration failed\n";
168         return -1;
169     }
170
171     result = SetDeviceInfo();
172
173     if (result != OC_STACK_OK)
174     {
175         std::cout << "Device Registration failed\n";
176         return -1;
177     }
178
179     DeletePlatformInfo();
180
181     // A condition variable will free the mutex it is given, then do a non-
182     // intensive block until 'notify' is called on it.  In this case, since we
183     // don't ever call cv.notify, this should be a non-processor intensive version
184     // of while(true);
185     std::mutex blocker;
186     std::condition_variable cv;
187     std::unique_lock<std::mutex> lock(blocker);
188     cv.wait(lock, []{return false;});
189
190     // No explicit call to stop the platform.
191     // When OCPlatform::destructor is invoked, internally we do platform cleanup
192
193     return 0;
194
195 }