1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/tests/driver_at/main.c

135 lines
2.6 KiB
C
Raw Normal View History

2018-05-22 12:52:27 +02:00
/*
* Copyright (C) 2018 OTA keys S.A.
2018-05-22 15:49:26 +02:00
* 2018 Inria
2018-05-22 12:52:27 +02:00
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief AT module test application
*
* @author Vincent Dupont <vincent@otakeys.com>
2018-05-22 15:49:26 +02:00
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
2018-05-22 12:52:27 +02:00
*
* @}
*/
2018-05-22 13:22:55 +02:00
#include <stdio.h>
2018-05-22 15:49:26 +02:00
#include <stdlib.h>
2018-05-22 13:22:55 +02:00
2018-05-22 12:52:27 +02:00
#include "at.h"
#include "shell.h"
#include "timex.h"
2018-05-22 15:49:26 +02:00
#include "periph/uart.h"
2018-05-22 12:52:27 +02:00
static at_dev_t at_dev;
static char buf[256];
static char resp[1024];
static int init(int argc, char **argv)
{
(void)argc;
(void)argv;
2018-05-22 15:49:26 +02:00
if (argc < 3) {
printf("Usage: %s <uart> <baudrate>\n", argv[0]);
return 1;
}
uint8_t uart = atoi(argv[1]);
uint32_t baudrate = atoi(argv[2]);
at_dev_init(&at_dev, UART_DEV(uart), baudrate, buf, sizeof(buf));
2018-05-22 12:52:27 +02:00
return 0;
}
static int send(int argc, char **argv)
{
if (argc < 2) {
2018-05-22 15:49:26 +02:00
printf("Usage: %s <command>\n", argv[0]);
2018-05-22 12:52:27 +02:00
return 1;
}
2018-05-23 10:38:05 +02:00
ssize_t len;
if ((len = at_send_cmd_get_resp(&at_dev, argv[1], resp, sizeof(resp), 10 * US_PER_SEC)) < 0) {
2018-05-22 12:52:27 +02:00
puts("Error");
2018-05-22 15:49:26 +02:00
return 1;
2018-05-22 12:52:27 +02:00
}
2018-05-23 10:38:05 +02:00
printf("Response (len=%d): %s\n", (int)len, resp);
return 0;
}
static int send_ok(int argc, char **argv)
{
if (argc < 2) {
printf("Usage: %s <command>\n", argv[0]);
return 1;
}
if (at_send_cmd_wait_ok(&at_dev, argv[1], 10 * US_PER_SEC) < 0) {
puts("Error");
return 1;
}
puts("OK");
return 0;
}
static int send_lines(int argc, char **argv)
{
if (argc < 2) {
printf("Usage: %s <command>\n", argv[0]);
return 1;
}
ssize_t len;
if ((len = at_send_cmd_get_lines(&at_dev, argv[1], resp, sizeof(resp), 10 * US_PER_SEC)) < 0) {
puts("Error");
return 1;
}
printf("Response (len=%d): %s\n", (int)len, resp);
2018-05-22 15:49:26 +02:00
2018-05-22 12:52:27 +02:00
return 0;
}
static int drain(int argc, char **argv)
{
(void)argc;
(void)argv;
at_drain(&at_dev);
return 0;
}
static const shell_command_t shell_commands[] = {
{ "init", "Initialize AT device", init },
{ "send", "Send a command and wait response", send },
2018-05-23 10:38:05 +02:00
{ "send_ok", "Send a command and wait OK", send_ok },
{ "send_lines", "Send a command and wait lines", send_lines },
2018-05-22 12:52:27 +02:00
{ "drain", "Drain AT device", drain },
{ NULL, NULL, NULL },
};
int main(void)
{
puts("AT command test app");
/* run the shell */
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}