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