Skip to content

Commit 77d210c

Browse files
authored
Merge pull request ARMmbed#64 from linlingao/socket_debug
Fixed socket handler bugs
2 parents 4d4bae9 + c7241c1 commit 77d210c

File tree

6 files changed

+350
-120
lines changed

6 files changed

+350
-120
lines changed

targets/TARGET_TI/TARGET_CC32XX/TARGET_CC3220SF/device/CC3220SF_WiFiInterface.cpp

Lines changed: 126 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,15 @@ nsapi_error_t CC3220SFInterface::set_credentials(const char *ssid, const char *p
133133
nsapi_error_t CC3220SFInterface::set_channel(uint8_t channel)
134134
{
135135
_channel = channel;
136-
_cc3200_simplelink.set_channel(channel);
137-
return NSAPI_ERROR_OK;
136+
int32_t status = _cc3200_simplelink.set_channel(channel);
137+
if (status == 0)
138+
{
139+
return NSAPI_ERROR_OK;
140+
}
141+
else
142+
{
143+
return NSAPI_ERROR_DEVICE_ERROR;
144+
}
138145
}
139146

140147
nsapi_error_t CC3220SFInterface::disconnect()
@@ -212,7 +219,7 @@ nsapi_connection_status_t CC3220SFInterface::get_connection_status() const
212219
}
213220

214221
struct cc3200_socket {
215-
int id;
222+
int sd;
216223
nsapi_protocol_t proto;
217224
bool connected;
218225
// SocketAddress addr;
@@ -221,11 +228,23 @@ struct cc3200_socket {
221228

222229
int CC3220SFInterface::socket_open(void **handle, nsapi_protocol_t proto)
223230
{
231+
int32_t sd;
224232
if (handle)
225233
{
226-
*(int32_t*)handle = _cc3200_simplelink.open_socket(proto);
227-
if (*(int32_t*)handle >= 0)
234+
struct cc3200_socket *socket = new struct cc3200_socket;
235+
if (!socket)
228236
{
237+
return NSAPI_ERROR_NO_SOCKET;
238+
}
239+
240+
sd = _cc3200_simplelink.open_socket(proto);
241+
if (sd >= 0) // socket open succeeded
242+
{
243+
socket->sd = sd;
244+
socket->proto = proto;
245+
socket->connected = false;
246+
socket->keepalive = 0;
247+
*handle = socket;
229248
return 0;
230249
}
231250
}
@@ -234,32 +253,93 @@ int CC3220SFInterface::socket_open(void **handle, nsapi_protocol_t proto)
234253

235254
int CC3220SFInterface::socket_close(void *handle)
236255
{
237-
return (_cc3200_simplelink.close_socket((uint32_t)handle));
256+
struct cc3200_socket *socket = (struct cc3200_socket *)handle;
257+
int err = 0;
258+
259+
if (!socket) {
260+
return NSAPI_ERROR_NO_SOCKET;
261+
}
262+
263+
if (socket->connected && !_cc3200_simplelink.close_socket(socket->sd))
264+
{
265+
err = NSAPI_ERROR_DEVICE_ERROR;
266+
}
267+
socket->connected = false;
268+
delete socket;
269+
return err;
270+
}
271+
272+
#if 0
273+
nsapi_error_t CC3220SFInterface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
274+
{
275+
nsapi_addr_t ip_address;
276+
ip_address.version = NSAPI_IPv4;
277+
if (_cc3200_simplelink.dns_lookup(name, (char*)ip_address.bytes, sizeof(ip_address.bytes), version) == NSAPI_ERROR_OK)
278+
{
279+
address->set_addr(ip_address);
280+
return NSAPI_ERROR_OK;
281+
}
282+
else
283+
{
284+
return NSAPI_ERROR_DNS_FAILURE;
285+
}
286+
}
287+
#endif
288+
289+
nsapi_error_t CC3220SFInterface::get_dns_server(int index, SocketAddress *address)
290+
{
291+
nsapi_addr_t dns_address;
292+
dns_address.version = NSAPI_IPv4;
293+
if (_cc3200_simplelink.getDNS(dns_address.bytes, sizeof(dns_address.bytes)) == NSAPI_ERROR_OK)
294+
{
295+
address->set_addr(dns_address);
296+
return NSAPI_ERROR_OK;
297+
}
298+
else
299+
{
300+
return NSAPI_ERROR_DNS_FAILURE;
301+
}
238302
}
239303

240304
nsapi_error_t CC3220SFInterface::setsockopt(nsapi_socket_t handle, int level,
241305
int optname, const void *optval, unsigned optlen)
242306
{
243-
nsapi_error_t retcode = _cc3200_simplelink.setsockopt((uint32_t)handle, level, optname, optval, optlen);
307+
struct cc3200_socket *socket = (struct cc3200_socket *)handle;
308+
if (!socket)
309+
{
310+
return NSAPI_ERROR_NO_SOCKET;
311+
}
312+
nsapi_error_t retcode = _cc3200_simplelink.setsockopt(socket->sd, level, optname, optval, optlen);
244313
return retcode;
245314
}
246315

247316
nsapi_error_t CC3220SFInterface::getsockopt(nsapi_socket_t handle, int level, int optname,
248317
void *optval, unsigned *optlen)
249318
{
250-
nsapi_error_t retcode = _cc3200_simplelink.getsockopt((uint32_t)handle, level, optname, optval, optlen);
319+
struct cc3200_socket *socket = (struct cc3200_socket *)handle;
320+
if (!socket)
321+
{
322+
return NSAPI_ERROR_NO_SOCKET;
323+
}
324+
nsapi_error_t retcode = _cc3200_simplelink.getsockopt(socket->sd, level, optname, optval, optlen);
251325
return retcode;
252326
}
253327

254328
int CC3220SFInterface::socket_bind(void *handle, const SocketAddress &address)
255329
{
330+
struct cc3200_socket *socket = (struct cc3200_socket *)handle;
331+
if (!socket)
332+
{
333+
return NSAPI_ERROR_NO_SOCKET;
334+
}
335+
256336
bool ipv6 = false;
257337
nsapi_addr_t nsapi_address = address.get_addr();
258338
if (nsapi_address.version == NSAPI_IPv6)
259339
{
260340
ipv6 = true;
261341
}
262-
return _cc3200_simplelink.bind_socket((uint32_t)handle, ipv6, nsapi_address.bytes, address.get_port());
342+
return _cc3200_simplelink.bind_socket(socket->sd, ipv6, nsapi_address.bytes, address.get_port());
263343
}
264344

265345
int CC3220SFInterface::socket_listen(void *handle, int backlog)
@@ -269,14 +349,25 @@ int CC3220SFInterface::socket_listen(void *handle, int backlog)
269349

270350
int CC3220SFInterface::socket_connect(void *handle, const SocketAddress &addr)
271351
{
352+
struct cc3200_socket *socket = (struct cc3200_socket *)handle;
353+
if (!socket)
354+
{
355+
return NSAPI_ERROR_NO_SOCKET;
356+
}
272357
bool ipv6 = false;
273358
nsapi_addr_t nsapi_address = addr.get_addr();
274359
if (nsapi_address.version == NSAPI_IPv6)
275360
{
276361
ipv6 = true;
277362
}
278363

279-
return _cc3200_simplelink.connect_socket((uint32_t)handle, ipv6, nsapi_address.bytes, addr.get_port());
364+
if (_cc3200_simplelink.connect_socket(socket->sd, ipv6, nsapi_address.bytes, addr.get_port()) == 0)
365+
{
366+
socket->connected= true;
367+
return 0;
368+
}
369+
printf("socket_connect failed\n");
370+
return NSAPI_ERROR_DEVICE_ERROR;
280371
}
281372

282373
int CC3220SFInterface::socket_accept(void *handle, void **socket, SocketAddress *address)
@@ -286,27 +377,47 @@ int CC3220SFInterface::socket_accept(void *handle, void **socket, SocketAddress
286377

287378
int CC3220SFInterface::socket_send(void *handle, const void *data, unsigned size)
288379
{
289-
return _cc3200_simplelink.send((uint32_t)handle, data, size);
380+
struct cc3200_socket *socket = (struct cc3200_socket *)handle;
381+
if (!socket)
382+
{
383+
return NSAPI_ERROR_NO_SOCKET;
384+
}
385+
return _cc3200_simplelink.send(socket->sd, data, size);
290386
}
291387

292388
int CC3220SFInterface::socket_recv(void *handle, void *data, unsigned size)
293389
{
294-
return _cc3200_simplelink.recv((uint32_t)handle, data, size);
390+
struct cc3200_socket *socket = (struct cc3200_socket *)handle;
391+
if (!socket)
392+
{
393+
return NSAPI_ERROR_NO_SOCKET;
394+
}
395+
return _cc3200_simplelink.recv(socket->sd, data, size);
295396
}
296397

297398
int CC3220SFInterface::socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size)
298399
{
400+
struct cc3200_socket *socket = (struct cc3200_socket *)handle;
401+
if (!socket)
402+
{
403+
return NSAPI_ERROR_NO_SOCKET;
404+
}
299405
bool ipv6 = false;
300406
nsapi_addr_t nsapi_address = address.get_addr();
301407
if (nsapi_address.version == NSAPI_IPv6)
302408
{
303409
ipv6 = true;
304410
}
305-
return _cc3200_simplelink.sendto_socket((uint32_t)handle, ipv6, data, size, nsapi_address.bytes, address.get_port());
411+
return _cc3200_simplelink.sendto_socket(socket->sd, ipv6, data, size, nsapi_address.bytes, address.get_port());
306412
}
307413

308414
int CC3220SFInterface::socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size)
309415
{
416+
struct cc3200_socket *socket = (struct cc3200_socket *)handle;
417+
if (!socket)
418+
{
419+
return NSAPI_ERROR_NO_SOCKET;
420+
}
310421
bool ipv6 = false;
311422
if (address)
312423
{
@@ -315,11 +426,11 @@ int CC3220SFInterface::socket_recvfrom(void *handle, SocketAddress *address, voi
315426
{
316427
ipv6 = true;
317428
}
318-
return _cc3200_simplelink.recvfrom((uint32_t)handle, ipv6, buffer, size, nsapi_address.bytes, address->get_port());
429+
return _cc3200_simplelink.recvfrom(socket->sd, ipv6, buffer, size, nsapi_address.bytes, address->get_port());
319430
}
320431
else
321432
{
322-
return _cc3200_simplelink.recv((uint32_t)handle, buffer, size);
433+
return 0;
323434
}
324435
}
325436

targets/TARGET_TI/TARGET_CC32XX/TARGET_CC3220SF/device/CC3220SF_WiFiInterface.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ class CC3220SFInterface: public NetworkStack, public WiFiInterface
132132
* If the network interface is set to non-blocking mode, scan will attempt to scan
133133
* for WiFi networks asynchronously and return NSAPI_ERROR_WOULD_BLOCK. If a callback
134134
* is attached, the callback will be called when the operation has completed.
135-
* @param ap Pointer to allocated array to store discovered AP
135+
* @param res Pointer to allocated array to store discovered AP
136136
* @param count Size of allocated @a res array, or 0 to only count available AP
137-
* @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
138137
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
139138
* see @a nsapi_error
140139
*/
@@ -156,6 +155,7 @@ class CC3220SFInterface: public NetworkStack, public WiFiInterface
156155
* @return 0 on success, negative error code on failure
157156
*/
158157
using NetworkInterface::gethostbyname;
158+
//virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version);
159159

160160
/** Add a domain name server to list of servers to query
161161
*
@@ -164,6 +164,8 @@ class CC3220SFInterface: public NetworkStack, public WiFiInterface
164164
*/
165165
using NetworkInterface::add_dns_server;
166166

167+
virtual nsapi_error_t get_dns_server(int index, SocketAddress *address);
168+
167169
/** Set socket options
168170
*
169171
* The setsockopt allow an application to pass stack-specific hints

0 commit comments

Comments
 (0)