24
24
25
25
#include "mbedtls/ssl.h"
26
26
27
- #define CLIENT_IP_ADDR { { { CLIENT_IPADDR0, CLIENT_IPADDR1, \
28
- CLIENT_IPADDR2, CLIENT_IPADDR3 } } }
29
-
30
- #define SERVER_IP_ADDR { { { SERVER_IPADDR0, SERVER_IPADDR1, \
31
- SERVER_IPADDR2, SERVER_IPADDR3 } } }
32
-
33
27
#define INET_FAMILY AF_INET
34
28
35
- static struct in_addr client_addr = CLIENT_IP_ADDR ;
36
- static struct in_addr server_addr = SERVER_IP_ADDR ;
29
+ #define TCP_BUF_CTR 5
30
+ #define TCP_BUF_SIZE 1024
31
+
32
+ NET_BUF_POOL_DEFINE (tcp_msg_pool , TCP_BUF_CTR , TCP_BUF_SIZE , 0 , NULL );
37
33
38
34
static void tcp_received (struct net_context * context ,
39
35
struct net_buf * buf , int status , void * user_data )
@@ -49,7 +45,6 @@ int tcp_tx(void *context, const unsigned char *buf, size_t size)
49
45
struct tcp_context * ctx = context ;
50
46
struct net_context * tcp_ctx ;
51
47
struct net_buf * send_buf ;
52
- struct sockaddr_in dst_addr ;
53
48
int rc , len ;
54
49
55
50
tcp_ctx = ctx -> net_ctx ;
@@ -66,16 +61,9 @@ int tcp_tx(void *context, const unsigned char *buf, size_t size)
66
61
return - EIO ;
67
62
}
68
63
69
- net_ipaddr_copy (& dst_addr .sin_addr , & server_addr );
70
- dst_addr .sin_family = AF_INET ;
71
- dst_addr .sin_port = htons (SERVER_PORT );
72
-
73
64
len = net_buf_frags_len (send_buf );
74
- k_sleep (TCP_TX_TIMEOUT );
75
65
76
- rc = net_context_sendto (send_buf , (struct sockaddr * )& dst_addr ,
77
- sizeof (struct sockaddr_in ),
78
- NULL , K_FOREVER , NULL , NULL );
66
+ rc = net_context_send (send_buf , NULL , ctx -> timeout , NULL , NULL );
79
67
80
68
if (rc < 0 ) {
81
69
printk ("Cannot send IPv4 data to peer (%d)\n" , rc );
@@ -88,74 +76,146 @@ int tcp_tx(void *context, const unsigned char *buf, size_t size)
88
76
89
77
int tcp_rx (void * context , unsigned char * buf , size_t size )
90
78
{
79
+ struct net_buf * data = NULL ;
91
80
struct tcp_context * ctx = context ;
81
+ int rc ;
92
82
uint8_t * ptr ;
93
- int pos = 0 ;
94
- int len ;
95
- struct net_buf * rx_buf ;
96
- int rc , read_bytes ;
83
+ int read_bytes ;
84
+ uint16_t offset ;
97
85
98
86
rc = net_context_recv (ctx -> net_ctx , tcp_received , K_FOREVER , ctx );
99
87
if (rc != 0 ) {
88
+ printk ("net_context_recv failed with code:%d\n" , rc );
100
89
return 0 ;
101
90
}
102
91
read_bytes = net_nbuf_appdatalen (ctx -> rx_nbuf );
103
92
104
- ptr = net_nbuf_appdata (ctx -> rx_nbuf );
105
- rx_buf = ctx -> rx_nbuf -> frags ;
106
- len = rx_buf -> len - (ptr - rx_buf -> data );
107
-
108
- while (rx_buf ) {
109
- memcpy (buf + pos , ptr , len );
110
- pos += len ;
111
- rx_buf = rx_buf -> frags ;
112
- if (!rx_buf ) {
113
- break ;
114
- }
115
- ptr = rx_buf -> data ;
116
- len = rx_buf -> len ;
117
- }
118
-
119
- if (read_bytes != pos ) {
93
+ data = net_buf_alloc (& tcp_msg_pool , APP_SLEEP_MSECS );
94
+ if (data == NULL ) {
95
+ net_nbuf_unref (ctx -> rx_nbuf );
96
+ printk ("net_buf_alloc failed\n" );
120
97
return - EIO ;
121
98
}
122
- rc = read_bytes ;
99
+
100
+ offset = net_buf_frags_len (ctx -> rx_nbuf ) - read_bytes ;
101
+ rc = net_nbuf_linear_copy (data , ctx -> rx_nbuf , offset , read_bytes );
102
+ ptr = net_nbuf_appdata (data );
103
+ memcpy (buf , ptr , read_bytes );
104
+
123
105
net_nbuf_unref (ctx -> rx_nbuf );
124
- ctx -> remaining = 0 ;
125
- ctx -> rx_nbuf = NULL ;
106
+ net_nbuf_unref (data );
107
+
108
+ return read_bytes ;
109
+ }
110
+
111
+ static int set_addr (struct sockaddr * sock_addr , const char * addr ,
112
+ uint16_t server_port )
113
+ {
114
+ void * ptr = NULL ;
115
+ int rc ;
116
+
117
+ #ifdef CONFIG_NET_IPV6
118
+ net_sin6 (sock_addr )-> sin6_port = htons (server_port );
119
+ sock_addr -> family = AF_INET6 ;
120
+ ptr = & (net_sin6 (sock_addr )-> sin6_addr );
121
+ rc = net_addr_pton (AF_INET6 , addr , ptr );
122
+ #else
123
+ net_sin (sock_addr )-> sin_port = htons (server_port );
124
+ sock_addr -> family = AF_INET ;
125
+ ptr = & (net_sin (sock_addr )-> sin_addr );
126
+ rc = net_addr_pton (AF_INET , addr , ptr );
127
+ #endif
128
+
129
+ if (rc ) {
130
+ printk ("Invalid IP address: %s\n" , addr );
131
+ }
132
+
133
+ return rc ;
134
+ }
135
+
136
+ static int if_addr_add (struct sockaddr * local_sock )
137
+ {
138
+ void * p = NULL ;
139
+
140
+ #ifdef CONFIG_NET_IPV6
141
+ p = net_if_ipv6_addr_add (net_if_get_default (),
142
+ & net_sin6 (local_sock )-> sin6_addr ,
143
+ NET_ADDR_MANUAL , 0 );
144
+ #else
145
+ p = net_if_ipv4_addr_add (net_if_get_default (),
146
+ & net_sin (local_sock )-> sin_addr ,
147
+ NET_ADDR_MANUAL , 0 );
148
+ #endif
149
+
150
+ if (p ) {
151
+ return 0 ;
152
+ }
153
+
154
+ return - EINVAL ;
155
+ }
156
+
157
+ int tcp_set_local_addr (struct tcp_context * ctx , const char * local_addr )
158
+ {
159
+ int rc ;
160
+
161
+ rc = set_addr (& ctx -> local_sock , local_addr , 0 );
162
+ if (rc ) {
163
+ printk ("set_addr (local) error\n" );
164
+ goto lb_exit ;
165
+ }
166
+
167
+ rc = if_addr_add (& ctx -> local_sock );
168
+ if (rc ) {
169
+ printk ("if_addr_add error\n" );
170
+ }
171
+
172
+ lb_exit :
126
173
return rc ;
127
174
}
128
175
129
- int tcp_init (struct tcp_context * ctx )
176
+ int tcp_init (struct tcp_context * ctx , const char * server_addr ,
177
+ uint16_t server_port )
130
178
{
131
- struct net_context * tcp_ctx = { 0 };
132
- struct sockaddr_in my_addr4 = { 0 };
179
+ struct sockaddr server_sock ;
133
180
int rc ;
134
181
135
- net_ipaddr_copy (& my_addr4 .sin_addr , & client_addr );
136
- my_addr4 .sin_family = AF_INET ;
137
- my_addr4 .sin_port = htons (CLIENT_PORT );
182
+ #ifdef CONFIG_NET_IPV6
183
+ socklen_t addr_len = sizeof (struct sockaddr_in6 );
184
+ sa_family_t family = AF_INET6 ;
185
+ #else
186
+ socklen_t addr_len = sizeof (struct sockaddr_in );
187
+ sa_family_t family = AF_INET ;
188
+ #endif
138
189
139
- rc = net_context_get (AF_INET , SOCK_DGRAM , IPPROTO_TCP , & tcp_ctx );
140
- if (rc < 0 ) {
141
- printk ("Cannot get network context for IPv4 TCP (%d)" , rc );
142
- return - EIO ;
190
+ rc = net_context_get (family , SOCK_STREAM , IPPROTO_TCP , & ctx -> net_ctx );
191
+ if (rc ) {
192
+ printk ("net_context_get error\n" );
193
+ return rc ;
143
194
}
144
195
145
- rc = net_context_bind (tcp_ctx , (struct sockaddr * )& my_addr4 ,
146
- sizeof (struct sockaddr_in ));
147
- if (rc < 0 ) {
148
- printk ("Cannot bind IPv4 TCP port %d (%d)" , CLIENT_PORT , rc );
149
- goto error ;
196
+ rc = net_context_bind (ctx -> net_ctx , & ctx -> local_sock , addr_len );
197
+ if (rc ) {
198
+ printk ("net_context_bind error\n" );
199
+ goto lb_exit ;
150
200
}
151
201
152
- ctx -> rx_nbuf = NULL ;
153
- ctx -> remaining = 0 ;
154
- ctx -> net_ctx = tcp_ctx ;
202
+ rc = set_addr (& server_sock , server_addr , server_port );
203
+ if (rc ) {
204
+ printk ("set_addr (server) error\n" );
205
+ goto lb_exit ;
206
+ }
207
+
208
+ rc = net_context_connect (ctx -> net_ctx , & server_sock , addr_len , NULL ,
209
+ ctx -> timeout , NULL );
210
+ if (rc ) {
211
+ printk ("net_context_connect error\n" );
212
+ goto lb_exit ;
213
+ }
155
214
156
215
return 0 ;
157
216
158
- error :
159
- net_context_put (tcp_ctx );
160
- return - EINVAL ;
217
+ lb_exit :
218
+ net_context_put (ctx -> net_ctx );
219
+
220
+ return rc ;
161
221
}
0 commit comments