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