1 /* ****************************************************************
3 * Copyright 2016 Intel Corporation All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************/
20 /// This sample provides steps to define an interface for a resource
21 /// (properties and methods) and host this resource on the server.
26 #include "platform_features.h"
27 #include <condition_variable>
29 #include "OCPlatform.h"
35 #include <mmdeviceapi.h>
36 #include <endpointvolume.h>
38 #include <math.h> /* log */
40 #define SAFE_RELEASE(x) \
42 { (x)->Release(); (x) = NULL; }
46 namespace PH = std::placeholders;
49 void * ChangeMediaRepresentation (void *param);
50 void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest);
52 void setVolume(int vol);
55 // Specifies where to notify all observers or list of observers
56 // false: notifies all observers
57 // true: notifies list of observers
58 bool isListOfObservers = false;
60 // Specifies secure or non-secure
61 // false: non-secure resource
62 // true: secure resource
63 bool isSecure = false;
65 /// Specifies whether Entity handler is going to do slow response or not
66 bool isSlowResponse = false;
69 // Forward declaring the entityHandler
71 /// This class represents a single resource named 'MediaResource'. This resource has
72 /// two simple properties named 'state' and 'volume'
78 /// Access this property from a TB client
82 std::string m_mediaUri;
83 OCResourceHandle m_resourceHandle;
84 OCRepresentation m_mediaRep;
85 ObservationIds m_interestedObservers;
90 :m_name("Media Player"), m_state(false), m_volume(0), m_mediaUri("/a/media"),
91 m_resourceHandle(nullptr)
93 // Initialize representation
94 m_mediaRep.setUri(m_mediaUri);
96 m_mediaRep.setValue("state", m_state);
97 m_mediaRep.setValue("volume", m_volume);
98 m_mediaRep.setValue("name", m_name);
101 /* Note that this does not need to be a member function: for classes you do not have
102 access to, you can accomplish this with a free function: */
104 /// This function internally calls registerResource API.
105 void createResource()
107 //URI of the resource
108 std::string resourceURI = m_mediaUri;
109 //resource type name. In this case, it is media
110 std::string resourceTypeName = "core.media";
111 // resource interface.
112 std::string resourceInterface = DEFAULT_INTERFACE;
114 // OCResourceProperty is defined ocstack.h
115 uint8_t resourceProperty;
118 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE;
122 resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
124 EntityHandler cb = std::bind(&MediaResource::entityHandler, this,PH::_1);
126 // This will internally create and register the resource.
127 OCStackResult result = OCPlatform::registerResource(
128 m_resourceHandle, resourceURI, resourceTypeName,
129 resourceInterface, cb, resourceProperty);
131 if (OC_STACK_OK != result)
133 cout << "Resource creation was unsuccessful\n";
137 OCResourceHandle getHandle()
139 return m_resourceHandle;
142 // Puts representation.
143 // Gets values from the representation and
144 // updates the internal state
145 void put(OCRepresentation& rep)
149 if (rep.getValue("state", m_state))
151 cout << "\t\t\t\t" << "state: " << m_state << endl;
159 cout << "\t\t\t\t" << "state not found in the representation" << endl;
162 if (rep.getValue("volume", m_volume))
164 cout << "\t\t\t\t" << "volume: " << m_volume << endl;
165 if((0 <= m_volume) && (m_volume <= 100))
172 cout << "\t\t\t\t" << "volume not found in the representation" << endl;
177 cout << e.what() << endl;
182 // Post representation.
183 // Post can create new resource or simply act like put.
184 // Gets values from the representation and
185 // updates the internal state
186 OCRepresentation post(OCRepresentation& rep)
193 // gets the updated representation.
194 // Updates the representation with latest internal state before
196 OCRepresentation get()
198 m_mediaRep.setValue("state", m_state);
199 m_mediaRep.setValue("volume", m_volume);
204 void addType(const std::string& type) const
206 OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
207 if (OC_STACK_OK != result)
209 cout << "Binding TypeName to Resource was unsuccessful\n";
213 void addInterface(const std::string& intf) const
215 OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, intf);
216 if (OC_STACK_OK != result)
218 cout << "Binding TypeName to Resource was unsuccessful\n";
223 // This is just a sample implementation of entity handler.
224 // Entity handler can be implemented in several ways by the manufacturer
225 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
227 cout << "\tIn Server CPP entity handler:\n";
228 OCEntityHandlerResult ehResult = OC_EH_ERROR;
231 // Get the request type and request flag
232 std::string requestType = request->getRequestType();
233 int requestFlag = request->getRequestHandlerFlag();
235 if(requestFlag & RequestHandlerFlag::RequestFlag)
237 cout << "\t\trequestFlag : Request\n";
238 auto pResponse = std::make_shared<OC::OCResourceResponse>();
239 pResponse->setRequestHandle(request->getRequestHandle());
240 pResponse->setResourceHandle(request->getResourceHandle());
242 // Check for query params (if any)
243 QueryParamsMap queries = request->getQueryParameters();
245 if (!queries.empty())
247 cout << "\nQuery processing upto entityHandler" << std::endl;
249 for (auto it : queries)
251 cout << "Query key: " << it.first << " value : " << it.second
255 // If the request type is GET
256 if(requestType == "GET")
258 cout << "\t\t\trequestType : GET\n";
259 if(isSlowResponse) // Slow response case
261 static int startedThread = 0;
264 std::thread t(handleSlowResponse, (void *)this, request);
268 ehResult = OC_EH_SLOW;
270 else // normal response case.
272 pResponse->setErrorCode(200);
273 pResponse->setResponseResult(OC_EH_OK);
274 pResponse->setResourceRepresentation(get());
275 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
281 else if(requestType == "PUT")
283 cout << "\t\t\trequestType : PUT\n";
284 OCRepresentation rep = request->getResourceRepresentation();
286 // Do related operations related to PUT request
287 // Update the mediaResource
289 pResponse->setErrorCode(200);
290 pResponse->setResponseResult(OC_EH_OK);
291 pResponse->setResourceRepresentation(get());
292 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
297 else if(requestType == "POST")
299 cout << "\t\t\trequestType : POST\n";
301 OCRepresentation rep = request->getResourceRepresentation();
303 // Do related operations related to POST request
304 OCRepresentation rep_post = post(rep);
305 pResponse->setResourceRepresentation(rep_post);
306 pResponse->setErrorCode(200);
307 if(rep_post.hasAttribute("createduri"))
309 pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
310 pResponse->setNewResourceUri(rep_post.getValue<std::string>("createduri"));
314 pResponse->setResponseResult(OC_EH_OK);
317 if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
322 else if(requestType == "DELETE")
324 cout << "Delete request received" << endl;
328 if(requestFlag & RequestHandlerFlag::ObserverFlag)
330 ObservationInfo observationInfo = request->getObservationInfo();
331 if(ObserveAction::ObserveRegister == observationInfo.action)
333 m_interestedObservers.push_back(observationInfo.obsId);
335 else if(ObserveAction::ObserveUnregister == observationInfo.action)
337 m_interestedObservers.erase(std::remove(
338 m_interestedObservers.begin(),
339 m_interestedObservers.end(),
340 observationInfo.obsId),
341 m_interestedObservers.end());
344 cout << "\t\trequestFlag : Observer\n";
346 static int startedThread = 0;
348 // Observation happens on a different thread in ChangeMediaRepresentation function.
349 // If we have not created the thread already, we will create one here.
353 std::thread t(ChangeMediaRepresentation, (void *)this);
363 cout << "Request invalid" << std::endl;
371 // ChangeMediaRepresentaion is an observation function,
372 // which notifies any changes to the resource to stack
373 // via notifyObservers
374 void * ChangeMediaRepresentation (void *param)
377 MediaResource* mediaPtr = (MediaResource*) param;
379 // This function continuously monitors for the changes
386 prevVolume = mediaPtr->m_volume;
387 mediaPtr->m_volume = getVolume();
388 if (prevVolume == mediaPtr->m_volume)
391 cout << "Volume changed from " << prevVolume << "% to " << mediaPtr->m_volume << "%\n";
393 // If under observation if there are any changes to the media resource
394 // we call notifyObservors
396 // For demostration we are changing the volume value and notifying.
398 cout << "\nVolume updated to : " << mediaPtr->m_volume << endl;
399 cout << "Notifying observers with resource handle: " << mediaPtr->getHandle() << endl;
401 OCStackResult result = OC_STACK_OK;
403 if(isListOfObservers)
405 std::shared_ptr<OCResourceResponse> resourceResponse =
406 {std::make_shared<OCResourceResponse>()};
408 resourceResponse->setErrorCode(200);
409 resourceResponse->setResourceRepresentation(mediaPtr->get(), DEFAULT_INTERFACE);
411 result = OCPlatform::notifyListOfObservers( mediaPtr->getHandle(),
412 mediaPtr->m_interestedObservers,
417 result = OCPlatform::notifyAllObservers(mediaPtr->getHandle());
420 if(OC_STACK_NO_OBSERVERS == result)
422 cout << "No More observers, stopping notifications" << endl;
431 void * handleSlowResponse (void *param, std::shared_ptr<OCResourceRequest> pRequest)
433 // This function handles slow response case
434 MediaResource* mediaPtr = (MediaResource*) param;
435 // Induce a case for slow response by using sleep
436 cout << "SLOW response" << std::endl;
439 auto pResponse = std::make_shared<OC::OCResourceResponse>();
440 pResponse->setRequestHandle(pRequest->getRequestHandle());
441 pResponse->setResourceHandle(pRequest->getResourceHandle());
442 pResponse->setResourceRepresentation(mediaPtr->get());
443 pResponse->setErrorCode(200);
444 pResponse->setResponseResult(OC_EH_OK);
446 // Set the slow response flag back to false
447 isSlowResponse = false;
448 OCPlatform::sendResponse(pResponse);
455 cout << "Usage : mediaserver <value>\n";
456 cout << " Default - Non-secure resource and notify all observers\n";
457 cout << " 1 - Non-secure resource and notify list of observers\n\n";
458 cout << " 2 - Secure resource and notify all observers\n";
459 cout << " 3 - Secure resource and notify list of observers\n\n";
460 cout << " 4 - Non-secure resource, GET slow response, notify all observers\n";
463 static FILE* client_open(const char* /*path*/, const char *mode)
465 return fopen("./oic_svr_db_server.json", mode);
472 // Set up a generic keyboard event.
473 ip.type = INPUT_KEYBOARD;
474 ip.ki.wScan = 0; // hardware scan code for key
476 ip.ki.dwExtraInfo = 0;
477 ip.ki.wVk = VK_MEDIA_PLAY_PAUSE; // virtual-key code for the "a" key
478 ip.ki.dwFlags = 0; // 0 for key press
480 SendInput(1, &ip, sizeof(INPUT));
481 // Release the "Play/Pause" key
482 ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
483 SendInput(1, &ip, sizeof(INPUT));
488 IAudioEndpointVolume *g_pEndptVol = NULL;
490 IMMDeviceEnumerator *pEnumerator = NULL;
491 IMMDevice *pDevice = NULL;
492 OSVERSIONINFO VersionInfo;
494 ZeroMemory(&VersionInfo, sizeof(OSVERSIONINFO));
495 VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
496 GetVersionEx(&VersionInfo);
499 // Get enumerator for audio endpoint devices.
500 hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
501 NULL, CLSCTX_INPROC_SERVER,
502 __uuidof(IMMDeviceEnumerator),
503 (void**)&pEnumerator);
505 // Get default audio-rendering device.
506 hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
508 hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),
509 CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
511 hr = g_pEndptVol->GetMasterVolumeLevelScalar(¤tVal);
512 fflush(stdout); // just in case
514 SAFE_RELEASE(pEnumerator)
515 SAFE_RELEASE(pDevice)
516 SAFE_RELEASE(g_pEndptVol)
518 return ((int) round(100 * currentVal));
522 void setVolume(int vol)
524 IAudioEndpointVolume *g_pEndptVol = NULL;
526 IMMDeviceEnumerator *pEnumerator = NULL;
527 IMMDevice *pDevice = NULL;
528 OSVERSIONINFO VersionInfo;
530 ZeroMemory(&VersionInfo, sizeof(OSVERSIONINFO));
531 VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
532 GetVersionEx(&VersionInfo);
535 // Get enumerator for audio endpoint devices.
536 hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
537 NULL, CLSCTX_INPROC_SERVER,
538 __uuidof(IMMDeviceEnumerator),
539 (void**)&pEnumerator);
541 // Get default audio-rendering device.
542 hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
544 hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),
545 CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
546 float got = (float)vol/100.0; // needs to be within 1.0 to 0.0
547 hr = g_pEndptVol->SetMasterVolumeLevelScalar(got, NULL);
548 fflush(stdout); // just in case
550 SAFE_RELEASE(pEnumerator)
551 SAFE_RELEASE(pDevice)
552 SAFE_RELEASE(g_pEndptVol)
556 int main(int argc, char* argv[])
558 OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
562 isListOfObservers = false;
567 int value = atoi(argv[1]);
571 isListOfObservers = true;
575 isListOfObservers = false;
579 isListOfObservers = true;
583 isSlowResponse = true;
596 // Create PlatformConfig object
598 OC::ServiceType::InProc,
599 OC::ModeType::Server,
600 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
601 0, // Uses randomly available port
602 OC::QualityOfService::LowQos,
606 OCPlatform::Configure(cfg);
609 // Create the instance of the resource class
610 // (in this case instance of class 'MediaResource').
611 MediaResource myMedia;
613 // Invoke createResource function of class media.
614 myMedia.createResource();
615 cout << "Created resource." << std::endl;
617 // A condition variable will free the mutex it is given, then do a non-
618 // intensive block until 'notify' is called on it. In this case, since we
619 // don't ever call cv.notify, this should be a non-processor intensive version
622 std::condition_variable cv;
623 std::unique_lock<std::mutex> lock(blocker);
624 cout <<"Waiting" << std::endl;
625 cv.wait(lock, []{return false;});
627 catch(OCException &e)
629 cout << "OCException in main : " << e.what() << endl;
632 // No explicit call to stop the platform.
633 // When OCPlatform::destructor is invoked, internally we do platform cleanup