comparison uptools/atinterf/fcup-atinterf.c @ 341:692dbc4c2f07

fcup-atinterf program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 04 Feb 2018 07:56:21 +0000
parents
children e0260c2982b6
comparison
equal deleted inserted replaced
340:88fb194b4b61 341:692dbc4c2f07
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <strings.h>
6 #include <unistd.h>
7
8 extern int target_fd;
9
10 int wakeup_after_sec = 7;
11
12 FILE *target_rd;
13 char response[515];
14
15 char command[513], message[513];
16 int cmd_with_msg;
17
18 read_command_input(buf)
19 char *buf;
20 {
21 char *nl;
22
23 if (!fgets(buf, 513, stdin))
24 return(0);
25 nl = index(buf, '\n');
26 if (!nl) {
27 printf("Ecommand or message is too long\n");
28 exit(1);
29 }
30 *nl = '\0';
31 return(1);
32 }
33
34 send_to_target(cmd, term)
35 char *cmd;
36 {
37 char *endp;
38 int len, cc;
39
40 endp = index(cmd, '\0');
41 *endp = term;
42 len = endp - cmd + 1;
43 cc = write(target_fd, cmd, len);
44 *endp = '\0';
45 if (cc != len) {
46 printf("Etarget write error\n");
47 exit(1);
48 }
49 }
50
51 collect_target_response()
52 {
53 char *nl;
54
55 if (!fgets(response, 515, target_rd)) {
56 printf("Etarget read error\n");
57 exit(1);
58 }
59 nl = index(response, '\n');
60 if (!nl) {
61 printf("Etarget response is too long\n");
62 exit(1);
63 }
64 while (nl > response && nl[-1] == '\r')
65 nl--;
66 *nl = '\0';
67 }
68
69 execute_command()
70 {
71 int c;
72
73 send_to_target(command, '\r');
74 collect_target_response();
75 if (strcmp(command, response)) {
76 printf("Efailed to get echo of command\n");
77 exit(1);
78 }
79 if (cmd_with_msg) {
80 if ((c = getc(target_rd)) != '>') {
81 ungetc(c, target_rd);
82 collect_target_response();
83 printf("F%s\n", response);
84 return;
85 }
86 if ((c = getc(target_rd)) != ' ') {
87 ungetc(c, target_rd);
88 collect_target_response();
89 printf("F%s\n", response);
90 return;
91 }
92 send_to_target(message, '\033');
93 collect_target_response();
94 if (strcmp(message, response)) {
95 printf("Efailed to get echo of message\n");
96 exit(1);
97 }
98 }
99 for (;;) {
100 collect_target_response();
101 if (!strcmp(response, "OK") || !strcmp(response, "ERROR") ||
102 !strcmp(response, "BUSY") ||
103 !strcmp(response, "NO CARRIER") ||
104 !strncmp(response, "+CME ERROR", 10) ||
105 !strncmp(response, "+CMS ERROR", 10)) {
106 printf("F%s\n", response);
107 return;
108 }
109 printf("I%s\n", response);
110 }
111 }
112
113 main(argc, argv)
114 char **argv;
115 {
116 if (argc < 3 || argc > 4) {
117 fprintf(stderr,
118 "usage: %s ttyport baudrate [wakeup-after-sec]\n",
119 argv[0]);
120 exit(1);
121 }
122 open_serial_port(argv[1]);
123 set_fixed_baudrate(argv[2]);
124 set_serial_nonblock(0);
125 if (argc > 3)
126 wakeup_after_sec = strtoul(argv[3], 0, 0);
127
128 target_rd = fdopen(target_fd, "r");
129 if (!target_rd) {
130 perror("fdopen");
131 exit(1);
132 }
133
134 while (read_command_input(command)) {
135 if (!strcasecmp(command, "c+m")) {
136 cmd_with_msg = 1;
137 if (!read_command_input(command))
138 break;
139 if (!read_command_input(message))
140 break;
141 } else
142 cmd_with_msg = 0;
143 execute_command();
144 fflush(stdout);
145 }
146 exit(0);
147 }