Просмотр исходного кода

add: iperf,支持client模式,添加air8101下的demo

然而, 当前固件跑一下client模式必死,稳定报ac1_tx_dma_dead错误
Wendal Chen 11 месяцев назад
Родитель
Сommit
8b1effdd97

+ 52 - 7
components/network/iperf/binding/luat_lib_iperf.c

@@ -14,6 +14,7 @@
 #include "luat_lwiperf.h"
 #include "luat_lwiperf.h"
 #include "luat_network_adapter.h"
 #include "luat_network_adapter.h"
 #include "luat_netdrv.h"
 #include "luat_netdrv.h"
+#include "luat_msgbus.h"
 #include "lwip/ip.h"
 #include "lwip/ip.h"
 
 
 #define LUAT_LOG_TAG "iperf"
 #define LUAT_LOG_TAG "iperf"
@@ -21,8 +22,36 @@
 
 
 static void* iperf_session;
 static void* iperf_session;
 
 
-static int start_gogogo(int adpater_id, int is_server) {
+static int l_iperf_report_handle(lua_State*L, void* ptr) {
+    rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
+    uint32_t bytes_transferred, ms_duration, bandwidth;
+    bytes_transferred = msg->arg1;
+    ms_duration = msg->arg2;
+    bandwidth = (int)ptr;
+    lua_getglobal(L, "sys_pub");
+    lua_pushstring(L, "IPERF_REPORT");
+    lua_pushinteger(L, bytes_transferred);
+    lua_pushinteger(L, ms_duration);
+    lua_pushinteger(L, bandwidth);
+    LLOGD("report bytes %ld ms_duration %ld bandwidth %ld kbps", bytes_transferred, ms_duration, bandwidth);
+    lua_call(L, 4, 0);
+    return 0;
+}
+
+static void iperf_report_cb(void *arg, enum lwiperf_report_type report_type,
+    const ip_addr_t* local_addr, u16_t local_port, const ip_addr_t* remote_addr, u16_t remote_port,
+    u32_t bytes_transferred, u32_t ms_duration, u32_t bandwidth_kbitpsec) {
+    rtos_msg_t msg = {0};
+    msg.arg1 = bytes_transferred;
+    msg.arg2 = ms_duration;
+    msg.ptr = (void*)bandwidth_kbitpsec;
+    msg.handler = l_iperf_report_handle;
+    luat_msgbus_put(&msg, 0);
+}
 
 
+static int start_gogogo(int adpater_id, int is_server, const ip_addr_t* remote_ip) {
+    char buff[64] = {0};
+    char buff2[64] = {0};
     if (adpater_id < 0 || adpater_id >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) {
     if (adpater_id < 0 || adpater_id >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) {
         // 必须明确指定合法的索引号
         // 必须明确指定合法的索引号
         LLOGE("非法的网络适配器索引号 %d", adpater_id);
         LLOGE("非法的网络适配器索引号 %d", adpater_id);
@@ -38,15 +67,15 @@ static int start_gogogo(int adpater_id, int is_server) {
         LLOGE("该网络还没就绪, 无法启动");
         LLOGE("该网络还没就绪, 无法启动");
         return 0;
         return 0;
     }
     }
+    ipaddr_ntoa_r(&drv->netif->ip_addr, buff, sizeof(buff));
     if (is_server) {
     if (is_server) {
-        char buff[64] = {0};
-        ipaddr_ntoa_r(&drv->netif->ip_addr, buff, sizeof(buff));
-        iperf_session = luat_lwiperf_start_tcp_server(&drv->netif->ip_addr, 5001, NULL, NULL);
+        iperf_session = luat_lwiperf_start_tcp_server(&drv->netif->ip_addr, 5001, iperf_report_cb, NULL);
         LLOGD("iperf listen %s:5001", buff);
         LLOGD("iperf listen %s:5001", buff);
     }
     }
     else {
     else {
-        //iperf_session = luat_lwiperf_start_tcp_server(&drv->netif->ip_addr, 5000, NULL, NULL);
-        return 0;
+        ipaddr_ntoa_r(remote_ip, buff2, sizeof(buff2));
+        luat_lwiperf_start_tcp_client(remote_ip, 5001, LWIPERF_CLIENT, iperf_report_cb, NULL, &drv->netif->ip_addr);
+        LLOGD("iperf connect %s --> %s:5001", buff, buff2);
     }
     }
     return iperf_session != NULL;
     return iperf_session != NULL;
 }
 }
@@ -69,7 +98,7 @@ static int l_iperf_server(lua_State *L) {
         return 0;
         return 0;
     }
     }
     int adpater_id = luaL_checkinteger(L, 1);
     int adpater_id = luaL_checkinteger(L, 1);
-    if (start_gogogo(adpater_id, 1)) {
+    if (start_gogogo(adpater_id, 1, NULL)) {
         lua_pushboolean(L, 1);
         lua_pushboolean(L, 1);
         return 1;
         return 1;
     }
     }
@@ -77,6 +106,21 @@ static int l_iperf_server(lua_State *L) {
 }
 }
 
 
 static int l_iperf_client(lua_State *L) {
 static int l_iperf_client(lua_State *L) {
+    ip_addr_t remote_ip = {0};
+    if (iperf_session != NULL) {
+        LLOGE("已经启动了server或者client,要先关掉才能启动新的");
+        return 0;
+    }
+    int adpater_id = luaL_checkinteger(L, 1);
+    const char* ip = luaL_checkstring(L, 2);
+    if (ipaddr_aton(ip, &remote_ip) == 0) {
+        LLOGE("非法的ip地址 %s", ip);
+        return 0;
+    }
+    if (start_gogogo(adpater_id, 0, &remote_ip)) {
+        lua_pushboolean(L, 1);
+        return 1;
+    }
     return 0;
     return 0;
 }
 }
 
 
@@ -92,6 +136,7 @@ static int l_iperf_abort(lua_State *L) {
         return 0;
         return 0;
     }
     }
     luat_lwiperf_abort(iperf_session);
     luat_lwiperf_abort(iperf_session);
+    iperf_session = NULL;
     lua_pushboolean(L, 1);
     lua_pushboolean(L, 1);
     return 1;
     return 1;
 }
 }

+ 2 - 1
components/network/iperf/include/luat_lwiperf.h

@@ -86,7 +86,8 @@ void* luat_lwiperf_start_tcp_server(const ip_addr_t* local_addr, u16_t local_por
 void* luat_lwiperf_start_tcp_server_default(lwiperf_report_fn report_fn, void* report_arg);
 void* luat_lwiperf_start_tcp_server_default(lwiperf_report_fn report_fn, void* report_arg);
 void* luat_lwiperf_start_tcp_client(const ip_addr_t* remote_addr, u16_t remote_port,
 void* luat_lwiperf_start_tcp_client(const ip_addr_t* remote_addr, u16_t remote_port,
                                enum lwiperf_client_type type,
                                enum lwiperf_client_type type,
-                               lwiperf_report_fn report_fn, void* report_arg);
+                               lwiperf_report_fn report_fn, void* report_arg,
+                               const ip_addr_t* local_addr);
 void* luat_lwiperf_start_tcp_client_default(const ip_addr_t* remote_addr,
 void* luat_lwiperf_start_tcp_client_default(const ip_addr_t* remote_addr,
                                lwiperf_report_fn report_fn, void* report_arg);
                                lwiperf_report_fn report_fn, void* report_arg);
 
 

+ 14 - 5
components/network/iperf/src/luat_lwiperf.c

@@ -396,7 +396,8 @@
   */
   */
  static err_t
  static err_t
  lwiperf_tx_start_impl(const ip_addr_t *remote_ip, u16_t remote_port, lwiperf_settings_t *settings, lwiperf_report_fn report_fn,
  lwiperf_tx_start_impl(const ip_addr_t *remote_ip, u16_t remote_port, lwiperf_settings_t *settings, lwiperf_report_fn report_fn,
-                       void *report_arg, lwiperf_state_base_t *related_master_state, lwiperf_state_tcp_t **new_conn)
+                       void *report_arg, lwiperf_state_base_t *related_master_state, lwiperf_state_tcp_t **new_conn,
+                      const ip_addr_t *local_ip)
  {
  {
    err_t err;
    err_t err;
    lwiperf_state_tcp_t *client_conn;
    lwiperf_state_tcp_t *client_conn;
@@ -435,6 +436,14 @@
    tcp_err(newpcb, lwiperf_tcp_err);
    tcp_err(newpcb, lwiperf_tcp_err);
  
  
    ip_addr_copy(remote_addr, *remote_ip);
    ip_addr_copy(remote_addr, *remote_ip);
+
+   if (local_ip != NULL) {
+    err = tcp_bind(newpcb, local_ip, 0);
+    if (err != ERR_OK) {
+      return err;
+    }
+   }
+   
  
  
    err = tcp_connect(newpcb, &remote_addr, remote_port, lwiperf_tcp_client_connected);
    err = tcp_connect(newpcb, &remote_addr, remote_port, lwiperf_tcp_client_connected);
    if (err != ERR_OK) {
    if (err != ERR_OK) {
@@ -454,7 +463,7 @@
    u16_t remote_port = (u16_t)lwip_htonl(conn->settings.remote_port);
    u16_t remote_port = (u16_t)lwip_htonl(conn->settings.remote_port);
  
  
    ret = lwiperf_tx_start_impl(&conn->conn_pcb->remote_ip, remote_port, &conn->settings, conn->report_fn, conn->report_arg,
    ret = lwiperf_tx_start_impl(&conn->conn_pcb->remote_ip, remote_port, &conn->settings, conn->report_fn, conn->report_arg,
-     conn->base.related_master_state, &new_conn);
+     conn->base.related_master_state, &new_conn, NULL);
    if (ret == ERR_OK) {
    if (ret == ERR_OK) {
      LWIP_ASSERT("new_conn != NULL", new_conn != NULL);
      LWIP_ASSERT("new_conn != NULL", new_conn != NULL);
      new_conn->settings.flags = 0; /* prevent the remote side starting back as client again */
      new_conn->settings.flags = 0; /* prevent the remote side starting back as client again */
@@ -756,7 +765,7 @@
                                 lwiperf_report_fn report_fn, void* report_arg)
                                 lwiperf_report_fn report_fn, void* report_arg)
  {
  {
    return  luat_lwiperf_start_tcp_client(remote_addr, LWIPERF_TCP_PORT_DEFAULT, LWIPERF_CLIENT,
    return  luat_lwiperf_start_tcp_client(remote_addr, LWIPERF_TCP_PORT_DEFAULT, LWIPERF_CLIENT,
-                                   report_fn, report_arg);
+                                   report_fn, report_arg, NULL);
  }
  }
  
  
  /**
  /**
@@ -767,7 +776,7 @@
   *          by calling @ref lwiperf_abort()
   *          by calling @ref lwiperf_abort()
   */
   */
  void*  luat_lwiperf_start_tcp_client(const ip_addr_t* remote_addr, u16_t remote_port,
  void*  luat_lwiperf_start_tcp_client(const ip_addr_t* remote_addr, u16_t remote_port,
-   enum lwiperf_client_type type, lwiperf_report_fn report_fn, void* report_arg)
+   enum lwiperf_client_type type, lwiperf_report_fn report_fn, void* report_arg, const ip_addr_t* local_addr)
  {
  {
    err_t ret;
    err_t ret;
    lwiperf_settings_t settings;
    lwiperf_settings_t settings;
@@ -796,7 +805,7 @@
    /* TODO: implement passing duration/amount of bytes to transfer */
    /* TODO: implement passing duration/amount of bytes to transfer */
    settings.amount = htonl((u32_t)-1000);
    settings.amount = htonl((u32_t)-1000);
  
  
-   ret = lwiperf_tx_start_impl(remote_addr, remote_port, &settings, report_fn, report_arg, NULL, &state);
+   ret = lwiperf_tx_start_impl(remote_addr, remote_port, &settings, report_fn, report_arg, NULL, &state, local_addr);
    if (ret == ERR_OK) {
    if (ret == ERR_OK) {
      LWIP_ASSERT("state != NULL", state != NULL);
      LWIP_ASSERT("state != NULL", state != NULL);
      if (type != LWIPERF_CLIENT) {
      if (type != LWIPERF_CLIENT) {

+ 50 - 0
demo/iperf/air8101/main.lua

@@ -0,0 +1,50 @@
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "wifidemo"
+VERSION = "1.0.0"
+
+-- 引入必要的库文件(lua编写), 内部库不需要require
+sys = require("sys")
+require("sysplus")
+
+--[[
+本demo演示AP的配网实例
+1. 启动后, 会创建一个 luatos_ + mac地址的热点
+2. 热点密码是 12341234
+3. 热点网关是 192.168.4.1, 同时也是配网网页的ip
+4. http://192.168.4.1
+]]
+
+
+if wdt then
+    --添加硬狗防止程序卡死,在支持的设备上启用这个功能
+    wdt.init(9000)--初始化watchdog设置为9s
+    sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
+end
+
+-- 初始化LED灯, 开发板上左右2个led分别是gpio12/gpio13
+local LEDA= gpio.setup(12, 0, gpio.PULLUP)
+-- local LEDB= gpio.setup(13, 0, gpio.PULLUP)
+
+local scan_result = {}
+
+sys.taskInit(function()
+    wlan.init()
+    sys.wait(100)
+    wlan.connect("luatos1234", "12341234")
+
+    sys.waitUntil("IP_READY")
+    sys.wait(100)
+    iperf.client(socket.LWIP_STA, "47.94.236.172")
+
+    sys.wait(30*1000)
+    iperf.abort()
+end)
+
+sys.subscribe("IPERF_REPORT", function(bytes, ms_duration, bandwidth)
+    log.info("iperf", bytes, ms_duration, bandwidth)
+end)
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!