714 lines
27 KiB
C
714 lines
27 KiB
C
// Copyright (c) 2022 Cesanta Software Limited
|
|
// All rights reserved
|
|
//
|
|
// UI example
|
|
// It implements the following endpoints:
|
|
// /api/config/get - respond with current config
|
|
// /api/config/set - POST a config change
|
|
// any other URI serves static files from s_root_dir
|
|
// Data and results are JSON strings
|
|
|
|
/**
|
|
* @file webserver_project.c
|
|
* @brief support webserver_project for XiUOS
|
|
* @version 3.0
|
|
* @author AIIT XUOS Lab
|
|
* @date 2023-11-07
|
|
*/
|
|
|
|
/*************************************************
|
|
File name: webserver_project.c
|
|
Description: support webserver_project for XiUOS
|
|
Others:
|
|
History:
|
|
1. Date: 2023-11-07
|
|
Author: AIIT XUOS Lab
|
|
Modification:
|
|
1、support xishutong-arm32 board, using W5500 to support webserver.
|
|
*************************************************/
|
|
|
|
|
|
#include "ip_addr.h"
|
|
#include "mongoose.h"
|
|
#include "netdev.h"
|
|
#include <sys_arch.h>
|
|
#include <lwip/sockets.h>
|
|
#include "lwip/sys.h"
|
|
#include <adapter.h>
|
|
|
|
/*******************************************************************************
|
|
* Local variable definitions ('static')
|
|
******************************************************************************/
|
|
char index_path[] = "login.html";
|
|
|
|
static const char* s_http_addr = "http://192.168.131.88:8000"; // HTTP port
|
|
static const char* s_root_dir = "webserver";
|
|
static const char* s_enable_hexdump = "no";
|
|
static const char* s_ssi_pattern = "#.html";
|
|
static const char* web_version = "XiUOS WebServer 1.0";
|
|
|
|
static struct netdev* p_netdev_webserver;
|
|
static struct netdev* p_netdev_ethernet;
|
|
static pthread_t tid;
|
|
|
|
#define WB_EVENT_TASK_STACK_SIZE 4096
|
|
#define WB_EVENT_TASK_PRIO 20
|
|
|
|
#define WB_4G_CONNECT 0x0001
|
|
#define WB_4G_DISCONNECT 0x0002
|
|
#define WB_MQTT_CONNECT 0x0004
|
|
#define WB_MQTT_DISCONNECT 0x0008
|
|
#define WB_LORA_CONNECT 0x0010
|
|
#define WB_LORA_DISCONNECT 0x0020
|
|
#define WB_ETHERNET_CONNECT 0x0040
|
|
#define WB_ETHERNET_DISCONNECT 0x0080
|
|
#define WB_EVENT_ALL (WB_4G_CONNECT | WB_4G_DISCONNECT | \
|
|
WB_MQTT_CONNECT | WB_MQTT_DISCONNECT | \
|
|
WB_LORA_CONNECT | WB_LORA_DISCONNECT | \
|
|
WB_ETHERNET_CONNECT | WB_ETHERNET_DISCONNECT)
|
|
|
|
static int wb_event;
|
|
static unsigned int status = 0;
|
|
static pthread_t wb_event_task;
|
|
|
|
/*define device info*/
|
|
static const char* device_name = "矽数通4G";
|
|
static const char* device_type = "xishutong-arm32";
|
|
static const char* device_serial_num = "123456789";
|
|
|
|
/*define webserver info*/
|
|
static struct webserver_config {
|
|
char *ip, *mask, *gw, *dns;
|
|
} webserver_config;
|
|
|
|
/*define interface info*/
|
|
static struct rs485_config {
|
|
int baud_rate;
|
|
int data_bit;
|
|
int stop_bit;
|
|
int parity;
|
|
} rs485_config;
|
|
int rs485_uart_fd = -1;
|
|
|
|
#define RS485_DEVICE_PATH "/dev/usart4_dev4"
|
|
|
|
/*define net 4G info*/
|
|
static struct net_4g_info {
|
|
char map_ip[20];
|
|
char connect_ip[20];
|
|
char operator[20];
|
|
char signal_strength[20];
|
|
char connect_port[20];
|
|
} net_4g_info;
|
|
|
|
struct Adapter* adapter;
|
|
|
|
static struct net_4g_mqtt_info {
|
|
char topic[20];
|
|
char username[20];
|
|
char password[20];
|
|
int client_id;
|
|
int connect_status;
|
|
} net_4g_mqtt_info;
|
|
|
|
/*define net LoRa info*/
|
|
struct net_lora_info
|
|
{
|
|
uint32_t frequency; // frequency
|
|
uint8_t sf; // spreadfactor
|
|
uint8_t bw; // bandwidth
|
|
|
|
uint32_t connect_status; //connect status
|
|
uint8_t lora_init_flag; //if 1 means already init
|
|
} net_lora_info;
|
|
|
|
/*define net Ethernet info*/
|
|
static struct net_ethernet_info {
|
|
char ethernetIp[20];
|
|
char ethernetNetmask[20];
|
|
char ethernetGateway[20];
|
|
char ethernetDNS[20];
|
|
char targetIp[20];
|
|
char targetPort[20];
|
|
char targetGateway[20];
|
|
char targetDNS[20];
|
|
int connect_status;
|
|
} net_ethernet_info;
|
|
|
|
static int socket_fd = -1;
|
|
|
|
static char tcp_ethernet_ipaddr[] = {192, 168, 130, 77};
|
|
static char tcp_ethernet_netmask[] = {255, 255, 254, 0};
|
|
static char tcp_ethernet_gwaddr[] = {192, 168, 130, 1};
|
|
|
|
static uint16_t tcp_socket_port = 8888;
|
|
|
|
/*define PLC info*/
|
|
static char *plc_json;
|
|
#define JSON_FILE_NAME "test_recipe.json"
|
|
|
|
/*******************************************************************************
|
|
* Function implementation - define interface info
|
|
******************************************************************************/
|
|
static void Rs485InitConfigure(void)
|
|
{
|
|
rs485_uart_fd = PrivOpen(RS485_DEVICE_PATH, O_RDWR);
|
|
if (rs485_uart_fd < 0) {
|
|
printf("open rs485 %s fd error:%d\n", RS485_DEVICE_PATH, rs485_uart_fd);
|
|
return;
|
|
}
|
|
printf("uart %s open success\n", RS485_DEVICE_PATH);
|
|
|
|
struct SerialDataCfg uart_cfg;
|
|
memset(&uart_cfg, 0, sizeof(struct SerialDataCfg));
|
|
|
|
rs485_config.baud_rate = BAUD_RATE_115200;
|
|
rs485_config.data_bit = DATA_BITS_8;
|
|
rs485_config.stop_bit = STOP_BITS_1;
|
|
rs485_config.parity = PARITY_NONE;
|
|
|
|
uart_cfg.serial_baud_rate = rs485_config.baud_rate;
|
|
uart_cfg.serial_data_bits = rs485_config.data_bit;
|
|
uart_cfg.serial_stop_bits = rs485_config.stop_bit;
|
|
uart_cfg.serial_parity_mode = rs485_config.parity;
|
|
uart_cfg.serial_bit_order = BIT_ORDER_LSB;
|
|
uart_cfg.serial_invert_mode = NRZ_NORMAL;
|
|
uart_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
|
uart_cfg.serial_timeout = -1;
|
|
uart_cfg.is_ext_uart = 0;
|
|
uart_cfg.dev_recv_callback = NULL;
|
|
|
|
struct PrivIoctlCfg ioctl_cfg;
|
|
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
|
ioctl_cfg.args = (void *)&uart_cfg;
|
|
|
|
if (0 != PrivIoctl(rs485_uart_fd, OPE_INT, &ioctl_cfg)) {
|
|
printf("ioctl uart fd error %d\n", rs485_uart_fd);
|
|
PrivClose(rs485_uart_fd);
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void Rs485Configure(int baud_rate, int data_bit, int stop_bit, int parity)
|
|
{
|
|
if (rs485_uart_fd < 0) {
|
|
rs485_uart_fd = PrivOpen(RS485_DEVICE_PATH, O_RDWR);
|
|
if (rs485_uart_fd < 0) {
|
|
printf("open rs485 %s fd error:%d\n", RS485_DEVICE_PATH, rs485_uart_fd);
|
|
return;
|
|
}
|
|
printf("uart %s open success\n", RS485_DEVICE_PATH);
|
|
}
|
|
|
|
struct SerialDataCfg uart_cfg;
|
|
memset(&uart_cfg, 0, sizeof(struct SerialDataCfg));
|
|
|
|
uart_cfg.serial_baud_rate = baud_rate;
|
|
uart_cfg.serial_data_bits = data_bit;
|
|
uart_cfg.serial_stop_bits = stop_bit;
|
|
uart_cfg.serial_parity_mode = parity;
|
|
uart_cfg.serial_bit_order = BIT_ORDER_LSB;
|
|
uart_cfg.serial_invert_mode = NRZ_NORMAL;
|
|
uart_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
|
uart_cfg.serial_timeout = -1;
|
|
uart_cfg.is_ext_uart = 0;
|
|
uart_cfg.dev_recv_callback = NULL;
|
|
|
|
struct PrivIoctlCfg ioctl_cfg;
|
|
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
|
ioctl_cfg.args = (void *)&uart_cfg;
|
|
|
|
if (0 != PrivIoctl(rs485_uart_fd, OPE_INT, &ioctl_cfg)) {
|
|
printf("ioctl uart fd error %d\n", rs485_uart_fd);
|
|
PrivClose(rs485_uart_fd);
|
|
return;
|
|
}
|
|
|
|
printf("Board RS485 changed to [br: %d, data: %d, stop: %d, party: %d]\n",
|
|
baud_rate, data_bit, stop_bit, parity);
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function implementation - define net 4G info
|
|
******************************************************************************/
|
|
static void Net4gGetInfo(char *ip, char *operator, char *signal_strength)
|
|
{
|
|
//to do
|
|
}
|
|
|
|
static void Net4gConnect(void)
|
|
{
|
|
int ec200a_baud_rate = 115200;
|
|
const char *send_msg = "Adapter_4G Test";
|
|
adapter->socket.socket_id = 0;
|
|
|
|
AdapterDeviceOpen(adapter);
|
|
AdapterDeviceControl(adapter, OPE_INT, &ec200a_baud_rate);
|
|
AdapterDeviceConnect(adapter, CLIENT, net_4g_info.connect_ip, net_4g_info.connect_port, IPV4);
|
|
}
|
|
|
|
static void Net4gDisconnect(void)
|
|
{
|
|
uint8_t priv_net_group = IP_PROTOCOL;
|
|
AdapterDeviceDisconnect(adapter, &priv_net_group);
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function implementation - define net 4G MQTT info
|
|
******************************************************************************/
|
|
static void NetMqttConnect(void)
|
|
{
|
|
//to do
|
|
}
|
|
|
|
static void NetMqttDisconnect(void)
|
|
{
|
|
//to do
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function implementation - define net LoRa info
|
|
******************************************************************************/
|
|
static void NetLoraConnect(void)
|
|
{
|
|
char* init_params[2] = {"TestLoraRadio", "probe"};
|
|
char* tx_params[4] = {"TestLoraRadio", "tx", "1", "2000"};
|
|
extern int TestLoraRadio(int argc, char *argv[]);
|
|
|
|
if (0 == net_lora_info.lora_init_flag) {
|
|
TestLoraRadio(2, init_params);
|
|
net_lora_info.lora_init_flag = 1;
|
|
}
|
|
|
|
TestLoraRadio(4, tx_params);
|
|
net_lora_info.connect_status = 1;
|
|
}
|
|
|
|
static void NetLoraDisconnect(void)
|
|
{
|
|
char* disconnect_params[2] = {"TestLoraRadio", "txdone"};
|
|
extern int TestLoraRadio(int argc, char *argv[]);
|
|
TestLoraRadio(2, disconnect_params);
|
|
net_lora_info.connect_status = 0;
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function implementation - define net Ethernet info
|
|
******************************************************************************/
|
|
static void TcpClientConnect(void)
|
|
{
|
|
int cnt = 20;
|
|
int ret;
|
|
char send_msg[128];
|
|
|
|
sscanf(net_ethernet_info.targetPort, "%d", &tcp_socket_port);
|
|
|
|
memset(send_msg, 0, sizeof(send_msg));
|
|
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (socket_fd < 0) {
|
|
printf("Socket error\n");
|
|
return;
|
|
}
|
|
|
|
struct sockaddr_in tcp_sock;
|
|
tcp_sock.sin_family = AF_INET;
|
|
tcp_sock.sin_port = htons(tcp_socket_port);
|
|
tcp_sock.sin_addr.s_addr = inet_addr(net_ethernet_info.targetIp);
|
|
|
|
memset(&(tcp_sock.sin_zero), 0, sizeof(tcp_sock.sin_zero));
|
|
|
|
int keepalive = 1;
|
|
int keepidle = 30;
|
|
int keepinterval = 5;
|
|
int keepcount = 5;
|
|
setsockopt(socket_fd,
|
|
IPPROTO_TCP, /* set option at TCP level */
|
|
TCP_KEEPALIVE, /* name of option */
|
|
(void*)&keepalive, /* the cast is historical cruft */
|
|
sizeof(keepalive)); /* length of option value */
|
|
|
|
setsockopt(socket_fd,
|
|
IPPROTO_TCP, /* set option at TCP level */
|
|
TCP_KEEPIDLE, /* name of option */
|
|
(void*)&keepidle, /* the cast is historical cruft */
|
|
sizeof(keepidle)); /* length of option value */
|
|
|
|
setsockopt(socket_fd,
|
|
IPPROTO_TCP, /* set option at TCP level */
|
|
TCP_KEEPINTVL, /* name of option */
|
|
(void*)&keepinterval, /* the cast is historical cruft */
|
|
sizeof(keepinterval)); /* length of option value */
|
|
|
|
setsockopt(socket_fd,
|
|
IPPROTO_TCP, /* set option at TCP level */
|
|
TCP_KEEPCNT, /* name of option */
|
|
(void*)&keepcount, /* the cast is historical cruft */
|
|
sizeof(keepcount)); /* length of option value */
|
|
|
|
ret = connect(socket_fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr));
|
|
if (ret < 0) {
|
|
printf("Unable to connect %s:%d = %d\n", net_ethernet_info.targetIp, tcp_socket_port, ret);
|
|
closesocket(socket_fd);
|
|
return;
|
|
}
|
|
|
|
printf("TCP connect %s:%d success, start to send.\n", net_ethernet_info.targetIp, tcp_socket_port);
|
|
net_ethernet_info.connect_status = 1;
|
|
|
|
while (cnt --) {
|
|
printf("Lwip client is running.\n");
|
|
snprintf(send_msg, sizeof(send_msg), "TCP test package times %d\r\n", cnt);
|
|
send(socket_fd, send_msg, strlen(send_msg), 0);
|
|
printf("Send tcp msg: %s ", send_msg);
|
|
PrivTaskDelay(1000);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
static void TcpClientDisconnect(void)
|
|
{
|
|
printf("TCP disconnect\n");
|
|
closesocket(socket_fd);
|
|
net_ethernet_info.connect_status = 0;
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function implementation - define plc info
|
|
******************************************************************************/
|
|
static void PlcInfoWriteToSd(const char *json)
|
|
{
|
|
extern int GetSdMountStatus(void);
|
|
if(GetSdMountStatus()) {
|
|
KPrintf("------Start download json file !------\r\n");
|
|
|
|
FILE *fp = fopen(JSON_FILE_NAME, "w");
|
|
if(fp == NULL) {
|
|
printf("%s file create failed,please check!\r\n", JSON_FILE_NAME);
|
|
} else {
|
|
printf("%s file create success!\r\n", JSON_FILE_NAME);
|
|
fprintf(fp, "%s", json);
|
|
fclose(fp);
|
|
}
|
|
printf("------download %s file done!------\r\n", JSON_FILE_NAME);
|
|
}
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function implementation - WebserverEventTask
|
|
******************************************************************************/
|
|
static void *WebserverEventTask(void *arg)
|
|
{
|
|
wb_event = PrivEventCreate(LINKLIST_FLAG_FIFO);
|
|
while(1) {
|
|
if (0 == PrivEventProcess(wb_event, WB_EVENT_ALL, EVENT_OR | EVENT_AUTOCLEAN, 0, &status)) {
|
|
switch( status ) {
|
|
case WB_4G_CONNECT:
|
|
Net4gConnect();
|
|
break;
|
|
case WB_4G_DISCONNECT:
|
|
Net4gDisconnect();
|
|
break;
|
|
case WB_MQTT_CONNECT:
|
|
NetMqttConnect();
|
|
break;
|
|
case WB_MQTT_DISCONNECT:
|
|
NetMqttDisconnect();
|
|
break;
|
|
case WB_LORA_CONNECT:
|
|
NetLoraConnect();
|
|
break;
|
|
case WB_LORA_DISCONNECT:
|
|
NetLoraDisconnect();
|
|
break;
|
|
case WB_ETHERNET_CONNECT:
|
|
TcpClientConnect();
|
|
break;
|
|
case WB_ETHERNET_DISCONNECT:
|
|
TcpClientDisconnect();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void WbEventInit(void)
|
|
{
|
|
char task_name[] = "wb_event_task";
|
|
pthread_args_t args;
|
|
args.pthread_name = task_name;
|
|
|
|
pthread_attr_t attr;
|
|
attr.schedparam.sched_priority = WB_EVENT_TASK_PRIO;
|
|
attr.stacksize = WB_EVENT_TASK_STACK_SIZE;
|
|
|
|
PrivTaskCreate(&wb_event_task, &attr, &WebserverEventTask, (void *)&args);
|
|
PrivTaskStartup(&wb_event_task);
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function implementation - webserver
|
|
******************************************************************************/
|
|
// Try to update a single configuration value
|
|
static void update_config(struct mg_str json, const char* path, char** value)
|
|
{
|
|
char* jval;
|
|
if ((jval = mg_json_get_str(json, path)) != NULL) {
|
|
free(*value);
|
|
*value = strdup(jval);
|
|
}
|
|
}
|
|
|
|
static void update_config_array(struct mg_str json, const char* path, char* value)
|
|
{
|
|
char* jval;
|
|
if ((jval = mg_json_get_str(json, path)) != NULL) {
|
|
sprintf(value, "%s", jval);
|
|
}
|
|
}
|
|
|
|
static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data)
|
|
{
|
|
if (ev == MG_EV_HTTP_MSG) {
|
|
struct mg_http_message *hm = (struct mg_http_message*)ev_data, tmp = { 0 };
|
|
/*define device info*/
|
|
if (mg_http_match_uri(hm, "/getSystemInfo")) {
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
|
"{%m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%m}\n",
|
|
MG_ESC("deviceName"), MG_ESC(device_name),
|
|
MG_ESC("deviceType"), MG_ESC(device_type),
|
|
MG_ESC("deviceNo"), MG_ESC(device_serial_num),
|
|
MG_ESC("ip"), MG_ESC(webserver_config.ip),
|
|
MG_ESC("netmask"), MG_ESC(webserver_config.mask),
|
|
MG_ESC("gateway"), MG_ESC(webserver_config.gw));
|
|
}
|
|
/*define webserver info*/
|
|
else if (mg_http_match_uri(hm, "/setNetInfo")) {
|
|
struct mg_str json = hm->body;
|
|
printf("json: %s\n", json.ptr);
|
|
update_config(json, "$.ip", &webserver_config.ip);
|
|
update_config(json, "$.netmask", &webserver_config.mask);
|
|
update_config(json, "$.gateway", &webserver_config.gw);
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
|
|
|
|
ip_addr_t ipaddr, maskaddr, gwaddr;
|
|
inet_aton(webserver_config.ip, &ipaddr);
|
|
inet_aton(webserver_config.mask, &maskaddr);
|
|
inet_aton(webserver_config.gw, &gwaddr);
|
|
p_netdev_webserver->ops->set_addr_info(p_netdev_webserver, &ipaddr, &maskaddr, &gwaddr);
|
|
|
|
printf("Board Webserver Net changed to [IP: %s, Mask: %s, GW: %s]\n",
|
|
webserver_config.ip,
|
|
webserver_config.mask,
|
|
webserver_config.gw);
|
|
}
|
|
/*define interface info*/
|
|
else if (mg_http_match_uri(hm, "/interface/get485Info")) {
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
|
"{%m:%d, %m:%d, %m:%d, %m:%d}\n",
|
|
MG_ESC("baudRate"), rs485_config.baud_rate,
|
|
MG_ESC("wordLength"), rs485_config.data_bit,
|
|
MG_ESC("stopBits"), rs485_config.stop_bit,
|
|
MG_ESC("parity"), rs485_config.parity);
|
|
} else if (mg_http_match_uri(hm, "/interface/set485Info")) {
|
|
struct mg_str json = hm->body;
|
|
printf("json: %s\n", json.ptr);
|
|
rs485_config.baud_rate = mg_json_get_long(json, "$.baudRate", 0);
|
|
rs485_config.data_bit = mg_json_get_long(json, "$.wordLength", 0);
|
|
rs485_config.stop_bit = mg_json_get_long(json, "$.stopBits", 0);
|
|
rs485_config.parity = mg_json_get_long(json, "$.parity", 0);
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
|
|
|
|
Rs485Configure(rs485_config.baud_rate, rs485_config.data_bit, rs485_config.stop_bit, rs485_config.parity);
|
|
}
|
|
/*define net 4G info*/
|
|
else if (mg_http_match_uri(hm, "/net/get4gInfo")) {
|
|
|
|
Net4gGetInfo(net_4g_info.map_ip, net_4g_info.operator, net_4g_info.signal_strength);
|
|
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
|
"{%m:%m, %m:%m, %m:%d}\n",
|
|
MG_ESC("mapIp"), MG_ESC(net_4g_info.map_ip),
|
|
MG_ESC("operator"), MG_ESC(net_4g_info.operator),
|
|
MG_ESC("signalIntensity"), MG_ESC(net_4g_info.signal_strength));
|
|
} else if (mg_http_match_uri(hm, "/net/set4gInfo")) {
|
|
struct mg_str json = hm->body;
|
|
update_config_array(json, "$.publicIp", net_4g_info.connect_ip);
|
|
update_config_array(json, "$.publicPort", net_4g_info.connect_port);
|
|
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
|
|
} else if (mg_http_match_uri(hm, "/net/connect4G")) {
|
|
//enable 4G connect function
|
|
PrivEvenTrigger(wb_event, WB_4G_CONNECT);
|
|
} else if (mg_http_match_uri(hm, "/net/disconnect4G")) {
|
|
//disable 4G connect function
|
|
PrivEvenTrigger(wb_event, WB_4G_CONNECT);
|
|
} else if (mg_http_match_uri(hm, "/net/getMQTTInfo")) {
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
|
"{%m:%m, %m:%m, %m:%m, %m:%d, %m:%d}\n",
|
|
MG_ESC("topic"), MG_ESC(net_4g_mqtt_info.topic),
|
|
MG_ESC("username"), MG_ESC(net_4g_mqtt_info.username),
|
|
MG_ESC("password"), MG_ESC(net_4g_mqtt_info.password),
|
|
MG_ESC("client_id"), net_4g_mqtt_info.client_id,
|
|
MG_ESC("status"), net_4g_mqtt_info.connect_status);
|
|
} else if (mg_http_match_uri(hm, "/net/setMQTTInfo")) {
|
|
struct mg_str json = hm->body;
|
|
update_config_array(json, "$.topic", net_4g_mqtt_info.topic);
|
|
update_config_array(json, "$.username", net_4g_mqtt_info.username);
|
|
update_config_array(json, "$.password", net_4g_mqtt_info.password);
|
|
net_4g_mqtt_info.client_id = mg_json_get_long(json, "$.client_id", 0);
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
|
|
} else if (mg_http_match_uri(hm, "/net/connectMQTT")) {
|
|
//enable 4G MQTT connect function
|
|
PrivEvenTrigger(wb_event, WB_MQTT_CONNECT);
|
|
} else if (mg_http_match_uri(hm, "/net/disconnectMQTT")) {
|
|
//disable 4G MQTT connect function
|
|
PrivEvenTrigger(wb_event, WB_MQTT_DISCONNECT);
|
|
}
|
|
/*define net LoRa info*/
|
|
else if (mg_http_match_uri(hm, "/net/getLoraInfo")) {
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
|
"{%m:%d, %m:%d, %m:%d, %m:%d}\n",
|
|
MG_ESC("range"), net_lora_info.frequency,
|
|
MG_ESC("bandwidth"), net_lora_info.bw,
|
|
MG_ESC("factor"), net_lora_info.sf,
|
|
MG_ESC("status"), net_lora_info.connect_status);
|
|
}
|
|
else if (mg_http_match_uri(hm, "/net/setLoraInfo")) {
|
|
struct mg_str json = hm->body;
|
|
printf("json: %s\n", json.ptr);
|
|
net_lora_info.frequency = mg_json_get_long(json, "$.range", 0);
|
|
net_lora_info.sf = mg_json_get_long(json, "$.factor", 0);
|
|
net_lora_info.bw = mg_json_get_long(json, "$.bandwidth", 0);
|
|
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
|
|
|
|
extern void LoraRadioParamsUpdate(uint32_t frequency, uint8_t sf, uint8_t bw);
|
|
LoraRadioParamsUpdate(net_lora_info.frequency, net_lora_info.sf, net_lora_info.bw);
|
|
} else if (mg_http_match_uri(hm, "/net/ConnectLora")) {
|
|
//enable LoRa connect function
|
|
PrivEvenTrigger(wb_event, WB_LORA_CONNECT);
|
|
} else if (mg_http_match_uri(hm, "/net/DisonnectLora")) {
|
|
//disable LoRa connect function
|
|
PrivEvenTrigger(wb_event, WB_LORA_DISCONNECT);
|
|
}
|
|
/*define net Ethernet info*/
|
|
else if (mg_http_match_uri(hm, "/net/getEthernetInfo")) {
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
|
"{%m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%d}\n",
|
|
MG_ESC("ethernetIp"), MG_ESC(net_ethernet_info.ethernetIp),
|
|
MG_ESC("ethernetNetmask"), MG_ESC(net_ethernet_info.ethernetNetmask),
|
|
MG_ESC("ethernetGateway"), MG_ESC(net_ethernet_info.ethernetGateway),
|
|
MG_ESC("ethernetDNS"), MG_ESC(net_ethernet_info.ethernetDNS),
|
|
MG_ESC("targetIp"), MG_ESC(net_ethernet_info.targetIp),
|
|
MG_ESC("targetPort"), MG_ESC(net_ethernet_info.targetPort),
|
|
MG_ESC("targetGateway"), MG_ESC(net_ethernet_info.targetGateway),
|
|
MG_ESC("targetDNS"), MG_ESC(net_ethernet_info.targetDNS),
|
|
MG_ESC("ethernetStatus"), net_ethernet_info.connect_status);
|
|
} else if (mg_http_match_uri(hm, "/net/setEthernetInfo")) {
|
|
struct mg_str json = hm->body;
|
|
printf("json: %s\n", json.ptr);
|
|
update_config_array(json, "$.ethernetIp", net_ethernet_info.ethernetIp);
|
|
update_config_array(json, "$.ethernetNetmask", net_ethernet_info.ethernetNetmask);
|
|
update_config_array(json, "$.ethernetGateway", net_ethernet_info.ethernetGateway);
|
|
update_config_array(json, "$.ethernetDNS", net_ethernet_info.ethernetDNS);
|
|
update_config_array(json, "$.targetIp", net_ethernet_info.targetIp);
|
|
update_config_array(json, "$.targetPort", net_ethernet_info.targetPort);
|
|
update_config_array(json, "$.targetGateway", net_ethernet_info.targetGateway);
|
|
update_config_array(json, "$.targetDNS", net_ethernet_info.targetDNS);
|
|
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
|
|
|
|
p_netdev_ethernet = netdev_get_by_name("hd");
|
|
if (p_netdev_ethernet) {
|
|
ip_addr_t ipaddr, maskaddr, gwaddr;
|
|
inet_aton(net_ethernet_info.ethernetIp, &ipaddr);
|
|
inet_aton(net_ethernet_info.ethernetNetmask, &maskaddr);
|
|
inet_aton(net_ethernet_info.ethernetGateway, &gwaddr);
|
|
p_netdev_ethernet->ops->set_addr_info(p_netdev_ethernet, &ipaddr, &maskaddr, &gwaddr);
|
|
|
|
printf("Ethernet Configuration changed to [IP: %s, Mask: %s, GW: %s]\n",
|
|
net_ethernet_info.ethernetIp, net_ethernet_info.ethernetNetmask,
|
|
net_ethernet_info.ethernetGateway);
|
|
}
|
|
} else if (mg_http_match_uri(hm, "/net/connectEthernet")) {
|
|
//enable Ethernet connect function
|
|
PrivEvenTrigger(wb_event, WB_ETHERNET_CONNECT);
|
|
} else if (mg_http_match_uri(hm, "/net/disconnectEthernet")) {
|
|
//disable Ethernet connect function
|
|
PrivEvenTrigger(wb_event, WB_ETHERNET_DISCONNECT);
|
|
}
|
|
/*define plc info*/
|
|
else if (mg_http_match_uri(hm, "/control/setPLCInfo")) {
|
|
struct mg_str json = hm->body;
|
|
printf("json: %s\n", json.ptr);
|
|
PlcInfoWriteToSd(json.ptr);
|
|
} else {
|
|
struct mg_str unknown = mg_str_n("?", 1), *cl;
|
|
struct mg_http_serve_opts opts = { .root_dir = s_root_dir, .ssi_pattern = s_ssi_pattern };
|
|
mg_http_serve_dir(c, hm, &opts);
|
|
mg_http_parse((char*)c->send.buf, c->send.len, &tmp);
|
|
cl = mg_http_get_header(&tmp, "Content-Length");
|
|
if (cl == NULL)
|
|
cl = &unknown;
|
|
MG_INFO(("%.*s %.*s %.*s %.*s", (int)hm->method.len, hm->method.ptr,
|
|
(int)hm->uri.len, hm->uri.ptr, (int)tmp.uri.len, tmp.uri.ptr,
|
|
(int)cl->len, cl->ptr));
|
|
}
|
|
}
|
|
(void)fn_data;
|
|
}
|
|
|
|
static void* do_webserver(void* args)
|
|
{
|
|
p_netdev_webserver = netdev_get_by_name("wz");
|
|
if (p_netdev_webserver == NULL) {
|
|
MG_INFO(("Did not find wz netdev, use default.\n"));
|
|
p_netdev_webserver = NETDEV_DEFAULT;
|
|
}
|
|
MG_INFO(("Webserver Use Netdev %s", p_netdev_webserver->name));
|
|
webserver_config.ip = strdup(inet_ntoa(*p_netdev_webserver->ip_addr));
|
|
webserver_config.mask = strdup(inet_ntoa(*p_netdev_webserver->netmask));
|
|
webserver_config.gw = strdup(inet_ntoa(*p_netdev_webserver->gw));
|
|
webserver_config.dns = strdup(inet_ntoa(p_netdev_webserver->dns_servers[0]));
|
|
|
|
#ifdef BSP_USING_RS485
|
|
Rs485InitConfigure();
|
|
#endif
|
|
|
|
adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME);
|
|
net_lora_info.lora_init_flag = 0;
|
|
WbEventInit();
|
|
|
|
struct mg_mgr mgr; // Event manager
|
|
// mg_log_set(MG_LL_INFO); // Set to 3 to enable debug
|
|
mg_log_set(MG_LL_ERROR); // Set to 3 to enable debug
|
|
mg_mgr_init(&mgr); // Initialise event manager
|
|
mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
|
|
for (;;)
|
|
mg_mgr_poll(&mgr, 50); // Infinite event loop
|
|
mg_mgr_free(&mgr);
|
|
return NULL;
|
|
}
|
|
|
|
int webserver(void)
|
|
{
|
|
char* params[2] = {"LwipNetworkActive", "-a"};
|
|
extern void LwipNetworkActive(int argc, char* argv[]);
|
|
LwipNetworkActive(2, params);
|
|
|
|
pthread_attr_t attr;
|
|
attr.schedparam.sched_priority = 30;
|
|
attr.stacksize = 0x4000;
|
|
|
|
char task_name[] = "do_webserver";
|
|
pthread_args_t args;
|
|
args.pthread_name = task_name;
|
|
|
|
PrivTaskCreate(&tid, &attr, &do_webserver, (void *)&args);
|
|
PrivTaskStartup(&tid);
|
|
|
|
return 0;
|
|
}
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(5),
|
|
Webserver, webserver, webserver for project); |