[IOT-1538] Add support for Protocol-Independent ID
[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         OCStackResult result = OC_STACK_OK;
109
110         /* Resource Information */
111         std::string resourceURI = m_mediaUri;
112         std::string resourceTypeName = "core.media";
113         std::string resourceInterface = DEFAULT_INTERFACE;
114
115         /* Device Information */
116         result = SetDeviceInfo();
117         if (OC_STACK_OK != result)
118         {
119             cout << "Device information registration was unsuccessful\n";
120             return;
121         }
122
123         /* Platform Info */
124         char* platformId = "0A3E0D6F-DBF5-404E-8719-D6880042463A";
125         char* manufacturerName = "OCF";
126         char* manufacturerLink = "https://www.iotivity.org";
127         char* modelNumber = "895";
128         char* dateOfManufacture = "2016-01-15";
129         char* platformVersion = "1.0";
130         char* osVersion = "1.0";
131         char* hardwareVersion = "1.0";
132         char* firmwareVersion = "1.0";
133         char* supportLink = "https://www.iotivity.org";
134         OCPlatformInfo platformInfo = { platformId,
135                                         manufacturerName,
136                                         manufacturerLink,
137                                         modelNumber,
138                                         dateOfManufacture,
139                                         platformVersion,
140                                         osVersion,
141                                         hardwareVersion,
142                                         firmwareVersion,
143                                         supportLink,
144                                         nullptr
145                                       };
146
147         result = OCPlatform::registerPlatformInfo(platformInfo);
148         if (OC_STACK_OK != result)
149         {
150             cout << "Platform information registration was unsuccessful\n";
151             return;
152         }
153
154         // OCResourceProperty is defined ocstack.h
155         uint8_t resourceProperty;
156         if (isSecure)
157         {
158             resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
159         }
160         else
161         {
162             resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
163         }
164         EntityHandler cb = std::bind(&MediaResource::entityHandler, this, PH::_1);
165
166         // This will internally create and register the resource.
167         result = OCPlatform::registerResource(m_resourceHandle, resourceURI, resourceTypeName,
168                                               resourceInterface, cb, resourceProperty);
169         if (OC_STACK_OK != result)
170         {
171             cout << "Resource creation was unsuccessful\n";
172         }
173     }
174
175     OCResourceHandle getHandle()
176     {
177         return m_resourceHandle;
178     }
179
180     // Puts representation.
181     // Gets values from the representation and
182     // updates the internal state
183     void put(OCRepresentation& rep)
184     {
185         try
186         {
187             if (rep.getValue("state", m_state))
188             {
189                 cout << "\t\t\t\t" << "state: " << m_state << endl;
190                 if(m_state)
191                 {
192                     playPause();
193                 }
194             }
195             else
196             {
197                 cout << "\t\t\t\t" << "state not found in the representation" << endl;
198             }
199
200             if (rep.getValue("volume", m_volume))
201             {
202                 cout << "\t\t\t\t" << "volume: " << m_volume << endl;
203                 if((0 <= m_volume) && (m_volume <= 100))
204                 {
205                     setVolume(m_volume);
206                 }
207             }
208             else
209             {
210                 cout << "\t\t\t\t" << "volume not found in the representation" << endl;
211             }
212         }
213         catch (exception& e)
214         {
215             cout << e.what() << endl;
216         }
217
218     }
219
220     // Post representation.
221     // Post can create new resource or simply act like put.
222     // Gets values from the representation and
223     // updates the internal state
224     OCRepresentation post(OCRepresentation& rep)
225     {
226         put(rep);
227         return get();
228     }
229
230
231     // gets the updated representation.
232     // Updates the representation with latest internal state before
233     // sending out.
234     OCRepresentation get()
235     {
236         m_mediaRep.setValue("state", m_state);
237         m_mediaRep.setValue("volume", m_volume);
238
239         return m_mediaRep;
240     }
241
242     void addType(const std::string& type) const
243     {
244         OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
245         if (OC_STACK_OK != result)
246         {
247             cout << "Binding TypeName to Resource was unsuccessful\n";
248         }
249     }
250
251     void addInterface(const std::string& intf) const
252     {
253         OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, intf);
254         if (OC_STACK_OK != result)
255         {
256             cout << "Binding TypeName to Resource was unsuccessful\n";
257         }
258     }
259
260 private:
261 // This is just a sample implementation of entity handler.
262 // Entity handler can be implemented in several ways by the manufacturer
263 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
264 {
265     cout << "\tIn Server CPP entity handler:\n";
266     OCEntityHandlerResult ehResult = OC_EH_ERROR;
267     if(request)
268     {
269         // Get the request type and request flag
270         std::string requestType = request->getRequestType();
271         int requestFlag = request->getRequestHandlerFlag();
272
273         if(requestFlag & RequestHandlerFlag::RequestFlag)
274         {
275             cout << "\t\trequestFlag : Request\n";
276             auto pResponse = std::make_shared<OC::OCResourceResponse>();
277             pResponse->setRequestHandle(request->getRequestHandle());
278             pResponse->setResourceHandle(request->getResourceHandle());
279
280             // Check for query params (if any)
281             QueryParamsMap queries = request->getQueryParameters();
282
283             if (!queries.empty())
284             {
285                 cout << "\nQuery processing upto entityHandler" << std::endl;
286             }
287             for (auto it : queries)
288             {
289                 cout << "Query key: " << it.first << " value : " << it.second
290                         << std::endl;
291             }
292
293             // If the request type is GET
294             if(requestType == "GET")
295             {
296                 cout << "\t\t\trequestType : GET\n";
297                 if(isSlowResponse) // Slow response case
298                 {
299                     static int startedThread = 0;
300                     if(!startedThread)
301                     {
302                         std::thread t(handleSlowResponse, (void *)this, request);
303                         startedThread = 1;
304                         t.detach();
305                     }
306                     ehResult = OC_EH_SLOW;
307                 }
308                 else // normal response case.
309                 {
310
311                     pResponse->setResponseResult(OC_EH_OK);
312                     pResponse->setResourceRepresentation(get());
313                     if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
314                     {
315                         ehResult = OC_EH_OK;
316                     }
317                 }
318             }
319             else if(requestType == "PUT")
320             {
321                 cout << "\t\t\trequestType : PUT\n";
322                 OCRepresentation rep = request->getResourceRepresentation();
323
324                 // Do related operations related to PUT request
325                 // Update the mediaResource
326                 put(rep);
327
328                 pResponse->setResponseResult(OC_EH_OK);
329                 pResponse->setResourceRepresentation(get());
330                 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
331                 {
332                     ehResult = OC_EH_OK;
333                 }
334             }
335             else if(requestType == "POST")
336             {
337                 cout << "\t\t\trequestType : POST\n";
338
339                 OCRepresentation rep = request->getResourceRepresentation();
340
341                 // Do related operations related to POST request
342                 OCRepresentation rep_post = post(rep);
343                 pResponse->setResourceRepresentation(rep_post);
344
345                 if(rep_post.hasAttribute("createduri"))
346                 {
347                     pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
348                     pResponse->setNewResourceUri(rep_post.getValue<std::string>("createduri"));
349                 }
350                 else
351                 {
352                     pResponse->setResponseResult(OC_EH_OK);
353                 }
354
355                 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
356                 {
357                     ehResult = OC_EH_OK;
358                 }
359             }
360             else if(requestType == "DELETE")
361             {
362                 cout << "Delete request received" << endl;
363             }
364         }
365
366         if(requestFlag & RequestHandlerFlag::ObserverFlag)
367         {
368             ObservationInfo observationInfo = request->getObservationInfo();
369             if(ObserveAction::ObserveRegister == observationInfo.action)
370             {
371                 m_interestedObservers.push_back(observationInfo.obsId);
372             }
373             else if(ObserveAction::ObserveUnregister == observationInfo.action)
374             {
375                 m_interestedObservers.erase(std::remove(
376                                                             m_interestedObservers.begin(),
377                                                             m_interestedObservers.end(),
378                                                             observationInfo.obsId),
379                                                             m_interestedObservers.end());
380             }
381
382             cout << "\t\trequestFlag : Observer\n";
383             gObservation = 1;
384             static int startedThread = 0;
385
386             // Observation happens on a different thread in ChangeMediaRepresentation function.
387             // If we have not created the thread already, we will create one here.
388
389             if(!startedThread)
390             {
391                 std::thread t(ChangeMediaRepresentation, (void *)this);
392                 startedThread = 1;
393                 t.detach();
394             }
395
396             ehResult = OC_EH_OK;
397         }
398     }
399     else
400     {
401         cout << "Request invalid" << std::endl;
402     }
403
404     return ehResult;
405 }
406
407 OCStackResult SetDeviceInfo()
408 {
409     OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME,
410                                                         "IoTivity Media Server");
411     if (result != OC_STACK_OK)
412     {
413         cout << "Failed to set device name" << endl;
414         return result;
415     }
416
417     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, "core.1.1.0");
418     if (result != OC_STACK_OK)
419     {
420         cout << "Failed to set spec version" << endl;
421         return result;
422     }
423
424     result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID,
425                                           "9f9752ed-b4ab-4662-af22-7e541bbee2fb");
426     if (result != OC_STACK_OK)
427     {
428         cout << "Failed to set piid" << endl;
429         return result;
430     }
431
432     return OC_STACK_OK;
433 }
434
435 };
436
437 // ChangeMediaRepresentaion is an observation function,
438 // which notifies any changes to the resource to stack
439 // via notifyObservers
440 void * ChangeMediaRepresentation (void *param)
441 {
442     int prevVolume = 0;
443     MediaResource* mediaPtr = (MediaResource*) param;
444
445     // This function continuously monitors for the changes
446     while (1)
447     {
448         Sleep(100);
449
450         if (gObservation)
451         {
452             prevVolume = mediaPtr->m_volume;
453             mediaPtr->m_volume = getVolume();
454             if (prevVolume == mediaPtr->m_volume)
455                 continue;
456
457             cout << "Volume changed from " << prevVolume << "% to " << mediaPtr->m_volume << "%\n";
458
459             // If under observation if there are any changes to the media resource
460             // we call notifyObservors
461             //
462             // For demostration we are changing the volume value and notifying.
463
464             cout << "\nVolume updated to : " << mediaPtr->m_volume << endl;
465             cout << "Notifying observers with resource handle: " << mediaPtr->getHandle() << endl;
466
467             OCStackResult result = OC_STACK_OK;
468
469             if(isListOfObservers)
470             {
471                 std::shared_ptr<OCResourceResponse> resourceResponse =
472                             {std::make_shared<OCResourceResponse>()};
473
474                 resourceResponse->setResourceRepresentation(mediaPtr->get(), DEFAULT_INTERFACE);
475
476                 result = OCPlatform::notifyListOfObservers(  mediaPtr->getHandle(),
477                                                              mediaPtr->m_interestedObservers,
478                                                              resourceResponse);
479             }
480             else
481             {
482                 result = OCPlatform::notifyAllObservers(mediaPtr->getHandle());
483             }
484
485             if(OC_STACK_NO_OBSERVERS == result)
486             {
487                 cout << "No More observers, stopping notifications" << endl;
488                 gObservation = 0;
489             }
490         }
491     }
492
493     return NULL;
494 }
495
496 void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest)
497 {
498     // This function handles slow response case
499     MediaResource* mediaPtr = (MediaResource*) param;
500     // Induce a case for slow response by using sleep
501     cout << "SLOW response" << std::endl;
502     sleep (10);
503
504     auto pResponse = std::make_shared<OC::OCResourceResponse>();
505     pResponse->setRequestHandle(pRequest->getRequestHandle());
506     pResponse->setResourceHandle(pRequest->getResourceHandle());
507     pResponse->setResourceRepresentation(mediaPtr->get());
508
509     pResponse->setResponseResult(OC_EH_OK);
510
511     // Set the slow response flag back to false
512     isSlowResponse = false;
513     OCPlatform::sendResponse(pResponse);
514     return NULL;
515 }
516
517 void PrintUsage()
518 {
519     cout << std::endl;
520     cout << "Usage : mediaserver <value>\n";
521     cout << "    Default - Non-secure resource and notify all observers\n";
522     cout << "    1 - Non-secure resource and notify list of observers\n\n";
523     cout << "    2 - Secure resource and notify all observers\n";
524     cout << "    3 - Secure resource and notify list of observers\n\n";
525     cout << "    4 - Non-secure resource, GET slow response, notify all observers\n";
526 }
527
528 static FILE* client_open(const char* /*path*/, const char* mode)
529 {
530     return fopen("./oic_svr_db_server.dat", mode);
531 }
532
533 void playPause()
534 {
535     INPUT ip;
536
537     // Set up a generic keyboard event.
538     ip.type = INPUT_KEYBOARD;
539     ip.ki.wScan = 0; // hardware scan code for key
540     ip.ki.time = 0;
541     ip.ki.dwExtraInfo = 0;
542     ip.ki.wVk = VK_MEDIA_PLAY_PAUSE; // virtual-key code for the "a" key
543     ip.ki.dwFlags = 0; // 0 for key press
544
545     SendInput(1, &ip, sizeof(INPUT));
546     // Release the "Play/Pause" key
547     ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
548     SendInput(1, &ip, sizeof(INPUT));
549 }
550
551 int getVolume()
552 {
553     IAudioEndpointVolume *g_pEndptVol = NULL;
554     HRESULT hr = S_OK;
555     IMMDeviceEnumerator *pEnumerator = NULL;
556     IMMDevice *pDevice = NULL;
557     OSVERSIONINFO VersionInfo;
558
559     ZeroMemory(&VersionInfo, sizeof(OSVERSIONINFO));
560     VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
561     GetVersionEx(&VersionInfo);
562     CoInitialize(NULL);
563
564     // Get enumerator for audio endpoint devices.
565     hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
566                           NULL, CLSCTX_INPROC_SERVER,
567                           __uuidof(IMMDeviceEnumerator),
568                           (void**)&pEnumerator);
569
570     // Get default audio-rendering device.
571     hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
572
573     hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),
574                            CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
575     float currentVal;
576     hr = g_pEndptVol->GetMasterVolumeLevelScalar(&currentVal);
577     fflush(stdout); // just in case
578
579     SAFE_RELEASE(pEnumerator)
580     SAFE_RELEASE(pDevice)
581     SAFE_RELEASE(g_pEndptVol)
582     CoUninitialize();
583     return ((int) round(100 * currentVal));
584
585 }
586
587 void setVolume(int vol)
588 {
589     IAudioEndpointVolume *g_pEndptVol = NULL;
590     HRESULT hr = S_OK;
591     IMMDeviceEnumerator *pEnumerator = NULL;
592     IMMDevice *pDevice = NULL;
593     OSVERSIONINFO VersionInfo;
594
595     ZeroMemory(&VersionInfo, sizeof(OSVERSIONINFO));
596     VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
597     GetVersionEx(&VersionInfo);
598     CoInitialize(NULL);
599
600     // Get enumerator for audio endpoint devices.
601     hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
602                           NULL, CLSCTX_INPROC_SERVER,
603                           __uuidof(IMMDeviceEnumerator),
604                           (void**)&pEnumerator);
605
606     // Get default audio-rendering device.
607     hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
608
609     hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),
610                            CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
611     float got = (float)vol/100.0; // needs to be within 1.0 to 0.0
612     hr = g_pEndptVol->SetMasterVolumeLevelScalar(got, NULL);
613     fflush(stdout); // just in case
614
615     SAFE_RELEASE(pEnumerator)
616     SAFE_RELEASE(pDevice)
617     SAFE_RELEASE(g_pEndptVol)
618     CoUninitialize();
619 }
620
621 int main(int argc, char* argv[])
622 {
623     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
624
625     if (argc == 1)
626     {
627         isListOfObservers = false;
628         isSecure = false;
629     }
630     else if (argc == 2)
631     {
632         int value = atoi(argv[1]);
633         switch (value)
634         {
635             case 1:
636                 isListOfObservers = true;
637                 isSecure = false;
638                 break;
639             case 2:
640                 isListOfObservers = false;
641                 isSecure = true;
642                 break;
643             case 3:
644                 isListOfObservers = true;
645                 isSecure = true;
646                 break;
647             case 4:
648                 isSlowResponse = true;
649                 break;
650             default:
651                 PrintUsage();
652                 break;
653        }
654      }
655     else
656     {
657         PrintUsage();
658         return -1;
659     }
660
661     // Create PlatformConfig object
662     PlatformConfig cfg {
663         OC::ServiceType::InProc,
664         OC::ModeType::Server,
665         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
666         0,         // Uses randomly available port
667         OC::QualityOfService::LowQos,
668         &ps
669     };
670
671     OCPlatform::Configure(cfg);
672     try
673     {
674         // Create the instance of the resource class
675         // (in this case instance of class 'MediaResource').
676         MediaResource myMedia;
677
678         // Invoke createResource function of class media.
679         myMedia.createResource();
680         cout << "Created resource." << std::endl;
681
682         // A condition variable will free the mutex it is given, then do a non-
683         // intensive block until 'notify' is called on it.  In this case, since we
684         // don't ever call cv.notify, this should be a non-processor intensive version
685         // of while(true);
686         std::mutex blocker;
687         std::condition_variable cv;
688         std::unique_lock<std::mutex> lock(blocker);
689         cout <<"Waiting" << std::endl;
690         cv.wait(lock, []{return false;});
691     }
692     catch(OCException &e)
693     {
694         cout << "OCException in main : " << e.what() << endl;
695     }
696
697     // No explicit call to stop the platform.
698     // When OCPlatform::destructor is invoked, internally we do platform cleanup
699
700     return 0;
701 }
702