合并提交

This commit is contained in:
zogodo
2019-09-16 01:45:13 +08:00
parent 3065ffe5c7
commit a4bba1ffb3
17 changed files with 709 additions and 772 deletions

View File

@@ -37,9 +37,14 @@
#include "mico.h"
#include "httpd_priv.h"
#include "app_httpd.h"
#include "user_gpio.h"
#include "main.h"
#include "web_data.c"
#define HTTP_CONTENT_HTML_ZIP "text/html\r\nContent-Encoding: gzip"
#define app_httpd_log(M, ...) custom_log("apphttpd", M, ##__VA_ARGS__)
#define HTTPD_HDR_DEFORT (HTTPD_HDR_ADD_SERVER|HTTPD_HDR_ADD_CONN_CLOSE|HTTPD_HDR_ADD_PRAGMA_NO_CACHE)
@@ -47,161 +52,243 @@ static bool is_http_init;
static bool is_handlers_registered;
struct httpd_wsgi_call g_app_handlers[];
static int web_send_wifisetting_page(httpd_request_t *req)
static int http_get_index_page(httpd_request_t *req)
{
OSStatus err = kNoErr;
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(wifisetting), HTTP_CONTENT_HTML_STR);
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wifisetting headers.") );
err = httpd_send_body(req->sock, wifisetting, sizeof(wifisetting));
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wifisetting body.") );
OSStatus err = kNoErr;
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(index_html), HTTP_CONTENT_HTML_ZIP);
require_noerr_action(err, exit, app_httpd_log("ERROR: Unable to send http wifisetting headers."));
err = httpd_send_body(req->sock, index_html, sizeof(index_html));
require_noerr_action(err, exit, app_httpd_log("ERROR: Unable to send http wifisetting body."));
exit:
return err;
return err;
}
static int http_get_socket_status(httpd_request_t *req)
{
OSStatus err = kNoErr;
char* status = get_socket_status();
err = httpd_send_all_header(req, HTTP_RES_200, strlen(status), HTTP_CONTENT_HTML_STR);
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http socket_status headers.") );
err = httpd_send_body(req->sock, socket_status, strlen(status));
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http socket_status body.") );
exit:
return err;
}
static int http_set_socket_status(httpd_request_t *req)
{
OSStatus err = kNoErr;
int buf_size = 512;
char *buf = malloc(buf_size);
err = httpd_get_data(req, buf, buf_size);
require_noerr(err, save_out);
set_socket_status(buf);
char* status = "OK";
err = httpd_send_all_header(req, HTTP_RES_200, strlen(status), HTTP_CONTENT_HTML_STR);
require_noerr_action(err, save_out, app_httpd_log("ERROR: Unable to send http socket_status headers."));
err = httpd_send_body(req->sock, status, strlen(status));
require_noerr_action(err, save_out, app_httpd_log("ERROR: Unable to send http socket_status body."));
save_out:
if (buf) free(buf);
return err;
}
static int web_send_result_page(httpd_request_t *req)
{
OSStatus err = kNoErr;
bool para_succ = false;
int buf_size = 512;
char *buf;
char value_ssid[maxSsidLen];
char value_pass[maxKeyLen];
char value_user[maxNameLen];
mico_Context_t* context = NULL;
context = mico_system_context_get( );
buf = malloc(buf_size);
err = httpd_get_data(req, buf, buf_size);
require_noerr( err, Save_Out );
err = httpd_get_tag_from_post_data(buf, "SSID", value_ssid, maxSsidLen);
require_noerr( err, Save_Out );
err = httpd_get_tag_from_post_data(buf, "USER", value_user, maxNameLen);
require_noerr( err, Save_Out );
OSStatus err = kNoErr;
bool para_succ = false;
int buf_size = 512;
char *buf;
char value_ssid[maxSsidLen];
char value_pass[maxKeyLen];
char value_user[maxNameLen];
mico_Context_t* context = NULL;
context = mico_system_context_get( );
buf = malloc(buf_size);
err = httpd_get_data(req, buf, buf_size);
require_noerr( err, Save_Out );
err = httpd_get_tag_from_post_data(buf, "SSID", value_ssid, maxSsidLen);
require_noerr( err, Save_Out );
err = httpd_get_tag_from_post_data(buf, "USER", value_user, maxNameLen);
require_noerr( err, Save_Out );
if(!strncmp(value_ssid, "\0", 1))
goto Save_Out;
if(!strncmp(value_user, "\0", 1))
goto Save_Out;
strncpy(context->micoSystemConfig.ssid, value_ssid, maxSsidLen);
strncpy(user_config->user, value_user, maxNameLen);
err = httpd_get_tag_from_post_data(buf, "PASS", value_pass, maxKeyLen);
require_noerr( err, Save_Out );
strncpy(context->micoSystemConfig.key, value_pass, maxKeyLen);
strncpy(context->micoSystemConfig.user_key, value_pass, maxKeyLen);
context->micoSystemConfig.keyLength = strlen(context->micoSystemConfig.key);
context->micoSystemConfig.user_keyLength = strlen(context->micoSystemConfig.key);
context->micoSystemConfig.channel = 0;
memset(context->micoSystemConfig.bssid, 0x0, 6);
context->micoSystemConfig.security = SECURITY_TYPE_AUTO;
context->micoSystemConfig.dhcpEnable = true;
para_succ = true;
if(!strncmp(value_ssid, "\0", 1))
goto Save_Out;
if(!strncmp(value_user, "\0", 1))
goto Save_Out;
strncpy(context->micoSystemConfig.ssid, value_ssid, maxSsidLen);
strncpy(user_config->user, value_user, maxNameLen);
err = httpd_get_tag_from_post_data(buf, "PASS", value_pass, maxKeyLen);
require_noerr( err, Save_Out );
strncpy(context->micoSystemConfig.key, value_pass, maxKeyLen);
strncpy(context->micoSystemConfig.user_key, value_pass, maxKeyLen);
context->micoSystemConfig.keyLength = strlen(context->micoSystemConfig.key);
context->micoSystemConfig.user_keyLength = strlen(context->micoSystemConfig.key);
context->micoSystemConfig.channel = 0;
memset(context->micoSystemConfig.bssid, 0x0, 6);
context->micoSystemConfig.security = SECURITY_TYPE_AUTO;
context->micoSystemConfig.dhcpEnable = true;
para_succ = true;
Save_Out:
if(para_succ == true)
{
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(wifisuccess), HTTP_CONTENT_HTML_STR);
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wifisuccess headers.") );
err = httpd_send_body(req->sock, wifisuccess, sizeof(wifisuccess));
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wifisuccess body.") );
context->micoSystemConfig.configured = allConfigured;
mico_system_context_update(context);
mico_system_power_perform( context, eState_Software_Reset );
}
else
{
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(wififail), HTTP_CONTENT_HTML_STR);
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wififail headers.") );
err = httpd_send_body(req->sock, wififail, sizeof(wififail));
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wififail body.") );
}
exit:
if(buf) free(buf);
return err;
if(para_succ == true)
{
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(index_html), HTTP_CONTENT_HTML_STR);
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http index_html headers.") );
err = httpd_send_body(req->sock, index_html, sizeof(index_html));
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http index_html body.") );
context->micoSystemConfig.configured = allConfigured;
mico_system_context_update(context);
mico_system_power_perform( context, eState_Software_Reset );
}
else
{
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(index_html), HTTP_CONTENT_HTML_STR);
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http index_html headers.") );
err = httpd_send_body(req->sock, index_html, sizeof(index_html));
require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http index_html body.") );
}
exit:
if(buf) free(buf);
return err;
}
static int http_wifi_config(httpd_request_t *req)
{
OSStatus err = kNoErr;
int buf_size = 512;
int ssid_size = 32;
int key_size = 64;
char *buf = malloc(buf_size);
char *wifi_ssid = malloc(ssid_size);
char *wifi_key = malloc(key_size);
err = httpd_get_data(req, buf, buf_size);
require_noerr(err, save_out);
err = httpd_get_tag_from_post_data(buf, "ssid", wifi_ssid, ssid_size);
require_noerr(err, save_out);
err = httpd_get_tag_from_post_data(buf, "key", wifi_key, key_size);
require_noerr(err, save_out);
wifi_connect(wifi_ssid, wifi_key);
char* status = "OK";
err = httpd_send_all_header(req, HTTP_RES_200, strlen(status), HTTP_CONTENT_HTML_STR);
require_noerr_action(err, exit, app_httpd_log("ERROR: Unable to send http socket_status headers."));
err = httpd_send_body(req->sock, status, strlen(status));
require_noerr_action(err, exit, app_httpd_log("ERROR: Unable to send http socket_status body."));
exit:
return err;
save_out:
if (buf) free(buf);
if (ssid_size) free(ssid_size);
if (key_size) free(key_size);
return err;
}
struct httpd_wsgi_call g_app_handlers[] = {
{"/", HTTPD_HDR_DEFORT, 0, web_send_wifisetting_page, NULL, NULL, NULL},
{"/result.htm", HTTPD_HDR_DEFORT, 0, NULL, web_send_result_page, NULL, NULL},
{"/setting.htm", HTTPD_HDR_DEFORT, 0, web_send_wifisetting_page, NULL, NULL, NULL},
{"/", HTTPD_HDR_DEFORT, 0, http_get_index_page, NULL, NULL, NULL},
{"/result.htm", HTTPD_HDR_DEFORT, 0, NULL, web_send_result_page, NULL, NULL},
{"/socket", HTTPD_HDR_DEFORT, 0, http_get_socket_status, http_set_socket_status, NULL, NULL},
{"/wifi/config", HTTPD_HDR_DEFORT, 0, NULL, http_wifi_config, NULL, NULL},
};
static int g_app_handlers_no = sizeof(g_app_handlers)/sizeof(struct httpd_wsgi_call);
static void app_http_register_handlers()
{
int rc;
rc = httpd_register_wsgi_handlers(g_app_handlers, g_app_handlers_no);
if (rc) {
app_httpd_log("failed to register test web handler");
}
int rc;
rc = httpd_register_wsgi_handlers(g_app_handlers, g_app_handlers_no);
if (rc) {
app_httpd_log("failed to register test web handler");
}
}
static int _app_httpd_start()
{
OSStatus err = kNoErr;
app_httpd_log("initializing web-services");
/*Initialize HTTPD*/
if(is_http_init == false) {
err = httpd_init();
require_noerr_action( err, exit, app_httpd_log("failed to initialize httpd") );
is_http_init = true;
}
/*Start http thread*/
err = httpd_start();
if(err != kNoErr) {
app_httpd_log("failed to start httpd thread");
httpd_shutdown();
}
OSStatus err = kNoErr;
app_httpd_log("initializing web-services");
/*Initialize HTTPD*/
if(is_http_init == false) {
err = httpd_init();
require_noerr_action( err, exit, app_httpd_log("failed to initialize httpd") );
is_http_init = true;
}
/*Start http thread*/
err = httpd_start();
if(err != kNoErr) {
app_httpd_log("failed to start httpd thread");
httpd_shutdown();
}
exit:
return err;
return err;
}
int app_httpd_start( void )
{
OSStatus err = kNoErr;
err = _app_httpd_start();
require_noerr( err, exit );
if (is_handlers_registered == false) {
app_http_register_handlers();
is_handlers_registered = true;
}
OSStatus err = kNoErr;
err = _app_httpd_start();
require_noerr( err, exit );
if (is_handlers_registered == false) {
app_http_register_handlers();
is_handlers_registered = true;
}
exit:
return err;
return err;
}
int app_httpd_stop()
{
OSStatus err = kNoErr;
/* HTTPD and services */
app_httpd_log("stopping down httpd");
err = httpd_stop();
require_noerr_action( err, exit, app_httpd_log("failed to halt httpd") );
OSStatus err = kNoErr;
/* HTTPD and services */
app_httpd_log("stopping down httpd");
err = httpd_stop();
require_noerr_action( err, exit, app_httpd_log("failed to halt httpd") );
exit:
return err;
return err;
}