Updated functions description to match with params listed in function signature
[platform/upstream/iotivity.git] / examples / simpleclient.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation 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 int observe_count()
36 {
37     static int oc = 0;
38     return ++oc;
39 }
40
41 void onObserve(const AttributeMap& attributeMap, const int& eCode, const int& sequenceNumber)
42 {
43     if(eCode == SUCCESS_RESPONSE)
44     {
45         std::cout << "OBSERVE RESULT:"<<std::endl;
46         std::cout << "\tSequenceNumber: "<< sequenceNumber << endl;
47         for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
48         {
49             std::cout << "\tAttribute name: "<< it->first << " value: ";
50             for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
51             {
52                 std::cout <<"\t"<< *valueItr << " ";
53             }
54
55             std::cout << std::endl;
56         }
57         
58         if(observe_count() > 30)
59         {
60             std::cout<<"Cancelling Observe..."<<std::endl;
61             OCStackResult result = curResource->cancelObserve();
62
63             std::cout << "Cancel result: "<< result <<std::endl;
64             sleep(10);
65             std::cout << "DONE"<<std::endl;
66             std::exit(0);
67         }
68     }
69     else
70     {
71         std::cout << "onObserve Response error: " << eCode << std::endl;
72         std::exit(-1);
73     }
74 }
75
76 // callback handler on PUT request
77 void onPut(const AttributeMap attributeMap, const int eCode)
78 {
79     if(eCode == SUCCESS_RESPONSE)
80     {
81         std::cout << "PUT request was successful" << std::endl;
82
83         for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
84         {
85             std::cout << "\tAttribute name: "<< it->first << " value: ";
86             for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
87             {
88                 std::cout <<"\t"<< *valueItr << " ";
89             }
90
91             std::cout << std::endl;
92         }
93
94         if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
95             std::cout << endl << "Observe is used." << endl << endl;
96         else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
97             std::cout << endl << "ObserveAll is used." << endl << endl;
98
99         curResource->observe(OBSERVE_TYPE_TO_USE, &onObserve);
100
101     }
102     else
103     {
104         std::cout << "onPut Response error: " << eCode << std::endl;
105         std::exit(-1);
106     }
107 }
108
109 // Local function to put a different state for this resource
110 void putLightRepresentation(std::shared_ptr<OCResource> resource)
111 {
112     if(resource)
113     {
114         std::cout << "Putting light representation..."<<std::endl;
115         // Create AttributeMap
116         AttributeMap attributeMap;
117         // Add the attribute name and values in the attribute map
118         AttributeValues stateVal;
119         stateVal.push_back("true");
120
121         AttributeValues powerVal;
122         powerVal.push_back("10");
123
124         attributeMap["state"] = stateVal;
125         attributeMap["power"] = powerVal;
126
127         // Create QueryParameters Map and add query params (if any)
128         QueryParamsMap queryParamsMap;
129
130         // Invoke resource's pit API with attribute map, query map and the callback parameter
131         resource->put(attributeMap, queryParamsMap, &onPut);
132     }
133 }
134
135 // callback handler on GET request
136 void onGet(const AttributeMap attributeMap, const int eCode)
137 {
138     if(eCode == SUCCESS_RESPONSE)
139     {
140         std::cout << "GET Succeeded:"<<std::endl;
141         for(auto it = attributeMap.begin(); it != attributeMap.end(); ++it)
142         {
143             std::cout << "\tAttribute name: "<< it->first << " value: ";
144             for(auto valueItr = it->second.begin(); valueItr != it->second.end(); ++valueItr)
145             {
146                 std::cout <<"\t"<< *valueItr << " ";
147             }
148
149             std::cout << std::endl;
150         }
151
152         putLightRepresentation(curResource);
153     }
154     else
155     {
156         std::cout << "onGet Response error: " << eCode << std::endl;
157         std::exit(-1);
158     }
159 }
160 // Local function to get representation of light resource
161 void getLightRepresentation(std::shared_ptr<OCResource> resource)
162 {
163     if(resource)
164     {
165         std::cout << "Getting Light Representation..."<<std::endl;
166         // Invoke resource's get API with the callback parameter
167         resource->get(&onGet);
168     }
169 }
170
171 // Callback to found resources
172 void foundResource(std::shared_ptr<OCResource> resource)
173 {
174
175     if(curResource)
176     {
177         std::cout << "Found another resource, ignoring"<<std::endl;
178     }
179
180     std::string resourceURI;
181     std::string hostAddress;
182     try
183     {
184         // Do some operations with resource object.
185         if(resource)
186         {
187             std::cout<<"DISCOVERED Resource:"<<std::endl;
188             // Get the resource URI
189             resourceURI = resource->uri();
190             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
191
192             // Get the resource host address
193             hostAddress = resource->host();
194             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
195
196             if(resourceURI == "/a/light")
197             {
198                 curResource = resource;
199                 // Call a local function which will internally invoke get API on the resource pointer
200                 getLightRepresentation(resource);
201             }
202         }
203         else
204         {
205             // Resource is invalid
206             std::cout << "Resource is invalid" << std::endl;
207         }
208
209     }
210     catch(std::exception& e)
211     {
212         //log(e.what());
213     }
214 }
215
216 void PrintUsage()
217 {
218     std::cout << std::endl;
219     std::cout << "Usage : simpleclient <ObserveType>" << std::endl;
220     std::cout << "   ObserveType : 1 - Observe" << std::endl;
221     std::cout << "   ObserveType : 2 - ObserveAll" << std::endl;
222 }
223
224 int main(int argc, char* argv[]) {
225     if (argc == 1)
226     {
227         OBSERVE_TYPE_TO_USE = ObserveType::Observe;
228     }
229     else if (argc == 2)
230     {
231         int value = atoi(argv[1]);
232         if (value == 1)
233             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
234         else if (value == 2)
235             OBSERVE_TYPE_TO_USE = ObserveType::ObserveAll;
236         else
237             OBSERVE_TYPE_TO_USE = ObserveType::Observe;
238     }
239     else
240     {
241         PrintUsage();
242         return -1;
243     }
244
245     // Create PlatformConfig object
246     PlatformConfig cfg;
247     cfg.ipAddress = "134.134.161.33";
248     cfg.port = 5683;
249     cfg.mode = ModeType::Client;
250     cfg.serviceType = ServiceType::InProc;
251
252     // Create a OCPlatform instance.
253     // Note: Platform creation is synchronous call.
254
255     try
256     {
257         OCPlatform platform(cfg);
258         std::cout << "Created Platform..."<<std::endl;
259         // Find all resources
260         platform.findResource("", "coap://224.0.1.187/oc/core?rt=core.light", &foundResource);
261         std::cout<< "Finding Resource... " <<std::endl;
262         while(true)
263         {
264             // some operations
265         }
266
267     }catch(OCException& e)
268     {
269         //log(e.what());
270     }
271
272     return 0;
273 }
274