net: tcp: Make the connect callback on success, not transmission
The net_context_connect() callback was being invoked synchronously
with the transmission of the SYN packet. That's not very useful, as
it doesn't tell the user anything they can't already figure out from
the return code. Move it to the receipt of the SYNACK instead, so the
app can know that it's time to start transmitting. This matches the
Unix semantics more closely, where connect(2) is a blocking call that
wakes up only when the connection is live.
Change-Id: I11e3cca8572d51bee215274e82667e0917587a0f
Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
diff --git a/include/net/net_context.h b/include/net/net_context.h
index 6188145..e052c59 100644
--- a/include/net/net_context.h
+++ b/include/net/net_context.h
@@ -112,6 +112,22 @@
int status,
void *user_data);
+/**
+ * @brief Connection callback.
+ *
+ * @details The connect callback is called after a connection is being
+ * established.
+ *
+ * @param context The context to use.
+ * @param status Status of the connection establishment. This is 0
+ * if the connection was established successfully, <0 if there was an
+ * error.
+ * @param user_data The user data given in net_context_connect() call.
+ */
+typedef void (*net_context_connect_cb_t)(struct net_context *context,
+ int status,
+ void *user_data);
+
struct net_tcp;
struct net_conn_handle;
@@ -144,6 +160,11 @@
*/
net_context_send_cb_t send_cb;
+ /** Connect callback to be called when a connection has been
+ * established.
+ */
+ net_context_connect_cb_t connect_cb;
+
/** User data.
*/
void *user_data;
@@ -440,22 +461,6 @@
int backlog);
/**
- * @brief Connection callback.
- *
- * @details The connect callback is called after a connection is being
- * established.
- *
- * @param context The context to use.
- * @param status Status of the connection establishment. This is 0
- * if the connection was established successfully, <0 if there was an
- * error.
- * @param user_data The user data given in net_context_connect() call.
- */
-typedef void (*net_context_connect_cb_t)(struct net_context *context,
- int status,
- void *user_data);
-
-/**
* @brief Create a network connection.
*
* @details The net_context_connect function creates a network
diff --git a/subsys/net/ip/net_context.c b/subsys/net/ip/net_context.c
index 3f2e875..b8f016b 100644
--- a/subsys/net/ip/net_context.c
+++ b/subsys/net/ip/net_context.c
@@ -927,6 +927,10 @@
net_tcp_change_state(context->tcp, NET_TCP_ESTABLISHED);
net_context_set_state(context, NET_CONTEXT_CONNECTED);
+ if (context->connect_cb) {
+ context->connect_cb(context, 0, context->user_data);
+ }
+
send_ack(context, raddr);
k_sem_give(&context->tcp->connect_wait);
@@ -1111,9 +1115,8 @@
send_syn(context, addr);
- if (cb) {
- cb(context, 0, user_data);
- }
+ context->connect_cb = cb;
+ context->user_data = user_data;
/* in tcp_synack_received() we give back this semaphore */
if (k_sem_take(&context->tcp->connect_wait, timeout)) {