Commit 2633628a authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by rojer

Add MG_hello for CC3200; upload file form

Mg_hello is a simpler example for CC3200
File upload form is served on / if index.html does not exist
and always available on /upload

PUBLISHED_FROM=f844245b26febc4b952e7c52ee4519ddb542072d
parent e5ae1c2e
<?xml version="1.0" encoding="UTF-8" ?>
<?ccsproject version="1.0"?>
<projectOptions>
<deviceVariant value="Cortex M.CC3200"/>
<deviceFamily value="TMS470"/>
<deviceEndianness value="little"/>
<codegenToolVersion value="5.2.7"/>
<isElfFormat value="true"/>
<connection value="common/targetdb/connections/Stellaris_ICDI_Connection.xml"/>
<linkerCommandFile value="cc3200v1p32.cmd"/>
<rts value="libc.a"/>
<createSlaveProjects value=""/>
<templateProperties value="id=com.ti.common.project.core.emptyProjectWithMainTemplate,"/>
<isTargetManual value="false"/>
</projectOptions>
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MG_hello</name>
<comment></comment>
<projects>
<project>Mongoose</project>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.ti.ccstudio.core.ccsNature</nature>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
<linkedResources>
<link>
<name>gpio_if.c</name>
<type>1</type>
<locationURI>CC3200_SDK_ROOT/example/common/gpio_if.c</locationURI>
</link>
<link>
<name>network_common.c</name>
<type>1</type>
<locationURI>CC3200_SDK_ROOT/example/common/network_common.c</locationURI>
</link>
<link>
<name>startup_ccs.c</name>
<type>1</type>
<locationURI>CC3200_SDK_ROOT/example/common/startup_ccs.c</locationURI>
</link>
<link>
<name>wifi.c</name>
<type>1</type>
<locationURI>PARENT-2-PROJECT_LOC/wifi.c</locationURI>
</link>
<link>
<name>wifi.h</name>
<type>1</type>
<locationURI>PARENT-2-PROJECT_LOC/wifi.h</locationURI>
</link>
</linkedResources>
<variableList>
<variable>
<name>CC3200_SDK_ROOT</name>
<value>$%7BTI_PRODUCTS_DIR%7D/CC3200SDK_1.2.0/cc3200-sdk</value>
</variable>
</variableList>
</projectDescription>
//*****************************************************************************
// cc3200v1p32.cmd
//
// CCS linker configuration file for cc3200 ES 1.32.
//
// Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//*****************************************************************************
--retain=g_pfnVectors
//*****************************************************************************
// The following command line options are set as part of the CCS project.
// If you are building using the command line, or for some reason want to
// define them here, you can uncomment and modify these lines as needed.
// If you are using CCS for building, it is probably better to make any such
// modifications in your CCS project and leave this file alone.
//*****************************************************************************
//*****************************************************************************
// The starting address of the application. Normally the interrupt vectors
// must be located at the beginning of the application.
//*****************************************************************************
#define RAM_BASE 0x20004000
/* System memory map */
MEMORY
{
/* Application uses internal RAM for program and data */
SRAM_CODE (RWX) : origin = 0x20004000, length = 0x20000
SRAM_DATA (RWX) : origin = 0x20024000, length = 0x1C000
}
/* Section allocation in memory */
SECTIONS
{
.intvecs: > RAM_BASE
.init_array : > SRAM_CODE
.vtable : > SRAM_CODE
.text : > SRAM_CODE
.const : > SRAM_CODE
.cinit : > SRAM_CODE
.pinit : > SRAM_CODE
.data : > SRAM_DATA
.bss : > SRAM_DATA
.sysmem : > SRAM_DATA
.stack : > SRAM_DATA(HIGH)
}
/*
* Copyright (c) 2014-2016 Cesanta Software Limited
* All rights reserved
*/
/* Set up an AP or connect to existing WiFi network. */
#define WIFI_AP_SSID "Mongoose"
#define WIFI_AP_PASS ""
#define WIFI_AP_CHAN 6
// #define WIFI_STA_SSID "YourWiFi"
// #define WIFI_STA_PASS "YourPass"
#define MG_TASK_PRIORITY 3
#define MG_TASK_STACK_SIZE 8192
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inc/hw_types.h>
#include <inc/hw_ints.h>
#include <inc/hw_memmap.h>
#include <driverlib/gpio.h>
#include <driverlib/interrupt.h>
#include <driverlib/pin.h>
#include <driverlib/prcm.h>
#include <driverlib/rom.h>
#include <driverlib/rom_map.h>
#include <example/common/gpio_if.h>
/* Mongoose.h brings in SimpleLink support. Do not include simplelink.h. */
#include <mongoose.h>
#include <simplelink/include/device.h>
#include "wifi.h"
static const char *upload_form = "\
<h1>Upload file</h1> \
<form action='/upload' method='POST' enctype='multipart/form-data'> \
<input type='file' name='file'> \
<input type='submit' value='Upload'> \
</form>";
static struct mg_str upload_fname(struct mg_connection *nc,
struct mg_str fname) {
struct mg_str lfn;
lfn.len = fname.len + 3;
lfn.p = malloc(lfn.len);
memcpy((char *) lfn.p, "SL:", 3);
memcpy((char *) lfn.p + 3, fname.p, fname.len);
return lfn;
}
void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
LOG(LL_DEBUG, ("%p ev %d", nc, ev));
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);
LOG(LL_INFO, ("Connection %p from %s", nc, addr));
break;
}
case MG_EV_HTTP_REQUEST: {
char addr[32];
struct http_message *hm = (struct http_message *) ev_data;
cs_stat_t st;
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
LOG(LL_INFO,
("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len,
hm->method.p, (int) hm->uri.len, hm->uri.p));
if (mg_vcmp(&hm->uri, "/upload") == 0 ||
(mg_vcmp(&hm->uri, "/") == 0 &&
mg_stat("SL:index.html", &st) != 0)) {
mg_send(nc, upload_form, strlen(upload_form));
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
}
struct mg_serve_http_opts opts;
memset(&opts, 0, sizeof(opts));
opts.document_root = "SL:";
mg_serve_http(nc, hm, opts);
break;
}
case MG_EV_CLOSE: {
LOG(LL_INFO, ("Connection %p closed", nc));
break;
}
case MG_EV_HTTP_PART_BEGIN:
case MG_EV_HTTP_PART_DATA:
case MG_EV_HTTP_PART_END: {
struct mg_http_multipart_part *mp =
(struct mg_http_multipart_part *) ev_data;
if (ev == MG_EV_HTTP_PART_BEGIN) {
LOG(LL_INFO, ("Begin file upload: %s", mp->file_name));
} else if (ev == MG_EV_HTTP_PART_END) {
LOG(LL_INFO, ("End file upload: %s", mp->file_name));
}
mg_file_upload_handler(nc, ev, ev_data, upload_fname);
}
}
}
static void mg_init(struct mg_mgr *mgr) {
LOG(LL_INFO, ("MG task running"));
sl_Start(0, 0, 0);
sl_fs_init();
#if defined(WIFI_STA_SSID)
if (!wifi_setup_sta(WIFI_STA_SSID, WIFI_STA_PASS)) {
LOG(LL_ERROR, ("Error setting up WiFi station"));
}
#elif defined(WIFI_AP_SSID)
if (!wifi_setup_ap(WIFI_AP_SSID, WIFI_AP_PASS, WIFI_AP_CHAN)) {
LOG(LL_ERROR, ("Error setting up WiFi AP"));
}
#else
#error WiFi not configured
#endif
/* We don't need SimpleLink's web server. */
sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID);
const char *err = "";
struct mg_bind_opts opts;
memset(&opts, 0, sizeof(opts));
opts.error_string = &err;
struct mg_connection *nc = mg_bind(mgr, "80", mg_ev_handler);
if (nc != NULL) {
mg_set_protocol_http_websocket(nc);
} else {
LOG(LL_ERROR, ("Failed to create listener: %s", err));
}
}
#ifndef USE_TIRTOS
/* Int vector table, defined in startup_gcc.c */
extern void (*const g_pfnVectors[])(void);
#endif
int main() {
#ifndef USE_TIRTOS
MAP_IntVTableBaseSet((unsigned long) &g_pfnVectors[0]);
#endif
MAP_IntEnable(FAULT_SYSTICK);
MAP_IntMasterEnable();
PRCMCC3200MCUInit();
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);
cs_log_set_level(LL_INFO);
cs_log_set_file(stdout);
LOG(LL_INFO, ("Hello, world!"));
/* Set up the red LED. Note that amber and green cannot be used as they share
* pins with I2C. */
MAP_PRCMPeripheralClkEnable(PRCM_GPIOA1, PRCM_RUN_MODE_CLK);
MAP_PinTypeGPIO(PIN_64, PIN_MODE_0, false);
MAP_GPIODirModeSet(GPIOA1_BASE, 0x2, GPIO_DIR_MODE_OUT);
GPIO_IF_LedConfigure(LED1);
GPIO_IF_LedOn(MCU_RED_LED_GPIO);
if (VStartSimpleLinkSpawnTask(8) != 0) {
LOG(LL_ERROR, ("Failed to create SL task"));
}
if (!mg_start_task(MG_TASK_PRIORITY, MG_TASK_STACK_SIZE, mg_init)) {
LOG(LL_ERROR, ("Failed to create MG task"));
}
osi_start();
return 0;
}
/* These are FreeRTOS hooks for various life situations. */
void vApplicationMallocFailedHook() {
}
void vApplicationIdleHook() {
}
void vApplicationStackOverflowHook(OsiTaskHandle *th, signed char *tn) {
}
void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *e,
SlHttpServerResponse_t *resp) {
}
void SimpleLinkSockEventHandler(SlSockEvent_t *e) {
}
void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *e) {
LOG(LL_ERROR, ("status %d sender %d", e->EventData.deviceEvent.status,
e->EventData.deviceEvent.sender));
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<configurations XML_version="1.2" id="configurations_0">
<configuration XML_version="1.2" id="configuration_0">
<instance XML_version="1.2" desc="Stellaris In-Circuit Debug Interface" href="connections/Stellaris_ICDI_Connection.xml" id="Stellaris In-Circuit Debug Interface" xml="Stellaris_ICDI_Connection.xml" xmlpath="connections"/>
<connection XML_version="1.2" id="Stellaris In-Circuit Debug Interface">
<instance XML_version="1.2" href="drivers/cc3200_cs_icepick.xml" id="drivers" xml="cc3200_cs_icepick.xml" xmlpath="drivers"/>
<instance XML_version="1.2" href="drivers/stellaris_cs_dap.xml" id="drivers" xml="stellaris_cs_dap.xml" xmlpath="drivers"/>
<instance XML_version="1.2" href="drivers/cc3200_cortex_m4.xml" id="drivers" xml="cc3200_cortex_m4.xml" xmlpath="drivers"/>
<platform XML_version="1.2" id="platform_0">
<instance XML_version="1.2" desc="CC3200" href="devices/CC3200.xml" id="CC3200" xml="CC3200.xml" xmlpath="devices"/>
</platform>
</connection>
</configuration>
</configurations>
The 'targetConfigs' folder contains target-configuration (.ccxml) files, automatically generated based
on the device and connection settings specified in your project on the Properties > General page.
Please note that in automatic target-configuration management, changes to the project's device and/or
connection settings will either modify an existing or generate a new target-configuration file. Thus,
if you manually edit these auto-generated files, you may need to re-apply your changes. Alternatively,
you may create your own target-configuration file for this project and manage it manually. You can
always switch back to automatic target-configuration management by checking the "Manage the project's
target-configuration automatically" checkbox on the project's Properties > General page.
\ No newline at end of file
...@@ -29,22 +29,22 @@ ...@@ -29,22 +29,22 @@
<link> <link>
<name>bm222.c</name> <name>bm222.c</name>
<type>1</type> <type>1</type>
<locationURI>copy_PARENT/bm222.c</locationURI> <locationURI>PARENT-2-PROJECT_LOC/bm222.c</locationURI>
</link> </link>
<link> <link>
<name>bm222.h</name> <name>bm222.h</name>
<type>1</type> <type>1</type>
<locationURI>copy_PARENT/bm222.h</locationURI> <locationURI>PARENT-2-PROJECT_LOC/bm222.h</locationURI>
</link> </link>
<link> <link>
<name>data.c</name> <name>data.c</name>
<type>1</type> <type>1</type>
<locationURI>copy_PARENT/data.c</locationURI> <locationURI>PARENT-2-PROJECT_LOC/data.c</locationURI>
</link> </link>
<link> <link>
<name>data.h</name> <name>data.h</name>
<type>1</type> <type>1</type>
<locationURI>copy_PARENT/data.h</locationURI> <locationURI>PARENT-2-PROJECT_LOC/data.h</locationURI>
</link> </link>
<link> <link>
<name>gpio_if.c</name> <name>gpio_if.c</name>
...@@ -59,7 +59,7 @@ ...@@ -59,7 +59,7 @@
<link> <link>
<name>main.c</name> <name>main.c</name>
<type>1</type> <type>1</type>
<locationURI>copy_PARENT/main.c</locationURI> <locationURI>PARENT-2-PROJECT_LOC/main.c</locationURI>
</link> </link>
<link> <link>
<name>network_common.c</name> <name>network_common.c</name>
...@@ -74,22 +74,22 @@ ...@@ -74,22 +74,22 @@
<link> <link>
<name>tmp006.c</name> <name>tmp006.c</name>
<type>1</type> <type>1</type>
<locationURI>copy_PARENT/tmp006.c</locationURI> <locationURI>PARENT-2-PROJECT_LOC/tmp006.c</locationURI>
</link> </link>
<link> <link>
<name>tmp006.h</name> <name>tmp006.h</name>
<type>1</type> <type>1</type>
<locationURI>copy_PARENT/tmp006.h</locationURI> <locationURI>PARENT-2-PROJECT_LOC/tmp006.h</locationURI>
</link> </link>
<link> <link>
<name>wifi.c</name> <name>wifi.c</name>
<type>1</type> <type>1</type>
<locationURI>copy_PARENT/wifi.c</locationURI> <locationURI>PARENT-2-PROJECT_LOC/wifi.c</locationURI>
</link> </link>
<link> <link>
<name>wifi.h</name> <name>wifi.h</name>
<type>1</type> <type>1</type>
<locationURI>copy_PARENT/wifi.h</locationURI> <locationURI>PARENT-2-PROJECT_LOC/wifi.h</locationURI>
</link> </link>
</linkedResources> </linkedResources>
<variableList> <variableList>
...@@ -97,9 +97,5 @@ ...@@ -97,9 +97,5 @@
<name>CC3200_SDK_ROOT</name> <name>CC3200_SDK_ROOT</name>
<value>$%7BTI_PRODUCTS_DIR%7D/CC3200SDK_1.2.0/cc3200-sdk</value> <value>$%7BTI_PRODUCTS_DIR%7D/CC3200SDK_1.2.0/cc3200-sdk</value>
</variable> </variable>
<variable>
<name>copy_PARENT</name>
<value>file:/Z:/cesanta/dev/mongoose/examples/CC3200</value>
</variable>
</variableList> </variableList>
</projectDescription> </projectDescription>
...@@ -8,29 +8,28 @@ ...@@ -8,29 +8,28 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
/* Driverlib includes */ #include <inc/hw_types.h>
#include "hw_types.h" #include <inc/hw_ints.h>
#include <inc/hw_memmap.h>
#include "hw_ints.h"
#include "hw_memmap.h" #include <driverlib/gpio.h>
#include "interrupt.h" #include <driverlib/interrupt.h>
#include "pin.h" #include <driverlib/pin.h>
#include "prcm.h" #include <driverlib/prcm.h>
#include "rom.h" #include <driverlib/rom.h>
#include "rom_map.h" #include <driverlib/rom_map.h>
#include "uart.h" #include <driverlib/uart.h>
#include "utils.h" #include <driverlib/utils.h>
#include "gpio.h" #include <example/common/gpio_if.h>
#include "gpio_if.h" #include <example/common/i2c_if.h>
#include "i2c_if.h"
/* Mongoose.h brings in SimpleLink support. Do not include simplelink.h. */
#include "mongoose.h" #include <mongoose.h>
#include "simplelink.h"
#include "device.h" #include <simplelink/include/device.h>
#include "data.h" #include "data.h"
#include "mongoose.h"
#include "wifi.h" #include "wifi.h"
/* Set up an AP or connect to existing WiFi network. */ /* Set up an AP or connect to existing WiFi network. */
...@@ -51,6 +50,13 @@ ...@@ -51,6 +50,13 @@
#define BM222_ADDR 0x18 #define BM222_ADDR 0x18
#define TMP006_ADDR 0x41 #define TMP006_ADDR 0x41
static const char *upload_form = "\
<h1>Upload file</h1> \
<form action='/upload' method='POST' enctype='multipart/form-data'> \
<input type='file' name='file'> \
<input type='submit' value='Upload'> \
</form>";
static struct mg_str upload_fname(struct mg_connection *nc, static struct mg_str upload_fname(struct mg_connection *nc,
struct mg_str fname) { struct mg_str fname) {
struct mg_str lfn; struct mg_str lfn;
...@@ -74,12 +80,20 @@ static void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) { ...@@ -74,12 +80,20 @@ static void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
case MG_EV_HTTP_REQUEST: { case MG_EV_HTTP_REQUEST: {
char addr[32]; char addr[32];
struct http_message *hm = (struct http_message *) ev_data; struct http_message *hm = (struct http_message *) ev_data;
cs_stat_t st;
mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE | mg_conn_addr_to_str(nc, addr, sizeof(addr), MG_SOCK_STRINGIFY_REMOTE |
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_IP |
MG_SOCK_STRINGIFY_PORT); MG_SOCK_STRINGIFY_PORT);
LOG(LL_INFO, LOG(LL_INFO,
("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len, ("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len,
hm->method.p, (int) hm->uri.len, hm->uri.p)); hm->method.p, (int) hm->uri.len, hm->uri.p));
if (mg_vcmp(&hm->uri, "/upload") == 0 ||
(mg_vcmp(&hm->uri, "/") == 0 &&
mg_stat("SL:index.html", &st) != 0)) {
mg_send(nc, upload_form, strlen(upload_form));
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
}
struct mg_serve_http_opts opts; struct mg_serve_http_opts opts;
memset(&opts, 0, sizeof(opts)); memset(&opts, 0, sizeof(opts));
opts.document_root = "SL:"; opts.document_root = "SL:";
...@@ -195,7 +209,7 @@ int main() { ...@@ -195,7 +209,7 @@ int main() {
MAP_PinTypeGPIO(PIN_64, PIN_MODE_0, false); MAP_PinTypeGPIO(PIN_64, PIN_MODE_0, false);
MAP_GPIODirModeSet(GPIOA1_BASE, 0x2, GPIO_DIR_MODE_OUT); MAP_GPIODirModeSet(GPIOA1_BASE, 0x2, GPIO_DIR_MODE_OUT);
GPIO_IF_LedConfigure(LED1); GPIO_IF_LedConfigure(LED1);
GPIO_IF_LedToggle(MCU_RED_LED_GPIO); GPIO_IF_LedOn(MCU_RED_LED_GPIO);
if (VStartSimpleLinkSpawnTask(8) != 0) { if (VStartSimpleLinkSpawnTask(8) != 0) {
LOG(LL_ERROR, ("Failed to create SL task")); LOG(LL_ERROR, ("Failed to create SL task"));
......
#include "wifi.h" #include "wifi.h"
#include "mongoose.h" #include "mongoose.h"
#include "simplelink.h"
#include "wlan.h"
#include "gpio_if.h" #include <simplelink/include/wlan.h>
#include <inc/hw_types.h>
#include <driverlib/gpio.h>
#include <example/common/gpio_if.h>
void SimpleLinkWlanEventHandler(SlWlanEvent_t *e) { void SimpleLinkWlanEventHandler(SlWlanEvent_t *e) {
switch (e->Event) { switch (e->Event) {
...@@ -22,8 +25,6 @@ void SimpleLinkWlanEventHandler(SlWlanEvent_t *e) { ...@@ -22,8 +25,6 @@ void SimpleLinkWlanEventHandler(SlWlanEvent_t *e) {
} }
} }
int ip_acquired = 0;
void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *e) { void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *e) {
if (e->Event == SL_NETAPP_IPV4_IPACQUIRED_EVENT) { if (e->Event == SL_NETAPP_IPV4_IPACQUIRED_EVENT) {
SlIpV4AcquiredAsync_t *ed = &e->EventData.ipAcquiredV4; SlIpV4AcquiredAsync_t *ed = &e->EventData.ipAcquiredV4;
...@@ -31,7 +32,6 @@ void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *e) { ...@@ -31,7 +32,6 @@ void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *e) {
SL_IPV4_BYTE(ed->ip, 2), SL_IPV4_BYTE(ed->ip, 1), SL_IPV4_BYTE(ed->ip, 2), SL_IPV4_BYTE(ed->ip, 1),
SL_IPV4_BYTE(ed->ip, 0))); SL_IPV4_BYTE(ed->ip, 0)));
GPIO_IF_LedToggle(MCU_RED_LED_GPIO); GPIO_IF_LedToggle(MCU_RED_LED_GPIO);
ip_acquired = 1;
} else if (e->Event == SL_NETAPP_IP_LEASED_EVENT) { } else if (e->Event == SL_NETAPP_IP_LEASED_EVENT) {
LOG(LL_INFO, ("IP leased")); LOG(LL_INFO, ("IP leased"));
} else { } else {
......
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
#define WIFI_AP_SSID "Mongoose" #define WIFI_AP_SSID "Mongoose"
#define WIFI_AP_PASS "" #define WIFI_AP_PASS ""
#define WIFI_AP_CHAN 6 #define WIFI_AP_CHAN 6
//#define WIFI_STA_SSID "YourWiFi" // #define WIFI_STA_SSID "YourWiFi"
//#define WIFI_STA_PASS "YourPass" // #define WIFI_STA_PASS "YourPass"
#define MG_TASK_PRIORITY 3 #define MG_TASK_PRIORITY 3
#define MG_TASK_STACK_SIZE 8192 #define MG_TASK_STACK_SIZE 8192
...@@ -37,14 +37,25 @@ ...@@ -37,14 +37,25 @@
#include "wifi.h" #include "wifi.h"
void mg_ev_handler(struct mg_connection *nc, int ev, void *p) { static const char *upload_form = "\
static const char *reply_fmt = <h1>Upload file</h1> \
"HTTP/1.0 200 OK\r\n" <form action='/upload' method='POST' enctype='multipart/form-data'> \
"Connection: close\r\n" <input type='file' name='file'> \
"Content-Type: text/plain\r\n" <input type='submit' value='Upload'> \
"\r\n" </form>";
"Hello %s\n";
LOG(LL_DEBUG, ("conn %p ev %d", nc, ev)); static struct mg_str upload_fname(struct mg_connection *nc,
struct mg_str fname) {
struct mg_str lfn;
lfn.len = fname.len + 3;
lfn.p = malloc(lfn.len);
memcpy((char *) lfn.p, "SL:", 3);
memcpy((char *) lfn.p + 3, fname.p, fname.len);
return lfn;
}
void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
LOG(LL_DEBUG, ("%p ev %d", nc, ev));
switch (ev) { switch (ev) {
case MG_EV_ACCEPT: { case MG_EV_ACCEPT: {
...@@ -56,20 +67,42 @@ void mg_ev_handler(struct mg_connection *nc, int ev, void *p) { ...@@ -56,20 +67,42 @@ void mg_ev_handler(struct mg_connection *nc, int ev, void *p) {
} }
case MG_EV_HTTP_REQUEST: { case MG_EV_HTTP_REQUEST: {
char addr[32]; char addr[32];
struct http_message *hm = (struct http_message *) p; struct http_message *hm = (struct http_message *) ev_data;
cs_stat_t st;
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
LOG(LL_INFO, LOG(LL_INFO,
("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len, ("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len,
hm->method.p, (int) hm->uri.len, hm->uri.p)); hm->method.p, (int) hm->uri.len, hm->uri.p));
mg_printf(nc, reply_fmt, addr); if (mg_vcmp(&hm->uri, "/upload") == 0 ||
nc->flags |= MG_F_SEND_AND_CLOSE; (mg_vcmp(&hm->uri, "/") == 0 &&
mg_stat("SL:index.html", &st) != 0)) {
mg_send(nc, upload_form, strlen(upload_form));
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
}
struct mg_serve_http_opts opts;
memset(&opts, 0, sizeof(opts));
opts.document_root = "SL:";
mg_serve_http(nc, hm, opts);
break; break;
} }
case MG_EV_CLOSE: { case MG_EV_CLOSE: {
LOG(LL_INFO, ("Connection %p closed", nc)); LOG(LL_INFO, ("Connection %p closed", nc));
break; break;
} }
case MG_EV_HTTP_PART_BEGIN:
case MG_EV_HTTP_PART_DATA:
case MG_EV_HTTP_PART_END: {
struct mg_http_multipart_part *mp =
(struct mg_http_multipart_part *) ev_data;
if (ev == MG_EV_HTTP_PART_BEGIN) {
LOG(LL_INFO, ("Begin file upload: %s", mp->file_name));
} else if (ev == MG_EV_HTTP_PART_END) {
LOG(LL_INFO, ("End file upload: %s", mp->file_name));
}
mg_file_upload_handler(nc, ev, ev_data, upload_fname);
}
} }
} }
...@@ -134,8 +167,6 @@ int main(void) { ...@@ -134,8 +167,6 @@ int main(void) {
return 1; return 1;
} }
LOG(LL_INFO, ("OK"));
osi_start(); osi_start();
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment