Imported Upstream version 1.1.0
[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 deviceInfo 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";
48
49 //Set of strings for each of platform info fields
50 std::string deviceName = "Bill's Battlestar";
51
52 //OCPlatformInfo Contains all the platform info to be stored
53 OCPlatformInfo platformInfo;
54
55 //OCDeviceInfo Contains all the device info to be stored
56 OCDeviceInfo deviceInfo;
57
58 void DeletePlatformInfo()
59 {
60     delete[] platformInfo.platformID;
61     delete[] platformInfo.manufacturerName;
62     delete[] platformInfo.manufacturerUrl;
63     delete[] platformInfo.modelNumber;
64     delete[] platformInfo.dateOfManufacture;
65     delete[] platformInfo.platformVersion;
66     delete[] platformInfo.operatingSystemVersion;
67     delete[] platformInfo.hardwareVersion;
68     delete[] platformInfo.firmwareVersion;
69     delete[] platformInfo.supportUrl;
70     delete[] platformInfo.systemTime;
71 }
72
73
74 void DeleteDeviceInfo()
75 {
76     delete[] deviceInfo.deviceName;
77 }
78
79 void DuplicateString(char ** targetString, std::string sourceString)
80 {
81     *targetString = new char[sourceString.length() + 1];
82     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
83 }
84
85 OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName,
86     std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture,
87     std::string platformVersion, std::string operatingSystemVersion, std::string hardwareVersion,
88     std::string firmwareVersion, std::string supportUrl, std::string systemTime)
89 {
90     DuplicateString(&platformInfo.platformID, platformID);
91     DuplicateString(&platformInfo.manufacturerName, manufacturerName);
92     DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
93     DuplicateString(&platformInfo.modelNumber, modelNumber);
94     DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
95     DuplicateString(&platformInfo.platformVersion, platformVersion);
96     DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
97     DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
98     DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
99     DuplicateString(&platformInfo.supportUrl, supportUrl);
100     DuplicateString(&platformInfo.systemTime, systemTime);
101     return OC_STACK_OK;
102 }
103
104
105 OCStackResult SetDeviceInfo(std::string deviceName)
106 {
107     DuplicateString(&deviceInfo.deviceName, deviceName);
108     return OC_STACK_OK;
109 }
110
111
112 int main()
113 {
114     // Create PlatformConfig object
115     PlatformConfig cfg {
116         OC::ServiceType::InProc,
117         OC::ModeType::Server,
118         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
119         0,         // Uses randomly available port
120         OC::QualityOfService::LowQos
121     };
122
123     OCPlatform::Configure(cfg);
124
125     std::cout<<"Starting server & setting platform info\n";
126
127     OCStackResult result = SetPlatformInfo(platformID, manufacturerName, manufacturerUrl,
128             modelNumber, dateOfManufacture, platformVersion,  operatingSystemVersion,
129             hardwareVersion, firmwareVersion,  supportUrl, systemTime);
130
131     result = OCPlatform::registerPlatformInfo(platformInfo);
132
133     if(result != OC_STACK_OK)
134     {
135         std::cout << "Platform Registration failed\n";
136         return -1;
137     }
138
139
140     result = SetDeviceInfo(deviceName);
141     OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
142     OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
143
144     result = OCPlatform::registerDeviceInfo(deviceInfo);
145
146     if(result != OC_STACK_OK)
147     {
148         std::cout << "Device Registration failed\n";
149         return -1;
150     }
151
152     DeletePlatformInfo();
153     DeleteDeviceInfo();
154
155     // A condition variable will free the mutex it is given, then do a non-
156     // intensive block until 'notify' is called on it.  In this case, since we
157     // don't ever call cv.notify, this should be a non-processor intensive version
158     // of while(true);
159     std::mutex blocker;
160     std::condition_variable cv;
161     std::unique_lock<std::mutex> lock(blocker);
162     cv.wait(lock, []{return false;});
163
164     // No explicit call to stop the platform.
165     // When OCPlatform::destructor is invoked, internally we do platform cleanup
166
167     return 0;
168
169 }