Skip to content

Commit 87ef66c

Browse files
authored
Merge pull request ARMmbed#61 from linlingao/socket
Add socket implementation
2 parents 9438658 + f0325bf commit 87ef66c

File tree

4 files changed

+446
-122
lines changed

4 files changed

+446
-122
lines changed

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

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ CC3220SFInterface::CC3220SFInterface():
2424
_initialized(false),
2525
_started(false)
2626
{
27-
//memset(_ids, 0, sizeof(_ids));
2827
//memset(_cbs, 0, sizeof(_cbs));
2928
memset(_ssid, 0, sizeof(_ssid));
3029
memset(_pass, 0, sizeof(_pass));
31-
//memset(_local_ports, 0, sizeof(_local_ports));
3230
_security = NSAPI_SECURITY_UNKNOWN;
3331
_cc3200_simplelink.initialize();
3432
}
@@ -213,35 +211,72 @@ nsapi_connection_status_t CC3220SFInterface::get_connection_status() const
213211
return _cc3200_simplelink.get_connection_status();
214212
}
215213

216-
#if 0
217-
WiFiInterface *WiFiInterface::get_default_instance() {
218-
static CC3220SF_WiFiInterface cc3220_wifi;
219-
return &cc3220_wifi;
220-
}
221-
#endif
214+
struct cc3200_socket {
215+
int id;
216+
nsapi_protocol_t proto;
217+
bool connected;
218+
// SocketAddress addr;
219+
int keepalive; // TCP
220+
};
222221

223222
int CC3220SFInterface::socket_open(void **handle, nsapi_protocol_t proto)
224223
{
225-
return 0;
224+
if (handle)
225+
{
226+
*(int32_t*)handle = _cc3200_simplelink.open_socket(proto);
227+
if (*(int32_t*)handle >= 0)
228+
{
229+
return 0;
230+
}
231+
}
232+
return -1;
226233
}
234+
227235
int CC3220SFInterface::socket_close(void *handle)
228236
{
229-
return 0;
237+
return (_cc3200_simplelink.close_socket((uint32_t)handle));
238+
}
239+
240+
nsapi_error_t CC3220SFInterface::setsockopt(nsapi_socket_t handle, int level,
241+
int optname, const void *optval, unsigned optlen)
242+
{
243+
nsapi_error_t retcode = _cc3200_simplelink.setsockopt((uint32_t)handle, level, optname, optval, optlen);
244+
return retcode;
245+
}
246+
247+
nsapi_error_t CC3220SFInterface::getsockopt(nsapi_socket_t handle, int level, int optname,
248+
void *optval, unsigned *optlen)
249+
{
250+
nsapi_error_t retcode = _cc3200_simplelink.getsockopt((uint32_t)handle, level, optname, optval, optlen);
251+
return retcode;
230252
}
231253

232254
int CC3220SFInterface::socket_bind(void *handle, const SocketAddress &address)
233255
{
234-
return 0;
256+
bool ipv6 = false;
257+
nsapi_addr_t nsapi_address = address.get_addr();
258+
if (nsapi_address.version == NSAPI_IPv6)
259+
{
260+
ipv6 = true;
261+
}
262+
return _cc3200_simplelink.bind_socket((uint32_t)handle, ipv6, nsapi_address.bytes, address.get_port());
235263
}
236264

237265
int CC3220SFInterface::socket_listen(void *handle, int backlog)
238266
{
239267
return 0;
240268
}
241269

242-
int CC3220SFInterface::socket_connect(void *handle, const SocketAddress &address)
270+
int CC3220SFInterface::socket_connect(void *handle, const SocketAddress &addr)
243271
{
244-
return 0;
272+
bool ipv6 = false;
273+
nsapi_addr_t nsapi_address = addr.get_addr();
274+
if (nsapi_address.version == NSAPI_IPv6)
275+
{
276+
ipv6 = true;
277+
}
278+
279+
return _cc3200_simplelink.connect_socket((uint32_t)handle, ipv6, nsapi_address.bytes, addr.get_port());
245280
}
246281

247282
int CC3220SFInterface::socket_accept(void *handle, void **socket, SocketAddress *address)
@@ -251,22 +286,41 @@ int CC3220SFInterface::socket_accept(void *handle, void **socket, SocketAddress
251286

252287
int CC3220SFInterface::socket_send(void *handle, const void *data, unsigned size)
253288
{
254-
return 0;
289+
return _cc3200_simplelink.send((uint32_t)handle, data, size);
255290
}
256291

257292
int CC3220SFInterface::socket_recv(void *handle, void *data, unsigned size)
258293
{
259-
return 0;
294+
return _cc3200_simplelink.recv((uint32_t)handle, data, size);
260295
}
261296

262297
int CC3220SFInterface::socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size)
263298
{
264-
return 0;
299+
bool ipv6 = false;
300+
nsapi_addr_t nsapi_address = address.get_addr();
301+
if (nsapi_address.version == NSAPI_IPv6)
302+
{
303+
ipv6 = true;
304+
}
305+
return _cc3200_simplelink.sendto_socket((uint32_t)handle, ipv6, data, size, nsapi_address.bytes, address.get_port());
265306
}
266307

267308
int CC3220SFInterface::socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size)
268309
{
269-
return 0;
310+
bool ipv6 = false;
311+
if (address)
312+
{
313+
nsapi_addr_t nsapi_address = address->get_addr();
314+
if (nsapi_address.version == NSAPI_IPv6)
315+
{
316+
ipv6 = true;
317+
}
318+
return _cc3200_simplelink.recvfrom((uint32_t)handle, ipv6, buffer, size, nsapi_address.bytes, address->get_port());
319+
}
320+
else
321+
{
322+
return _cc3200_simplelink.recv((uint32_t)handle, buffer, size);
323+
}
270324
}
271325

272326
void CC3220SFInterface::socket_attach(void *handle, void (*callback)(void *), void *data)

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
#include "mbed.h"
2121
#include "cc3200_simplelink.h"
2222

23-
// TODO: check this
24-
#define CC3220SF_SOCKET_COUNT 5
25-
2623
/** TI (CC3220SF) interface class
2724
* Implementation of the NetworkStack for TI CC3200 Simplelink stack
2825
*/
@@ -143,7 +140,7 @@ class CC3220SFInterface: public NetworkStack, public WiFiInterface
143140
*/
144141
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, unsigned count);
145142

146-
#if 0
143+
147144
/** Translates a hostname to an IP address with specific version
148145
*
149146
* The hostname may be either a domain name or an IP address. If the
@@ -200,7 +197,6 @@ class CC3220SFInterface: public NetworkStack, public WiFiInterface
200197
*/
201198
virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, int optname,
202199
void *optval, unsigned *optlen);
203-
#endif
204200

205201
/** Register callback for status reporting
206202
*
@@ -332,28 +328,14 @@ class CC3220SFInterface: public NetworkStack, public WiFiInterface
332328
static const int CC3220SF_PASSPHRASE_MIN_LENGTH = 8; /* The shortest allowed passphrase */
333329

334330
CC3200_SIMPLELINK _cc3200_simplelink;
335-
//bool _ids[CC3220SF_SOCKET_COUNT];
336331
int _initialized;
337332
int _started;
338-
339333
char _ssid[CC3220SF_SSID_MAX_LENGTH + 1]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
340334
nsapi_security_t _security;
341335
uint8_t _channel;
342336
char _pass[CC3220SF_PASSPHRASE_MAX_LENGTH + 1];
343-
#if 0
344-
uint16_t _local_ports[CC3220SF_SOCKET_COUNT];
345337

346-
bool _disable_default_softap();
347-
void event();
348-
bool _get_firmware_ok();
349-
#endif
350338
nsapi_error_t _init(void);
351339
nsapi_error_t _startup(const int8_t wifi_mode);
352-
#if 0
353-
struct {
354-
void (*callback)(void *);
355-
void *data;
356-
} _cbs[CC3220SF_SOCKET_COUNT];
357-
#endif
358340
};
359341
#endif

0 commit comments

Comments
 (0)