Skip to content

Cellular: Fix cellular stack and drivers for netsocket tests #9705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ TEST_F(TestAT_CellularContext, connect_disconnect_sync)
network_cb_count = 0;
// connect in sync mode, semaphore will return 0 so timeout is returned
ASSERT_EQ(ctx.connect(), NSAPI_ERROR_TIMEOUT);
ASSERT_EQ(network_cb_count, 0);
ASSERT_EQ(network_cb_count, 1);

my_AT_CTX ctx1(at, &dev);
ctx1.attach(&network_cb);
Expand All @@ -486,7 +486,7 @@ TEST_F(TestAT_CellularContext, connect_disconnect_sync)

ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);

ASSERT_EQ(network_cb_count, 4);
ASSERT_EQ(network_cb_count, 5);

ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED);

Expand Down
16 changes: 12 additions & 4 deletions features/cellular/framework/AT/ATHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ using namespace mbed_cellular_util;
#define PROCESS_URC_TIME 20

// Suppress logging of very big packet payloads, maxlen is approximate due to write/read are cached
#define DEBUG_MAXLEN 80
#define DEBUG_MAXLEN 60
#define DEBUG_END_MARK "..\r"

const char *mbed::OK = "OK\r\n";
const uint8_t OK_LENGTH = 4;
Expand Down Expand Up @@ -463,7 +464,7 @@ ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len)
}
buf[read_len] = c;
if (_debug_on && read_len >= DEBUG_MAXLEN) {
debug_print("..", sizeof(".."));
debug_print(DEBUG_END_MARK, sizeof(DEBUG_END_MARK) - 1);
_debug_on = false;
}
}
Expand Down Expand Up @@ -555,9 +556,15 @@ ssize_t ATHandler::read_hex_string(char *buf, size_t size)
size_t buf_idx = 0;
char hexbuf[2];

bool debug_on = _debug_on;
for (; read_idx < size * 2 + match_pos; read_idx++) {
int c = get_char();

if (_debug_on && read_idx >= DEBUG_MAXLEN) {
debug_print(DEBUG_END_MARK, sizeof(DEBUG_END_MARK) - 1);
_debug_on = false;
}

if (match_pos) {
buf_idx++;
} else {
Expand Down Expand Up @@ -595,6 +602,7 @@ ssize_t ATHandler::read_hex_string(char *buf, size_t size)
}
}
}
_debug_on = debug_on;

if (read_idx && (read_idx == size * 2 + match_pos)) {
buf_idx++;
Expand Down Expand Up @@ -1168,7 +1176,7 @@ size_t ATHandler::write(const void *data, size_t len)
if (write_len + ret < DEBUG_MAXLEN) {
debug_print((char *)data + write_len, ret);
} else {
debug_print("..", sizeof(".."));
debug_print(DEBUG_END_MARK, sizeof(DEBUG_END_MARK) - 1);
_debug_on = false;
}
}
Expand Down Expand Up @@ -1228,7 +1236,7 @@ void ATHandler::debug_print(const char *p, int len)
debug("\n");
} else if (c == '\n') {
} else {
debug("[%d]", c);
debug("#%02x", c);
}
} else {
debug("%c", c);
Expand Down
16 changes: 8 additions & 8 deletions features/cellular/framework/AT/AT_CellularContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ nsapi_error_t AT_CellularContext::connect()
if (_is_connected) {
return NSAPI_ERROR_IS_CONNECTED;
}
call_network_cb(NSAPI_STATUS_CONNECTING);

nsapi_error_t err = _device->attach_to_network();
_cb_data.error = check_operation(err, OP_CONNECT);

Expand All @@ -130,6 +132,10 @@ nsapi_error_t AT_CellularContext::connect()
}
}

if (_cb_data.error == NSAPI_ERROR_ALREADY) {
return NSAPI_ERROR_OK;
}

return _cb_data.error;
}

Expand Down Expand Up @@ -543,8 +549,6 @@ nsapi_error_t AT_CellularContext::activate_context()

void AT_CellularContext::do_connect()
{
call_network_cb(NSAPI_STATUS_CONNECTING);

if (!_is_context_active) {
_cb_data.error = do_activate_context();
#if !NSAPI_PPP_AVAILABLE
Expand Down Expand Up @@ -677,10 +681,11 @@ nsapi_error_t AT_CellularContext::disconnect()
} else {
deactivate_ip_context();
}
} else {
call_network_cb(NSAPI_STATUS_DISCONNECTED);
}

_is_connected = false;
call_network_cb(NSAPI_STATUS_DISCONNECTED);

return _at.unlock_return_error();
}
Expand Down Expand Up @@ -736,11 +741,6 @@ void AT_CellularContext::deactivate_context()
_at.write_int(_cid);
_at.cmd_stop_read_resp();
}

_at.clear_error();
_at.cmd_start("AT+CGATT=0");
_at.cmd_stop_read_resp();
_at.restore_at_timeout();
}

nsapi_error_t AT_CellularContext::get_apn_backoff_timer(int &backoff_timer)
Expand Down
38 changes: 38 additions & 0 deletions features/cellular/framework/AT/AT_CellularStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ nsapi_size_or_error_t AT_CellularStack::socket_sendto(nsapi_socket_t handle, con
return NSAPI_ERROR_DEVICE_ERROR;
}

if (socket->closed && !socket->rx_avail) {
tr_info("sendto socket %d closed", socket->id);
return NSAPI_ERROR_NO_CONNECTION;
}

if (size == 0) {
if (socket->proto == NSAPI_UDP) {
return NSAPI_ERROR_UNSUPPORTED;
} else if (socket->proto == NSAPI_TCP) {
return 0;
}
}

nsapi_size_or_error_t ret_val = NSAPI_ERROR_OK;

if (!socket->created) {
Expand Down Expand Up @@ -299,6 +312,11 @@ nsapi_size_or_error_t AT_CellularStack::socket_recvfrom(nsapi_socket_t handle, S
return NSAPI_ERROR_DEVICE_ERROR;
}

if (socket->closed) {
tr_info("recvfrom socket %d closed", socket->id);
return 0;
}

nsapi_size_or_error_t ret_val = NSAPI_ERROR_OK;

if (!socket->created) {
Expand All @@ -319,6 +337,11 @@ nsapi_size_or_error_t AT_CellularStack::socket_recvfrom(nsapi_socket_t handle, S

_at.unlock();

if (socket->closed) {
tr_info("recvfrom socket %d closed", socket->id);
return 0;
}

if (ret_val >= 0) {
if (addr) {
tr_info("Socket %d recv %d bytes from %s port %d", find_socket_index(socket), ret_val, addr->get_ip_address(), addr->get_port());
Expand Down Expand Up @@ -352,3 +375,18 @@ int AT_CellularStack::get_socket_index_by_port(uint16_t port)
}
return -1;
}

AT_CellularStack::CellularSocket *AT_CellularStack::find_socket(int sock_id)
{
CellularSocket *sock = NULL;
for (int i = 0; i < _socket_count; i++) {
if (_socket[i] && _socket[i]->id == sock_id) {
sock = _socket[i];
break;
}
}
if (!sock) {
tr_error("Socket not found %d", sock_id);
}
return sock;
}
10 changes: 10 additions & 0 deletions features/cellular/framework/AT/AT_CellularStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
_cb(NULL),
_data(NULL),
created(false),
closed(false),
started(false),
tx_ready(false),
rx_avail(false),
Expand All @@ -108,6 +109,7 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
void (*_cb)(void *);
void *_data;
bool created; // socket has been created on modem stack
bool closed; // socket has been closed by a peer
bool started; // socket has been opened on modem stack
bool tx_ready; // socket is ready for sending on modem stack
bool rx_avail; // socket has data for reading on modem stack
Expand Down Expand Up @@ -166,6 +168,14 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
virtual nsapi_size_or_error_t socket_recvfrom_impl(CellularSocket *socket, SocketAddress *address,
void *buffer, nsapi_size_t size) = 0;

/**
* Find the socket handle based on socket identifier
*
* @param sock_id Socket identifier
* @return Socket handle, NULL on error
*/
CellularSocket *find_socket(int sock_id);

// socket container
CellularSocket **_socket;

Expand Down
1 change: 1 addition & 0 deletions features/cellular/framework/device/CellularDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ nsapi_error_t CellularDevice::start_state_machine(CellularStateMachine::Cellular
_mutex.lock();
nsapi_error_t err = create_state_machine();
if (err) {
_mutex.unlock();
return err;
}

Expand Down
Loading