1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 ///This sample demonstrates the device discovery feature
23 ///The client queries for the device related information
24 ///stored by the server.
28 #include <condition_variable>
30 #include "OCPlatform.h"
35 static void printUsage()
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;
41 //Callback after device information is received
42 void receivedPlatformInfo(const OCRepresentation& rep)
44 std::cout << "\nPlatform Information received ---->\n";
46 std::string values[] =
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 "
61 for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]) ; i += 2)
63 if(rep.getValue(values[i], value))
65 std::cout << values[i + 1] << " : "<< value << std::endl;
70 void receivedDeviceInfo(const OCRepresentation& rep)
72 std::cout << "\nDevice Information received ---->\n";
74 std::string values[] =
78 "lcv", "Spec version url ",
79 "dmv", "Data Model Model ",
82 for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]) ; i += 2)
84 if(rep.getValue(values[i], value))
86 std::cout << values[i + 1] << " : "<< value << std::endl;
91 int main(int argc, char* argv[]) {
93 std::ostringstream platformDiscoveryRequest;
94 std::ostringstream deviceDiscoveryRequest;
96 std::string platformDiscoveryURI = "/oic/p";
97 std::string deviceDiscoveryURI = "/oic/d";
99 //Default Connectivity type
100 OCConnectivityType connectivityType = CT_ADAPTER_IP;
106 std::size_t inputValLen;
107 int optionSelected = std::stoi(argv[1], &inputValLen);
109 if(inputValLen == strlen(argv[1]))
111 if(optionSelected == 0)
113 std::cout << "Using IP."<< std::endl;
114 connectivityType = CT_ADAPTER_IP;
118 std::cout << "Invalid connectivity type selected." << std::endl;
125 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
128 catch(std::exception&)
130 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
137 // Create PlatformConfig object
139 OC::ServiceType::InProc,
140 OC::ModeType::Client,
143 OC::QualityOfService::LowQos
146 OCPlatform::Configure(cfg);
149 platformDiscoveryRequest << OC_MULTICAST_PREFIX << platformDiscoveryURI;
150 deviceDiscoveryRequest << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
154 std::cout<< "Querying for platform information... ";
156 ret = OCPlatform::getPlatformInfo("", platformDiscoveryRequest.str(), connectivityType,
157 &receivedPlatformInfo);
159 if (ret == OC_STACK_OK)
161 std::cout << "done." << std::endl;
165 std::cout << "failed." << std::endl;
168 std::cout<< "Querying for device information... ";
170 ret = OCPlatform::getDeviceInfo("", deviceDiscoveryRequest.str(), connectivityType,
171 &receivedDeviceInfo);
173 if (ret == OC_STACK_OK)
175 std::cout << "done." << std::endl;
179 std::cout << "failed." << std::endl;
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
187 std::condition_variable cv;
188 std::unique_lock<std::mutex> lock(blocker);
189 cv.wait(lock, []{return false;});
191 }catch(OCException& e)
193 std::cerr << "Failure in main thread: "<<e.reason()<<std::endl;