Skip to content
Snippets Groups Projects
Unverified Commit e13bc428 authored by Martine Lenders's avatar Martine Lenders Committed by GitHub
Browse files

Merge pull request #10191 from fedepell/at_recv_bytes_1

drivers/at: add function to read raw data bytes from modem
parents 4b11509b e34d46e0
No related branches found
No related tags found
No related merge requests found
......@@ -63,6 +63,24 @@ void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len)
uart_write(dev->uart, (const uint8_t *)bytes, len);
}
ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout)
{
char *resp_pos = bytes;
while (len) {
int read_res;
if ((read_res = isrpipe_read_timeout(&dev->isrpipe, resp_pos, 1, timeout)) == 1) {
resp_pos += read_res;
len -= read_res;
}
else if (read_res == -ETIMEDOUT) {
break;
}
}
return (resp_pos - bytes);
}
int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout)
{
size_t cmdlen = strlen(command);
......
......@@ -220,6 +220,18 @@ int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
*/
void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len);
/**
* @brief Receive raw bytes from a device
*
* @param[in] dev device to operate on
* @param[in] bytes buffer where store received bytes
* @param[in] len maximum number of bytes to receive
* @param[in] timeout timeout (in usec) of inactivity to finish read
*
* @returns Number of bytes read, eventually zero if no bytes available
*/
ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout);
/**
* @brief Send command to device
*
......
......@@ -105,6 +105,25 @@ static int send_lines(int argc, char **argv)
return 0;
}
static int send_recv_bytes(int argc, char **argv)
{
char buffer[64];
if (argc < 3) {
printf("Usage: %s <command> <number of bytes>\n", argv[0]);
return 1;
}
sprintf(buffer, "%s%s", argv[1], AT_SEND_EOL);
at_send_bytes(&at_dev, buffer, strlen(buffer));
ssize_t len = at_recv_bytes(&at_dev, buffer, atoi(argv[2]), 10 * US_PER_SEC);
printf("Response (len=%d): %s\n", (int)len, buffer);
return 0;
}
static int drain(int argc, char **argv)
{
(void)argc;
......@@ -228,6 +247,7 @@ static const shell_command_t shell_commands[] = {
{ "send", "Send a command and wait response", send },
{ "send_ok", "Send a command and wait OK", send_ok },
{ "send_lines", "Send a command and wait lines", send_lines },
{ "send_recv_bytes", "Send a command and wait response as raw bytes", send_recv_bytes },
{ "drain", "Drain AT device", drain },
{ "power_on", "Power on AT device", power_on },
{ "power_off", "Power off AT device", power_off },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment