Jelajahi Sumber

[bc28] support alloc socket dynamically

luhuadong 5 tahun lalu
induk
melakukan
d904b67968
2 mengubah file dengan 26 tambahan dan 35 penghapusan
  1. 15 0
      class/bc28/README.md
  2. 11 35
      class/bc28/at_socket_bc28.c

+ 15 - 0
class/bc28/README.md

@@ -0,0 +1,15 @@
+# BC28
+
+## 注意事项
+
+### 申请 socket
+
+- BC28 最多支持 7 个 socket,编号 0-6;
+- 如果 BC28 使用支持 MQTT 或 CoAP 协议的固件,则相关服务会占用部分 socket,因此最小可用的 socket 并不一定是 0;
+- 可以创建 1 个 UDP socket,多个 TCP socket;
+- 另外,执行 DNS 域名解析时也会占用 socket;
+
+### 连接 socket
+
+- R02 的基线版本固件才支持 `AT+QTCPIND` 查询 TCP 连接情况,因此目前采用保守的 30 秒延时来确保连接状态;
+

+ 11 - 35
class/bc28/at_socket_bc28.c

@@ -20,6 +20,7 @@
  * Change Logs:
  * Date           Author            Notes
  * 2020-02-13     luhuadong         first version
+ * 2020-07-19     luhuadong         support alloc socket
  */
 
 #include <stdio.h>
@@ -194,7 +195,7 @@ static int bc28_socket_close(struct at_socket *socket)
 {
     int result = RT_EOK;
     at_response_t resp = RT_NULL;
-    int device_socket = (int) socket->user_data + AT_DEVICE_BC28_MIN_SOCKET;
+    int device_socket = (int) socket->user_data;
     struct at_device *device = (struct at_device *) socket->device;
 
     resp = at_create_resp(64, 0, rt_tick_from_millisecond(3000));
@@ -223,16 +224,16 @@ static int bc28_socket_close(struct at_socket *socket)
  * create socket by AT commands.
  *
  * @param type connect socket type(tcp, udp)
- * @param port listen port (range: 0-65535), if 0 means get a random port
  *
  * @return >=0: create socket success, return the socket id (0-6)
  *          -1: send or exec AT commands error
  *          -5: no memory
  */
-static int bc28_socket_create(struct at_device *device, enum at_socket_type type, uint32_t port)
+static int bc28_socket_create(struct at_device *device, enum at_socket_type type)
 {
     const char *type_str = RT_NULL;
     uint32_t protocol = 0;
+    uint32_t port = 0; /* range: 0-65535, if 0 means get a random port */
     at_response_t resp = RT_NULL;
     int socket = -1, result = 0;
 
@@ -309,7 +310,7 @@ static int bc28_socket_connect(struct at_socket *socket, char *ip, int32_t port,
     uint32_t event = 0;
     at_response_t resp = RT_NULL;
     int result = 0, event_result = 0;
-    int device_socket = (int) socket->user_data + AT_DEVICE_BC28_MIN_SOCKET;
+    int device_socket = (int) socket->user_data;
     int return_socket = -1;
     struct at_device *device = (struct at_device *) socket->device;
 
@@ -328,32 +329,6 @@ static int bc28_socket_connect(struct at_socket *socket, char *ip, int32_t port,
         return -RT_ENOMEM;
     }
 
-    return_socket = bc28_socket_create(device, type, 0);
-
-    if (return_socket != device_socket)
-    {
-        LOG_E("socket not match (request %d, return %d).", device_socket, return_socket);
-
-        result = at_obj_exec_cmd(device->client, resp, "AT+NSOCL=%d", return_socket);
-        if (result < 0)
-        {
-            LOG_E("%s device close socket(%d) failed [%d].", device->name, return_socket, result);
-        }
-        else
-        {
-            LOG_D("%s device close socket(%d).", device->name, return_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, NULL, 0);
-            }
-        }
-        
-        result = -RT_ERROR;
-        goto __exit;
-    }
-
     /* if the protocol is not tcp, no need connect to server */
     if (type != AT_SOCKET_TCP)
     {
@@ -439,7 +414,7 @@ static int bc28_socket_send(struct at_socket *socket, const char *buff,
     int result = 0, 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 + AT_DEVICE_BC28_MIN_SOCKET;
+    int device_socket = (int) socket->user_data;
     struct at_device *device = (struct at_device *) socket->device;
     struct at_device_bc28 *bc28 = (struct at_device_bc28 *) device->user_data;
     rt_mutex_t lock = device->client->lock;
@@ -748,10 +723,10 @@ static void urc_close_func(struct at_client *client, const char *data, rt_size_t
 
     bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_FAIL));
     
-    if (device_socket - AT_DEVICE_BC28_MIN_SOCKET >= 0)
+    if (device_socket >= 0)
     {
         /* get at socket object by device socket descriptor */
-        socket = &(device->sockets[device_socket - AT_DEVICE_BC28_MIN_SOCKET]);
+        socket = &(device->sockets[device_socket]);
 
         /* notice the socket is disconnect by remote */
         if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
@@ -828,7 +803,7 @@ static void urc_recv_func(struct at_client *client, const char *data, rt_size_t
     rt_free(hex_buf);
 
     /* get at socket object by device socket descriptor */
-    socket = &(device->sockets[device_socket - AT_DEVICE_BC28_MIN_SOCKET]);
+    socket = &(device->sockets[device_socket]);
 
     /* notice the receive buffer and buffer size */
     if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
@@ -895,6 +870,7 @@ static const struct at_urc urc_table[] =
 
 static const struct at_socket_ops bc28_socket_ops =
 {
+    bc28_socket_create,
     bc28_socket_connect,
     bc28_socket_close,
     bc28_socket_send,
@@ -916,7 +892,7 @@ int bc28_socket_class_register(struct at_device_class *class)
 {
     RT_ASSERT(class);
 
-    class->socket_num = AT_DEVICE_BC28_SOCKETS_NUM - AT_DEVICE_BC28_MIN_SOCKET;
+    class->socket_num = AT_DEVICE_BC28_SOCKETS_NUM;
     class->socket_ops = &bc28_socket_ops;
 
     return RT_EOK;