Skip to content

Commit

Permalink
Merge pull request #2681 from cesanta/ub
Browse files Browse the repository at this point in the history
keep embedded-friendly config API
  • Loading branch information
scaprile authored Apr 3, 2024
2 parents 5d0d01c + 7876d46 commit 9276f76
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 2 additions & 4 deletions examples/esp32/uart-bridge/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

const char *s_listening_url = "http://0.0.0.0:80";

#if 0
char *config_read(void) {
return mg_file_read(&mg_fs_posix, FS_ROOT "/config.json", NULL);
struct mg_str config_read(void) {
return mg_file_read(&mg_fs_posix, FS_ROOT "/config.json");
}
#endif

void config_write(struct mg_str config) {
mg_file_write(&mg_fs_posix, FS_ROOT "/config.json", config.ptr, config.len);
Expand Down
7 changes: 6 additions & 1 deletion examples/uart-bridge/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct state {
void uart_init(int tx, int rx, int baud);
int uart_read(void *buf, size_t len);
void uart_write(const void *buf, size_t len);
struct mg_str config_read(void);
void config_write(struct mg_str config);

// Let users define their own UART API. If they don't, use a dummy one
Expand Down Expand Up @@ -53,6 +54,10 @@ int uart_read(void *buf, size_t len) {
#endif
}

struct mg_str config_read(void) {
return mg_file_read(&mg_fs_posix, "config.json");
}

void config_write(struct mg_str config) {
mg_file_write(&mg_fs_posix, "config.json", config.ptr, config.len);
}
Expand Down Expand Up @@ -182,7 +187,7 @@ static void config_apply(struct mg_str s) {
// HTTP request handler function
void uart_bridge_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN && c->is_listening) {
struct mg_str config = mg_file_read(&mg_fs_posix, "config.json");
struct mg_str config = config_read();
if (config.ptr != NULL) config_apply(config);
free((char *) config.ptr);
s_state.tcp.url = strdup(DEFAULT_TCP);
Expand Down

0 comments on commit 9276f76

Please sign in to comment.