FreeCalypso > hg > freecalypso-tools
comparison uptools/atinterf/fcup-atinterf.c @ 345:cc207d81c05f
fcup-atinterf: implemented wakeup of sleeping targets
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 04 Feb 2018 16:05:11 +0000 |
| parents | e0260c2982b6 |
| children | 7eddc2578fdb |
comparison
equal
deleted
inserted
replaced
| 344:e0260c2982b6 | 345:cc207d81c05f |
|---|---|
| 1 #include <sys/types.h> | 1 #include <sys/types.h> |
| 2 #include <sys/time.h> | |
| 2 #include <stdio.h> | 3 #include <stdio.h> |
| 3 #include <stdlib.h> | 4 #include <stdlib.h> |
| 4 #include <string.h> | 5 #include <string.h> |
| 5 #include <strings.h> | 6 #include <strings.h> |
| 6 #include <unistd.h> | 7 #include <unistd.h> |
| 7 | 8 |
| 8 extern int target_fd; | 9 extern int target_fd; |
| 9 | |
| 10 int wakeup_after_sec = 7; | |
| 11 | 10 |
| 12 FILE *target_rd; | 11 FILE *target_rd; |
| 13 char response[515]; | 12 char response[515]; |
| 14 | 13 |
| 15 char command[513], message[513]; | 14 char command[513], message[513]; |
| 41 *endp = term; | 40 *endp = term; |
| 42 len = endp - cmd + 1; | 41 len = endp - cmd + 1; |
| 43 cc = write(target_fd, cmd, len); | 42 cc = write(target_fd, cmd, len); |
| 44 *endp = '\0'; | 43 *endp = '\0'; |
| 45 if (cc != len) { | 44 if (cc != len) { |
| 45 printf("Etarget write error\n"); | |
| 46 exit(1); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 single_char_to_target(ch) | |
| 51 { | |
| 52 char buf = ch; | |
| 53 int cc; | |
| 54 | |
| 55 cc = write(target_fd, &buf, 1); | |
| 56 if (cc != 1) { | |
| 46 printf("Etarget write error\n"); | 57 printf("Etarget write error\n"); |
| 47 exit(1); | 58 exit(1); |
| 48 } | 59 } |
| 49 } | 60 } |
| 50 | 61 |
| 108 } | 119 } |
| 109 printf("I%s\n", response); | 120 printf("I%s\n", response); |
| 110 } | 121 } |
| 111 } | 122 } |
| 112 | 123 |
| 124 wakeup_at() | |
| 125 { | |
| 126 single_char_to_target('A'); | |
| 127 usleep(50000); | |
| 128 single_char_to_target('T'); | |
| 129 usleep(50000); | |
| 130 single_char_to_target('\r'); | |
| 131 collect_target_response(); | |
| 132 if (response[0] && strcmp(response, "AT")) { | |
| 133 badresp: printf("Ebad response to wakeup AT command\n"); | |
| 134 exit(1); | |
| 135 } | |
| 136 collect_target_response(); | |
| 137 if (strcmp(response, "OK")) | |
| 138 goto badresp; | |
| 139 } | |
| 140 | |
| 113 main(argc, argv) | 141 main(argc, argv) |
| 114 char **argv; | 142 char **argv; |
| 115 { | 143 { |
| 144 int wakeup_after_sec = 7; | |
| 145 struct timeval curtime, last_time, timediff; | |
| 146 | |
| 116 if (argc < 3 || argc > 4) { | 147 if (argc < 3 || argc > 4) { |
| 117 fprintf(stderr, | 148 fprintf(stderr, |
| 118 "usage: %s ttyport baudrate [wakeup-after-sec]\n", | 149 "usage: %s ttyport baudrate [wakeup-after-sec]\n", |
| 119 argv[0]); | 150 argv[0]); |
| 120 exit(1); | 151 exit(1); |
| 121 } | 152 } |
| 122 open_serial_port(argv[1]); | 153 open_serial_port(argv[1]); |
| 123 set_fixed_baudrate(argv[2]); | 154 set_fixed_baudrate(argv[2]); |
| 124 set_serial_nonblock(0); | |
| 125 if (argc > 3) | 155 if (argc > 3) |
| 126 wakeup_after_sec = strtoul(argv[3], 0, 0); | 156 wakeup_after_sec = strtoul(argv[3], 0, 0); |
| 127 | 157 |
| 158 set_serial_nonblock(0); | |
| 128 target_rd = fdopen(target_fd, "r"); | 159 target_rd = fdopen(target_fd, "r"); |
| 129 if (!target_rd) { | 160 if (!target_rd) { |
| 130 perror("fdopen"); | 161 perror("fdopen"); |
| 131 exit(1); | 162 exit(1); |
| 132 } | 163 } |
| 164 bzero(&last_time, sizeof(struct timeval)); | |
| 133 | 165 |
| 134 while (read_command_input(command)) { | 166 while (read_command_input(command)) { |
| 135 if (!strcasecmp(command, "c+m")) { | 167 if (!strcasecmp(command, "c+m")) { |
| 136 cmd_with_msg = 1; | 168 cmd_with_msg = 1; |
| 137 if (!read_command_input(command)) | 169 if (!read_command_input(command)) |
| 138 break; | 170 break; |
| 139 if (!read_command_input(message)) | 171 if (!read_command_input(message)) |
| 140 break; | 172 break; |
| 141 } else | 173 } else |
| 142 cmd_with_msg = 0; | 174 cmd_with_msg = 0; |
| 175 if (wakeup_after_sec) { | |
| 176 gettimeofday(&curtime, 0); | |
| 177 timersub(&curtime, &last_time, &timediff); | |
| 178 if (timediff.tv_sec >= wakeup_after_sec) | |
| 179 wakeup_at(); | |
| 180 bcopy(&curtime, &last_time, sizeof(struct timeval)); | |
| 181 } | |
| 143 execute_command(); | 182 execute_command(); |
| 144 fflush(stdout); | 183 fflush(stdout); |
| 145 } | 184 } |
| 146 exit(0); | 185 exit(0); |
| 147 } | 186 } |
