5c7511c1428fe876fef71cfde83f9c042f92d12e
[platform/upstream/iotivity.git] / resource / examples / devicediscoveryclient.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 client queries for the device related information
24 ///stored by the server.
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 //Callback after device information is received
36 void receivedDeviceInfo(const OCRepresentation& rep)
37 {
38     std::cout << "\nDevice Information received ---->\n";
39
40     std::string contentType;
41     std::string dateOfManufacture;
42     std::string deviceName;
43     std::string deviceUUID;
44     std::string firmwareVersion;
45     std::string hostName;
46     std::string manufacturerName;
47     std::string manufacturerUrl;
48     std::string modelNumber;
49     std::string platformVersion;
50     std::string supportUrl;
51     std::string version;
52
53     if(rep.getValue("ct", contentType))
54     {
55         std::cout << "Content Type: " << contentType << std::endl;
56     }
57
58     if(rep.getValue("mndt", dateOfManufacture))
59     {
60         std::cout << "Date of manufacture: " << dateOfManufacture << std::endl;
61     }
62
63     if(rep.getValue("dn", deviceName))
64     {
65         std::cout << "Device Name: " << deviceName << std::endl;
66     }
67
68     if(rep.getValue("di", deviceUUID))
69     {
70         std::cout << "Device UUID: " << deviceUUID << std::endl;
71     }
72
73     if(rep.getValue("mnfv", firmwareVersion))
74     {
75         std::cout << "Firmware Version: " << firmwareVersion << std::endl;
76     }
77
78     if(rep.getValue("hn", hostName))
79     {
80         std::cout << "Host Name: " << hostName << std::endl;
81     }
82
83     if(rep.getValue("mnmn", manufacturerName))
84     {
85         std::cout << "Manufacturer Name: " << manufacturerName << std::endl;
86     }
87
88     if(rep.getValue("mnml", manufacturerUrl))
89     {
90         std::cout << "Manufacturer Url: " << manufacturerUrl << std::endl;
91     }
92
93     if(rep.getValue("mnmo", modelNumber))
94     {
95         std::cout << "Model No. : " << modelNumber << std::endl;
96     }
97
98     if(rep.getValue("mnpv", platformVersion))
99     {
100         std::cout << "Platform Version: " << platformVersion << std::endl;
101     }
102
103     if(rep.getValue("mnsl", supportUrl))
104     {
105         std::cout << "Support URL: " << supportUrl << std::endl;
106     }
107
108     if(rep.getValue("icv", version))
109     {
110         std::cout << "Version: " << version << std::endl;
111     }
112 }
113
114 int main(int argc, char* argv[]) {
115
116     std::ostringstream requestURI;
117     std::string deviceDiscoveryURI = "/oc/core/d";
118
119     OCConnectivityType connectivityType = OC_IPV4;
120
121     if(argc == 2)
122     {
123         try
124         {
125             std::size_t inputValLen;
126             int optionSelected = std::stoi(argv[1], &inputValLen);
127
128             if(inputValLen == strlen(argv[1]))
129             {
130                 if(optionSelected == 0)
131                 {
132                     connectivityType = OC_IPV4;
133                 }
134                 else if(optionSelected == 1)
135                 {
136                     // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported
137                     //connectivityType = OC_IPV6;
138                     connectivityType = OC_IPV4;
139                     std::cout << "IPv6 not currently supported. Using default IPv4" << std::endl;
140                 }
141                 else
142                 {
143                     std::cout << "Invalid connectivity type selected. Using default IPv4"
144                     << std::endl;
145                 }
146             }
147             else
148             {
149                 std::cout << "Invalid connectivity type selected. Using default IPv4" << std::endl;
150             }
151         }
152         catch(std::exception&)
153         {
154             std::cout << "Invalid input argument. Using IPv4 as connectivity type" << std::endl;
155         }
156     }
157     else
158     {
159         std::cout << "Usage devicediscoveryclient <connectivityType(0|1)>" << std::endl;
160         std::cout << "connectivityType: Default IPv4" << std::endl;
161         std::cout << "connectivityType 0: IPv4" << std::endl;
162         std::cout << "connectivityType 1: IPv6 (not currently supported)" << std::endl;
163     }
164     // Create PlatformConfig object
165     PlatformConfig cfg {
166         OC::ServiceType::InProc,
167         OC::ModeType::Client,
168         "0.0.0.0",
169         0,
170         OC::QualityOfService::LowQos
171     };
172
173     OCPlatform::Configure(cfg);
174     try
175     {
176         requestURI << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
177
178         OCPlatform::getDeviceInfo("", requestURI.str(), connectivityType,
179                 &receivedDeviceInfo);
180         std::cout<< "Querying for device information... " <<std::endl;
181
182         // A condition variable will free the mutex it is given, then do a non-
183         // intensive block until 'notify' is called on it.  In this case, since we
184         // don't ever call cv.notify, this should be a non-processor intensive version
185         // of while(true);
186         std::mutex blocker;
187         std::condition_variable cv;
188         std::unique_lock<std::mutex> lock(blocker);
189         cv.wait(lock, []{return false;});
190
191     }catch(OCException& e)
192     {
193         std::cerr << "Failure in main thread: "<<e.reason()<<std::endl;
194     }
195
196     return 0;
197 }
198
199