Skip to content

Commit c43a3f4

Browse files
author
Cruz Monrreal
authored
Merge pull request #8148 from kivaisan/wise_1570_cellular_tcp
Cellular: Quectel BC95 TCP socket support
2 parents ebff1e5 + 1f1b5d3 commit c43a3f4

File tree

2 files changed

+74
-24
lines changed

2 files changed

+74
-24
lines changed

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,36 @@ nsapi_error_t QUECTEL_BC95_CellularStack::socket_accept(void *server, void **soc
4141
return NSAPI_ERROR_UNSUPPORTED;
4242
}
4343

44+
nsapi_error_t QUECTEL_BC95_CellularStack::socket_connect(nsapi_socket_t handle, const SocketAddress &address)
45+
{
46+
CellularSocket *socket = (CellularSocket *)handle;
47+
48+
_at.lock();
49+
if (!socket->created) {
50+
const nsapi_error_t error_create = create_socket_impl(socket);
51+
if (error_create != NSAPI_ERROR_OK) {
52+
return error_create;
53+
}
54+
}
55+
56+
_at.cmd_start("AT+NSOCO=");
57+
_at.write_int(socket->id);
58+
_at.write_string(address.get_ip_address(), false);
59+
_at.write_int(address.get_port());
60+
_at.cmd_stop();
61+
_at.resp_start();
62+
_at.resp_stop();
63+
_at.unlock();
64+
65+
if (_at.get_last_error() == NSAPI_ERROR_OK) {
66+
socket->remoteAddress = address;
67+
socket->connected = true;
68+
return NSAPI_ERROR_OK;
69+
}
70+
71+
return NSAPI_ERROR_NO_CONNECTION;
72+
}
73+
4474
void QUECTEL_BC95_CellularStack::urc_nsonmi()
4575
{
4676
int sock_id = _at.read_int();
@@ -63,7 +93,7 @@ int QUECTEL_BC95_CellularStack::get_max_socket_count()
6393

6494
bool QUECTEL_BC95_CellularStack::is_protocol_supported(nsapi_protocol_t protocol)
6595
{
66-
return (protocol == NSAPI_UDP);
96+
return (protocol == NSAPI_UDP || protocol == NSAPI_TCP);
6797
}
6898

6999
nsapi_error_t QUECTEL_BC95_CellularStack::socket_close_impl(int sock_id)
@@ -85,8 +115,32 @@ nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *soc
85115
bool socketOpenWorking = false;
86116

87117
if (socket->proto == NSAPI_UDP) {
88-
89118
_at.cmd_start("AT+NSOCR=DGRAM,17,");
119+
} else if (socket->proto == NSAPI_TCP) {
120+
_at.cmd_start("AT+NSOCR=STREAM,6,");
121+
} else {
122+
return NSAPI_ERROR_PARAMETER;
123+
}
124+
_at.write_int(socket->localAddress.get_port());
125+
_at.write_int(1);
126+
_at.cmd_stop();
127+
_at.resp_start();
128+
sock_id = _at.read_int();
129+
_at.resp_stop();
130+
131+
socketOpenWorking = (_at.get_last_error() == NSAPI_ERROR_OK);
132+
133+
if (!socketOpenWorking) {
134+
_at.cmd_start("AT+NSOCL=0");
135+
_at.cmd_stop();
136+
_at.resp_start();
137+
_at.resp_stop();
138+
139+
if (socket->proto == NSAPI_UDP) {
140+
_at.cmd_start("AT+NSOCR=DGRAM,17,");
141+
} else if (socket->proto == NSAPI_TCP) {
142+
_at.cmd_start("AT+NSOCR=STREAM,6,");
143+
}
90144
_at.write_int(socket->localAddress.get_port());
91145
_at.write_int(1);
92146
_at.cmd_stop();
@@ -95,23 +149,6 @@ nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *soc
95149
_at.resp_stop();
96150

97151
socketOpenWorking = (_at.get_last_error() == NSAPI_ERROR_OK);
98-
99-
if (!socketOpenWorking) {
100-
_at.cmd_start("AT+NSOCL=0");
101-
_at.cmd_stop();
102-
_at.resp_start();
103-
_at.resp_stop();
104-
105-
_at.cmd_start("AT+NSOCR=DGRAM,17,");
106-
_at.write_int(socket->localAddress.get_port());
107-
_at.write_int(1);
108-
_at.cmd_stop();
109-
_at.resp_start();
110-
sock_id = _at.read_int();
111-
_at.resp_stop();
112-
113-
socketOpenWorking = (_at.get_last_error() == NSAPI_ERROR_OK);
114-
}
115152
}
116153

117154
if (!socketOpenWorking || (sock_id == -1)) {
@@ -145,11 +182,22 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_sendto_impl(CellularSoc
145182
int hexlen = char_str_to_hex_str((const char *)data, size, hexstr);
146183
// NULL terminated for write_string
147184
hexstr[hexlen] = 0;
148-
_at.cmd_start("AT+NSOST=");
149-
_at.write_int(socket->id);
150-
_at.write_string(address.get_ip_address(), false);
151-
_at.write_int(address.get_port());
152-
_at.write_int(size);
185+
186+
if (socket->proto == NSAPI_UDP) {
187+
_at.cmd_start("AT+NSOST=");
188+
_at.write_int(socket->id);
189+
_at.write_string(address.get_ip_address(), false);
190+
_at.write_int(address.get_port());
191+
_at.write_int(size);
192+
} else if (socket->proto == NSAPI_TCP) {
193+
_at.cmd_start("AT+NSOSD=");
194+
_at.write_int(socket->id);
195+
_at.write_int(size);
196+
} else {
197+
delete hexstr;
198+
return NSAPI_ERROR_PARAMETER;
199+
}
200+
153201
_at.write_string(hexstr, false);
154202
_at.cmd_stop();
155203
_at.resp_start();

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class QUECTEL_BC95_CellularStack : public AT_CellularStack {
3636
virtual nsapi_error_t socket_accept(nsapi_socket_t server,
3737
nsapi_socket_t *handle, SocketAddress *address = 0);
3838

39+
virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
40+
3941
protected: // AT_CellularStack
4042

4143
virtual int get_max_socket_count();

0 commit comments

Comments
 (0)