Imported Upstream version 0.9.2
[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 static void printUsage()
36 {
37     std::cout << "Usage devicediscoveryclient <0|1>" << std::endl;
38     std::cout << "connectivityType: Default IP" << std::endl;
39     std::cout << "connectivityType 0: IP" << std::endl;
40 }
41 //Callback after device information is received
42 void receivedPlatformInfo(const OCRepresentation& rep)
43 {
44     std::cout << "\nPlatform Information received ---->\n";
45     std::string value;
46     std::string values[] =
47     {
48         "pi",   "Platform ID                    ",
49         "mnmn", "Manufacturer name              ",
50         "mnml", "Manufacturer url               ",
51         "mnmo", "Manufacturer Model No          ",
52         "mndt", "Manufactured Date              ",
53         "mnpv", "Manufacturer Platform Version  ",
54         "mnos", "Manufacturer OS version        ",
55         "mnhw", "Manufacturer hardware version  ",
56         "mnfv", "Manufacturer firmware version  ",
57         "mnsl", "Manufacturer support url       ",
58         "st",   "Manufacturer system time       "
59     };
60
61     for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]) ; i += 2)
62     {
63         if(rep.getValue(values[i], value))
64         {
65             std::cout << values[i + 1] << " : "<< value << std::endl;
66         }
67     }
68 }
69
70 void receivedDeviceInfo(const OCRepresentation& rep)
71 {
72     std::cout << "\nDevice Information received ---->\n";
73     std::string value;
74     std::string values[] =
75     {
76         "di",   "Device ID        ",
77         "n",    "Device name      ",
78         "lcv",  "Spec version url ",
79         "dmv",  "Data Model Model ",
80     };
81
82     for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]) ; i += 2)
83     {
84         if(rep.getValue(values[i], value))
85         {
86             std::cout << values[i + 1] << " : "<< value << std::endl;
87         }
88     }
89 }
90
91 int main(int argc, char* argv[]) {
92
93     std::ostringstream platformDiscoveryRequest;
94     std::ostringstream deviceDiscoveryRequest;
95
96     std::string platformDiscoveryURI = "/oic/p";
97     std::string deviceDiscoveryURI   = "/oic/d";
98
99     //Default Connectivity type
100     OCConnectivityType connectivityType = CT_ADAPTER_IP;
101
102     if(argc == 2)
103     {
104         try
105         {
106             std::size_t inputValLen;
107             int optionSelected = std::stoi(argv[1], &inputValLen);
108
109             if(inputValLen == strlen(argv[1]))
110             {
111                 if(optionSelected == 0)
112                 {
113                     std::cout << "Using IP."<< std::endl;
114                     connectivityType = CT_ADAPTER_IP;
115                 }
116                 else
117                 {
118                     std::cout << "Invalid connectivity type selected." << std::endl;
119                     printUsage();
120                     return -1;
121                 }
122             }
123             else
124             {
125                 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
126             }
127         }
128         catch(std::exception&)
129         {
130             std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
131         }
132     }
133     else
134     {
135         printUsage();
136     }
137     // Create PlatformConfig object
138     PlatformConfig cfg {
139         OC::ServiceType::InProc,
140         OC::ModeType::Client,
141         "0.0.0.0",
142         0,
143         OC::QualityOfService::LowQos
144     };
145
146     OCPlatform::Configure(cfg);
147     try
148     {
149         platformDiscoveryRequest << OC_MULTICAST_PREFIX << platformDiscoveryURI;
150         deviceDiscoveryRequest << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
151
152         OCStackResult ret;
153
154         std::cout<< "Querying for platform information... ";
155
156         ret = OCPlatform::getPlatformInfo("", platformDiscoveryRequest.str(), connectivityType,
157                 &receivedPlatformInfo);
158
159         if (ret == OC_STACK_OK)
160         {
161             std::cout << "done." << std::endl;
162         }
163         else
164         {
165             std::cout << "failed." << std::endl;
166         }
167
168         std::cout<< "Querying for device information... ";
169
170         ret = OCPlatform::getDeviceInfo("", deviceDiscoveryRequest.str(), connectivityType,
171                 &receivedDeviceInfo);
172
173         if (ret == OC_STACK_OK)
174         {
175             std::cout << "done." << std::endl;
176         }
177         else
178         {
179             std::cout << "failed." << std::endl;
180         }
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