Cellular: Quectel BC95 TCP socket support

pull/8148/head
Kimmo Vaisanen 2018-09-17 11:58:34 +03:00
parent 0cd43157d1
commit 1f1b5d3a5a
2 changed files with 74 additions and 24 deletions

View File

@ -41,6 +41,36 @@ nsapi_error_t QUECTEL_BC95_CellularStack::socket_accept(void *server, void **soc
return NSAPI_ERROR_UNSUPPORTED;
}
nsapi_error_t QUECTEL_BC95_CellularStack::socket_connect(nsapi_socket_t handle, const SocketAddress &address)
{
CellularSocket *socket = (CellularSocket *)handle;
_at.lock();
if (!socket->created) {
const nsapi_error_t error_create = create_socket_impl(socket);
if (error_create != NSAPI_ERROR_OK) {
return error_create;
}
}
_at.cmd_start("AT+NSOCO=");
_at.write_int(socket->id);
_at.write_string(address.get_ip_address(), false);
_at.write_int(address.get_port());
_at.cmd_stop();
_at.resp_start();
_at.resp_stop();
_at.unlock();
if (_at.get_last_error() == NSAPI_ERROR_OK) {
socket->remoteAddress = address;
socket->connected = true;
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_CONNECTION;
}
void QUECTEL_BC95_CellularStack::urc_nsonmi()
{
int sock_id = _at.read_int();
@ -63,7 +93,7 @@ int QUECTEL_BC95_CellularStack::get_max_socket_count()
bool QUECTEL_BC95_CellularStack::is_protocol_supported(nsapi_protocol_t protocol)
{
return (protocol == NSAPI_UDP);
return (protocol == NSAPI_UDP || protocol == NSAPI_TCP);
}
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
bool socketOpenWorking = false;
if (socket->proto == NSAPI_UDP) {
_at.cmd_start("AT+NSOCR=DGRAM,17,");
} else if (socket->proto == NSAPI_TCP) {
_at.cmd_start("AT+NSOCR=STREAM,6,");
} else {
return NSAPI_ERROR_PARAMETER;
}
_at.write_int(socket->localAddress.get_port());
_at.write_int(1);
_at.cmd_stop();
_at.resp_start();
sock_id = _at.read_int();
_at.resp_stop();
socketOpenWorking = (_at.get_last_error() == NSAPI_ERROR_OK);
if (!socketOpenWorking) {
_at.cmd_start("AT+NSOCL=0");
_at.cmd_stop();
_at.resp_start();
_at.resp_stop();
if (socket->proto == NSAPI_UDP) {
_at.cmd_start("AT+NSOCR=DGRAM,17,");
} else if (socket->proto == NSAPI_TCP) {
_at.cmd_start("AT+NSOCR=STREAM,6,");
}
_at.write_int(socket->localAddress.get_port());
_at.write_int(1);
_at.cmd_stop();
@ -95,23 +149,6 @@ nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *soc
_at.resp_stop();
socketOpenWorking = (_at.get_last_error() == NSAPI_ERROR_OK);
if (!socketOpenWorking) {
_at.cmd_start("AT+NSOCL=0");
_at.cmd_stop();
_at.resp_start();
_at.resp_stop();
_at.cmd_start("AT+NSOCR=DGRAM,17,");
_at.write_int(socket->localAddress.get_port());
_at.write_int(1);
_at.cmd_stop();
_at.resp_start();
sock_id = _at.read_int();
_at.resp_stop();
socketOpenWorking = (_at.get_last_error() == NSAPI_ERROR_OK);
}
}
if (!socketOpenWorking || (sock_id == -1)) {
@ -145,11 +182,22 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_sendto_impl(CellularSoc
int hexlen = char_str_to_hex_str((const char *)data, size, hexstr);
// NULL terminated for write_string
hexstr[hexlen] = 0;
_at.cmd_start("AT+NSOST=");
_at.write_int(socket->id);
_at.write_string(address.get_ip_address(), false);
_at.write_int(address.get_port());
_at.write_int(size);
if (socket->proto == NSAPI_UDP) {
_at.cmd_start("AT+NSOST=");
_at.write_int(socket->id);
_at.write_string(address.get_ip_address(), false);
_at.write_int(address.get_port());
_at.write_int(size);
} else if (socket->proto == NSAPI_TCP) {
_at.cmd_start("AT+NSOSD=");
_at.write_int(socket->id);
_at.write_int(size);
} else {
delete hexstr;
return NSAPI_ERROR_PARAMETER;
}
_at.write_string(hexstr, false);
_at.cmd_stop();
_at.resp_start();

View File

@ -36,6 +36,8 @@ protected: // NetworkStack
virtual nsapi_error_t socket_accept(nsapi_socket_t server,
nsapi_socket_t *handle, SocketAddress *address = 0);
virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
protected: // AT_CellularStack
virtual int get_max_socket_count();