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 <sathish.narasimman@intel.com>
This commit is contained in:
Sathish Narasimman 2016-11-14 13:09:29 +05:30 committed by Johan Hedberg
commit 77446839ad
3 changed files with 32 additions and 32 deletions

View file

@ -39,23 +39,21 @@ int at_check_byte(struct net_buf *buf, char check_byte)
return 0; return 0;
} }
const char *skip_whitespace(const char *buf) static void skip_whitespace(struct at_client *at)
{ {
while (*buf == ' ') { while (at->buf[at->pos] == ' ') {
buf++; 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; uint32_t i;
buf = skip_whitespace(buf); skip_whitespace(at);
for (i = 0, *val = 0; isdigit(*buf); buf++, i++) { for (i = 0, *val = 0; isdigit(at->buf[at->pos]); at->pos++, i++) {
*val = *val * 10 + *buf - '0'; *val = *val * 10 + at->buf[at->pos] - '0';
} }
if (i == 0) { if (i == 0) {
@ -65,13 +63,13 @@ int at_get_number(const char *buf, uint32_t *val)
return 0; 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) { 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, 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) char stop_byte, enum at_cmd_state cmd_state)
{ {
int cmd_len = 0; int cmd_len = 0;
int len = buf->len; uint8_t pos = at->pos;
const char *str = buf->data; 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) { if (*str != stop_byte) {
at->buf[at->buf_pos] = *str; at->buf[at->pos++] = *str;
cmd_len++; cmd_len++;
str++; str++;
at->buf_pos++; pos = at->pos;
} else { } else {
cmd_len++; cmd_len++;
at->buf[at->buf_pos] = '\0'; at->buf[at->pos] = '\0';
at->pos = 0;
at->cmd_state = cmd_state; at->cmd_state = cmd_state;
break; break;
} }
} }
net_buf_pull(buf, cmd_len); net_buf_pull(buf, cmd_len);
if (at->buf_pos == at->buf_max_len) { if (pos == at->buf_max_len) {
return -ENOBUFS; 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) char stop_byte, enum at_state state)
{ {
int cmd_len = 0; int cmd_len = 0;
int len = buf->len; uint8_t pos = at->pos;
const char *str = buf->data; 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) { if (*str != stop_byte) {
at->buf[at->buf_pos] = *str; at->buf[at->pos++] = *str;
cmd_len++; cmd_len++;
str++; str++;
at->buf_pos++; pos = at->pos;
} else { } else {
cmd_len++; cmd_len++;
at->buf[at->buf_pos] = '\0'; at->buf[at->pos] = '\0';
at->pos = 0;
at->state = state; at->state = state;
break; break;
} }
} }
net_buf_pull(buf, cmd_len); net_buf_pull(buf, cmd_len);
if (at->buf_pos == at->buf_max_len) { if (pos == at->buf_max_len) {
return -ENOBUFS; 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) static void reset_buffer(struct at_client *at)
{ {
memset(at->buf, 0, at->buf_max_len); 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) 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, static int cmd_start(struct at_client *at, struct net_buf *buf,
const char *prefix, parse_val_t func) 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; at->state = AT_STATE_UNSOLICITED_CMD;
return -ENODATA; return -ENODATA;
} }

View file

@ -62,7 +62,7 @@ typedef int (*handle_cmd_input_t)(struct at_client *at, struct net_buf *buf,
struct at_client { struct at_client {
char *buf; char *buf;
uint8_t buf_pos; uint8_t pos;
uint8_t buf_max_len; uint8_t buf_max_len;
uint8_t state; uint8_t state;
uint8_t cmd_state; uint8_t cmd_state;
@ -73,7 +73,7 @@ struct at_client {
/* Register the callback functions */ /* Register the callback functions */
void at_register(struct at_client *at, at_resp_cb_t resp, void at_register(struct at_client *at, at_resp_cb_t resp,
at_finish_cb_t finish); 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 */ /* This parsing will only works for non-fragmented net_buf */
int at_parse_input(struct at_client *at, struct net_buf *buf); int at_parse_input(struct at_client *at, struct net_buf *buf);
/* This command parsing will only works for non-fragmented net_buf */ /* This command parsing will only works for non-fragmented net_buf */

View file

@ -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); struct bt_hfp_hf *hf = CONTAINER_OF(hf_at, struct bt_hfp_hf, at);
uint32_t val; uint32_t val;
int err; int ret;
err = at_get_number(hf_at->buf, &val); ret = at_get_number(hf_at, &val);
if (err < 0) { if (ret < 0) {
BT_ERR("Error getting value"); BT_ERR("Error getting value");
return err; return ret;
} }
hf->ag_features = val; hf->ag_features = val;