Skip to content

Commit 39733cb

Browse files
authored
Merge pull request #11306 from AnttiKauppila/ATHandler_improvements
At handler improvements
2 parents bdd6cb8 + 9151606 commit 39733cb

26 files changed

+231
-618
lines changed

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ void ATHandler::handle_args(const char *format, std::va_list list)
12371237
{
12381238
while (*format != '\0') {
12391239
if (*format == 'd') {
1240-
int i = va_arg(list, int);
1240+
int32_t i = va_arg(list, int32_t);
12411241
write_int(i);
12421242
} else if (*format == 's') {
12431243
char *str = (char *)va_arg(list, char *);
@@ -1305,10 +1305,14 @@ nsapi_error_t ATHandler::at_cmd_str(const char *cmd, const char *cmd_chr, char *
13051305

13061306
cmd_stop();
13071307

1308-
memcpy(_cmd_buffer, cmd, strlen(cmd));
1309-
_cmd_buffer[strlen(cmd)] = ':';
1310-
_cmd_buffer[strlen(cmd) + 1] = '\0';
1311-
resp_start(_cmd_buffer);
1308+
if (cmd && strlen(cmd) > 0) {
1309+
memcpy(_cmd_buffer, cmd, strlen(cmd));
1310+
_cmd_buffer[strlen(cmd)] = ':';
1311+
_cmd_buffer[strlen(cmd) + 1] = '\0';
1312+
resp_start(_cmd_buffer);
1313+
} else {
1314+
resp_start();
1315+
}
13121316

13131317
resp_buf[0] = '\0';
13141318
read_string(resp_buf, buf_size);

features/cellular/framework/AT/AT_CellularInformation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ nsapi_error_t AT_CellularInformation::get_imsi(char *imsi, size_t buf_size)
8383
return NSAPI_ERROR_PARAMETER;
8484
}
8585
_at.lock();
86-
_at.cmd_start("AT+CIMI");
87-
_at.cmd_stop();
86+
_at.cmd_start_stop("+CIMI", "");
8887
_at.resp_start();
8988
_at.read_string(imsi, MAX_IMSI_LENGTH + 1);
9089
_at.resp_stop();

features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularInformation.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@ GEMALTO_CINTERION_CellularInformation::GEMALTO_CINTERION_CellularInformation(ATH
2525

2626
nsapi_error_t GEMALTO_CINTERION_CellularInformation::get_iccid(char *buf, size_t buf_size)
2727
{
28-
_at.lock();
29-
_at.cmd_start("AT^SCID");
30-
_at.cmd_stop();
31-
_at.resp_start("^SCID:");
32-
_at.read_string(buf, buf_size);
33-
_at.resp_stop();
34-
return _at.unlock_return_error();
28+
return _at.at_cmd_str("^SCID", "", buf, buf_size);
3529
}
3630

3731
} /* namespace mbed */

features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularStack.cpp

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,11 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::socket_close_impl(int sock_id)
144144

145145
_at.set_at_timeout(FAILURE_TIMEOUT);
146146

147-
_at.cmd_start("AT^SISC=");
148-
_at.write_int(sock_id);
149-
_at.cmd_stop_read_resp();
147+
_at.at_cmd_discard("^SISC", "=", "%d", sock_id);
150148

151149
_at.clear_error(); // clear SISS even though SISC fails
152-
_at.cmd_start("AT^SISS=");
153-
_at.write_int(sock_id);
154-
_at.write_string("srvType");
155-
_at.write_string("none");
156-
_at.cmd_stop_read_resp();
150+
151+
_at.at_cmd_discard("^SISS", "=", "%d%s%s", sock_id, "srvType", "none");
157152

158153
_at.restore_at_timeout();
159154

@@ -170,8 +165,7 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::socket_open_defer(CellularSocket
170165
int internet_service_id = find_socket_index(socket);
171166
bool foundSrvType = false;
172167
bool foundConIdType = false;
173-
_at.cmd_start("AT^SISS?");
174-
_at.cmd_stop();
168+
_at.cmd_start_stop("^SISS", "?");
175169
_at.resp_start("^SISS:");
176170
/*
177171
* Profile is a list of tag-value map:
@@ -211,19 +205,11 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::socket_open_defer(CellularSocket
211205
_at.resp_stop();
212206

213207
if (!foundSrvType) {
214-
_at.cmd_start("AT^SISS=");
215-
_at.write_int(internet_service_id);
216-
_at.write_string("srvType");
217-
_at.write_string("Socket");
218-
_at.cmd_stop_read_resp();
208+
_at.at_cmd_discard("^SISS", "=", "%d%s%s", internet_service_id, "srvType", "Socket");
219209
}
220210

221211
if (!foundConIdType) {
222-
_at.cmd_start("AT^SISS=");
223-
_at.write_int(internet_service_id);
224-
_at.write_string("conId");
225-
_at.write_int(connection_profile_id);
226-
_at.cmd_stop_read_resp();
212+
_at.at_cmd_discard("^SISS", "=", "%d%s%d", internet_service_id, "conId", connection_profile_id);
227213
}
228214

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

252-
_at.cmd_start("AT^SISO=");
253-
_at.write_int(internet_service_id);
254-
_at.cmd_stop_read_resp();
238+
_at.at_cmd_discard("^SISO", "=", "%d", internet_service_id);
255239

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

339323
_at.set_at_timeout(FAILURE_TIMEOUT);
340-
_at.cmd_start("AT^SISW=");
341-
_at.write_int(socket->id);
342-
_at.write_int(size);
343324

344325
if (GEMALTO_CINTERION::get_module() != GEMALTO_CINTERION::ModuleBGS2) {
345-
_at.write_int(0);
346-
347326
// UDP requires Udp_RemClient
348327
if (socket->proto == NSAPI_UDP) {
349328
char socket_address[NSAPI_IPv6_SIZE + sizeof("[]:12345") - 1 + 1];
@@ -352,12 +331,14 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_sendto_impl(Cellul
352331
} else {
353332
std::sprintf(socket_address, "[%s]:%u", address.get_ip_address(), address.get_port());
354333
}
355-
_at.write_string(socket_address);
334+
_at.cmd_start_stop("^SISW", "=", "%d%d%d%s", socket->id, size, 0, socket_address);
335+
} else {
336+
_at.cmd_start_stop("^SISW", "=", "%d%d%d", socket->id, size, 0);
356337
}
338+
} else {
339+
_at.cmd_start_stop("^SISW", "=", "%d%d", socket->id, size);
357340
}
358341

359-
_at.cmd_stop();
360-
361342
sisw_retry:
362343
_at.resp_start("^SISW:");
363344
if (!_at.info_resp()) {
@@ -418,10 +399,7 @@ nsapi_size_or_error_t GEMALTO_CINTERION_CellularStack::socket_recvfrom_impl(Cell
418399
size = UDP_PACKET_SIZE;
419400
}
420401

421-
_at.cmd_start("AT^SISR=");
422-
_at.write_int(socket->id);
423-
_at.write_int(size);
424-
_at.cmd_stop();
402+
_at.cmd_start_stop("^SISR", "=", "%d%d", socket->id, size);
425403

426404
sisr_retry:
427405
_at.resp_start("^SISR:");
@@ -516,8 +494,8 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::create_connection_profile(int con
516494

517495
char conParamType[sizeof("GPRS0") + 1];
518496
std::sprintf(conParamType, "GPRS%d", (_stack_type == IPV4_STACK) ? 0 : 6);
519-
_at.cmd_start("AT^SICS?");
520-
_at.cmd_stop();
497+
498+
_at.cmd_start_stop("^SICS", "?");
521499
bool found_connection = false;
522500
_at.resp_start("^SICS:");
523501
while (_at.info_resp()) {
@@ -543,46 +521,25 @@ nsapi_error_t GEMALTO_CINTERION_CellularStack::create_connection_profile(int con
543521

544522
// connection profile is bound to a PDP context and it can not be changed
545523
if (!found_connection) {
546-
_at.cmd_start("AT^SICS=");
547-
_at.write_int(connection_profile_id);
548-
_at.write_string("conType");
549-
_at.write_string(conParamType);
550-
_at.cmd_stop_read_resp();
524+
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "conType", conParamType);
551525

552526
if (_apn && strlen(_apn) > 0) {
553-
_at.cmd_start("AT^SICS=");
554-
_at.write_int(connection_profile_id);
555-
_at.write_string("apn");
556-
_at.write_string(_apn);
557-
_at.cmd_stop_read_resp();
527+
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "apn", _apn);
558528
}
559529

560530
if (_user && strlen(_user) > 0) {
561-
_at.cmd_start("AT^SICS=");
562-
_at.write_int(connection_profile_id);
563-
_at.write_string("user");
564-
_at.write_string(_user);
565-
_at.cmd_stop_read_resp();
531+
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "user", _user);
566532
}
567533

568534
if (_password && strlen(_password) > 0) {
569-
_at.cmd_start("AT^SICS=");
570-
_at.write_int(connection_profile_id);
571-
_at.write_string("passwd");
572-
_at.write_string(_password);
573-
_at.cmd_stop_read_resp();
535+
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "passwd", _password);
574536
}
575537

576538
// set maximum inactivity timeout
577-
_at.cmd_start("AT^SICS=");
578-
_at.write_int(connection_profile_id);
579-
_at.write_string("inactTO");
580-
_at.write_int(0xffff); // 2^16-1
581-
_at.cmd_stop_read_resp();
539+
_at.at_cmd_discard("^SICS", "=", "%d%s%d", connection_profile_id, "inactTO", 0xffff);
582540

583541
// use URC mode ON
584-
_at.cmd_start("AT^SCFG=\"Tcp/withURCs\",\"on\"");
585-
_at.cmd_stop_read_resp();
542+
_at.at_cmd_discard("^SCFG", "=", "%s%s", "Tcp/withURCs", "on");
586543
}
587544

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

605-
_at.cmd_start("AT^SICS=");
606-
_at.write_int(connection_profile_id);
607-
_at.write_string("conType");
608-
_at.write_string("none");
609-
_at.cmd_stop_read_resp();
561+
_at.at_cmd_discard("^SICS", "=", "%d%s%s", connection_profile_id, "conType", "none");
610562

611563
_at.clear_error();
612564
}

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,16 @@ nsapi_error_t QUECTEL_BC95::get_sim_state(SimState &state)
5555
{
5656
_at->lock();
5757
_at->flush();
58-
_at->cmd_start("AT+NCCID?");
59-
_at->cmd_stop();
60-
_at->resp_start("+NCCID:");
61-
if (_at->info_resp()) {
62-
state = SimStateReady;
63-
} else {
58+
nsapi_error_t err = _at->at_cmd_discard("+NCCID", "?");
59+
_at->unlock();
60+
61+
state = SimStateReady;
62+
if (err != NSAPI_ERROR_OK) {
6463
tr_warn("SIM not readable.");
65-
state = SimStateUnknown; // SIM may not be ready yet
64+
state = SimStateUnknown;
6665
}
67-
_at->resp_stop();
68-
return _at->unlock_return_error();
66+
67+
return err;
6968
}
7069

7170
AT_CellularNetwork *QUECTEL_BC95::open_network_impl(ATHandler &at)
@@ -89,12 +88,9 @@ nsapi_error_t QUECTEL_BC95::init()
8988

9089
_at->lock();
9190
_at->flush();
92-
_at->cmd_start("AT");
93-
_at->cmd_stop_read_resp();
91+
_at->at_cmd_discard("", ""); //Send AT
9492

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

9995
return _at->unlock_return_error();
10096
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ QUECTEL_BC95_CellularInformation::~QUECTEL_BC95_CellularInformation()
2929
// According to BC95_AT_Commands_Manual_V1.9
3030
nsapi_error_t QUECTEL_BC95_CellularInformation::get_iccid(char *buf, size_t buf_size)
3131
{
32-
_at.lock();
33-
_at.cmd_start("AT+NCCID?");
34-
_at.cmd_stop();
35-
_at.resp_start("+NCCID:");
36-
_at.read_string(buf, buf_size);
37-
_at.resp_stop();
38-
return _at.unlock_return_error();
32+
return _at.at_cmd_str("+NCCID", "?", buf, buf_size);
3933
}
4034

4135
} /* namespace mbed */

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

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ nsapi_error_t QUECTEL_BC95_CellularStack::socket_connect(nsapi_socket_t handle,
6262
_at.write_int(socket->id);
6363
_at.write_string(address.get_ip_address(), false);
6464
_at.write_int(address.get_port());
65-
_at.cmd_stop();
66-
_at.resp_start();
67-
_at.resp_stop();
65+
_at.cmd_stop_read_resp();
66+
6867
_at.unlock();
6968

7069
if (_at.get_last_error() == NSAPI_ERROR_OK) {
@@ -130,13 +129,11 @@ nsapi_error_t QUECTEL_BC95_CellularStack::socket_close_impl(int sock_id)
130129
if (sock && sock->closed) {
131130
return NSAPI_ERROR_OK;
132131
}
133-
_at.cmd_start("AT+NSOCL=");
134-
_at.write_int(sock_id);
135-
_at.cmd_stop_read_resp();
132+
nsapi_error_t err = _at.at_cmd_discard("+NSOCL", "=", "%d", sock_id);
136133

137-
tr_info("Close socket: %d error: %d", sock_id, _at.get_last_error());
134+
tr_info("Close socket: %d error: %d", sock_id, err);
138135

139-
return _at.get_last_error();
136+
return err;
140137
}
141138

142139
nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *socket)
@@ -145,54 +142,23 @@ nsapi_error_t QUECTEL_BC95_CellularStack::create_socket_impl(CellularSocket *soc
145142
bool socketOpenWorking = false;
146143

147144
if (socket->proto == NSAPI_UDP) {
148-
_at.cmd_start("AT+NSOCR=DGRAM,17,");
145+
_at.cmd_start_stop("+NSOCR", "=DGRAM,", "%d%d%d", 17, socket->localAddress.get_port(), 1);
149146
} else if (socket->proto == NSAPI_TCP) {
150-
_at.cmd_start("AT+NSOCR=STREAM,6,");
147+
_at.cmd_start_stop("+NSOCR", "=STREAM,", "%d%d%d", 6, socket->localAddress.get_port(), 1);
151148
} else {
152149
return NSAPI_ERROR_PARAMETER;
153150
}
154-
_at.write_int(socket->localAddress.get_port());
155-
_at.write_int(1);
156-
_at.cmd_stop();
157151
_at.resp_start();
158152
sock_id = _at.read_int();
159153
_at.resp_stop();
160154

161155
socketOpenWorking = (_at.get_last_error() == NSAPI_ERROR_OK);
162156

163-
if (!socketOpenWorking) {
164-
_at.cmd_start("AT+NSOCL=0");
165-
_at.cmd_stop_read_resp();
166-
167-
if (socket->proto == NSAPI_UDP) {
168-
_at.cmd_start("AT+NSOCR=DGRAM,17,");
169-
} else if (socket->proto == NSAPI_TCP) {
170-
_at.cmd_start("AT+NSOCR=STREAM,6,");
171-
}
172-
_at.write_int(socket->localAddress.get_port());
173-
_at.write_int(1);
174-
_at.cmd_stop();
175-
_at.resp_start();
176-
sock_id = _at.read_int();
177-
_at.resp_stop();
178-
179-
socketOpenWorking = (_at.get_last_error() == NSAPI_ERROR_OK);
180-
}
181-
182157
if (!socketOpenWorking || (sock_id == -1)) {
183158
tr_error("Socket create failed!");
184159
return NSAPI_ERROR_NO_SOCKET;
185160
}
186161

187-
// Check for duplicate socket id delivered by modem
188-
for (int i = 0; i < BC95_SOCKET_MAX; i++) {
189-
CellularSocket *sock = _socket[i];
190-
if (sock && sock->id != -1 && sock->id == sock_id) {
191-
tr_error("Duplicate socket index: %d, sock_id: %d", i, sock_id);
192-
return NSAPI_ERROR_NO_SOCKET;
193-
}
194-
}
195-
196162
tr_info("Socket create id: %d", sock_id);
197163

198164
socket->id = sock_id;
@@ -261,10 +227,8 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_recvfrom_impl(CellularS
261227
int port;
262228
char ip_address[NSAPI_IP_SIZE];
263229

264-
_at.cmd_start("AT+NSORF=");
265-
_at.write_int(socket->id);
266-
_at.write_int(size <= PACKET_SIZE_MAX ? size : PACKET_SIZE_MAX);
267-
_at.cmd_stop();
230+
_at.cmd_start_stop("+NSORF", "=", "%d%d", socket->id, size < PACKET_SIZE_MAX ? size : PACKET_SIZE_MAX);
231+
268232
_at.resp_start();
269233
// receiving socket id
270234
_at.skip_param();

0 commit comments

Comments
 (0)