Statistics
| Branch: | Tag: | Revision:

mongoose / examples / NXP_K64 / main.c @ eaef5bd1

History | View | Annotate | Download (4.29 KB)

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

    
6
#include "mongoose.h"
7

    
8
#include <stdio.h>
9
#include <stdlib.h>
10

    
11
#include "lwip/dhcp.h"
12
#include "lwip/init.h"
13
#include "lwip/netif.h"
14
#include "lwip/timers.h"
15
#include "netif/etharp.h"
16

    
17
#include "ethernetif.h"
18
#include "board.h"
19
#include "fsl_gpio.h"
20
#include "fsl_port.h"
21

    
22
#include "pin_mux.h"
23

    
24
/* IP address configuration. */
25
#define USE_DHCP 1 /* For static IP, set to 0 and configure options below */
26
#if !USE_DHCP
27
#define STATIC_IP "192.168.0.111"
28
#define STATIC_NM "255.255.255.0"
29
#define STATIC_GW "192.168.0.1"
30
#endif
31

    
32
void gpio_init() {
33
  CLOCK_EnableClock(kCLOCK_PortA);
34
  CLOCK_EnableClock(kCLOCK_PortB);
35
  CLOCK_EnableClock(kCLOCK_PortE);
36
  PORT_SetPinMux(BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN,
37
                 kPORT_MuxAsGpio);
38
  LED_RED_INIT(0);
39
  PORT_SetPinMux(BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN,
40
                 kPORT_MuxAsGpio);
41
  LED_GREEN_INIT(0);
42
  PORT_SetPinMux(BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN,
43
                 kPORT_MuxAsGpio);
44
  LED_BLUE_INIT(0);
45
  LED_BLUE_OFF();
46
}
47

    
48
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
49
  if (ev == MG_EV_POLL) return;
50
  /* printf("ev %d\r\n", ev); */
51
  switch (ev) {
52
    case MG_EV_ACCEPT: {
53
      char addr[32];
54
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
55
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
56
      printf("%p: Connection from %s\r\n", nc, addr);
57
      break;
58
    }
59
    case MG_EV_HTTP_REQUEST: {
60
      struct http_message *hm = (struct http_message *) ev_data;
61
      char addr[32];
62
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
63
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
64
      printf("%p: %.*s %.*s\r\n", nc, (int) hm->method.len, hm->method.p,
65
             (int) hm->uri.len, hm->uri.p);
66
      mg_send_response_line(nc, 200,
67
                            "Content-Type: text/html\r\n"
68
                            "Connection: close");
69
      mg_printf(nc,
70
                "\r\n<h1>Hello, %s!</h1>\r\n"
71
                "You asked for %.*s\r\n",
72
                addr, (int) hm->uri.len, hm->uri.p);
73
      nc->flags |= MG_F_SEND_AND_CLOSE;
74
      LED_BLUE_TOGGLE();
75
      break;
76
    }
77
    case MG_EV_CLOSE: {
78
      printf("%p: Connection closed\r\n", nc);
79
      break;
80
    }
81
  }
82
}
83

    
84
/*
85
 * This is a callback invoked by Mongoose to signal that a poll is needed soon.
86
 * Since we're in a tight polling loop anyway (see below), we don't need to do
87
 * anything.
88
 */
89
void mg_lwip_mgr_schedule_poll(struct mg_mgr *mgr) {
90
}
91

    
92
int main(void) {
93
  struct netif eth0;
94
  MPU_Type *base = MPU;
95
  BOARD_InitPins();
96
  BOARD_BootClockRUN();
97
  BOARD_InitDebugConsole();
98
  /* Disable MPU. */
99
  base->CESR &= ~MPU_CESR_VLD_MASK;
100
  lwip_init();
101
  gpio_init();
102
  LED_RED_ON();
103
  printf("Waiting for link...\r\n");
104
#if USE_DHCP
105
  netif_add(&eth0, NULL, NULL, NULL, NULL, ethernetif_init, ethernet_input);
106
  printf("Waiting for DHCP...\r\n");
107
  LED_GREEN_ON();
108
  dhcp_start(&eth0);
109
  u8_t os = 0xff, ds;
110
  do {
111
    ds = eth0.dhcp->state;
112
    if (ds != os) {
113
      printf("  DHCP state: %d\r\n", ds);
114
      os = ds;
115
    }
116
    sys_check_timeouts();
117
  } while (ds != DHCP_BOUND);
118
  printf("DHCP bound.\r\n");
119
#else
120
  ip_addr_t ip, nm, gw;
121
  if (!ipaddr_aton(STATIC_IP, &ip) || !ipaddr_aton(STATIC_NM, &nm) ||
122
      !ipaddr_aton(STATIC_GW, &gw)) {
123
    printf("Invalid static IP configuration.\r\n");
124
    return 1;
125
  } else {
126
    netif_add(&eth0, &ip, &nm, &gw, NULL, ethernetif_init, ethernet_input);
127
    netif_set_up(&eth0);
128
  }
129
#endif
130
  netif_set_default(&eth0);
131
  printf("Setting up HTTP server...\r\n");
132

    
133
  struct mg_mgr mgr;
134
  mg_mgr_init(&mgr, NULL);
135

    
136
  const char *err;
137
  struct mg_bind_opts opts = {};
138
  opts.error_string = &err;
139
  struct mg_connection *nc = mg_bind_opt(&mgr, "80", ev_handler, opts);
140
  if (nc == NULL) {
141
    printf("Failed to create listener: %s\r\n", err);
142
    LED_RED_ON();
143
    LED_GREEN_OFF();
144
    return 1;
145
  }
146
  mg_set_protocol_http_websocket(nc);
147
  printf("Server address: http://%s/\r\n", ipaddr_ntoa(&eth0.ip_addr));
148
  LED_RED_OFF();
149
  LED_GREEN_ON();
150

    
151
  while (1) {
152
    sys_check_timeouts();
153
    mg_mgr_poll(&mgr, 0);
154
  }
155

    
156
  return 0;
157
}