Statistics
| Branch: | Tag: | Revision:

mongoose / examples / settings_panel_for_a_device / settings_panel.c @ eaef5bd1

History | View | Annotate | Download (2.81 KB)

1
/*
2
 * Copyright (c) 2015 Cesanta Software Limited
3
 * All rights reserved
4
 */
5

    
6
#include "mongoose.h"
7

    
8
struct device_settings {
9
  char setting1[100];
10
  char setting2[100];
11
  char setting3[4];
12
};
13

    
14
static const char *s_http_port = "8000";
15
static struct mg_serve_http_opts s_http_server_opts;
16
static struct device_settings s_settings = {"value1", "value2", ""};
17

    
18
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
19
  /* Get form variables and store settings values */
20
  mg_get_http_var(&hm->body, "setting1", s_settings.setting1,
21
                  sizeof(s_settings.setting1));
22
  mg_get_http_var(&hm->body, "setting2", s_settings.setting2,
23
                  sizeof(s_settings.setting2));
24
  mg_get_http_var(&hm->body, "setting3", s_settings.setting3,
25
                  sizeof(s_settings.setting3));
26

    
27
  printf("Settings updated to '%s' '%s' '%s'\n", s_settings.setting1,
28
         s_settings.setting2, s_settings.setting3);
29
  /* Send response */
30
  mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%.*s",
31
            (unsigned long) hm->body.len, (int) hm->body.len, hm->body.p);
32
}
33

    
34
static void handle_ssi_call(struct mg_connection *nc, const char *param) {
35
  if (strcmp(param, "setting1") == 0) {
36
    mg_printf_html_escape(nc, "%s", s_settings.setting1);
37
  } else if (strcmp(param, "setting2") == 0) {
38
    mg_printf_html_escape(nc, "%s", s_settings.setting2);
39
  } else if (strcmp(param, "setting3_is_one") == 0) {
40
    if (strcmp(s_settings.setting3, "one") == 0) mg_printf(nc, "checked");
41
  } else if (strcmp(param, "setting3_is_two") == 0) {
42
    if (strcmp(s_settings.setting3, "two") == 0) mg_printf(nc, "checked");
43
  }
44
}
45

    
46
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
47
  struct http_message *hm = (struct http_message *) ev_data;
48

    
49
  switch (ev) {
50
    case MG_EV_HTTP_REQUEST:
51
      if (mg_vcmp(&hm->uri, "/save") == 0) {
52
        handle_save(nc, hm); /* Handle RESTful call */
53
      } else {
54
        mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */
55
      }
56
      break;
57
    case MG_EV_SSI_CALL:
58
      handle_ssi_call(nc, ev_data);
59
      break;
60
    default:
61
      break;
62
  }
63
}
64

    
65
int main(int argc, char *argv[]) {
66
  struct mg_mgr mgr;
67
  struct mg_connection *nc;
68
  char *p, path[512];
69

    
70
  mg_mgr_init(&mgr, NULL);
71
  nc = mg_bind(&mgr, s_http_port, ev_handler);
72
  mg_set_protocol_http_websocket(nc);
73
  s_http_server_opts.document_root = "./web_root";
74
  s_http_server_opts.auth_domain = "example.com";
75
  // mgr.hexdump_file = "/dev/stdout";
76

    
77
  /* If our current directory */
78
  if (argc > 0 && (p = strrchr(argv[0], '/'))) {
79
    snprintf(path, sizeof(path), "%.*s/web_root", (int) (p - argv[0]), argv[0]);
80
    s_http_server_opts.document_root = path;
81
  }
82

    
83
  printf("Starting device configurator on port %s\n", s_http_port);
84
  for (;;) {
85
    mg_mgr_poll(&mgr, 1000);
86
  }
87
  mg_mgr_free(&mgr);
88

    
89
  return 0;
90
}