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 
109  bool beginSearch(const ObjectClass& cls, ServiceSearch::Callback callback)
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 
157  bool requestDescription(const String& url, DescriptionSearch::Callback callback);
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
virtual void onNotify(BasicMessage &msg)
Called by framework to handle an incoming SSDP message.
Message using regular HTTP header management class.
Definition: SSDP/src/include/Network/SSDP/Message.h:71
void clear()
Perform a reset and destroy all created devices.
Definition: ControlPoint.h:100
bool add(LinkedItem *item)
Represents any kind of device, including a root device.
Definition: Libraries/UPnP/src/include/Network/UPnP/Device.h:57
ControlPoint(size_t maxResponseSize=2048)
Constructor.
Definition: ControlPoint.h:78
Definition: Search.h:106
Provides http base used for client and server connections.
Definition: HttpConnection.h:27
Definition: HttpClient.h:28
describes a counted string stored in flash memory
Definition: String.hpp:173
bool beginSearch(const Urn &urn, SsdpSearch::Callback callback)
Searches for UPnP device or service and returns SSDP response messages.
Definition: ControlPoint.h:112
Class to access a Vector of objects stored in flash.
Definition: Vector.hpp:109
Definition: DeviceControl.h:48
The String class.
Definition: WString.h:136
Class template for singly-linked list of objects.
Definition: ObjectList.h:48
bool requestDescription(const String &url, DescriptionSearch::Callback callback)
Send a request for description document.
bool sendRequest(HttpRequest *request)
Send a request.
Defines the information used to create an outgoing message.
Definition: MessageSpec.h:74
bool formatMessage(Message &msg, MessageSpec &ms) override
Standard fields have been completed.
static void onSsdpMessage(BasicMessage &msg)
Called via SSDP when incoming message received.
int add(const FlashString &domain, const ObjectClass::List &classes)
Structure for UPnP URNs.
Definition: Urn.h:40
Definition: ClassGroup.h:62
Describes device or service class.
Definition: ObjectClass.h:53
void reset()
Cancel any outstanding search and reset the list of known unique service names.
Definition: ControlPoint.h:91
Definition: ActionRequest.h:24
Definition: Search.h:124
Handles incoming messages.
Definition: SSDP/src/include/Network/SSDP/Message.h:61
bool remove(LinkedItem *item)
~ControlPoint()
Definition: ControlPoint.h:83
Delegate< void(SSDP::BasicMessage &message)> Callback
Callback invoked for every matching SSDP message.
Definition: Search.h:110
Definition: Search.h:143
Class to manage a double null-terminated list of strings, such as "one\0two\0three\0".
Definition: CStringArray.h:21
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:36
bool isSearchActive() const
Determine if there's an active search in progress.
Definition: ControlPoint.h:159
bool cancelSearch()
Cancel any active search operation.
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:215
This is a helper class used by ControlPoint to manage different search types.
Definition: Search.h:49
rapidxml::xml_document< char > Document
Definition: RapidXML.h:36
Definition: Search.h:166
void clear()
Definition: ObjectList.h:126
static const ObjectClass * findClass(const Urn &objectType)
Find a registered class.
void clear()
Empty the array.
Definition: CStringArray.h:248