Skip to content

Commit

Permalink
Merge pull request #20140 from krzysztof-cabaj/tests-drivers-at
Browse files Browse the repository at this point in the history
tests/drivers/at: add check if device is initialized before sending command
  • Loading branch information
kfessel authored Apr 19, 2024
2 parents 4a7dc81 + 59d5af7 commit f130ebf
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/drivers/at/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static at_dev_t at_dev;
static char buf[256];
static char resp[1024];
static char rp_buf[256];
static bool initialized = false;
static bool is_power_on = false;

static int init(int argc, char **argv)
{
Expand Down Expand Up @@ -72,6 +74,8 @@ static int init(int argc, char **argv)
return 1;
}

initialized = true;

return res;
}

Expand All @@ -82,6 +86,18 @@ static int send(int argc, char **argv)
return 1;
}

if (initialized == false) {
puts("Error: AT device not initialized.\n");
puts("Execute init function first.\n");
return 1;
}

if (is_power_on == false) {
puts("Error: AT device not power on.\n");
puts("Execute power_on function first.\n");
return 1;
}

ssize_t len;
if ((len = at_send_cmd_get_resp(&at_dev, argv[1], resp, sizeof(resp), 10 * US_PER_SEC)) < 0) {
puts("Error");
Expand All @@ -100,6 +116,18 @@ static int send_ok(int argc, char **argv)
return 1;
}

if (initialized == false) {
puts("Error: AT device not initialized.\n");
puts("Execute init function first.\n");
return 1;
}

if (is_power_on == false) {
puts("Error: AT device not power on.\n");
puts("Execute power_on function first.\n");
return 1;
}

if (at_send_cmd_wait_ok(&at_dev, argv[1], 10 * US_PER_SEC) < 0) {
puts("Error");
return 1;
Expand All @@ -117,6 +145,18 @@ static int send_lines(int argc, char **argv)
return 1;
}

if (initialized == false) {
puts("Error: AT device not initialized.\n");
puts("Execute init function first.\n");
return 1;
}

if (is_power_on == false) {
puts("Error: AT device not power on.\n");
puts("Execute power_on function first.\n");
return 1;
}

ssize_t len;
if ((len = at_send_cmd_get_lines(&at_dev, argv[1], resp, sizeof(resp),
10 * US_PER_SEC)) < 0) {
Expand All @@ -138,6 +178,18 @@ static int send_recv_bytes(int argc, char **argv)
return 1;
}

if (initialized == false) {
puts("Error: AT device not initialized.\n");
puts("Execute init function first.\n");
return 1;
}

if (is_power_on == false) {
puts("Error: AT device not power on.\n");
puts("Execute power_on function first.\n");
return 1;
}

sprintf(buffer, "%s%s", argv[1], CONFIG_AT_SEND_EOL);
at_send_bytes(&at_dev, buffer, strlen(buffer));

Expand All @@ -160,6 +212,18 @@ static int send_recv_bytes_until_string(int argc, char **argv)
return 1;
}

if (initialized == false) {
puts("Error: AT device not initialized.\n");
puts("Execute init function first.\n");
return 1;
}

if (is_power_on == false) {
puts("Error: AT device not power on.\n");
puts("Execute power_on function first.\n");
return 1;
}

sprintf(buffer, "%s%s", argv[1], CONFIG_AT_SEND_EOL);
at_send_bytes(&at_dev, buffer, strlen(buffer));
memset(buffer, 0, sizeof(buffer));
Expand Down Expand Up @@ -192,6 +256,7 @@ static int power_on(int argc, char **argv)
(void)argv;

at_dev_poweron(&at_dev);
is_power_on = true;

puts("Powered on");

Expand All @@ -204,6 +269,7 @@ static int power_off(int argc, char **argv)
(void)argv;

at_dev_poweroff(&at_dev);
is_power_on = false;

puts("Powered off");

Expand Down

0 comments on commit f130ebf

Please sign in to comment.