From 77446839add28d422cd54eada488822ed2077618 Mon Sep 17 00:00:00 2001 From: Sathish Narasimman Date: Mon, 14 Nov 2016 13:09:29 +0530 Subject: [PATCH] Bluetooth: AT: Improve API() to work with buffer increment at_get_number which converts the string into number now operates on at_client to get the string buffer. This patch also improves API() skip_whitespace to be operated on at_client. Also the the API's get_cmd_value, get_response_string are updated to work with buf increment. Also in this patch the return type of the function str_has_prefix is changed from 'int' to 'bool'. Change-Id: Ia626e0d13212b84413cce0444349975f4abe1cf6 Signed-off-by: Sathish Narasimman --- subsys/bluetooth/host/at.c | 52 +++++++++++++++++----------------- subsys/bluetooth/host/at.h | 4 +-- subsys/bluetooth/host/hfp_hf.c | 8 +++--- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/subsys/bluetooth/host/at.c b/subsys/bluetooth/host/at.c index 2c2eb8d76e4..97adc381b0b 100644 --- a/subsys/bluetooth/host/at.c +++ b/subsys/bluetooth/host/at.c @@ -39,23 +39,21 @@ int at_check_byte(struct net_buf *buf, char check_byte) return 0; } -const char *skip_whitespace(const char *buf) +static void skip_whitespace(struct at_client *at) { - while (*buf == ' ') { - buf++; + while (at->buf[at->pos] == ' ') { + at->pos++; } - - return buf; } -int at_get_number(const char *buf, uint32_t *val) +int at_get_number(struct at_client *at, uint32_t *val) { uint32_t i; - buf = skip_whitespace(buf); + skip_whitespace(at); - for (i = 0, *val = 0; isdigit(*buf); buf++, i++) { - *val = *val * 10 + *buf - '0'; + for (i = 0, *val = 0; isdigit(at->buf[at->pos]); at->pos++, i++) { + *val = *val * 10 + at->buf[at->pos] - '0'; } if (i == 0) { @@ -65,13 +63,13 @@ int at_get_number(const char *buf, uint32_t *val) return 0; } -int str_has_prefix(const char *str, const char *prefix) +static bool str_has_prefix(const char *str, const char *prefix) { if (strncmp(str, prefix, strlen(prefix)) != 0) { - return -EINVAL; + return false; } - return 0; + return true; } static int at_parse_result(const char *str, struct net_buf *buf, @@ -95,25 +93,26 @@ static int get_cmd_value(struct at_client *at, struct net_buf *buf, char stop_byte, enum at_cmd_state cmd_state) { int cmd_len = 0; - int len = buf->len; + uint8_t pos = at->pos; const char *str = buf->data; - while (cmd_len < len && at->buf_pos != at->buf_max_len) { + while (cmd_len < buf->len && at->pos != at->buf_max_len) { if (*str != stop_byte) { - at->buf[at->buf_pos] = *str; + at->buf[at->pos++] = *str; cmd_len++; str++; - at->buf_pos++; + pos = at->pos; } else { cmd_len++; - at->buf[at->buf_pos] = '\0'; + at->buf[at->pos] = '\0'; + at->pos = 0; at->cmd_state = cmd_state; break; } } net_buf_pull(buf, cmd_len); - if (at->buf_pos == at->buf_max_len) { + if (pos == at->buf_max_len) { return -ENOBUFS; } @@ -124,25 +123,26 @@ static int get_response_string(struct at_client *at, struct net_buf *buf, char stop_byte, enum at_state state) { int cmd_len = 0; - int len = buf->len; + uint8_t pos = at->pos; const char *str = buf->data; - while (cmd_len < len && at->buf_pos != at->buf_max_len) { + while (cmd_len < buf->len && at->pos != at->buf_max_len) { if (*str != stop_byte) { - at->buf[at->buf_pos] = *str; + at->buf[at->pos++] = *str; cmd_len++; str++; - at->buf_pos++; + pos = at->pos; } else { cmd_len++; - at->buf[at->buf_pos] = '\0'; + at->buf[at->pos] = '\0'; + at->pos = 0; at->state = state; break; } } net_buf_pull(buf, cmd_len); - if (at->buf_pos == at->buf_max_len) { + if (pos == at->buf_max_len) { return -ENOBUFS; } @@ -152,7 +152,7 @@ static int get_response_string(struct at_client *at, struct net_buf *buf, static void reset_buffer(struct at_client *at) { memset(at->buf, 0, at->buf_max_len); - at->buf_pos = 0; + at->pos = 0; } static int at_state_start(struct at_client *at, struct net_buf *buf) @@ -266,7 +266,7 @@ int at_parse_input(struct at_client *at, struct net_buf *buf) static int cmd_start(struct at_client *at, struct net_buf *buf, const char *prefix, parse_val_t func) { - if (str_has_prefix(at->buf, prefix) < 0) { + if (!str_has_prefix(at->buf, prefix)) { at->state = AT_STATE_UNSOLICITED_CMD; return -ENODATA; } diff --git a/subsys/bluetooth/host/at.h b/subsys/bluetooth/host/at.h index c6e24ac2ee5..96a334cad45 100644 --- a/subsys/bluetooth/host/at.h +++ b/subsys/bluetooth/host/at.h @@ -62,7 +62,7 @@ typedef int (*handle_cmd_input_t)(struct at_client *at, struct net_buf *buf, struct at_client { char *buf; - uint8_t buf_pos; + uint8_t pos; uint8_t buf_max_len; uint8_t state; uint8_t cmd_state; @@ -73,7 +73,7 @@ struct at_client { /* Register the callback functions */ void at_register(struct at_client *at, at_resp_cb_t resp, at_finish_cb_t finish); -int at_get_number(const char *buf, uint32_t *val); +int at_get_number(struct at_client *at, uint32_t *val); /* This parsing will only works for non-fragmented net_buf */ int at_parse_input(struct at_client *at, struct net_buf *buf); /* This command parsing will only works for non-fragmented net_buf */ diff --git a/subsys/bluetooth/host/hfp_hf.c b/subsys/bluetooth/host/hfp_hf.c index 939ee3635ac..0876f117611 100644 --- a/subsys/bluetooth/host/hfp_hf.c +++ b/subsys/bluetooth/host/hfp_hf.c @@ -90,12 +90,12 @@ int brsf_handle(struct at_client *hf_at) { struct bt_hfp_hf *hf = CONTAINER_OF(hf_at, struct bt_hfp_hf, at); uint32_t val; - int err; + int ret; - err = at_get_number(hf_at->buf, &val); - if (err < 0) { + ret = at_get_number(hf_at, &val); + if (ret < 0) { BT_ERR("Error getting value"); - return err; + return ret; } hf->ag_features = val;