Skip to content

Commit

Permalink
Parse multi-messages read
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Jul 11, 2017
1 parent 7a5c722 commit 791af3d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/at.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ char *at_parse(const char *str, struct bt_at *at) {
command[0] = command[sizeof(at->command)] = '\0';
}

/* consume <LF> from the end of the response */
if (feed[1] == '\n')
feed++;

}

/* In the BT specification, all AT commands are in uppercase letters.
Expand Down
2 changes: 0 additions & 2 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@

#include "a2dp-codecs.h"
#include "a2dp-rtp.h"
#include "at.h"
#include "bluealsa.h"
#include "hfp.h"
#include "transport.h"
#include "utils.h"
#include "shared/log.h"
Expand Down
19 changes: 16 additions & 3 deletions src/rfcomm.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ void *rfcomm_thread(void *arg) {
rfcomm_callback *callback;
struct bt_at at;
char buffer[256];
ssize_t len;

if (t->rfcomm.hfp_state != HFP_CONNECTED) {

Expand Down Expand Up @@ -467,7 +468,7 @@ void *rfcomm_thread(void *arg) {
continue;
}

if (read(pfds[1].fd, buffer, sizeof(buffer)) == -1) {
if ((len = read(pfds[1].fd, buffer, sizeof(buffer))) == -1) {
switch (errno) {
case ECONNABORTED:
case ECONNRESET:
Expand All @@ -482,9 +483,16 @@ void *rfcomm_thread(void *arg) {
}
}

/* In case of reading more than one message from the RFCOMM, we have to
* parse all of them before we can read from the socket once more. */
buffer[len] = '\0';
char *msg = buffer;
char *next;

parse:
/* parse AT message received from the RFCOMM */
if (at_parse(buffer, &at) == NULL) {
warn("Invalid AT message: %s", buffer);
if ((next = at_parse(msg, &at)) == NULL) {
warn("Invalid AT message: %s", msg);
continue;
}

Expand All @@ -496,6 +504,11 @@ void *rfcomm_thread(void *arg) {
rfcomm_write_at_msg(pfds[1].fd, AT_TYPE_RESP, NULL, "ERROR");
}

if (next[0] != '\0') {
msg = next;
goto parse;
}

}

fail:
Expand Down
2 changes: 1 addition & 1 deletion test/test-at.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main(void) {

/* concatenated commands */
const char *cmd = "\r\nOK\r\n\r\n+COPS:1\r\n";
assert(at_parse(cmd, &at) == &cmd[5]);
assert(at_parse(cmd, &at) == &cmd[6]);
assert(at.type == AT_TYPE_RESP);
assert(strcmp(at.command, "") == 0);
assert(strcmp(at.value, "OK") == 0);
Expand Down

0 comments on commit 791af3d

Please sign in to comment.