Initial commit: TXW82x FPV v2.7.0.7-42229 SDK + project sources
This commit is contained in:
107
sdk/app/http/certs.h
Normal file
107
sdk/app/http/certs.h
Normal file
@@ -0,0 +1,107 @@
|
||||
#ifndef CERTS_H_
|
||||
#define CERTS_H_
|
||||
|
||||
static const char *s_ssl_cert =
|
||||
"-----BEGIN CERTIFICATE-----\r\n" \
|
||||
"MIIBhzCCASygAwIBAgIUbnMoVd8TtWH1T09dANkK2LU6IUswCgYIKoZIzj0EAwIw\r\n" \
|
||||
"RDELMAkGA1UEBhMCSUUxDzANBgNVBAcMBkR1YmxpbjEQMA4GA1UECgwHQ2VzYW50\r\n" \
|
||||
"YTESMBAGA1UEAwwJVGVzdCBSb290MB4XDTIwMDUwOTIxNTE0OVoXDTMwMDUwOTIx\r\n" \
|
||||
"NTE0OVowETEPMA0GA1UEAwwGc2VydmVyMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcD\r\n" \
|
||||
"QgAEkuBGnInDN6l06zVVQ1VcrOvH5FDu9MC6FwJc2e201P8hEpq0Q/SJS2nkbSuW\r\n" \
|
||||
"H/wBTTBaeXN2uhlBzMUWK790KKMvMC0wCQYDVR0TBAIwADALBgNVHQ8EBAMCA6gw\r\n" \
|
||||
"EwYDVR0lBAwwCgYIKwYBBQUHAwEwCgYIKoZIzj0EAwIDSQAwRgIhAPo6xx7LjCdZ\r\n" \
|
||||
"QY133XvLjAgVFrlucOZHONFVQuDXZsjwAiEAzHBNligA08c5U3SySYcnkhurGg50\r\n" \
|
||||
"BllCI0eYQ9ggp/o=\r\n" \
|
||||
"-----END CERTIFICATE-----\r\n";
|
||||
|
||||
static const char *s_ssl_key =
|
||||
"-----BEGIN PRIVATE KEY-----\r\n" \
|
||||
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQglNni0t9Dg9icgG8w\r\n" \
|
||||
"kbfxWSS+TuNgbtNybIQXcm3NHpmhRANCAASS4EacicM3qXTrNVVDVVys68fkUO70\r\n" \
|
||||
"wLoXAlzZ7bTU/yESmrRD9IlLaeRtK5Yf/AFNMFp5c3a6GUHMxRYrv3Qo\r\n" \
|
||||
"-----END PRIVATE KEY-----\r\n";
|
||||
|
||||
|
||||
static const char *s_ca =
|
||||
"-\n"
|
||||
"Subject: C=US, O=DigiCert Inc, OU=www.digicert.com, CN=DigiCert High Assurance EV Root CA\n"
|
||||
"Not Before: Nov 10 00:00:00 2006 GMT\n"
|
||||
"Not After : Nov 10 00:00:00 2031 GMT\n"
|
||||
"-----BEGIN CERTIFICATE-----\n"
|
||||
"MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\n"
|
||||
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
|
||||
"d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\n"
|
||||
"ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL\n"
|
||||
"MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3\n"
|
||||
"LmRpZ2ljZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug\n"
|
||||
"RVYgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbM5XPm\n"
|
||||
"+9S75S0tMqbf5YE/yc0lSbZxKsPVlDRnogocsF9ppkCxxLeyj9CYpKlBWTrT3JTW\n"
|
||||
"PNt0OKRKzE0lgvdKpVMSOO7zSW1xkX5jtqumX8OkhPhPYlG++MXs2ziS4wblCJEM\n"
|
||||
"xChBVfvLWokVfnHoNb9Ncgk9vjo4UFt3MRuNs8ckRZqnrG0AFFoEt7oT61EKmEFB\n"
|
||||
"Ik5lYYeBQVCmeVyJ3hlKV9Uu5l0cUyx+mM0aBhakaHPQNAQTXKFx01p8VdteZOE3\n"
|
||||
"hzBWBOURtCmAEvF5OYiiAhF8J2a3iLd48soKqDirCmTCv2ZdlYTBoSUeh10aUAsg\n"
|
||||
"EsxBu24LUTi4S8sCAwEAAaNjMGEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQF\n"
|
||||
"MAMBAf8wHQYDVR0OBBYEFLE+w2kD+L9HAdSYJhoIAu9jZCvDMB8GA1UdIwQYMBaA\n"
|
||||
"FLE+w2kD+L9HAdSYJhoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec\n"
|
||||
"nzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z\n"
|
||||
"eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF\n"
|
||||
"hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2\n"
|
||||
"Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\n"
|
||||
"vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\n"
|
||||
"+OkuE6N36B9K\n"
|
||||
"-----END CERTIFICATE-----\n"
|
||||
"\n"
|
||||
"Subject: C=US, O=DigiCert Inc, OU=www.digicert.com, CN=DigiCert Global Root CA\n"
|
||||
"Not Before: Nov 10 00:00:00 2006 GMT\n"
|
||||
"Not After : Nov 10 00:00:00 2031 GMT\n"
|
||||
"-----BEGIN CERTIFICATE-----\n"
|
||||
"MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n"
|
||||
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
|
||||
"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n"
|
||||
"QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n"
|
||||
"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
|
||||
"b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n"
|
||||
"9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n"
|
||||
"CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n"
|
||||
"nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n"
|
||||
"43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n"
|
||||
"T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n"
|
||||
"gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n"
|
||||
"BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n"
|
||||
"TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n"
|
||||
"DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n"
|
||||
"hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n"
|
||||
"06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n"
|
||||
"PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n"
|
||||
"YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n"
|
||||
"CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n"
|
||||
"-----END CERTIFICATE-----\n"
|
||||
"\n"
|
||||
"Not Before: Nov 10 00:00:00 2006 GMT\n"
|
||||
"Not After : Nov 10 00:00:00 2031 GMT\n"
|
||||
"Subject: C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA\n"
|
||||
"-----BEGIN CERTIFICATE-----\n"
|
||||
"MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n"
|
||||
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
|
||||
"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n"
|
||||
"QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n"
|
||||
"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
|
||||
"b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n"
|
||||
"9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n"
|
||||
"CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n"
|
||||
"nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n"
|
||||
"43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n"
|
||||
"T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n"
|
||||
"gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n"
|
||||
"BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n"
|
||||
"TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n"
|
||||
"DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n"
|
||||
"hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n"
|
||||
"06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n"
|
||||
"PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n"
|
||||
"YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n"
|
||||
"CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n"
|
||||
"-----END CERTIFICATE-----\n"
|
||||
"\n"
|
||||
"";
|
||||
#endif
|
||||
153
sdk/app/http/http_client_demo/http_client_demo.c
Normal file
153
sdk/app/http/http_client_demo/http_client_demo.c
Normal file
@@ -0,0 +1,153 @@
|
||||
// Copyright (c) 2021 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Example HTTP client. Connect to `s_url`, send request, wait for a response,
|
||||
// print the response and exit.
|
||||
// You can change `s_url` from the command line by executing: ./example YOUR_URL
|
||||
//
|
||||
// To enable SSL/TLS, make SSL=OPENSSL or make SSL=MBEDTLS
|
||||
|
||||
#include "sys_config.h"
|
||||
#include "typesdef.h"
|
||||
#include "list.h"
|
||||
#include "dev.h"
|
||||
#include "devid.h"
|
||||
#include "osal/string.h"
|
||||
#include "osal/semaphore.h"
|
||||
#include "osal/mutex.h"
|
||||
#include "osal/irq.h"
|
||||
#include "osal/work.h"
|
||||
#include "osal/sleep.h"
|
||||
#include "osal/timer.h"
|
||||
#include "hal/gpio.h"
|
||||
#include "lib/common/common.h"
|
||||
#include "lib/common/sysevt.h"
|
||||
#include "lib/syscfg/syscfg.h"
|
||||
#include <string.h>
|
||||
#if HTTPS_CLIENT || HTTP_CLIENT
|
||||
#include "mongoose.h"
|
||||
#include "http_client_demo.h"
|
||||
#include "certs.h"
|
||||
// The very first web page in history. You can replace it from command line
|
||||
#if HTTPS_CLIENT
|
||||
static const char *s_url = "https://example.org/";//AP模式下无法透传上网
|
||||
#elif HTTP_CLIENT
|
||||
static const char *s_url = "http://192.168.1.100";
|
||||
#else
|
||||
static const char *s_url = "";
|
||||
#endif
|
||||
static const char *s_post_data = NULL; // POST data
|
||||
static const uint64_t s_timeout_ms = 1500; // Connect timeout in milliseconds
|
||||
|
||||
static struct mg_str *get_header_value(struct mg_http_message *hm,const char *name);
|
||||
|
||||
// Print HTTP response and signal that we're done
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// Connection created. Store connect expiration time in c->data
|
||||
*(uint64_t *) c->data = mg_millis() + s_timeout_ms;
|
||||
} else if (ev == MG_EV_POLL) {
|
||||
if (mg_millis() > *(uint64_t *) c->data &&
|
||||
(c->is_connecting || c->is_resolving)) {
|
||||
mg_error(c, "Connect timeout");
|
||||
}
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
// Connected to server. Extract host name from URL
|
||||
struct mg_str host = mg_url_host(s_url);
|
||||
|
||||
// If s_url is https://, tell client connection to use TLS
|
||||
if (mg_url_is_ssl(s_url)) {
|
||||
struct mg_tls_opts opts = {
|
||||
//.ca = s_ca,
|
||||
.srvname = host
|
||||
};
|
||||
mg_tls_init(c, &opts);
|
||||
}
|
||||
|
||||
// Send request
|
||||
int content_length = s_post_data ? strlen(s_post_data) : 0;
|
||||
mg_printf(c,
|
||||
"%s %s HTTP/1.0\r\n"
|
||||
"Host: %.*s\r\n"
|
||||
"Content-Type: octet-stream\r\n"
|
||||
"Content-Length: %d\r\n"
|
||||
"\r\n",
|
||||
s_post_data ? "POST" : "GET", mg_url_uri(s_url), (int) host.len,
|
||||
host.ptr, content_length);
|
||||
mg_send(c, s_post_data, content_length);
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
// Response is received. Print it
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
get_header_value(hm,"Content-Type");
|
||||
MG_INFO(("%.*s", (int) hm->message.len, hm->message.ptr));
|
||||
c->is_closing = 1; // Tell mongoose to close this connection
|
||||
*(bool *) fn_data = true; // Tell event loop to stop
|
||||
} else if (ev == MG_EV_ERROR) {
|
||||
*(bool *) fn_data = true; // Error, tell event loop to stop
|
||||
}
|
||||
}
|
||||
|
||||
os_task_t http_client_task;
|
||||
|
||||
int http_client_demo(void *arg) {
|
||||
|
||||
struct mg_mgr mgr; // Event manager
|
||||
bool done = false; // Event handler flips it to true
|
||||
mg_log_set(MG_LL_DEBUG); // Set to 0 to disable debug
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
|
||||
mg_http_connect(&mgr, s_url, fn, &done); // Create client connection
|
||||
while (!done) mg_mgr_poll(&mgr, 50); // Event manager loops until 'done'
|
||||
mg_mgr_free(&mgr); // Free resources
|
||||
return 0;
|
||||
}
|
||||
|
||||
static sysevt_hdl_res dhcp_client_get_ip(uint32 event_id, uint32 data, uint32 priv)
|
||||
{
|
||||
static uint8_t is_get_ip = 0;
|
||||
printf("%s:%d!!!!!!!!!!!\n",__FUNCTION__,__LINE__);
|
||||
if(!is_get_ip)
|
||||
{
|
||||
is_get_ip = 1;
|
||||
OS_TASK_INIT("http_client_task", &http_client_task, http_client_demo, NULL, OS_TASK_PRIORITY_NORMAL, 20*1024);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int http_client_init(uint32 ip)
|
||||
{
|
||||
|
||||
printf("%s:%d\tip:%s\n",__FUNCTION__,__LINE__,inet_ntoa(ip));
|
||||
|
||||
sys_event_take(SYS_EVENT(SYS_EVENT_WIFI, SYSEVT_WIFI_STA_CONNECTTED),dhcp_client_get_ip,0);
|
||||
//if(!ip)
|
||||
//{
|
||||
// sys_event_take(SYS_EVENT(SYS_EVENT_NETWORK, SYSEVT_LWIP_DHCPC_DONE),dhcp_client_get_ip,0);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// OS_TASK_INIT("http_client_task", &http_client_task, http_client_demo, NULL, OS_TASK_PRIORITY_NORMAL, 10*1024);
|
||||
//}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct mg_str *get_header_value(struct mg_http_message *hm,const char *name) {
|
||||
if (name == NULL)
|
||||
return NULL;
|
||||
struct mg_str *cl;
|
||||
if ((cl = mg_http_get_header(hm, name)) != NULL) {
|
||||
printf("%s--get %s %.*s\r\n", __FILE__, name, (int) cl->len, cl->ptr);
|
||||
return cl;
|
||||
} else {
|
||||
printf("%s--can not get %s %.*s\r\n", __FILE__, name);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
7
sdk/app/http/http_client_demo/http_client_demo.h
Normal file
7
sdk/app/http/http_client_demo/http_client_demo.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef HTTPS_CLIENT_H
|
||||
#define HTTPS_CLIENT_H
|
||||
|
||||
#define HTTP_DIR "0:/HTTP"
|
||||
|
||||
int http_client_init(uint32 ip);
|
||||
#endif
|
||||
151
sdk/app/http/http_server_demo/http_server_demo.c
Normal file
151
sdk/app/http/http_server_demo/http_server_demo.c
Normal file
@@ -0,0 +1,151 @@
|
||||
// Copyright (c) 2020 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// HTTP server example. This server serves both static and dynamic content.
|
||||
// It opens two ports: plain HTTP on port 8000 and HTTP on port 8443.
|
||||
// It implements the following endpoints:
|
||||
// /api/stats - respond with free-formatted stats on current connections
|
||||
// /api/f2/:id - wildcard example, respond with JSON string {"result": "URI"}
|
||||
// any other URI serves static files from s_root_dir
|
||||
//
|
||||
// To enable SSL/TLS (using self-signed certificates in PEM files),
|
||||
// 1. make SSL=OPENSSL or make SSL=MBEDTLS
|
||||
// 2. curl -k https://127.0.0.1:8443
|
||||
#include "sys_config.h"
|
||||
#include "typesdef.h"
|
||||
#include "list.h"
|
||||
#include "dev.h"
|
||||
#include "devid.h"
|
||||
#include "osal/string.h"
|
||||
#include "osal/semaphore.h"
|
||||
#include "osal/mutex.h"
|
||||
#include "osal/irq.h"
|
||||
#include "osal/work.h"
|
||||
#include "osal/sleep.h"
|
||||
#include "osal/timer.h"
|
||||
#include "hal/gpio.h"
|
||||
#include "lib/common/common.h"
|
||||
#include "lib/common/sysevt.h"
|
||||
#include "lib/syscfg/syscfg.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "netif/ethernetif.h"
|
||||
#include <string.h>
|
||||
#if HTTP_SERVER || HTTPS_SERVER
|
||||
#include "mongoose.h"
|
||||
#include "http_server_demo.h"
|
||||
#include "certs.h"
|
||||
static char s_http_addr[30] = "http://"; // HTTP port
|
||||
static char s_https_addr[30] = "https://"; // HTTPS port
|
||||
static const char *s_root_dir = ".";
|
||||
os_task_t http_server_task;
|
||||
// We use the same event handler function for HTTP and HTTPS connections
|
||||
// fn_data is NULL for plain HTTP, and non-NULL for HTTPS
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_ACCEPT && fn_data != NULL) {
|
||||
printf("ev == MG_EV_ACCEPT && fn_data != NULL\r\n");
|
||||
struct mg_tls_opts opts = {
|
||||
//.ca = s_ca,//双向验证,一般不用
|
||||
.cert = s_ssl_cert,
|
||||
.certkey = s_ssl_key
|
||||
};
|
||||
mg_tls_init(c, &opts);
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
MG_INFO(("%.*s %.*s %ld", (int) hm->method.len, hm->method.ptr,
|
||||
(int) hm->uri.len, hm->uri.ptr, (long) hm->body.len));
|
||||
if (mg_http_match_uri(hm, "/api/stats")) {
|
||||
// Print some statistics about currently established connections
|
||||
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||
mg_http_printf_chunk(c, "ID PROTO TYPE LOCAL REMOTE\n");
|
||||
for (struct mg_connection *t = c->mgr->conns; t != NULL; t = t->next) {
|
||||
(c, "%-3lu %4s %s %I %I\n", t->id,
|
||||
t->is_udp ? "UDP" : "TCP",
|
||||
t->is_listening ? "LISTENING"
|
||||
: t->is_accepted ? "ACCEPTED "
|
||||
: "CONNECTED",
|
||||
4, &t->loc.ip, 4, &t->rem.ip);
|
||||
}
|
||||
mg_http_printf_chunk(c, ""); // Don't forget the last empty chunk
|
||||
} else if (mg_http_match_uri(hm, "/api/f2/*")) {
|
||||
mg_http_reply(c, 200, "", "{\"result\": \"%.*s\"}\n", (int) hm->uri.len,
|
||||
hm->uri.ptr);
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {.root_dir = s_root_dir};
|
||||
|
||||
mg_http_serve_dir(c, ev_data, &opts);
|
||||
}
|
||||
}
|
||||
(void) fn_data;
|
||||
}
|
||||
|
||||
int http_server_demo(void) {
|
||||
struct mg_mgr mgr; // Event manager
|
||||
mg_log_set(MG_LL_DEBUG); // Set log level
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
#if HTTP_SERVER
|
||||
mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
|
||||
#elif HTTPS_SERVER
|
||||
mg_http_listen(&mgr, s_https_addr, fn, (void *) 1); // HTTPS listener
|
||||
#endif
|
||||
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
||||
mg_mgr_free(&mgr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static sysevt_hdl_res dhcp_client_get_ip(uint32 event_id, uint32 data, uint32 priv)
|
||||
{
|
||||
static uint8_t is_get_ip = 0;
|
||||
printf("%s:%d!!!!!!!!!!!\n",__FUNCTION__,__LINE__);
|
||||
if(!is_get_ip)
|
||||
{
|
||||
ip_addr_t ipaddr;
|
||||
char *ip;
|
||||
is_get_ip = 1;
|
||||
ipaddr = lwip_netif_get_ip2("w0");
|
||||
ip = inet_ntoa(ipaddr);
|
||||
#if HTTP_SERVER
|
||||
strncat(s_http_addr,ip,strlen(ip));
|
||||
printf("server_ip: %s\r\n", s_http_addr);
|
||||
#elif HTTPS_SERVER
|
||||
strncat(s_https_addr,ip,strlen(ip));
|
||||
printf("server_ip: %s\r\n", s_https_addr);
|
||||
#endif
|
||||
|
||||
OS_TASK_INIT("http", &http_server_task, http_server_demo, NULL, OS_TASK_PRIORITY_NORMAL, 20*1024);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int http_server_init(uint32 ip)
|
||||
{
|
||||
|
||||
printf("%s:%d\tip:%X\n",__FUNCTION__,__LINE__,ip);
|
||||
|
||||
|
||||
if(!ip)
|
||||
{
|
||||
sys_event_take(SYS_EVENT(SYS_EVENT_NETWORK, SYSEVT_LWIP_DHCPC_DONE),dhcp_client_get_ip,0);
|
||||
}
|
||||
else
|
||||
{
|
||||
ip_addr_t ipaddr;
|
||||
char *ip;
|
||||
ipaddr = lwip_netif_get_ip2("w0");
|
||||
ip = inet_ntoa(ipaddr);
|
||||
#if HTTP_SERVER
|
||||
strncat(s_http_addr,ip,strlen(ip));
|
||||
printf("server_ip: %s\r\n", s_http_addr);
|
||||
#elif HTTPS_SERVER
|
||||
strncat(s_https_addr,ip,strlen(ip));
|
||||
printf("server_ip: %s\r\n", s_https_addr);
|
||||
#endif
|
||||
OS_TASK_INIT("http", &http_server_task, http_server_demo, NULL, OS_TASK_PRIORITY_NORMAL, 20*1024);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
6
sdk/app/http/http_server_demo/http_server_demo.h
Normal file
6
sdk/app/http/http_server_demo/http_server_demo.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef HTTP_SERVER_H
|
||||
#define HTTP_SERVER_H
|
||||
|
||||
|
||||
int http_server_init(uint32 ip);
|
||||
#endif
|
||||
153
sdk/app/http/httpfile_client_demo/httpfile_client_demo.c
Normal file
153
sdk/app/http/httpfile_client_demo/httpfile_client_demo.c
Normal file
@@ -0,0 +1,153 @@
|
||||
// Copyright (c) 2021 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Example HTTP client. Connect to `s_url`, send request, wait for a response,
|
||||
// print the response and exit.
|
||||
// You can change `s_url` from the command line by executing: ./example YOUR_URL
|
||||
//
|
||||
// To enable SSL/TLS, make SSL=OPENSSL or make SSL=MBEDTLS
|
||||
|
||||
#include "sys_config.h"
|
||||
#include "typesdef.h"
|
||||
#include "list.h"
|
||||
#include "dev.h"
|
||||
#include "devid.h"
|
||||
#include "osal/string.h"
|
||||
#include "osal/semaphore.h"
|
||||
#include "osal/mutex.h"
|
||||
#include "osal/irq.h"
|
||||
#include "osal/work.h"
|
||||
#include "osal/sleep.h"
|
||||
#include "osal/timer.h"
|
||||
#include "hal/gpio.h"
|
||||
#include "lib/common/common.h"
|
||||
#include "lib/common/sysevt.h"
|
||||
#include "lib/syscfg/syscfg.h"
|
||||
#include <string.h>
|
||||
#if HTTPFILE_CLIENT
|
||||
#include "mongoose.h"
|
||||
#include "httpfile_client_demo.h"
|
||||
// The very first web page in history. You can replace it from command line
|
||||
static const char *s_url = "http://192.168.1.100/2.txt";
|
||||
static const char *s_post_data = NULL; // POST data
|
||||
static const uint64_t s_timeout_ms = 1500; // Connect timeout in milliseconds
|
||||
|
||||
static struct mg_str *get_header_value(struct mg_http_message *hm,const char *name);
|
||||
|
||||
// Print HTTP response and signal that we're done
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// Connection created. Store connect expiration time in c->data
|
||||
*(uint64_t *) c->data = mg_millis() + s_timeout_ms;
|
||||
} else if (ev == MG_EV_POLL) {
|
||||
if (mg_millis() > *(uint64_t *) c->data &&
|
||||
(c->is_connecting || c->is_resolving)) {
|
||||
mg_error(c, "Connect timeout");
|
||||
}
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
// Connected to server. Extract host name from URL
|
||||
struct mg_str host = mg_url_host(s_url);
|
||||
|
||||
// If s_url is https://, tell client connection to use TLS
|
||||
if (mg_url_is_ssl(s_url)) {
|
||||
struct mg_tls_opts opts = {
|
||||
//.ca = s_ca,
|
||||
.srvname = host
|
||||
};
|
||||
mg_tls_init(c, &opts);
|
||||
}
|
||||
|
||||
// Send request
|
||||
int content_length = s_post_data ? strlen(s_post_data) : 0;
|
||||
mg_printf(c,
|
||||
"%s %s HTTP/1.0\r\n"
|
||||
"Host: %.*s\r\n"
|
||||
"Content-Type: octet-stream\r\n"
|
||||
"Content-Length: %d\r\n"
|
||||
"\r\n",
|
||||
s_post_data ? "POST" : "GET", mg_url_uri(s_url), (int) host.len,
|
||||
host.ptr, content_length);
|
||||
mg_send(c, s_post_data, content_length);
|
||||
} /**/ else if (ev == MG_EV_HTTP_CHUNK) {
|
||||
//size_t len;
|
||||
//char *data;
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
//fwrite(hm->chunk.ptr, 1, hm->chunk.len, stdout);
|
||||
//MG_INFO(("hm->chunk.len %d %.*s\r\n", (int)hm->chunk.len, (int) hm->chunk.len, hm->chunk.ptr));
|
||||
mg_file_write(&mg_fs_fat, HTTP_DIR"/6.txt", hm->chunk.ptr, hm->chunk.len);
|
||||
mg_http_delete_chunk(c, hm);
|
||||
//data = mg_file_read(&mg_fs_fat,HTTP_DIR"/2.txt",&len);
|
||||
//MG_INFO(("mg_file_read length is %d\r\n",len));
|
||||
if (hm->chunk.len == 0) *(bool *) fn_data = true; // Last chunk
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
// Response is received. Print it
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
get_header_value(hm,"Content-Type");
|
||||
printf("hm->message.len:%d\r\n",hm->message.len);
|
||||
MG_INFO(("%.*s", (int) hm->message.len, hm->message.ptr));
|
||||
c->is_closing = 1; // Tell mongoose to close this connection
|
||||
*(bool *) fn_data = true; // Tell event loop to stop
|
||||
} else if (ev == MG_EV_ERROR) {
|
||||
*(bool *) fn_data = true; // Error, tell event loop to stop
|
||||
}
|
||||
}
|
||||
|
||||
os_task_t httpfile_client_task;
|
||||
|
||||
int httpfile_client_demo(void *arg) {
|
||||
|
||||
struct mg_mgr mgr; // Event manager
|
||||
bool done = false; // Event handler flips it to true
|
||||
int ret;
|
||||
mg_log_set(MG_LL_NONE); // Set to 0 to disable debug
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
ret = f_mkdir(HTTP_DIR);
|
||||
if(ret != 0 && ret != 8)
|
||||
return -1;
|
||||
mg_http_connect(&mgr, s_url, fn, &done); // Create client connection
|
||||
while (!done) mg_mgr_poll(&mgr, 10); // Event manager loops until 'done'
|
||||
mg_mgr_free(&mgr); // Free resources
|
||||
return 0;
|
||||
}
|
||||
|
||||
static sysevt_hdl_res dhcp_client_get_ip(uint32 event_id, uint32 data, uint32 priv)
|
||||
{
|
||||
static uint8_t is_get_ip = 0;
|
||||
printf("%s:%d!!!!!!!!!!!\n",__FUNCTION__,__LINE__);
|
||||
if(!is_get_ip)
|
||||
{
|
||||
is_get_ip = 1;
|
||||
|
||||
OS_TASK_INIT("httpfile_client_task", &httpfile_client_task, httpfile_client_demo, NULL, OS_TASK_PRIORITY_NORMAL, 20*1024);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int httpfile_client_init(uint32 ip)
|
||||
{
|
||||
|
||||
printf("%s:%d\tip:%s\n",__FUNCTION__,__LINE__,inet_ntoa(ip));
|
||||
|
||||
sys_event_take(SYS_EVENT(SYS_EVENT_WIFI, SYSEVT_WIFI_STA_CONNECTTED),dhcp_client_get_ip,0);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct mg_str *get_header_value(struct mg_http_message *hm,const char *name) {
|
||||
if (name == NULL)
|
||||
return NULL;
|
||||
struct mg_str *cl;
|
||||
if ((cl = mg_http_get_header(hm, name)) != NULL) {
|
||||
printf("%s--get %s %.*s\r\n", __FILE__, name, (int) cl->len, cl->ptr);
|
||||
return cl;
|
||||
} else {
|
||||
printf("%s--can not get %s %.*s\r\n", __FILE__, name);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
7
sdk/app/http/httpfile_client_demo/httpfile_client_demo.h
Normal file
7
sdk/app/http/httpfile_client_demo/httpfile_client_demo.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef HTTPFILE_CLIENT_H
|
||||
#define HTTPFILE_CLIENT_H
|
||||
|
||||
#define HTTP_DIR "0:/HTTP"
|
||||
|
||||
int httpfile_client_init(uint32 ip);
|
||||
#endif
|
||||
124
sdk/app/http/httpfile_server_demo/httpfile_server_demo.c
Normal file
124
sdk/app/http/httpfile_server_demo/httpfile_server_demo.c
Normal file
@@ -0,0 +1,124 @@
|
||||
// Copyright (c) 2020 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// HTTP server example. This server serves both static and dynamic content.
|
||||
// It opens two ports: plain HTTP on port 8000 and HTTP on port 8443.
|
||||
// It implements the following endpoints:
|
||||
// /api/stats - respond with free-formatted stats on current connections
|
||||
// /api/f2/:id - wildcard example, respond with JSON string {"result": "URI"}
|
||||
// any other URI serves static files from s_root_dir
|
||||
//
|
||||
// To enable SSL/TLS (using self-signed certificates in PEM files),
|
||||
// 1. make SSL=OPENSSL or make SSL=MBEDTLS
|
||||
// 2. curl -k https://127.0.0.1:8443
|
||||
#include "sys_config.h"
|
||||
#include "typesdef.h"
|
||||
#include "list.h"
|
||||
#include "dev.h"
|
||||
#include "devid.h"
|
||||
#include "osal/string.h"
|
||||
#include "osal/semaphore.h"
|
||||
#include "osal/mutex.h"
|
||||
#include "osal/irq.h"
|
||||
#include "osal/work.h"
|
||||
#include "osal/sleep.h"
|
||||
#include "osal/timer.h"
|
||||
#include "hal/gpio.h"
|
||||
#include "lib/common/common.h"
|
||||
#include "lib/common/sysevt.h"
|
||||
#include "lib/syscfg/syscfg.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "netif/ethernetif.h"
|
||||
#include <string.h>
|
||||
#if HTTPFILE_SERVER
|
||||
#include "mongoose.h"
|
||||
#include "httpfile_server_demo.h"
|
||||
static char s_http_addr[30] = "http://"; // HTTP port
|
||||
os_task_t httpfile_server_task;
|
||||
// HTTP request handler function. It implements the following endpoints:
|
||||
// /upload - Saves the next file chunk
|
||||
// all other URI - serves web_root/ directory
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (ev == MG_EV_HTTP_CHUNK && mg_http_match_uri(hm, "/upload")) {
|
||||
//MG_INFO(("Got chunk len %lu , Got query len %lu\r\n", (unsigned long) hm->chunk.len,(unsigned long) hm->query.len));
|
||||
//MG_INFO(("Query string: [%.*s]\r\n", (int) hm->query.len, hm->query.ptr));
|
||||
mg_file_write(&mg_fs_fat, HTTP_DIR"/e17.txt", hm->chunk.ptr, hm->chunk.len);
|
||||
//MG_INFO(("Chunk data: [%.*s]\r\n", (int) hm->chunk.len, hm->chunk.ptr));
|
||||
mg_http_delete_chunk(c, hm);
|
||||
if (hm->chunk.len == 0) {
|
||||
MG_INFO(("Last chunk received, sending response"));
|
||||
mg_http_reply(c, 200, "", "ok (chunked)\n");
|
||||
}
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_serve_opts opts = { .fs = &mg_fs_fat, .root_dir = HTTP_DIR"/web_root"};
|
||||
mg_http_serve_dir(c, hm, &opts);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int httpfile_server_demo(void) {
|
||||
struct mg_mgr mgr; // Event manager
|
||||
int ret;
|
||||
mg_log_set(MG_LL_NONE); // Set log level
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
ret = f_mkdir(HTTP_DIR);
|
||||
if(ret != 0 && ret != 8) {
|
||||
printf("%s set failed\r\n",__FUNCTION__);
|
||||
return -1;
|
||||
}
|
||||
mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
|
||||
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
||||
mg_mgr_free(&mgr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static sysevt_hdl_res dhcp_client_get_ip(uint32 event_id, uint32 data, uint32 priv)
|
||||
{
|
||||
static uint8_t is_get_ip = 0;
|
||||
printf("%s:%d!!!!!!!!!!!\n",__FUNCTION__,__LINE__);
|
||||
if(!is_get_ip)
|
||||
{
|
||||
ip_addr_t ipaddr;
|
||||
char *ip;
|
||||
is_get_ip = 1;
|
||||
ipaddr = lwip_netif_get_ip2("w0");
|
||||
ip = inet_ntoa(ipaddr);
|
||||
strncat(s_http_addr,ip,strlen(ip));
|
||||
printf("server_ip: %s\r\n", s_http_addr);
|
||||
|
||||
OS_TASK_INIT("http", &httpfile_server_task, httpfile_server_demo, NULL, OS_TASK_PRIORITY_NORMAL, 30*1024);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int httpfile_server_init(uint32 ip)
|
||||
{
|
||||
|
||||
printf("%s:%d\tip:%X\n",__FUNCTION__,__LINE__,ip);
|
||||
|
||||
|
||||
if(!ip)
|
||||
{
|
||||
sys_event_take(SYS_EVENT(SYS_EVENT_NETWORK, SYSEVT_LWIP_DHCPC_DONE),dhcp_client_get_ip,0);
|
||||
}
|
||||
else
|
||||
{
|
||||
ip_addr_t ipaddr;
|
||||
char *ip;
|
||||
ipaddr = lwip_netif_get_ip2("w0");
|
||||
ip = inet_ntoa(ipaddr);
|
||||
strncat(s_http_addr,ip,strlen(ip));
|
||||
printf("server_ip: %s\r\n", s_http_addr);
|
||||
OS_TASK_INIT("http", &httpfile_server_task, httpfile_server_demo, NULL, OS_TASK_PRIORITY_NORMAL, 30*1024);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
7
sdk/app/http/httpfile_server_demo/httpfile_server_demo.h
Normal file
7
sdk/app/http/httpfile_server_demo/httpfile_server_demo.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef HTTPFILE_SERVER_H
|
||||
#define HTTPFILE_SERVER_H
|
||||
|
||||
#define HTTP_DIR "0:/HTTP"
|
||||
|
||||
int httpfile_server_init(uint32 ip);
|
||||
#endif
|
||||
Reference in New Issue
Block a user