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