Modify CopyRequest() and add free code for ehRequest after confirm
[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 WinUIClientApp::FindResources()\r
86 {\r
87     std::ostringstream requestURI;\r
88     requestURI << OC_RSRVD_WELL_KNOWN_URI;// << "?rt=core.media";\r
89     OCPlatform::findResource("", requestURI.str(),\r
90                             CT_DEFAULT, std::bind(&WinUIClientApp::foundResource, this, std::placeholders::_1));\r
91 }\r
92 \r
93 \r
94 // Callback to found resources\r
95 void WinUIClientApp::foundResource(std::shared_ptr<OCResource> resource)\r
96 {\r
97     std::cout << "In foundResource\n";\r
98     std::string resourceURI;\r
99     std::string hostAddress;\r
100     try\r
101     {\r
102         {\r
103             std::lock_guard<std::mutex> lock(curResourceLock);\r
104             if (discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end())\r
105             {\r
106                 std::cout << "Found resource " << resource->uniqueIdentifier() <<\r
107                     " for the first time on server with ID: "<< resource->sid()<<std::endl;\r
108                 discoveredResources[resource->uniqueIdentifier()] = resource;\r
109             }\r
110             else\r
111             {\r
112                 std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl;\r
113             }\r
114 \r
115             if (curResource)\r
116             {\r
117                 std::cout << "Found another resource, ignoring"<<std::endl;\r
118                 return;\r
119             }\r
120         }\r
121 \r
122         // Do some operations with resource object.\r
123         if (resource)\r
124         {\r
125             std::cout<<"DISCOVERED Resource:"<<std::endl;\r
126             // Get the resource URI\r
127             resourceURI = resource->uri();\r
128             std::cout << "\tURI of the resource: " << resourceURI << std::endl;\r
129 \r
130             // Get the resource host address\r
131             hostAddress = resource->host();\r
132             std::cout << "\tHost address of the resource: " << hostAddress << std::endl;\r
133 \r
134             // Get the resource types\r
135             std::cout << "\tList of resource types: " << std::endl;\r
136             for(auto &resourceTypes : resource->getResourceTypes())\r
137             {\r
138                 std::cout << "\t\t" << resourceTypes << std::endl;\r
139             }\r
140 \r
141             // Get the resource interfaces\r
142             std::cout << "\tList of resource interfaces: " << std::endl;\r
143             for(auto &resourceInterfaces : resource->getResourceInterfaces())\r
144             {\r
145                 std::cout << "\t\t" << resourceInterfaces << std::endl;\r
146             }\r
147 \r
148             if (resourceURI == "/a/media")\r
149             {\r
150                 curResource = resource;\r
151                 // Call a local function which will internally invoke get API on the resource pointer\r
152                 this->GetMediaRepresentation();\r
153                 this->BeginObserving();\r
154             }\r
155         }\r
156         else\r
157         {\r
158             // Resource is invalid\r
159             std::cout << "Resource is invalid" << std::endl;\r
160         }\r
161 \r
162     }\r
163     catch(std::exception& e)\r
164     {\r
165         std::cerr << "Exception in foundResource: "<< e.what() << std::endl;\r
166     }\r
167 }\r
168 \r
169 // Local function to get representation of media resource\r
170 void WinUIClientApp::GetMediaRepresentation()\r
171 {\r
172     if (curResource)\r
173     {\r
174         std::cout << "Getting Media Representation..."<<std::endl;\r
175         // Invoke resource's get API with the callback parameter\r
176 \r
177         QueryParamsMap test;\r
178         curResource->get(test, std::bind(&WinUIClientApp::onGet, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));\r
179 \r
180     }\r
181     else\r
182     {\r
183         std::cout << "No Current Resource to GetMediaRepresentation..."<<std::endl;\r
184     }\r
185 }\r
186 \r
187 // Callback handler on GET request\r
188 void WinUIClientApp::onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)\r
189 {\r
190     try\r
191     {\r
192         if (OC_STACK_OK == eCode)\r
193         {\r
194             std::cout << "GET request was successful" << std::endl;\r
195             std::cout << "Resource URI: " << rep.getUri() << std::endl;\r
196 \r
197             rep.getValue("state", mymedia.m_state);\r
198             rep.getValue("volume", mymedia.m_volume);\r
199             rep.getValue("name", mymedia.m_name);\r
200 \r
201             std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
202             std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
203             std::cout << "\tname: " << mymedia.m_name << std::endl;\r
204 \r
205             g_CurSliderVal = mymedia.m_volume;\r
206             SendMessage(hwndVolumeSlider, TBM_SETPOS, TRUE, g_CurSliderVal);\r
207             LabelPrintf(hwndVolumeExpectedLabel,"Volume: %i", g_CurSliderVal);\r
208 \r
209         }\r
210         else\r
211         {\r
212             std::cout << "onGET Response error: " << eCode << std::endl;\r
213             std::exit(-1);\r
214         }\r
215     }\r
216     catch(std::exception& e)\r
217     {\r
218         std::cout << "Exception: " << e.what() << " in onGet" << std::endl;\r
219     }\r
220 }\r
221 \r
222 // Local function to put a different state for this resource\r
223 void WinUIClientApp::PutMediaRepresentation()\r
224 {\r
225     if (curResource)\r
226     {\r
227         OCRepresentation rep;\r
228 \r
229         std::cout << "Putting media representation..."<<std::endl;\r
230 \r
231         rep.setValue("state", mymedia.m_state);\r
232         rep.setValue("volume", mymedia.m_volume);\r
233 \r
234         // Invoke resource's put API with rep, query map and the callback parameter\r
235         curResource->put(rep, QueryParamsMap(), std::bind(&WinUIClientApp::onPut, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));\r
236     }\r
237 }\r
238 \r
239 // callback handler on PUT request\r
240 void WinUIClientApp::onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode)\r
241 {\r
242     try\r
243     {\r
244         if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_CHANGED == eCode)\r
245         {\r
246             std::cout << "PUT request was successful" << std::endl;\r
247 \r
248             rep.getValue("state", mymedia.m_state);\r
249             rep.getValue("volume", mymedia.m_volume);\r
250             rep.getValue("name", mymedia.m_name);\r
251 \r
252             std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
253             std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
254             std::cout << "\tname: " << mymedia.m_name << std::endl;\r
255 \r
256         }\r
257         else\r
258         {\r
259             std::cout << "onPut Response error: " << eCode << std::endl;\r
260             std::exit(-1);\r
261         }\r
262     }\r
263     catch(std::exception& e)\r
264     {\r
265         std::cout << "Exception: " << e.what() << " in onPut" << std::endl;\r
266     }\r
267 }\r
268 \r
269 // Local function to put a different state for this resource\r
270 void WinUIClientApp::PostMediaRepresentation()\r
271 {\r
272     if (curResource)\r
273     {\r
274         OCRepresentation rep;\r
275 \r
276         std::cout << "Posting media representation..."<<std::endl;\r
277 \r
278         rep.setValue("state", mymedia.m_state);\r
279         rep.setValue("volume", mymedia.m_volume);\r
280 \r
281         // Invoke resource's post API with rep, query map and the callback parameter\r
282         curResource->post(rep, QueryParamsMap(), std::bind(&WinUIClientApp::onPost, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));\r
283     }\r
284 }\r
285 \r
286 void WinUIClientApp::onPost(const HeaderOptions& /*headerOptions*/,\r
287         const OCRepresentation& rep, const int eCode)\r
288 {\r
289     try\r
290     {\r
291         if (OC_STACK_OK == eCode || (OC_STACK_RESOURCE_CREATED == eCode\r
292                 || OC_STACK_RESOURCE_CHANGED == eCode))\r
293         {\r
294             std::cout << "POST request was successful" << std::endl;\r
295 \r
296             if (rep.hasAttribute("createduri"))\r
297             {\r
298                 std::cout << "\tUri of the created resource: "\r
299                     << rep.getValue<std::string>("createduri") << std::endl;\r
300             }\r
301             else\r
302             {\r
303                 rep.getValue("state", mymedia.m_state);\r
304                 rep.getValue("volume", mymedia.m_volume);\r
305                 rep.getValue("name", mymedia.m_name);\r
306 \r
307                 std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
308                 std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
309                 std::cout << "\tname: " << mymedia.m_name << std::endl;\r
310             }\r
311 \r
312         }\r
313         else\r
314         {\r
315             std::cout << "onPost Response error: " << eCode << std::endl;\r
316             std::exit(-1);\r
317         }\r
318     }\r
319     catch (std::exception& e)\r
320     {\r
321         std::cout << "Exception: " << e.what() << " in onPost" << std::endl;\r
322     }\r
323 }\r
324 \r
325 void WinUIClientApp::onPost2(const HeaderOptions& /*headerOptions*/,\r
326         const OCRepresentation& rep, const int eCode)\r
327 {\r
328     try\r
329     {\r
330         if (OC_STACK_OK == eCode || OC_STACK_RESOURCE_CREATED == eCode)\r
331         {\r
332             std::cout << "POST request was successful" << std::endl;\r
333 \r
334             if (rep.hasAttribute("createduri"))\r
335             {\r
336                 std::cout << "\tUri of the created resource: "\r
337                     << rep.getValue<std::string>("createduri") << std::endl;\r
338             }\r
339             else\r
340             {\r
341                 rep.getValue("state", mymedia.m_state);\r
342                 rep.getValue("volume", mymedia.m_volume);\r
343                 rep.getValue("name", mymedia.m_name);\r
344 \r
345                 std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
346                 std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
347                 std::cout << "\tname: " << mymedia.m_name << std::endl;\r
348             }\r
349 \r
350         }\r
351         else\r
352         {\r
353             std::cout << "onPost2 Response error: " << eCode << std::endl;\r
354             std::exit(-1);\r
355         }\r
356     }\r
357     catch(std::exception& e)\r
358     {\r
359         std::cout << "Exception: " << e.what() << " in onPost2" << std::endl;\r
360     }\r
361 \r
362 }\r
363 \r
364 void WinUIClientApp::onObserve(const HeaderOptions /*headerOptions*/, const OCRepresentation& rep,\r
365                     const int& eCode, const int& sequenceNumber)\r
366 {\r
367     try\r
368     {\r
369         if (OC_STACK_OK == eCode && sequenceNumber <= MAX_SEQUENCE_NUMBER)\r
370         {\r
371             if (sequenceNumber == OC_OBSERVE_REGISTER)\r
372             {\r
373                 std::cout << "Observe registration action is successful" << std::endl;\r
374             }\r
375 \r
376             std::cout << "OBSERVE RESULT:"<<std::endl;\r
377             std::cout << "\tSequenceNumber: "<< sequenceNumber << std::endl;\r
378             rep.getValue("state", mymedia.m_state);\r
379             rep.getValue("volume", mymedia.m_volume);\r
380             rep.getValue("name", mymedia.m_name);\r
381 \r
382             std::cout << "\tstate: " << mymedia.m_state << std::endl;\r
383             std::cout << "\tvolume: " << mymedia.m_volume << std::endl;\r
384             std::cout << "\tname: " << mymedia.m_name << std::endl;\r
385 \r
386             g_CurSliderVal = mymedia.m_volume;\r
387             SendMessage(hwndVolumeSlider, TBM_SETPOS, TRUE, g_CurSliderVal);\r
388             LabelPrintf(hwndVolumeExpectedLabel,"Volume: %i", g_CurSliderVal);\r
389         }\r
390         else\r
391         {\r
392             if (OC_STACK_OK == eCode)\r
393             {\r
394                 std::cout << "No observe option header is returned in the response." << std::endl;\r
395                 std::cout << "For a registration request, it means the registration failed"\r
396                         << std::endl;\r
397             }\r
398             else\r
399             {\r
400                 std::cout << "onObserve Response error: " << eCode << std::endl;\r
401                 std::exit(-1);\r
402             }\r
403         }\r
404     }\r
405     catch (std::exception& e)\r
406     {\r
407         std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;\r
408     }\r
409 \r
410 }\r
411 \r
412 void WinUIClientApp::BeginObserving()\r
413 {\r
414     if (OBSERVE_TYPE_TO_USE == ObserveType::Observe)\r
415         std::cout << std::endl << "Observe is used." << std::endl << std::endl;\r
416     else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll)\r
417         std::cout << std::endl << "ObserveAll is used." << std::endl << std::endl;\r
418 \r
419     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
420 }\r
421 \r
422 void WinUIClientApp::CancelObserving()\r
423 {\r
424     std::cout<<"Cancelling Observe..."<<std::endl;\r
425     OCStackResult result = curResource->cancelObserve();\r
426 }\r
427 \r
428 std::shared_ptr<OCResource> WinUIClientApp::GetResource()\r
429 {\r
430     return curResource;\r
431 }\r
432 \r
433 int WinUIClientApp::observe_count()\r
434 {\r
435     static int oc = 0;\r
436     return ++oc;\r
437 }\r
438 \r