Imported Upstream version 0.9.1
[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 receivedPlatformInfo(const OCRepresentation& rep)
37 {
38     std::cout << "\nPlatform Information received ---->\n";
39     std::string value;
40     std::string values[22] =
41     {
42         "pi",   "Platform ID                    ",
43         "mnmn", "Manufacturer name              ",
44         "mnml", "Manufacturer url               ",
45         "mnmo", "Manufacturer Model No          ",
46         "mndt", "Manufactured Date              ",
47         "mnpv", "Manufacturer Platform Version  ",
48         "mnos", "Manufacturer OS version        ",
49         "mnhw", "Manufacturer hardware version  ",
50         "mnfv", "Manufacturer firmware version  ",
51         "mnsl", "Manufacturer support url       ",
52         "st",   "Manufacturer system time       "
53     };
54
55     for (int i = 0; i < 22; i += 2)
56     {
57         if(rep.getValue(values[i], value))
58         {
59             std::cout << values[i + 1] << " : "<< value << std::endl;
60         }
61     }
62 }
63
64 void receivedDeviceInfo(const OCRepresentation& rep)
65 {
66     std::cout << "Implement me !" << std::endl;
67 }
68
69 int main(int argc, char* argv[]) {
70
71     std::ostringstream platformDiscoveryRequest;
72     std::ostringstream deviceDiscoveryRequest;
73
74     std::string platformDiscoveryURI = "/oic/p";
75     std::string deviceDiscoveryURI   = "/oic/d";
76
77     OCConnectivityType connectivityType = OC_IPV4;
78
79     if(argc == 2)
80     {
81         try
82         {
83             std::size_t inputValLen;
84             int optionSelected = std::stoi(argv[1], &inputValLen);
85
86             if(inputValLen == strlen(argv[1]))
87             {
88                 if(optionSelected == 0)
89                 {
90                     connectivityType = OC_IPV4;
91                 }
92                 else if(optionSelected == 1)
93                 {
94                     // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported
95                     //connectivityType = OC_IPV6;
96                     connectivityType = OC_IPV4;
97                     std::cout << "IPv6 not currently supported. Using default IPv4" << std::endl;
98                 }
99                 else
100                 {
101                     std::cout << "Invalid connectivity type selected. Using default IPv4"
102                     << std::endl;
103                 }
104             }
105             else
106             {
107                 std::cout << "Invalid connectivity type selected. Using default IPv4" << std::endl;
108             }
109         }
110         catch(std::exception&)
111         {
112             std::cout << "Invalid input argument. Using IPv4 as connectivity type" << std::endl;
113         }
114     }
115     else
116     {
117         std::cout << "Usage devicediscoveryclient <connectivityType(0|1)>" << std::endl;
118         std::cout << "connectivityType: Default IPv4" << std::endl;
119         std::cout << "connectivityType 0: IPv4" << std::endl;
120         std::cout << "connectivityType 1: IPv6 (not currently supported)" << std::endl;
121     }
122     // Create PlatformConfig object
123     PlatformConfig cfg {
124         OC::ServiceType::InProc,
125         OC::ModeType::Client,
126         "0.0.0.0",
127         0,
128         OC::QualityOfService::LowQos
129     };
130
131     OCPlatform::Configure(cfg);
132     try
133     {
134         platformDiscoveryRequest << OC_MULTICAST_PREFIX << platformDiscoveryURI;
135         deviceDiscoveryRequest << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
136
137         OCStackResult ret;
138
139         std::cout<< "Querying for platform information... ";
140
141         ret = OCPlatform::getPlatformInfo("", platformDiscoveryRequest.str(), connectivityType,
142                 &receivedPlatformInfo);
143
144         if (ret == OC_STACK_OK)
145         {
146             std::cout << "done." << std::endl;
147         }
148         else
149         {
150             std::cout << "failed." << std::endl;
151         }
152
153         bool is_oic_d_implemented = false;
154         if (is_oic_d_implemented)
155         {
156             std::cout<< "Querying for device information... ";
157
158             ret = OCPlatform::getDeviceInfo("", deviceDiscoveryRequest.str(), connectivityType,
159                     &receivedDeviceInfo);
160
161             if (ret == OC_STACK_OK)
162             {
163                 std::cout << "done." << std::endl;
164             }
165             else
166             {
167                 std::cout << "failed." << std::endl;
168             }
169         }
170         // A condition variable will free the mutex it is given, then do a non-
171         // intensive block until 'notify' is called on it.  In this case, since we
172         // don't ever call cv.notify, this should be a non-processor intensive version
173         // of while(true);
174         std::mutex blocker;
175         std::condition_variable cv;
176         std::unique_lock<std::mutex> lock(blocker);
177         cv.wait(lock, []{return false;});
178
179     }catch(OCException& e)
180     {
181         std::cerr << "Failure in main thread: "<<e.reason()<<std::endl;
182     }
183
184     return 0;
185 }
186
187