1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // OCClient.cpp : Defines the entry point for the console application.
23 #define __GXX_EXPERIMENTAL_CXX0X__ 1
27 #include "OCPlatform.h"
29 #include "oicapp-utils.h"
36 const int SUCCESS_RESPONSE = 0;
37 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
39 const char *PREFIX_URI = "Uri : ";
40 const char *PREFIX_HOST = "Host : ";
42 OCPlatform *g_platform = nullptr;
45 std::shared_ptr< OCResource > g_curResource = nullptr;
46 AttributeMap g_curAttributeMap;
48 oicappData *g_oicappClientAd;
49 oicappData *g_oicFindAd;
50 oicappData *g_oicObserveAd;
52 OCStackResult nmfindResource(const std::string &host , const std::string &resourceName);
53 void onObserve(const OCRepresentation &rep , const int &eCode , const int &sequenceNumber);
57 void findResourceCandidate(oicappData *ad)
61 nmfindResource("" , "coap://224.0.1.187/oc/core?rt=NotificationManager.Hosting");
62 std::cout << "Finding Resource... " << std::endl;
65 catch (OCException &e)
67 ERR("findResourceCandidate exception: %s" , e.what().c_str());
72 void startObserve(oicappData *ad)
74 if (g_curResource != nullptr)
78 g_curResource->observe(ObserveType::Observe , test , &onObserve);
82 void printAttributeMap(const AttributeMap attributeMap)
84 for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
86 DBG("\tAttribute name: %s" , it->first.c_str());
87 for (auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
89 DBG("\t\tAttribute value: %s" , (*valueItr).c_str());
94 void updateAttribute(const AttributeMap attributeMap)
96 g_oicappClientAd->temp = std::stoi(attributeMap.at("temp")[0]);
97 g_oicappClientAd->humid = std::stoi(attributeMap.at("humid")[0]);
102 DBG("Cancelling Observe...");
104 OCStackResult result = OC_STACK_ERROR;
106 if (g_curResource != nullptr)
108 result = g_curResource->cancelObserve();
111 DBG("Cancel result: %d" , result);
114 void onObserve(const OCRepresentation &rep , const int &eCode , const int &sequenceNumber)
117 AttributeMap attributeMap = rep.getAttributeMap();
118 if (eCode == SUCCESS_RESPONSE)
120 DBG("OBSERVE RESULT:");
121 DBG("\tSequenceNumber: %d" , sequenceNumber);
123 printAttributeMap(attributeMap);
124 // updateAttribute(attributeMap);
125 g_curAttributeMap = attributeMap;
130 ERR("onObserve Response error: %d" , eCode);
135 // callback handler on PUT request
136 void onPut(const OCRepresentation &rep , const int eCode)
138 AttributeMap attributeMap = rep.getAttributeMap();
139 if (eCode == SUCCESS_RESPONSE)
141 DBG("PUT request was successful");
143 printAttributeMap(attributeMap);
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.");
150 if (g_curResource != nullptr)
152 DBG("Observe Start");
154 g_curResource->observe(ObserveType::Observe , test , &onObserve);
159 ERR("onPut Response error: %d" , eCode);
164 // callback handler on GET request
165 void onGet(const OCRepresentation &rep , const int eCode)
168 AttributeMap attributeMap = rep.getAttributeMap();
169 if (eCode == SUCCESS_RESPONSE)
171 DBG("GET Succeeded:");
173 printAttributeMap(attributeMap);
174 updateAttribute(attributeMap);
178 ERR("onGet Response error: %d" , eCode);
183 // Local function to get representation of light resource
184 void getLightRepresentation(std::shared_ptr< OCResource > resource)
188 DBG("Getting Light Representation...");
189 // Invoke resource's get API with the callback parameter
191 resource->get(test , &onGet);
195 // Callback to found resources
196 static void foundResource(std::shared_ptr< OCResource > resource)
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());
206 if (resource->uri().find("/a/NM") != string::npos)
209 g_curResource = resource;
215 ERR("Resource is invalid");
219 catch (std::exception &e)
221 ERR("foundResource exception: %s" , e.what().c_str());
225 OCStackResult nmfindResource(const std::string &host , const std::string &resourceName)
227 if (g_platform != nullptr)
229 return g_platform->findResource(host , resourceName , &foundResource);
232 return OC_STACK_ERROR;
235 int oicapp_client_start(oicappData *ad)
237 g_cfg.ipAddress = ad->ipAddr;
239 g_cfg.mode = ModeType::Client;
240 g_cfg.serviceType = ServiceType::InProc;
242 retv_if(NULL != g_platform , -1);
244 g_platform = new OCPlatform(g_cfg);
246 g_oicappClientAd = ad;
251 void oicapp_client_stop()
253 if (g_curResource != nullptr)
256 g_curResource = NULL;
264 g_oicappClientAd = NULL;
267 // Local function to put a different state for this resource
268 int oicapp_client_put(int power , int level)
270 std::shared_ptr< OCResource > resource = g_curResource;
272 retv_if(NULL == g_curResource , -1);
274 DBG("Putting light representation...");
276 AttributeMap attributeMap;
278 AttributeValues tempVal;
279 AttributeValues humidVal;
281 tempVal.push_back(to_string(power));
282 humidVal.push_back(to_string(level));
284 attributeMap["temp"] = tempVal;
285 attributeMap["humid"] = humidVal;
287 // Create QueryParameters Map and add query params (if any)
288 QueryParamsMap queryParamsMap;
290 OCRepresentation rep;
291 rep.setAttributeMap(attributeMap);
293 // Invoke resource's pit API with attribute map, query map and the callback parameter
294 resource->put(rep , queryParamsMap , &onPut);
302 if (g_curResource->uri().find("/a/NM/TempHumSensor/virtual") != string::npos)
304 oicappData *ad = g_oicFindAd;
306 std::string tmpuri = PREFIX_URI + g_curResource->uri();
307 std::string tmphost = PREFIX_HOST + g_curResource->host();
309 DBG("OnFound Resource...");
310 DBG("Resource Uri : %s" , tmpuri.c_str());
311 DBG("Resource Host: %s" , tmphost.c_str());
313 _gl_update_item(ad , tmphost.c_str() , ad->itemConsumerHost);
314 _gl_update_item(ad , tmpuri.c_str() , ad->itemConsumerUri);
321 oicappData *ad = g_oicObserveAd;
323 AttributeMap attributeMap = g_curAttributeMap;
325 std::string tmpStr[2];
327 for (auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
329 tmpStr[index] = it->first;
330 tmpStr[index].append(" : ");
331 for (auto value = it->second.begin() ; value != it->second.end() ; ++value)
333 tmpStr[index].append(*value);
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);