Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / protocol-plugin / sample-app / linux / mqtt / mqttclient.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics 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 /// @file mqttclient.cpp
22
23 /// @brief Samplecode which controls MQTT-fan plugin using Protocol Plugin Manager.
24
25 #include <string>
26 #include <cstdlib>
27 #include <pthread.h>
28 #include "OCPlatform.h"
29 #include "PluginManager.h"
30 #include "OCApi.h"
31
32 using namespace OC;
33 using namespace OIC;
34
35 const int SUCCESS_RESPONSE = 0;
36 std::shared_ptr<OCResource> curFanResource;
37 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
38 int count = 0;
39 void putFanRepresentation(std::shared_ptr<OCResource> resource);
40
41 class Fan
42 {
43     public:
44
45         bool m_state;
46         int m_power;
47         std::string m_name;
48
49         Fan() : m_state(false), m_power(0), m_name("")
50         {
51         }
52 };
53
54 Fan myfan;
55
56 int observe_count()
57 {
58     static int oc = 0;
59     return ++oc;
60 }
61
62 void onObserve(const HeaderOptions headerOptions, const OCRepresentation &rep,
63                const int &eCode, const int &sequenceNumber)
64 {
65     if (eCode == OC_STACK_OK)
66     {
67         std::cout << "OBSERVE RESULT:" << std::endl;
68         std::cout << "\tSequenceNumber: " << sequenceNumber << std::endl;
69
70         rep.getValue("state", myfan.m_state);
71         rep.getValue("power", myfan.m_power);
72         rep.getValue("name", myfan.m_name);
73
74         std::cout << "\tstate: " << myfan.m_state << std::endl;
75         std::cout << "\tpower: " << myfan.m_power << std::endl;
76         std::cout << "\tname: " << myfan.m_name << std::endl;
77
78         if (observe_count() > 30)
79         {
80             std::cout << "Cancelling Observe..." << std::endl;
81             OCStackResult result = curFanResource->cancelObserve();
82
83             std::cout << "Cancel result: " << result << std::endl;
84             sleep(10);
85             std::cout << "DONE" << std::endl;
86             std::exit(0);
87         }
88     }
89     else
90     {
91         std::cout << "onObserve Response error: " << eCode << std::endl;
92         std::exit(-1);
93     }
94 }
95
96 void onPost2(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
97 {
98     if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
99     {
100         std::cout << "POST request was successful" << std::endl;
101
102         if (rep.hasAttribute("createduri"))
103         {
104             std::cout << "\tUri of the created resource: "
105                       << rep.getValue<std::string>("createduri") << std::endl;
106         }
107         else
108         {
109             rep.getValue("state", myfan.m_state);
110             rep.getValue("power", myfan.m_power);
111             rep.getValue("name", myfan.m_name);
112
113             std::cout << "\tstate: " << myfan.m_state << std::endl;
114             std::cout << "\tpower: " << myfan.m_power << std::endl;
115             std::cout << "\tname: " << myfan.m_name << std::endl;
116         }
117
118         if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
119             std::cout << std::endl << "Observe is used." << std::endl << std::endl;
120         else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
121             std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
122
123         //curFanResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
124
125     }
126     else
127     {
128         std::cout << "onPost Response error: " << eCode << std::endl;
129         std::exit(-1);
130     }
131 }
132
133 void onPost(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
134 {
135     if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED)
136     {
137         std::cout << "POST request was successful" << std::endl;
138
139         if (rep.hasAttribute("createduri"))
140         {
141             std::cout << "\tUri of the created resource: "
142                       << rep.getValue<std::string>("createduri") << std::endl;
143         }
144         else
145         {
146             rep.getValue("state", myfan.m_state);
147             rep.getValue("power", myfan.m_power);
148             rep.getValue("name", myfan.m_name);
149
150             std::cout << "\tstate: " << myfan.m_state << std::endl;
151             std::cout << "\tpower: " << myfan.m_power << std::endl;
152             std::cout << "\tname: " << myfan.m_name << std::endl;
153         }
154
155         OCRepresentation rep2;
156
157         std::cout << "Posting fan representation..." << std::endl;
158
159         myfan.m_state = true;
160         myfan.m_power = 55;
161
162         rep2.setValue("state", myfan.m_state);
163         rep2.setValue("power", myfan.m_power);
164
165         curFanResource->post(rep2, QueryParamsMap(), &onPost2);
166     }
167     else
168     {
169         std::cout << "onPost Response error: " << eCode << std::endl;
170         std::exit(-1);
171     }
172 }
173
174 // Local function to put a different state for this resource
175 void postFanRepresentation(std::shared_ptr<OCResource> resource)
176 {
177     if (resource)
178     {
179         OCRepresentation rep;
180
181         std::cout << "Posting fan representation..." << std::endl;
182
183         myfan.m_state = false;
184         myfan.m_power = 105;
185
186         rep.setValue("state", myfan.m_state);
187         rep.setValue("power", myfan.m_power);
188
189         // Invoke resource's post API with rep, query map and the callback parameter
190         resource->post(rep, QueryParamsMap(), &onPost);
191     }
192 }
193
194 // callback handler on PUT request
195 void onPut(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
196 {
197     if (eCode == OC_STACK_OK)
198     {
199         std::cout << "PUT request was successful" << std::endl;
200
201         rep.getValue("state", myfan.m_state);
202         rep.getValue("power", myfan.m_power);
203         rep.getValue("name", myfan.m_name);
204
205         std::cout << "\tstate: " << myfan.m_state << std::endl;
206         std::cout << "\tpower: " << myfan.m_power << std::endl;
207         std::cout << "\tname: " << myfan.m_name << std::endl;
208
209         putFanRepresentation(curFanResource);
210     }
211     else
212     {
213         std::cout << "onPut Response error: " << eCode << std::endl;
214         std::exit(-1);
215     }
216 }
217
218 // Local function to put a different state for this resource
219 void putFanRepresentation(std::shared_ptr<OCResource> resource)
220 {
221     if (resource)
222     {
223         OCRepresentation rep;
224
225         std::cout << "Putting fan representation..." << std::endl;
226
227         myfan.m_state = true;
228         if (myfan.m_power == 1)
229             myfan.m_power = 0;
230         else
231             myfan.m_power = 1;
232         sleep(5);
233         rep.setValue("state", myfan.m_state);
234         rep.setValue("power", myfan.m_power);
235
236         // Invoke resource's put API with rep, query map and the callback parameter
237         resource->put(rep, QueryParamsMap(), &onPut);
238     }
239 }
240
241 // Callback handler on GET request
242 void onFanGet(const HeaderOptions &headerOptions, const OCRepresentation &rep, const int eCode)
243 {
244     if (eCode == OC_STACK_OK)
245     {
246         std::cout << "GET Fan request was successful" << std::endl;
247         std::cout << "Resource URI: " << rep.getUri() << std::endl;
248
249         rep.getValue("state", myfan.m_state);
250         rep.getValue("power", myfan.m_power);
251         rep.getValue("name", myfan.m_name);
252
253         std::cout << "\tstate: " << myfan.m_state << std::endl;
254         std::cout << "\tpower: " << myfan.m_power << std::endl;
255         std::cout << "\tname: " << myfan.m_name << std::endl;
256
257         putFanRepresentation(curFanResource);
258     }
259     else
260     {
261         std::cout << "onGET Response error: " << eCode << std::endl;
262         std::exit(-1);
263     }
264 }
265
266
267 // Local function to get representation of fan resource
268 void getFanRepresentation(std::shared_ptr<OCResource> resource)
269 {
270     if (resource)
271     {
272         std::cout << "Getting Fan Representation..." << std::endl;
273         // Invoke resource's get API with the callback parameter
274
275         QueryParamsMap test;
276         resource->get(test, &onFanGet);
277     }
278 }
279
280 // Callback to found resources
281 void foundResourceFan(std::shared_ptr<OCResource> resource)
282 {
283     if (curFanResource)
284     {
285         std::cout << "Found another resource, ignoring" << std::endl;
286     }
287
288     std::string resourceURI;
289     std::string hostAddress;
290     try
291     {
292         // Do some operations with resource object.
293         if (resource)
294         {
295             std::cout << "DISCOVERED Resource:" << std::endl;
296             // Get the resource URI
297             resourceURI = resource->uri();
298             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
299
300             // Get the resource host address
301             hostAddress = resource->host();
302             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
303
304             // Get the resource types
305             std::cout << "\tList of resource types: " << std::endl;
306             for (auto &resourceTypes : resource->getResourceTypes())
307             {
308                 std::cout << "\t\t" << resourceTypes << std::endl;
309             }
310
311             // Get the resource interfaces
312             std::cout << "\tList of resource interfaces: " << std::endl;
313             for (auto &resourceInterfaces : resource->getResourceInterfaces())
314             {
315                 std::cout << "\t\t" << resourceInterfaces << std::endl;
316             }
317
318             if (resourceURI == "/a/fan")
319             {
320                 curFanResource = resource;
321                 // Call a local function which will internally invoke get API on the resource pointer
322                 putFanRepresentation(curFanResource);
323             }
324         }
325         else
326         {
327             // Resource is invalid
328             std::cout << "Resource is invalid" << std::endl;
329         }
330
331     }
332     catch (std::exception &e)
333     {
334         std::cout << "Exception: " << e.what() << std::endl;
335     }
336 }
337
338 void PrintUsage()
339 {
340     std::cout << std::endl;
341     std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
342     std::cout << "   ObserveType : 1 - Observe" << std::endl;
343     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
344 }
345
346
347
348 int main(int argc, char *argv[])
349 {
350     std::string name;
351     std::string key = "Name";
352     std::string  state = "";
353     std::string  id = "";
354     std::ostringstream requestURI;
355
356     if (argc == 1)
357     {
358         OBSERVE_TYPE_TO_USE = ObserveType::Observe;
359     }
360     else if (argc == 2)
361     {
362         int value = atoi(argv[1]);
363         if (value == 1)
364             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
365         else if (value == 2)
366             OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
367         else
368             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
369     }
370     else
371     {
372         PrintUsage();
373         return -1;
374     }
375
376     // Create PlatformConfig object
377     PlatformConfig cfg
378     {
379         OC::ServiceType::InProc,
380         OC::ModeType::Both,
381         "0.0.0.0",
382         0,
383         OC::QualityOfService::LowQos
384     };
385
386     OCPlatform::Configure(cfg);
387
388     try
389     {
390         PluginManager *m_pm = new PluginManager();
391         std::cout << "start light Plugin by Resource Type"  << std::endl;
392         m_pm->startPlugins("ResourceType", "oic.fan");
393         sleep(2);
394         // makes it so that all boolean values are printed as 'true/false' in this stream
395         std::cout.setf(std::ios::boolalpha);
396         // Find all resources
397         requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=core.fan";
398         OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResourceFan);
399         std::cout << "Finding Resource... " << std::endl;
400         while (true)
401         {
402             // some operations
403         }
404
405     }
406     catch (OCException &e)
407     {
408         std::cout << "Exception: " << e.what() << std::endl;
409     }
410
411     return 0;
412 }