nuttx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jerpe...@apache.org
Subject [incubator-nuttx] 02/05: drivers: wireless: Fix to bind() with port=0 in gs2200m.c
Date Thu, 14 May 2020 12:18:00 GMT
This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 3ead63c353f37791d36a37d82db9640f1061bcf8
Author: Masayuki Ishikawa <masayuki.ishikawa@gmail.com>
AuthorDate: Thu May 14 11:26:55 2020 +0900

    drivers: wireless: Fix to bind() with port=0 in gs2200m.c
    
    Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
---
 drivers/wireless/gs2200m.c | 44 +++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)

diff --git a/drivers/wireless/gs2200m.c b/drivers/wireless/gs2200m.c
index 6ced941..1329854 100644
--- a/drivers/wireless/gs2200m.c
+++ b/drivers/wireless/gs2200m.c
@@ -100,6 +100,9 @@
 #define HAL_TIMEOUT  5000000  /* in us */
 #define WR_MAX_RETRY 100
 
+#define PORT_START   50000
+#define PORT_END     59999
+
 /****************************************************************************
  * Private Data Types
  ****************************************************************************/
@@ -2102,17 +2105,38 @@ static int gs2200m_ioctl_bind(FAR struct gs2200m_dev_s *dev,
                               FAR struct gs2200m_bind_msg *msg)
 {
   enum pkt_type_e type = TYPE_OK;
+  bool auto_assign = false;
+  char port_str[6];
+  uint16_t port;
   char cid = 'z';
   int ret = OK;
 
   wlinfo("+++ start: (cid=%c, port=%s) \n", msg->cid, msg->port);
 
+  port = (uint16_t)strtol(msg->port, NULL, 10);
+
+  if (0 == port)
+    {
+      auto_assign = true;
+      port = PORT_START;
+    }
+
+retry:
+
+  snprintf(port_str, sizeof(port_str), "%d", port);
+
   /* Start TCP/UDP server and retrieve cid */
 
-  type = gs2200m_start_server(dev, msg->port, msg->is_tcp, &cid);
+  type = gs2200m_start_server(dev, port_str, msg->is_tcp, &cid);
 
   if (type != TYPE_OK)
     {
+      if (auto_assign && (port < PORT_END))
+        {
+          port++;
+          goto retry;
+        }
+
       ret = -EINVAL;
       goto errout;
     }
@@ -2200,8 +2224,6 @@ static int gs2200m_ioctl_send(FAR struct gs2200m_dev_s *dev,
 {
   FAR struct gs2200m_bind_msg bmsg;
   enum pkt_type_e type;
-  bool assigned = false;
-  uint16_t s_port;
   int ret = OK;
 
   wlinfo("+++ start: (cid=%c) \n", msg->cid);
@@ -2214,20 +2236,12 @@ static int gs2200m_ioctl_send(FAR struct gs2200m_dev_s *dev,
 
   if (!msg->is_tcp && 'z' == msg->cid)
     {
-      memset(&bmsg, 0, sizeof(bmsg));
-
-      for (s_port = 50000; s_port < 60000; s_port++)
-        {
-          snprintf(bmsg.port, sizeof(bmsg.port), "%d", s_port);
+      /* NOTE: need to assign port automatically */
 
-          if (0 == gs2200m_ioctl_bind(dev, &bmsg))
-            {
-              assigned = true;
-              break;
-            }
-        }
+      memset(&bmsg, 0, sizeof(bmsg));
+      ret = gs2200m_ioctl_bind(dev, &bmsg);
+      ASSERT(0 == ret);
 
-      ASSERT(assigned);
       wlinfo("+++ cid is assigned for udp (cid=%c) \n", bmsg.cid);
       msg->cid = bmsg.cid;
     }


Mime
View raw message