Statistics
| Branch: | Tag: | Revision:

mongoose / examples / nRF51 / http / main.c @ eaef5bd1

History | View | Annotate | Download (2.61 KB)

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

    
6
#include "bleconfig.h"
7
#include "myboard.h"
8

    
9
#include "mongoose.h"
10

    
11
/*
12
 * This is a callback invoked by Mongoose to signal that a poll is needed soon.
13
 * Since we're in a tight polling loop anyway (see below), we don't need to do
14
 * anything.
15
 */
16
void mg_lwip_mgr_schedule_poll(struct mg_mgr *mgr) {
17
}
18

    
19
// Define an event handler function
20
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
21
  if (ev == MG_EV_POLL) return;
22
  /* printf("ev %d\r\n", ev); */
23
  switch (ev) {
24
    case MG_EV_ACCEPT: {
25
      char addr[32];
26
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
27
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
28
      printf("%p: Connection from %s\r\n", nc, addr);
29
      break;
30
    }
31
    case MG_EV_HTTP_REQUEST: {
32
      struct http_message *hm = (struct http_message *) ev_data;
33
      char addr[32];
34
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
35
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
36
      printf("%p: %.*s %.*s\r\n", nc, (int) hm->method.len, hm->method.p,
37
             (int) hm->uri.len, hm->uri.p);
38
      mg_send_response_line(nc, 200,
39
                            "Content-Type: text/html\r\n"
40
                            "Connection: close");
41
      mg_printf(nc,
42
                "\r\n<h1>Hello, %s!</h1>\r\n"
43
                "You asked for %.*s\r\n",
44
                addr, (int) hm->uri.len, hm->uri.p);
45
      nc->flags |= MG_F_SEND_AND_CLOSE;
46
      LEDS_INVERT(LED_THREE);
47
      break;
48
    }
49
    case MG_EV_CLOSE: {
50
      printf("%p: Connection closed\r\n", nc);
51
      break;
52
    }
53
  }
54
}
55

    
56
/**
57
 * @brief Function for application main entry.
58
 */
59
int main(void) {
60
  cs_log_set_file(stdout);
61

    
62
  bleconfig_init();
63

    
64
  {
65
    struct mg_mgr mgr;
66

    
67
    mg_mgr_init(&mgr, NULL);  // Initialize event manager object
68

    
69
    // Note that many connections can be added to a single event manager
70
    // Connections can be created at any point, e.g. in event handler function
71
    const char *err;
72
    struct mg_bind_opts opts;
73
    struct mg_connection *nc = NULL;
74
    memset(&opts, 0x00, sizeof(opts));
75
    opts.error_string = &err;
76
    nc = mg_bind_opt(
77
        &mgr, "80", ev_handler,
78
        opts);  // Create listening connection and add it to the event manager
79
    if (nc == NULL) {
80
      printf("Failed to create listener: %s\n", err);
81
      return 1;
82
    }
83
    mg_set_protocol_http_websocket(nc);
84

    
85
    for (;;) {  // Start infinite event loop
86
      bleconfig_poll();
87
      mg_mgr_poll(&mgr, 0);
88
    }
89
  }
90
}