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