Skip to content

Commit 915e674

Browse files
feat(driver/at): add wall time in debug log to determine timeout issue
Signed-off-by: Coelacanthus <uwu@coelacanthus.name>
1 parent edb31ec commit 915e674

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

driver/apdu/at_cmd.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

33
#include <cjson/cJSON.h>
4+
#include <lpac/utils.h>
5+
6+
#include <inttypes.h>
47

58
#define AT_BUFFER_SIZE 20480
69
#define AT_READ_BUFFER_SIZE 4096
@@ -12,6 +15,17 @@
1215

1316
struct at_userdata;
1417

18+
#define AT_DEBUG(direct, line) \
19+
do { \
20+
if (getenv_or_default(ENV_AT_DEBUG, (bool)false)) { \
21+
struct timespec t = get_wall_time(userdata->wall); \
22+
fprintf(stderr, "AT_DEBUG_" direct "(%" PRId64 ".%" PRId64 "): %s\n", (int64_t)t.tv_sec, \
23+
(int64_t)t.tv_nsec, line); \
24+
} \
25+
} while (0)
26+
#define AT_DEBUG_TX(line) AT_DEBUG("TX", line)
27+
#define AT_DEBUG_RX(line) AT_DEBUG("RX", line)
28+
1529
int enumerate_serial_device(cJSON *device);
1630

1731
char *get_at_default_device(struct at_userdata *userdata);

driver/apdu/at_cmd_unix.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stdlib.h>
1313
#include <string.h>
1414
#include <termios.h> // Required for serial port configuration
15+
#include <time.h>
1516
#include <unistd.h>
1617

1718
struct at_userdata {
@@ -23,6 +24,8 @@ struct at_userdata {
2324
size_t at_read_buffer_len;
2425

2526
char **channels;
27+
28+
struct timespec wall;
2629
};
2730

2831
#if defined(__linux__)
@@ -124,8 +127,7 @@ int at_expect(struct at_userdata *userdata, char **response, const char *expecte
124127
if (strlen(line) == 0)
125128
continue;
126129

127-
if (getenv_or_default(ENV_AT_DEBUG, (bool)false))
128-
fprintf(stderr, "AT_DEBUG_RX: %s\n", line);
130+
AT_DEBUG_RX(line);
129131

130132
if (strcmp(line, "ERROR") == 0) {
131133
result = -1;

driver/apdu/at_cmd_win32.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdio.h>
1111
#include <stdlib.h>
1212
#include <string.h>
13+
#include <time.h>
1314

1415
#pragma comment(lib, "setupapi.lib")
1516

@@ -21,6 +22,8 @@ struct at_userdata {
2122
char *default_device;
2223

2324
char **channels;
25+
26+
struct timespec wall;
2427
};
2528

2629
int enumerate_serial_device(cJSON *devices) {
@@ -124,8 +127,7 @@ int at_expect(struct at_userdata *userdata, char **response, const char *expecte
124127
continue;
125128
}
126129

127-
if (getenv_or_default(ENV_AT_DEBUG, (bool)false))
128-
fprintf(stderr, "AT_DEBUG_RX: %s\n", line);
130+
AT_DEBUG_RX(line);
129131

130132
if (strcmp(line, "ERROR") == 0) {
131133
result = -1;
@@ -201,6 +203,7 @@ int at_setup_userdata(struct at_userdata **userdata) {
201203
if (*userdata == NULL)
202204
return -1;
203205
memset(*userdata, 0, sizeof(struct at_userdata));
206+
clock_gettime(CLOCK_MONOTONIC, &(*userdata)->wall);
204207
(*userdata)->default_device = "COM3";
205208
(*userdata)->hComm = INVALID_HANDLE_VALUE;
206209
(*userdata)->at_cmd_buffer = calloc(AT_BUFFER_SIZE, 1);

driver/apdu/at_helpers.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ int at_emit_command(struct at_userdata *userdata, const char *fmt, ...) {
7777
formatted[n + 1] = '\n'; // LF
7878
formatted[n + 2] = '\0'; // NUL
7979

80-
if (getenv_or_default(ENV_AT_DEBUG, (bool)false))
81-
fprintf(stderr, "AT_DEBUG_TX: %s", formatted);
80+
AT_DEBUG_TX(formatted);
8281

8382
int ret = at_write_command(userdata, formatted);
8483
return ret;

0 commit comments

Comments
 (0)