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 vector<string> rt = resource->getResourceTypes();
222 cout << "Device found: " << resource->uri() << endl;
223 cout << "DI: " << resource->sid() << endl;
225 g_callbackLock.notify_all();
227 for (auto it = rt.begin(); it != rt.end(); it++)
229 cout << "RT: " << *it << endl;
231 if (it->compare("oic.d.airconditioner") == 0)
233 string searchQuery = "/oic/res?di=";
234 searchQuery += resource->sid();
235 cout << "Airconditioner found" << endl;
236 OCPlatform::findResource(g_host, searchQuery,
237 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4),
238 &foundAirconditionerResource);
240 OCPlatform::OCPresenceHandle handle;
241 if (OCPlatform::subscribeDevicePresence(handle, g_host, { resource->sid() },
242 static_cast<OCConnectivityType>
243 (CT_ADAPTER_TCP | CT_IP_USE_V4), &onObserve) != OC_STACK_OK)
245 cout << "Device presence failed" << endl;
251 void presenceDevice(OCStackResult , const unsigned int i, const string &str)
253 cout << "Presence received, i=" << i << " str=" << str << endl;
256 static FILE *client_open(const char * /*path*/, const char *mode)
258 return fopen("./aircon_controller.dat", mode);
261 int main(int argc, char *argv[])
263 if (argc != 4 && argc != 5)
265 cout << "Put \"[host-ipaddress:port] [authprovider] [authcode]\" for sign-up and sign-in and discover resources"
267 cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in and discover resources" <<
272 OCPersistentStorage ps{ client_open, fread, fwrite, fclose, unlink };
278 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
279 0, // Uses randomly available port
280 QualityOfService::LowQos,
284 OCPlatform::Configure(cfg);
286 OCStackResult result = OC_STACK_ERROR;
288 g_host = "coap+tcp://";
291 OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(g_host,
296 unique_lock<mutex> lock(blocker);
300 accountMgr->signIn(argv[2], argv[3], &handleLoginoutCB);
301 g_callbackLock.wait(lock);
305 accountMgr->signUp(argv[2], argv[3], &handleLoginoutCB);
306 g_callbackLock.wait(lock);
307 accountMgr->signIn(g_uid, g_accesstoken, &handleLoginoutCB);
308 g_callbackLock.wait(lock);
312 cout << "Subscribing resource presence ";
314 string query = "oic.wk.d&di=F0FDE28F-36BF-49BC-89F1-6AFB8D73E93C";
316 OCPlatform::OCPresenceHandle presenceHandle;
318 result = OCPlatform::subscribePresence(presenceHandle, g_host, query,
319 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4), &presenceDevice);
321 cout << " result: " << result << endl;
324 cout << "Finding airconditioner ";
326 result = OCPlatform::findResource(g_host, "/oic/res?rt=oic.wk.d",
327 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4),
330 cout << " result: " << result << endl;
332 g_callbackLock.wait(lock);
334 cout << "PUT 1/0 to turn on/off air conditioner, q to terminate" << endl;
341 OCRepresentation rep;
346 turnOnOffSwitch(true);
350 turnOnOffSwitch(false);