55#include " Client.h"
66#include " DLNAControlPointRequestParser.h"
77#include " basic/Url.h"
8+ #include " dlna/clients/IControlPoint.h"
9+ #include " dlna/clients/SubscriptionMgrControlPoint.h"
810#include " dlna/common/DLNADeviceInfo.h"
911#include " dlna/common/Schedule.h"
1012#include " dlna/common/Scheduler.h"
11- #include " dlna/clients/IControlPoint.h"
12- #include " dlna/clients/SubscriptionMgrControlPoint.h"
1313#include " dlna/devices/DLNADevice.h"
1414#include " dlna/xml/XMLDeviceParser.h"
1515#include " dlna/xml/XMLParser.h"
@@ -87,10 +87,11 @@ class DLNAControlPoint : public IControlPoint {
8787
8888 // / Defines the local url (needed for subscriptions)
8989 void setLocalURL (Url url) override { local_url = url; }
90- void setLocalURL (IPAddress url, int port=9001 , const char * path=" " ) override {
90+ void setLocalURL (IPAddress url, int port = 9001 ,
91+ const char * path = " " ) override {
9192 char buffer[200 ];
92- snprintf (buffer, sizeof (buffer), " http://%s:%d%s" , url.toString ().c_str (), port,
93- path);
93+ snprintf (buffer, sizeof (buffer), " http://%s:%d%s" , url.toString ().c_str (),
94+ port, path);
9495 local_url.setUrl (buffer);
9596 }
9697
@@ -118,7 +119,6 @@ class DLNAControlPoint : public IControlPoint {
118119 void setHttpServer (IHttpServer& server) override {
119120 DlnaLogger.log (DlnaLogLevel::Debug, " DLNAControlPointMgr::setHttpServer" );
120121 p_http_server = &server;
121- subscription_mgr.setHttpServer (server);
122122 }
123123
124124 // / Register a callback that will be invoked when parsing SOAP/Action results
@@ -161,14 +161,6 @@ class DLNAControlPoint : public IControlPoint {
161161 is_active = true ;
162162 setTransports (http, udp);
163163
164- if (p_http_server) {
165- // handle server requests
166- if (!p_http_server->begin ()) {
167- DlnaLogger.log (DlnaLogLevel::Error, " HttpServer begin failed" );
168- return false ;
169- }
170- }
171-
172164 // setup multicast UDP
173165 if (!(p_udp->begin (DLNABroadcastAddress))) {
174166 DlnaLogger.log (DlnaLogLevel::Error, " UDP begin failed" );
@@ -200,22 +192,6 @@ class DLNAControlPoint : public IControlPoint {
200192 loop ();
201193 }
202194
203- // setup subscription manager
204- if (local_url && p_http_server) {
205- subscription_mgr.setup (http, udp, local_url, getDevice ());
206- } else {
207- if (!local_url && !p_http_server) {
208- DlnaLogger.log (DlnaLogLevel::Info,
209- " No local URL and no HttpServer for subscriptions" );
210- } else if (!local_url) {
211- DlnaLogger.log (DlnaLogLevel::Warning,
212- " No local URL for subscriptions" );
213- } else {
214- DlnaLogger.log (DlnaLogLevel::Warning,
215- " No HttpServer for subscriptions" );
216- }
217- }
218-
219195 // If we exited early because a device was found, deactivate the MSearch
220196 // schedule so it will stop repeating. The scheduler will clean up
221197 // inactive schedules on its next pass.
@@ -236,6 +212,36 @@ class DLNAControlPoint : public IControlPoint {
236212 p_udp = &udp;
237213 }
238214
215+ // / Subscribes to event notifications for all services of the selected device
216+ bool subscribe () {
217+ if (devices.size () == 0 ) return false ;
218+ if (!getDevice ()) return false ;
219+
220+ // setup subscription manager
221+ if (local_url && p_http_server) {
222+ if (p_http_server) {
223+ // handle server requests
224+ if (!p_http_server->begin ()) {
225+ DlnaLogger.log (DlnaLogLevel::Error, " HttpServer begin failed" );
226+ return false ;
227+ }
228+ }
229+ subscription_mgr.setHttpServer (*p_http_server);
230+ subscription_mgr.setup (*p_http, *p_udp, local_url, getDevice ());
231+ return true ;
232+ }
233+
234+ if (!local_url && !p_http_server) {
235+ DlnaLogger.log (DlnaLogLevel::Info,
236+ " No local URL and no HttpServer for subscriptions" );
237+ } else if (!local_url) {
238+ DlnaLogger.log (DlnaLogLevel::Warning, " No local URL for subscriptions" );
239+ } else {
240+ DlnaLogger.log (DlnaLogLevel::Warning, " No HttpServer for subscriptions" );
241+ }
242+ return false ;
243+ }
244+
239245 // / Stops the processing and releases the resources
240246 void end () override {
241247 DlnaLogger.log (DlnaLogLevel::Debug, " DLNAControlPointMgr::end" );
@@ -365,6 +371,8 @@ class DLNAControlPoint : public IControlPoint {
365371 // / Provides the device information of the actually selected device
366372 DLNADeviceInfo& getDevice () override {
367373 DlnaLogger.log (DlnaLogLevel::Debug, " DLNAControlPointMgr::getDevice" );
374+ if (default_device_idx < 0 || default_device_idx >= devices.size ())
375+ return NO_DEVICE;
368376 return devices[default_device_idx];
369377 }
370378
@@ -672,7 +680,8 @@ class DLNAControlPoint : public IControlPoint {
672680 bool matches (const char * usn) {
673681 if (search_target == nullptr || *search_target == ' \0 ' ) return true ;
674682 if (StrView (search_target).equals (" ssdp:all" )) return true ;
675- return StrView (usn).contains (search_target) || StrView (search_target).contains (usn);
683+ return StrView (usn).contains (search_target) ||
684+ StrView (search_target).contains (usn);
676685 }
677686
678687 // / processes a bye-bye message
@@ -776,7 +785,8 @@ class DLNAControlPoint : public IControlPoint {
776785 " Service control_url: %s, device base: %s" ,
777786 StrView (service.control_url ).c_str (),
778787 StrView (device.getBaseURL ()).c_str ());
779- Url post_url = getUrl (device, service.control_url , url_buffer, DLNA_MAX_URL_LEN);
788+ Url post_url =
789+ getUrl (device, service.control_url , url_buffer, DLNA_MAX_URL_LEN);
780790 DlnaLogger.log (DlnaLogLevel::Info, " POST URL computed: %s" , post_url.url ());
781791
782792 // send HTTP POST and collect/handle response. If the caller provided an
0 commit comments