4 #include <condition_variable>
11 #include "ocpayload.h"
14 #include <OCPlatform.h>
20 condition_variable g_callbackLock;
25 OC::OCResource::Ptr g_binaryswitchResource;
27 void printRepresentation(OCRepresentation rep)
29 for (auto itr = rep.begin(); itr != rep.end(); ++itr)
31 cout << "\t" << itr->attrname() << ":\t" << itr->getValueToString() << endl;
32 if (itr->type() == AttributeType::Vector)
34 switch (itr->base_type())
36 case AttributeType::OCRepresentation:
37 for (auto itr2 : (*itr).getValue<vector<OCRepresentation> >())
39 printRepresentation(itr2);
43 case AttributeType::Integer:
44 for (auto itr2 : (*itr).getValue<vector<int> >())
46 cout << "\t\t" << itr2 << endl;
50 case AttributeType::String:
51 for (auto itr2 : (*itr).getValue<vector<string> >())
53 cout << "\t\t" << itr2 << endl;
58 cout << "Unhandled base type " << itr->base_type() << endl;
62 else if (itr->type() == AttributeType::OCRepresentation)
64 printRepresentation((*itr).getValue<OCRepresentation>());
69 void handleLoginoutCB(const HeaderOptions &,
70 const OCRepresentation &rep, const int ecode)
72 cout << "Login/out response received code: " << ecode << endl;
74 if (rep.getPayload() != NULL)
76 printRepresentation(rep);
81 g_accesstoken = rep.getValueToString("accesstoken");
83 g_uid = rep.getValueToString("uid");
86 g_callbackLock.notify_all();
89 void printResource(const OCRepresentation &rep)
91 cout << "URI: " << rep.getUri() << endl;
93 vector<string> rt = rep.getResourceTypes();
94 for (auto it = rt.begin(); it != rt.end(); it++)
96 cout << "RT: " << (*it) << endl;
99 for (auto it = rep.begin();
100 it != rep.end(); it++)
102 cout << it->attrname() << " : " << it->getValueToString() << endl;
105 vector<OCRepresentation> children = rep.getChildren();
107 for (auto it = children.begin();
108 it != children.end(); it++)
114 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation &rep,
115 const int &eCode, const int &sequenceNumber)
119 if (eCode == OC_STACK_OK && sequenceNumber <= MAX_SEQUENCE_NUMBER)
121 if (sequenceNumber == OC_OBSERVE_REGISTER)
123 cout << "Observe registration action is successful" << endl;
126 cout << "OBSERVE RESULT:" << endl;
127 printRepresentation(rep);
131 if (eCode == OC_STACK_OK)
133 cout << "Observe registration failed or de-registration action failed/succeeded" << endl;
137 cout << "onObserve Response error: " << eCode << endl;
144 cout << "Exception: " << e.what() << " in onObserve" << endl;
148 void onPost(const HeaderOptions & /*headerOptions*/, const OCRepresentation &rep, const int eCode)
150 cout << "POST response: " << eCode << endl;
152 printRepresentation(rep);
155 void turnOnOffSwitch(bool toTurn)
157 OCRepresentation binarySwitch;
158 binarySwitch.setValue("value", toTurn);
160 QueryParamsMap query;
161 g_binaryswitchResource->post("oic.r.switch.binary", DEFAULT_INTERFACE, binarySwitch, query,
165 void getCollectionResource(const HeaderOptions &,
166 const OCRepresentation &rep, const int ecode)
168 cout << "Resource get: " << ecode << endl;
172 vector<OCRepresentation> children = rep.getChildren();
174 cout << "Constructing binary switch" << endl;
176 for (auto it = children.begin(); it != children.end(); it++)
178 cout << "RT: " << it->getResourceTypes().at(0) << endl;
180 if (it->getResourceTypes().at(0).compare("oic.r.switch.binary") == 0)
182 cout << "Observing " << it->getUri() << endl;
183 g_binaryswitchResource = OCPlatform::constructResourceObject(g_host,
185 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4), true,
186 { string("oic.r.switch.binary") }, { string(DEFAULT_INTERFACE) });
188 QueryParamsMap query;
189 g_binaryswitchResource->observe(ObserveType::Observe, query, &onObserve);
194 void foundAirconditionerResource(shared_ptr<OC::OCResource> resource)
196 vector<string> rt = resource->getResourceTypes();
198 cout << "Aircondition resource found: " << resource->uri() << endl;
200 g_callbackLock.notify_all();
202 for (auto it = rt.begin(); it != rt.end(); it++)
204 cout << "RT: " << *it << endl;
206 if (it->compare("x.com.samsung.da.device") == 0)
208 cout << "Found Samsung Airconditioner" << endl;
210 QueryParamsMap query;
211 query["if"] = string(LINK_INTERFACE);
212 //Request to collection resource
213 resource->get(query, &getCollectionResource);
218 void foundDevice(shared_ptr<OC::OCResource> resource)
220 cout << "Found device called!" << endl;
222 vector<string> rt = resource->getResourceTypes();
224 cout << "Device found: " << resource->uri() << endl;
225 cout << "DI: " << resource->sid() << endl;
227 g_callbackLock.notify_all();
229 for (auto it = rt.begin(); it != rt.end(); it++)
231 cout << "RT: " << *it << endl;
233 if (it->compare("oic.d.airconditioner") == 0)
235 string searchQuery = "/oic/res?di=";
236 searchQuery += resource->sid();
237 cout << "Airconditioner found" << endl;
238 OCPlatform::findResource(g_host, searchQuery,
239 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4),
240 &foundAirconditionerResource);
242 OCPlatform::OCPresenceHandle handle;
243 if (OCPlatform::subscribeDevicePresence(handle, g_host, { resource->sid() },
244 static_cast<OCConnectivityType>
245 (CT_ADAPTER_TCP | CT_IP_USE_V4), &onObserve) != OC_STACK_OK)
247 cout << "Device presence failed" << endl;
253 void errorFoundDevice(const std::string &uri, const int ecode)
255 cout << "Found device error on " << uri << " code " << ecode << endl;
256 g_callbackLock.notify_all();
259 void presenceDevice(OCStackResult , const unsigned int i, const string &str)
261 cout << "Presence received, i=" << i << " str=" << str << endl;
264 static FILE *client_open(const char * /*path*/, const char *mode)
266 return fopen("./aircon_controller.dat", mode);
269 int main(int argc, char *argv[])
271 if (argc != 4 && argc != 5)
273 cout << "Put \"[host-ipaddress:port] [authprovider] [authcode]\" for sign-up and sign-in and discover resources"
275 cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in and discover resources" <<
280 OCPersistentStorage ps{ client_open, fread, fwrite, fclose, unlink };
286 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
287 0, // Uses randomly available port
288 QualityOfService::LowQos,
292 OCPlatform::Configure(cfg);
294 OCStackResult result = OC_STACK_ERROR;
296 g_host = "coap+tcp://";
299 OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(g_host,
304 unique_lock<mutex> lock(blocker);
308 accountMgr->signIn(argv[2], argv[3], &handleLoginoutCB);
309 g_callbackLock.wait(lock);
313 accountMgr->signUp(argv[2], argv[3], &handleLoginoutCB);
314 g_callbackLock.wait(lock);
315 accountMgr->signIn(g_uid, g_accesstoken, &handleLoginoutCB);
316 g_callbackLock.wait(lock);
320 cout << "Subscribing resource presence ";
322 string query = "oic.wk.d&di=F0FDE28F-36BF-49BC-89F1-6AFB8D73E93C";
324 OCPlatform::OCPresenceHandle presenceHandle;
326 result = OCPlatform::subscribePresence(presenceHandle, g_host, query,
327 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4), &presenceDevice);
329 cout << " result: " << result << endl;
332 cout << "Finding airconditioner ";
334 result = OCPlatform::findResource(g_host, "/oic/res?rt=oic.wk.d",
335 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4),
336 &foundDevice, &errorFoundDevice);
338 cout << " result: " << result << endl;
340 g_callbackLock.wait(lock);
342 cout << "PUT 1/0 to turn on/off air conditioner, q to terminate" << endl;
349 OCRepresentation rep;
354 turnOnOffSwitch(true);
358 turnOnOffSwitch(false);