osmo_io: Add osmo_iofd_notify_connected()

Don't call write_enable() in osmo_iofd_register(). This was used to
detect whether a socket is connected or not, but would always be
enabled, even on unconnected sockets. Instead make this behaviour
explicit by calling osmo_iofd_notify_connected().

Change-Id: Ieed10bc94c8aad821c0a8f7764db0e05c054c1e3
diff --git a/include/osmocom/core/osmo_io.h b/include/osmocom/core/osmo_io.h
index aac25a3..932b909 100644
--- a/include/osmocom/core/osmo_io.h
+++ b/include/osmocom/core/osmo_io.h
@@ -75,6 +75,8 @@
 int osmo_iofd_close(struct osmo_io_fd *iofd);
 void osmo_iofd_free(struct osmo_io_fd *iofd);
 
+void osmo_iofd_notify_connected(struct osmo_io_fd *iofd);
+
 int osmo_iofd_write_msgb(struct osmo_io_fd *iofd, struct msgb *msg);
 int osmo_iofd_sendto_msgb(struct osmo_io_fd *iofd, struct msgb *msg, int sendto_flags,
 			  const struct osmo_sockaddr *dest);
diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map
index d31392b..50be67c 100644
--- a/src/core/libosmocore.map
+++ b/src/core/libosmocore.map
@@ -273,6 +273,7 @@
 osmo_iofd_txqueue_len;
 osmo_iofd_unregister;
 osmo_iofd_uring_init;
+osmo_iofd_notify_connected;
 osmo_iofd_write_msgb;
 osmo_ip_str_type;
 osmo_isdnhdlc_decode;
diff --git a/src/core/osmo_io.c b/src/core/osmo_io.c
index 4cef142..fdb9e32 100644
--- a/src/core/osmo_io.c
+++ b/src/core/osmo_io.c
@@ -433,7 +433,9 @@
 
 	IOFD_FLAG_UNSET(iofd, IOFD_FLAG_CLOSED);
 	osmo_iofd_ops.read_enable(iofd);
-	osmo_iofd_ops.write_enable(iofd);
+
+	if (iofd->tx_queue.current_length > 0)
+		osmo_iofd_ops.write_enable(iofd);
 
 	return rc;
 }
@@ -603,4 +605,14 @@
 	iofd->io_ops = *ioops;
 }
 
+/*! Notify the user if/when the socket is connected
+ *  When the socket is connected the write_cb will be called.
+ *  \param[in] iofd the file descriptor */
+void osmo_iofd_notify_connected(struct osmo_io_fd *iofd)
+{
+	OSMO_ASSERT(iofd->mode == OSMO_IO_FD_MODE_READ_WRITE);
+	osmo_iofd_ops.write_enable(iofd);
+}
+
+
 #endif /* defined(__linux__) */
diff --git a/tests/osmo_io/osmo_io_test.c b/tests/osmo_io/osmo_io_test.c
index a42e6d0..cff594b 100644
--- a/tests/osmo_io/osmo_io_test.c
+++ b/tests/osmo_io/osmo_io_test.c
@@ -86,6 +86,8 @@
 	osmo_iofd_register(iofd1, fds[0]);
 	iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_READ_WRITE, &ioops_conn_read_write, NULL);
 	osmo_iofd_register(iofd2, fds[1]);
+	// Explicitly check if ep1 is connected through write_cb
+	osmo_iofd_notify_connected(iofd1);
 
 	/* Allow enough cycles to handle the messages */
 	for (int i = 0; i < 128; i++)
diff --git a/tests/osmo_io/osmo_io_test.ok b/tests/osmo_io/osmo_io_test.ok
index 82a6f38..6527c19 100644
--- a/tests/osmo_io/osmo_io_test.ok
+++ b/tests/osmo_io/osmo_io_test.ok
@@ -1,10 +1,6 @@
 Running test_connected
 ep1: write() returned rc=0
-ep2: write() returned rc=0
 ep1: write() returned rc=16
-ep2: write() returned rc=16
-ep1: read() msg with len=16
-01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 
 ep2: read() msg with len=16
 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 
 Running test_unconnected