[CONPRO-1473] Fix for build error [-Werror=implicit-function-declaration]
[platform/upstream/iotivity.git] / resource / examples / fridgeclient.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 /// 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
25
26 #include <iostream>
27 #include <stdexcept>
28 #include <condition_variable>
29 #include <mutex>
30 #include <atomic>
31 #include "OCPlatform.h"
32 #include "OCApi.h"
33
34 using namespace OC;
35 namespace PH = std::placeholders;
36
37 // Option ID for API version and client token
38 const uint16_t API_VERSION = 2048;
39 const uint16_t TOKEN = 3000;
40
41 static void printUsage()
42 {
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;
46 }
47 class ClientFridge
48 {
49     public:
50     ClientFridge(OCConnectivityType ct): m_callbackCount(0),
51         m_callsMade(0),m_connectivityType(ct)
52     {
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);
59
60         if(OC_STACK_OK != result)
61         {
62             throw new std::runtime_error("Fridge Find Resource Failed");
63         }
64
65         std::cout << "Waiting to discover fridge... "<<std::endl;
66         {
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;});
72         }
73     }
74
75     private:
76     void foundDevice(std::shared_ptr<OCResource> resource)
77     {
78         using namespace OC::OCPlatform;
79         if(resource && resource->uri() == "/device")
80         {
81             std::cout << "Discovered a device object"<<std::endl;
82             std::cout << "\tHost: "<<resource->host()<<std::endl;
83             std::cout << "\tURI:  "<<resource->uri() <<std::endl;
84         }
85
86         // we have now found a resource, so lets create a few resource objects
87         // for the other resources that we KNOW are associated with the intel.fridge
88         // server, and query them.
89         std::vector<std::string> lightTypes = {"intel.fridge.light"};
90         std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
91         OCResource::Ptr light = constructResourceObject(resource->host(),
92                                 "/light", m_connectivityType, false, lightTypes, ifaces);
93
94         if(!light)
95         {
96             std::cout << "Error: Light Resource Object construction returned null\n";
97             return;
98         }
99
100         std::vector<std::string> doorTypes = {"intel.fridge.door"};
101         OCResource::Ptr leftdoor = constructResourceObject(resource->host(),
102                                 "/door/left", m_connectivityType, false, doorTypes, ifaces);
103
104         if(!leftdoor)
105         {
106             std::cout << "Error: Left Door Resource Object construction returned null\n";
107             return;
108         }
109
110         OCResource::Ptr rightdoor = constructResourceObject(resource->host(),
111                                 "/door/right", m_connectivityType, false, doorTypes, ifaces);
112
113         if(!rightdoor)
114         {
115             std::cout << "Error: Right Door Resource Object construction returned null\n";
116             return;
117         }
118
119         OCResource::Ptr randomdoor = constructResourceObject(resource->host(),
120                                 "/door/random", m_connectivityType, false, doorTypes, ifaces);
121         if(!randomdoor)
122         {
123             std::cout << "Error: Random Door Resource Object construction returned null\n";
124             return;
125         }
126
127         // Set header options with API version and token
128         HeaderOptions headerOptions;
129         try
130         {
131             // Set API version and client token
132             HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
133             HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
134             headerOptions.push_back(apiVersion);
135             headerOptions.push_back(clientToken);
136         }
137         catch(OCException& e)
138         {
139             std::cout << "Error creating HeaderOption: " << e.what() << std::endl;
140         }
141
142
143         // Setting header options will send above options in all requests
144         // Header options are set per resource.
145         // Below, header options are set only for device resource
146         resource->setHeaderOptions(headerOptions);
147
148         ++m_callsMade;
149         resource->get(QueryParamsMap(), GetCallback(
150                 std::bind(&ClientFridge::getResponse, this, "Device", PH::_1,
151                     PH::_2, PH::_3, resource, 0)
152                 ));
153         ++m_callsMade;
154         light->get(QueryParamsMap(), GetCallback(
155                 std::bind(&ClientFridge::getResponse, this, "Fridge Light", PH::_1,
156                     PH::_2, PH::_3, light, 1)
157                 ));
158         ++m_callsMade;
159         leftdoor->get(QueryParamsMap(), GetCallback(
160                 std::bind(&ClientFridge::getResponse, this, "Left Door", PH::_1,
161                     PH::_2, PH::_3, leftdoor, 2)
162                 ));
163         ++m_callsMade;
164         rightdoor->get(QueryParamsMap(), GetCallback(
165                 std::bind(&ClientFridge::getResponse, this, "Right Door", PH::_1,
166                     PH::_2, PH::_3, rightdoor, 3)
167                 ));
168         ++m_callsMade;
169         randomdoor->get(QueryParamsMap(), GetCallback(
170                 std::bind(&ClientFridge::getResponse, this, "Random Door", PH::_1,
171                     PH::_2, PH::_3, randomdoor, 4)
172                 ));
173         ++m_callsMade;
174         resource->deleteResource(DeleteCallback(
175                 std::bind(&ClientFridge::deleteResponse, this, "Device", PH::_1,
176                     PH::_2, resource, 0)
177                 ));
178     }
179
180     // Note that resourceName, resource, and getId are all bound via the std::bind mechanism.
181     // it is possible to attach ANY arbitrary data to do whatever you would like here.  It may,
182     // however be a better fit to wrap each call in an object so a fuller context (and additional
183     // requests) can be easily made inside of a simple context
184     void getResponse(const std::string& resourceName, const HeaderOptions& headerOptions,
185                 const OCRepresentation& rep, const int eCode, OCResource::Ptr resource, int getId)
186     {
187         std::cout << "Got a response from get from the " << resourceName << std::endl;
188         std::cout << "Get ID is "<<getId<<" and resource URI is " << resource->uri() << std::endl;
189         std::cout << "Get eCode is "<< eCode << std::endl;
190         std::cout << "OCRepresentation uri is " << rep.getUri() << std::endl;
191
192         printHeaderOptions(headerOptions);
193
194         std::cout << "The Attribute Data is: "<<std::endl;
195
196         switch(getId)
197         {
198             case 0:
199                 {
200                     // Get on device
201                     std::string name;
202                     rep.getValue("device_name", name);
203                     std::cout << "Name of device: "<< name << std::endl;
204                     break;
205                 }
206             case 1:
207                 {
208                     bool isOn = false;
209                     rep.getValue("on",isOn);
210                     std::cout<<"The fridge light is "<< ((isOn)?"":"not ") <<"on"<<std::endl;
211                 }
212                 break;
213             case 2:
214             case 3:
215                 {
216                     bool isOpen = false;
217                     std::string side;
218                     rep.getValue("open", isOpen);
219                     rep.getValue("side", side);
220                     std::cout << "Door is "<<isOpen<<" and is on the "<<side<<std::endl;
221                 }
222                 break;
223             case 4:
224                 {
225                     // Get on random resource called.
226                     std::string name;
227                     rep.getValue("device_name", name);
228                     std::cout << "Name of fridge: "<< name << std::endl;
229                     break;
230                 }
231         }
232         ++m_callbackCount;
233
234         if(m_callbackCount == m_callsMade)
235         {
236             m_cv.notify_all();
237         }
238     }
239
240     //Callback function to handle response for deleteResource call.
241     void deleteResponse(const std::string& resourceName, const HeaderOptions& headerOptions,
242                 const int /*eCode*/, OCResource::Ptr resource, int deleteId)
243     {
244         std::cout << "Got a response from delete from the "<< resourceName << std::endl;
245         std::cout << "Delete ID is "<<deleteId<<" and resource URI is "<<resource->uri()<<std::endl;
246         printHeaderOptions(headerOptions);
247
248         ++m_callbackCount;
249
250         if(m_callbackCount == m_callsMade)
251         {
252             m_cv.notify_all();
253         }
254     }
255
256     //Function to print the headerOptions received from the server
257     void printHeaderOptions(const HeaderOptions& headerOptions)
258     {
259         for (auto it = headerOptions.begin(); it != headerOptions.end(); ++it)
260         {
261             if(it->getOptionID() == API_VERSION)
262             {
263                 std::cout << "Server API version in GET response: " <<
264                         it->getOptionData() << std::endl;
265             }
266         }
267     }
268
269     std::mutex m_mutex;
270     std::condition_variable m_cv;
271     std::atomic<int> m_callbackCount;
272     std::atomic<int> m_callsMade;
273     OCConnectivityType m_connectivityType;
274 };
275
276 int main(int argc, char* argv[])
277 {
278     OCConnectivityType connectivityType = CT_ADAPTER_IP;
279     if(argc == 2)
280     {
281         try
282         {
283             std::size_t inputValLen;
284             int optionSelected = std::stoi(argv[1], &inputValLen);
285
286             if(inputValLen == strlen(argv[1]))
287             {
288                 if(optionSelected == 0)
289                 {
290                     std::cout << "Using IP."<< std::endl;
291                     connectivityType = CT_ADAPTER_IP;
292                 }
293                 else
294                 {
295                     std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
296                     printUsage();
297                 }
298             }
299             else
300             {
301                 std::cout << "Invalid connectivity type selected. Using default IP" << std::endl;
302             }
303         }
304         catch(std::exception&)
305         {
306             std::cout << "Invalid input argument. Using IP as connectivity type" << std::endl;
307         }
308     }
309     else
310     {
311         printUsage();
312         std::cout << "Default input argument. Using IP as connectivity type" << std::endl;
313     }
314
315     PlatformConfig cfg
316     {
317         ServiceType::InProc,
318         ModeType::Client,
319         "0.0.0.0",
320         0,
321         QualityOfService::LowQos
322     };
323
324     OCPlatform::Configure(cfg);
325     ClientFridge cf(connectivityType);
326     return 0;
327 }
328