Arduino DLNA Server
Loading...
Searching...
No Matches
Action.h
Go to the documentation of this file.
1
2#pragma once
3// #include "dlna/DLNAServiceInfo.h"
4#include "dlna/DLNACommon.h"
6#include "basic/Logger.h" // ensure DlnaLogger and DlnaLogLevel declarations
7
8namespace tiny_dlna {
9
10class DLNAServiceInfo;
11class ActionRequest;
12
17class Argument {
18 public:
19 Argument() = default;
20 Argument(const char* nme, const char* val) {
21 name = nme;
22 value = val;
23 }
24 Str name{20};
26};
27
49 public:
50 ActionReply(bool valid = true) { is_valid = valid; }
51 bool isValid() const { return is_valid; }
52 void setValid(bool flag) { is_valid = flag; }
53 void add(ActionReply alt) {
54 if (!alt) setValid(false);
55 for (auto& arg : alt.arguments) {
56 arguments.push_back(arg);
57 }
58 }
60 for (auto& a : arguments) {
61 if (StrView(a.name).equals(arg.name.c_str())) {
62 a.value = arg.value;
63 return;
64 }
65 }
66 arguments.push_back(arg);
67 }
68
69 // Helper: find argument by name in ActionReply (returns nullptr if not found)
70 const char* findArgument(const char* name) {
71 for (auto& a : arguments) {
72 if (a.name.equals(name)) return a.value.c_str();
73 }
74 return nullptr;
75 }
76
77 operator bool() { return is_valid; }
78
79 int size() { return arguments.size(); }
80
81 void clear() { arguments.clear(); }
82
83 void logArguments() {
84 for (auto& a : arguments) {
85 DlnaLogger.log(DlnaLogLevel::Debug, " -> %s = %s",
86 StrView(a.name.c_str()).c_str(),
87 StrView(a.value.c_str()).c_str());
88 }
89 }
90
91 protected:
93 bool is_valid = true;
94};
95
105 public:
106 ActionRequest() = default;
107
108 ActionRequest(DLNAServiceInfo& srv, const char* act) {
109 p_service = &srv;
110 action = act;
111 }
112
113 void addArgument(Argument arg) { arguments.push_back(arg); }
114
115 void addArgument(const char* name, const char* value) {
116 if (!StrView(value).isEmpty()) {
117 for (auto& a : arguments) {
118 if (StrView(a.name).equals(name)) {
119 a.value = value;
120 return;
121 }
122 }
123 Argument arg{name, value};
124 addArgument(arg);
125 }
126 }
127
128 const char* getArgumentValue(const char* name) {
129 for (auto& a : arguments) {
130 if (a.name.endsWithIgnoreCase(name)) {
131 return StrView(a.value.c_str()).c_str();
132 }
133 }
134
135 Str list{80};
136 for (auto& a : arguments) {
137 list.add(a.name.c_str());
138 list.add(" ");
139 }
140
141 DlnaLogger.log(DlnaLogLevel::Info, "Argument '%s' not found in (%s)", name,
142 list.c_str());
143 return "";
144 }
145
146 int getArgumentIntValue(const char* name) {
147 return StrView(getArgumentValue(name)).toInt();
148 }
149
150 void clear() {
151 arguments.clear();
152 action.clear();
153 }
154
155 void setAction(const char* act) { action = act; }
156
157 const char* getAction() { return action.c_str(); }
158
159 Str& getActionStr() { return action; }
160
162
164
166
167 const Vector<Argument>& getArguments() const { return arguments; }
168
169 void setResultCount(int v) { result_count = v; }
170
171 int getResultCount() const { return result_count; }
172
173 operator bool() { return p_service != nullptr && !action.isEmpty(); }
174
175 protected:
177 // Start with an empty vector; previously we default-constructed with a
178 // size of 10 which introduced 10 empty arguments and led to malformed
179 // SOAP XML (< /> repeated). We only store explicitly added arguments.
183};
184
185} // namespace tiny_dlna
Represents the result of invoking a DLNA service Action.
Definition: Action.h:48
ActionReply(bool valid=true)
Definition: Action.h:50
int size()
Definition: Action.h:79
const char * findArgument(const char *name)
Definition: Action.h:70
bool isValid() const
Definition: Action.h:51
void add(ActionReply alt)
Definition: Action.h:53
void clear()
Definition: Action.h:81
void setValid(bool flag)
Definition: Action.h:52
Vector< Argument > arguments
Definition: Action.h:92
void addArgument(Argument arg)
Definition: Action.h:59
bool is_valid
Definition: Action.h:93
void logArguments()
Definition: Action.h:83
Represents a request to invoke a remote DLNA service action.
Definition: Action.h:104
Str & getActionStr()
Definition: Action.h:159
void addArgument(Argument arg)
Definition: Action.h:113
const char * getArgumentValue(const char *name)
Definition: Action.h:128
int getArgumentIntValue(const char *name)
Definition: Action.h:146
Str action
Definition: Action.h:182
DLNAServiceInfo * getService()
Definition: Action.h:163
void setAction(const char *act)
Definition: Action.h:155
Vector< Argument > arguments
Definition: Action.h:180
int getResultCount() const
Definition: Action.h:171
const char * getAction()
Definition: Action.h:157
int result_count
Definition: Action.h:181
void setService(DLNAServiceInfo *srv)
Definition: Action.h:161
Vector< Argument > & getArguments()
Definition: Action.h:165
const Vector< Argument > & getArguments() const
Definition: Action.h:167
void addArgument(const char *name, const char *value)
Definition: Action.h:115
DLNAServiceInfo * p_service
Definition: Action.h:176
void setResultCount(int v)
Definition: Action.h:169
void clear()
Definition: Action.h:150
ActionRequest(DLNAServiceInfo &srv, const char *act)
Definition: Action.h:108
DLNA Service: Action Argument.
Definition: Action.h:17
Str value
Definition: Action.h:25
Str name
Definition: Action.h:24
Argument(const char *nme, const char *val)
Definition: Action.h:20
Attributes needed for the DLNA Service Definition.
Definition: DLNAServiceInfo.h:18
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 const char * c_str()
provides the string value as const char*
Definition: StrView.h:376
int toInt()
Converts the string to an int.
Definition: StrView.h:607
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 add(const char *append)
Append C-string (ignored if nullptr)
Definition: Str.h:96
void clear()
Clear contents (size -> 0)
Definition: Str.h:93
const char * c_str() const
C-string pointer to internal buffer.
Definition: Str.h:88
Lightweight wrapper around std::vector with Arduino-friendly helpers and a pluggable allocator.
Definition: Vector.h:39
Definition: Allocator.h:13