25c344e675c60491a1c93435027c7955cf1c2aef
[platform/upstream/iotivity.git] / service / notification-manager / SampleApp / tizen / sampleConsumer / src / oicapp-client.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 #define __GXX_EXPERIMENTAL_CXX0X__ 1
24
25 #include <string>
26 #include <cstdlib>
27 #include "OCPlatform.h"
28 #include "OCApi.h"
29 #include "oicapp-utils.h"
30
31 using namespace OC;
32
33 #define ISFORLINUX 1
34 #define ISFORTIZEN 0
35
36 const int SUCCESS_RESPONSE = 0;
37 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
38
39 const char *PREFIX_URI = "Uri : ";
40 const char *PREFIX_HOST = "Host : ";
41
42 OCPlatform *g_platform = nullptr;
43 PlatformConfig g_cfg;
44
45 std::shared_ptr< OCResource > g_curResource = nullptr;
46 AttributeMap g_curAttributeMap;
47
48 oicappData *g_oicappClientAd;
49 oicappData *g_oicFindAd;
50 oicappData *g_oicObserveAd;
51
52 OCStackResult nmfindResource(const std::string &host , const std::string &resourceName);
53 void onObserve(const OCRepresentation &rep , const int &eCode , const int &sequenceNumber);
54 void onfound();
55 void onobserve();
56
57 void findResourceCandidate(oicappData *ad)
58 {
59     try
60     {
61         nmfindResource("" , "coap://224.0.1.187/oc/core?rt=NotificationManager.Hosting");
62         std::cout << "Finding Resource... " << std::endl;
63
64     }
65     catch (OCException &e)
66     {
67         ERR("findResourceCandidate exception: %s" , e.what().c_str());
68     }
69     g_oicFindAd = ad;
70 }
71
72 void startObserve(oicappData *ad)
73 {
74     if (g_curResource != nullptr)
75     {
76         g_oicObserveAd = ad;
77         QueryParamsMap test;
78         g_curResource->observe(ObserveType::Observe , test , &onObserve);
79     }
80 }
81
82 void printAttributeMap(const AttributeMap attributeMap)
83 {
84     for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
85     {
86         DBG("\tAttribute name: %s" , it->first.c_str());
87         for (auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
88         {
89             DBG("\t\tAttribute value: %s" , (*valueItr).c_str());
90         }
91     }
92 }
93
94 void updateAttribute(const AttributeMap attributeMap)
95 {
96     g_oicappClientAd->temp = std::stoi(attributeMap.at("temp")[0]);
97     g_oicappClientAd->humid = std::stoi(attributeMap.at("humid")[0]);
98 }
99
100 void cancelObserve()
101 {
102     DBG("Cancelling Observe...");
103
104     OCStackResult result = OC_STACK_ERROR;
105
106     if (g_curResource != nullptr)
107     {
108         result = g_curResource->cancelObserve();
109     }
110
111     DBG("Cancel result: %d" , result);
112 }
113
114 void onObserve(const OCRepresentation &rep , const int &eCode , const int &sequenceNumber)
115 {
116
117     AttributeMap attributeMap = rep.getAttributeMap();
118     if (eCode == SUCCESS_RESPONSE)
119     {
120         DBG("OBSERVE RESULT:");
121         DBG("\tSequenceNumber: %d" , sequenceNumber);
122
123         printAttributeMap(attributeMap);
124 //      updateAttribute(attributeMap);
125         g_curAttributeMap = attributeMap;
126         onobserve();
127     }
128     else
129     {
130         ERR("onObserve Response error: %d" , eCode);
131         //std::exit(-1);
132     }
133 }
134
135 // callback handler on PUT request
136 void onPut(const OCRepresentation &rep , const int eCode)
137 {
138     AttributeMap attributeMap = rep.getAttributeMap();
139     if (eCode == SUCCESS_RESPONSE)
140     {
141         DBG("PUT request was successful");
142
143         printAttributeMap(attributeMap);
144
145         if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
146             INFO("Observe is used.");
147         else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
148             INFO("ObserveAll is used.");
149
150         if (g_curResource != nullptr)
151         {
152             DBG("Observe Start");
153             QueryParamsMap test;
154             g_curResource->observe(ObserveType::Observe , test , &onObserve);
155         }
156     }
157     else
158     {
159         ERR("onPut Response error: %d" , eCode);
160         //std::exit(-1);
161     }
162 }
163
164 // callback handler on GET request
165 void onGet(const OCRepresentation &rep , const int eCode)
166 {
167
168     AttributeMap attributeMap = rep.getAttributeMap();
169     if (eCode == SUCCESS_RESPONSE)
170     {
171         DBG("GET Succeeded:");
172
173         printAttributeMap(attributeMap);
174         updateAttribute(attributeMap);
175     }
176     else
177     {
178         ERR("onGet Response error: %d" , eCode);
179         //std::exit(-1);
180     }
181 }
182
183 // Local function to get representation of light resource
184 void getLightRepresentation(std::shared_ptr< OCResource > resource)
185 {
186     if (resource)
187     {
188         DBG("Getting Light Representation...");
189         // Invoke resource's get API with the callback parameter
190         QueryParamsMap test;
191         resource->get(test , &onGet);
192     }
193 }
194
195 // Callback to found resources
196 static void foundResource(std::shared_ptr< OCResource > resource)
197 {
198     try
199     {
200         if (resource)
201         {
202             DBG("DISCOVERED Resource:");
203             DBG("\tURI of the resource: %s" , resource->uri().c_str());
204             DBG("\tHost address of the resource: %s" , resource->host().c_str());
205
206             if (resource->uri().find("/a/NM") != string::npos)
207             {
208
209                 g_curResource = resource;
210                 onfound();
211             }
212         }
213         else
214         {
215             ERR("Resource is invalid");
216         }
217
218     }
219     catch (std::exception &e)
220     {
221         ERR("foundResource exception: %s" , e.what().c_str());
222     }
223 }
224
225 OCStackResult nmfindResource(const std::string &host , const std::string &resourceName)
226 {
227     if (g_platform != nullptr)
228     {
229         return g_platform->findResource(host , resourceName , &foundResource);
230     }
231
232     return OC_STACK_ERROR;
233 }
234
235 int oicapp_client_start(oicappData *ad)
236 {
237     g_cfg.ipAddress = ad->ipAddr;
238     g_cfg.port = 5683;
239     g_cfg.mode = ModeType::Client;
240     g_cfg.serviceType = ServiceType::InProc;
241
242     retv_if(NULL != g_platform , -1);
243
244     g_platform = new OCPlatform(g_cfg);
245
246     g_oicappClientAd = ad;
247
248     return 0;
249 }
250
251 void oicapp_client_stop()
252 {
253     if (g_curResource != nullptr)
254     {
255         cancelObserve();
256         g_curResource = NULL;
257     }
258
259     if (g_platform)
260     {
261         delete (g_platform);
262         g_platform = NULL;
263     }
264     g_oicappClientAd = NULL;
265 }
266
267 // Local function to put a different state for this resource
268 int oicapp_client_put(int power , int level)
269 {
270     std::shared_ptr< OCResource > resource = g_curResource;
271
272     retv_if(NULL == g_curResource , -1);
273
274     DBG("Putting light representation...");
275
276     AttributeMap attributeMap;
277
278     AttributeValues tempVal;
279     AttributeValues humidVal;
280
281     tempVal.push_back(to_string(power));
282     humidVal.push_back(to_string(level));
283
284     attributeMap["temp"] = tempVal;
285     attributeMap["humid"] = humidVal;
286
287     // Create QueryParameters Map and add query params (if any)
288     QueryParamsMap queryParamsMap;
289
290     OCRepresentation rep;
291     rep.setAttributeMap(attributeMap);
292
293     // Invoke resource's pit API with attribute map, query map and the callback parameter
294     resource->put(rep , queryParamsMap , &onPut);
295
296     return 0;
297 }
298
299 void onfound()
300 {
301
302     if (g_curResource->uri().find("/a/NM/TempHumSensor/virtual") != string::npos)
303     {
304         oicappData *ad = g_oicFindAd;
305
306         std::string tmpuri = PREFIX_URI + g_curResource->uri();
307         std::string tmphost = PREFIX_HOST + g_curResource->host();
308
309         DBG("OnFound Resource...");
310         DBG("Resource Uri : %s" , tmpuri.c_str());
311         DBG("Resource Host: %s" , tmphost.c_str());
312
313         _gl_update_item(ad , tmphost.c_str() , ad->itemConsumerHost);
314         _gl_update_item(ad , tmpuri.c_str() , ad->itemConsumerUri);
315     }
316
317 }
318 void onobserve()
319 {
320
321     oicappData *ad = g_oicObserveAd;
322
323     AttributeMap attributeMap = g_curAttributeMap;
324
325     std::string tmpStr[2];
326     int index = 0;
327     for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
328     {
329         tmpStr[index] = it->first;
330         tmpStr[index].append(" : ");
331         for (auto value = it->second.begin() ; value != it->second.end() ; ++value)
332         {
333             tmpStr[index].append(*value);
334         }
335         index++;
336     }
337
338     DBG("%s" , tmpStr[0].c_str());
339     DBG("%s" , tmpStr[1].c_str());
340     _gl_update_item(ad , strdup(tmpStr[0].c_str()) , ad->itemConsumerTemp);
341     _gl_update_item(ad , strdup(tmpStr[1].c_str()) , ad->itemConsumerHumid);
342 }