Statistics
| Branch: | Tag: | Revision:

mongoose / examples / ESP8266_RTOS / user / user_main.c @ eaef5bd1

History | View | Annotate | Download (2.84 KB)

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

    
6
#include "esp_common.h"
7

    
8
#include "mongoose.h"
9

    
10
#define AP_SSID "Mongoose"
11
#define AP_PASS "Mongoose"
12
#define AP_CHAN 9
13
#define MG_LISTEN_ADDR "80"
14

    
15
#define MG_TASK_STACK_SIZE 4096
16
#define MGOS_TASK_PRIORITY 1
17

    
18
void uart_div_modify(int uart_no, unsigned int freq);
19

    
20
void ev_handler(struct mg_connection *nc, int ev, void *p) {
21
  static const char *reply_fmt =
22
      "HTTP/1.0 200 OK\r\n"
23
      "Connection: close\r\n"
24
      "Content-Type: text/plain\r\n"
25
      "\r\n"
26
      "Hello %s\n";
27

    
28
  switch (ev) {
29
    case MG_EV_ACCEPT: {
30
      char addr[32];
31
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
32
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
33
      printf("Connection %p from %s\n", nc, addr);
34
      break;
35
    }
36
    case MG_EV_HTTP_REQUEST: {
37
      char addr[32];
38
      struct http_message *hm = (struct http_message *) p;
39
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
40
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
41
      printf("HTTP request from %s: %.*s %.*s\n", addr, (int) hm->method.len,
42
             hm->method.p, (int) hm->uri.len, hm->uri.p);
43
      mg_printf(nc, reply_fmt, addr);
44
      nc->flags |= MG_F_SEND_AND_CLOSE;
45
      break;
46
    }
47
    case MG_EV_CLOSE: {
48
      printf("Connection %p closed\n", nc);
49
      break;
50
    }
51
  }
52
}
53

    
54
void setup_ap(void) {
55
  int off = 0;
56
  struct ip_info info;
57
  struct softap_config cfg;
58

    
59
  wifi_set_opmode_current(SOFTAP_MODE);
60
  memset(&cfg, 0, sizeof(cfg));
61
  strcpy((char *) cfg.ssid, AP_SSID);
62
  strcpy((char *) cfg.password, AP_PASS);
63
  cfg.ssid_len = strlen((const char *) cfg.ssid);
64
  cfg.authmode =
65
      strlen((const char *) cfg.password) ? AUTH_WPA2_PSK : AUTH_OPEN;
66
  cfg.channel = AP_CHAN;
67
  cfg.ssid_hidden = 0;
68
  cfg.max_connection = 10;
69
  cfg.beacon_interval = 100; /* ms */
70

    
71
  printf("Setting up AP '%s' on channel %d\n", cfg.ssid, cfg.channel);
72
  wifi_softap_set_config_current(&cfg);
73
  wifi_softap_dhcps_stop();
74
  wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &off);
75
  wifi_softap_dhcps_start();
76
  wifi_get_ip_info(SOFTAP_IF, &info);
77
  printf("WiFi AP: SSID %s, channel %d, IP " IPSTR "\n", cfg.ssid, cfg.channel,
78
         IP2STR(&info.ip));
79
}
80

    
81
static void mg_task(void *arg) {
82
  struct mg_mgr mgr;
83
  struct mg_connection *nc;
84

    
85
  printf("SDK version: %s\n", system_get_sdk_version());
86
  setup_ap();
87

    
88
  mg_mgr_init(&mgr, NULL);
89

    
90
  nc = mg_bind(&mgr, MG_LISTEN_ADDR, ev_handler);
91
  if (nc == NULL) {
92
    printf("Error setting up listener!\n");
93
    return;
94
  }
95
  mg_set_protocol_http_websocket(nc);
96

    
97
  while (1) {
98
    mg_mgr_poll(&mgr, 1000);
99
  }
100
}
101

    
102
xTaskHandle s_mg_task_handle;
103

    
104
void user_init(void) {
105
  uart_div_modify(0, UART_CLK_FREQ / 115200);
106

    
107
  xTaskCreate(mg_task, (const signed char *) "mongoose", MG_TASK_STACK_SIZE,
108
              NULL, MGOS_TASK_PRIORITY, &s_mg_task_handle);
109
}