| 65 | // ============================================================================= |
| 66 | |
| 67 | json Rpc::handle(const json& request) { |
| 68 | // Extract method name |
| 69 | if (!request.contains("method")) { |
| 70 | FL_ERROR("RPC: Invalid Request - missing 'method' field"); |
| 71 | return detail::makeJsonRpcError(-32600, "Invalid Request: missing 'method'", request["id"]); |
| 72 | } |
| 73 | |
| 74 | auto methodOpt = request["method"].as_string(); |
| 75 | if (!methodOpt.has_value()) { |
| 76 | FL_ERROR("RPC: Invalid Request - 'method' must be a string"); |
| 77 | return detail::makeJsonRpcError(-32600, "Invalid Request: 'method' must be a string", request["id"]); |
| 78 | } |
| 79 | fl::string methodName = methodOpt.value(); |
| 80 | |
| 81 | #if FL_PLATFORM_HAS_LARGE_MEMORY |
| 82 | // Handle built-in rpc.discover method. Gated on Low-memory targets because |
| 83 | // it transitively anchors `TypedSchemaGenerator<Sig>::params()` for every |
| 84 | // registered method (each 800+ B per signature) plus the schema-building |
| 85 | // JSON tree. The LPC8xx / AVR-class JSON-RPC bring-up surface treats the |
| 86 | // RPC catalog as a known constant -- callers don't introspect at runtime. |
| 87 | // See FastLED #3081. |
| 88 | if (methodName == "rpc.discover") { |
| 89 | json response = json::object(); |
| 90 | response.set("jsonrpc", "2.0"); |
| 91 | response.set("result", schema()); |
| 92 | if (request.contains("id")) { |
| 93 | response.set("id", request["id"]); |
| 94 | } |
| 95 | return response; |
| 96 | } |
| 97 | #endif |
| 98 | |
| 99 | // Look up the method |
| 100 | auto it = mRegistry.find(methodName); |
| 101 | if (it == mRegistry.end()) { |
| 102 | FL_WARN("RPC: Method not found: " << methodName.c_str()); |
| 103 | return detail::makeJsonRpcError(-32601, "Method not found: " + methodName, request["id"]); |
| 104 | } |
| 105 | |
| 106 | // Extract params (default to empty array) |
| 107 | json params = request.contains("params") ? request["params"] : json::parse("[]"); |
| 108 | if (!params.is_array()) { |
| 109 | FL_ERROR("RPC: Invalid params - must be an array for method: " << methodName.c_str()); |
| 110 | return detail::makeJsonRpcError(-32602, "Invalid params: must be an array", request["id"]); |
| 111 | } |
| 112 | |
| 113 | // Check if this is an async function |
| 114 | const detail::RpcEntry& entry = it->second; |
| 115 | bool isAsync = (entry.mMode == RpcMode::ASYNC || entry.mMode == RpcMode::ASYNC_STREAM); |
| 116 | |
| 117 | // Check if this is a response-aware function (uses ResponseSend&) |
| 118 | bool isResponseAware = entry.mIsResponseAware; |
| 119 | |
| 120 | // For async functions, send ACK immediately |
| 121 | if (isAsync && mResponseSink && request.contains("id")) { |
| 122 | json ack = json::object(); |
| 123 | ack.set("jsonrpc", "2.0"); |
| 124 | ack.set("id", request["id"]); |
no test coverage detected