FastLED 3.10.8
Loading...
Searching...
No Matches

◆ processRpc()

fl::json fl::Remote::processRpc ( const fl::json & request)

Process JSON-RPC request (with optional "timestamp" field for scheduling) Returns JSON-RPC response: {"result": ...} or {"error": {...}}.

Definition at line 154 of file remote.cpp.hpp.

154 {
155#if FL_PLATFORM_HAS_LARGE_MEMORY
156 // Extract optional timestamp field (0 = immediate, >0 = scheduled).
157 // Low-memory builds drop the scheduling path entirely (see #3224 Tier 1B):
158 // the LPC8xx integer-only RPC contract is invoked immediate-only.
159 u32 timestamp = 0;
160 if (request.contains("timestamp") && request["timestamp"].is_int()) {
161 timestamp = static_cast<u32>(request["timestamp"].as_int().value());
162 }
163#endif
164
165 u32 receivedAt = fl::millis();
166
167#if FL_PLATFORM_HAS_LARGE_MEMORY
168 // Execute or schedule
169 if (timestamp != 0) {
170 // Scheduled execution - result will be pushed to ResponseSink after execution
171 scheduleFunction(timestamp, receivedAt, request);
172 FL_DBG("RPC: Scheduled function - result will be pushed after execution");
173
174 // Return acknowledgment with null result and "scheduled" marker
175 fl::json response = fl::json::object();
176 if (request.contains("id")) {
177 response.set("id", request["id"]);
178 }
179 response.set("result", fl::json(nullptr));
180 response.set("scheduled", true); // Marker to not queue this response
181 return response;
182 }
183#endif
184
185 // Immediate execution path (always reached on Low-memory).
186
187#if FL_PLATFORM_HAS_LARGE_MEMORY
188 // Store request ID BEFORE invoking function (needed for async functions).
189 // This allows sendAsyncResponse() to find the request ID while function is
190 // running. Low-memory drops the async-tracking machinery (no production
191 // LowMemory sketch uses bindAsync / sendAsyncResponse today) -- see
192 // #3224 Tier 1B.
193 if (request.contains("id") && request.contains("method")) {
194 fl::string methodName = request["method"].as_string().value_or("");
195 int requestId = request["id"].as_int().value_or(0);
196 mAsyncRequests[methodName] = {requestId, receivedAt};
197 FL_DBG("Stored request ID for " << methodName.c_str() << " (id=" << requestId << ")");
198 }
199#endif
200
201 // Immediate execution - pass directly to Rpc
202 fl::json response = mRpc.handle(request);
203
204#if FL_PLATFORM_HAS_LARGE_MEMORY
205 // For async functions, response already sent via sendAsyncResponse().
206 // Envelope marker names renamed in #3228 -- accept legacy `__async` for
207 // one release of back-compat in case external Server forks set it.
208 bool ackAlready = (response.contains("ackOnly") && response["ackOnly"].as_bool().value_or(false))
209 || (response.contains("__async") && response["__async"].as_bool().value_or(false));
210 if (ackAlready) {
211 // Don't return response (ACK already sent by Rpc).
212 // Return a skip-envelope to prevent Server from queueing it.
213 fl::json nullResponse = fl::json::object();
214 nullResponse.set("noEnqueue", true);
215 return nullResponse;
216 }
217
218 // For sync functions, remove request ID (not needed)
219 if (request.contains("method")) {
220 fl::string methodName = request["method"].as_string().value_or("");
221 mAsyncRequests.erase(methodName);
222 }
223#endif
224
225#if FL_PLATFORM_HAS_LARGE_MEMORY
226 // Record result for the post-call results vector. LowMemory targets drop
227 // mResults entirely (see #3224 Tier 1B) -- RPC callers there receive
228 // results directly via the response sink, not through Remote::mResults.
229 if (response.contains("result") && request.contains("method")) {
230 fl::string funcName = request["method"].as_string().value_or("");
231 recordResult(funcName, response["result"], 0, receivedAt, receivedAt, false);
232 }
233#else
234 (void)receivedAt;
235#endif
236
237 return response;
238}
int requestId
fl::Rpc mRpc
Definition remote.h:225
const char * c_str() const FL_NO_EXCEPT
fl::optional< fl::string > as_string() const FL_NO_EXCEPT
Definition json.h:443
fl::optional< i64 > as_int() const FL_NO_EXCEPT
Definition json.h:398
void set(const fl::string &key, const json &value) FL_NO_EXCEPT
Definition json.h:866
bool contains(size_t idx) const FL_NO_EXCEPT
Definition json.h:790
static json object() FL_NO_EXCEPT
Definition json.h:857
#define FL_DBG
Definition log.h:553
constexpr int type_rank< T >::value
fl::u32 millis() FL_NO_EXCEPT
Universal millisecond timer - returns milliseconds since system startup.

References fl::basic_string::c_str(), fl::basic_string::erase(), FL_DBG, fl::FL_NO_EXCEPT, fl::millis(), mRpc, fl::json::object(), requestId, fl::json::set(), and fl::type_rank< T >::value.

Referenced by Remote().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: