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 //Callback after device information is received
36 void receivedPlatformInfo(const OCRepresentation& rep)
38 std::cout << "\nPlatform Information received ---->\n";
40 std::string values[22] =
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 "
55 for (int i = 0; i < 22; i += 2)
57 if(rep.getValue(values[i], value))
59 std::cout << values[i + 1] << " : "<< value << std::endl;
64 void receivedDeviceInfo(const OCRepresentation& rep)
66 std::cout << "Implement me !" << std::endl;
69 int main(int argc, char* argv[]) {
71 std::ostringstream platformDiscoveryRequest;
72 std::ostringstream deviceDiscoveryRequest;
74 std::string platformDiscoveryURI = "/oic/p";
75 std::string deviceDiscoveryURI = "/oic/d";
77 OCConnectivityType connectivityType = OC_IPV4;
83 std::size_t inputValLen;
84 int optionSelected = std::stoi(argv[1], &inputValLen);
86 if(inputValLen == strlen(argv[1]))
88 if(optionSelected == 0)
90 connectivityType = OC_IPV4;
92 else if(optionSelected == 1)
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;
101 std::cout << "Invalid connectivity type selected. Using default IPv4"
107 std::cout << "Invalid connectivity type selected. Using default IPv4" << std::endl;
110 catch(std::exception&)
112 std::cout << "Invalid input argument. Using IPv4 as connectivity type" << std::endl;
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;
122 // Create PlatformConfig object
124 OC::ServiceType::InProc,
125 OC::ModeType::Client,
128 OC::QualityOfService::LowQos
131 OCPlatform::Configure(cfg);
134 platformDiscoveryRequest << OC_MULTICAST_PREFIX << platformDiscoveryURI;
135 deviceDiscoveryRequest << OC_MULTICAST_PREFIX << deviceDiscoveryURI;
139 std::cout<< "Querying for platform information... ";
141 ret = OCPlatform::getPlatformInfo("", platformDiscoveryRequest.str(), connectivityType,
142 &receivedPlatformInfo);
144 if (ret == OC_STACK_OK)
146 std::cout << "done." << std::endl;
150 std::cout << "failed." << std::endl;
153 bool is_oic_d_implemented = false;
154 if (is_oic_d_implemented)
156 std::cout<< "Querying for device information... ";
158 ret = OCPlatform::getDeviceInfo("", deviceDiscoveryRequest.str(), connectivityType,
159 &receivedDeviceInfo);
161 if (ret == OC_STACK_OK)
163 std::cout << "done." << std::endl;
167 std::cout << "failed." << std::endl;
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
175 std::condition_variable cv;
176 std::unique_lock<std::mutex> lock(blocker);
177 cv.wait(lock, []{return false;});
179 }catch(OCException& e)
181 std::cerr << "Failure in main thread: "<<e.reason()<<std::endl;