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;
71 void startObserve(oicappData *ad)
73 if(g_curResource != nullptr)
77 g_curResource->observe(ObserveType::Observe , test , &onObserve);
81 void printAttributeMap(const AttributeMap attributeMap)
83 for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
85 DBG("\tAttribute name: %s" , it->first.c_str());
86 for(auto valueItr = it->second.begin() ; valueItr != it->second.end() ; ++valueItr)
88 DBG("\t\tAttribute value: %s" , (*valueItr).c_str());
93 void updateAttribute(const AttributeMap attributeMap)
95 g_oicappClientAd->temp = std::stoi(attributeMap.at("temp")[0]);
96 g_oicappClientAd->humid = std::stoi(attributeMap.at("humid")[0]);
101 DBG("Cancelling Observe...");
103 OCStackResult result = OC_STACK_ERROR;
105 if(g_curResource != nullptr)
107 result = g_curResource->cancelObserve();
110 DBG("Cancel result: %d" , result);
113 void onObserve(const OCRepresentation& rep , const int& eCode , const int& sequenceNumber)
116 AttributeMap attributeMap = rep.getAttributeMap();
117 if(eCode == SUCCESS_RESPONSE)
119 DBG("OBSERVE RESULT:");
120 DBG("\tSequenceNumber: %d" , sequenceNumber);
122 printAttributeMap(attributeMap);
123 // updateAttribute(attributeMap);
124 g_curAttributeMap = attributeMap;
129 ERR("onObserve Response error: %d" , eCode);
134 // callback handler on PUT request
135 void onPut(const OCRepresentation& rep , const int eCode)
137 AttributeMap attributeMap = rep.getAttributeMap();
138 if(eCode == SUCCESS_RESPONSE)
140 DBG("PUT request was successful");
142 printAttributeMap(attributeMap);
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.");
149 if(g_curResource != nullptr)
151 DBG("Observe Start");
153 g_curResource->observe(ObserveType::Observe , test , &onObserve);
158 ERR("onPut Response error: %d" , eCode);
163 // callback handler on GET request
164 void onGet(const OCRepresentation& rep , const int eCode)
167 AttributeMap attributeMap = rep.getAttributeMap();
168 if(eCode == SUCCESS_RESPONSE)
170 DBG("GET Succeeded:");
172 printAttributeMap(attributeMap);
173 updateAttribute(attributeMap);
177 ERR("onGet Response error: %d" , eCode);
182 // Local function to get representation of light resource
183 void getLightRepresentation(std::shared_ptr< OCResource > resource)
187 DBG("Getting Light Representation...");
188 // Invoke resource's get API with the callback parameter
190 resource->get(test , &onGet);
194 // Callback to found resources
195 static void foundResource(std::shared_ptr< OCResource > resource)
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());
205 if(resource->uri().find("/a/NM") != string::npos)
208 g_curResource = resource;
214 ERR("Resource is invalid");
218 catch(std::exception& e)
223 OCStackResult nmfindResource(const std::string& host , const std::string& resourceName)
225 if(g_platform != nullptr)
227 return g_platform->findResource(host , resourceName , &foundResource);
230 return OC_STACK_ERROR;
233 int oicapp_client_start(oicappData *ad)
235 g_cfg.ipAddress = ad->ipAddr;
237 g_cfg.mode = ModeType::Client;
238 g_cfg.serviceType = ServiceType::InProc;
240 retv_if(NULL != g_platform , -1);
242 g_platform = new OCPlatform(g_cfg);
244 g_oicappClientAd = ad;
249 void oicapp_client_stop()
251 if(g_curResource != nullptr)
254 g_curResource = NULL;
262 g_oicappClientAd = NULL;
265 // Local function to put a different state for this resource
266 int oicapp_client_put(int power , int level)
268 std::shared_ptr< OCResource > resource = g_curResource;
270 retv_if(NULL == g_curResource , -1);
272 DBG("Putting light representation...");
274 AttributeMap attributeMap;
276 AttributeValues tempVal;
277 AttributeValues humidVal;
279 tempVal.push_back(to_string(power));
280 humidVal.push_back(to_string(level));
282 attributeMap["temp"] = tempVal;
283 attributeMap["humid"] = humidVal;
285 // Create QueryParameters Map and add query params (if any)
286 QueryParamsMap queryParamsMap;
288 OCRepresentation rep;
289 rep.setAttributeMap(attributeMap);
291 // Invoke resource's pit API with attribute map, query map and the callback parameter
292 resource->put(rep , queryParamsMap , &onPut);
300 if(g_curResource->uri().find("/a/NM/TempHumSensor/virtual") != string::npos)
302 oicappData *ad = g_oicFindAd;
304 std::string tmpuri = PREFIX_URI + g_curResource->uri();
305 std::string tmphost = PREFIX_HOST + g_curResource->host();
307 DBG("OnFound Resource...");
308 DBG("Resource Uri : %s" , tmpuri.c_str());
309 DBG("Resource Host: %s" , tmphost.c_str());
311 _gl_update_item(ad , tmphost.c_str() , ad->itemConsumerHost);
312 _gl_update_item(ad , tmpuri.c_str() , ad->itemConsumerUri);
319 oicappData *ad = g_oicObserveAd;
321 AttributeMap attributeMap = g_curAttributeMap;
323 std::string tmpStr[2];
325 for(auto it = attributeMap.begin() ; it != attributeMap.end() ; ++it)
327 tmpStr[index] = it->first;
328 tmpStr[index].append(" : ");
329 for(auto value = it->second.begin() ; value != it->second.end() ; ++value)
331 tmpStr[index].append(*value);
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);