4 #include <condition_variable>
12 #include "ocpayload.h"
15 #include <OCPlatform.h>
17 #define DEFAULT_CONTEXT_VALUE 0x99
19 #define maxSequenceNumber 0xFFFFFF
25 condition_variable g_callbackLock;
27 std::string g_accesstoken;
30 OC::OCResource::Ptr g_binaryswitchResource;
32 void printRepresentation(OCRepPayloadValue *value)
36 std::cout << "Key: " << value->name;
40 std::cout << " Value: None" << std::endl;
43 std::cout << " Value: " << value->i << std::endl;
45 case OCREP_PROP_DOUBLE:
46 std::cout << " Value: " << value->d << std::endl;
49 std::cout << " Value: " << value->b << std::endl;
51 case OCREP_PROP_STRING:
52 std::cout << " Value: " << value->str << std::endl;
54 case OCREP_PROP_BYTE_STRING:
55 std::cout << " Value: Byte String" << std::endl;
57 case OCREP_PROP_OBJECT:
58 std::cout << " Value: Object" << std::endl;
60 case OCREP_PROP_ARRAY:
61 std::cout << " Value: Array" << std::endl;
65 if (strcmp(value->name, "accesstoken") == 0)
67 g_accesstoken = value->str;
70 if (strcmp(value->name, "uid") == 0)
79 void handleLoginoutCB(const HeaderOptions &,
80 const OCRepresentation &rep, const int ecode)
82 cout << "Login/out response received code: " << ecode << endl;
84 if (rep.getPayload() != NULL)
86 printRepresentation(rep.getPayload()->values);
89 g_callbackLock.notify_all();
92 void printRepresentation(const OCRepresentation &rep)
94 cout << "URI: " << rep.getUri() << endl;
96 vector<string> rt = rep.getResourceTypes();
97 for (auto it = rt.begin(); it != rt.end(); it++)
99 cout << "RT: " << (*it) << endl;
102 for (auto it = rep.begin();
103 it != rep.end(); it++)
105 cout << it->attrname() << " : " << it->getValueToString() << endl;
108 vector<OCRepresentation> children = rep.getChildren();
110 for (auto it = children.begin();
111 it != children.end(); it++)
113 printRepresentation(*it);
117 void onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation &rep,
118 const int &eCode, const int &sequenceNumber)
122 if (eCode == OC_STACK_OK && sequenceNumber != maxSequenceNumber + 1)
124 if (sequenceNumber == OC_OBSERVE_REGISTER)
126 cout << "Observe registration action is successful" << endl;
129 cout << "OBSERVE RESULT:" << endl;
130 printRepresentation(rep);
134 if (eCode == OC_STACK_OK)
136 std::cout << "Observe registration failed or de-registration action failed/succeeded" << std::endl;
140 cout << "onObserve Response error: " << eCode << endl;
147 cout << "Exception: " << e.what() << " in onObserve" << endl;
151 void onPut(const HeaderOptions & /*headerOptions*/, const OCRepresentation &rep, const int eCode)
153 cout << "PUT response: " << eCode << endl;
155 printRepresentation(rep);
158 void turnOnOffSwitch(bool toTurn)
160 OCRepresentation binarySwitch;
161 binarySwitch.setValue("value", toTurn);
163 QueryParamsMap query;
164 g_binaryswitchResource->post("oic.r.switch.binary", DEFAULT_INTERFACE, binarySwitch, query, &onPut);
167 void getCollectionResource(const HeaderOptions &,
168 const OCRepresentation &rep, const int ecode)
170 cout << "Resource get: " << ecode << endl;
172 printRepresentation(rep);
174 vector<OCRepresentation> children = rep.getChildren();
176 cout << "Constructing binary switch" << endl;
178 for (auto it = children.begin(); it != children.end(); it++)
180 cout << "RT: " << it->getResourceTypes().at(0) << endl;
182 if (it->getResourceTypes().at(0).compare("oic.r.switch.binary") == 0)
184 cout << "Observing " << it->getUri() << endl;
185 g_binaryswitchResource = OCPlatform::constructResourceObject(g_host,
187 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4), true,
188 { string("oic.r.switch.binary") }, { string(DEFAULT_INTERFACE) });
190 QueryParamsMap query;
191 g_binaryswitchResource->observe(ObserveType::Observe, query, &onObserve);
196 void foundAirconditionerResource(shared_ptr<OC::OCResource> resource)
198 vector<string> rt = resource->getResourceTypes();
200 cout << "Aircondition resource found: " << resource->uri() << endl;
202 g_callbackLock.notify_all();
204 for (auto it = rt.begin(); it != rt.end(); it++)
206 cout << "RT: " << *it << endl;
208 if (it->compare("x.com.samsung.da.device") == 0)
210 cout << "Found Samsung Airconditioner" << endl;
212 QueryParamsMap query;
213 query["if"] = string(LINK_INTERFACE);
214 //Request to collection resource
215 resource->get(query, &getCollectionResource);
220 void foundDevice(shared_ptr<OC::OCResource> resource)
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);
245 void presenceDevice(OCStackResult , const unsigned int i, const std::string &str)
247 cout << "Presence received, i=" << i << " str=" << str << endl;
250 static FILE *client_open(const char * /*path*/, const char *mode)
252 return fopen("./resource_controller.dat", mode);
255 int main(int argc, char *argv[])
257 if (argc != 4 && argc != 5)
259 cout << "Put \"[host-ipaddress:port] [authprovider] [authcode]\" for sign-up and sign-in and discover resources"
261 cout << "Put \"[host-ipaddress:port] [uid] [accessToken] 1\" for sign-in and discover resources" <<
266 OCPersistentStorage ps{ client_open, fread, fwrite, fclose, unlink };
272 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
273 0, // Uses randomly available port
274 QualityOfService::LowQos,
278 OCPlatform::Configure(cfg);
280 OCStackResult result = OC_STACK_ERROR;
282 g_host = "coap+tcp://";
285 OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(g_host,
290 unique_lock<std::mutex> lock(blocker);
294 accountMgr->signIn(argv[2], argv[3], &handleLoginoutCB);
295 g_callbackLock.wait(lock);
299 accountMgr->signUp(argv[2], argv[3], &handleLoginoutCB);
300 g_callbackLock.wait(lock);
301 accountMgr->signIn(g_uid, g_accesstoken, &handleLoginoutCB);
302 g_callbackLock.wait(lock);
306 cout << "Subscribing resource presence ";
308 string query = "oic.wk.d&di=F0FDE28F-36BF-49BC-89F1-6AFB8D73E93C";
310 OCPlatform::OCPresenceHandle presenceHandle;
312 result = OCPlatform::subscribePresence(presenceHandle, g_host, query,
313 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4), &presenceDevice);
315 cout << " result: " << result << endl;
318 cout << "Finding airconditioner ";
320 result = OCPlatform::findResource(g_host, "/oic/res?rt=oic.wk.d",
321 static_cast<OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4),
324 cout << " result: " << result << endl;
326 g_callbackLock.wait(lock);
328 cout << "PUT 1/0 to turn on/off air conditioner, q to terminate" << endl;
335 OCRepresentation rep;
340 turnOnOffSwitch(true);
344 turnOnOffSwitch(false);