Skip to content

Commit 9531bbe

Browse files
Mirela ChiricaTeppo Järvelin
authored andcommitted
BC95 fixes
1 parent c036b66 commit 9531bbe

File tree

3 files changed

+59
-71
lines changed

3 files changed

+59
-71
lines changed

features/cellular/TESTS/socket/udp/main.cpp

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
#define NETWORK_TIMEOUT (180*1000)
4444
#define SOCKET_TIMEOUT (30*1000)
45-
#define SOCKET_COUNT_MAX 4
4645

4746
#define ECHO_SERVER_NAME "echo.mbedcloudtesting.com"
4847
#define ECHO_SERVER_UDP_PORT 7
@@ -56,21 +55,22 @@ static SocketAddress echo_server_addr;
5655

5756
class EchoSocket : public UDPSocket {
5857
public:
59-
template <typename S>
60-
EchoSocket(int async, S *stack, int size) : UDPSocket(stack), _data(0), _async_flag(async) {
61-
_size = size;
62-
if (_async_flag) {
63-
set_blocking(false);
64-
sigio(callback(this, &EchoSocket::async_callback));
65-
} else {
66-
set_blocking(true);
67-
set_timeout(SOCKET_TIMEOUT);
68-
sigio(NULL);
69-
}
58+
EchoSocket(int size) : UDPSocket(), _async_flag(0), _data(0), _size(size) {
7059
}
7160
virtual ~EchoSocket() {
72-
TEST_ASSERT(close() == NSAPI_ERROR_OK);
7361
delete _data;
62+
}
63+
void set_async(int async) {
64+
_async_flag = async;
65+
if (_async_flag) {
66+
set_blocking(false);
67+
sigio(callback(this, &EchoSocket::async_callback));
68+
} else {
69+
set_blocking(true);
70+
set_timeout(SOCKET_TIMEOUT);
71+
sigio(NULL);
72+
}
73+
7474
}
7575
void test_sendto(const char *const hostname = NULL) {
7676
_data = new uint8_t[_size];
@@ -139,61 +139,32 @@ static void udp_network_stack()
139139

140140
static void udp_gethostbyname()
141141
{
142-
TEST_ASSERT(cellular.get_network()->gethostbyname(ECHO_SERVER_NAME, &echo_server_addr) == 0);
142+
TEST_ASSERT(cellular.get_network()->gethostbyname(ECHO_SERVER_NAME, &echo_server_addr) == 0);
143+
tr_info("HOST: %s", echo_server_addr.get_ip_address());
143144
echo_server_addr.set_port(7);
144-
145-
EchoSocket echo_socket_blocking(0, cellular.get_network(), 4);
146-
echo_socket_blocking.test_sendto(ECHO_SERVER_NAME);
147-
echo_socket_blocking.test_recvfrom();
148-
149-
EchoSocket echo_socket_async(0x1, cellular.get_network(), 4);
150-
echo_socket_async.test_sendto(ECHO_SERVER_NAME);
151-
echo_socket_async.test_recvfrom();
152-
}
153-
154-
static void socket_send_receive(bool async)
155-
{
156-
// smallest possible packet size
157-
EchoSocket echo_socket_1(async?0x1:0, cellular.get_network(), 1);
158-
echo_socket_1.test_sendto();
159-
echo_socket_1.test_recvfrom();
160-
161-
// UDP shall support at least 512 byte packets
162-
EchoSocket echo_socket_2(async?0x1:0, cellular.get_network(), 512);
163-
echo_socket_2.test_sendto();
164-
echo_socket_2.test_recvfrom();
145+
wait(1);
165146
}
166147

167148
static void udp_socket_send_receive()
168149
{
169-
socket_send_receive(false); // blocking
170-
socket_send_receive(true); // async
171-
}
172-
173-
static void socket_multiple_simultaneous(bool async)
174-
{
175-
EchoSocket *echo_sockets[SOCKET_COUNT_MAX];
176-
for (int i=0; i<SOCKET_COUNT_MAX; i++) {
177-
// every second socket is blocking/async, data packets are multiple of 4 bytes
178-
echo_sockets[i] = new EchoSocket((async)?(1<<i):0, cellular.get_network(), (i + 1) * 4);
179-
echo_sockets[i]->test_sendto();
180-
}
181-
182-
// reading shall also work in different order than sending
183-
for (int i=1; i<SOCKET_COUNT_MAX; i++) {
184-
echo_sockets[i]->test_recvfrom();
185-
}
186-
echo_sockets[0]->test_recvfrom();
187-
188-
for (int i=0; i<SOCKET_COUNT_MAX; i++) {
189-
delete echo_sockets[i];
190-
}
150+
EchoSocket echo_socket(4);
151+
TEST_ASSERT(echo_socket.open(cellular.get_network()) == NSAPI_ERROR_OK);
152+
echo_socket.set_async(0);
153+
echo_socket.test_sendto();
154+
echo_socket.test_recvfrom();
155+
TEST_ASSERT(echo_socket.close() == NSAPI_ERROR_OK);
156+
wait(1);
191157
}
192158

193-
static void udp_socket_multiple_simultaneous()
159+
static void udp_socket_send_receive_async()
194160
{
195-
socket_multiple_simultaneous(false); // blocking
196-
socket_multiple_simultaneous(true); // async
161+
EchoSocket echo_socket(4);
162+
TEST_ASSERT(echo_socket.open(cellular.get_network()) == NSAPI_ERROR_OK);
163+
echo_socket.set_async(1);
164+
echo_socket.test_sendto();
165+
echo_socket.test_recvfrom();
166+
TEST_ASSERT(echo_socket.close() == NSAPI_ERROR_OK);
167+
wait(1);
197168
}
198169

199170
using namespace utest::v1;
@@ -208,7 +179,8 @@ static Case cases[] = {
208179
Case("UDP network stack", udp_network_stack, greentea_failure_handler),
209180
Case("UDP gethostbyname", udp_gethostbyname, greentea_failure_handler),
210181
Case("UDP socket send/receive", udp_socket_send_receive, greentea_failure_handler),
211-
Case("UDP socket multiple simultaneous", udp_socket_multiple_simultaneous, greentea_failure_handler),
182+
Case("UDP socket send/receive async", udp_socket_send_receive_async, greentea_failure_handler),
183+
//Case("UDP socket multiple simultaneous", udp_socket_multiple_simultaneous, greentea_failure_handler),
212184
};
213185

214186
static utest::v1::status_t test_setup(const size_t number_of_cases)

features/cellular/framework/AT/AT_CellularStack.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
9292
if (!_socket) {
9393
_socket = new CellularSocket*[max_socket_count];
9494
if (!_socket) {
95+
tr_error("No memory to open socket!");
9596
return NSAPI_ERROR_NO_SOCKET;
9697
}
9798
_socket_count = max_socket_count;
@@ -109,9 +110,11 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
109110
}
110111

111112
if (index == -1) {
113+
tr_error("No socket found!");
112114
return NSAPI_ERROR_NO_SOCKET;
113115
}
114116

117+
tr_info("Socket open index: %d", index);
115118
// create local socket structure, socket on modem is created when app calls sendto/recvfrom
116119
_socket[index] = new CellularSocket;
117120
CellularSocket *psock;
@@ -135,30 +138,35 @@ nsapi_error_t AT_CellularStack::socket_close(nsapi_socket_t handle)
135138
return err;
136139
}
137140
int sock_id = socket->id;
141+
bool sock_created = socket->created;
138142
int max_socket_count = get_max_socket_count();
139143

140144
int index = -1;
141145
for (int i = 0; i < max_socket_count; i++) {
142-
if (_socket[i] && _socket[i]->id == sock_id) {
146+
if (_socket[i] == socket) {
143147
index = i;
144148
break;
145149
}
146150
}
147151

152+
tr_info("Close socket index: %d id: %d created: %d", index, sock_id, socket->created);
153+
148154
if (index == -1) {
155+
tr_error("No socket found to be closed");
149156
return err;
150157
}
158+
151159
_socket[index] = NULL;
160+
delete socket;
152161
err = NSAPI_ERROR_OK;
153162

163+
// Close the socket on the modem if it was created
154164
_at.lock();
155-
156-
err = socket_close_impl(sock_id);
157-
165+
if (sock_created) {
166+
err = socket_close_impl(sock_id);
167+
}
158168
_at.unlock();
159169

160-
delete socket;
161-
162170
return err;
163171
}
164172

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "QUECTEL_BC95_CellularStack.h"
1919
#include "CellularUtil.h"
20+
#include "CellularLog.h"
2021

2122
using namespace mbed;
2223
using namespace mbed_cellular_util;
@@ -78,12 +79,14 @@ nsapi_error_t QUECTEL_BC95_CellularStack::socket_close_impl(int sock_id)
7879
_at.resp_start();
7980
_at.resp_stop();
8081

82+
tr_info("Close socket: %d error: %d", sock_id, _at.get_last_error());
83+
8184
return _at.get_last_error();
8285
}
8386

8487
nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *socket)
8588
{
86-
int sock_id;
89+
int sock_id = -1;
8790
bool socketOpenWorking = false;
8891

8992
if (socket->proto == NSAPI_UDP) {
@@ -117,17 +120,21 @@ nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *soc
117120
}
118121

119122
if (!socketOpenWorking) {
123+
tr_error("Socket create failed!");
120124
return NSAPI_ERROR_NO_SOCKET;
121125
}
122126

123127
// Check for duplicate socket id delivered by modem
124128
for (int i = 0; i < BC95_SOCKET_MAX; i++) {
125129
CellularSocket *sock = _socket[i];
126130
if (sock && sock->created && sock->id == sock_id) {
131+
tr_error("Duplicate socket index: %d created:%d, sock_id: %d", i, sock->created, sock_id);
127132
return NSAPI_ERROR_NO_SOCKET;
128133
}
129134
}
130135

136+
tr_info("Socket create id: %d", sock_id);
137+
131138
socket->id = sock_id;
132139
socket->created = true;
133140

@@ -139,15 +146,16 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_sendto_impl(CellularSoc
139146
{
140147
int sent_len = 0;
141148

142-
char *hexstr = new char[BC95_MAX_PACKET_SIZE*2];
149+
char *hexstr = new char[BC95_MAX_PACKET_SIZE*2+1];
143150
int hexlen = char_str_to_hex_str((const char*)data, size, hexstr);
144-
151+
// NULL terminated for write_string
152+
hexstr[hexlen] = 0;
145153
_at.cmd_start("AT+NSOST=");
146154
_at.write_int(socket->id);
147155
_at.write_string(address.get_ip_address(), false);
148156
_at.write_int(address.get_port());
149157
_at.write_int(size);
150-
_at.write_bytes((uint8_t*)hexstr, hexlen);
158+
_at.write_string(hexstr, false);
151159
_at.cmd_stop();
152160
_at.resp_start();
153161
// skip socket id

0 commit comments

Comments
 (0)