diff --git a/examples/Makefile b/examples/Makefile index 479c0f2e6312ad70210b228d327b436dada88db9..715ad977fdd22426f365849418c6bedb24c83c02 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -3,7 +3,7 @@ # `wildcard ./*/` works in both linux and linux/wine, while `wildcard */` enumerates nothing under wine SUBDIRS = $(sort $(dir $(wildcard ./*/))) -SUBDIRS:=$(filter-out ./ ./CC3200/ ./ESP8266_RTOS/ ./MSP432/ ./NXP_K64/ ./PIC32/ ./STM32F4_CC3100/, $(SUBDIRS)) +SUBDIRS:=$(filter-out ./ ./CC3200/ ./ESP8266_RTOS/ ./MSP432/ ./NXP_K64/ ./PIC32/ ./STM32F4_CC3100/ ./mbed/, $(SUBDIRS)) ifeq ($(OS), Windows_NT) SUBDIRS:=$(filter-out ./load_balancer/ ./netcat/ ./raspberry_pi_mjpeg_led/ ./captive_dns_server/, $(SUBDIRS)) diff --git a/examples/mbed/MACROS.txt b/examples/mbed/MACROS.txt new file mode 100644 index 0000000000000000000000000000000000000000..699a20099988fe1363cc159aebf1b265cfb8e17b --- /dev/null +++ b/examples/mbed/MACROS.txt @@ -0,0 +1 @@ +MG_NET_IF=MG_NET_IF_SIMPLELINK diff --git a/examples/mbed/main.cpp b/examples/mbed/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0c053c92287ca2bb2400b63c3b31dc4f4e3a1 --- /dev/null +++ b/examples/mbed/main.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#include "mbed.h" +#include "mongoose.h" + +#define WIFI_STA_SSID "yourssid" +#define WIFI_STA_PASS "yourpass" + +#define HTTP_SERVER_PORT "80" + +DigitalOut led_green(LED1); +DigitalOut led_red(LED3); +DigitalOut led_blue(LED4); + +void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { + switch (ev) { + case MG_EV_ACCEPT: { + char addr[32]; + mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), + MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); + printf("%p: Connection from %s\r\n", nc, addr); + break; + } + case MG_EV_HTTP_REQUEST: { + struct http_message *hm = (struct http_message *) ev_data; + char addr[32]; + mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), + MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); + printf("%p: %.*s %.*s\r\n", nc, (int) hm->method.len, hm->method.p, + (int) hm->uri.len, hm->uri.p); + mg_send_response_line(nc, 200, + "Content-Type: text/html\r\n" + "Connection: close"); + mg_printf(nc, + "\r\n<h1>Hello, %s!</h1>\r\n" + "You asked for %.*s\r\n", + addr, (int) hm->uri.len, hm->uri.p); + nc->flags |= MG_F_SEND_AND_CLOSE; + + led_blue = !led_blue; + break; + } + case MG_EV_CLOSE: { + printf("%p: Connection closed\r\n", nc); + break; + } + } +} + +int main() { + struct mg_mgr mgr; + + Serial pc(SERIAL_TX, SERIAL_RX, 115200); + printf("Mongoose demo\n"); + led_green = 1; /* off */ + led_blue = 1; /* off */ + + SimpleLinkInterface wifi(PG_10, PG_11, SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); + + sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); + + wifi.connect(WIFI_STA_SSID, WIFI_STA_PASS); + + const char *ip = wifi.get_ip_address(); + const char *gw = wifi.get_gateway(); + const char *mac = wifi.get_mac_address(); + printf("IP address is: %s\n", ip ? ip : "No IP"); + printf("GW address is: %s\n", gw ? gw : "No IP"); + printf("MAC address is: %s\n", mac ? mac : "No MAC"); + + mg_mgr_init(&mgr, NULL); + + const char *err; + struct mg_bind_opts opts = {}; + opts.error_string = &err; + mg_connection *nc = mg_bind_opt(&mgr, HTTP_SERVER_PORT, ev_handler, opts); + if (nc == NULL) { + printf("Failed to create listener: %s\r\n", err); + led_red = 0; /* on */ + return 1; + } + mg_set_protocol_http_websocket(nc); + printf("Server address: http://%s:%s\n", ip, HTTP_SERVER_PORT); + + while (true) { + mg_mgr_poll(&mgr, 1000); + led_green = !led_green; + } +} diff --git a/examples/mbed/mbed-os.lib b/examples/mbed/mbed-os.lib new file mode 100644 index 0000000000000000000000000000000000000000..4bd628cf508a9642b3123028fc3bbb0ddbaaadea --- /dev/null +++ b/examples/mbed/mbed-os.lib @@ -0,0 +1 @@ +https://github.com/ARMmbed/mbed-os/#f5fb485dcd4e6ed8d913d61da453ae04b804ec11 diff --git a/examples/mbed/mongoose b/examples/mbed/mongoose new file mode 120000 index 0000000000000000000000000000000000000000..c25bddb6dd4666c6eb8cc92e33f1d60f64c3162b --- /dev/null +++ b/examples/mbed/mongoose @@ -0,0 +1 @@ +../.. \ No newline at end of file diff --git a/examples/mbed/simplelink.lib b/examples/mbed/simplelink.lib new file mode 100644 index 0000000000000000000000000000000000000000..3b3a2cd4813458f11e66319fd2ce4d53eedbb804 --- /dev/null +++ b/examples/mbed/simplelink.lib @@ -0,0 +1 @@ +https://github.com/cesanta/simplelink_mbed/#d1d93d105d02e95607f366d268d16088beca1fb5 diff --git a/mongoose.h b/mongoose.h index 29d1bfabe62f4cd2f792c7469a750aa57edefdac..0a4aefacb76932218cdc79b7969d9dd8bfa39d0a 100644 --- a/mongoose.h +++ b/mongoose.h @@ -794,6 +794,10 @@ struct timeval { #if MG_NET_IF == MG_NET_IF_SIMPLELINK +#define MG_SIMPLELINK_NO_OSI 1 + +#include <simplelink.h> + typedef int sock_t; #define INVALID_SOCKET (-1)