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