Statistics
| Branch: | Tag: | Revision:

mongoose / examples / NXP_LPC4088 / LPCXpresso / Mongoose_LPC4088_QSB_BM / example / src / main.c @ eaef5bd1

History | View | Annotate | Download (5.04 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
#include <time.h>
11

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

    
19
#include "board.h"
20
#include "lpc_phy.h"
21
#include "arch/lpc17xx_40xx_emac.h"
22
#include "arch/lpc_arch.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 ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
33
  if (ev == MG_EV_POLL) return;
34
  /* printf("ev %d\r\n", ev); */
35
  switch (ev) {
36
    case MG_EV_ACCEPT: {
37
      char addr[32];
38
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
39
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
40
      printf("%p: Connection from %s\r\n", nc, addr);
41
      break;
42
    }
43
    case MG_EV_HTTP_REQUEST: {
44
      struct http_message *hm = (struct http_message *) ev_data;
45
      char addr[32];
46
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
47
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
48
      printf("%p: %.*s %.*s\r\n", nc, (int) hm->method.len, hm->method.p,
49
             (int) hm->uri.len, hm->uri.p);
50
      mg_send_response_line(nc, 200,
51
                            "Content-Type: text/html\r\n"
52
                            "Connection: close");
53
      mg_printf(nc,
54
                "\r\n<h1>Hello, %s!</h1>\r\n"
55
                "You asked for %.*s\r\n",
56
                addr, (int) hm->uri.len, hm->uri.p);
57
      nc->flags |= MG_F_SEND_AND_CLOSE;
58
      Board_LED_Toggle(2);
59
      break;
60
    }
61
    case MG_EV_CLOSE: {
62
      printf("%p: Connection closed\r\n", nc);
63
      break;
64
    }
65
  }
66
}
67

    
68
void handle_eth(struct netif *eth_if) {
69
  /* PHY link status. */
70
  uint32_t status = lpcPHYStsPoll();
71

    
72
  if (status & PHY_LINK_CHANGED) {
73
    if (status & PHY_LINK_CONNECTED) {
74
      Board_LED_Set(0, true);
75

    
76
      if (status & PHY_LINK_SPEED100) {
77
        Chip_ENET_Set100Mbps(LPC_ETHERNET);
78
      } else {
79
        Chip_ENET_Set10Mbps(LPC_ETHERNET);
80
      }
81
      if (status & PHY_LINK_FULLDUPLX) {
82
        Chip_ENET_SetFullDuplex(LPC_ETHERNET);
83
      } else {
84
        Chip_ENET_SetHalfDuplex(LPC_ETHERNET);
85
      }
86

    
87
      netif_set_link_up(eth_if);
88
      printf("Link up\r\n");
89
    } else {
90
      Board_LED_Set(0, false);
91
      netif_set_link_down(eth_if);
92
      printf("Link down\r\n");
93
    }
94
  }
95

    
96
  Board_LED_Set(1, (eth_if->dhcp->state == DHCP_BOUND));
97

    
98
  /* Handle packets as part of this loop, not in the IRQ handler */
99
  lpc_enetif_input(eth_if);
100

    
101
  /* Free TX buffers that are done sending */
102
  lpc_tx_reclaim(eth_if);
103
}
104

    
105
int gettimeofday(struct timeval *tv, void *tzvp) {
106
  tv->tv_sec = time(NULL);
107
  tv->tv_usec = 0;
108
  return 0;
109
}
110

    
111
/*
112
 * This is a callback invoked by Mongoose to signal that a poll is needed soon.
113
 * Since we're in a tight polling loop anyway (see below), we don't need to do
114
 * anything.
115
 */
116
void mg_lwip_mgr_schedule_poll(struct mg_mgr *mgr) {
117
}
118

    
119
int main(void) {
120
  struct netif eth0;
121

    
122
  SystemCoreClockUpdate();
123
  Board_Init();
124
  SysTick_Enable(1);
125

    
126
  lwip_init();
127

    
128
  Board_LED_Set(0, false); /* Link state */
129
  Board_LED_Set(1, false); /* DHCP state */
130
  Board_LED_Set(2, false); /* HTTP request activity indicator */
131
  Board_LED_Set(3, false); /* Error indicator */
132

    
133
#if USE_DHCP
134
  netif_add(&eth0, NULL, NULL, NULL, NULL, lpc_enetif_init, ethernet_input);
135
  printf("Waiting for DHCP...\r\n");
136
  dhcp_start(&eth0);
137
  u8_t os = 0xff, ds;
138
  do {
139
    ds = eth0.dhcp->state;
140
    if (ds != os) {
141
      printf("  DHCP state: %d\r\n", ds);
142
      os = ds;
143
    }
144
    handle_eth(&eth0);
145
    sys_check_timeouts();
146
  } while (ds != DHCP_BOUND);
147
  printf("DHCP bound.\r\n");
148
#else
149
  ip_addr_t ip, nm, gw;
150
  if (!ipaddr_aton(STATIC_IP, &ip) || !ipaddr_aton(STATIC_NM, &nm) ||
151
      !ipaddr_aton(STATIC_GW, &gw)) {
152
    printf("Invalid static IP configuration.\r\n");
153
    Board_LED_Set(3, true);
154
    return 1;
155
  } else {
156
    netif_add(&eth0, &ip, &nm, &gw, NULL, lpc_enetif_init, ethernet_input);
157
    netif_set_up(&eth0);
158
  }
159
#endif
160
  netif_set_default(&eth0);
161
  printf("Setting up HTTP server...\r\n");
162

    
163
  struct mg_mgr mgr;
164
  mg_mgr_init(&mgr, NULL);
165

    
166
  const char *err;
167
  struct mg_bind_opts opts = {};
168
  opts.error_string = &err;
169
  struct mg_connection *nc = mg_bind_opt(&mgr, "80", ev_handler, opts);
170
  if (nc == NULL) {
171
    printf("Failed to create listener: %s\r\n", err);
172
    Board_LED_Set(3, true);
173
    return 1;
174
  }
175
  mg_set_protocol_http_websocket(nc);
176
  printf("Server address: http://%s/\r\n", ipaddr_ntoa(&eth0.ip_addr));
177

    
178
  while (1) {
179
    /* Ethernet link status, low level input. */
180
    handle_eth(&eth0);
181

    
182
    /* LWIP timers - ARP, DHCP, TCP, etc. */
183
    sys_check_timeouts();
184

    
185
    /* Mongoose poll */
186
    mg_mgr_poll(&mgr, 0);
187
  }
188

    
189
  return 0;
190
}