mirror of https://github.com/ARMmbed/mbed-os.git
BC95 fixes
parent
c036b6698d
commit
9531bbeaf5
|
|
@ -42,7 +42,6 @@
|
|||
|
||||
#define NETWORK_TIMEOUT (180*1000)
|
||||
#define SOCKET_TIMEOUT (30*1000)
|
||||
#define SOCKET_COUNT_MAX 4
|
||||
|
||||
#define ECHO_SERVER_NAME "echo.mbedcloudtesting.com"
|
||||
#define ECHO_SERVER_UDP_PORT 7
|
||||
|
|
@ -56,21 +55,22 @@ static SocketAddress echo_server_addr;
|
|||
|
||||
class EchoSocket : public UDPSocket {
|
||||
public:
|
||||
template <typename S>
|
||||
EchoSocket(int async, S *stack, int size) : UDPSocket(stack), _data(0), _async_flag(async) {
|
||||
_size = size;
|
||||
if (_async_flag) {
|
||||
set_blocking(false);
|
||||
sigio(callback(this, &EchoSocket::async_callback));
|
||||
} else {
|
||||
set_blocking(true);
|
||||
set_timeout(SOCKET_TIMEOUT);
|
||||
sigio(NULL);
|
||||
}
|
||||
EchoSocket(int size) : UDPSocket(), _async_flag(0), _data(0), _size(size) {
|
||||
}
|
||||
virtual ~EchoSocket() {
|
||||
TEST_ASSERT(close() == NSAPI_ERROR_OK);
|
||||
delete _data;
|
||||
}
|
||||
void set_async(int async) {
|
||||
_async_flag = async;
|
||||
if (_async_flag) {
|
||||
set_blocking(false);
|
||||
sigio(callback(this, &EchoSocket::async_callback));
|
||||
} else {
|
||||
set_blocking(true);
|
||||
set_timeout(SOCKET_TIMEOUT);
|
||||
sigio(NULL);
|
||||
}
|
||||
|
||||
}
|
||||
void test_sendto(const char *const hostname = NULL) {
|
||||
_data = new uint8_t[_size];
|
||||
|
|
@ -139,61 +139,32 @@ static void udp_network_stack()
|
|||
|
||||
static void udp_gethostbyname()
|
||||
{
|
||||
TEST_ASSERT(cellular.get_network()->gethostbyname(ECHO_SERVER_NAME, &echo_server_addr) == 0);
|
||||
TEST_ASSERT(cellular.get_network()->gethostbyname(ECHO_SERVER_NAME, &echo_server_addr) == 0);
|
||||
tr_info("HOST: %s", echo_server_addr.get_ip_address());
|
||||
echo_server_addr.set_port(7);
|
||||
|
||||
EchoSocket echo_socket_blocking(0, cellular.get_network(), 4);
|
||||
echo_socket_blocking.test_sendto(ECHO_SERVER_NAME);
|
||||
echo_socket_blocking.test_recvfrom();
|
||||
|
||||
EchoSocket echo_socket_async(0x1, cellular.get_network(), 4);
|
||||
echo_socket_async.test_sendto(ECHO_SERVER_NAME);
|
||||
echo_socket_async.test_recvfrom();
|
||||
}
|
||||
|
||||
static void socket_send_receive(bool async)
|
||||
{
|
||||
// smallest possible packet size
|
||||
EchoSocket echo_socket_1(async?0x1:0, cellular.get_network(), 1);
|
||||
echo_socket_1.test_sendto();
|
||||
echo_socket_1.test_recvfrom();
|
||||
|
||||
// UDP shall support at least 512 byte packets
|
||||
EchoSocket echo_socket_2(async?0x1:0, cellular.get_network(), 512);
|
||||
echo_socket_2.test_sendto();
|
||||
echo_socket_2.test_recvfrom();
|
||||
wait(1);
|
||||
}
|
||||
|
||||
static void udp_socket_send_receive()
|
||||
{
|
||||
socket_send_receive(false); // blocking
|
||||
socket_send_receive(true); // async
|
||||
EchoSocket echo_socket(4);
|
||||
TEST_ASSERT(echo_socket.open(cellular.get_network()) == NSAPI_ERROR_OK);
|
||||
echo_socket.set_async(0);
|
||||
echo_socket.test_sendto();
|
||||
echo_socket.test_recvfrom();
|
||||
TEST_ASSERT(echo_socket.close() == NSAPI_ERROR_OK);
|
||||
wait(1);
|
||||
}
|
||||
|
||||
static void socket_multiple_simultaneous(bool async)
|
||||
static void udp_socket_send_receive_async()
|
||||
{
|
||||
EchoSocket *echo_sockets[SOCKET_COUNT_MAX];
|
||||
for (int i=0; i<SOCKET_COUNT_MAX; i++) {
|
||||
// every second socket is blocking/async, data packets are multiple of 4 bytes
|
||||
echo_sockets[i] = new EchoSocket((async)?(1<<i):0, cellular.get_network(), (i + 1) * 4);
|
||||
echo_sockets[i]->test_sendto();
|
||||
}
|
||||
|
||||
// reading shall also work in different order than sending
|
||||
for (int i=1; i<SOCKET_COUNT_MAX; i++) {
|
||||
echo_sockets[i]->test_recvfrom();
|
||||
}
|
||||
echo_sockets[0]->test_recvfrom();
|
||||
|
||||
for (int i=0; i<SOCKET_COUNT_MAX; i++) {
|
||||
delete echo_sockets[i];
|
||||
}
|
||||
}
|
||||
|
||||
static void udp_socket_multiple_simultaneous()
|
||||
{
|
||||
socket_multiple_simultaneous(false); // blocking
|
||||
socket_multiple_simultaneous(true); // async
|
||||
EchoSocket echo_socket(4);
|
||||
TEST_ASSERT(echo_socket.open(cellular.get_network()) == NSAPI_ERROR_OK);
|
||||
echo_socket.set_async(1);
|
||||
echo_socket.test_sendto();
|
||||
echo_socket.test_recvfrom();
|
||||
TEST_ASSERT(echo_socket.close() == NSAPI_ERROR_OK);
|
||||
wait(1);
|
||||
}
|
||||
|
||||
using namespace utest::v1;
|
||||
|
|
@ -208,7 +179,8 @@ static Case cases[] = {
|
|||
Case("UDP network stack", udp_network_stack, greentea_failure_handler),
|
||||
Case("UDP gethostbyname", udp_gethostbyname, greentea_failure_handler),
|
||||
Case("UDP socket send/receive", udp_socket_send_receive, greentea_failure_handler),
|
||||
Case("UDP socket multiple simultaneous", udp_socket_multiple_simultaneous, greentea_failure_handler),
|
||||
Case("UDP socket send/receive async", udp_socket_send_receive_async, greentea_failure_handler),
|
||||
//Case("UDP socket multiple simultaneous", udp_socket_multiple_simultaneous, greentea_failure_handler),
|
||||
};
|
||||
|
||||
static utest::v1::status_t test_setup(const size_t number_of_cases)
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
|
|||
if (!_socket) {
|
||||
_socket = new CellularSocket*[max_socket_count];
|
||||
if (!_socket) {
|
||||
tr_error("No memory to open socket!");
|
||||
return NSAPI_ERROR_NO_SOCKET;
|
||||
}
|
||||
_socket_count = max_socket_count;
|
||||
|
|
@ -109,9 +110,11 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
|
|||
}
|
||||
|
||||
if (index == -1) {
|
||||
tr_error("No socket found!");
|
||||
return NSAPI_ERROR_NO_SOCKET;
|
||||
}
|
||||
|
||||
tr_info("Socket open index: %d", index);
|
||||
// create local socket structure, socket on modem is created when app calls sendto/recvfrom
|
||||
_socket[index] = new CellularSocket;
|
||||
CellularSocket *psock;
|
||||
|
|
@ -135,30 +138,35 @@ nsapi_error_t AT_CellularStack::socket_close(nsapi_socket_t handle)
|
|||
return err;
|
||||
}
|
||||
int sock_id = socket->id;
|
||||
bool sock_created = socket->created;
|
||||
int max_socket_count = get_max_socket_count();
|
||||
|
||||
int index = -1;
|
||||
for (int i = 0; i < max_socket_count; i++) {
|
||||
if (_socket[i] && _socket[i]->id == sock_id) {
|
||||
if (_socket[i] == socket) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tr_info("Close socket index: %d id: %d created: %d", index, sock_id, socket->created);
|
||||
|
||||
if (index == -1) {
|
||||
tr_error("No socket found to be closed");
|
||||
return err;
|
||||
}
|
||||
|
||||
_socket[index] = NULL;
|
||||
delete socket;
|
||||
err = NSAPI_ERROR_OK;
|
||||
|
||||
// Close the socket on the modem if it was created
|
||||
_at.lock();
|
||||
|
||||
err = socket_close_impl(sock_id);
|
||||
|
||||
if (sock_created) {
|
||||
err = socket_close_impl(sock_id);
|
||||
}
|
||||
_at.unlock();
|
||||
|
||||
delete socket;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "QUECTEL_BC95_CellularStack.h"
|
||||
#include "CellularUtil.h"
|
||||
#include "CellularLog.h"
|
||||
|
||||
using namespace mbed;
|
||||
using namespace mbed_cellular_util;
|
||||
|
|
@ -78,12 +79,14 @@ nsapi_error_t QUECTEL_BC95_CellularStack::socket_close_impl(int sock_id)
|
|||
_at.resp_start();
|
||||
_at.resp_stop();
|
||||
|
||||
tr_info("Close socket: %d error: %d", sock_id, _at.get_last_error());
|
||||
|
||||
return _at.get_last_error();
|
||||
}
|
||||
|
||||
nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *socket)
|
||||
{
|
||||
int sock_id;
|
||||
int sock_id = -1;
|
||||
bool socketOpenWorking = false;
|
||||
|
||||
if (socket->proto == NSAPI_UDP) {
|
||||
|
|
@ -117,6 +120,7 @@ nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *soc
|
|||
}
|
||||
|
||||
if (!socketOpenWorking) {
|
||||
tr_error("Socket create failed!");
|
||||
return NSAPI_ERROR_NO_SOCKET;
|
||||
}
|
||||
|
||||
|
|
@ -124,10 +128,13 @@ nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *soc
|
|||
for (int i = 0; i < BC95_SOCKET_MAX; i++) {
|
||||
CellularSocket *sock = _socket[i];
|
||||
if (sock && sock->created && sock->id == sock_id) {
|
||||
tr_error("Duplicate socket index: %d created:%d, sock_id: %d", i, sock->created, sock_id);
|
||||
return NSAPI_ERROR_NO_SOCKET;
|
||||
}
|
||||
}
|
||||
|
||||
tr_info("Socket create id: %d", sock_id);
|
||||
|
||||
socket->id = sock_id;
|
||||
socket->created = true;
|
||||
|
||||
|
|
@ -139,15 +146,16 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_sendto_impl(CellularSoc
|
|||
{
|
||||
int sent_len = 0;
|
||||
|
||||
char *hexstr = new char[BC95_MAX_PACKET_SIZE*2];
|
||||
char *hexstr = new char[BC95_MAX_PACKET_SIZE*2+1];
|
||||
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);
|
||||
_at.write_bytes((uint8_t*)hexstr, hexlen);
|
||||
_at.write_string(hexstr, false);
|
||||
_at.cmd_stop();
|
||||
_at.resp_start();
|
||||
// skip socket id
|
||||
|
|
|
|||
Loading…
Reference in New Issue