1 /*
2 * Copyright (c) 2006-2018, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2018-04-14 chenyong first version
9 */
10
11 #include <at.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14
15 static char send_buf[AT_CMD_MAX_LEN];
16 static rt_size_t last_cmd_len = 0;
17
18 /**
19 * dump hex format data to console device
20 *
21 * @param name name for hex object, it will show on log header
22 * @param buf hex buffer
23 * @param size buffer size
24 */
at_print_raw_cmd(const char * name,const char * buf,rt_size_t size)25 void at_print_raw_cmd(const char *name, const char *buf, rt_size_t size)
26 {
27 #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
28 #define WIDTH_SIZE 32
29
30 rt_size_t i, j;
31
32 for (i = 0; i < size; i += WIDTH_SIZE)
33 {
34 rt_kprintf("[D/AT] %s: %04X-%04X: ", name, i, i + WIDTH_SIZE);
35 for (j = 0; j < WIDTH_SIZE; j++)
36 {
37 if (i + j < size)
38 {
39 rt_kprintf("%02X ", buf[i + j]);
40 }
41 else
42 {
43 rt_kprintf(" ");
44 }
45 if ((j + 1) % 8 == 0)
46 {
47 rt_kprintf(" ");
48 }
49 }
50 rt_kprintf(" ");
51 for (j = 0; j < WIDTH_SIZE; j++)
52 {
53 if (i + j < size)
54 {
55 rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
56 }
57 }
58 rt_kprintf("\n");
59 }
60 }
61
at_get_last_cmd(rt_size_t * cmd_size)62 const char *at_get_last_cmd(rt_size_t *cmd_size)
63 {
64 *cmd_size = last_cmd_len;
65 return send_buf;
66 }
67
at_vprintf(rt_device_t device,const char * format,va_list args)68 rt_size_t at_vprintf(rt_device_t device, const char *format, va_list args)
69 {
70 last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, args);
71
72 #ifdef AT_PRINT_RAW_CMD
73 at_print_raw_cmd("send", send_buf, last_cmd_len);
74 #endif
75
76 return rt_device_write(device, 0, send_buf, last_cmd_len);
77 }
78
at_vprintfln(rt_device_t device,const char * format,va_list args)79 rt_size_t at_vprintfln(rt_device_t device, const char *format, va_list args)
80 {
81 rt_size_t len;
82
83 len = at_vprintf(device, format, args);
84
85 rt_device_write(device, 0, "\r\n", 2);
86
87 return len + 2;
88 }
89