Skip to content

Commit 796ac1e

Browse files
committed
Update server client ref to match client class.
2 parents dc62fa5 + 953b76a commit 796ac1e

File tree

4 files changed

+186
-25
lines changed

4 files changed

+186
-25
lines changed

Adafruit_CC3000.cpp

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ Adafruit_CC3000_Client Adafruit_CC3000::connectTCP(uint32_t destIP, uint16_t des
13001300

13011301
//printHex((byte *)&socketAddress, sizeof(socketAddress));
13021302
//if (CC3KPrinter != 0) CC3KPrinter->print(F("Connecting socket ... "));
1303-
if (-1 == connect(tcp_socket, &socketAddress, sizeof(socketAddress)))
1303+
if (-1 == ::connect(tcp_socket, &socketAddress, sizeof(socketAddress)))
13041304
{
13051305
CHECK_PRINTER {
13061306
CC3KPrinter->println(F("Connection error"));
@@ -1350,7 +1350,7 @@ Adafruit_CC3000_Client Adafruit_CC3000::connectUDP(uint32_t destIP, uint16_t des
13501350
}
13511351

13521352
//printHex((byte *)&socketAddress, sizeof(socketAddress));
1353-
if (-1 == connect(udp_socket, &socketAddress, sizeof(socketAddress)))
1353+
if (-1 == ::connect(udp_socket, &socketAddress, sizeof(socketAddress)))
13541354
{
13551355
CHECK_PRINTER {
13561356
CC3KPrinter->println(F("Connection error"));
@@ -1390,7 +1390,80 @@ void Adafruit_CC3000_Client::operator=(const Adafruit_CC3000_Client& other) {
13901390
memcpy(_rx_buf, other._rx_buf, RXBUFFERSIZE);
13911391
}
13921392

1393-
bool Adafruit_CC3000_Client::connected(void) {
1393+
Adafruit_CC3000_Client::operator bool()
1394+
{
1395+
return true;
1396+
}
1397+
1398+
int Adafruit_CC3000_Client::connect(const char *host, uint16_t port){
1399+
1400+
// if (!_initialised) return 0;
1401+
// if (!ulCC3000Connected) return 0;
1402+
// if (!ulCC3000DHCP) return 0;
1403+
1404+
uint32_t ip = 0;
1405+
1406+
int16_t r = gethostbyname(host, strlen(host), &ip);
1407+
1408+
if (ip!=0 && r!=0)
1409+
return connect(ip, port);
1410+
else
1411+
return 0;
1412+
}
1413+
1414+
int Adafruit_CC3000_Client::connect(IPAddress destIP, uint16_t destPort)
1415+
{
1416+
bufsiz = 0;
1417+
_rx_buf_idx = 0;
1418+
sockaddr socketAddress;
1419+
int32_t tcp_socket;
1420+
1421+
// Create the socket(s)
1422+
//if (CC3KPrinter != 0) CC3KPrinter->print(F("Creating socket ... "));
1423+
tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1424+
if (-1 == tcp_socket)
1425+
{
1426+
CHECK_PRINTER {
1427+
CC3KPrinter->println(F("Failed to open socket"));
1428+
}
1429+
return 0;
1430+
}
1431+
//CC3KPrinter->print(F("DONE (socket ")); CC3KPrinter->print(tcp_socket); CC3KPrinter->println(F(")"));
1432+
1433+
// Try to open the socket
1434+
memset(&socketAddress, 0x00, sizeof(socketAddress));
1435+
socketAddress.sa_family = AF_INET;
1436+
socketAddress.sa_data[0] = (destPort & 0xFF00) >> 8; // Set the Port Number
1437+
socketAddress.sa_data[1] = (destPort & 0x00FF);
1438+
socketAddress.sa_data[2] = destIP >> 24;
1439+
socketAddress.sa_data[3] = destIP >> 16;
1440+
socketAddress.sa_data[4] = destIP >> 8;
1441+
socketAddress.sa_data[5] = destIP;
1442+
1443+
CHECK_PRINTER {
1444+
CC3KPrinter->print(F("\n\rConnect to "));
1445+
CC3KPrinter->print(destIP);
1446+
CC3KPrinter->print(':');
1447+
CC3KPrinter->println(destPort);
1448+
}
1449+
1450+
//printHex((byte *)&socketAddress, sizeof(socketAddress));
1451+
//if (CC3KPrinter != 0) CC3KPrinter->print(F("Connecting socket ... "));
1452+
if (-1 == ::connect(tcp_socket, &socketAddress, sizeof(socketAddress)))
1453+
{
1454+
CHECK_PRINTER {
1455+
CC3KPrinter->println(F("Connection error"));
1456+
}
1457+
closesocket(tcp_socket);
1458+
return 0;
1459+
}
1460+
// if (CC3KPrinter != 0) CC3KPrinter->println(F("DONE"));
1461+
1462+
_socket = tcp_socket;
1463+
return 1;
1464+
}
1465+
1466+
uint8_t Adafruit_CC3000_Client::connected(void) {
13941467
if (_socket < 0) return false;
13951468

13961469
if (! available() && closed_sockets[_socket] == true) {
@@ -1404,11 +1477,15 @@ bool Adafruit_CC3000_Client::connected(void) {
14041477
else return true;
14051478
}
14061479

1407-
int16_t Adafruit_CC3000_Client::write(const void *buf, uint16_t len, uint32_t flags)
1480+
size_t Adafruit_CC3000_Client::write(const void *buf, uint16_t len, uint32_t flags)
14081481
{
14091482
return send(_socket, buf, len, flags);
14101483
}
14111484

1485+
size_t Adafruit_CC3000_Client::write(const uint8_t *buf, size_t len)
1486+
{
1487+
return write(buf, len, 0);
1488+
}
14121489

14131490
size_t Adafruit_CC3000_Client::write(uint8_t c)
14141491
{
@@ -1492,19 +1569,28 @@ size_t Adafruit_CC3000_Client::fastrprintln(char *str) {
14921569
return r;
14931570
}
14941571

1495-
int16_t Adafruit_CC3000_Client::read(void *buf, uint16_t len, uint32_t flags)
1572+
int Adafruit_CC3000_Client::read(void *buf, uint16_t len, uint32_t flags)
14961573
{
14971574
return recv(_socket, buf, len, flags);
14981575

14991576
}
15001577

1578+
int Adafruit_CC3000_Client::read(uint8_t *buf, size_t len)
1579+
{
1580+
return read(buf, len, 0);
1581+
}
1582+
15011583
int32_t Adafruit_CC3000_Client::close(void) {
15021584
int32_t x = closesocket(_socket);
15031585
_socket = -1;
15041586
return x;
15051587
}
15061588

1507-
uint8_t Adafruit_CC3000_Client::read(void)
1589+
void Adafruit_CC3000_Client::stop(){
1590+
close();
1591+
}
1592+
1593+
int Adafruit_CC3000_Client::read(void)
15081594
{
15091595
while ((bufsiz <= 0) || (bufsiz == _rx_buf_idx)) {
15101596
cc3k_int_poll();
@@ -1523,7 +1609,7 @@ uint8_t Adafruit_CC3000_Client::read(void)
15231609
return ret;
15241610
}
15251611

1526-
uint8_t Adafruit_CC3000_Client::available(void) {
1612+
int Adafruit_CC3000_Client::available(void) {
15271613
// not open!
15281614
if (_socket < 0) return 0;
15291615

@@ -1548,6 +1634,28 @@ uint8_t Adafruit_CC3000_Client::available(void) {
15481634
else return 0; // no data is available
15491635
}
15501636

1637+
void Adafruit_CC3000_Client::flush(){
1638+
1639+
}
1640+
1641+
int Adafruit_CC3000_Client::peek(){
1642+
while ((bufsiz <= 0) || (bufsiz == _rx_buf_idx)) {
1643+
cc3k_int_poll();
1644+
// buffer in some more data
1645+
bufsiz = recv(_socket, _rx_buf, sizeof(_rx_buf), 0);
1646+
if (bufsiz == -57) {
1647+
close();
1648+
return 0;
1649+
}
1650+
//if (CC3KPrinter != 0) { CC3KPrinter->println("Read "); CC3KPrinter->print(bufsiz); CC3KPrinter->println(" bytes"); }
1651+
_rx_buf_idx = 0;
1652+
}
1653+
uint8_t ret = _rx_buf[_rx_buf_idx];
1654+
1655+
//if (CC3KPrinter != 0) { CC3KPrinter->print("("); CC3KPrinter->write(ret); CC3KPrinter->print(")"); }
1656+
return ret;
1657+
}
1658+
15511659
void Adafruit_CC3000::setPrinter(Print* p) {
15521660
CC3KPrinter = p;
15531661
}

Adafruit_CC3000.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "utility/wlan.h"
3030
#include "utility/netapp.h"
3131
#include "ccspi.h"
32+
#include "Client.h"
3233

3334
#if defined(__arm__) && defined(__SAM3X8E__) // Arduino Due
3435
#define SPI_CLOCK_DIVIDER 6 // used to set the speed for the SPI bus; 6 == 14 Mhz on the Arduino Due
@@ -81,7 +82,7 @@ typedef enum
8182

8283
class Adafruit_CC3000;
8384

84-
class Adafruit_CC3000_Client : public Print {
85+
class Adafruit_CC3000_Client : public Client {
8586
public:
8687
Adafruit_CC3000_Client(uint16_t s);
8788
Adafruit_CC3000_Client(void);
@@ -91,7 +92,10 @@ class Adafruit_CC3000_Client : public Print {
9192
// NOTE: If public functions below are added/modified/removed please make sure to update the
9293
// Adafruit_CC3000_ClientRef class to match!
9394

94-
bool connected(void);
95+
int connect(IPAddress ip, uint16_t port);
96+
int connect(const char *host, uint16_t port);
97+
98+
uint8_t connected(void);
9599
size_t write(uint8_t c);
96100

97101
size_t fastrprint(const char *str);
@@ -101,11 +105,18 @@ class Adafruit_CC3000_Client : public Print {
101105
size_t fastrprint(const __FlashStringHelper *ifsh);
102106
size_t fastrprintln(const __FlashStringHelper *ifsh);
103107

104-
int16_t write(const void *buf, uint16_t len, uint32_t flags = 0);
105-
int16_t read(void *buf, uint16_t len, uint32_t flags = 0);
106-
uint8_t read(void);
108+
size_t write(const void *buf, uint16_t len, uint32_t flags = 0);
109+
int read(void *buf, uint16_t len, uint32_t flags = 0);
110+
int read(void);
107111
int32_t close(void);
108-
uint8_t available(void);
112+
int available(void);
113+
114+
int read(uint8_t *buf, size_t size);
115+
size_t write(const uint8_t *buf, size_t size);
116+
int peek();
117+
void flush();
118+
void stop();
119+
operator bool();
109120

110121
uint8_t _rx_buf[RXBUFFERSIZE], _rx_buf_idx;
111122
int16_t bufsiz;

Adafruit_CC3000_Server.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ Adafruit_CC3000_ClientRef::operator bool() {
3939
// Below are wrappers around the public client functions. These hide the fact that users
4040
// are dealing with a reference to a client instance and allow code to be written using
4141
// value semantics like in the Ethernet library.
42-
bool Adafruit_CC3000_ClientRef::connected(void) {
42+
int Adafruit_CC3000_ClientRef::connect(IPAddress ip, uint16_t port) {
43+
HANDLE_NULL(_client, false);
44+
return _client->connect(ip, port);
45+
}
46+
47+
int Adafruit_CC3000_ClientRef::connect(const char *host, uint16_t port) {
48+
HANDLE_NULL(_client, false);
49+
return _client->connect(host, port);
50+
}
51+
52+
uint8_t Adafruit_CC3000_ClientRef::connected(void) {
4353
HANDLE_NULL(_client, false);
4454
return _client->connected();
4555
}
@@ -79,17 +89,17 @@ size_t Adafruit_CC3000_ClientRef::fastrprintln(const __FlashStringHelper *ifsh)
7989
return _client->fastrprintln(ifsh);
8090
}
8191

82-
int16_t Adafruit_CC3000_ClientRef::write(const void *buf, uint16_t len, uint32_t flags) {
92+
size_t Adafruit_CC3000_ClientRef::write(const void *buf, uint16_t len, uint32_t flags) {
8393
HANDLE_NULL(_client, 0);
8494
return _client->write(buf, len, flags);
8595
}
8696

87-
int16_t Adafruit_CC3000_ClientRef::read(void *buf, uint16_t len, uint32_t flags) {
97+
int Adafruit_CC3000_ClientRef::read(void *buf, uint16_t len, uint32_t flags) {
8898
HANDLE_NULL(_client, 0);
8999
return _client->read(buf, len, flags);
90100
}
91101

92-
uint8_t Adafruit_CC3000_ClientRef::read(void) {
102+
int Adafruit_CC3000_ClientRef::read(void) {
93103
HANDLE_NULL(_client, 0);
94104
return _client->read();
95105
}
@@ -99,11 +109,34 @@ int32_t Adafruit_CC3000_ClientRef::close(void) {
99109
return _client->close();
100110
}
101111

102-
uint8_t Adafruit_CC3000_ClientRef::available(void) {
112+
int Adafruit_CC3000_ClientRef::available(void) {
103113
HANDLE_NULL(_client, 0);
104114
return _client->available();
105115
}
106116

117+
int Adafruit_CC3000_ClientRef::read(uint8_t *buf, size_t size) {
118+
HANDLE_NULL(_client, 0);
119+
return _client->read(buf, size);
120+
}
121+
122+
size_t Adafruit_CC3000_ClientRef::write(const uint8_t *buf, size_t size) {
123+
HANDLE_NULL(_client, 0);
124+
return _client->write(buf, size);
125+
}
126+
127+
int Adafruit_CC3000_ClientRef::peek() {
128+
HANDLE_NULL(_client, 0);
129+
return _client->peek();
130+
}
131+
132+
void Adafruit_CC3000_ClientRef::flush() {
133+
if (_client != NULL) _client->flush();
134+
}
135+
136+
void Adafruit_CC3000_ClientRef::stop() {
137+
if (_client != NULL) _client->stop();
138+
}
139+
107140

108141
/**************************************************************************/
109142
/*

Adafruit_CC3000_Server.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#include "Adafruit_CC3000.h"
2626

27-
#include "Print.h"
27+
#include "Client.h"
2828
#include "Server.h"
2929

3030
// Assume 4 sockets available, 1 of which is used for listening, so at most 3
@@ -35,14 +35,17 @@
3535
// and acts like a client instance value. This is done to mimic the semantics
3636
// of the Ethernet library, without running into problems allowing client buffers
3737
// to be copied and get out of sync.
38-
class Adafruit_CC3000_ClientRef : public Print {
38+
class Adafruit_CC3000_ClientRef : public Client {
3939
public:
4040
Adafruit_CC3000_ClientRef(Adafruit_CC3000_Client* client);
4141
// Return true if the referenced client is connected. This is provided for
4242
// compatibility with Ethernet library code.
4343
operator bool();
4444
// Below are all the public methods of the client class:
45-
bool connected(void);
45+
int connect(IPAddress ip, uint16_t port);
46+
int connect(const char *host, uint16_t port);
47+
48+
uint8_t connected(void);
4649
size_t write(uint8_t c);
4750

4851
size_t fastrprint(const char *str);
@@ -52,11 +55,17 @@ class Adafruit_CC3000_ClientRef : public Print {
5255
size_t fastrprint(const __FlashStringHelper *ifsh);
5356
size_t fastrprintln(const __FlashStringHelper *ifsh);
5457

55-
int16_t write(const void *buf, uint16_t len, uint32_t flags = 0);
56-
int16_t read(void *buf, uint16_t len, uint32_t flags = 0);
57-
uint8_t read(void);
58+
size_t write(const void *buf, uint16_t len, uint32_t flags = 0);
59+
int read(void *buf, uint16_t len, uint32_t flags = 0);
60+
int read(void);
5861
int32_t close(void);
59-
uint8_t available(void);
62+
int available(void);
63+
64+
int read(uint8_t *buf, size_t size);
65+
size_t write(const uint8_t *buf, size_t size);
66+
int peek();
67+
void flush();
68+
void stop();
6069

6170
private:
6271
// Hide the fact that users are really dealing with a pointer to a client

0 commit comments

Comments
 (0)