interface: Disconnect interfaces properly 08/291108/2 accepted/tizen/unified/20230411.161308
authorDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 10 Apr 2023 04:41:22 +0000 (13:41 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 10 Apr 2023 06:28:04 +0000 (15:28 +0900)
Some interfaces can be connected automatically without host runs lthor
due to the characteristics of connection such as usb. So, even the
network interface is used for transmitting image, usb connection
thread may wait for handshake. Previously in this case, when flashing
via network is not completed successfully, usb cannot be
re-initialized since the previous usb connection is not closed
properly. Thus, this patch makes sure all interfaces are disconnected
when the flash-manager is terminated.

Change-Id: I019e75b0bb774c292a2d7c142c0d00c7942322e1
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
src/interface.c
src/interface.h

index 778f73a..581a8e1 100644 (file)
@@ -92,12 +92,34 @@ int tfm_interface_set_private(struct tfm_interface_context *ictx,
        return 0;
 }
 
+static void _interface_disconnect(struct tfm_interface *intf)
+{
+       if (!intf)
+               return;
+
+       if (intf->ops && intf->ops->disconnect) {
+               intf->ops->disconnect(intf);
+               intf->ready = 0;
+               fprintf(stdout, "interface %s is disconnected successfully.\n", intf->name);
+       }
+}
+
+static void cleanup_connection(void *ptr)
+{
+       struct tfm_interface *intf = ptr;
+
+       if (intf->ready)
+               _interface_disconnect(intf);
+}
+
 static void *connect_thread_main(void *ptr)
 {
        struct tfm_interface *intf = ptr;
        struct tfm_interface_context *ictx = intf->ctx;
        int ret;
 
+       pthread_cleanup_push(cleanup_connection, intf);
+
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
        /* for checking which interface is connected */
@@ -112,6 +134,7 @@ static void *connect_thread_main(void *ptr)
                return NULL;
        }
 
+       intf->ready = 1;
        fprintf(stdout, "interface %s is ready.\n", intf->name);
 
        /*
@@ -121,7 +144,7 @@ static void *connect_thread_main(void *ptr)
        ret = thor_handshake(intf);
        if (ret < 0) {
                fprintf(stderr, "failed to handshake: %s\n", intf->name);
-               intf->ops->disconnect(intf);
+               _interface_disconnect(intf);
                return NULL;
        }
 
@@ -131,7 +154,7 @@ static void *connect_thread_main(void *ptr)
                fprintf(stderr,
                        "interface %s will be ignored due to %s\n",
                        intf->name, ictx->connected->name);
-               intf->ops->disconnect(intf);
+               _interface_disconnect(intf);
                return NULL;
        }
 
@@ -142,6 +165,15 @@ static void *connect_thread_main(void *ptr)
 
        pthread_cond_signal(&ictx->signal);
 
+       /*
+        * At this point, the corresponding interface is connected and done
+        * hand-shake successfully, and it will be used for transmission.
+        * Thus, to keep connection, pthread_cleanup_pop is called with
+        * zero parameter; cleanup function will not be invoked. The connected
+        * interface will be closed via tfm_interface_disconnect() in main.c
+        */
+       pthread_cleanup_pop(0);
+
        return NULL;
 }
 
@@ -174,11 +206,7 @@ struct tfm_interface *tfm_interface_connect(struct tfm_interface_context *ictx)
 
 void tfm_interface_disconnect(struct tfm_interface *intf)
 {
-       if (!intf)
-               return;
-
-       if (intf->ops && intf->ops->disconnect)
-               intf->ops->disconnect(intf);
+       _interface_disconnect(intf);
 
        intf->ctx->connected = NULL;
 }
@@ -233,6 +261,7 @@ struct tfm_interface_context *tfm_interface_init(void)
 
                intf->name = driver->name;
                intf->rxd = intf->txd = -1;
+               intf->ready = 0;
                intf->ops = &driver->ops;
                intf->priv = NULL;
                intf->tid = 0;
index ae2ca93..2061691 100644 (file)
@@ -51,6 +51,7 @@ struct tfm_interface {
        char *name;
        int rxd;
        int txd;
+       int ready;
        struct tfm_interface_ops *ops;
 
        void *priv;