annotate uptools/atcmd/smwrite.c @ 407:19e5a3e2f9c0

fcup-settime: moved time() retrieval a little closer to the output A fundamental problem with all simple time transfer tools is that there is always some delay between the time retrieval on the source system and that transmitted time being set on the destination, and the resulting time on the destination system is off by that delay amount. This delay cannot be fully eliminated when working in a simple environment like ours, but we should make our best effort to minimize it. In the present case, moving the atinterf_init() call before the time() retrieval should make a teensy-tiny improvement.
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Aug 2018 21:52:17 +0000
parents ce3b57b8920b
children dc2fd8e6f42c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
385
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program is a debug and development aid - it uses the AT+CMGW command
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * in PDU mode to write messages into SIM or ME SMS storage, simulating
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * arbitrary received or sent messages. This trick may come in useful
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * in the development and testing of SMS handling in phone handset UI firmware.
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/types.h>
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <ctype.h>
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <strings.h>
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <unistd.h>
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "../../rvinterf/include/exitcodes.h"
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 extern char at_response[];
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char input_line[176*2+2];
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 int lineno;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 process_cmdline(argc, argv)
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 char **argv;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 int c;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 extern int optind;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 while ((c = getopt(argc, argv, "B:np:RX:")) != EOF)
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (!atinterf_cmdline_opt(c)) {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 /* error msg already printed */
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 exit(ERROR_USAGE);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (argc != optind) {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 fprintf(stderr, "usage: %s [options]\n", argv[0]);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 exit(ERROR_USAGE);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return(0);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 get_input_line()
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 char *nl;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (!fgets(input_line, sizeof input_line, stdin))
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 return(0);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 lineno++;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 nl = index(input_line, '\n');
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 if (!nl) {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 fprintf(stderr, "input line %d: too long or unterminated\n",
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 lineno);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 exit(ERROR_USAGE);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 *nl = '\0';
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return(1);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 get_input_line_noeof()
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 int rc;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 rc = get_input_line();
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (!rc) {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 fprintf(stderr, "error: premature EOF in input\n");
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 exit(ERROR_USAGE);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 cmgw_callback()
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 /* skip empty lines */
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (at_response[1])
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 puts(at_response+1);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 process_record()
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 int msgstat;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 u_char pdubin[176];
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 int cc, scalen;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 char send_cmd[32];
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (!get_input_line())
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 return(0);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 if (!strcmp(input_line, "Received unread message:"))
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 msgstat = 0;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 else if (!strcmp(input_line, "Received message:"))
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 msgstat = 1;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 else if (!strcmp(input_line, "Stored unsent message:"))
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 msgstat = 2;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 else if (!strcmp(input_line, "Sent message:"))
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 msgstat = 3;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 else {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 fprintf(stderr,
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 "input line %d: expected beginning of message record\n",
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 lineno);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 exit(ERROR_USAGE);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 for (;;) {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 get_input_line_noeof();
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 if (!isupper(input_line[0]))
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 break;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 if (!index(input_line, ':'))
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 break;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 cc = decode_hex_line(input_line, pdubin, sizeof pdubin);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 if (cc < 1) {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 inv: fprintf(stderr, "input line %d: not a valid PDU\n", lineno);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 exit(ERROR_USAGE);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 scalen = pdubin[0] + 1;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 if (cc < scalen + 1)
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 goto inv;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 /* good to go */
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 sprintf(send_cmd, "AT+CMGW=%u,%d", cc - scalen, msgstat);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 atinterf_exec_cmd_needok(send_cmd, input_line, cmgw_callback);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 /* expect blank line after each PDU */
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 get_input_line_noeof();
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 if (input_line[0]) {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 fprintf(stderr,
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 "input line %d: expected blank line after PDU\n",
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 lineno);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 exit(ERROR_USAGE);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 return(1);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 }
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 main(argc, argv)
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 char **argv;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 {
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 process_cmdline(argc, argv);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 atinterf_init();
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 /* enable verbose error messages */
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 atinterf_exec_cmd_needok("AT+CMEE=2", 0, 0);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 /* set PDU mode */
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 atinterf_exec_cmd_needok("AT+CMGF=0", 0, 0);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 /* process stdin */
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 while (process_record())
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 ;
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 exit(0);
ce3b57b8920b fcup-smwrite program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 }