Wendal Chen 1 рік тому
батько
коміт
8524766cab

+ 46 - 0
components/network/netdrv/binding/luat_lib_netdrv.c

@@ -0,0 +1,46 @@
+
+/*
+@module  netdrv
+@summary 网络设备管理
+@catalog 外设API
+@version 1.0
+@date    2025.01.07
+@demo netdrv
+@tag LUAT_USE_NETDRV
+*/
+#include "luat_base.h"
+#include "luat_gpio.h"
+#include "luat_mem.h"
+#include "luat_mcu.h"
+#include "luat_msgbus.h"
+#include "luat_timer.h"
+#include "luat_rtos.h"
+#include "luat_netdrv.h"
+
+#define LUAT_LOG_TAG "netdrv"
+#include "luat_log.h"
+
+/*
+初始化指定netdrv设备
+*/
+static int l_netdrv_setup(lua_State *L) {
+    luat_netdrv_conf_t conf = {0};
+    conf.id = luaL_checkinteger(L, 1);
+    conf.impl = luaL_optinteger(L, 2, 0);
+    conf.tp = luaL_optinteger(L, 3, 0);
+    int ret = luat_netdrv_setup(&conf);
+    lua_pushboolean(L, ret == 0 ? 1 : 0);
+    return 1;
+}
+
+#include "rotable2.h"
+static const rotable_Reg_t reg_netdrv[] =
+{
+    { "setup" ,         ROREG_FUNC(l_netdrv_setup )},
+	{ NULL,             ROREG_INT(0) }
+};
+
+LUAMOD_API int luaopen_netdrv( lua_State *L ) {
+    luat_newlib2(L, reg_netdrv);
+    return 1;
+}

+ 46 - 0
components/network/netdrv/include/luat_netdrv.h

@@ -0,0 +1,46 @@
+#ifndef LUAT_NETDRV_H
+#define LUAT_NETDRV_H
+
+#include "lwip/pbuf.h"
+
+typedef void (*luat_netdrv_dataout_cb)(void* userdata, struct pbuf* pb, int flags);
+typedef int (*luat_netdrv_bootup_cb)(void* userdata);
+typedef int (*luat_netdrv_ready_cb)(void* userdata);
+
+typedef struct luat_netdrv {
+    int32_t id;
+    struct netif* netif;
+    luat_netdrv_dataout_cb dataout;
+    luat_netdrv_bootup_cb boot;
+    luat_netdrv_ready_cb ready;
+    void* userdata;
+}luat_netdrv_t;
+
+enum {
+    LUAT_NETDRV_TP_NATIVE,
+    LUAT_NETDRV_TP_CH390H,
+    LUAT_NETDRV_TP_W5100,
+    LUAT_NETDRV_TP_W5500,
+    LUAT_NETDRV_TP_SPINET,
+    LUAT_NETDRV_TP_UARTNET,
+    LUAT_NETDRV_TP_USB
+};
+
+typedef struct luat_netdrv_conf
+{
+    int32_t id;
+    int32_t impl;
+    int32_t tp;
+    int32_t flags;
+}luat_netdrv_conf_t;
+
+
+luat_netdrv_t* luat_netdrv_setup(luat_netdrv_conf_t *conf);
+
+int luat_netdrv_dhcp(int32_t id, int32_t enable);
+
+int luat_netdrv_ready(int32_t id);
+
+int luat_netdrv_register(int32_t id, luat_netdrv_t* drv);
+
+#endif

+ 37 - 0
components/network/netdrv/src/luat_netdrv.c

@@ -0,0 +1,37 @@
+#include "luat_base.h"
+#include "luat_netdrv.h"
+#include "luat_network_adapter.h"
+
+static luat_netdrv_t* drvs[NW_ADAPTER_QTY];
+
+luat_netdrv_t* luat_netdrv_setup(luat_netdrv_conf_t *conf) {
+    if (conf->id < 0 || conf->id >= NW_ADAPTER_QTY) {
+        return NULL;
+    }
+    if (drvs[conf->id] == NULL) {
+        // 注册新的设备?
+    }
+    else {
+        drvs[conf->id]->boot(NULL);
+    }
+    return NULL;
+}
+
+int luat_netdrv_dhcp(int32_t id, int32_t enable) {
+    return -1;
+}
+
+int luat_netdrv_ready(int32_t id) {
+    if (drvs[id] == NULL) {
+        return -1;
+    }
+    return drvs[id]->ready(drvs[id]->userdata);
+}
+
+int luat_netdrv_register(int32_t id, luat_netdrv_t* drv) {
+    if (drvs[id] != NULL) {
+        return -1;
+    }
+    drvs[id] = drv;
+    return 0;
+}