[Bugfix][T108-GSW][bug-view-1974] if called too early,  mbtk rtp init will fail

    Only Configure: No
    Affected branch: unknown
    Affected module: rtp
    Is it affected on both ZXIC and ASR: only ASR
    Self-test: yes
    Doc Update: no

Change-Id: Ib260b67858fb457f7e6fb69d846f47f1bfa0dbb9
diff --git a/mbtk/libmbtk_lib/rtp/mbtk_rtp.c b/mbtk/libmbtk_lib/rtp/mbtk_rtp.c
index 58d1077..26450f2 100755
--- a/mbtk/libmbtk_lib/rtp/mbtk_rtp.c
+++ b/mbtk/libmbtk_lib/rtp/mbtk_rtp.c
@@ -32,13 +32,18 @@
 
 static int rtp_cli_fd = -1;
 
+#define RTP_INIT_CHECK() \
+   if(rtp_cli_fd < 0) { \
+        LOGW("RTP client not inited."); \
+        if(mbtk_rtp_init()!=0) \
+        { \
+            LOGE("RTP init failed"); \
+            return -2; \
+        } \
+    } 
+
 static char* rtp_cmd_exec(const char* cmd, char *rsp, int rsp_len)
 {
-    if(rtp_cli_fd < 0) {
-        LOGW("RTP client not inited.");
-        return NULL;
-    }
-
     int len = write(rtp_cli_fd, cmd, strlen(cmd));
     if(len != strlen(cmd)) {
         LOGE("write() fail(%d) : %d/%d", errno, len, strlen(cmd));
@@ -63,6 +68,32 @@
     }
 }
 
+/*hq modify at 20250725 begin*/
+int mbtk_rtp_connect()
+{
+    struct sockaddr_un cli_addr;
+    memset(&cli_addr, 0, sizeof(cli_addr));
+    cli_addr.sun_family = AF_LOCAL;
+    strcpy(cli_addr.sun_path, RTP_IPC_SOCK_PATH);
+
+    int cnt=0;
+    while(cnt<40) // max 20 s
+    {
+        if(connect(rtp_cli_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
+        {
+            LOGE("connect() cnt %d fail[%d].",cnt,errno);           
+        }
+        else
+        {    
+            return 0;
+        }
+        usleep(500 * 1000);//500ms
+        cnt++;
+    }
+    return -1;
+}
+/*hq modify at 20250725 end*/
+
 
 int mbtk_rtp_init()
 {
@@ -77,14 +108,9 @@
         LOGE("socket() fail[%d].", errno);
         goto error;
     }
-
-    struct sockaddr_un cli_addr;
-    memset(&cli_addr, 0, sizeof(cli_addr));
-    cli_addr.sun_family = AF_LOCAL;
-    strcpy(cli_addr.sun_path, RTP_IPC_SOCK_PATH);
-    if(connect(rtp_cli_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
+    
+    if(mbtk_rtp_connect())  // hq modify at 20250725
     {
-        LOGE("connect() fail[%d].", errno);
         goto error;
     }
 
@@ -113,10 +139,7 @@
 
 int mbtk_rtp_enable(bool enable)
 {
-    if(rtp_cli_fd < 0) {
-        LOGW("RTP client not inited.");
-        return -1;
-    }
+    RTP_INIT_CHECK();
 
     // gnss_init:x
     char cmd[100] = {0};
@@ -138,11 +161,8 @@
 
 int mbtk_rtp_volume_set(int volume)
 {
-    if(rtp_cli_fd < 0) {
-        LOGW("RTP client not inited.");
-        return -1;
-    }
-
+    RTP_INIT_CHECK();
+       
     // gnss_init:x
     char cmd[100] = {0};
     char rsp[100] = {0};
@@ -163,16 +183,13 @@
 
 int mbtk_rtp_remote_ip_set(const char *ipv4)
 {
-    if(rtp_cli_fd < 0) {
-        LOGW("RTP client not inited.");
-        return -1;
-    }
-
     uint32 IPAddr;
     if(str_empty(ipv4) || inet_pton(AF_INET, ipv4, &IPAddr) < 0) {
         LOGE("inet_pton() fail.");
         return -1;
     }
+  
+    RTP_INIT_CHECK();
 
     // gnss_init:x
     char cmd[100] = {0};
@@ -194,16 +211,13 @@
 
 int mbtk_rtp_vlan_set(const char *vlan)
 {
-    if(rtp_cli_fd < 0) {
-        LOGW("RTP client not inited.");
-        return -1;
-    }
-
     if(str_empty(vlan)) {
         LOGE("vlan is empty.");
         return -1;
     }
 
+    RTP_INIT_CHECK();
+
     char cmd[100] = {0};
     char rsp[100] = {0};
     snprintf(cmd, sizeof(cmd), "vlan %s", vlan); // vlan <dev>
@@ -224,10 +238,7 @@
 
 int mbtk_rtp_server_port_set(int port)
 {
-    if(rtp_cli_fd < 0) {
-        LOGW("RTP client not inited.");
-        return -1;
-    }
+    RTP_INIT_CHECK();
 
 #if 0
     if(53248 != port) {
@@ -256,10 +267,7 @@
 
 int mbtk_rtp_client_port_set(int port)
 {
-    if(rtp_cli_fd < 0) {
-        LOGW("RTP client not inited.");
-        return -1;
-    }
+    RTP_INIT_CHECK();
 
     // gnss_init:x
     char cmd[100] = {0};
@@ -281,16 +289,13 @@
 
 int mbtk_rtp_sample_rate_set(int sample_rate)
 {
-    if(rtp_cli_fd < 0) {
-        LOGW("RTP client not inited.");
-        return -1;
-    }
-
     if(sample_rate != 8000 && sample_rate != 16000) {
         LOGE("Only support 8000/16000.");
         return -1;
     }
 
+    RTP_INIT_CHECK();
+
     // gnss_init:x
     char cmd[100] = {0};
     char rsp[100] = {0};
@@ -311,16 +316,13 @@
 
 int mbtk_rtp_channel_set(int channel)
 {
-    if(rtp_cli_fd < 0) {
-        LOGW("RTP client not inited.");
-        return -1;
-    }
-
     if(channel != 1) {
         LOGE("Only support 1 channel.");
         return -1;
     }
 
+    RTP_INIT_CHECK();
+
     // gnss_init:x
     char cmd[100] = {0};
     char rsp[100] = {0};