ControlPoint.h
Go to the documentation of this file.
1 /****
2  * ControlPoint.h
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  * Copyright 2020 slaff <slaff@attachix.com>
6  *
7  * This file is part of the Sming UPnP Library
8  *
9  * This library is free software: you can redistribute it and/or modify it under the terms of the
10  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with this library.
17  * If not, see <https://www.gnu.org/licenses/>.
18  *
19  ****/
20 
21 #pragma once
22 
23 #include "Object.h"
24 #include "ObjectList.h"
25 #include "ClassGroup.h"
26 #include <Network/SSDP/Message.h>
27 #include <Network/HttpClient.h>
28 #include "Constants.h"
29 #include "DeviceControl.h"
30 #include "Search.h"
31 #include <memory>
32 
33 namespace UPnP
34 {
35 class ControlPoint : public ObjectTemplate<ControlPoint, BaseObject>
36 {
37 public:
42  ControlPoint(size_t maxResponseSize = 2048) : maxResponseSize(maxResponseSize)
43  {
44  controlPoints.add(this);
45  }
46 
48  {
49  controlPoints.remove(this);
50  }
51 
55  void reset()
56  {
57  cancelSearch();
58  uniqueServiceNames.clear();
59  }
60 
64  void clear()
65  {
66  reset();
67  rootDevices.clear();
68  }
69 
76  bool beginSearch(const Urn& urn, SsdpSearch::Callback callback)
77  {
78  return submitSearch(new SsdpSearch(urn, callback));
79  }
80 
87  bool beginSearch(const Urn& urn, DescriptionSearch::Callback callback)
88  {
89  return submitSearch(new DescriptionSearch(urn, callback));
90  }
91 
98  bool beginSearch(const ObjectClass& cls, DeviceSearch::Callback callback)
99  {
100  return submitSearch(new DeviceSearch(cls, callback));
101  }
102 
110  {
111  return submitSearch(new ServiceSearch(cls, callback));
112  }
113 
114  template <typename Device> bool beginSearch(Delegate<bool(Device&)> callback)
115  {
116  return beginSearch(Device().getClass(),
117  [callback](DeviceControl& device) { return callback(reinterpret_cast<Device&>(device)); });
118  }
119 
123  bool isSearchActive() const
124  {
125  return bool(activeSearch);
126  }
127 
134  bool cancelSearch();
135 
140  virtual void onNotify(BasicMessage& msg);
141 
142  bool formatMessage(Message& msg, MessageSpec& ms) override;
143 
149  bool sendRequest(HttpRequest* request);
150 
158 
162  static void onSsdpMessage(BasicMessage& msg);
163 
169  static const ObjectClass* findClass(const Urn& objectType);
170 
179  static void registerClasses(const FlashString& domain, const ObjectClass::List& classes)
180  {
181  objectClasses.add(domain, classes);
182  }
183 
184 private:
185  using List = ObjectList<ControlPoint>;
186 
187  bool submitSearch(Search* search);
188  static bool processDescriptionResponse(HttpConnection& connection, String& buffer, XML::Document& description);
189 
190  static List controlPoints;
191  static ClassGroup::List objectClasses;
192  DeviceControl::OwnedList rootDevices;
193  static HttpClient http;
194  size_t maxResponseSize; // <<< Maximum size of XML description that can be processed
195  CStringArray uniqueServiceNames;
196  std::unique_ptr<Search> activeSearch;
197 };
198 
199 } // namespace UPnP
Class to manage a double null-terminated list of strings, such as "one\0two\0three\0".
Definition: CStringArray.h:22
void clear()
Empty the array.
Definition: CStringArray.h:238
describes a counted string stored in flash memory
Definition: String.hpp:174
Class to access a Vector of objects stored in flash.
Definition: Vector.hpp:110
Definition: HttpClient.h:29
Provides http base used for client and server connections.
Definition: HttpConnection.h:28
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:37
Handles incoming messages.
Definition: SSDP/src/include/Network/SSDP/Message.h:62
Defines the information used to create an outgoing message.
Definition: MessageSpec.h:75
Message using regular HTTP header management class.
Definition: SSDP/src/include/Network/SSDP/Message.h:72
The String class.
Definition: WString.h:137
Definition: ClassGroup.h:39
int add(const FlashString &domain, const ObjectClass::List &classes)
Definition: ControlPoint.h:36
bool beginSearch(const ObjectClass &cls, DeviceSearch::Callback callback)
Searches for UPnP device.
Definition: ControlPoint.h:98
bool beginSearch(Delegate< bool(Device &)> callback)
Definition: ControlPoint.h:114
static void registerClasses(const FlashString &domain, const ObjectClass::List &classes)
Use this method to register all device and service classes you need to control.
Definition: ControlPoint.h:179
bool sendRequest(HttpRequest *request)
Send a request.
static void onSsdpMessage(BasicMessage &msg)
Called via SSDP when incoming message received.
void reset()
Cancel any outstanding search and reset the list of known unique service names.
Definition: ControlPoint.h:55
bool beginSearch(const Urn &urn, DescriptionSearch::Callback callback)
Searches for UPnP device or service and fetches its description.
Definition: ControlPoint.h:87
void clear()
Perform a reset and destroy all created devices.
Definition: ControlPoint.h:64
bool isSearchActive() const
Determine if there's an active search in progress.
Definition: ControlPoint.h:123
~ControlPoint()
Definition: ControlPoint.h:47
ControlPoint(size_t maxResponseSize=2048)
Constructor.
Definition: ControlPoint.h:42
bool requestDescription(const String &url, DescriptionSearch::Callback callback)
Send a request for description document.
bool beginSearch(const Urn &urn, SsdpSearch::Callback callback)
Searches for UPnP device or service and returns SSDP response messages.
Definition: ControlPoint.h:76
bool cancelSearch()
Cancel any active search operation.
bool formatMessage(Message &msg, MessageSpec &ms) override
Standard fields have been completed.
static const ObjectClass * findClass(const Urn &objectType)
Find a registered class.
bool beginSearch(const ObjectClass &cls, ServiceSearch::Callback callback)
Searches for UPnP service.
Definition: ControlPoint.h:109
virtual void onNotify(BasicMessage &msg)
Called by framework to handle an incoming SSDP message.
Definition: DeviceControl.h:32
Represents any kind of device, including a root device.
Definition: Libraries/UPnP/src/include/Network/UPnP/Device.h:58
bool add(LinkedItem *item)
bool remove(LinkedItem *item)
Class template for singly-linked list of objects.
Definition: ObjectList.h:32
Base class template for linked items with type casting.
Definition: BaseObject.h:80
void clear()
Definition: ObjectList.h:109
Structure for UPnP URNs.
Definition: Urn.h:41
Definition: ActionRequest.h:25
rapidxml::xml_document< char > Document
Definition: RapidXML.h:36
Definition: Search.h:106
Definition: Search.h:125
Describes device or service class.
Definition: ObjectClass.h:36
This is a helper class used by ControlPoint to manage different search types.
Definition: Search.h:31
Definition: Search.h:148
Definition: Search.h:88