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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// This fridgeclient represents a client trying to discover the associated
22 /// fridgeserver. The device resource is the only one available for discovery
23 /// on the server, so we have to take the fact that we know the device tag
24 /// to then generate a Resource object
28 #include <condition_variable>
31 #include "OCPlatform.h"
35 namespace PH = std::placeholders;
37 // Option ID for API version and client token
38 const uint16_t API_VERSION = 2048;
39 const uint16_t TOKEN = 3000;
41 static void printUsage()
43 std::cout << "Usage: fridgeclient <0|1>" <<std::endl;
44 std::cout << "connectivityType: Default IP" << std::endl;
45 std::cout << "connectivityType 0: IP" << std::endl;
50 ClientFridge(OCConnectivityType ct): m_callbackCount(0),
51 m_callsMade(0),m_connectivityType(ct)
53 std::ostringstream requestURI;
54 requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=intel.fridge";
55 std::cout << "Fridge Client has started " <<std::endl;
56 FindCallback f (std::bind(&ClientFridge::foundDevice, this, PH::_1));
57 OCStackResult result = OCPlatform::findResource(
58 "", requestURI.str(), CT_DEFAULT, f);
60 if(OC_STACK_OK != result)
62 throw new std::runtime_error("Fridge Find Resource Failed");
65 std::cout << "Waiting to discover fridge... "<<std::endl;
67 // we want to block this thread until the client has finished
68 // its duties, so we block on the CV until we have completed
69 // what we are looking to do
70 std::unique_lock<std::mutex> lk(m_mutex);
71 m_cv.wait(lk, [this]{ return m_callbackCount!=0 && m_callbackCount == m_callsMade;});
76 void foundDevice(std::shared_ptr<OCResource> resource)
78 using namespace OC::OCPlatform;
79 std::string platformDiscoveryURI = "/oic/p";
80 std::string deviceDiscoveryURI = "/oic/d";
81 if(resource && resource->uri() == "/device")
83 std::cout << "Discovered a device object"<<std::endl;
84 std::cout << "\tHost: "<<resource->host()<<std::endl;
85 std::cout << "\tURI: "<<resource->uri() <<std::endl;
90 std::cout << "Querying for platform information... " << std::endl;
92 ret = OCPlatform::getPlatformInfo("", platformDiscoveryURI, CT_ADAPTER_IP, NULL);
94 if (ret == OC_STACK_OK)
96 std::cout << "Get platform information is done." << std::endl;
100 std::cout << "Get platform information failed." << std::endl;
103 std::cout << "Querying for device information... " << std::endl;
105 ret = OCPlatform::getDeviceInfo(resource->host(), deviceDiscoveryURI, CT_ADAPTER_IP, NULL);
107 if (ret == OC_STACK_OK)
109 std::cout << "Getting device information is done." << std::endl;
113 std::cout << "Getting device information failed." << std::endl;
116 // we have now found a resource, so lets create a few resource objects
117 // for the other resources that we KNOW are associated with the intel.fridge
118 // server, and query them.
119 std::vector<std::string> lightTypes = {"intel.fridge.light"};
120 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
121 OCResource::Ptr light = constructResourceObject(resource->host(),
122 "/light", m_connectivityType, false, lightTypes, ifaces);
126 std::cout << "Error: Light Resource Object construction returned null\n";
130 std::vector<std::string> doorTypes = {"intel.fridge.door"};
131 OCResource::Ptr leftdoor = constructResourceObject(resource->host(),
132 "/door/left", m_connectivityType, false, doorTypes, ifaces);
136 std::cout << "Error: Left Door Resource Object construction returned null\n";
140 OCResource::Ptr rightdoor = constructResourceObject(resource->host(),
141 "/door/right", m_connectivityType, false, doorTypes, ifaces);
145 std::cout << "Error: Right Door Resource Object construction returned null\n";
149 OCResource::Ptr randomdoor = constructResourceObject(resource->host(),
150 "/door/random", m_connectivityType, false, doorTypes, ifaces);
153 std::cout << "Error: Random Door Resource Object construction returned null\n";
157 // Set header options with API version and token
158 HeaderOptions headerOptions;
161 // Set API version and client token
162 HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
163 HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
164 headerOptions.push_back(apiVersion);
165 headerOptions.push_back(clientToken);
167 catch(OCException& e)
169 std::cout << "Error creating HeaderOption: " << e.what() << std::endl;
173 // Setting header options will send above options in all requests
174 // Header options are set per resource.
175 // Below, header options are set only for device resource
176 resource->setHeaderOptions(headerOptions);
179 resource->get(QueryParamsMap(), GetCallback(
180 std::bind(&ClientFridge::getResponse, this, "Device", PH::_1,
181 PH::_2, PH::_3, resource, 0)
184 light->get(QueryParamsMap(), GetCallback(
185 std::bind(&ClientFridge::getResponse, this, "Fridge Light", PH::_1,
186 PH::_2, PH::_3, light, 1)
189 leftdoor->get(QueryParamsMap(), GetCallback(
190 std::bind(&ClientFridge::getResponse, this, "Left Door", PH::_1,
191 PH::_2, PH::_3, leftdoor, 2)
194 rightdoor->get(QueryParamsMap(), GetCallback(
195 std::bind(&ClientFridge::getResponse, this, "Right Door", PH::_1,
196 PH::_2, PH::_3, rightdoor, 3)
199 randomdoor->get(QueryParamsMap(), GetCallback(
200 std::bind(&ClientFridge::getResponse, this, "Random Door", PH::_1,
201 PH::_2, PH::_3, randomdoor, 4)
204 resource->deleteResource(DeleteCallback(
205 std::bind(&ClientFridge::deleteResponse, this, "Device", PH::_1,
210 // Note that resourceName, resource, and getId are all bound via the std::bind mechanism.
211 // it is possible to attach ANY arbitrary data to do whatever you would like here. It may,
212 // however be a better fit to wrap each call in an object so a fuller context (and additional
213 // requests) can be easily made inside of a simple context
214 void getResponse(const std::string& resourceName, const HeaderOptions& headerOptions,
215 const OCRepresentation& rep, const int eCode, OCResource::Ptr resource, int getId)
217 std::cout << "Got a response from get from the " << resourceName << std::endl;
218 std::cout << "Get ID is "<<getId<<" and resource URI is " << resource->uri() << std::endl;
219 std::cout << "Get eCode is "<< eCode << std::endl;
220 std::cout << "OCRepresentation uri is " << rep.getUri() << std::endl;
222 printHeaderOptions(headerOptions);
224 std::cout << "The Attribute Data is: "<<std::endl;
232 rep.getValue("device_name", name);
233 std::cout << "Name of device: "<< name << std::endl;
239 rep.getValue("on",isOn);
240 std::cout<<"The fridge light is "<< ((isOn)?"":"not ") <<"on"<<std::endl;
248 rep.getValue("open", isOpen);
249 rep.getValue("side", side);
250 std::cout << "Door is "<<isOpen<<" and is on the "<<side<<std::endl;
255 // Get on random resource called.
257 rep.getValue("device_name", name);
258 std::cout << "Name of fridge: "<< name << std::endl;
264 if(m_callbackCount == m_callsMade)
270 //Callback function to handle response for deleteResource call.
271 void deleteResponse(const std::string& resourceName, const HeaderOptions& headerOptions,
272 const int /*eCode*/, OCResource::Ptr resource, int deleteId)
274 std::cout << "Got a response from delete from the "<< resourceName << std::endl;
275 std::cout << "Delete ID is "<<deleteId<<" and resource URI is "<<resource->uri()<<std::endl;
276 printHeaderOptions(headerOptions);
280 if(m_callbackCount == m_callsMade)
286 //Function to print the headerOptions received from the server
287 void printHeaderOptions(const HeaderOptions& headerOptions)
289 for (auto it = headerOptions.begin(); it != headerOptions.end(); ++it)
291 if(it->getOptionID() == API_VERSION)
293 std::cout << "Server API version in GET response: " <<
294 it->getOptionData() << std::endl;
300 std::condition_variable m_cv;
301 std::atomic<int> m_callbackCount;
302 std::atomic<int> m_callsMade;
303 OCConnectivityType m_connectivityType;
306 int main(int argc, char* argv[])
308 OCConnectivityType connectivityType = CT_ADAPTER_IP;
313 std::size_t inputValLen;
314 int optionSelected = std::stoi(argv[1], &inputValLen);
316 if(inputValLen == strlen(argv[1]))
318 if(optionSelected == 0)
320 std::cout << "Using IP."<< std::endl;
321 connectivityType = CT_ADAPTER_IP;
325 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
331 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
334 catch(std::exception&)
336 std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
342 std::cout << "Default input argument. Using IP as connectivity type" << std::endl;
351 QualityOfService::LowQos
354 OCPlatform::Configure(cfg);
355 ClientFridge cf(connectivityType);