Skip to content
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

At handler improvements #11306

Merged
merged 2 commits into from
Sep 2, 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
14 changes: 9 additions & 5 deletions features/cellular/framework/AT/ATHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ void ATHandler::handle_args(const char *format, std::va_list list)
{
while (*format != '\0') {
if (*format == 'd') {
int i = va_arg(list, int);
int32_t i = va_arg(list, int32_t);
write_int(i);
} else if (*format == 's') {
char *str = (char *)va_arg(list, char *);
Expand Down Expand Up @@ -1292,10 +1292,14 @@ nsapi_error_t ATHandler::at_cmd_str(const char *cmd, const char *cmd_chr, char *

cmd_stop();

memcpy(_cmd_buffer, cmd, strlen(cmd));
_cmd_buffer[strlen(cmd)] = ':';
_cmd_buffer[strlen(cmd) + 1] = '\0';
resp_start(_cmd_buffer);
if (cmd && strlen(cmd) > 0) {
memcpy(_cmd_buffer, cmd, strlen(cmd));
_cmd_buffer[strlen(cmd)] = ':';
_cmd_buffer[strlen(cmd) + 1] = '\0';
resp_start(_cmd_buffer);
} else {
resp_start();
}

resp_buf[0] = '\0';
read_string(resp_buf, buf_size);
Expand Down
3 changes: 1 addition & 2 deletions features/cellular/framework/AT/AT_CellularInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ nsapi_error_t AT_CellularInformation::get_imsi(char *imsi, size_t buf_size)
return NSAPI_ERROR_PARAMETER;
}
_at.lock();
_at.cmd_start("AT+CIMI");
_at.cmd_stop();
_at.cmd_start_stop("+CIMI", "");
_at.resp_start();
_at.read_string(imsi, MAX_IMSI_LENGTH + 1);
_at.resp_stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ GEMALTO_CINTERION_CellularInformation::GEMALTO_CINTERION_CellularInformation(ATH

nsapi_error_t GEMALTO_CINTERION_CellularInformation::get_iccid(char *buf, size_t buf_size)
{
_at.lock();
_at.cmd_start("AT^SCID");
_at.cmd_stop();
_at.resp_start("^SCID:");
_at.read_string(buf, buf_size);
_at.resp_stop();
return _at.unlock_return_error();
return _at.at_cmd_str("^SCID", "", buf, buf_size);
}

} /* namespace mbed */
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,11 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::socket_close_impl(int sock_id)

_at.set_at_timeout(FAILURE_TIMEOUT);

_at.cmd_start("AT^SISC=");
_at.write_int(sock_id);
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SISC", "=", "%d", sock_id);

_at.clear_error(); // clear SISS even though SISC fails
_at.cmd_start("AT^SISS=");
_at.write_int(sock_id);
_at.write_string("srvType");
_at.write_string("none");
_at.cmd_stop_read_resp();

_at.at_cmd_discard("^SISS", "=", "%d%s%s", sock_id, "srvType", "none");

_at.restore_at_timeout();

Expand All @@ -170,8 +165,7 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::socket_open_defer(CellularSocket
int internet_service_id = find_socket_index(socket);
bool foundSrvType = false;
bool foundConIdType = false;
_at.cmd_start("AT^SISS?");
_at.cmd_stop();
_at.cmd_start_stop("^SISS", "?");
_at.resp_start("^SISS:");
/*
* Profile is a list of tag-value map:
Expand Down Expand Up @@ -211,19 +205,11 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::socket_open_defer(CellularSocket
_at.resp_stop();

if (!foundSrvType) {
_at.cmd_start("AT^SISS=");
_at.write_int(internet_service_id);
_at.write_string("srvType");
_at.write_string("Socket");
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SISS", "=", "%d%s%s", internet_service_id, "srvType", "Socket");
}

if (!foundConIdType) {
_at.cmd_start("AT^SISS=");
_at.write_int(internet_service_id);
_at.write_string("conId");
_at.write_int(connection_profile_id);
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SISS", "=", "%d%s%d", internet_service_id, "conId", connection_profile_id);
}

// host address (IPv4) and local+remote port is needed only for BGS2 which does not support UDP server socket
Expand All @@ -249,9 +235,7 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::socket_open_defer(CellularSocket
_at.write_string(sock_addr);
_at.cmd_stop_read_resp();

_at.cmd_start("AT^SISO=");
_at.write_int(internet_service_id);
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SISO", "=", "%d", internet_service_id);

if (_at.get_last_error()) {
tr_error("Socket %d open failed!", socket->id);
Expand Down Expand Up @@ -337,13 +321,8 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_sendto_impl(Cellul
}

_at.set_at_timeout(FAILURE_TIMEOUT);
_at.cmd_start("AT^SISW=");
_at.write_int(socket->id);
_at.write_int(size);

if (GEMALTO_CINTERION::get_module() != GEMALTO_CINTERION::ModuleBGS2) {
_at.write_int(0);

// UDP requires Udp_RemClient
if (socket->proto == NSAPI_UDP) {
char socket_address[NSAPI_IPv6_SIZE + sizeof("[]:12345") - 1 + 1];
Expand All @@ -352,12 +331,14 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_sendto_impl(Cellul
} else {
std::sprintf(socket_address, "[%s]:%u", address.get_ip_address(), address.get_port());
}
_at.write_string(socket_address);
_at.cmd_start_stop("^SISW", "=", "%d%d%d%s", socket->id, size, 0, socket_address);
} else {
_at.cmd_start_stop("^SISW", "=", "%d%d%d", socket->id, size, 0);
}
} else {
_at.cmd_start_stop("^SISW", "=", "%d%d", socket->id, size);
}

_at.cmd_stop();

sisw_retry:
_at.resp_start("^SISW:");
if (!_at.info_resp()) {
Expand Down Expand Up @@ -418,10 +399,7 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell
size = UDP_PACKET_SIZE;
}

_at.cmd_start("AT^SISR=");
_at.write_int(socket->id);
_at.write_int(size);
_at.cmd_stop();
_at.cmd_start_stop("^SISR", "=", "%d%d", socket->id, size);

sisr_retry:
_at.resp_start("^SISR:");
Expand Down Expand Up @@ -516,8 +494,8 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::create_connection_profile(int con

char conParamType[sizeof("GPRS0") + 1];
std::sprintf(conParamType, "GPRS%d", (_stack_type == IPV4_STACK) ? 0 : 6);
_at.cmd_start("AT^SICS?");
_at.cmd_stop();

_at.cmd_start_stop("^SICS", "?");
bool found_connection = false;
_at.resp_start("^SICS:");
while (_at.info_resp()) {
Expand All @@ -543,46 +521,25 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::create_connection_profile(int con

// connection profile is bound to a PDP context and it can not be changed
if (!found_connection) {
_at.cmd_start("AT^SICS=");
_at.write_int(connection_profile_id);
_at.write_string("conType");
_at.write_string(conParamType);
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "conType", conParamType);

if (_apn && strlen(_apn) > 0) {
_at.cmd_start("AT^SICS=");
_at.write_int(connection_profile_id);
_at.write_string("apn");
_at.write_string(_apn);
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "apn", _apn);
}

if (_user && strlen(_user) > 0) {
_at.cmd_start("AT^SICS=");
_at.write_int(connection_profile_id);
_at.write_string("user");
_at.write_string(_user);
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "user", _user);
}

if (_password && strlen(_password) > 0) {
_at.cmd_start("AT^SICS=");
_at.write_int(connection_profile_id);
_at.write_string("passwd");
_at.write_string(_password);
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "passwd", _password);
}

// set maximum inactivity timeout
_at.cmd_start("AT^SICS=");
_at.write_int(connection_profile_id);
_at.write_string("inactTO");
_at.write_int(0xffff); // 2^16-1
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SICS", "=", "%d%s%d", connection_profile_id, "inactTO", 0xffff);

// use URC mode ON
_at.cmd_start("AT^SCFG=\"Tcp/withURCs\",\"on\"");
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SCFG", "=", "%s%s", "Tcp/withURCs", "on");
}

tr_debug("Cinterion profile %d, %s (err %d)", connection_profile_id, (_stack_type == IPV4_STACK) ? "IPv4" : "IPv6", _at.get_last_error());
Expand All @@ -598,15 +555,10 @@ void GEMALTO_CINTERION_CellularStack::close_connection_profile(int connection_pr
// To clear connection profile need to detach from packet data.
// After detach modem sends PDP disconnected event to network class,
// which propagates network disconnected to upper layer to start reconnecting.
_at.cmd_start("AT+CGATT=0");
_at.cmd_stop_read_resp();
_at.at_cmd_discard("+CGATT", "=0");
_at.clear_error();

_at.cmd_start("AT^SICS=");
_at.write_int(connection_profile_id);
_at.write_string("conType");
_at.write_string("none");
_at.cmd_stop_read_resp();
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "conType", "none");

_at.clear_error();
}
Expand Down
24 changes: 10 additions & 14 deletions features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,16 @@ nsapi_error_t QUECTEL_BC95::get_sim_state(SimState &state)
{
_at->lock();
_at->flush();
_at->cmd_start("AT+NCCID?");
_at->cmd_stop();
_at->resp_start("+NCCID:");
if (_at->info_resp()) {
state = SimStateReady;
} else {
nsapi_error_t err = _at->at_cmd_discard("+NCCID", "?");
_at->unlock();

state = SimStateReady;
if (err != NSAPI_ERROR_OK) {
tr_warn("SIM not readable.");
state = SimStateUnknown; // SIM may not be ready yet
state = SimStateUnknown;
}
_at->resp_stop();
return _at->unlock_return_error();

return err;
}

AT_CellularNetwork *QUECTEL_BC95::open_network_impl(ATHandler &at)
Expand All @@ -87,12 +86,9 @@ nsapi_error_t QUECTEL_BC95::init()
{
_at->lock();
_at->flush();
_at->cmd_start("AT");
_at->cmd_stop_read_resp();
_at->at_cmd_discard("", ""); //Send AT

_at->cmd_start("AT+CMEE="); // verbose responses
_at->write_int(1);
_at->cmd_stop_read_resp();
_at->at_cmd_discard("+CMEE", "=1"); // verbose responses

return _at->unlock_return_error();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ QUECTEL_BC95_CellularInformation::~QUECTEL_BC95_CellularInformation()
// According to BC95_AT_Commands_Manual_V1.9
nsapi_error_t QUECTEL_BC95_CellularInformation::get_iccid(char *buf, size_t buf_size)
{
_at.lock();
_at.cmd_start("AT+NCCID?");
_at.cmd_stop();
_at.resp_start("+NCCID:");
_at.read_string(buf, buf_size);
_at.resp_stop();
return _at.unlock_return_error();
return _at.at_cmd_str("+NCCID", "?", buf, buf_size);
}

} /* namespace mbed */
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ nsapi_error_t QUECTEL_BC95_CellularStack::socket_connect(nsapi_socket_t handle,
_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.cmd_stop_read_resp();

_at.unlock();

if (_at.get_last_error() == NSAPI_ERROR_OK) {
Expand Down Expand Up @@ -130,13 +129,11 @@ nsapi_error_t QUECTEL_BC95_CellularStack::socket_close_impl(int sock_id)
if (sock && sock->closed) {
return NSAPI_ERROR_OK;
}
_at.cmd_start("AT+NSOCL=");
_at.write_int(sock_id);
_at.cmd_stop_read_resp();
nsapi_error_t err = _at.at_cmd_discard("+NSOCL", "=", "%d", sock_id);

tr_info("Close socket: %d error: %d", sock_id, _at.get_last_error());
tr_info("Close socket: %d error: %d", sock_id, err);

return _at.get_last_error();
return err;
}

nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *socket)
Expand All @@ -145,54 +142,23 @@ 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,");
_at.cmd_start_stop("+NSOCR", "=DGRAM,", "%d%d%d", 17, socket->localAddress.get_port(), 1);
} else if (socket->proto == NSAPI_TCP) {
_at.cmd_start("AT+NSOCR=STREAM,6,");
_at.cmd_start_stop("+NSOCR", "=STREAM,", "%d%d%d", 6, socket->localAddress.get_port(), 1);
} 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_read_resp();

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();
_at.resp_start();
sock_id = _at.read_int();
_at.resp_stop();

socketOpenWorking = (_at.get_last_error() == NSAPI_ERROR_OK);
}

if (!socketOpenWorking || (sock_id == -1)) {
tr_error("Socket create failed!");
return NSAPI_ERROR_NO_SOCKET;
}

// Check for duplicate socket id delivered by modem
for (int i = 0; i < BC95_SOCKET_MAX; i++) {
CellularSocket *sock = _socket[i];
if (sock && sock->id != -1 && sock->id == sock_id) {
tr_error("Duplicate socket index: %d, sock_id: %d", i, sock_id);
return NSAPI_ERROR_NO_SOCKET;
}
}

tr_info("Socket create id: %d", sock_id);

socket->id = sock_id;
Expand Down Expand Up @@ -261,10 +227,8 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_recvfrom_impl(CellularS
int port;
char ip_address[NSAPI_IP_SIZE];

_at.cmd_start("AT+NSORF=");
_at.write_int(socket->id);
_at.write_int(size <= PACKET_SIZE_MAX ? size : PACKET_SIZE_MAX);
_at.cmd_stop();
_at.cmd_start_stop("+NSORF", "=", "%d%d", socket->id, size < PACKET_SIZE_MAX ? size : PACKET_SIZE_MAX);

_at.resp_start();
// receiving socket id
_at.skip_param();
Expand Down
Loading