1 /* ****************************************************************
\r
3 * Copyright 2016 Intel Corporation All Rights Reserved.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 ******************************************************************/
\r
20 #include "winuiclient.h"
\r
21 #include <Windows.h>
\r
22 #include <Commctrl.h>
\r
23 #include <functional>
\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
30 WinUIClientApp::WinUIClientApp(OCPersistentStorage ps)
\r
31 : persistentStorage(ps),
\r
32 OBSERVE_TYPE_TO_USE(ObserveType::Observe)
\r
37 WinUIClientApp::~WinUIClientApp()
\r
42 void WinUIClientApp::Initialize()
\r
44 // Create PlatformConfig object
\r
45 PlatformConfig cfg {
\r
46 OC::ServiceType::InProc,
\r
50 OC::QualityOfService::LowQos,
\r
54 OCPlatform::Configure(cfg);
\r
57 void WinUIClientApp::Run()
\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
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
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
79 }catch(OCException& e)
\r
81 std::cerr << "Exception in main: "<<e.what();
\r
85 void receivedPlatformInfo(const OCRepresentation& rep)
\r
87 std::cout << "\nPlatform Information received ---->\n";
\r
89 std::string values[] =
\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
104 for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]); i += 2)
\r
106 if(rep.getValue(values[i], value))
\r
108 std::cout << values[i + 1] << " : "<< value << std::endl;
\r
113 void receivedDeviceInfo(const OCRepresentation& rep)
\r
115 std::cout << "\nDevice Information received ---->\n";
\r
117 std::string values[] =
\r
119 "di", "Device ID ",
\r
120 "n", "Device name ",
\r
121 "lcv", "Spec version url ",
\r
122 "dmv", "Data Model Model ",
\r
125 for (unsigned int i = 0; i < sizeof(values) / sizeof(values[0]); i += 2)
\r
127 if (rep.getValue(values[i], value))
\r
129 std::cout << values[i + 1] << " : " << value << std::endl;
\r
134 void WinUIClientApp::FindResources()
\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
143 // Callback to found resources
\r
144 void WinUIClientApp::foundResource(std::shared_ptr<OCResource> resource)
\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
154 std::lock_guard<std::mutex> lock(curResourceLock);
\r
155 if (discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())
\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
163 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;
\r
168 std::cout << "Found another resource, ignoring"<<std::endl;
\r
173 // Do some operations with resource object.
\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
181 // Get the resource host address
\r
182 hostAddress = resource->host();
\r
183 std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
\r
185 // Get the resource types
\r
186 std::cout << "\tList of resource types: " << std::endl;
\r
187 for(auto &resourceTypes : resource->getResourceTypes())
\r
189 std::cout << "\t\t" << resourceTypes << std::endl;
\r
192 // Get the resource interfaces
\r
193 std::cout << "\tList of resource interfaces: " << std::endl;
\r
194 for(auto &resourceInterfaces : resource->getResourceInterfaces())
\r
196 std::cout << "\t\t" << resourceInterfaces << std::endl;
\r
200 std::cout << "Querying for platform information... " << std::endl;
\r
202 ret = OCPlatform::getPlatformInfo(resource->host(), platformDiscoveryURI,
\r
203 resource->connectivityType(),
\r
204 &receivedPlatformInfo);
\r
206 if (ret == OC_STACK_OK)
\r
208 std::cout << "Get platform information is done." << std::endl;
\r
212 std::cout << "Get platform information failed." << std::endl;
\r
215 std::cout << "Querying for device information... " << std::endl;
\r
217 ret = OCPlatform::getDeviceInfo(resource->host(), deviceDiscoveryURI,
\r
218 resource->connectivityType(),
\r
219 &receivedDeviceInfo);
\r
222 if (ret == OC_STACK_OK)
\r
224 std::cout << "Getting device information is done." << std::endl;
\r
228 std::cout << "Getting device information failed." << std::endl;
\r
231 if (resourceURI == "/a/media")
\r
233 curResource = resource;
\r
234 // Call a local function which will internally invoke get API on the resource pointer
\r
235 this->GetMediaRepresentation();
\r
236 this->BeginObserving();
\r
241 // Resource is invalid
\r
242 std::cout << "Resource is invalid" << std::endl;
\r
246 catch(std::exception& e)
\r
248 std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
\r
252 // Local function to get representation of media resource
\r
253 void WinUIClientApp::GetMediaRepresentation()
\r
257 std::cout << "Getting Media Representation..."<<std::endl;
\r
258 // Invoke resource's get API with the callback parameter
\r
260 QueryParamsMap test;
\r
261 curResource->get(test, std::bind(&WinUIClientApp::onGet, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
\r
266 std::cout << "No Current Resource to GetMediaRepresentation..."<<std::endl;
\r
270 // Callback handler on GET request
\r
271 void WinUIClientApp::onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
\r
275 if (OC_STACK_OK == eCode)
\r
277 std::cout << "GET request was successful" << std::endl;
\r
278 std::cout << "Resource URI: " << rep.getUri() << std::endl;
\r
280 rep.getValue("state", mymedia.m_state);
\r
281 rep.getValue("volume", mymedia.m_volume);
\r
282 rep.getValue("name", mymedia.m_name);
\r
284 std::cout << "\tstate: " << mymedia.m_state << std::endl;
\r
285 std::cout << "\tvolume: " << mymedia.m_volume << std::endl;
\r
286 std::cout << "\tname: " << mymedia.m_name << std::endl;
\r
288 g_CurSliderVal = mymedia.m_volume;
\r
289 SendMessage(hwndVolumeSlider, TBM_SETPOS, TRUE, g_CurSliderVal);
\r
290 LabelPrintf(hwndVolumeExpectedLabel,"Volume: %i", g_CurSliderVal);
\r
295 std::cout << "onGET Response error: " << eCode << std::endl;
\r
299 catch(std::exception& e)
\r
301 std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
\r
305 // Local function to put a different state for this resource
\r
306 void WinUIClientApp::PutMediaRepresentation()
\r
310 OCRepresentation rep;
\r
312 std::cout << "Putting media representation..."<<std::endl;
\r
314 rep.setValue("state", mymedia.m_state);
\r
315 rep.setValue("volume", mymedia.m_volume);
\r
317 // Invoke resource's put API with rep, query map and the callback parameter
\r
318 curResource->put(rep, QueryParamsMap(), std::bind(&WinUIClientApp::onPut, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
\r
322 // callback handler on PUT request
\r
323 void WinUIClientApp::onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)
\r
327 if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_CHANGED == eCode)
\r
329 std::cout << "PUT request was successful" << std::endl;
\r
331 rep.getValue("state", mymedia.m_state);
\r
332 rep.getValue("volume", mymedia.m_volume);
\r
333 rep.getValue("name", mymedia.m_name);
\r
335 std::cout << "\tstate: " << mymedia.m_state << std::endl;
\r
336 std::cout << "\tvolume: " << mymedia.m_volume << std::endl;
\r
337 std::cout << "\tname: " << mymedia.m_name << std::endl;
\r
342 std::cout << "onPut Response error: " << eCode << std::endl;
\r
346 catch(std::exception& e)
\r
348 std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
\r
352 // Local function to put a different state for this resource
\r
353 void WinUIClientApp::PostMediaRepresentation()
\r
357 OCRepresentation rep;
\r
359 std::cout << "Posting media representation..."<<std::endl;
\r
361 rep.setValue("state", mymedia.m_state);
\r
362 rep.setValue("volume", mymedia.m_volume);
\r
364 // Invoke resource's post API with rep, query map and the callback parameter
\r
365 curResource->post(rep, QueryParamsMap(), std::bind(&WinUIClientApp::onPost, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
\r
369 void WinUIClientApp::onPost(const HeaderOptions& /*headerOptions*/,
\r
370 const OCRepresentation& rep, const int eCode)
\r
374 if (OC_STACK_OK == eCode || (OC_STACK_RESOURCE_CREATED == eCode
\r
375 || OC_STACK_RESOURCE_CHANGED == eCode))
\r
377 std::cout << "POST request was successful" << std::endl;
\r
379 if (rep.hasAttribute("createduri"))
\r
381 std::cout << "\tUri of the created resource: "
\r
382 << rep.getValue<std::string>("createduri") << std::endl;
\r
386 rep.getValue("state", mymedia.m_state);
\r
387 rep.getValue("volume", mymedia.m_volume);
\r
388 rep.getValue("name", mymedia.m_name);
\r
390 std::cout << "\tstate: " << mymedia.m_state << std::endl;
\r
391 std::cout << "\tvolume: " << mymedia.m_volume << std::endl;
\r
392 std::cout << "\tname: " << mymedia.m_name << std::endl;
\r
398 std::cout << "onPost Response error: " << eCode << std::endl;
\r
402 catch (std::exception& e)
\r
404 std::cout << "Exception: " << e.what() << " in onPost" << std::endl;
\r
408 void WinUIClientApp::onPost2(const HeaderOptions& /*headerOptions*/,
\r
409 const OCRepresentation& rep, const int eCode)
\r
413 if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_CREATED == eCode)
\r
415 std::cout << "POST request was successful" << std::endl;
\r
417 if (rep.hasAttribute("createduri"))
\r
419 std::cout << "\tUri of the created resource: "
\r
420 << rep.getValue<std::string>("createduri") << std::endl;
\r
424 rep.getValue("state", mymedia.m_state);
\r
425 rep.getValue("volume", mymedia.m_volume);
\r
426 rep.getValue("name", mymedia.m_name);
\r
428 std::cout << "\tstate: " << mymedia.m_state << std::endl;
\r
429 std::cout << "\tvolume: " << mymedia.m_volume << std::endl;
\r
430 std::cout << "\tname: " << mymedia.m_name << std::endl;
\r
436 std::cout << "onPost2 Response error: " << eCode << std::endl;
\r
440 catch(std::exception& e)
\r
442 std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;
\r
447 void WinUIClientApp::onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,
\r
448 const int& eCode, const int& sequenceNumber)
\r
452 if (OC_STACK_OK == eCode && sequenceNumber <= MAX_SEQUENCE_NUMBER)
\r
454 if (sequenceNumber == OC_OBSERVE_REGISTER)
\r
456 std::cout << "Observe registration action is successful" << std::endl;
\r
459 std::cout << "OBSERVE RESULT:"<<std::endl;
\r
460 std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;
\r
461 rep.getValue("state", mymedia.m_state);
\r
462 rep.getValue("volume", mymedia.m_volume);
\r
463 rep.getValue("name", mymedia.m_name);
\r
465 std::cout << "\tstate: " << mymedia.m_state << std::endl;
\r
466 std::cout << "\tvolume: " << mymedia.m_volume << std::endl;
\r
467 std::cout << "\tname: " << mymedia.m_name << std::endl;
\r
469 g_CurSliderVal = mymedia.m_volume;
\r
470 SendMessage(hwndVolumeSlider, TBM_SETPOS, TRUE, g_CurSliderVal);
\r
471 LabelPrintf(hwndVolumeExpectedLabel,"Volume: %i", g_CurSliderVal);
\r
475 if (OC_STACK_OK == eCode)
\r
477 std::cout << "No observe option header is returned in the response." << std::endl;
\r
478 std::cout << "For a registration request, it means the registration failed"
\r
483 std::cout << "onObserve Response error: " << eCode << std::endl;
\r
488 catch (std::exception& e)
\r
490 std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
\r
495 void WinUIClientApp::BeginObserving()
\r
497 if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)
\r
498 std::cout << std::endl << "Observe is used." << std::endl << std::endl;
\r
499 else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)
\r
500 std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;
\r
502 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
505 void WinUIClientApp::CancelObserving()
\r
507 std::cout<<"Cancelling Observe..."<<std::endl;
\r
508 OCStackResult result = curResource->cancelObserve();
\r
511 std::shared_ptr<OCResource> WinUIClientApp::GetResource()
\r
513 return curResource;
\r
516 int WinUIClientApp::observe_count()
\r