client/player: Add transport.receive command
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Mon, 25 Apr 2022 23:58:54 +0000 (16:58 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:54 +0000 (14:55 +0530)
This adds transport.receive command:

Get/Set file to receive
Usage:
 receive [filename]

Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
client/player.c

index d658cbd..02db954 100644 (file)
@@ -80,10 +80,13 @@ static GList *transports = NULL;
 struct transport {
        int sk;
        int mtu[2];
+       char *filename;
+       int fd;
        struct io *io;
        uint32_t seq;
 } transport = {
        .sk = -1,
+       .fd = -1,
 };
 
 static void endpoint_unregister(void *data)
@@ -2218,7 +2221,7 @@ static bool transport_disconnected(struct io *io, void *user_data)
 static bool transport_recv(struct io *io, void *user_data)
 {
        uint8_t buf[1024];
-       int ret;
+       int ret, len;
 
        ret = read(io_get_fd(io), buf, sizeof(buf));
        if (ret < 0) {
@@ -2231,6 +2234,13 @@ static bool transport_recv(struct io *io, void *user_data)
 
        transport.seq++;
 
+       if (transport.fd) {
+               len = write(transport.fd, buf, ret);
+               if (len < 0)
+                       bt_shell_printf("Unable to write: %s (%d)",
+                                               strerror(errno), -errno);
+       }
+
        return true;
 }
 
@@ -2450,13 +2460,17 @@ static void cmd_release_transport(int argc, char *argv[])
        return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
-static int open_file(const char *filename)
+static int open_file(const char *filename, int flags)
 {
        int fd = -1;
 
        bt_shell_printf("Opening %s ...\n", filename);
 
-       fd = open(filename, O_RDONLY);
+       if (flags & O_CREAT)
+               fd = open(filename, flags, 0755);
+       else
+               fd = open(filename, flags);
+
        if (fd <= 0)
                bt_shell_printf("Can't open file %s: %s\n", filename,
                                                strerror(errno));
@@ -2513,7 +2527,7 @@ static void cmd_send_transport(int argc, char *argv[])
                return bt_shell_noninteractive_quit(EXIT_FAILURE);
        }
 
-       fd = open_file(argv[1]);
+       fd = open_file(argv[1], O_RDONLY);
 
        bt_shell_printf("Sending ...\n");
        err = transport_send(fd);
@@ -2526,6 +2540,38 @@ static void cmd_send_transport(int argc, char *argv[])
        return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
+static void transport_close(void)
+{
+       if (transport.fd < 0)
+               return;
+
+       close(transport.fd);
+       transport.fd = -1;
+
+       free(transport.filename);
+       transport.filename = NULL;
+}
+
+static void cmd_receive_transport(int argc, char *argv[])
+{
+       if (argc == 1) {
+               bt_shell_printf("Filename: %s\n", transport.filename);
+               return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+       }
+
+       transport_close();
+
+       transport.fd = open_file(argv[1], O_RDWR | O_CREAT);
+       if (transport.fd < 0)
+               return bt_shell_noninteractive_quit(EXIT_FAILURE);
+
+       transport.filename = strdup(argv[1]);
+
+       bt_shell_printf("Filename: %s\n", transport.filename);
+
+       return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
 static void volume_callback(const DBusError *error, void *user_data)
 {
        if (dbus_error_is_set(error)) {
@@ -2588,6 +2634,8 @@ static const struct bt_shell_menu transport_menu = {
                                                transport_generator },
        { "send",        "<filename>",  cmd_send_transport,
                                                "Send contents of a file" },
+       { "receive",     "[filename]",  cmd_receive_transport,
+                                               "Get/Set file to receive" },
        { "volume",      "<transport> [value]", cmd_volume_transport,
                                                "Get/Set transport volume",
                                                transport_generator },
@@ -2616,4 +2664,5 @@ void player_add_submenu(void)
 void player_remove_submenu(void)
 {
        g_dbus_client_unref(client);
+       transport_close();
 }