jonas 3 yıl önce
ebeveyn
işleme
3a70e941a6

+ 3 - 0
README.md

@@ -35,6 +35,7 @@ The AT device software package is composed of the transplantation files and samp
 | class/n58 | N58 device's migration directory for AT components, realizing AT Socket function |
 | class/m5311 | M5311 device is aimed at AT component transplantation catalog, realizes AT Socket function |
 | class/l610 | A migration directory for AT components of L610 equipment, realizing AT Socket function |
+| class/ml305 | A migration directory for AT components of ML305 equipment, realizing AT Socket function |
 ### 1.2 License ###
 
 See the `LICENSE` file for details.
@@ -122,6 +123,7 @@ RT-Thread online packages --->
         [] Neoway N21 --->
         [] Neoway N58 --->
         [] ChinaMobile M5311 --->
+        [] ChinaMobile ML305 --->
         Version (latest) --->
 ```
 
@@ -156,6 +158,7 @@ RT-Thread online packages --->
 - **Neoway N21**: enable N21 (NB-IoT module) device support;
 - **Neoway N58**: enable N58 (4G module) device support;
 - **ChinaMobile M5311**: enable M5311 (NB-IoT module) device support;
+- **ChinaMobile ML305**: enable ML305 (4G module) device support;
 - **Version**: download the software package version;
 
 The above configuration options take 2G module and WIFI module options as examples to introduce the configuration method of the AT device software package of the `V2.X.X` version. The following points are worth noting:

+ 3 - 0
README_ZH.md

@@ -35,6 +35,7 @@ AT device 软件包是由 RT-Thread AT 组件针对不同 AT 设备的移植文
 | class/n58 | N58 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
 | class/m5311 | M5311 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
 | class/l610 | L610 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
+| class/ml305 | ML305 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
 ### 1.2 许可证 ###
 
 详见 `LICENSE` 文件。
@@ -120,6 +121,7 @@ RT-Thread online packages  --->
         [ ]   Neoway N21  --->
         [ ]   Neoway N58  --->
         [ ]   ChinaMobile M5311  --->
+        [ ]   ChinaMobile ML305  --->
         Version (latest)  --->
 ```
 
@@ -154,6 +156,7 @@ RT-Thread online packages  --->
 - **Neoway N21**:开启 N21(NB-IoT 模块)设备支持;
 - **Neoway N58**:开启 N58(4G 模块)设备支持;
 - **ChinaMobile M5311**:开启 M5311(NB-IoT 模块)设备支持;
+- **ChinaMobile ML305**:开启 ML305(4G 模块)设备支持;
 - **Version** 下载软件包版本;
 
 上面配置选项以 2G 模块和 WIFI 模块选项为例,介绍了`V2.X.X` 版本 AT device 软件包配置方式,如下几点值得注意:

+ 9 - 0
SConscript

@@ -193,6 +193,15 @@ if GetDepend(['AT_DEVICE_USING_L610']):
     if GetDepend(['AT_DEVICE_L610_SAMPLE']):
         src +=Glob('samples/at_sample_l610.c')
 
+# ML305
+if GetDepend(['AT_DEVICE_USING_ML305']):
+    path += [cwd + '/class/ml305']
+    src += Glob('class/ml305/at_device_ml305.c')
+    if GetDepend(['AT_USING_SOCKET']):
+        src +=Glob('class/ml305/at_socket_ml305.c')
+    if GetDepend(['AT_DEVICE_ML305_SAMPLE']):
+        src +=Glob('samples/at_sample_ml305.c')
+
 group = DefineGroup('at_device', src, depend = ['PKG_USING_AT_DEVICE'], CPPPATH = path)
 
 Return('group')

+ 990 - 0
class/ml305/at_device_ml305.c

@@ -0,0 +1,990 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-12-16     Jonas        first version
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+
+#include <at_device_ml305.h>
+
+//#define AT_DEBUG
+#define LOG_TAG                    "at.dev.ml305"
+#include <at_log.h>
+
+#ifdef AT_DEVICE_USING_ML305
+#define ML305_AT_DEFAULT_TIMEOUT        1000
+#define ML305_WAIT_CONNECT_TIME         5000
+#define ML305_THREAD_STACK_SIZE         2048
+#define ML305_POWER_OFF                 RT_FALSE
+#define ML305_POWER_ON                  RT_TRUE
+#define ML305_POWER_ON_TIME             3
+#define ML305_POWER_OFF_TIME            4
+#define ML305_THREAD_PRIORITY           (RT_THREAD_PRIORITY_MAX/2)
+
+/* AT+CSTT command default*/
+static char *CSTT_CHINA_MOBILE  = "AT+CSTT=\"CMNET\"";
+static char *CSTT_CHINA_UNICOM  = "AT+CSTT=\"UNINET\"";
+static char *CSTT_CHINA_TELECOM = "AT+CSTT=\"CTNET\"";
+
+static rt_bool_t ml305_get_power_state(struct at_device *device)
+{
+    struct at_device_ml305 *ml305 = RT_NULL;
+
+    ml305 = (struct at_device_ml305 *) device->user_data;
+    
+    return (!rt_pin_read(ml305->power_status_pin)) ? ML305_POWER_ON : ML305_POWER_OFF;
+}
+
+static void ml305_power_on(struct at_device *device)
+{
+    struct at_device_ml305 *ml305 = RT_NULL;
+
+    ml305 = (struct at_device_ml305 *) device->user_data;
+
+    /* not nead to set pin configuration for ml305 device power on */
+    if (ml305->power_pin == -1 || ml305->power_status_pin == -1)
+    {
+        return;
+    }
+
+    if(ml305_get_power_state(device) == ML305_POWER_ON)
+    {
+        return;
+    }
+    
+    rt_pin_write(ml305->power_pin, PIN_HIGH);
+    rt_thread_mdelay(ML305_POWER_ON_TIME * RT_TICK_PER_SECOND);
+    rt_pin_write(ml305->power_pin, PIN_LOW);
+}
+
+static void ml305_power_off(struct at_device *device)
+{
+    struct at_device_ml305 *ml305 = RT_NULL;
+
+    ml305 = (struct at_device_ml305 *) device->user_data;
+    do{
+        /* not nead to set pin configuration for ml305 device power on */
+        if (ml305->power_pin == -1 || ml305->power_status_pin == -1)
+        {
+            return;
+        }
+
+        if(ml305_get_power_state(device) == ML305_POWER_OFF)
+        {
+            return;
+        }
+        
+        rt_pin_write(ml305->power_pin, PIN_HIGH);
+        rt_thread_mdelay(ML305_POWER_OFF_TIME * RT_TICK_PER_SECOND);
+        rt_pin_write(ml305->power_pin, PIN_LOW);
+        rt_thread_mdelay(100);
+    }while(ml305_get_power_state(device) != ML305_POWER_OFF);
+}
+
+static int ml305_check_link_status(struct at_device *device)
+{
+    at_response_t resp = RT_NULL;
+    struct at_device_ml305 *ml305 = RT_NULL;
+    int result = -RT_ERROR;
+    int link_stat = 0;
+    
+    RT_ASSERT(device);
+
+    ml305 = (struct at_device_ml305 *)device->user_data;
+    
+    resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
+    if (resp == RT_NULL)
+    {
+        LOG_E("no memory for resp create.");
+        return -RT_ERROR;
+    }
+    
+    if(at_obj_exec_cmd(device->client, resp, "AT+CGREG?") < 0)
+    {
+        result = -RT_ERROR;
+        goto __exit;
+    }
+
+    if (at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %*d,%d", &link_stat) > 0)
+    {
+        if (link_stat == 1 || link_stat == 5)
+        {
+            result = RT_EOK;
+        }
+    }
+    
+    if(at_obj_exec_cmd(device->client, resp, "AT+CGACT?") < 0)
+    {
+        result = -RT_ERROR;
+        goto __exit;
+    }
+    //+CGACT: 1,1
+    if (at_resp_parse_line_args_by_kw(resp, "+CGACT:", "+CGACT: %*d,%d", &link_stat) > 0)
+    {
+        result = link_stat;
+    }
+
+__exit:
+    
+    if(resp)
+    {
+        at_delete_resp(resp);
+    }
+    
+    return(result);
+}
+
+/* =============================  ml305 network interface operations ============================= */
+
+/* set ml305 network interface device status and address information */
+static int ml305_netdev_set_info(struct netdev *netdev)
+{
+#define ML305_IMEI_RESP_SIZE      32
+#define ML305_IPADDR_RESP_SIZE    64
+#define ML305_DNS_RESP_SIZE       96
+#define ML305_INFO_RESP_TIMO      rt_tick_from_millisecond(300)
+
+    int result = RT_EOK;
+    ip_addr_t addr;
+    at_response_t resp = RT_NULL;
+    struct at_device *device = RT_NULL;
+
+    RT_ASSERT(netdev);
+
+    device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
+    if (device == RT_NULL)
+    {
+        LOG_E("get device(%s) failed.", netdev->name);
+        return -RT_ERROR;
+    }
+
+    /* set network interface device status */
+    netdev_low_level_set_status(netdev, RT_TRUE);
+    netdev_low_level_set_link_status(netdev, RT_TRUE);
+    netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
+
+    resp = at_create_resp(ML305_IMEI_RESP_SIZE, 0, ML305_INFO_RESP_TIMO);
+    if (resp == RT_NULL)
+    {
+        LOG_E("no memory for resp create.");
+        result = -RT_ENOMEM;
+        goto __exit;
+    }
+
+    /* set network interface device hardware address(IMEI) */
+    {
+        #define ML305_NETDEV_HWADDR_LEN   8
+        #define ML305_IMEI_LEN            15
+
+        char imei[ML305_IMEI_LEN] = {0};
+        int i = 0, j = 0;
+
+        /* send "AT+GSN" commond to get device IMEI */
+        if (at_obj_exec_cmd(device->client, resp, "AT+GSN=1") < 0)
+        {
+            result = -RT_ERROR;
+            goto __exit;
+        }
+        if(at_resp_parse_line_args_by_kw(resp, "+GSN:", "+GSN:%s", imei) <= 0)
+        {
+            LOG_E("ml305 device(%s) prase \"AT+GSN\" commands resposne data error.", device->name);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+
+        LOG_D("ml305 device(%s) IMEI number: %s", device->name, imei);
+
+        netdev->hwaddr_len = ML305_NETDEV_HWADDR_LEN;
+        /* get hardware address by IMEI */
+        for (i = 0, j = 0; i < ML305_NETDEV_HWADDR_LEN && j < ML305_IMEI_LEN; i++, j += 2)
+        {
+            if (j != ML305_IMEI_LEN - 1)
+            {
+                netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
+            }
+            else
+            {
+                netdev->hwaddr[i] = (imei[j] - '0');
+            }
+        }
+    }
+
+    /* set network interface device IP address */
+    {
+        #define IP_ADDR_SIZE_MAX    16
+        char ipaddr[IP_ADDR_SIZE_MAX] = {0};
+
+        at_resp_set_info(resp, ML305_IPADDR_RESP_SIZE, 2, ML305_INFO_RESP_TIMO);
+
+        /* send "AT+CIFSR" commond to get IP address */
+        if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR=1") < 0)
+        {
+            result = -RT_ERROR;
+            goto __exit;
+        }
+
+        if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*d,\"%[^\"]", ipaddr) <= 0)
+        {
+            LOG_E("ml305 device(%s) prase \"AT+CGPADDR=1\" commands resposne data error!", device->name);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+
+        LOG_D("ml305 device(%s) IP address: %s", device->name, ipaddr);
+
+        /* set network interface address information */
+        inet_aton(ipaddr, &addr);
+        netdev_low_level_set_ipaddr(netdev, &addr);
+    }
+
+    /* set network interface device dns server */
+    {
+        #define DNS_ADDR_SIZE_MAX   16
+        char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
+
+        at_resp_set_info(resp, ML305_DNS_RESP_SIZE, 0, ML305_INFO_RESP_TIMO);
+
+        /* send "AT+CDNSCFG?" commond to get DNS servers address */
+        if (at_obj_exec_cmd(device->client, resp, "AT+MDNSCFG?") < 0)
+        {
+            result = -RT_ERROR;
+            goto __exit;
+        }
+        //+MDNSCFG: IPV4 DNS1:211.136.17.107 IPV4 DNS2:211.136.20.203
+        const char *dns_str = at_resp_get_line_by_kw(resp, "DNS1:");
+        const char *dns1_str = strstr(dns_str, "DNS1:");
+        sscanf(dns1_str, "DNS1:%s", dns_server1);
+        const char *dns2_str = strstr(dns_str, "DNS2:");
+        sscanf(dns2_str, "DNS2:%s", dns_server2);
+        
+        LOG_D("ml305 device(%s) primary DNS server address: %s", device->name, dns_server1);
+        LOG_D("ml305 device(%s) secondary DNS server address: %s", device->name, dns_server2);
+
+        inet_aton(dns_server1, &addr);
+        netdev_low_level_set_dns_server(netdev, 0, &addr);
+
+        inet_aton(dns_server2, &addr);
+        netdev_low_level_set_dns_server(netdev, 1, &addr);
+    }
+
+__exit:
+    if (resp)
+    {
+        at_delete_resp(resp);
+    }
+
+    return result;
+}
+
+static void check_link_status_entry(void *parameter)
+{
+#define ML305_LINK_STATUS_OK   1
+#define ML305_LINK_RESP_SIZE   128
+#define ML305_LINK_RESP_TIMO   (3 * RT_TICK_PER_SECOND)
+#define ML305_LINK_DELAY_TIME  (30 * RT_TICK_PER_SECOND)
+
+    at_response_t resp = RT_NULL;
+    int result_code, link_status;
+    struct at_device *device = RT_NULL;
+    struct netdev *netdev = (struct netdev *)parameter;
+
+    device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
+    if (device == RT_NULL)
+    {
+        LOG_E("get device(%s) failed.", netdev->name);
+        return;
+    }
+
+    resp = at_create_resp(ML305_LINK_RESP_SIZE, 0, ML305_LINK_RESP_TIMO);
+    if (resp == RT_NULL)
+    {
+        LOG_E("no memory for response create.");
+        return;
+    }
+    while(1)
+    {
+        link_status = ml305_check_link_status(device);
+        if(link_status < 0)
+        {
+            rt_thread_mdelay(ML305_LINK_DELAY_TIME);
+            continue;
+        }
+        /* check the network interface device link status  */
+        if ((ML305_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
+        {
+            netdev_low_level_set_link_status(netdev, (ML305_LINK_STATUS_OK == link_status));
+        }
+        
+        rt_thread_mdelay(ML305_LINK_DELAY_TIME);
+    }
+}
+
+static int ml305_netdev_check_link_status(struct netdev *netdev)
+{
+#define ML305_LINK_THREAD_TICK           20
+#define ML305_LINK_THREAD_STACK_SIZE     (1024 + 512)
+#define ML305_LINK_THREAD_PRIORITY       (RT_THREAD_PRIORITY_MAX - 2)
+
+    rt_thread_t tid;
+    char tname[RT_NAME_MAX] = {0};
+
+    if (netdev == RT_NULL)
+    {
+        LOG_E("input network interface device is NULL.\n");
+        return -RT_ERROR;
+    }
+
+    rt_snprintf(tname, RT_NAME_MAX, "%s_link", netdev->name);
+
+    tid = rt_thread_create(tname, check_link_status_entry, (void *) netdev,
+            ML305_LINK_THREAD_STACK_SIZE, ML305_LINK_THREAD_PRIORITY, ML305_LINK_THREAD_TICK);
+    if (tid)
+    {
+        rt_thread_startup(tid);
+    }
+
+    return RT_EOK;
+}
+
+static int ml305_net_init(struct at_device *device);
+
+static int ml305_netdev_set_up(struct netdev *netdev)
+{
+    struct at_device *device = RT_NULL;
+
+    device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
+    if (device == RT_NULL)
+    {
+        LOG_E("get ml305 device by netdev name(%s) failed.", netdev->name);
+        return -RT_ERROR;
+    }
+
+    if (device->is_init == RT_FALSE)
+    {
+        device->is_init = RT_TRUE;
+        ml305_net_init(device);
+
+        netdev_low_level_set_status(netdev, RT_TRUE);
+        LOG_D("the network interface device(%s) set up status.", netdev->name);
+    }
+
+    return RT_EOK;
+}
+
+static int ml305_netdev_set_down(struct netdev *netdev)
+{
+    struct at_device *device = RT_NULL;
+
+    device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
+    if (device == RT_NULL)
+    {
+        LOG_E("get ml305 device by netdev name(%s) failed.", netdev->name);
+        return -RT_ERROR;
+    }
+
+    if (device->is_init == RT_TRUE)
+    {
+        ml305_power_off(device);
+        device->is_init = RT_FALSE;
+
+        netdev_low_level_set_status(netdev, RT_FALSE);
+        LOG_D("the network interface device(%s) set down status.", netdev->name);
+    }
+
+    return RT_EOK;
+}
+
+static int ml305_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
+{
+#define ML305_DNS_RESP_LEN     8
+#define ML305_DNS_RESP_TIMEO   rt_tick_from_millisecond(300)
+
+    int result = RT_EOK;
+    at_response_t resp = RT_NULL;
+    struct at_device *device = RT_NULL;
+
+    RT_ASSERT(netdev);
+    RT_ASSERT(dns_server);
+
+    device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
+    if (device == RT_NULL)
+    {
+        LOG_E("get ml305 device by netdev name(%s) failed.", netdev->name);
+        return -RT_ERROR;
+    }
+
+    resp = at_create_resp(ML305_DNS_RESP_LEN, 0, ML305_DNS_RESP_TIMEO);
+    if (resp == RT_NULL)
+    {
+        LOG_D("ml305 set dns server failed, no memory for response object.");
+        result = -RT_ENOMEM;
+        goto __exit;
+    }
+
+    /* send "AT+CDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
+    if (at_obj_exec_cmd(device->client, resp, "AT+CDNSCFG=\"%s\"", inet_ntoa(*dns_server)) < 0)
+    {
+        result = -RT_ERROR;
+        goto __exit;
+    }
+
+    netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
+
+__exit:
+    if (resp)
+    {
+        at_delete_resp(resp);
+    }
+
+    return result;
+}
+
+static int ml305_ping_domain_resolve(struct at_device *device, const char *name, char ip[16])
+{
+    int result = RT_EOK;
+    char recv_ip[16] = { 0 };
+    at_response_t resp = RT_NULL;
+
+    /* The maximum response time is 14 seconds, affected by network status */
+    resp = at_create_resp(512, 4, 14 * RT_TICK_PER_SECOND);
+    if (resp == RT_NULL)
+    {
+        LOG_E("no memory for ml305 device(%s) response structure.", device->name);
+        return -RT_ENOMEM;
+    }
+
+    if (at_obj_exec_cmd(device->client, resp, "AT+CGACT=1,1") < 0)
+    {
+        result = -RT_ERROR;
+        goto __exit;
+    }
+
+    if (at_obj_exec_cmd(device->client, resp, "AT+MDNSGIP=\"%s\"", name) < 0)
+    {
+        result = -RT_ERROR;
+        goto __exit;
+    }
+
+    if (at_resp_parse_line_args_by_kw(resp, "+MDNSGIP:", "%*[^,],%*[^,],\"%[^\"]", recv_ip) < 0)
+    {
+        rt_thread_mdelay(100);
+        /* resolve failed, maybe receive an URC CRLF */
+    }
+
+    if (rt_strlen(recv_ip) < 8)
+    {
+        rt_thread_mdelay(100);
+        /* resolve failed, maybe receive an URC CRLF */
+    }
+    else
+    {
+        rt_strncpy(ip, recv_ip, 15);
+        ip[15] = '\0';
+    }
+
+__exit:
+    if (resp)
+    {
+        at_delete_resp(resp);
+    }
+
+    return result;
+}
+
+#ifdef NETDEV_USING_PING
+#ifdef AT_DEVICE_USING_ML305
+static int ml305_netdev_ping(struct netdev *netdev, const char *host,
+        size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
+{
+    rt_kprintf("I don't have PING function!\r\n");
+    return RT_EOK;
+}
+#endif
+#endif /* NETDEV_USING_PING */
+
+#ifdef NETDEV_USING_NETSTAT
+void ml305_netdev_netstat(struct netdev *netdev)
+{
+    // TODO netstat support
+}
+#endif /* NETDEV_USING_NETSTAT */
+
+const struct netdev_ops ml305_netdev_ops =
+{
+    ml305_netdev_set_up,
+    ml305_netdev_set_down,
+
+    RT_NULL, /* not support set ip, netmask, gatway address */
+    ml305_netdev_set_dns_server,
+    RT_NULL, /* not support set DHCP status */
+
+#ifdef NETDEV_USING_PING
+    ml305_netdev_ping,
+#endif
+#ifdef NETDEV_USING_NETSTAT
+    ml305_netdev_netstat,
+#endif
+};
+
+static struct netdev *ml305_netdev_add(const char *netdev_name)
+{
+#define ML305_NETDEV_MTU       1500
+    struct netdev *netdev = RT_NULL;
+
+    RT_ASSERT(netdev_name);
+
+    netdev = netdev_get_by_name(netdev_name);
+    if (netdev != RT_NULL)
+    {
+        return (netdev);
+    }
+
+    netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
+    if (netdev == RT_NULL)
+    {
+        LOG_E("no memory for ml305 device(%s) netdev structure.", netdev_name);
+        return RT_NULL;
+    }
+
+    netdev->mtu = ML305_NETDEV_MTU;
+    netdev->ops = &ml305_netdev_ops;
+
+#ifdef SAL_USING_AT
+    extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
+    /* set the network interface socket/netdb operations */
+    sal_at_netdev_set_pf_info(netdev);
+#endif
+
+    netdev_register(netdev, netdev_name, RT_NULL);
+
+    return netdev;
+}
+
+/* =============================  ml305 device operations ============================= */
+
+#define AT_SEND_CMD(client, resp, resp_line, timeout, cmd)                                         \
+    do {                                                                                           \
+        (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout));    \
+        if (at_obj_exec_cmd((client), (resp), (cmd)) < 0)                                          \
+        {                                                                                          \
+            result = -RT_ERROR;                                                                    \
+            goto __exit;                                                                           \
+        }                                                                                          \
+    } while(0)                                                                                     \
+
+/* init for ml305 */
+static void ml305_init_thread_entry(void *parameter)
+{
+#define INIT_RETRY                      5
+#define CPIN_RETRY                      10
+#define CSQ_RETRY                       10
+#define CREG_RETRY                      10
+#define CGREG_RETRY                     20
+#define IPADDR_RETRY                    10
+#define CGATT_RETRY                     10
+#define COMMON_RETRY                    10
+    int i, qimux, retry_num = INIT_RETRY;
+    char parsed_data[10] = {0};
+    rt_err_t result = RT_EOK;
+    at_response_t resp = RT_NULL;
+    struct at_device *device = (struct at_device *)parameter;
+    struct at_client *client = device->client;
+
+    resp = at_create_resp(128, 0, rt_tick_from_millisecond(500));
+    if (resp == RT_NULL)
+    {
+        LOG_E("no memory for ml305 device(%s) response structure.", device->name);
+        return;
+    }
+
+    LOG_D("start initializing the device(%s)", device->name);
+    ml305_power_off(device);
+    while (retry_num--)
+    {
+        rt_memset(parsed_data, 0, sizeof(parsed_data));
+        rt_thread_mdelay(500);
+        ml305_power_on(device);
+        rt_thread_mdelay(1000);
+
+        /* wait ml305 startup finish */
+        if (at_client_obj_wait_connect(client, ML305_WAIT_CONNECT_TIME))
+        {
+            result = -RT_ETIMEOUT;
+            goto __exit;
+        }
+
+        /* disable echo */
+        AT_SEND_CMD(client, resp, 0, ML305_AT_DEFAULT_TIMEOUT, "ATE0");
+        /* get module version */
+        AT_SEND_CMD(client, resp, 5, ML305_AT_DEFAULT_TIMEOUT, "ATI");
+        /* show module version */
+        for (i = 0; i < (int)resp->line_counts - 1; i++)
+        {
+            LOG_D("%s", at_resp_get_line(resp, i + 1));
+        }
+        /* check SIM card */
+        for (i = 0; i < CPIN_RETRY; i++)
+        {
+            AT_SEND_CMD(client, resp, 2, 5 * RT_TICK_PER_SECOND, "AT+CPIN?");
+
+            if (at_resp_get_line_by_kw(resp, "READY"))
+            {
+                LOG_I("ml305 device(%s) SIM card detection success.", device->name);
+                break;
+            }
+            rt_thread_mdelay(500);
+        }
+        if (i == CPIN_RETRY)
+        {
+            LOG_E("ml305 device(%s) SIM card detection failed.", device->name);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+        /* check SIM card */
+        for (i = 0; i < CPIN_RETRY; i++)
+        {
+            AT_SEND_CMD(client, resp, 2, 10 * 1000, "AT+ICCID");
+            if (at_resp_get_line_by_kw(resp, "+ICCID:"))
+            {
+                LOG_D("%s device SIM card detection success.", device->name);
+                break;
+            }
+            rt_thread_mdelay(1000);
+        }
+        if (i == CPIN_RETRY)
+        {
+            LOG_E("%s device SIM card detection failed.", device->name);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+        
+        /* check signal strength */
+        for (i = 0; i < CSQ_RETRY; i++)
+        {
+            AT_SEND_CMD(client, resp, 0, ML305_AT_DEFAULT_TIMEOUT, "AT+CSQ");
+            at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
+            if (rt_strncmp(parsed_data, "99,99", sizeof(parsed_data)))
+            {
+                LOG_D("signal strength: %s", parsed_data);
+                break;
+            }
+            rt_thread_mdelay(2000);
+        }
+        if(i == CSQ_RETRY)
+        {
+            LOG_E("%s device signal strength check failed(%s).", device->name, parsed_data);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+        
+        /* check the GSM network is registered */
+        for (i = 0; i < CREG_RETRY; i++)
+        {
+            AT_SEND_CMD(client, resp, 0, ML305_AT_DEFAULT_TIMEOUT, "AT+CREG?");
+            at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
+            if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
+                !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
+            {
+                LOG_D("ml305 device(%s) GSM network is registered(%s),", device->name, parsed_data);
+                break;
+            }
+            if(!strncmp(parsed_data, "0,3", sizeof(parsed_data)))
+            {
+                LOG_E("%s device GSM network is register failed(%s).", device->name, parsed_data);
+                result = -RT_ERROR;
+                goto __exit;
+            }
+            rt_thread_mdelay(1000 + 500 * (i + 1));
+        }
+        if (i == CREG_RETRY)
+        {
+            LOG_E("%s device GSM network is register failed(%s).", device->name, parsed_data);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+
+        /* check packet domain attach or detach */
+        for (i = 0; i < CGATT_RETRY; i++)
+        {
+            AT_SEND_CMD(client, resp, 0, ML305_AT_DEFAULT_TIMEOUT, "AT+CGATT?");
+            at_resp_parse_line_args_by_kw(resp, "+CGATT:", "+CGATT: %s", &parsed_data);
+            if (!rt_strncmp(parsed_data, "1", 1))
+            {
+                LOG_D("%s device Packet domain attach.", device->name);
+                break;
+            }
+
+            rt_thread_mdelay(1000);
+        }
+        if (i == CGATT_RETRY)
+        {
+            LOG_E("%s device GPRS attach failed.", device->name);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+
+        /* Define PDP Context */
+        for (i = 0; i < COMMON_RETRY; i++)
+        {
+            if (at_obj_exec_cmd(client, resp, "AT+CGDCONT=1,\"IP\",\"CMIOT\"") == RT_EOK)
+            {
+                LOG_D("%s device Define PDP Context Success.", device->name);
+                break;
+            }
+            rt_thread_mdelay(100);
+        }
+        if (i == COMMON_RETRY)
+        {
+            LOG_E("%s device Define PDP Context failed.", device->name);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+
+        /* PDP Context Activate*/
+        for (i = 0; i < COMMON_RETRY; i++)
+        {
+            if (at_obj_exec_cmd(client, resp, "AT+CGACT=1,1") == RT_EOK)
+            {
+                LOG_D("%s device PDP Context Activate Success.", device->name);
+                break;
+            }
+            rt_thread_mdelay(500);
+        }
+        if (i == COMMON_RETRY)
+        {
+            LOG_E("%s device PDP Context Activate failed.", device->name);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+        
+#if defined (AT_DEBUG)
+        /* check the GPRS network IP address */
+        for (i = 0; i < IPADDR_RETRY; i++)
+        {
+            if (at_obj_exec_cmd(client, resp, "AT+CGPADDR=1") == RT_EOK)
+            {
+                #define IP_ADDR_SIZE_MAX    16
+                char ipaddr[IP_ADDR_SIZE_MAX] = {0};
+
+                /* parse response data "+CGPADDR: 1,<IP_address>" */
+                if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*d,%s", ipaddr) > 0)
+                {
+                    LOG_D("%s device IP address: %s", device->name, ipaddr);
+                    break;
+                }
+            }
+            rt_thread_mdelay(1000);
+        }
+        if (i == IPADDR_RETRY)
+        {
+            LOG_E("%s device GPRS is get IP address failed", device->name);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+#endif
+
+        result = RT_EOK;
+        
+__exit:
+        if (result == RT_EOK)
+        {
+            break;
+        }
+        else
+        {
+            /* power off the ml305 device */
+            ml305_power_off(device);
+            rt_thread_mdelay(1000);
+
+            LOG_I("ml305 device(%s) initialize retry...", device->name);
+        }
+    }
+
+    if (resp)
+    {
+        at_delete_resp(resp);
+    }
+
+    if (result == RT_EOK)
+    {
+        device->is_init = RT_TRUE;
+
+        /* set network interface device status and address information */
+        ml305_netdev_set_info(device->netdev);
+        /*  */
+        ml305_netdev_check_link_status(device->netdev);
+        /*  */
+        LOG_I("ml305 device(%s) network initialize success!", device->name);
+
+    }
+    else
+    {
+        device->is_init = RT_FALSE;
+
+        netdev_low_level_set_status(device->netdev, RT_FALSE);
+        LOG_E("ml305 device(%s) network initialize failed(%d)!", device->name, result);
+    }
+}
+
+static int ml305_net_init(struct at_device *device)
+{
+#ifdef AT_DEVICE_ML305_INIT_ASYN
+    rt_thread_t tid;
+
+    tid = rt_thread_create("ml305_net_init", ml305_init_thread_entry, (void *)device,
+                ML305_THREAD_STACK_SIZE, ML305_THREAD_PRIORITY, 20);
+    if (tid)
+    {
+        rt_thread_startup(tid);
+    }
+    else
+    {
+        LOG_E("create ml305 device(%s) initialization thread failed.", device->name);
+        return -RT_ERROR;
+    }
+#else
+    ml305_init_thread_entry(device);
+#endif /* AT_DEVICE_ML305_INIT_ASYN */
+
+    return RT_EOK;
+}
+
+static void urc_func(struct at_client *client, const char *data, rt_size_t size)
+{
+    RT_ASSERT(data);
+
+    LOG_I("URC data : %.*s", size, data);
+}
+
+/* ml305 device URC table for the device control */
+static const struct at_urc urc_table[] =
+{
+        {"READY",         "\r\n",                 urc_func},
+};
+
+static int ml305_init(struct at_device *device)
+{
+    struct at_device_ml305 *ml305 = (struct at_device_ml305 *) device->user_data;
+
+    struct serial_configure serial_config = RT_SERIAL_CONFIG_DEFAULT;
+    
+    rt_device_t serial = rt_device_find(ml305->client_name);
+    
+     if (serial == RT_NULL)
+    {
+        LOG_E("ml305 device(%s) initialize failed, get AT client(%s) failed.", ml305->device_name, ml305->client_name);
+        return -RT_ERROR;
+    }
+    serial_config.bufsz = ml305->recv_buff_size;
+    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &serial_config);
+    
+    /* initialize AT client */
+    at_client_init(ml305->client_name, ml305->recv_buff_size);
+
+    device->client = at_client_get(ml305->client_name);
+    if (device->client == RT_NULL)
+    {
+        LOG_E("ml305 device(%s) initialize failed, get AT client(%s) failed.", ml305->device_name, ml305->client_name);
+        return -RT_ERROR;
+    }
+
+    /* register URC data execution function  */
+    at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
+
+#ifdef AT_USING_SOCKET
+    ml305_socket_init(device);
+#endif
+
+    /* add ml305 device to the netdev list */
+    device->netdev = ml305_netdev_add(ml305->device_name);
+    if (device->netdev == RT_NULL)
+    {
+        LOG_E("ml305 device(%s) initialize failed, get network interface device failed.", ml305->device_name);
+        return -RT_ERROR;
+    }
+
+    /* initialize ml305 pin configuration */
+    if (ml305->power_pin != -1)
+    {
+        rt_pin_mode(ml305->power_pin, PIN_MODE_OUTPUT);
+    }
+    
+    if (ml305->power_status_pin != -1)
+    {
+        rt_pin_mode(ml305->power_status_pin, PIN_MODE_INPUT_PULLUP);
+    }
+    /* initialize ml305 device network */
+    return ml305_netdev_set_up(device->netdev);
+}
+
+static int ml305_deinit(struct at_device *device)
+{
+    return ml305_netdev_set_down(device->netdev);
+}
+
+static int ml305_control(struct at_device *device, int cmd, void *arg)
+{
+    int result = -RT_ERROR;
+
+    RT_ASSERT(device);
+
+    switch (cmd)
+    {
+    case AT_DEVICE_CTRL_POWER_ON:
+    case AT_DEVICE_CTRL_POWER_OFF:
+    case AT_DEVICE_CTRL_RESET:
+    case AT_DEVICE_CTRL_LOW_POWER:
+    case AT_DEVICE_CTRL_SLEEP:
+    case AT_DEVICE_CTRL_WAKEUP:
+    case AT_DEVICE_CTRL_NET_CONN:
+    case AT_DEVICE_CTRL_NET_DISCONN:
+    case AT_DEVICE_CTRL_SET_WIFI_INFO:
+    case AT_DEVICE_CTRL_GET_SIGNAL:
+    case AT_DEVICE_CTRL_GET_GPS:
+    case AT_DEVICE_CTRL_GET_VER:
+        LOG_W("ml305 not support the control command(%d).", cmd);
+        break;
+    default:
+        LOG_E("input error control command(%d).", cmd);
+        break;
+    }
+
+    return result;
+}
+
+const struct at_device_ops ml305_device_ops =
+{
+    ml305_init,
+    ml305_deinit,
+    ml305_control,
+};
+
+static int ml305_device_class_register(void)
+{
+    struct at_device_class *class = RT_NULL;
+
+    class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
+    if (class == RT_NULL)
+    {
+        LOG_E("no memory for ml305 device class create.");
+        return -RT_ENOMEM;
+    }
+
+    /* fill ml305 device class object */
+#ifdef AT_USING_SOCKET
+    ml305_socket_class_register(class);
+#endif
+    class->device_ops = &ml305_device_ops;
+
+    return at_device_class_register(class, AT_DEVICE_CLASS_ML305);
+}
+INIT_DEVICE_EXPORT(ml305_device_class_register);
+
+#endif /* AT_DEVICE_USING_ML305 */

+ 54 - 0
class/ml305/at_device_ml305.h

@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-12-16     Jonas        first version
+ */
+
+#ifndef __ML305_DEVICE_H__
+#define __ML305_DEVICE_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
+
+#include <at_device.h>
+
+#define ML305_AT_RESP_TIMEOUT       RT_TICK_PER_SECOND
+
+/* The maximum number of sockets supported by the ml305 device */
+#define AT_DEVICE_ML305_SOCKETS_NUM         6
+
+struct at_device_ml305
+{
+    char *device_name;
+    char *client_name;
+
+    int power_pin;
+    int power_status_pin;
+    size_t recv_buff_size;
+    struct at_device device;
+ 
+    void *user_data;
+};
+
+#ifdef AT_USING_SOCKET
+
+/* ml305 device socket initialize */
+int ml305_socket_init(struct at_device *device);
+
+/* ml305 device class socket register */
+int ml305_socket_class_register(struct at_device_class *class);
+
+#endif /* AT_USING_SOCKET */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  /*  __AT_DEVICE_ML305_H__*/

+ 649 - 0
class/ml305/at_socket_ml305.c

@@ -0,0 +1,649 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-12-16     Jonas        first version
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <at_device_ml305.h>
+
+#define LOG_TAG                        "at.skt.ml305"
+#include <at_log.h>
+
+#if defined(AT_DEVICE_USING_ML305) && defined(AT_USING_SOCKET)
+#if !defined (ML305_MODULE_SEND_MAX_SIZE)
+#define ML305_MODULE_SEND_MAX_SIZE   4096
+#endif
+/* set real event by current socket and current state */
+#define SET_EVENT(socket, event)       (((socket + 1) << 16) | (event))
+
+/* AT socket event type */
+#define ML305_EVENT_CONN_OK          (1L << 0)
+#define ML305_EVENT_SEND_OK          (1L << 1)
+#define ML305_EVENT_RECV_OK          (1L << 2)
+#define ML305_EVENT_CLOSE_OK         (1L << 3)
+#define ML305_EVENT_CONN_FAIL        (1L << 4)
+#define ML305_EVENT_SEND_FAIL        (1L << 5)
+
+static at_evt_cb_t at_evt_cb_set[] = {
+        [AT_SOCKET_EVT_RECV] = NULL,
+        [AT_SOCKET_EVT_CLOSED] = NULL,
+};
+
+static int ml305_socket_event_send(struct at_device *device, uint32_t event)
+{
+    return (int) rt_event_send(device->socket_event, event);
+}
+
+static int ml305_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
+{
+    int result = RT_EOK;
+    rt_uint32_t recved;
+
+    result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
+    if (result != RT_EOK)
+    {
+        return -RT_ETIMEOUT;
+    }
+
+    return recved;
+}
+
+/**
+ * close socket by AT commands.
+ *
+ * @param current socket
+ *
+ * @return  0: close socket success
+ *         -1: send AT commands error
+ *         -2: wait socket event timeout
+ *         -5: no memory
+ */
+static int ml305_socket_close(struct at_socket *socket)
+{
+    uint32_t event = 0;
+    int result = RT_EOK;
+    int device_socket = (int) socket->user_data;
+    struct at_device *device = (struct at_device *) socket->device;
+
+    /* clear socket close event */
+    event = SET_EVENT(device_socket, ML305_EVENT_CLOSE_OK);
+    ml305_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
+    
+    if (at_obj_exec_cmd(device->client, NULL, "AT+MIPCLOSE=%d", device_socket) < 0)
+    {
+        result = -RT_ERROR;
+        goto __exit;
+    }
+    
+    if (ml305_socket_event_recv(device, event, rt_tick_from_millisecond(300 * 3), RT_EVENT_FLAG_AND) < 0)
+    {
+        LOG_E("ml305 device(%s) socket(%d) close failed, wait close OK timeout.", device->name, device_socket);
+        result = -RT_ETIMEOUT;
+        goto __exit;
+    }
+
+__exit:
+    return result;
+}
+
+/**
+ * create TCP/UDP client or server connect by AT commands.
+ *
+ * @param socket current socket
+ * @param ip server or client IP address
+ * @param port server or client port
+ * @param type connect socket type(tcp, udp)
+ * @param is_client connection is client
+ *
+ * @return   0: connect success
+ *          -1: connect failed, send commands error or type error
+ *          -2: wait socket event timeout
+ *          -5: no memory
+ */
+static int ml305_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
+{
+    uint32_t event = 0;
+    rt_bool_t retryed = RT_FALSE;
+    at_response_t resp = RT_NULL;
+    int result = RT_EOK, event_result = 0;
+    int device_socket = (int) socket->user_data;
+    struct at_device *device = (struct at_device *) socket->device;
+
+    RT_ASSERT(ip);
+    RT_ASSERT(port >= 0);
+
+    resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
+    if (resp == RT_NULL)
+    {
+        LOG_E("no memory for ml305 device(%s) response structure.", device->name);
+        return -RT_ENOMEM;
+    }
+
+__retry:
+
+    /* clear socket connect event */
+    event = SET_EVENT(device_socket, ML305_EVENT_CONN_OK | ML305_EVENT_CONN_FAIL);
+    ml305_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
+
+    if (is_client)
+    {
+        switch (type)
+        {
+        case AT_SOCKET_TCP:
+            /* send AT commands(eg: AT+MIPOPEN=0,"TCP","x.x.x.x", 1234) to connect TCP server */
+            if (at_obj_exec_cmd(device->client, RT_NULL, "AT+MIPOPEN=%d,\"TCP\",\"%s\",%d", device_socket, ip, port) < 0)
+            {
+                result = -RT_ERROR;
+                goto __exit;
+            }
+            break;
+
+        case AT_SOCKET_UDP:
+            if (at_obj_exec_cmd(device->client, RT_NULL, "AT+MIPOPEN=%d,\"UDP\",\"%s\",%d", device_socket, ip, port) < 0)
+            {
+                result = -RT_ERROR;
+                goto __exit;
+            }
+            break;
+
+        default:
+            LOG_E("ml305 device(%s) not supported connect type : %d.", device->name, type);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+            }
+
+    /* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use */
+    if (ml305_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
+    {
+        LOG_E("ml305 device(%s) socket(%d) connect failed, wait connect result timeout.",device->name, device_socket);
+        result = -RT_ETIMEOUT;
+        goto __exit;
+    }
+    /* waiting OK or failed result */
+    event_result = ml305_socket_event_recv(device,
+            ML305_EVENT_CONN_OK | ML305_EVENT_CONN_FAIL, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
+    if (event_result < 0)
+    {
+        LOG_E("ml305 device(%s) socket(%d) connect failed, wait connect OK|FAIL timeout.", device->name, device_socket);
+        result = -RT_ETIMEOUT;
+        goto __exit;
+    }
+    /* check result */
+    if (event_result & ML305_EVENT_CONN_FAIL)
+    {
+        if (retryed == RT_FALSE)
+        {
+            LOG_D("ml305 device(%s) socket(%d) connect failed, maybe the socket was not be closed at the last time and now will retry.",
+                    device->name, device_socket);
+            if (ml305_socket_close(socket) < 0)
+            {
+                result = -RT_ERROR;
+                goto __exit;
+            }
+            retryed = RT_TRUE;
+            goto __retry;
+        }
+        LOG_E("ml305 device(%s) socket(%d) connect failed.", device->name, device_socket);
+        result = -RT_ERROR;
+        goto __exit;
+    }
+
+__exit:
+    if (resp)
+    {
+        at_delete_resp(resp);
+    }
+
+    return result;
+}
+
+/**
+ * send data to server or client by AT commands.
+ *
+ * @param socket current socket
+ * @param buff send buffer
+ * @param bfsz send buffer size
+ * @param type connect socket type(tcp, udp)
+ *
+ * @return >=0: the size of send success
+ *          -1: send AT commands error or send data error
+ *          -2: waited socket event timeout
+ *          -5: no memory
+ */
+static int ml305_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
+{
+    uint32_t event = 0;
+    int result = RT_EOK, event_result = 0;
+    size_t cur_pkt_size = 0, sent_size = 0;
+    at_response_t resp = RT_NULL;
+    int device_socket = (int) socket->user_data;
+    struct at_device *device = (struct at_device *) socket->device;
+    rt_mutex_t lock = device->client->lock;
+
+    RT_ASSERT(buff);
+
+    resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
+    if (resp == RT_NULL)
+    {
+        LOG_E("no memory for ml305 device(%s) response structure.", device->name);
+        return -RT_ENOMEM;
+    }
+
+    rt_mutex_take(lock, RT_WAITING_FOREVER);
+
+    /* clear socket connect event */
+    event = SET_EVENT(device_socket, ML305_EVENT_SEND_OK | ML305_EVENT_SEND_FAIL);
+    ml305_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
+    
+    /* set AT client end sign to deal with '>' sign.*/
+    at_obj_set_end_sign(device->client, '>');
+
+    while (sent_size < bfsz)
+    {
+        if (bfsz - sent_size < ML305_MODULE_SEND_MAX_SIZE)
+        {
+            cur_pkt_size = bfsz - sent_size;
+        }
+        else
+        {
+            cur_pkt_size = ML305_MODULE_SEND_MAX_SIZE;
+        }
+
+        /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
+        if (at_obj_exec_cmd(device->client, resp, "AT+MIPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
+        {
+            result = -RT_ERROR;
+            goto __exit;
+        }
+
+        /* send the real data to server or client */
+        result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
+        if (result == 0)
+        {
+            result = -RT_ERROR;
+            goto __exit;
+        }
+        /* waiting result event from AT URC */
+        if (ml305_socket_event_recv(device, SET_EVENT(device_socket, 0), 15 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
+        {
+            LOG_E("ml305 device(%s) socket(%d) send failed, wait connect result timeout.", device->name, device_socket);
+            result = -RT_ETIMEOUT;
+            goto __exit;
+        }
+        /* waiting OK or failed result */
+        event_result = ml305_socket_event_recv(device,
+                                                ML305_EVENT_SEND_OK | ML305_EVENT_SEND_FAIL, 5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
+        if (event_result < 0)
+        {
+            LOG_E("ml305 device(%s) socket(%d) send failed, wait connect OK|FAIL timeout.", device->name, device_socket);
+            result = -RT_ETIMEOUT;
+            goto __exit;
+        }
+        /* check result */
+        if (event_result & ML305_EVENT_SEND_FAIL)
+        {
+            LOG_E("ml305 device(%s) socket(%d) send failed.", device->name, device_socket);
+            result = -RT_ERROR;
+            goto __exit;
+        }
+
+        sent_size += cur_pkt_size;
+    }
+
+__exit:
+    /* reset the end sign for data conflict */
+    at_obj_set_end_sign(device->client, 0);
+
+    rt_mutex_release(lock);
+
+    if (resp)
+    {
+        at_delete_resp(resp);
+    }
+
+    return result;
+}
+
+/**
+ * domain resolve by AT commands.
+ *
+ * @param name domain name
+ * @param ip parsed IP address, it's length must be 16
+ *
+ * @return  0: domain resolve success
+ *         -1: send AT commands error or response error
+ *         -2: wait socket event timeout
+ *         -5: no memory
+ */
+static int ml305_domain_resolve(const char *name, char ip[16])
+{
+#define RESOLVE_RETRY                  5
+
+    int i, result = RT_EOK;
+    char recv_ip[16] = { 0 };
+    at_response_t resp = RT_NULL;
+    struct at_device *device = RT_NULL;
+
+    RT_ASSERT(name);
+    RT_ASSERT(ip);
+
+    device = at_device_get_first_initialized();
+    if (device == RT_NULL)
+    {
+        LOG_E("get first initialization ml305 device failed.");
+        return -RT_ERROR;
+    }
+
+    /* The maximum response time is 14 seconds, affected by network status */
+    resp = at_create_resp(1024, 4, 14 * RT_TICK_PER_SECOND);
+
+    if (resp == RT_NULL)
+    {
+        LOG_E("no memory for ml305 device(%s) response structure.", device->name);
+        return -RT_ENOMEM;
+    }
+
+    for (i = 0; i < RESOLVE_RETRY; i++)
+    {
+        int err_code = 0;
+
+        if (at_obj_exec_cmd(device->client, resp, "AT+MDNSGIP=\"%s\"", name) < 0)
+        {
+            result = -RT_ERROR;
+            goto __exit;
+        }
+
+        /* domain name prase error options */
+        if (at_resp_parse_line_args_by_kw(resp, "+MDNSGIP:", "+MDNSGIP: 1,%d", &err_code) > 0)
+        {
+            /* 3 - network error, 8 - dns common error */
+            if (err_code == 3 || err_code == 8)
+            {
+                result = -RT_ERROR;
+                goto __exit;
+            }
+        }
+
+        /* parse the third line of response data, get the IP address */
+        if (at_resp_parse_line_args_by_kw(resp, "+MDNSGIP: 0", "%*[^,],%*[^,],\"%[^\"]", recv_ip) < 0)
+        {
+            rt_thread_mdelay(100);
+            /* resolve failed, maybe receive an URC CRLF */
+            continue;
+        }
+
+        if (rt_strlen(recv_ip) < 8)
+        {
+            rt_thread_mdelay(100);
+            /* resolve failed, maybe receive an URC CRLF */
+            continue;
+        }
+        else
+        {
+            rt_thread_mdelay(10);
+            rt_strncpy(ip, recv_ip, 15);
+            ip[15] = '\0';
+            break;
+        }
+    }
+
+__exit:
+    if (resp)
+    {
+        at_delete_resp(resp);
+    }
+
+    return result;
+}
+
+/**
+ * set AT socket event notice callback
+ *
+ * @param event notice event
+ * @param cb notice callback
+ */
+static void ml305_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
+{
+    if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
+    {
+        at_evt_cb_set[event] = cb;
+    }
+}
+
+static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
+{
+    int device_socket = 0;
+    struct at_device *device = RT_NULL;
+    char *client_name = client->device->parent.name;
+
+    RT_ASSERT(data && size);
+
+    device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
+    if (device == RT_NULL)
+    {
+        LOG_E("get ml305 device by client name(%s) failed.", client_name);
+        return;
+    }
+
+    /* get the current socket by receive data */
+    sscanf(data, "%d,%*s", &device_socket);
+
+    if (strstr(data, "CONNECT OK"))
+    {
+        ml305_socket_event_send(device, SET_EVENT(device_socket, ML305_EVENT_CONN_OK));
+    }
+    else if (strstr(data, "CONNECT FAIL"))
+    {
+        ml305_socket_event_send(device, SET_EVENT(device_socket, ML305_EVENT_CONN_FAIL));
+    }
+}
+
+static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
+{
+    int device_socket = 0;
+    struct at_device *device = RT_NULL;
+    char *client_name = client->device->parent.name;
+
+    RT_ASSERT(data && size);
+
+    device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
+    if (device == RT_NULL)
+    {
+        LOG_E("get ml305 device by client name(%s) failed.", client_name);
+        return;
+    }
+
+    /* get the current socket by receive data */
+    sscanf(data, "%d,%*s", &device_socket);
+
+    if (rt_strstr(data, "SEND OK"))
+    {
+        ml305_socket_event_send(device, SET_EVENT(device_socket, ML305_EVENT_SEND_OK));
+    }
+    else if (rt_strstr(data, "SEND FAIL"))
+    {
+        ml305_socket_event_send(device, SET_EVENT(device_socket, ML305_EVENT_SEND_FAIL));
+    }
+}
+
+static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
+{
+    int device_socket = 0;
+    struct at_device *device = RT_NULL;
+    char *client_name = client->device->parent.name;
+
+    RT_ASSERT(data && size);
+
+    device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
+    if (device == RT_NULL)
+    {
+        LOG_E("get ml305 device by client name(%s) failed.", client_name);
+        return;
+    }
+
+    /* get the current socket by receive data */
+    sscanf(data, "%d,%*s", &device_socket);
+
+    if (rt_strstr(data, "CLOSE OK"))
+    {
+        ml305_socket_event_send(device, SET_EVENT(device_socket, ML305_EVENT_CLOSE_OK));
+    }
+    else if (rt_strstr(data, "CLOSED"))
+    {
+        struct at_socket *socket = RT_NULL;
+
+        /* get AT socket object by device socket descriptor */
+        socket = &(device->sockets[device_socket]);
+
+        /* notice the socket is disconnect by remote */
+        if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
+        {
+            at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
+        }
+    }
+}
+
+static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
+{
+    int device_socket = 0;
+    rt_int32_t timeout;
+    rt_size_t temp_size = 0;
+    char temp[8] = {0};
+    rt_size_t bfsz = 0;
+    char *recv_buf = RT_NULL;
+    struct at_socket *socket = RT_NULL;
+    struct at_device *device = RT_NULL;
+    char *client_name = client->device->parent.name;
+
+    RT_ASSERT(data && size);
+
+    /* get the current socket and receive buffer size by receive data */
+    sscanf(data, "+MIPURC:%*[^,],%d,%d", &device_socket, (int *) &bfsz);
+    /* get receive timeout by receive buffer length */
+    timeout = bfsz;
+
+    if (device_socket < 0 || bfsz == 0)
+    {
+        return;
+    }
+
+    device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
+    if (device == RT_NULL)
+    {
+        LOG_E("get ml305 device by client name(%s) failed.", client_name);
+        return;
+    }
+
+    recv_buf = (char *) rt_calloc(1, bfsz);
+    if (recv_buf == RT_NULL)
+    {
+        LOG_E("no memory for ml305 device(%s) URC receive buffer (%d).", device->name, bfsz);
+        temp_size = (size-(rt_strstr(data,":")+1-data)-2);
+        /* read and clean the coming data */
+        while (temp_size < bfsz)
+        {
+            if (bfsz - temp_size > sizeof(temp))
+            {
+                at_client_obj_recv(client, temp, sizeof(temp), timeout);
+            }
+            else
+            {
+                at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
+            }
+            temp_size += sizeof(temp);
+        }
+        return;
+    }
+
+    /* sync receive data */
+    if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
+    {
+        LOG_E("ml305 device(%s) receive size(%d) data failed.", device->name, bfsz);
+        rt_free(recv_buf);
+        return;
+    }
+
+    /* get AT socket object by device socket descriptor */
+    socket = &(device->sockets[device_socket]);
+
+    /* notice the receive buffer and buffer size */
+    if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
+    {
+        at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
+    }
+}
+static void urc_state_func(struct at_client *client, const char *data, rt_size_t size)
+{
+    RT_ASSERT(data);
+    LOG_I("URC state data : %.*s", size, data);
+}
+static void urc_func(struct at_client *client, const char *data, rt_size_t size)
+{
+    RT_ASSERT(data);
+    LOG_I("URC data : %.*s", size, data);
+}
+static void urc_mipurc_func(struct at_client *client, const char *data, rt_size_t size)
+{
+    RT_ASSERT(data && size);
+    
+    switch(*(data + 10))
+    {
+    case 's' : urc_state_func(client, data, size); break;   //+MIPURC: ”state”,<connect_id>,<connect_state>
+    case 'r' : urc_recv_func(client, data, size); break;    //+MIPURC: “recv”,<connect_id>,<recv_length>
+    default  : urc_func(client, data, size);      break;
+    }
+}
+
+/* ML305 device URC table for the socket data */
+static const struct at_urc urc_table[] =
+{
+    {"",            ",CONNECT OK\r\n",     urc_connect_func},
+    {"",            ",CONNECT FAIL\r\n",   urc_connect_func},
+    {"",            ",SEND FAIL\r\n",      urc_send_func},
+    {"",            ",SEND OK\r\n",        urc_send_func},
+    {"",            ",CLOSE OK\r\n",       urc_close_func},
+    {"",            ",CLOSED\r\n",         urc_close_func},
+    {"+MIPURC:",    "\r\n",                urc_mipurc_func},
+};
+
+static struct at_socket_ops ml305_socket_ops =
+{
+    ml305_socket_connect,
+    ml305_socket_close,
+    ml305_socket_send,
+    ml305_domain_resolve,
+    ml305_socket_set_event_cb,
+#if defined(AT_SW_VERSION_NUM) && AT_SW_VERSION_NUM > 0x10300
+    RT_NULL,
+#endif
+};
+
+int ml305_socket_init(struct at_device *device)
+{
+    RT_ASSERT(device);
+
+    /* register URC data execution function  */
+    at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
+
+    return RT_EOK;
+}
+
+int ml305_socket_class_register(struct at_device_class *class)
+{
+    RT_ASSERT(class);
+
+    class->socket_num = AT_DEVICE_ML305_SOCKETS_NUM;
+    class->socket_ops = &ml305_socket_ops;
+
+    return RT_EOK;
+}
+
+#endif /* AT_DEVICE_USING_ML305 && AT_USING_SOCKET */

+ 1 - 0
inc/at_device.h

@@ -51,6 +51,7 @@ extern "C" {
 #define AT_DEVICE_CLASS_M5311          0X13U
 #define AT_DEVICE_CLASS_N720           0X14U
 #define AT_DEVICE_CLASS_L610           0X15U
+#define AT_DEVICE_CLASS_ML305          0X16U
 
 /* Options and Commands for AT device control opreations */
 #define AT_DEVICE_CTRL_POWER_ON        0x01L

+ 55 - 0
samples/at_sample_ml305.c

@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-12-16     Jonas        first version
+ */
+
+#include <at_device_ml305.h>
+
+#define LOG_TAG                         "at.sample.ml305"
+#include <at_log.h>
+
+#define ML305_SAMPLE_DEIVCE_NAME        "ml3050"
+
+#define ML305_SAMPLE_CLIENT_NAME        "uart3"
+
+#if !defined (ML305_SAMPLE_POWER_PIN)
+    #define ML305_SAMPLE_POWER_PIN      0x1C
+#endif
+
+#if !defined (ML305_SAMPLE_STATUS_PIN)
+    #define ML305_SAMPLE_STATUS_PIN     0x1E
+#endif
+
+#if !defined (ML305_SAMPLE_RECV_BUFF_LEN)
+    #define ML305_SAMPLE_RECV_BUFF_LEN  4096
+#endif
+
+#if !defined (AT_DEVICE_CLASS_ML305)
+    #define AT_DEVICE_CLASS_ML305       0xFFFFU
+#endif
+static struct at_device_ml305 ml3050 =
+{
+    ML305_SAMPLE_DEIVCE_NAME,
+    ML305_SAMPLE_CLIENT_NAME,
+
+    ML305_SAMPLE_POWER_PIN,
+    ML305_SAMPLE_STATUS_PIN,
+    ML305_SAMPLE_RECV_BUFF_LEN,
+};
+
+static int ml305_device_register(void)
+{
+    struct at_device_ml305 *ml305 = &ml3050;
+
+    return at_device_register(&(ml305->device),
+                              ml305->device_name,
+                              ml305->client_name,
+                              AT_DEVICE_CLASS_ML305,
+                              (void *) ml305);
+}
+INIT_APP_EXPORT(ml305_device_register);