Modify CopyRequest() and add free code for ehRequest after confirm
[platform/upstream/iotivity.git] / resource / examples / mediaserver.cpp
1 /* ****************************************************************
2  *
3  * Copyright 2016 Intel Corporation All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ******************************************************************/
18
19 ///
20 /// This sample provides steps to define an interface for a resource
21 /// (properties and methods) and host this resource on the server.
22 ///
23
24 #include <functional>
25 #include <mutex>
26 #include "platform_features.h"
27 #include <condition_variable>
28
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31 #include "ocpayload.h"
32 #include <windows.h>
33 #include <objbase.h>
34
35 #include <commctrl.h>
36 #include <mmdeviceapi.h>
37 #include <endpointvolume.h>
38 #include <stdio.h>
39 #include <math.h>       /* log */
40
41 #define SAFE_RELEASE(x)  \
42               if ((x) != NULL)  \
43                 { (x)->Release(); (x) = NULL; }
44
45 using namespace OC;
46 using namespace std;
47 namespace PH = std::placeholders;
48
49 int gObservation = 0;
50 void * ChangeMediaRepresentation (void *param);
51 void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest);
52 void playPause(void);
53 void setVolume(int vol);
54 int getVolume(void);
55
56 // Specifies where to notify all observers or list of observers
57 // false: notifies all observers
58 // true: notifies list of observers
59 bool isListOfObservers = false;
60
61 // Specifies secure or non-secure
62 // false: non-secure resource
63 // true: secure resource
64 bool isSecure = false;
65
66 /// Specifies whether Entity handler is going to do slow response or not
67 bool isSlowResponse = false;
68
69
70 // Forward declaring the entityHandler
71
72 /// This class represents a single resource named 'MediaResource'. This resource has
73 /// two simple properties named 'state' and 'volume'
74
75 class MediaResource
76 {
77
78 public:
79     /// Access this property from a TB client
80     std::string m_name;
81     bool m_state;
82     int m_volume;
83     std::string m_mediaUri;
84     OCResourceHandle m_resourceHandle;
85     OCRepresentation m_mediaRep;
86     ObservationIds m_interestedObservers;
87
88 public:
89     /// Constructor
90     MediaResource()
91         :m_name("Media Player"), m_state(false), m_volume(0), m_mediaUri("/a/media"),
92                 m_resourceHandle(nullptr)
93     {
94         // Initialize representation
95         m_mediaRep.setUri(m_mediaUri);
96
97         m_mediaRep.setValue("state", m_state);
98         m_mediaRep.setValue("volume", m_volume);
99         m_mediaRep.setValue("name", m_name);
100     }
101
102     /* Note that this does not need to be a member function: for classes you do not have
103     access to, you can accomplish this with a free function: */
104
105     /// This function internally calls registerResource API.
106     void createResource()
107     {
108         //URI of the resource
109         std::string resourceURI = m_mediaUri;
110         //resource type name. In this case, it is media
111         std::string resourceTypeName = "core.media";
112         // resource interface.
113         std::string resourceInterface = DEFAULT_INTERFACE;
114
115         // OCResourceProperty is defined ocstack.h
116         uint8_t resourceProperty;
117         if(isSecure)
118         {
119             resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
120         }
121         else
122         {
123             resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
124         }
125         EntityHandler cb = std::bind(&MediaResource::entityHandler, this,PH::_1);
126
127         // This will internally create and register the resource.
128         OCStackResult result = OCPlatform::registerResource(
129                                     m_resourceHandle, resourceURI, resourceTypeName,
130                                     resourceInterface, cb, resourceProperty);
131
132         if (OC_STACK_OK != result)
133         {
134             cout << "Resource creation was unsuccessful\n";
135         }
136     }
137
138     OCResourceHandle getHandle()
139     {
140         return m_resourceHandle;
141     }
142
143     // Puts representation.
144     // Gets values from the representation and
145     // updates the internal state
146     void put(OCRepresentation& rep)
147     {
148         try
149         {
150             if (rep.getValue("state", m_state))
151             {
152                 cout << "\t\t\t\t" << "state: " << m_state << endl;
153                 if(m_state)
154                 {
155                     playPause();
156                 }
157             }
158             else
159             {
160                 cout << "\t\t\t\t" << "state not found in the representation" << endl;
161             }
162
163             if (rep.getValue("volume", m_volume))
164             {
165                 cout << "\t\t\t\t" << "volume: " << m_volume << endl;
166                 if((0 <= m_volume) && (m_volume <= 100))
167                 {
168                     setVolume(m_volume);
169                 }
170             }
171             else
172             {
173                 cout << "\t\t\t\t" << "volume not found in the representation" << endl;
174             }
175         }
176         catch (exception& e)
177         {
178             cout << e.what() << endl;
179         }
180
181     }
182
183     // Post representation.
184     // Post can create new resource or simply act like put.
185     // Gets values from the representation and
186     // updates the internal state
187     OCRepresentation post(OCRepresentation& rep)
188     {
189         put(rep);
190         return get();
191     }
192
193
194     // gets the updated representation.
195     // Updates the representation with latest internal state before
196     // sending out.
197     OCRepresentation get()
198     {
199         m_mediaRep.setValue("state", m_state);
200         m_mediaRep.setValue("volume", m_volume);
201
202         return m_mediaRep;
203     }
204
205     void addType(const std::string& type) const
206     {
207         OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
208         if (OC_STACK_OK != result)
209         {
210             cout << "Binding TypeName to Resource was unsuccessful\n";
211         }
212     }
213
214     void addInterface(const std::string& intf) const
215     {
216         OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, intf);
217         if (OC_STACK_OK != result)
218         {
219             cout << "Binding TypeName to Resource was unsuccessful\n";
220         }
221     }
222
223 private:
224 // This is just a sample implementation of entity handler.
225 // Entity handler can be implemented in several ways by the manufacturer
226 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
227 {
228     cout << "\tIn Server CPP entity handler:\n";
229     OCEntityHandlerResult ehResult = OC_EH_ERROR;
230     if(request)
231     {
232         // Get the request type and request flag
233         std::string requestType = request->getRequestType();
234         int requestFlag = request->getRequestHandlerFlag();
235
236         if(requestFlag & RequestHandlerFlag::RequestFlag)
237         {
238             cout << "\t\trequestFlag : Request\n";
239             auto pResponse = std::make_shared<OC::OCResourceResponse>();
240             pResponse->setRequestHandle(request->getRequestHandle());
241             pResponse->setResourceHandle(request->getResourceHandle());
242
243             // Check for query params (if any)
244             QueryParamsMap queries = request->getQueryParameters();
245
246             if (!queries.empty())
247             {
248                 cout << "\nQuery processing upto entityHandler" << std::endl;
249             }
250             for (auto it : queries)
251             {
252                 cout << "Query key: " << it.first << " value : " << it.second
253                         << std::endl;
254             }
255
256             // If the request type is GET
257             if(requestType == "GET")
258             {
259                 cout << "\t\t\trequestType : GET\n";
260                 if(isSlowResponse) // Slow response case
261                 {
262                     static int startedThread = 0;
263                     if(!startedThread)
264                     {
265                         std::thread t(handleSlowResponse, (void *)this, request);
266                         startedThread = 1;
267                         t.detach();
268                     }
269                     ehResult = OC_EH_SLOW;
270                 }
271                 else // normal response case.
272                 {
273                     pResponse->setErrorCode(200);
274                     pResponse->setResponseResult(OC_EH_OK);
275                     pResponse->setResourceRepresentation(get());
276                     if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
277                     {
278                         ehResult = OC_EH_OK;
279                     }
280                 }
281             }
282             else if(requestType == "PUT")
283             {
284                 cout << "\t\t\trequestType : PUT\n";
285                 OCRepresentation rep = request->getResourceRepresentation();
286
287                 // Do related operations related to PUT request
288                 // Update the mediaResource
289                 put(rep);
290                 pResponse->setErrorCode(200);
291                 pResponse->setResponseResult(OC_EH_OK);
292                 pResponse->setResourceRepresentation(get());
293                 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
294                 {
295                     ehResult = OC_EH_OK;
296                 }
297             }
298             else if(requestType == "POST")
299             {
300                 cout << "\t\t\trequestType : POST\n";
301
302                 OCRepresentation rep = request->getResourceRepresentation();
303
304                 // Do related operations related to POST request
305                 OCRepresentation rep_post = post(rep);
306                 pResponse->setResourceRepresentation(rep_post);
307                 pResponse->setErrorCode(200);
308                 if(rep_post.hasAttribute("createduri"))
309                 {
310                     pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
311                     pResponse->setNewResourceUri(rep_post.getValue<std::string>("createduri"));
312                 }
313                 else
314                 {
315                     pResponse->setResponseResult(OC_EH_OK);
316                 }
317
318                 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
319                 {
320                     ehResult = OC_EH_OK;
321                 }
322             }
323             else if(requestType == "DELETE")
324             {
325                 cout << "Delete request received" << endl;
326             }
327         }
328
329         if(requestFlag & RequestHandlerFlag::ObserverFlag)
330         {
331             ObservationInfo observationInfo = request->getObservationInfo();
332             if(ObserveAction::ObserveRegister == observationInfo.action)
333             {
334                 m_interestedObservers.push_back(observationInfo.obsId);
335             }
336             else if(ObserveAction::ObserveUnregister == observationInfo.action)
337             {
338                 m_interestedObservers.erase(std::remove(
339                                                             m_interestedObservers.begin(),
340                                                             m_interestedObservers.end(),
341                                                             observationInfo.obsId),
342                                                             m_interestedObservers.end());
343             }
344
345             cout << "\t\trequestFlag : Observer\n";
346             gObservation = 1;
347             static int startedThread = 0;
348
349             // Observation happens on a different thread in ChangeMediaRepresentation function.
350             // If we have not created the thread already, we will create one here.
351
352             if(!startedThread)
353             {
354                 std::thread t(ChangeMediaRepresentation, (void *)this);
355                 startedThread = 1;
356                 t.detach();
357             }
358
359             ehResult = OC_EH_OK;
360         }
361     }
362     else
363     {
364         cout << "Request invalid" << std::endl;
365     }
366
367     return ehResult;
368 }
369
370 };
371
372 // ChangeMediaRepresentaion is an observation function,
373 // which notifies any changes to the resource to stack
374 // via notifyObservers
375 void * ChangeMediaRepresentation (void *param)
376 {
377     int prevVolume = 0;
378     MediaResource* mediaPtr = (MediaResource*) param;
379
380     // This function continuously monitors for the changes
381     while (1)
382     {
383         Sleep(100);
384
385         if (gObservation)
386         {
387             prevVolume = mediaPtr->m_volume;
388             mediaPtr->m_volume = getVolume();
389             if (prevVolume == mediaPtr->m_volume)
390                 continue;
391
392             cout << "Volume changed from " << prevVolume << "% to " << mediaPtr->m_volume << "%\n";
393
394             // If under observation if there are any changes to the media resource
395             // we call notifyObservors
396             //
397             // For demostration we are changing the volume value and notifying.
398
399             cout << "\nVolume updated to : " << mediaPtr->m_volume << endl;
400             cout << "Notifying observers with resource handle: " << mediaPtr->getHandle() << endl;
401
402             OCStackResult result = OC_STACK_OK;
403
404             if(isListOfObservers)
405             {
406                 std::shared_ptr<OCResourceResponse> resourceResponse =
407                             {std::make_shared<OCResourceResponse>()};
408
409                 resourceResponse->setErrorCode(200);
410                 resourceResponse->setResourceRepresentation(mediaPtr->get(), DEFAULT_INTERFACE);
411
412                 result = OCPlatform::notifyListOfObservers(  mediaPtr->getHandle(),
413                                                              mediaPtr->m_interestedObservers,
414                                                              resourceResponse);
415             }
416             else
417             {
418                 result = OCPlatform::notifyAllObservers(mediaPtr->getHandle());
419             }
420
421             if(OC_STACK_NO_OBSERVERS == result)
422             {
423                 cout << "No More observers, stopping notifications" << endl;
424                 gObservation = 0;
425             }
426         }
427     }
428
429     return NULL;
430 }
431
432 void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest)
433 {
434     // This function handles slow response case
435     MediaResource* mediaPtr = (MediaResource*) param;
436     // Induce a case for slow response by using sleep
437     cout << "SLOW response" << std::endl;
438     sleep (10);
439
440     auto pResponse = std::make_shared<OC::OCResourceResponse>();
441     pResponse->setRequestHandle(pRequest->getRequestHandle());
442     pResponse->setResourceHandle(pRequest->getResourceHandle());
443     pResponse->setResourceRepresentation(mediaPtr->get());
444     pResponse->setErrorCode(200);
445     pResponse->setResponseResult(OC_EH_OK);
446
447     // Set the slow response flag back to false
448     isSlowResponse = false;
449     OCPlatform::sendResponse(pResponse);
450     return NULL;
451 }
452
453 void PrintUsage()
454 {
455     cout << std::endl;
456     cout << "Usage : mediaserver <value>\n";
457     cout << "    Default - Non-secure resource and notify all observers\n";
458     cout << "    1 - Non-secure resource and notify list of observers\n\n";
459     cout << "    2 - Secure resource and notify all observers\n";
460     cout << "    3 - Secure resource and notify list of observers\n\n";
461     cout << "    4 - Non-secure resource, GET slow response, notify all observers\n";
462 }
463
464 static FILE* client_open(const char* /*path*/, const char *mode)
465 {
466     return fopen("./oic_svr_db_server.json", mode);
467 }
468
469 void playPause()
470 {
471     INPUT ip;
472
473     // Set up a generic keyboard event.
474     ip.type = INPUT_KEYBOARD;
475     ip.ki.wScan = 0; // hardware scan code for key
476     ip.ki.time = 0;
477     ip.ki.dwExtraInfo = 0;
478     ip.ki.wVk = VK_MEDIA_PLAY_PAUSE; // virtual-key code for the "a" key
479     ip.ki.dwFlags = 0; // 0 for key press
480
481     SendInput(1, &ip, sizeof(INPUT));
482     // Release the "Play/Pause" key
483     ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
484     SendInput(1, &ip, sizeof(INPUT));
485 }
486
487 int getVolume()
488 {
489     IAudioEndpointVolume *g_pEndptVol = NULL;
490     HRESULT hr = S_OK;
491     IMMDeviceEnumerator *pEnumerator = NULL;
492     IMMDevice *pDevice = NULL;
493     OSVERSIONINFO VersionInfo;
494
495     ZeroMemory(&VersionInfo, sizeof(OSVERSIONINFO));
496     VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
497     GetVersionEx(&VersionInfo);
498     CoInitialize(NULL);
499
500     // Get enumerator for audio endpoint devices.
501     hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
502                           NULL, CLSCTX_INPROC_SERVER,
503                           __uuidof(IMMDeviceEnumerator),
504                           (void**)&pEnumerator);
505
506     // Get default audio-rendering device.
507     hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
508
509     hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),
510                            CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
511     float currentVal;
512     hr = g_pEndptVol->GetMasterVolumeLevelScalar(&currentVal);
513     fflush(stdout); // just in case
514
515     SAFE_RELEASE(pEnumerator)
516     SAFE_RELEASE(pDevice)
517     SAFE_RELEASE(g_pEndptVol)
518     CoUninitialize();
519     return ((int) round(100 * currentVal));
520
521 }
522
523 void setVolume(int vol)
524 {
525     IAudioEndpointVolume *g_pEndptVol = NULL;
526     HRESULT hr = S_OK;
527     IMMDeviceEnumerator *pEnumerator = NULL;
528     IMMDevice *pDevice = NULL;
529     OSVERSIONINFO VersionInfo;
530
531     ZeroMemory(&VersionInfo, sizeof(OSVERSIONINFO));
532     VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
533     GetVersionEx(&VersionInfo);
534     CoInitialize(NULL);
535
536     // Get enumerator for audio endpoint devices.
537     hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
538                           NULL, CLSCTX_INPROC_SERVER,
539                           __uuidof(IMMDeviceEnumerator),
540                           (void**)&pEnumerator);
541
542     // Get default audio-rendering device.
543     hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
544
545     hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),
546                            CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
547     float got = (float)vol/100.0; // needs to be within 1.0 to 0.0
548     hr = g_pEndptVol->SetMasterVolumeLevelScalar(got, NULL);
549     fflush(stdout); // just in case
550
551     SAFE_RELEASE(pEnumerator)
552     SAFE_RELEASE(pDevice)
553     SAFE_RELEASE(g_pEndptVol)
554     CoUninitialize();
555 }
556
557 int main(int argc, char* argv[])
558 {
559     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
560
561     if (argc == 1)
562     {
563         isListOfObservers = false;
564         isSecure = false;
565     }
566     else if (argc == 2)
567     {
568         int value = atoi(argv[1]);
569         switch (value)
570         {
571             case 1:
572                 isListOfObservers = true;
573                 isSecure = false;
574                 break;
575             case 2:
576                 isListOfObservers = false;
577                 isSecure = true;
578                 break;
579             case 3:
580                 isListOfObservers = true;
581                 isSecure = true;
582                 break;
583             case 4:
584                 isSlowResponse = true;
585                 break;
586             default:
587                 PrintUsage();
588                 break;
589        }
590      }
591     else
592     {
593         PrintUsage();
594         return -1;
595     }
596
597     // Create PlatformConfig object
598     PlatformConfig cfg {
599         OC::ServiceType::InProc,
600         OC::ModeType::Server,
601         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
602         0,         // Uses randomly available port
603         OC::QualityOfService::LowQos,
604         &ps
605     };
606
607     OCPlatform::Configure(cfg);
608     try
609     {
610         // Create the instance of the resource class
611         // (in this case instance of class 'MediaResource').
612         MediaResource myMedia;
613
614         // Invoke createResource function of class media.
615         myMedia.createResource();
616         cout << "Created resource." << std::endl;
617
618         // A condition variable will free the mutex it is given, then do a non-
619         // intensive block until 'notify' is called on it.  In this case, since we
620         // don't ever call cv.notify, this should be a non-processor intensive version
621         // of while(true);
622         std::mutex blocker;
623         std::condition_variable cv;
624         std::unique_lock<std::mutex> lock(blocker);
625         cout <<"Waiting" << std::endl;
626         cv.wait(lock, []{return false;});
627     }
628     catch(OCException &e)
629     {
630         cout << "OCException in main : " << e.what() << endl;
631     }
632
633     // No explicit call to stop the platform.
634     // When OCPlatform::destructor is invoked, internally we do platform cleanup
635
636     return 0;
637 }
638