Skip to content

Commit

Permalink
Hackishly make the Websocket API available via REST
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAbata committed Apr 9, 2024
1 parent 105f802 commit 41e6fff
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 12 deletions.
10 changes: 6 additions & 4 deletions include/system/websocket_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ struct websocket_client {
typedef struct websocket_client websocket_client_t;

typedef int websocket_client_handle_t;
typedef command_err_t (*websocket_command_func_t)(cJSON* command, cJSON* response,
websocket_client_t* client);
typedef command_err_t (*websocket_command_func_t
)(cJSON* command, cJSON* response, websocket_client_t* client);

struct websocket_command {
const char* command;
Expand All @@ -37,13 +37,15 @@ enum {
WS_BROADCAST_SYSTEM = (1 << 1),
};

esp_err_t rest_get_handler(httpd_req_t* req);
esp_err_t websocket_handler(httpd_req_t* req);
esp_err_t websocket_open_fd(httpd_handle_t hd, int sockfd);
void websocket_close_fd(httpd_handle_t hd, int sockfd);

void websocket_register_command(const websocket_command_t* command);
void websocket_run_command(const char* command, cJSON* data, cJSON* response,
websocket_client_t* client);
void websocket_run_command(
const char* command, cJSON* data, cJSON* response, websocket_client_t* client
);
void websocket_run_commands(cJSON* commands, cJSON* response, websocket_client_t* client);

// Broadcast update triggers:
Expand Down
53 changes: 45 additions & 8 deletions src/system/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,48 @@
static const char* TAG = "http_server";
static httpd_handle_t _server = NULL;

static const httpd_uri_t routes[] = { {
.uri = "/",
.method = HTTP_GET,
.handler = &websocket_handler,
.user_ctx = NULL,
.is_websocket = true,
.handle_ws_control_frames = true,
} };
static const httpd_uri_t routes[] = {
{
.uri = "/",
.method = HTTP_GET,
.handler = &websocket_handler,
.user_ctx = NULL,
.is_websocket = true,
.handle_ws_control_frames = true,
},
{
.uri = "/stream",
.method = HTTP_GET,
.handler = &websocket_handler,
.user_ctx = NULL,
.is_websocket = true,
.handle_ws_control_frames = true,
},
{
.uri = "*",
.method = HTTP_GET,
.handler = &rest_get_handler,
.user_ctx = NULL,
.is_websocket = false,
.handle_ws_control_frames = false,
},
{
.uri = "*",
.method = HTTP_POST,
.handler = &rest_get_handler,
.user_ctx = NULL,
.is_websocket = false,
.handle_ws_control_frames = false,
},
{
.uri = "*",
.method = HTTP_OPTIONS,
.handler = &rest_get_handler,
.user_ctx = NULL,
.is_websocket = false,
.handle_ws_control_frames = false,
},
};

static void init_routes(httpd_handle_t server) {
for (size_t i = 0; i < sizeof(routes) / sizeof(routes[0]); i++) {
Expand All @@ -41,6 +75,9 @@ static httpd_handle_t start_webserver(void) {
config.server_port = Config.websocket_port;
}

// Allow wildcard matching:
config.uri_match_fn = httpd_uri_match_wildcard;

// Open/Close callbacks
config.open_fn = websocket_open_fd;
config.close_fn = websocket_close_fd;
Expand Down
36 changes: 36 additions & 0 deletions src/system/websocket_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,42 @@ void websocket_close_fd(httpd_handle_t hd, int sockfd) {
}
}

esp_err_t rest_get_handler(httpd_req_t* req) {
httpd_resp_set_type(req, "application/json");

// TODO: Parse out request URIs here for GET params
// TODO: Remove the client requirement on the websocket run command
// TODO: ... eugh a lot

if (req->method == HTTP_GET) {
ESP_LOGI(TAG, "Starting GET: %s", req->uri);
} else if (req->method == HTTP_POST) {
ESP_LOGI(TAG, "Starting POST: %s", req->uri);
} else if (req->method == HTTP_OPTIONS) {
httpd_resp_send(req, "", 0);
return ESP_OK;
} else {
httpd_resp_send_404(req);
return ESP_ERR_NOT_FOUND;
}

cJSON* resp = cJSON_CreateObject();

websocket_run_command(req->uri + 1, NULL, resp, NULL);

char* buf = cJSON_Print(resp);

if (buf != NULL) {
httpd_resp_send(req, buf, strlen(buf));
} else {
httpd_resp_send_500(req);
}

free(buf);
cJSON_Delete(resp);
return ESP_OK;
}

esp_err_t websocket_handler(httpd_req_t* req) {
if (req->method == HTTP_GET) {
ESP_LOGD(TAG, "This was the handshake.");
Expand Down

0 comments on commit 41e6fff

Please sign in to comment.