kodi
PltService.h
Go to the documentation of this file.
1 /*****************************************************************
2 |
3 | Platinum - Service
4 |
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
8 |
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
13 |
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 | licensing@plutinosoft.com
21 |
22 | This program is distributed in the hope that it will be useful,
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 | GNU General Public License for more details.
26 |
27 | You should have received a copy of the GNU General Public License
28 | along with this program; see the file LICENSE.txt. If not, write to
29 | the Free Software Foundation, Inc.,
30 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 | http://www.gnu.org/licenses/gpl-2.0.html
32 |
33 ****************************************************************/
34 
39 #ifndef _PLT_SERVICE_H_
40 #define _PLT_SERVICE_H_
41 
42 /*----------------------------------------------------------------------
43 | includes
44 +---------------------------------------------------------------------*/
45 #include "Neptune.h"
46 #include "PltEvent.h"
47 #include "PltArgument.h"
48 #include "PltStateVariable.h"
49 #include "PltAction.h"
50 
51 /*----------------------------------------------------------------------
52 | forward declarations
53 +---------------------------------------------------------------------*/
54 class PLT_DeviceData;
55 
56 /*----------------------------------------------------------------------
57 | PLT_Service class
58 +---------------------------------------------------------------------*/
68 {
69 public:
70  // methods
80  const char* type,
81  const char* id,
82  const char* name,
83  const char* last_change_namespace = NULL);
84  virtual ~PLT_Service();
85 
86  // methods
91  NPT_Result InitURLs(const char* service_name);
92 
97  bool IsValid() { return (m_ActionDescs.GetItemCount() > 0); }
98 
105  NPT_Result PauseEventing(bool pause = true);
106 
107  // class methods
108  static bool IsTrue(const NPT_String& value) {
109  if (value.Compare("1", true) &&
110  value.Compare("true", true) &&
111  value.Compare("yes", true)) {
112  return false;
113  }
114  return true;
115  }
116 
117  // accessor methods
122  NPT_Result SetSCPDURL(const char* url) { m_SCPDURL = url; return NPT_SUCCESS; }
123 
124  /*
125  Set the Service Control url for control points to be able to invoke actions.
126  @param url relative path of control url
127  */
128  NPT_Result SetControlURL(const char* url) { m_ControlURL = url; return NPT_SUCCESS; };
129 
135  NPT_Result SetEventSubURL(const char* url) { m_EventSubURL = url; return NPT_SUCCESS; };
136 
143  NPT_String GetSCPDURL(bool absolute = false);
144 
151  NPT_String GetControlURL(bool absolute = false);
152 
159  NPT_String GetEventSubURL(bool absolute = false);
160 
165  const NPT_String& GetServiceID() const { return m_ServiceID; }
166 
171  const NPT_String& GetServiceType() const { return m_ServiceType; }
172 
177  const NPT_String& GetServiceName() const { return m_ServiceName; }
178 
183  PLT_DeviceData* GetDevice() { return m_Device; }
184 
191  NPT_Result ForceVersion(NPT_Cardinal version);
192 
197  NPT_Result GetSCPDXML(NPT_String& xml);
198 
203  NPT_Result SetSCPDXML(const char* xml);
204 
211  NPT_Result GetDescription(NPT_XmlElementNode* parent, NPT_XmlElementNode** service = NULL);
212 
221  NPT_Result SetStateVariable(const char* name, const char* value, const bool clearonsend = false);
222 
230  NPT_Result SetStateVariableRate(const char* name, NPT_TimeInterval rate);
231 
238  NPT_Result SetStateVariableExtraAttribute(const char* name, const char* key, const char* value);
239 
244  NPT_Result IncStateVariable(const char* name);
245 
251  PLT_StateVariable* FindStateVariable(const char* name);
252 
258  NPT_Result GetStateVariableValue(const char* name, NPT_String& value);
259 
264  bool IsSubscribable();
265 
270  const NPT_List<PLT_StateVariable*>& GetStateVariables() const { return m_StateVars; }
271 
277  PLT_ActionDesc* FindActionDesc(const char* name);
278 
283  const NPT_Array<PLT_ActionDesc*>& GetActionDescs() const { return m_ActionDescs; }
284 
285 private:
292  class PLT_ServiceEventTask : public PLT_ThreadTask {
293  public:
294  PLT_ServiceEventTask(PLT_Service* service) : m_Service(service) {}
295 
296  void DoRun() override {
297  while (!IsAborting(100)) m_Service->NotifyChanged();
298  }
299 
300  private:
301  PLT_Service* m_Service;
302  };
303 
304  // methods
305  void Cleanup();
306 
312  NPT_Result AddChanged(PLT_StateVariable* var);
313 
320  NPT_Result UpdateLastChange(NPT_List<PLT_StateVariable*>& vars);
321 
325  NPT_Result NotifyChanged();
326 
327  // Events
331  NPT_Result ProcessNewSubscription(
332  PLT_TaskManagerReference task_manager,
333  const NPT_SocketAddress& addr,
334  const NPT_String& callback_urls,
335  int timeout,
336  NPT_HttpResponse& response);
337 
342  NPT_Result ProcessRenewSubscription(
343  const NPT_SocketAddress& addr,
344  const NPT_String& sid,
345  int timeout,
346  NPT_HttpResponse& response);
347 
352  NPT_Result ProcessCancelSubscription(
353  const NPT_SocketAddress& addr,
354  const NPT_String& sid,
355  NPT_HttpResponse& response);
356 
357 
358 protected:
359  // friends that need to call private functions
360  friend class PLT_StateVariable; // AddChanged
361  friend class PLT_DeviceHost; // ProcessXXSubscription
362 
363  //members
364  PLT_DeviceData* m_Device;
365  NPT_String m_ServiceType;
366  NPT_String m_ServiceID;
367  NPT_String m_ServiceName;
368  NPT_String m_SCPDURL;
369  NPT_String m_ControlURL;
370  NPT_String m_EventSubURL;
371  PLT_ServiceEventTask* m_EventTask;
372  NPT_Array<PLT_ActionDesc*> m_ActionDescs;
373  NPT_List<PLT_StateVariable*> m_StateVars;
374  NPT_Mutex m_Lock;
375  NPT_List<PLT_StateVariable*> m_StateVarsChanged;
376  NPT_List<PLT_StateVariable*> m_StateVarsToPublish;
378  bool m_EventingPaused;
379  NPT_String m_LastChangeNamespace;
380 };
381 
382 /*----------------------------------------------------------------------
383 | PLT_ServiceSCPDURLFinder
384 +---------------------------------------------------------------------*/
390 {
391 public:
392  // methods
393  PLT_ServiceSCPDURLFinder(const char* url) : m_URL(url) {}
394  virtual ~PLT_ServiceSCPDURLFinder() {}
395  bool operator()(PLT_Service* const & service) const;
396 
397 private:
398  // members
399  NPT_String m_URL;
400 };
401 
402 /*----------------------------------------------------------------------
403 | PLT_ServiceControlURLFinder
404 +---------------------------------------------------------------------*/
410 {
411 public:
412  // methods
413  PLT_ServiceControlURLFinder(const char* url) : m_URL(url) {}
414  virtual ~PLT_ServiceControlURLFinder() {}
415  bool operator()(PLT_Service* const & service) const;
416 
417 private:
418  // members
419  NPT_String m_URL;
420 };
421 
422 /*----------------------------------------------------------------------
423 | PLT_ServiceEventSubURLFinder
424 +---------------------------------------------------------------------*/
430 {
431 public:
432  // methods
433  PLT_ServiceEventSubURLFinder(const char* url) : m_URL(url) {}
434  virtual ~PLT_ServiceEventSubURLFinder() {}
435  bool operator()(PLT_Service* const & service) const;
436 
437 private:
438  // members
439  NPT_String m_URL;
440 };
441 
442 /*----------------------------------------------------------------------
443 | PLT_ServiceIDFinder
444 +---------------------------------------------------------------------*/
450 {
451 public:
452  // methods
453  PLT_ServiceIDFinder(const char* id) : m_Id(id) {}
454  virtual ~PLT_ServiceIDFinder() {}
455  bool operator()(PLT_Service* const & service) const;
456 
457 private:
458  // members
459  NPT_String m_Id;
460 };
461 
462 /*----------------------------------------------------------------------
463 | PLT_ServiceTypeFinder
464 +---------------------------------------------------------------------*/
470 {
471 public:
472  // methods
473  PLT_ServiceTypeFinder(const char* type) : m_Type(type) {}
474  virtual ~PLT_ServiceTypeFinder() {}
475  bool operator()(PLT_Service* const & service) const;
476 
477 private:
478  // members
479  NPT_String m_Type;
480 };
481 
482 /*----------------------------------------------------------------------
483 | PLT_ServiceNameFinder
484 +---------------------------------------------------------------------*/
490 {
491 public:
492  // methods
493  PLT_ServiceNameFinder(const char* name) : m_Name(name) {}
494  virtual ~PLT_ServiceNameFinder() {}
495  bool operator()(PLT_Service* const & service) const;
496 
497 private:
498  // members
499  NPT_String m_Name;
500 };
501 
502 /*----------------------------------------------------------------------
503 | PLT_LastChangeXMLIterator
504 +---------------------------------------------------------------------*/
510 {
511 public:
512  // methods
513  PLT_LastChangeXMLIterator(NPT_XmlElementNode* node) : m_Node(node) {}
514  virtual ~PLT_LastChangeXMLIterator() {}
515 
516  NPT_Result operator()(PLT_StateVariable* const & var) const;
517 
518 private:
519  NPT_XmlElementNode* m_Node;
520 };
521 
522 #endif /* _PLT_SERVICE_H_ */
NPT_Result GetSCPDXML(NPT_String &xml)
Return the service SCPD xml document.
Definition: PltService.cpp:91
NPT_Result InitURLs(const char *service_name)
When service is hosted by a PLT_DeviceHost, this setups the SCPD, control and event urls...
Definition: PltService.cpp:156
UPnP State Variable.
Definition: NptXml.h:172
const NPT_List< PLT_StateVariable * > & GetStateVariables() const
Return the list of state variables.
Definition: PltService.h:270
The PLT_LastChangeXMLIterator class is used to serialize the LastChange variable changes into xml giv...
Definition: PltService.h:509
The PLT_ActionDesc class provides information about a UPnP Service given action.
Definition: PltAction.h:61
const NPT_Array< PLT_ActionDesc * > & GetActionDescs() const
Return an array of actions descriptions PLT_ActionDesc.
Definition: PltService.h:283
Definition: NptHttp.h:315
UPnP Service Action Argument.
Definition: NptArray.h:54
The PLT_ServiceIDFinder class returns an instance of a PLT_Service given a service id...
Definition: PltService.h:449
NPT_Result ForceVersion(NPT_Cardinal version)
When a control point discover a new service with a higher version number than it can work with...
Definition: PltService.cpp:401
UPnP Service.
Definition: PltService.h:67
NPT_Result SetSCPDXML(const char *xml)
Set the service SCPD xml document.
Definition: PltService.cpp:172
Definition: NptThreads.h:76
NPT_Result GetStateVariableValue(const char *name, NPT_String &value)
Return the state variable value given a state variable name.
Definition: PltService.cpp:436
PLT_DeviceData * GetDevice()
Return the PLT_DeviceData* the service is associated with.
Definition: PltService.h:183
Definition: NptTime.h:50
NPT_Result PauseEventing(bool pause=true)
When a PLT_DeviceHost needs to change more than one state variables at a time but would rather send o...
Definition: PltService.cpp:774
NPT_Result SetStateVariableExtraAttribute(const char *name, const char *key, const char *value)
Certain state variables require extra xml attributes when serialized.
Definition: PltService.cpp:490
The PLT_StateVariable class maintains the state of a UPnP Service state variable. ...
Definition: PltStateVariable.h:75
Definition: NptSockets.h:77
PLT_StateVariable * FindStateVariable(const char *name)
Return the PLT_StateVariable pointer given a state variable name.
Definition: PltService.cpp:425
PLT_ActionDesc * FindActionDesc(const char *name)
Return the PLT_ActionDesc given an action name.
Definition: PltService.cpp:414
UPnP Eventing.
NPT_String GetEventSubURL(bool absolute=false)
Return the Event subscription url associated with this service.
Definition: PltService.cpp:391
The PLT_ServiceNameFinder class returns an instance of a PLT_Service given a service name...
Definition: PltService.h:489
bool IsSubscribable()
Return whether a service is capable of sending events.
Definition: PltService.cpp:448
UPnP Service Action.
The PLT_DeviceData class holds information about a device being advertised or found by a control poin...
Definition: PltDeviceData.h:93
The PLT_ServiceEventSubURLFinder class returns an instance of a PLT_Service given a service event sub...
Definition: PltService.h:429
UPnP Device Host.
Definition: PltDeviceHost.h:69
The PLT_ServiceSCPDURLFinder class returns an instance of a PLT_Service given a service SCPD url...
Definition: PltService.h:389
NPT_Result SetStateVariable(const char *name, const char *value, const bool clearonsend=false)
Set a new value for a given state variable.
Definition: PltService.cpp:462
const NPT_String & GetServiceType() const
Return the service type.
Definition: PltService.h:171
NPT_Result GetDescription(NPT_XmlElementNode *parent, NPT_XmlElementNode **service=NULL)
Populate the UPnP Device description document with service information.
Definition: PltService.cpp:136
The PLT_ServiceTypeFinder class returns an instance of a PLT_Service given a service type...
Definition: PltService.h:469
PLT_Service(PLT_DeviceData *device, const char *type, const char *id, const char *name, const char *last_change_namespace=NULL)
Create an instance of a UPnP Service either hosted or discovered.
Definition: PltService.cpp:49
const NPT_String & GetServiceName() const
Return the service friendly name.
Definition: PltService.h:177
NPT_String GetSCPDURL(bool absolute=false)
Return the SCPD url associated with this service.
Definition: PltService.cpp:371
const NPT_String & GetServiceID() const
Return the service id.
Definition: PltService.h:165
The PLT_ServiceControlURLFinder class returns an instance of a PLT_Service given a service control ur...
Definition: PltService.h:409
The PLT_ThreadTask class is a base class for executing a given task in a worker thread.
Definition: PltThreadTask.h:56
Definition: NptStrings.h:57
NPT_String GetControlURL(bool absolute=false)
Return the Control url associated with this service.
Definition: PltService.cpp:381
bool IsValid()
Verify the service has been properly initialized or is a valid discovered service.
Definition: PltService.h:97
Definition: NptList.h:54
NPT_Result SetSCPDURL(const char *url)
Set the SCPD url for control points to be able to fetch the SCPD xml document.
Definition: PltService.h:122
NPT_Result IncStateVariable(const char *name)
Helper function to increment a state variable representing a number.
Definition: PltService.cpp:506
NPT_Result SetStateVariableRate(const char *name, NPT_TimeInterval rate)
Certain state variables notifications must not be sent faster than a certain rate according to the UP...
Definition: PltService.cpp:476
NPT_Result SetEventSubURL(const char *url)
Set the Service Event subscription url for control points to be able to subscribe to events...
Definition: PltService.h:135