Skip to content

Commit 3568c54

Browse files
author
Cruz Monrreal
authored
Merge pull request #8180 from kivaisan/quectel_bg96_tcp_socket
Cellular: Quectel BG96 TCP socket support
2 parents 085b5a6 + 7931378 commit 3568c54

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.cpp

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,71 @@ nsapi_error_t QUECTEL_BG96_CellularStack::socket_accept(void *server, void **soc
3939
return NSAPI_ERROR_UNSUPPORTED;
4040
}
4141

42+
nsapi_error_t QUECTEL_BG96_CellularStack::socket_connect(nsapi_socket_t handle, const SocketAddress &address)
43+
{
44+
CellularSocket *socket = (CellularSocket *)handle;
45+
46+
int modem_connect_id = -1;
47+
int request_connect_id = socket->id;
48+
int err = -1;
49+
50+
_at.lock();
51+
if (socket->proto == NSAPI_TCP) {
52+
_at.cmd_start("AT+QIOPEN=");
53+
_at.write_int(_cid);
54+
_at.write_int(request_connect_id);
55+
_at.write_string("TCP");
56+
_at.write_string(address.get_ip_address());
57+
_at.write_int(address.get_port());
58+
_at.write_int(socket->localAddress.get_port());
59+
_at.write_int(0);
60+
_at.cmd_stop();
61+
62+
handle_open_socket_response(modem_connect_id, err);
63+
64+
if ((_at.get_last_error() == NSAPI_ERROR_OK) && err) {
65+
_at.cmd_start("AT+QICLOSE=");
66+
_at.write_int(modem_connect_id);
67+
_at.cmd_stop();
68+
_at.resp_start();
69+
_at.resp_stop();
70+
71+
_at.cmd_start("AT+QIOPEN=");
72+
_at.write_int(_cid);
73+
_at.write_int(request_connect_id);
74+
_at.write_string("TCP");
75+
_at.write_string(address.get_ip_address());
76+
_at.write_int(address.get_port());
77+
_at.write_int(socket->localAddress.get_port());
78+
_at.write_int(0);
79+
_at.cmd_stop();
80+
81+
handle_open_socket_response(modem_connect_id, err);
82+
}
83+
}
84+
85+
// If opened successfully BUT not requested one, close it
86+
if (!err && (modem_connect_id != request_connect_id)) {
87+
_at.cmd_start("AT+QICLOSE=");
88+
_at.write_int(modem_connect_id);
89+
_at.cmd_stop();
90+
_at.resp_start();
91+
_at.resp_stop();
92+
}
93+
94+
nsapi_error_t ret_val = _at.get_last_error();
95+
_at.unlock();
96+
97+
if ((ret_val == NSAPI_ERROR_OK) && (modem_connect_id == request_connect_id)) {
98+
socket->created = true;
99+
socket->remoteAddress = address;
100+
socket->connected = true;
101+
return NSAPI_ERROR_OK;
102+
}
103+
104+
return NSAPI_ERROR_NO_CONNECTION;
105+
}
106+
42107
void QUECTEL_BG96_CellularStack::urc_qiurc()
43108
{
44109
int sock_id = 0;
@@ -66,16 +131,18 @@ int QUECTEL_BG96_CellularStack::get_max_socket_count()
66131

67132
bool QUECTEL_BG96_CellularStack::is_protocol_supported(nsapi_protocol_t protocol)
68133
{
69-
return (protocol == NSAPI_UDP);
134+
return (protocol == NSAPI_UDP || protocol == NSAPI_TCP);
70135
}
71136

72137
nsapi_error_t QUECTEL_BG96_CellularStack::socket_close_impl(int sock_id)
73138
{
139+
_at.set_at_timeout(BG96_CLOSE_SOCKET_TIMEOUT);
74140
_at.cmd_start("AT+QICLOSE=");
75141
_at.write_int(sock_id);
76142
_at.cmd_stop();
77143
_at.resp_start();
78144
_at.resp_stop();
145+
_at.restore_at_timeout();
79146

80147
return _at.get_last_error();
81148
}
@@ -198,8 +265,10 @@ nsapi_size_or_error_t QUECTEL_BG96_CellularStack::socket_sendto_impl(CellularSoc
198265
_at.cmd_start("AT+QISEND=");
199266
_at.write_int(socket->id);
200267
_at.write_int(size);
201-
_at.write_string(address.get_ip_address());
202-
_at.write_int(address.get_port());
268+
if (socket->proto == NSAPI_UDP) {
269+
_at.write_string(address.get_ip_address());
270+
_at.write_int(address.get_port());
271+
}
203272
_at.cmd_stop();
204273

205274
_at.resp_start(">");

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularStack.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace mbed {
2424

2525
#define BG96_SOCKET_MAX 12
2626
#define BG96_CREATE_SOCKET_TIMEOUT 150000 //150 seconds
27+
#define BG96_CLOSE_SOCKET_TIMEOUT 20000 // TCP socket max timeout is >10sec
2728

2829
class QUECTEL_BG96_CellularStack : public AT_CellularStack {
2930
public:
@@ -37,6 +38,8 @@ class QUECTEL_BG96_CellularStack : public AT_CellularStack {
3738
virtual nsapi_error_t socket_accept(nsapi_socket_t server,
3839
nsapi_socket_t *handle, SocketAddress *address = 0);
3940

41+
virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
42+
4043
protected: // AT_CellularStack
4144

4245
virtual int get_max_socket_count();

0 commit comments

Comments
 (0)