send_http, 扫描接口

This commit is contained in:
zogodo
2019-09-28 21:42:51 +08:00
parent fd1e7402f5
commit 5607f25d09
5 changed files with 87 additions and 50 deletions

View File

@@ -39,16 +39,9 @@
#include "app_httpd.h"
#include "user_gpio.h"
#include "user_wifi.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)
static bool is_http_init;
static bool is_handlers_registered;
struct httpd_wsgi_call g_app_handlers[];
@@ -67,22 +60,8 @@ exit:
return err;
}
#define TC1_STATUS_JSON \
"{\
'sockets':'%s',\
'mode':%d,\
'station_ssid':'%s',\
'station_pwd':'%s',\
'ap_ssid':'%s',\
'ap_pwd':'%s',\
'ip':'%s',\
'mask':'%s',\
'gateway':'%s'\
}"
static int http_get_tc1_status(httpd_request_t *req)
{
OSStatus err = kNoErr;
const unsigned char* sockets = get_socket_status();
char* ap_name = "TC1-AP";
char* ap_pwd = "12345678";
@@ -94,11 +73,8 @@ static int http_get_tc1_status(httpd_request_t *req)
sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key,
ap_name, ap_pwd, ip, mask, gateway);
err = httpd_send_all_header(req, HTTP_RES_200, strlen(tc1_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, (const unsigned char*)tc1_status, strlen(tc1_status));
require_noerr_action(err, exit, app_httpd_log("ERROR: Unable to send http socket_status body."));
OSStatus err = kNoErr;
send_http(tc1_status, strlen(tc1_status), exit, &err);
exit:
return err;
@@ -112,19 +88,13 @@ static int http_set_socket_status(httpd_request_t *req)
char *buf = malloc(buf_size);
err = httpd_get_data(req, buf, buf_size);
require_noerr(err, save_out);
require_noerr(err, exit);
set_socket_status(buf);
char* status = "OK";
send_http("OK", 2, exit, &err);
err = httpd_send_all_header(req, HTTP_RES_200, strlen(socket_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, (const unsigned char*)status, strlen(socket_status));
require_noerr_action(err, save_out, app_httpd_log("ERROR: Unable to send http socket_status body."));
save_out:
exit:
if (buf) free(buf);
return err;
}
@@ -157,39 +127,48 @@ static int http_set_wifi_config(httpd_request_t *req)
char *wifi_key = malloc(key_size);
err = httpd_get_data(req, buf, buf_size);
require_noerr(err, save_out);
require_noerr(err, exit);
err = httpd_get_tag_from_post_data(buf, "ssid", wifi_ssid, ssid_size);
require_noerr(err, save_out);
require_noerr(err, exit);
err = httpd_get_tag_from_post_data(buf, "key", wifi_key, key_size);
require_noerr(err, save_out);
require_noerr(err, exit);
wifi_connect(wifi_ssid, wifi_key);
char* status = "OK";
err = httpd_send_all_header(req, HTTP_RES_200, strlen(socket_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, (const unsigned char*)status, strlen(socket_status));
require_noerr_action(err, exit, app_httpd_log("ERROR: Unable to send http socket_status body."));
send_http("OK", 2, exit, &err);
exit:
return err;
save_out:
if (buf) free(buf);
if (wifi_ssid) free(wifi_ssid);
if (wifi_key) free(wifi_key);
return err;
}
static int http_get_wifi_scan(httpd_request_t *req)
{
OSStatus err = kNoErr;
send_http("OK", 2, exit, &err);
exit:
return err;
}
static int http_set_wifi_scan(httpd_request_t *req)
{
micoWlanStartScanAdv();
OSStatus err = kNoErr;
send_http("OK", 2, exit, &err);
exit:
return err;
}
struct httpd_wsgi_call g_app_handlers[] = {
{"/", HTTPD_HDR_DEFORT, 0, http_get_index_page, NULL, NULL, NULL},
{"/socket", HTTPD_HDR_DEFORT, 0, NULL, http_set_socket_status, NULL, NULL},
{"/status", HTTPD_HDR_DEFORT, 0, http_get_tc1_status, NULL, NULL, NULL},
{"/wifi/config", HTTPD_HDR_DEFORT, 0, http_get_wifi_config, http_set_wifi_config, NULL, NULL},
{"/wifi/scan", HTTPD_HDR_DEFORT, 0, http_get_wifi_scan, http_set_wifi_scan, NULL, NULL},
};
static int g_app_handlers_no = sizeof(g_app_handlers)/sizeof(struct httpd_wsgi_call);

View File

@@ -31,6 +31,31 @@
******************************************************************************
*/
#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)
#define send_http(DATA, LEN, LABEL, P_ERR) \
*(P_ERR) = httpd_send_all_header(req, HTTP_RES_200, LEN , HTTP_CONTENT_HTML_STR); \
require_noerr_action(*(P_ERR), LABEL, app_httpd_log("ERROR: Unable to send http DATA headers.")); \
*(P_ERR) = httpd_send_body(req->sock, (const unsigned char*)DATA, LEN); \
require_noerr_action(*(P_ERR), LABEL, app_httpd_log("ERROR: Unable to send http DATA body.")); \
#define TC1_STATUS_JSON \
"{\
'sockets':'%s',\
'mode':%d,\
'station_ssid':'%s',\
'station_pwd':'%s',\
'ap_ssid':'%s',\
'ap_pwd':'%s',\
'ip':'%s',\
'mask':'%s',\
'gateway':'%s'\
}"
int app_httpd_start(void);
int app_httpd_stop();

View File

@@ -129,7 +129,7 @@
<option value="2">China-Net</option>
<option value="3">CMCT</option>
</select>
<button id="rescan">Rescan</button>
<button id="rescan" onclick="Rescan()">Rescan</button>
</td>
</tr>
<tr>
@@ -319,6 +319,29 @@ function SubmitNetwork() {
}
}
var get_ret = false;
function Rescan() {
HttpPost("/wifi/scan", function (re) {
alert(re);
if (re == "OK") {
get_ret = true;
window.setTimeout(GetScanResult, 1000);
}
});
}
var tt = 5;
function GetScanResult() {
HttpGet("/wifi/scan", function (re) {
console.log(re);
if (get_ret && tt <= 0) {
get_ret = 0
}
window.setTimeout(GetScanResult, 1000);
tt--;
});
}
</script>
</body>
</html>

File diff suppressed because one or more lines are too long

View File

@@ -73,6 +73,13 @@ static void wifi_status_callback(WiFiEvent status, void* arg)
}
}
//wifi扫描结果回调
void wifi_scan_callback(ScanResult *pApList, mico_Context_t * const inContext)
{
os_log("wifi_scan_callback ApNum[%d] ApList[0](%s)", (int)pApList[0].ApNum, pApList[0].ApList[0].ssid);
}
//100ms定时器回调
static void wifi_led_timer_callback(void* arg)
{
@@ -133,6 +140,9 @@ void wifi_init(void)
mico_system_notify_register(mico_notify_DHCP_COMPLETED, (void *) wifi_get_ip_callback, NULL);
//wifi连接状态改变回调
mico_system_notify_register(mico_notify_WIFI_STATUS_CHANGED, (void*) wifi_status_callback, NULL);
//wifi扫描结果回调
mico_system_notify_register(mico_notify_WIFI_SCAN_COMPLETED, (void*)wifi_scan_callback, NULL);
//sntp_init();
//启动定时器开始进行wifi连接
if (!mico_rtos_is_timer_running(&wifi_led_timer)) mico_rtos_start_timer(&wifi_led_timer);