a187fab9c46912a21e7b9c340615f2f3302c5e31
[platform/upstream/iotivity.git] / resource / examples / winuiclient.cpp
1 /* ****************************************************************\r
2  *\r
3  * Copyright 2016 Intel Corporation All Rights Reserved.\r
4  *\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  *\r
17  ******************************************************************/\r
18 \r
19 #include <string>\r
20 #include "winuiclient.h"\r
21 #include <Windows.h>\r
22 #include <Commctrl.h>\r
23 #include <functional>\r
24 \r
25 extern int g_CurSliderVal;\r
26 extern HWND hwndVolumeSlider, hwndVolumeExpectedLabel;\r
27 using namespace WinUIClient;\r
28 void LabelPrintf (HWND hwndEdit, TCHAR * szFormat, ...);\r
29 \r
30 WinUIClientApp::WinUIClientApp(OCPersistentStorage ps)\r
31     : persistentStorage(ps),\r
32       OBSERVE_TYPE_TO_USE(ObserveType::Observe)\r
33 {\r
34 \r
35 }\r
36 \r
37 WinUIClientApp::~WinUIClientApp()\r
38 {\r
39 \r
40 }\r
41 \r
42 void WinUIClientApp::Initialize()\r
43 {\r
44     // Create PlatformConfig object\r
45     PlatformConfig cfg {\r
46         OC::ServiceType::InProc,\r
47         OC::ModeType::Both,\r
48         "0.0.0.0",\r
49         0,\r
50         OC::QualityOfService::LowQos,\r
51         &persistentStorage\r
52     };\r
53 \r
54     OCPlatform::Configure(cfg);\r
55 }\r
56 \r
57 void WinUIClientApp::Run()\r
58 {\r
59     try\r
60     {\r
61         // makes it so that all boolean values are printed as 'true/false' in this stream\r
62         std::cout.setf(std::ios::boolalpha);\r
63         // Find all resources\r
64         std::ostringstream requestURI;// << "?rt=core.media";\r
65         std::string s = OC_RSRVD_WELL_KNOWN_URI;\r
66         requestURI << s;\r
67 \r
68         OCPlatform::findResource("", requestURI.str(),\r
69                 CT_DEFAULT, std::bind(&WinUIClientApp::foundResource, this, std::placeholders::_1));\r
70         std::cout<< "Finding Resource... " <<std::endl;\r
71 \r
72         // Find resource is done twice so that we discover the original resources a second time.\r
73         // These resources will have the same uniqueidentifier (yet be different objects), so that\r
74         // we can verify/show the duplicate-checking code in foundResource(above);\r
75         OCPlatform::findResource("", requestURI.str(),\r
76                 CT_DEFAULT, std::bind(&WinUIClientApp::foundResource, this, std::placeholders::_1));\r
77         std::cout<< "Finding Resource for second time..." << std::endl;\r
78 \r
79     }catch(OCException& e)\r
80     {\r
81         std::cerr << "Exception in main: "<<e.what();\r
82     }\r
83 }\r
84 \r
85 void receivedPlatformInfo(const OCRepresentation& rep)\r
86 {\r
87     std::cout << "\nPlatform Information received ---->\n";\r
88     std::string value;\r
89     std::string values[] =\r
90     {\r
91         "pi",   "Platform ID                    ",\r
92         "mnmn", "Manufacturer name              ",\r
93         "mnml", "Manufacturer url               ",\r
94         "mnmo", "Manufacturer Model No          ",\r
95         "mndt", "Manufactured Date              ",\r
96         "mnpv", "Manufacturer Platform Version  ",\r
97         "mnos", "Manufacturer OS version        ",\r
98         "mnhw", "Manufacturer hardware version  ",\r
99         "mnfv", "Manufacturer firmware version  ",\r
100         "mnsl", "Manufacturer support url       ",\r
101         "st",   "Manufacturer system time       "\r
102     };\r
103 \r
104     for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]) ; i += 2)\r
105     {\r
106         if(rep.getValue(values[i], value))\r
107         {\r
108             std::cout << values[i + 1] << " : "<< value << std::endl;\r
109         }\r
110     }\r
111 }\r
112 \r
113 void receivedDeviceInfo(const OCRepresentation& rep)\r
114 {\r
115     std::cout << "\nDevice Information received ---->\n";\r
116     std::string value;\r
117     std::string values[] =\r
118     {\r
119         "di",  "Device ID        ",\r
120         "n",   "Device name      ",\r
121         "lcv", "Spec version url ",\r
122         "dmv", "Data Model Model ",\r
123     };\r
124 \r
125     for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]); i += 2)\r
126     {\r
127         if (rep.getValue(values[i], value))\r
128         {\r
129             std::cout << values[i + 1] << " : " << value << std::endl;\r
130         }\r
131     }\r
132 }\r
133 \r
134 void WinUIClientApp::FindResources()\r
135 {\r
136     std::ostringstream requestURI;\r
137     requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.media";\r
138     OCPlatform::findResource("", requestURI.str(),\r
139                             CT_DEFAULT, std::bind(&WinUIClientApp::foundResource, this, std::placeholders::_1));\r
140 }\r
141 \r
142 \r
143 // Callback to found resources\r
144 void WinUIClientApp::foundResource(std::shared_ptr<OCResource> resource)\r
145 {\r
146     std::cout << "In foundResource\n";\r
147     std::string resourceURI;\r
148     std::string hostAddress;\r
149     std::string platformDiscoveryURI = "/oic/p";\r
150     std::string deviceDiscoveryURI   = "/oic/d";\r
151     try\r
152     {\r
153         {\r
154             std::lock_guard<std::mutex> lock(curResourceLock);\r
155             if (discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())\r
156             {\r
157                 std::cout << "Found resource " << resource->uniqueIdentifier() <<\r
158                     " for the first time on server with ID: "<< resource->sid()<<std::endl;\r
159                 discoveredResources[resource->uniqueIdentifier()] = resource;\r
160             }\r
161             else\r
162             {\r
163                 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;\r
164             }\r
165 \r
166             if (curResource)\r
167             {\r
168                 std::cout << "Found another resource, ignoring"<<std::endl;\r
169                 return;\r
170             }\r
171         }\r
172 \r
173         // Do some operations with resource object.\r
174         if (resource)\r
175         {\r
176             std::cout<<"DISCOVERED Resource:"<<std::endl;\r
177             // Get the resource URI\r
178             resourceURI = resource->uri();\r
179             std::cout << "\tURI of the resource: " << resourceURI << std::endl;\r
180 \r
181             // Get the resource host address\r
182             hostAddress = resource->host();\r
183             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;\r
184 \r
185             // Get the resource types\r
186             std::cout << "\tList of resource types: " << std::endl;\r
187             for(auto &resourceTypes : resource->getResourceTypes())\r
188             {\r
189                 std::cout << "\t\t" << resourceTypes << std::endl;\r
190             }\r
191 \r
192             // Get the resource interfaces\r
193             std::cout << "\tList of resource interfaces: " << std::endl;\r
194             for(auto &resourceInterfaces : resource->getResourceInterfaces())\r
195             {\r
196                 std::cout << "\t\t" << resourceInterfaces << std::endl;\r
197             }\r
198             \r
199             OCStackResult ret;\r
200             std::cout << "Querying for platform information... " << std::endl;\r
201 \r
202             ret = OCPlatform::getPlatformInfo("", platformDiscoveryURI, CT_ADAPTER_IP,\r
203                     &receivedPlatformInfo);\r
204 \r
205             if (ret == OC_STACK_OK)\r
206             {\r
207                 std::cout << "Get platform information is done." << std::endl;\r
208             }\r
209             else\r
210             {\r
211                 std::cout << "Get platform information failed." << std::endl;\r
212             }\r
213 \r
214             std::cout << "Querying for device information... " << std::endl;\r
215 \r
216             ret = OCPlatform::getDeviceInfo(resource->host(), deviceDiscoveryURI, CT_ADAPTER_IP,\r
217                     &receivedDeviceInfo);\r
218 \r
219             if (ret == OC_STACK_OK)\r
220             {\r
221                 std::cout << "Getting device information is done." << std::endl;\r
222             }\r
223             else\r
224             {\r
225                 std::cout << "Getting device information failed." << std::endl;\r
226             }\r
227    \r
228             if (resourceURI == "/a/media")\r
229             {\r
230                 curResource = resource;\r
231                 // Call a local function which will internally invoke get API on the resource pointer\r
232                 this->GetMediaRepresentation();\r
233                 this->BeginObserving();\r
234             }\r
235         }\r
236         else\r
237         {\r
238             // Resource is invalid\r
239             std::cout << "Resource is invalid" << std::endl;\r
240         }\r
241 \r
242     }\r
243     catch(std::exception& e)\r
244     {\r
245         std::cerr << "Exception in foundResource: "<< e.what() << std::endl;\r
246     }\r
247 }\r
248 \r
249 // Local function to get representation of media resource\r
250 void WinUIClientApp::GetMediaRepresentation()\r
251 {\r
252     if (curResource)\r
253     {\r
254         std::cout << "Getting Media Representation..."<<std::endl;\r
255         // Invoke resource's get API with the callback parameter\r
256 \r
257         QueryParamsMap test;\r
258         curResource->get(test, std::bind(&WinUIClientApp::onGet, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));\r
259 \r
260     }\r
261     else\r
262     {\r
263         std::cout << "No Current Resource to GetMediaRepresentation..."<<std::endl;\r
264     }\r
265 }\r
266 \r
267 // Callback handler on GET request\r
268 void WinUIClientApp::onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)\r
269 {\r
270     try\r
271     {\r
272         if (OC_STACK_OK == eCode)\r
273         {\r
274             std::cout << "GET request was successful" << std::endl;\r
275             std::cout << "Resource URI: " << rep.getUri() << std::endl;\r
276 \r
277             rep.getValue("state", mymedia.m_state);\r
278             rep.getValue("volume", mymedia.m_volume);\r
279             rep.getValue("name", mymedia.m_name);\r
280 \r
281             std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
282             std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
283             std::cout << "\tname: " << mymedia.m_name << std::endl;\r
284 \r
285             g_CurSliderVal = mymedia.m_volume;\r
286             SendMessage(hwndVolumeSlider, TBM_SETPOS, TRUE, g_CurSliderVal);\r
287             LabelPrintf(hwndVolumeExpectedLabel,"Volume: %i", g_CurSliderVal);\r
288 \r
289         }\r
290         else\r
291         {\r
292             std::cout << "onGET Response error: " << eCode << std::endl;\r
293             std::exit(-1);\r
294         }\r
295     }\r
296     catch(std::exception& e)\r
297     {\r
298         std::cout << "Exception: " << e.what() << " in onGet" << std::endl;\r
299     }\r
300 }\r
301 \r
302 // Local function to put a different state for this resource\r
303 void WinUIClientApp::PutMediaRepresentation()\r
304 {\r
305     if (curResource)\r
306     {\r
307         OCRepresentation rep;\r
308 \r
309         std::cout << "Putting media representation..."<<std::endl;\r
310 \r
311         rep.setValue("state", mymedia.m_state);\r
312         rep.setValue("volume", mymedia.m_volume);\r
313 \r
314         // Invoke resource's put API with rep, query map and the callback parameter\r
315         curResource->put(rep, QueryParamsMap(), std::bind(&WinUIClientApp::onPut, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));\r
316     }\r
317 }\r
318 \r
319 // callback handler on PUT request\r
320 void WinUIClientApp::onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)\r
321 {\r
322     try\r
323     {\r
324         if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_CHANGED == eCode)\r
325         {\r
326             std::cout << "PUT request was successful" << std::endl;\r
327 \r
328             rep.getValue("state", mymedia.m_state);\r
329             rep.getValue("volume", mymedia.m_volume);\r
330             rep.getValue("name", mymedia.m_name);\r
331 \r
332             std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
333             std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
334             std::cout << "\tname: " << mymedia.m_name << std::endl;\r
335 \r
336         }\r
337         else\r
338         {\r
339             std::cout << "onPut Response error: " << eCode << std::endl;\r
340             std::exit(-1);\r
341         }\r
342     }\r
343     catch(std::exception& e)\r
344     {\r
345         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;\r
346     }\r
347 }\r
348 \r
349 // Local function to put a different state for this resource\r
350 void WinUIClientApp::PostMediaRepresentation()\r
351 {\r
352     if (curResource)\r
353     {\r
354         OCRepresentation rep;\r
355 \r
356         std::cout << "Posting media representation..."<<std::endl;\r
357 \r
358         rep.setValue("state", mymedia.m_state);\r
359         rep.setValue("volume", mymedia.m_volume);\r
360 \r
361         // Invoke resource's post API with rep, query map and the callback parameter\r
362         curResource->post(rep, QueryParamsMap(), std::bind(&WinUIClientApp::onPost, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));\r
363     }\r
364 }\r
365 \r
366 void WinUIClientApp::onPost(const HeaderOptions& /*headerOptions*/,\r
367         const OCRepresentation& rep, const int eCode)\r
368 {\r
369     try\r
370     {\r
371         if (OC_STACK_OK == eCode || (OC_STACK_RESOURCE_CREATED == eCode\r
372                 || OC_STACK_RESOURCE_CHANGED == eCode))\r
373         {\r
374             std::cout << "POST request was successful" << std::endl;\r
375 \r
376             if (rep.hasAttribute("createduri"))\r
377             {\r
378                 std::cout << "\tUri of the created resource: "\r
379                     << rep.getValue<std::string>("createduri") << std::endl;\r
380             }\r
381             else\r
382             {\r
383                 rep.getValue("state", mymedia.m_state);\r
384                 rep.getValue("volume", mymedia.m_volume);\r
385                 rep.getValue("name", mymedia.m_name);\r
386 \r
387                 std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
388                 std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
389                 std::cout << "\tname: " << mymedia.m_name << std::endl;\r
390             }\r
391 \r
392         }\r
393         else\r
394         {\r
395             std::cout << "onPost Response error: " << eCode << std::endl;\r
396             std::exit(-1);\r
397         }\r
398     }\r
399     catch (std::exception& e)\r
400     {\r
401         std::cout << "Exception: " << e.what() << " in onPost" << std::endl;\r
402     }\r
403 }\r
404 \r
405 void WinUIClientApp::onPost2(const HeaderOptions& /*headerOptions*/,\r
406         const OCRepresentation& rep, const int eCode)\r
407 {\r
408     try\r
409     {\r
410         if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_CREATED == eCode)\r
411         {\r
412             std::cout << "POST request was successful" << std::endl;\r
413 \r
414             if (rep.hasAttribute("createduri"))\r
415             {\r
416                 std::cout << "\tUri of the created resource: "\r
417                     << rep.getValue<std::string>("createduri") << std::endl;\r
418             }\r
419             else\r
420             {\r
421                 rep.getValue("state", mymedia.m_state);\r
422                 rep.getValue("volume", mymedia.m_volume);\r
423                 rep.getValue("name", mymedia.m_name);\r
424 \r
425                 std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
426                 std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
427                 std::cout << "\tname: " << mymedia.m_name << std::endl;\r
428             }\r
429 \r
430         }\r
431         else\r
432         {\r
433             std::cout << "onPost2 Response error: " << eCode << std::endl;\r
434             std::exit(-1);\r
435         }\r
436     }\r
437     catch(std::exception& e)\r
438     {\r
439         std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;\r
440     }\r
441 \r
442 }\r
443 \r
444 void WinUIClientApp::onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,\r
445                     const int& eCode, const int& sequenceNumber)\r
446 {\r
447     try\r
448     {\r
449         if (OC_STACK_OK == eCode && sequenceNumber <= MAX_SEQUENCE_NUMBER)\r
450         {\r
451             if (sequenceNumber == OC_OBSERVE_REGISTER)\r
452             {\r
453                 std::cout << "Observe registration action is successful" << std::endl;\r
454             }\r
455 \r
456             std::cout << "OBSERVE RESULT:"<<std::endl;\r
457             std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;\r
458             rep.getValue("state", mymedia.m_state);\r
459             rep.getValue("volume", mymedia.m_volume);\r
460             rep.getValue("name", mymedia.m_name);\r
461 \r
462             std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
463             std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
464             std::cout << "\tname: " << mymedia.m_name << std::endl;\r
465 \r
466             g_CurSliderVal = mymedia.m_volume;\r
467             SendMessage(hwndVolumeSlider, TBM_SETPOS, TRUE, g_CurSliderVal);\r
468             LabelPrintf(hwndVolumeExpectedLabel,"Volume: %i", g_CurSliderVal);\r
469         }\r
470         else\r
471         {\r
472             if (OC_STACK_OK == eCode)\r
473             {\r
474                 std::cout << "No observe option header is returned in the response." << std::endl;\r
475                 std::cout << "For a registration request, it means the registration failed"\r
476                         << std::endl;\r
477             }\r
478             else\r
479             {\r
480                 std::cout << "onObserve Response error: " << eCode << std::endl;\r
481                 std::exit(-1);\r
482             }\r
483         }\r
484     }\r
485     catch (std::exception& e)\r
486     {\r
487         std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;\r
488     }\r
489 \r
490 }\r
491 \r
492 void WinUIClientApp::BeginObserving()\r
493 {\r
494     if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)\r
495         std::cout << std::endl << "Observe is used." << std::endl << std::endl;\r
496     else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)\r
497         std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;\r
498 \r
499     curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), std::bind(&WinUIClientApp::onObserve, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));\r
500 }\r
501 \r
502 void WinUIClientApp::CancelObserving()\r
503 {\r
504     std::cout<<"Cancelling Observe..."<<std::endl;\r
505     OCStackResult result = curResource->cancelObserve();\r
506 }\r
507 \r
508 std::shared_ptr<OCResource> WinUIClientApp::GetResource()\r
509 {\r
510     return curResource;\r
511 }\r
512 \r
513 int WinUIClientApp::observe_count()\r
514 {\r
515     static int oc = 0;\r
516     return ++oc;\r
517 }\r
518 \r