net: tcp: move accept_cb from net_context to net_tcp

net_context is used for more than just TCP contexts.  However,
the accept_cb field is only used for TCP.  Let's move it from
the generic net_context structure to the TCP specific net_tcp
structure.

Change-Id: If923c7aba1355cf5f91c07a7e7e469d385c7c365
Signed-off-by: Michael Scott <michael.scott@linaro.org>
This commit is contained in:
Michael Scott 2017-01-19 14:53:41 -08:00 committed by Jukka Rissanen
parent 34b67374de
commit eb9055c019
4 changed files with 31 additions and 25 deletions

View File

@ -106,11 +106,11 @@ typedef void (*net_context_send_cb_t)(struct net_context *context,
* @param status The status code, 0 on success, < 0 otherwise
* @param user_data The user data given in net_context_accept() call.
*/
typedef void (*net_context_accept_cb_t)(struct net_context *new_context,
struct sockaddr *addr,
socklen_t addrlen,
int status,
void *user_data);
typedef void (*net_tcp_accept_cb_t)(struct net_context *new_context,
struct sockaddr *addr,
socklen_t addrlen,
int status,
void *user_data);
struct net_tcp;
@ -164,11 +164,6 @@ struct net_context {
#if defined(CONFIG_NET_TCP)
/** TCP connection information */
struct net_tcp *tcp;
/** Accept callback to be called when the connection has been
* established.
*/
net_context_accept_cb_t accept_cb;
#endif /* CONFIG_NET_TCP */
};
@ -516,7 +511,7 @@ int net_context_connect(struct net_context *context,
* @return 0 if ok, < 0 if error
*/
int net_context_accept(struct net_context *context,
net_context_accept_cb_t cb,
net_tcp_accept_cb_t cb,
int32_t timeout,
void *user_data);

View File

@ -1260,7 +1260,7 @@ static enum net_verdict tcp_syn_rcvd(struct net_conn *conn,
net_tcp_print_recv_info("ACK", buf, NET_TCP_BUF(buf)->src_port);
if (!context->accept_cb) {
if (!context->tcp->accept_cb) {
NET_DBG("No accept callback, connection reset.");
goto reset;
}
@ -1376,11 +1376,11 @@ static enum net_verdict tcp_syn_rcvd(struct net_conn *conn,
new_context->user_data = context->user_data;
context->user_data = NULL;
context->accept_cb(new_context,
&new_context->remote,
addrlen,
0,
new_context->user_data);
context->tcp->accept_cb(new_context,
&new_context->remote,
addrlen,
0,
new_context->user_data);
}
return NET_DROP;
@ -1397,7 +1397,7 @@ reset:
#endif /* CONFIG_NET_TCP */
int net_context_accept(struct net_context *context,
net_context_accept_cb_t cb,
net_tcp_accept_cb_t cb,
int32_t timeout,
void *user_data)
{
@ -1486,7 +1486,11 @@ int net_context_accept(struct net_context *context,
}
context->user_data = user_data;
context->accept_cb = cb;
/* accept callback is only valid for TCP contexts */
if (net_context_get_ip_proto(context) == IPPROTO_TCP) {
context->tcp->accept_cb = cb;
}
#endif /* CONFIG_NET_TCP */
return 0;

View File

@ -149,6 +149,8 @@ struct net_tcp *net_tcp_alloc(struct net_context *context)
tcp_context[i].send_seq = init_isn();
tcp_context[i].recv_max_ack = tcp_context[i].send_seq + 1u;
tcp_context[i].accept_cb = NULL;
k_timer_init(&tcp_context[i].retry_timer, tcp_retry_expired, NULL);
return &tcp_context[i];
@ -810,12 +812,12 @@ void net_tcp_change_state(struct net_tcp *tcp,
tcp->context->conn_handler = NULL;
}
if (tcp->context->accept_cb) {
tcp->context->accept_cb(tcp->context,
&tcp->context->remote,
sizeof(struct sockaddr),
-ENETRESET,
tcp->context->user_data);
if (tcp->accept_cb) {
tcp->accept_cb(tcp->context,
&tcp->context->remote,
sizeof(struct sockaddr),
-ENETRESET,
tcp->context->user_data);
}
}

View File

@ -133,6 +133,11 @@ struct net_tcp {
uint32_t state : 4;
/** Remaining bits in this uint32_t */
uint32_t _padding : 15;
/** Accept callback to be called when the connection has been
* established.
*/
net_tcp_accept_cb_t accept_cb;
};
static inline bool net_tcp_is_used(struct net_tcp *tcp)