New APIs : hasAttribute, numberOfAttributes, erase in OCRepresentation.
[platform/upstream/iotivity.git] / examples / simpleclient.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 // OCClient.cpp : Defines the entry point for the console application.
22 //
23 #include <string>
24 #include <cstdlib>
25 #include <pthread.h>
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28
29 using namespace OC;
30
31 const int SUCCESS_RESPONSE = 0;
32 std::shared_ptr<OCResource> curResource;
33 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
34
35 class Light
36 {
37 public:
38
39     bool m_state;
40     int m_power;
41     std::string m_name;
42
43     Light() : m_state(false), m_power(0), m_name("")
44     {
45     }
46 };
47
48 Light mylight;
49
50 int observe_count()
51 {
52     static int oc = 0;
53     return ++oc;
54 }
55
56 void onObserve(const OCRepresentation& rep, const int& eCode, const int& sequenceNumber)
57 {
58     if(eCode == SUCCESS_RESPONSE)
59     {
60         std::cout << "OBSERVE RESULT:"<<std::endl;
61         std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
62
63         rep.getValue("state", mylight.m_state);
64         rep.getValue("power", mylight.m_power);
65         rep.getValue("name", mylight.m_name);
66
67         std::cout << "\tstate: " << mylight.m_state << std::endl;
68         std::cout << "\tpower: " << mylight.m_power << std::endl;
69         std::cout << "\tname: " << mylight.m_name << std::endl;
70
71         if(observe_count() > 30)
72         {
73             std::cout<<"Cancelling Observe..."<<std::endl;
74             OCStackResult result = curResource->cancelObserve();
75
76             std::cout << "Cancel result: "<< result <<std::endl;
77             sleep(10);
78             std::cout << "DONE"<<std::endl;
79             std::exit(0);
80         }
81     }
82     else
83     {
84         std::cout << "onObserve Response error: " << eCode << std::endl;
85         std::exit(-1);
86     }
87 }
88
89 // callback handler on PUT request
90 void onPut(const OCRepresentation& rep, const int eCode)
91 {
92     if(eCode == SUCCESS_RESPONSE)
93     {
94         std::cout << "PUT request was successful" << std::endl;
95
96         rep.getValue("state", mylight.m_state);
97         rep.getValue("power", mylight.m_power);
98         rep.getValue("name", mylight.m_name);
99
100         std::cout << "\tstate: " << mylight.m_state << std::endl;
101         std::cout << "\tpower: " << mylight.m_power << std::endl;
102         std::cout << "\tname: " << mylight.m_name << std::endl;
103
104         if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
105             std::cout << endl << "Observe is used." << endl << endl;
106         else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
107             std::cout << endl << "ObserveAll is used." << endl << endl;
108
109         curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve);
110
111     }
112     else
113     {
114         std::cout << "onPut Response error: " << eCode << std::endl;
115         std::exit(-1);
116     }
117 }
118
119 // Local function to put a different state for this resource
120 void putLightRepresentation(std::shared_ptr<OCResource> resource)
121 {
122     if(resource)
123     {
124         OCRepresentation rep;
125
126         std::cout << "Putting light representation..."<<std::endl;
127
128         mylight.m_state = true;
129         mylight.m_power = 15;
130
131         rep.setValue("state", mylight.m_state);
132         rep.setValue("power", mylight.m_power);
133
134         // Create QueryParameters Map and add query params (if any)
135         QueryParamsMap queryParamsMap;
136
137         // Invoke resource's pit API with rep, query map and the callback parameter
138         resource->put(rep, queryParamsMap, &onPut);
139     }
140 }
141
142 // Callback handler on GET request
143 void onGet(const OCRepresentation& rep, const int eCode)
144 {
145     if(eCode == SUCCESS_RESPONSE)
146     {
147         std::cout << "GET request was successful" << std::endl;
148         std::cout << "Resource URI: " << rep.getUri() << std::endl;
149
150         rep.getValue("state", mylight.m_state);
151         rep.getValue("power", mylight.m_power);
152         rep.getValue("name", mylight.m_name);
153
154         std::cout << "\tstate: " << mylight.m_state << std::endl;
155         std::cout << "\tpower: " << mylight.m_power << std::endl;
156         std::cout << "\tname: " << mylight.m_name << std::endl;
157
158         putLightRepresentation(curResource);
159     }
160     else
161     {
162         std::cout << "onGET Response error: " << eCode << std::endl;
163         std::exit(-1);
164     }
165 }
166
167 // Local function to get representation of light resource
168 void getLightRepresentation(std::shared_ptr<OCResource> resource)
169 {
170     if(resource)
171     {
172         std::cout << "Getting Light Representation..."<<std::endl;
173         // Invoke resource's get API with the callback parameter
174
175         QueryParamsMap test;
176         resource->get(test, &onGet);
177     }
178 }
179
180 // Callback to found resources
181 void foundResource(std::shared_ptr<OCResource> resource)
182 {
183     if(curResource)
184     {
185         std::cout << "Found another resource, ignoring"<<std::endl;
186     }
187
188     std::string resourceURI;
189     std::string hostAddress;
190     try
191     {
192         // Do some operations with resource object.
193         if(resource)
194         {
195             std::cout<<"DISCOVERED Resource:"<<std::endl;
196             // Get the resource URI
197             resourceURI = resource->uri();
198             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
199
200             // Get the resource host address
201             hostAddress = resource->host();
202             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
203
204             // Get the resource types
205             std::cout << "\tList of resource types: " << std::endl;
206             for(auto &resourceTypes : resource->getResourceTypes())
207             {
208                 std::cout << "\t\t" << resourceTypes << std::endl;
209             }
210
211             // Get the resource interfaces
212             std::cout << "\tList of resource interfaces: " << std::endl;
213             for(auto &resourceInterfaces : resource->getResourceInterfaces())
214             {
215                 std::cout << "\t\t" << resourceInterfaces << std::endl;
216             }
217
218             if(resourceURI == "/a/light")
219             {
220                 curResource = resource;
221                 // Call a local function which will internally invoke get API on the resource pointer
222                 getLightRepresentation(resource);
223             }
224         }
225         else
226         {
227             // Resource is invalid
228             std::cout << "Resource is invalid" << std::endl;
229         }
230
231     }
232     catch(std::exception& e)
233     {
234         //log(e.what());
235     }
236 }
237
238 void PrintUsage()
239 {
240     std::cout << std::endl;
241     std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
242     std::cout << "   ObserveType : 1 - Observe" << std::endl;
243     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
244 }
245
246 int main(int argc, char* argv[]) {
247     if (argc == 1)
248     {
249         OBSERVE_TYPE_TO_USE = ObserveType::Observe;
250     }
251     else if (argc == 2)
252     {
253         int value = atoi(argv[1]);
254         if (value == 1)
255             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
256         else if (value == 2)
257             OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
258         else
259             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
260     }
261     else
262     {
263         PrintUsage();
264         return -1;
265     }
266
267     // Create PlatformConfig object
268     PlatformConfig cfg {
269         OC::ServiceType::InProc,
270         OC::ModeType::Client,
271         "0.0.0.0",
272         0,
273         OC::QualityOfService::NonConfirmable
274     };
275
276     // Create a OCPlatform instance.
277     // Note: Platform creation is synchronous call.
278
279     try
280     {
281         OCPlatform platform(cfg);
282         std::cout << "Created Platform..."<<std::endl;
283         // Find all resources
284         platform.findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource);
285         std::cout<< "Finding Resource... " <<std::endl;
286         while(true)
287         {
288             // some operations
289         }
290
291     }catch(OCException& e)
292     {
293         //log(e.what());
294     }
295
296     return 0;
297 }
298