合并提交

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;
}

View File

@@ -31,12 +31,6 @@
******************************************************************************
*/
extern const unsigned char wififail[0xAC9];
extern const unsigned char wifisetting[3618];
extern const unsigned char wifisuccess[0x9BC];
int app_httpd_start( void );
int app_httpd_stop();

310
TC1/http_server/index.html Normal file
View File

@@ -0,0 +1,310 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html" charset="utf-8">
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport">
<title>Welcom TC1</title>
<style type="text/css">
fieldset{margin-bottom:10px;border:#000 solid 1px;}
input,select,button{padding:5px;border:#777 solid 1px;}
button{background:none;border:1px #777 solid;}
button:disabled,select:disabled,input:disabled{background:#bfbfbf;}
td{padding:5px;}
.socket input,.mode input{margin-right:-4px;}
.submit{padding:5px;margin-top:5px;}
.success{color:#58dc5c;}
.error{color:#ff2d2d;}
.info{color:#e700ea;}
.network div{padding:5px;}
.connect div{padding:5px;}
.right{text-align:right;}
.line{height:9px;border-bottom:1px solid #dedede;}
#power_div{margin-top:10px;border-left:1px solid #000;border-bottom:1px solid #000;}
#power_line{height:100px;position:relative;overflow:scroll;margin-top:-100px;}
.power_pre{position:absolute;bottom:0;float:left;height:76px;width:0;border-left:1px solid #000;border-right:1px solid #000;}
</style>
</head>
<body>
<fieldset class="socket">
<legend>Power Controll</legend>
<table id="socket_tb">
<tr>
<td>
<input type="checkbox" id="socket1" name="socket">
<label for="socket1">Socket-1</label>
</td>
<td>
<input type="checkbox" id="socket2" name="socket">
<label for="socket2">Socket-2</label>
</td>
<td>
<input type="checkbox" id="socket3" name="socket">
<label for="socket3">Socket-3</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" id="socket4" name="socket">
<label for="socket4">Socket-4</label>
</td>
<td>
<input type="checkbox" id="socket5" name="socket">
<label for="socket5">Socket-5</label>
</td>
<td>
<input type="checkbox" id="socket6" name="socket">
<label for="socket6">Socket-6</label>
</td>
</tr>
</table>
<div class="submit">
<button class="submit_bt" onclick="SetSocket()">Submit</button>
<span class="status_sp success">OK</span>
</div>
</fieldset>
<fieldset class="chart">
<legend>Power Status</legend>
<div id="power_div">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>
<div id="power_line">
<div class="power_pre" style="height:70px;left:20px;"></div>
<div class="power_pre" style="height:75px;left:40px;"></div>
<div class="power_pre" style="height:80px;left:60px;"></div>
<div class="power_pre" style="height:85px;left:80px;"></div>
<div class="power_pre" style="height:80px;left:100px;"></div>
<div class="power_pre" style="height:75px;left:120px;"></div>
<div class="power_pre" style="height:70px;left:140px;"></div>
<div class="power_pre" style="height:65px;left:160px;"></div>
<div class="power_pre" style="height:70px;left:180px;"></div>
<div class="power_pre" style="height:75px;left:200px;"></div>
<div class="power_pre" style="height:80px;left:220px;"></div>
<div class="power_pre" style="height:85px;left:240px;"></div>
<div class="power_pre" style="height:90px;left:260px;"></div>
<div class="power_pre" style="height:85px;left:280px;"></div>
<div class="power_pre" style="height:80px;left:300px;"></div>
<div class="power_pre" style="height:78px;left:320px;"></div>
<div class="power_pre" style="height:76px;left:340px;"></div>
<div class="power_pre" style="height:72px;left:360px;"></div>
<div class="power_pre" style="height:85px;left:380px;"></div>
<div class="power_pre" style="height:80px;left:400px;"></div>
<div class="power_pre" style="height:75px;left:420px;"></div>
<div class="power_pre" style="height:70px;left:440px;"></div>
<div class="power_pre" style="height:65px;left:460px;"></div>
<div class="power_pre" style="height:70px;left:480px;"></div>
<div class="power_pre" style="height:75px;left:500px;"></div>
<div class="power_pre" style="height:80px;left:520px;"></div>
<div class="power_pre" style="height:85px;left:540px;"></div>
<div class="power_pre" style="height:90px;left:560px;"></div>
<div class="power_pre" style="height:85px;left:580px;"></div>
<div class="power_pre" style="height:80px;left:600px;"></div>
<div class="power_pre" style="height:78px;left:620px;"></div>
<div class="power_pre" style="height:76px;left:640px;"></div>
<div class="power_pre" style="height:72px;left:660px;"></div>
<div class="power_pre" style="height:85px;left:680px;"></div>
<div class="power_pre" style="height:80px;left:700px;"></div>
</div>
</fieldset>
<fieldset class="network">
<legend>Network config</legend>
<table>
<tr>
<td class="right">SSID:</td>
<td>
<select id="wifi" onchange="SelectWifi()">
<option value="0">Manual</option>
<option value="1">CMCC</option>
<option value="2">China-Net</option>
<option value="3">CMCT</option>
</select>
<button id="rescan">Rescan</button>
</td>
</tr>
<tr>
<td class="right">SSID:</td>
<td>
<input id="ssid" type="text" onchange="ChangeInput(1)"/>
</td>
</tr>
<tr>
<td class="right">PSWD:</td>
<td>
<input id="pswd" type="password" onchange="ChangeInput(2)"/>
</td>
</tr>
<tr>
<td class="right">MODE:</td>
<td class="mode">
<input type="radio" id="mode1" name="mode" onchange="ChangeMode(1)">
<label for="mode1">AP</label>
<input type="radio" id="mode2" name="mode" onchange="ChangeMode(2)">
<label for="mode2">STATION</label>
</td>
</tr>
</table>
<div class="submit">
<button class="submit_bt" onclick="SubmitNetwork()">Submit</button>
<span class="status_sp success">OK</span>
</div>
</fieldset>
<fieldset class="connect">
<legend>Connect Status</legend>
<table>
<tr>
<td class="right">IP:</td>
<td class="info">192.168.33.108</td>
</tr>
<tr>
<td class="right">Mask:</td>
<td class="info">255.255.255.0</td>
</tr>
<tr>
<td class="right">Gateway:</td>
<td class="info">192.168.33.1</td>
</tr>
</table>
</fieldset>
<script type="text/javascript">
function Ajax(url, onsuccess, type, data) {
var xml_http = new XMLHttpRequest();
xml_http.open(type, url, true);
xml_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml_http.onreadystatechange = function () {
if (xml_http.readyState == 4) {
if (xml_http.status == 200) {
onsuccess(xml_http.responseText);
}
else {
alert("Ajax error: " + xml_http.status);
}
}
};
if (data != null && type == "POST") {
xml_http.send(data);
} else {
xml_http.send();
}
}
function HttpGet(url, onsuccess) {
Ajax(url, onsuccess, "GET", null);
}
function HttpPost(url, onsuccess, data) {
Ajax(url, onsuccess, "POST", data);
}
var submit_bts = document.getElementsByClassName("submit_bt");
var info_spans = document.getElementsByClassName("status_sp");
var socket_tb = document.getElementById("socket_tb");
var checkboxs = socket_tb.getElementsByTagName("input");
var rescan_btn = document.getElementById("rescan");
var ssid_slt = document.getElementById("wifi");
var ssid_ipt = document.getElementById("ssid");
var pswd_ipt = document.getElementById("pswd");
var mode_rd1 = document.getElementById("mode1");
var mode_rd2 = document.getElementById("mode2");
HttpGet("/socket", function (re) {
var status = re;
var status_arr = status.split(",");
for (var i = 0; i < status_arr.length; i++) {
checkboxs[i].checked = status_arr[i] == "1";
}
});
function SetOK(i) {
submit_bts[i].disabled = false;
info_spans[i].className = "status_sp success";
info_spans[i].innerHTML = "OK";
}
function SetIng(i) {
submit_bts[i].disabled = true;
info_spans[i].className = "status_sp info";
info_spans[i].innerHTML = "ing...";
}
function SetSocket() {
SetIng(0);
var sockets_st = "";
for (var i = 0; i < checkboxs.length; i++) {
sockets_st += (checkboxs[i].checked ? "1," : "0,");
}
HttpPost("/socket", function (re) {
SetOK(0);
}, sockets_st);
}
function SelectWifi() {
ssid_ipt.disabled = ssid_slt.value != "0";
}
var mode = 0;
var ap_name = "TC1-AP";
var ap_pswd = "12345678";
var station_name = "";
var station_pswd = "";
function ChangeMode(m) {
if (mode == m) return;
if (m == 1) {
ssid_slt.value = "0";
ssid_slt.disabled = true;
rescan_btn.disabled = true;
ssid_ipt.disabled = false;
ssid_ipt.value = ap_name;
pswd_ipt.value = ap_pswd;
} else {
ssid_slt.value = "0";
ssid_slt.disabled = false;
rescan_btn.disabled = false;
ssid_ipt.disabled = false;
ssid_ipt.value = station_name;
pswd_ipt.value = station_pswd;
}
mode = m;
}
ChangeMode(1);
mode_rd1.checked = true;
function ChangeInput(type) {
if(mode == 1) {
ap_name = ssid_ipt.value;
ap_pswd = pswd_ipt.value;
} else {
station_name = ssid_ipt.value;
station_pswd = pswd_ipt.value;
}
}
function SubmitNetwork() {
if(mode == 1) {
} else {
var params = "ssid="+station_name+"&key="+station_pswd;
HttpPost("/wifi/config", function (re) {
alert(re);
}, params);
}
}
</script>
</body>
</html>

25
TC1/http_server/test.py Normal file
View File

@@ -0,0 +1,25 @@
#! /bin/env python3
import os
import glob
import binascii
import gzip
try:
io = __import__("io").BytesIO
except:
io = __import__("StringIO").StringIO
for fn in glob.glob('*.html'):
s = open(fn, 'rb').read()
dat = io()
with gzip.GzipFile(fileobj=dat, mode="w") as f:
f.write(s)
dat = dat.getvalue()
try:
s = ','.join(["0x%02x" % c for c in dat])
except:
s = ','.join(["0x"+binascii.hexlify(c) for c in dat])
fn = fn.replace('.', '_')
print("const unsigned char %s[0x%x] = {%s};" % (fn, len(dat), s))

File diff suppressed because one or more lines are too long