21template <
typename ClientType>
54 std::function<size_t(Print&,
void*)>
writer;
89template <
typename ClientType>
130 const char* sid =
nullptr,
131 uint32_t timeoutSec = 1800)
override {
141 if (
StrView(callbackUrl).isEmpty()) {
144 "subscribe: missing CALLBACK header for new subscription");
152 if (!renewed.
isEmpty())
return renewed;
158 snprintf(buffer,
sizeof(buffer),
"uuid:%lu", millis());
166 s->
expires_at = millis() + (uint64_t)timeoutSec * 1000ULL;
170 if (existing_sub->service == &service &&
174 "subscribe: found existing subscription for service "
175 "'%s' and callback '%s', renewing SID '%s'",
178 existing_sub->sid.c_str());
180 existing_sub->timeout_sec = timeoutSec;
181 existing_sub->expires_at = millis() + (uint64_t)timeoutSec * 1000ULL;
182 return existing_sub->sid;
241 std::function<
size_t(Print&,
void*)> changeWriter,
242 void* ref)
override {
250 if (sub->
service != &service)
continue;
299 "pending notification dropped: missing subscription");
308 snprintf(seqBuf,
sizeof(seqBuf),
"%d", pn.
seq);
339 "dropping notify to %s after %d errors with rc=%d %s",
351 "Published: %d notifications, %d remaining (for %d subscriptions)",
407 uint64_t now = millis();
430 const char* callbackUrl, uint32_t timeoutSec) {
437 ex->
expires_at = millis() + (uint64_t)timeoutSec * 1000ULL;
474 const char* service_abbrev =
479 written += out.println(
"<?xml version=\"1.0\"?>");
480 written += out.println(
482 "xmlns:e=\"urn:schemas-upnp-org:metadata-1-0/events\">");
483 written += out.println(
"<e:property>");
484 written += out.println(
"<LastChange>");
487 out_esc.print(
"<Event xmlns=\"urn:schemas-upnp-org:metadata-1-0/");
488 written += out_esc.print(service_abbrev);
489 written += out_esc.print(
"/\">");
490 written += out_esc.print(
"<InstanceID val=\"");
491 written += out_esc.print(instance_id);
492 written += out_esc.print(
"\">");
496 written += out_esc.print(
"</InstanceID>");
497 written += out_esc.print(
"</Event>");
499 written += out.println();
500 written += out.println(
"</LastChange>");
501 written += out.println(
"</e:property>");
502 written += out.println(
"</e:propertyset>");
526 callbackHeader ? callbackHeader :
"null");
528 timeoutHeader ? timeoutHeader :
"null");
530 sidHeader ? sidHeader :
"null");
534 if (callbackHeader) {
535 cbStr = callbackHeader;
551 uint32_t tsec = 1800;
552 if (timeoutHeader &&
StrView(timeoutHeader).startsWith(
"Second-")) {
553 tsec = atoi(timeoutHeader + 7);
557 const char* callbackPtr = cbStr.
isEmpty() ? nullptr : cbStr.
c_str();
558 const char* sidPtr = sidStr.
isEmpty() ? nullptr : sidStr.
c_str();
560 Str sid =
subscribe(service, callbackPtr, sidPtr, tsec);
563 "subscribe request rejected (missing data)");
612 "handleUnsubscribeRequest: failed for SID %s",
613 sid ? sid :
"(null)");
Setup of a Basic DLNA Device service. The device registers itself to the network and answers to the D...
Definition: DLNADevice.h:39
Attributes needed for the DLNA Service Definition.
Definition: DLNAServiceInfo.h:18
Str service_id
Definition: DLNAServiceInfo.h:38
int instance_id
Definition: DLNAServiceInfo.h:56
const char * subscription_namespace_abbrev
Definition: DLNAServiceInfo.h:53
Simple API to process get, put, post, del http requests I tried to use Arduino HttpClient,...
Definition: HttpRequest.h:27
HttpRequestHeader & request() override
Returns the request header.
Definition: HttpRequest.h:165
void setHost(const char *host) override
Sets the host name for the requests.
Definition: HttpRequest.h:48
HttpReplyHeader & reply() override
Returns the reply header.
Definition: HttpRequest.h:162
int notify(Url &url, std::function< size_t(Print &, void *)> writer, const char *mime=nullptr, void *ref=nullptr) override
Sends a NOTIFY request with streaming body.
Definition: HttpRequest.h:91
void setTimeout(int ms) override
Sets the timeout.
Definition: HttpRequest.h:184
void setAgent(const char *agent) override
Sets the user agent.
Definition: HttpRequest.h:168
Abstract interface for HTTP server functionality.
Definition: IHttpServer.h:30
virtual void replyNotFound()=0
Send 404 Not Found response.
virtual void replyOK()=0
Send 200 OK response.
virtual HttpReplyHeader & replyHeader()=0
Get reference to reply header.
virtual Client & client()=0
Get reference to current client.
virtual void endClient()=0
Close the client connection.
virtual HttpRequestHeader & requestHeader()=0
Get reference to request header.
Abstract interface for UPnP event subscription management.
Definition: ISubscriptionMgrDevice.h:27
Class with does not do any output: it can be used to determine the length of the output.
Definition: NullPrint.h:12
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:18
virtual bool isEmpty()
checks if the string is empty
Definition: StrView.h:383
virtual const char * c_str()
provides the string value as const char*
Definition: StrView.h:376
virtual bool equals(const char *str)
checks if the string equals indicated parameter string
Definition: StrView.h:177
Heap-backed string utility used throughout tiny_dlna.
Definition: Str.h:27
bool isEmpty() const
True if empty.
Definition: Str.h:54
void trim()
Trim spaces on both ends.
Definition: Str.h:235
const char * c_str() const
C-string pointer to internal buffer.
Definition: Str.h:88
bool replace(const char *toReplace, const char *replaced, int startPos=0)
Replace first occurrence of toReplace with replaced starting at startPos.
Definition: Str.h:269
Manages UPnP event subscriptions and queued notifications for a DLNA device.
Definition: SubscriptionMgrDevice.h:90
Str subscribe(DLNAServiceInfo &service, const char *callbackUrl, const char *sid=nullptr, uint32_t timeoutSec=1800) override
Add or renew a subscription for a service.
Definition: SubscriptionMgrDevice.h:129
void addChange(DLNAServiceInfo &service, std::function< size_t(Print &, void *)> changeWriter, void *ref) override
Enqueue a state-variable change for delivery.
Definition: SubscriptionMgrDevice.h:240
int publish() override
Deliver queued notifications.
Definition: SubscriptionMgrDevice.h:278
Str renewSubscription(DLNAServiceInfo &service, const char *sid, const char *callbackUrl, uint32_t timeoutSec)
Try to renew an existing subscription identified by SID for the given service. If successful the subs...
Definition: SubscriptionMgrDevice.h:429
bool unsubscribe(DLNAServiceInfo &service, const char *sid) override
Remove a subscription identified by SID.
Definition: SubscriptionMgrDevice.h:203
size_t pendingCount() override
Number of queued pending notifications.
Definition: SubscriptionMgrDevice.h:367
Vector< Subscription * > subscriptions
Definition: SubscriptionMgrDevice.h:395
void end() override
Convenience method to disable subscriptions at the end of the lifecycle.
Definition: SubscriptionMgrDevice.h:382
void setSubscriptionsActive(bool flag) override
Enable or disable subscription delivery.
Definition: SubscriptionMgrDevice.h:377
SubscriptionMgrDevice()=default
Construct a new Subscription Manager.
void clear()
Clear all subscriptions and pending notifications.
Definition: SubscriptionMgrDevice.h:451
static size_t createXML(Print &out, void *ref)
Generate the propertyset XML for a single variable and write it to the provided Print instance....
Definition: SubscriptionMgrDevice.h:471
~SubscriptionMgrDevice()
Destroy the Subscription Manager.
Definition: SubscriptionMgrDevice.h:110
bool processUnsubscribeRequest(IHttpServer &server, DLNAServiceInfo &service) override
Handle UNSUBSCRIBE request including HTTP response.
Definition: SubscriptionMgrDevice.h:594
bool isSubscriptionsActive() override
Query whether subscription delivery is active.
Definition: SubscriptionMgrDevice.h:388
bool processSubscribeRequest(IHttpServer &server, DLNAServiceInfo &service) override
Handle SUBSCRIBE request including HTTP response.
Definition: SubscriptionMgrDevice.h:517
bool is_active
Definition: SubscriptionMgrDevice.h:397
size_t subscriptionsCount() override
Number of active subscriptions.
Definition: SubscriptionMgrDevice.h:361
void removeExpired()
Remove expired subscriptions.
Definition: SubscriptionMgrDevice.h:406
Vector< PendingNotification > pending_list
Definition: SubscriptionMgrDevice.h:396
URL parser which breaks a full url string up into its individual parts.
Definition: Url.h:18
const char * host()
Definition: Url.h:41
const char * url()
Definition: Url.h:39
Lightweight wrapper around std::vector with Arduino-friendly helpers and a pluggable allocator.
Definition: Vector.h:39
#define DLNA_HTTP_REQUEST_TIMEOUT_MS
Define the default http request timeout.
Definition: dlna_config.h:20
#define MAX_NOTIFY_RETIES
Define maximum number of notify retries.
Definition: dlna_config.h:163
Definition: Allocator.h:13
Print wrapper that escapes & < > " ' while forwarding to an underlying Print. Returns the expanded ou...
Definition: EscapingPrint.h:8
Representation of a queued notification to be delivered later.
Definition: SubscriptionMgrDevice.h:51
int error_count
Definition: SubscriptionMgrDevice.h:57
int seq
Definition: SubscriptionMgrDevice.h:58
void * ref
Definition: SubscriptionMgrDevice.h:56
Subscription * p_subscription
Definition: SubscriptionMgrDevice.h:52
std::function< size_t(Print &, void *)> writer
Definition: SubscriptionMgrDevice.h:54
Represents a single event subscription for a service.
Definition: SubscriptionMgrDevice.h:33
uint32_t timeout_sec
Definition: SubscriptionMgrDevice.h:36
Str sid
Definition: SubscriptionMgrDevice.h:34
uint32_t seq
Definition: SubscriptionMgrDevice.h:37
DLNAServiceInfo * service
Definition: SubscriptionMgrDevice.h:39
Str callback_url
Definition: SubscriptionMgrDevice.h:35
uint64_t expires_at
Definition: SubscriptionMgrDevice.h:38