|
|
@@ -5,6 +5,9 @@
|
|
|
#include "luat_mem.h"
|
|
|
#include "luat_rtos.h"
|
|
|
#include "luat_crypto.h"
|
|
|
+#include "luat_netdrv.h"
|
|
|
+#include "luat_netdrv_whale.h"
|
|
|
+#include "lwip/prot/ethernet.h"
|
|
|
|
|
|
#define LUAT_LOG_TAG "airlink"
|
|
|
#include "luat_log.h"
|
|
|
@@ -12,33 +15,68 @@
|
|
|
luat_rtos_queue_t airlink_cmd_queue;
|
|
|
luat_rtos_queue_t airlink_ippkg_queue;
|
|
|
|
|
|
-int luat_airlink_start(int id) {
|
|
|
- if (airlink_cmd_queue == NULL) {
|
|
|
+extern int luat_airlink_start_slave(void);
|
|
|
+extern int luat_airlink_start_master(void);
|
|
|
+
|
|
|
+int luat_airlink_init(void)
|
|
|
+{
|
|
|
+ luat_netdrv_whale_t cfg = {0};
|
|
|
+ luat_netdrv_t *drv = NULL;
|
|
|
+ // 注册2个网络设备, STA和AP
|
|
|
+ cfg.id = NW_ADAPTER_INDEX_LWIP_WIFI_STA;
|
|
|
+ cfg.flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
|
|
|
+ cfg.mtu = 1460;
|
|
|
+ drv = luat_netdrv_whale_create(&cfg);
|
|
|
+ if (drv != NULL)
|
|
|
+ {
|
|
|
+ // LLOGD("注册STA网络设备");
|
|
|
+ luat_netdrv_register(cfg.id, drv);
|
|
|
+ }
|
|
|
+
|
|
|
+ // AP设备
|
|
|
+ cfg.id = NW_ADAPTER_INDEX_LWIP_WIFI_AP;
|
|
|
+ drv = luat_netdrv_whale_create(&cfg);
|
|
|
+ if (drv != NULL)
|
|
|
+ {
|
|
|
+ // LLOGD("注册AP网络设备");
|
|
|
+ luat_netdrv_register(cfg.id, drv);
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int luat_airlink_start(int id)
|
|
|
+{
|
|
|
+ if (airlink_cmd_queue == NULL)
|
|
|
+ {
|
|
|
luat_rtos_queue_create(&airlink_cmd_queue, 2048, sizeof(airlink_queue_item_t));
|
|
|
}
|
|
|
- if (airlink_ippkg_queue == NULL) {
|
|
|
+ if (airlink_ippkg_queue == NULL)
|
|
|
+ {
|
|
|
luat_rtos_queue_create(&airlink_ippkg_queue, 2048, sizeof(airlink_queue_item_t));
|
|
|
}
|
|
|
- if (id == 0) {
|
|
|
- extern int luat_airlink_start_slave(void);
|
|
|
+ if (id == 0)
|
|
|
+ {
|
|
|
luat_airlink_start_slave();
|
|
|
}
|
|
|
- else {
|
|
|
- extern int luat_airlink_start_master(void);
|
|
|
+ else
|
|
|
+ {
|
|
|
luat_airlink_start_master();
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int luat_airlink_stop(int id) {
|
|
|
+int luat_airlink_stop(int id)
|
|
|
+{
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-void luat_airlink_data_unpack(uint8_t* buff, size_t len, size_t* pkg_offset, size_t* pkg_size) {
|
|
|
+void luat_airlink_data_unpack(uint8_t *buff, size_t len, size_t *pkg_offset, size_t *pkg_size)
|
|
|
+{
|
|
|
size_t tlen = 0;
|
|
|
uint16_t crc16 = 0;
|
|
|
uint16_t crc16_data = 0;
|
|
|
- if (len < 12) {
|
|
|
+ if (len < 12)
|
|
|
+ {
|
|
|
*pkg_offset = 0;
|
|
|
*pkg_size = 0;
|
|
|
return;
|
|
|
@@ -49,31 +87,35 @@ void luat_airlink_data_unpack(uint8_t* buff, size_t len, size_t* pkg_offset, siz
|
|
|
if (buff[i] == 0xA1 && buff[i + 1] == 0xB1 && buff[i + 2] == 0xCA && buff[i + 3] == 0x66)
|
|
|
{
|
|
|
// 找到了magic
|
|
|
- tlen = buff[i + 4] + buff[i+5] * 256;
|
|
|
- crc16 = buff[i + 6] + buff[i+7] * 256;
|
|
|
- LLOGD("找到magic, 且数据长度为 %d", tlen);
|
|
|
- if (tlen > 0 && tlen + 4 + i + 4 <= len) {
|
|
|
+ tlen = buff[i + 4] + buff[i + 5] * 256;
|
|
|
+ crc16 = buff[i + 6] + buff[i + 7] * 256;
|
|
|
+ // LLOGD("找到magic, 且数据长度为 %d", tlen);
|
|
|
+ if (tlen > 0 && tlen + 4 + i + 4 <= len)
|
|
|
+ {
|
|
|
// 计算crc16
|
|
|
crc16_data = luat_crc16(buff + i + 4 + 4, tlen, 0xFFFF, 0x1021, 0);
|
|
|
- if (crc16_data == crc16) {
|
|
|
- LLOGD("crc16校验成功");
|
|
|
+ if (crc16_data == crc16)
|
|
|
+ {
|
|
|
+ // LLOGD("crc16校验成功");
|
|
|
*pkg_offset = i + 4 + 4;
|
|
|
*pkg_size = tlen;
|
|
|
return;
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
LLOGD("crc16校验失败 %d %d", crc16_data, crc16);
|
|
|
}
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
LLOGD("数据长度错误");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-void luat_airlink_data_pack(uint8_t* buff, size_t len, uint8_t* dst) {
|
|
|
+void luat_airlink_data_pack(uint8_t *buff, size_t len, uint8_t *dst)
|
|
|
+{
|
|
|
// 先写入magic
|
|
|
dst[0] = 0xA1;
|
|
|
dst[1] = 0xB1;
|
|
|
@@ -92,28 +134,34 @@ void luat_airlink_data_pack(uint8_t* buff, size_t len, uint8_t* dst) {
|
|
|
memcpy(dst + 8, buff, len);
|
|
|
}
|
|
|
|
|
|
-void luat_airlink_print_buff(const char* tag, uint8_t* buff, size_t len) {
|
|
|
+void luat_airlink_print_buff(const char *tag, uint8_t *buff, size_t len)
|
|
|
+{
|
|
|
static char tmpbuff[1024] = {0};
|
|
|
- for (size_t i = 0; i < len; i+=8)
|
|
|
+ for (size_t i = 0; i < len; i += 8)
|
|
|
{
|
|
|
// sprintf(tmpbuff + i * 2, "%02X", buff[i]);
|
|
|
// LLOGD("SPI TX[%d] 0x%02X", i, buff[i]);
|
|
|
- LLOGD("%s [%04X-%04X] %02X%02X%02X%02X%02X%02X%02X%02X", tag, i, i + 8,
|
|
|
- buff[i+0], buff[i+1], buff[i+2], buff[i+3],
|
|
|
- buff[i+4], buff[i+5], buff[i+6], buff[i+7]);
|
|
|
+ LLOGD("%s [%04X-%04X] %02X%02X%02X%02X%02X%02X%02X%02X", tag, i, i + 8,
|
|
|
+ buff[i + 0], buff[i + 1], buff[i + 2], buff[i + 3],
|
|
|
+ buff[i + 4], buff[i + 5], buff[i + 6], buff[i + 7]);
|
|
|
}
|
|
|
// LLOGD("SPI0 %s", tmpbuff);
|
|
|
}
|
|
|
|
|
|
-int luat_airlink_queue_send(int tp, airlink_queue_item_t* item) {
|
|
|
- if (tp == LUAT_AIRLINK_QUEUE_CMD) {
|
|
|
- if (airlink_cmd_queue == NULL) {
|
|
|
+int luat_airlink_queue_send(int tp, airlink_queue_item_t *item)
|
|
|
+{
|
|
|
+ if (tp == LUAT_AIRLINK_QUEUE_CMD)
|
|
|
+ {
|
|
|
+ if (airlink_cmd_queue == NULL)
|
|
|
+ {
|
|
|
return -1;
|
|
|
}
|
|
|
return luat_rtos_queue_send(airlink_cmd_queue, item, 0, 0);
|
|
|
}
|
|
|
- if (tp == LUAT_AIRLINK_QUEUE_IPPKG) {
|
|
|
- if (airlink_ippkg_queue == NULL) {
|
|
|
+ if (tp == LUAT_AIRLINK_QUEUE_IPPKG)
|
|
|
+ {
|
|
|
+ if (airlink_ippkg_queue == NULL)
|
|
|
+ {
|
|
|
return -1;
|
|
|
}
|
|
|
return luat_rtos_queue_send(airlink_ippkg_queue, item, 0, 0);
|
|
|
@@ -121,37 +169,48 @@ int luat_airlink_queue_send(int tp, airlink_queue_item_t* item) {
|
|
|
return -2;
|
|
|
}
|
|
|
|
|
|
-int luat_airlink_queue_get_cnt(int tp) {
|
|
|
+int luat_airlink_queue_get_cnt(int tp)
|
|
|
+{
|
|
|
size_t len = 0;
|
|
|
int ret = -2;
|
|
|
- if (tp == LUAT_AIRLINK_QUEUE_CMD) {
|
|
|
- if (airlink_cmd_queue == NULL) {
|
|
|
+ if (tp == LUAT_AIRLINK_QUEUE_CMD)
|
|
|
+ {
|
|
|
+ if (airlink_cmd_queue == NULL)
|
|
|
+ {
|
|
|
return -1;
|
|
|
}
|
|
|
ret = luat_rtos_queue_get_cnt(airlink_cmd_queue, &len);
|
|
|
}
|
|
|
- if (tp == LUAT_AIRLINK_QUEUE_IPPKG) {
|
|
|
- if (airlink_ippkg_queue == NULL) {
|
|
|
+ if (tp == LUAT_AIRLINK_QUEUE_IPPKG)
|
|
|
+ {
|
|
|
+ if (airlink_ippkg_queue == NULL)
|
|
|
+ {
|
|
|
return -1;
|
|
|
}
|
|
|
ret = luat_rtos_queue_get_cnt(airlink_ippkg_queue, &len);
|
|
|
}
|
|
|
- if (ret) {
|
|
|
+ if (ret)
|
|
|
+ {
|
|
|
return ret;
|
|
|
}
|
|
|
return len;
|
|
|
}
|
|
|
|
|
|
-int luat_airlink_cmd_recv(int tp, airlink_queue_item_t* item, size_t timeout) {
|
|
|
+int luat_airlink_cmd_recv(int tp, airlink_queue_item_t *item, size_t timeout)
|
|
|
+{
|
|
|
int ret = -2;
|
|
|
- if (tp == LUAT_AIRLINK_QUEUE_CMD) {
|
|
|
- if (airlink_cmd_queue == NULL) {
|
|
|
+ if (tp == LUAT_AIRLINK_QUEUE_CMD)
|
|
|
+ {
|
|
|
+ if (airlink_cmd_queue == NULL)
|
|
|
+ {
|
|
|
return -1;
|
|
|
}
|
|
|
ret = luat_rtos_queue_recv(airlink_cmd_queue, item, 0, timeout);
|
|
|
}
|
|
|
- if (tp == LUAT_AIRLINK_QUEUE_IPPKG) {
|
|
|
- if (airlink_ippkg_queue == NULL) {
|
|
|
+ if (tp == LUAT_AIRLINK_QUEUE_IPPKG)
|
|
|
+ {
|
|
|
+ if (airlink_ippkg_queue == NULL)
|
|
|
+ {
|
|
|
return -1;
|
|
|
}
|
|
|
ret = luat_rtos_queue_recv(airlink_ippkg_queue, item, 0, timeout);
|
|
|
@@ -159,16 +218,35 @@ int luat_airlink_cmd_recv(int tp, airlink_queue_item_t* item, size_t timeout) {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-int luat_airlink_queue_send_ippkg(uint8_t adapter_id, uint8_t* data, size_t len) {
|
|
|
- if (len < 8) {
|
|
|
- // LLOGE("数据包太小了, 抛弃掉");
|
|
|
+int luat_airlink_queue_send_ippkg(uint8_t adapter_id, uint8_t *data, size_t len)
|
|
|
+{
|
|
|
+ if (len < 8)
|
|
|
+ {
|
|
|
+ LLOGE("数据包太小了, 抛弃掉");
|
|
|
return -1;
|
|
|
}
|
|
|
+ luat_netdrv_t* netdrv = luat_netdrv_get(adapter_id);
|
|
|
+ if (netdrv == NULL || netdrv->netif == NULL) {
|
|
|
+ LLOGW("应该是BUG了, netdrv为空或者没有netif %d", adapter_id);
|
|
|
+ return -2;
|
|
|
+ }
|
|
|
+ struct eth_hdr* eth = (struct eth_hdr*)data;
|
|
|
+ if (netdrv->netif->flags & NETIF_FLAG_ETHARP) {
|
|
|
+ if (eth->type == PP_HTONS(ETHTYPE_IP) || eth->type == PP_HTONS(ETHTYPE_ARP)) {
|
|
|
+ // LLOGD("是ARP/IP包,继续转发");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // LLOGD("不是ARP/IP包,丢弃掉");
|
|
|
+ return -3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
airlink_queue_item_t item = {
|
|
|
.len = len + 5,
|
|
|
.cmd = luat_heap_malloc(len + 8),
|
|
|
};
|
|
|
- if (item.cmd == NULL) {
|
|
|
+ if (item.cmd == NULL)
|
|
|
+ {
|
|
|
return -2;
|
|
|
}
|
|
|
memcpy(item.cmd->data + 1, data, len);
|
|
|
@@ -179,22 +257,64 @@ int luat_airlink_queue_send_ippkg(uint8_t adapter_id, uint8_t* data, size_t len)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int luat_airlink_cmd_recv_simple(airlink_queue_item_t* cmd) {
|
|
|
+int luat_airlink_cmd_recv_simple(airlink_queue_item_t *cmd)
|
|
|
+{
|
|
|
// 看待发送队列里有没有数据, 有就发送
|
|
|
int ret = luat_airlink_queue_get_cnt(LUAT_AIRLINK_QUEUE_CMD);
|
|
|
// LLOGD("待发送CMD队列长度 %d", ret);
|
|
|
airlink_queue_item_t item = {0};
|
|
|
- if (ret > 0) {
|
|
|
+ if (ret > 0)
|
|
|
+ {
|
|
|
ret = luat_airlink_cmd_recv(LUAT_AIRLINK_QUEUE_CMD, &item, 0);
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
ret = luat_airlink_queue_get_cnt(LUAT_AIRLINK_QUEUE_IPPKG);
|
|
|
// LLOGD("待发送IPPKG队列长度 %d", ret);
|
|
|
- if (ret > 0) {
|
|
|
+ if (ret > 0)
|
|
|
+ {
|
|
|
ret = luat_airlink_cmd_recv(LUAT_AIRLINK_QUEUE_IPPKG, &item, 0);
|
|
|
- LLOGD("获取到IP数据包 %d %p", ret, item.cmd);
|
|
|
+ // LLOGD("从队列获取到IP数据包 %d %p", item.len, item.cmd);
|
|
|
+ // luat_airlink_hexdump("从队列获取到IP数据包", item.cmd->data + 1, item.len - 1);
|
|
|
}
|
|
|
}
|
|
|
memcpy(cmd, &item, sizeof(airlink_queue_item_t));
|
|
|
return 0;
|
|
|
}
|
|
|
+
|
|
|
+void luat_airlink_print_mac_pkg(uint8_t* buff, uint16_t len) {
|
|
|
+ if (len < 24 || len > 1600) {
|
|
|
+ LLOGW("非法的pkg长度 %d", len);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LLOGD("pkg len %d 前24个字节 " MACFMT MACFMT MACFMT MACFMT, len, MAC_ARG(buff), MAC_ARG(buff + 6), MAC_ARG(buff+12), MAC_ARG(buff + 18));
|
|
|
+
|
|
|
+ struct eth_hdr* eth = (struct eth_hdr*)buff;
|
|
|
+ struct ip_hdr* iphdr = (struct ip_hdr*)(buff + SIZEOF_ETH_HDR);
|
|
|
+ struct etharp_hdr* arp = (struct etharp_hdr*)(buff + SIZEOF_ETH_HDR);
|
|
|
+ // LLOGD("eth " MACFMT " -> " MACFMT " tp %02X", MAC_ARG(eth->src.addr), MAC_ARG(eth->dest.addr), (u16_t)lwip_htons(eth->type));
|
|
|
+ switch (eth->type) {
|
|
|
+ case PP_HTONS(ETHTYPE_IP):
|
|
|
+ // LLOGD(" ipv%d %d len %d", (u16_t)IPH_V(iphdr), (u16_t)IPH_PROTO(iphdr),(u16_t)IPH_LEN(iphdr));
|
|
|
+ break;
|
|
|
+ case PP_HTONS(ETHTYPE_ARP):
|
|
|
+ // LLOGD(" arp proto %d", arp->proto);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void luat_airlink_hexdump(const char* tag, uint8_t* buff, uint16_t len) {
|
|
|
+ if (len > 500) {
|
|
|
+ len = 500;
|
|
|
+ }
|
|
|
+ uint8_t* tmp = luat_heap_opt_zalloc(LUAT_HEAP_PSRAM, len * 2 + 1);
|
|
|
+ if (tmp == NULL) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (size_t i = 0; i < len; i++)
|
|
|
+ {
|
|
|
+ sprintf((char*)(tmp + i * 2), "%02X", buff[i]);
|
|
|
+ }
|
|
|
+ LLOGD("%s %s", tag, tmp);
|
|
|
+ luat_heap_opt_free(LUAT_HEAP_PSRAM, tmp);
|
|
|
+}
|