Skip to content

Commit

Permalink
drivers/at: expose some internal API
Browse files Browse the repository at this point in the history
  • Loading branch information
derMihai committed May 28, 2024
1 parent e62c25a commit cb1f0d1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
13 changes: 4 additions & 9 deletions drivers/at/at.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command,
return at_readline_skip_empty(dev, resp_buf, len, false, timeout);
}

static ssize_t get_resp_with_prefix(at_dev_t *dev, const char *resp_prefix,
ssize_t at_get_resp_with_prefix(at_dev_t *dev, const char *resp_prefix,
char *resp_buf, size_t len, uint32_t timeout)
{
ssize_t res;
Expand Down Expand Up @@ -368,7 +368,7 @@ ssize_t at_send_cmd_get_resp_wait_ok(at_dev_t *dev, const char *command, const c
if (res) {
return res;
}
res = get_resp_with_prefix(dev, resp_prefix, resp_buf, len, timeout);
res = at_get_resp_with_prefix(dev, resp_prefix, resp_buf, len, timeout);
if (res < 1) {
/* error or OK (empty response) */
return res;
Expand Down Expand Up @@ -438,7 +438,7 @@ ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf
return get_lines(dev, resp_buf, len, timeout);
}

static int wait_prompt(at_dev_t *dev, uint32_t timeout)
int at_wait_prompt(at_dev_t *dev, uint32_t timeout)
{
ssize_t res;
do {
Expand Down Expand Up @@ -467,7 +467,7 @@ int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout
if (res) {
return (int)res;
}
return wait_prompt(dev, timeout);
return at_wait_prompt(dev, timeout);
}

int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout)
Expand Down Expand Up @@ -665,11 +665,6 @@ ssize_t _emb_read_line_or_echo(at_dev_t *dev, char const *cmd, char *resp_buf,
size_t len, uint32_t timeout);
__attribute__((alias("get_lines")))
ssize_t _emb_get_lines(at_dev_t *dev, char *resp_buf, size_t len, uint32_t timeout);
__attribute__((alias("get_resp_with_prefix")))
ssize_t _emb_get_resp_with_prefix(at_dev_t *dev, const char *resp_prefix,
char *resp_buf, size_t len, uint32_t timeout);
__attribute__((alias("wait_echo")))
int _emb_wait_echo(at_dev_t *dev, char const *command, uint32_t timeout);
__attribute__((alias("wait_prompt")))
int _emb_wait_prompt(at_dev_t *dev, uint32_t timeout);
#endif
46 changes: 43 additions & 3 deletions drivers/include/at.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ typedef struct {
*
* @retval string containing the error value.
*/
static inline char const *at_get_err_info(at_dev_t *dev)
static inline char const *at_get_err_info(at_dev_t const *dev)
{
return dev->rp_buf;
}
Expand Down Expand Up @@ -316,6 +316,18 @@ int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout);
*/
int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout);

/**
* Waits for the prompt character (>).
*
* @param[in] dev device to operate on
* @param[in] timeout timeout (in usec)
*
* @retval 0 when prompt is received
* @retval -AT_ERR_EXTENDED if failed and a error code can be retrieved with
* @ref at_get_err_info() (i.e. DCE answered with `CMx ERROR`)
* @retval <0 other failures
*/
int at_wait_prompt(at_dev_t *dev, uint32_t timeout);
/**
* @brief Send AT command, wait for response
*
Expand All @@ -340,14 +352,42 @@ int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout
*/
ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command, char *resp_buf,
size_t len, uint32_t timeout);

/**
* @brief Wait for a response with a specific prefix.
*
* If the provided prefix is NULL or empty, this behaves just like
* @ref at_read_line_skip_empty(). Otherwise, it repeatedly calls @ref

Check failure on line 359 in drivers/include/at.h

View workflow job for this annotation

GitHub Actions / static-tests

unable to resolve reference to 'at_read_line_skip_empty()' for ref command
* at_read_line_skip_empty() and:

Check failure on line 360 in drivers/include/at.h

View workflow job for this annotation

GitHub Actions / static-tests

unable to resolve reference to 'at_read_line_skip_empty()' for ref command
* - if the prefix matches: the whole line is copied to @p and stops
* - if the prefix does not match:
* - if the response is OK: stops and returns 0
* - if the response is ERROR/CMx ERROR: stops and returns accordingly
* - none of the above: handles response as URC and continues
*
* @param[in] dev device to operate on
* @param[in] resp_prefix expected prefix in the response
* @param[out] resp_buf buffer for storing response
* @param[in] len len of @p buffer
* @param[in] timeout timeout (in usec)
*
* @retval n response length if the prefix of the response matches
* @retval 0 if the response was OK
* @retval -AT_ERR_EXTENDED if failed and a error code can be retrieved with
* @ref at_get_err_info() (i.e. DCE answered with `CMx ERROR`)
* @retval <0 on error
*/
ssize_t at_get_resp_with_prefix(at_dev_t *dev, const char *resp_prefix,
char *resp_buf, size_t len, uint32_t timeout);
/**
* @brief Send AT command, wait for response plus OK
*
* This function sends the supplied @p command, then waits and returns one
* line of response. The response is guaranteed null-terminated.
*
* A possible empty line will be skipped.
* Calls following in order:
* - @ref at_send_cmd()
* - @ref at_get_resp_with_prefix()
* - @ref at_wait_ok()
*
* URCs are automatically handled. If no prefix is provided, the response
* may be an URC. In that case, @ref at_postprocess_urc() can be called with the
Expand Down
23 changes: 10 additions & 13 deletions tests/drivers/at_unit/tests-at.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ static void assert_urc_count(unsigned expected)
int _emb_read_line_or_echo(at_dev_t *dev, char const *cmd, char *resp_buf,
size_t len, uint32_t timeout);
ssize_t _emb_get_lines(at_dev_t *dev, char *resp_buf, size_t len, uint32_t timeout);
ssize_t _emb_get_resp_with_prefix(at_dev_t *dev, const char *resp_prefix,
char *resp_buf, size_t len, uint32_t timeout);
int _emb_wait_echo(at_dev_t *dev, char const *command, uint32_t timeout);
int _emb_wait_prompt(at_dev_t *dev, uint32_t timeout);

static void inject_resp_str(at_dev_t *dev, char const *str)
{
Expand Down Expand Up @@ -344,7 +341,7 @@ void test_get_resp_with_prefix(void)
AT_RECV_EOL
"+RESPONSE: 123"
AT_RECV_EOL);
res = _emb_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
res = at_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
TEST_ASSERT(res > 0);
res = strcmp("123", resp_buf);
TEST_ASSERT(res == 0);
Expand All @@ -356,7 +353,7 @@ void test_get_resp_with_prefix(void)
AT_RECV_EOL
"OK"
AT_RECV_EOL);
res = _emb_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
res = at_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
TEST_ASSERT(res == 0);

inject_resp_str(dev,
Expand All @@ -365,7 +362,7 @@ void test_get_resp_with_prefix(void)
AT_RECV_EOL
AT_RECV_EOL "+CME ERROR: 1"
AT_RECV_EOL);
res = _emb_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
res = at_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
TEST_ASSERT(res == -AT_ERR_EXTENDED);
res = strncmp("1", dev->rp_buf, 1);
TEST_ASSERT(res == 0);
Expand All @@ -377,22 +374,22 @@ void test_get_resp_with_prefix(void)
AT_RECV_EOL
"ERROR"
AT_RECV_EOL);
res = _emb_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
res = at_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
TEST_ASSERT(res == -1);

inject_resp_str(dev,
AT_RECV_EOL
UNIT_TEST_SHORT_URC
AT_RECV_EOL);
res = _emb_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
res = at_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
TEST_ASSERT(res == -ETIMEDOUT);

inject_resp_str(dev,
"trash"
AT_RECV_EOL
"+RESPONSE: 123"
AT_RECV_EOL);
res = _emb_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
res = at_get_resp_with_prefix(dev, "+RESPONSE: ", resp_buf, sizeof(resp_buf), 10000);
TEST_ASSERT(res > 0);
res = strcmp("123", resp_buf);
TEST_ASSERT(res == 0);
Expand Down Expand Up @@ -500,7 +497,7 @@ void test_wait_prompt(void)
AT_RECV_EOL
">"
"123");
res = _emb_wait_prompt(dev, 1000);
res = at_wait_prompt(dev, 1000);
TEST_ASSERT(res == 0);
res = isrpipe_read_timeout(&dev->isrpipe, (unsigned char *)resp_buf, sizeof(resp_buf), 1000);
TEST_ASSERT(res == 3);
Expand All @@ -512,7 +509,7 @@ void test_wait_prompt(void)
AT_RECV_EOL
">"
"456");
res = _emb_wait_prompt(dev, 1000);
res = at_wait_prompt(dev, 1000);
TEST_ASSERT(res == 0);
res = isrpipe_read_timeout(&dev->isrpipe, (unsigned char *)resp_buf, sizeof(resp_buf), 1000);
TEST_ASSERT(res == 3);
Expand All @@ -525,13 +522,13 @@ void test_wait_prompt(void)
AT_RECV_EOL
"ERROR"
AT_RECV_EOL);
res = _emb_wait_prompt(dev, 1000);
res = at_wait_prompt(dev, 1000);
TEST_ASSERT(res == -1);

inject_resp_str(dev,
">"
"123");
res = _emb_wait_prompt(dev, 1000);
res = at_wait_prompt(dev, 1000);
TEST_ASSERT(res == 0);
res = isrpipe_read_timeout(&dev->isrpipe, (unsigned char *)resp_buf, sizeof(resp_buf), 1000);
TEST_ASSERT(res == 3);
Expand Down

0 comments on commit cb1f0d1

Please sign in to comment.