FreeCalypso > hg > freecalypso-tools
comparison rvinterf/rvtat/main.c @ 346:99471c57155a
fcup-rvtat program written, compiles
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 04 Feb 2018 16:45:05 +0000 |
| parents | uptools/atinterf/fcup-atinterf.c@cc207d81c05f |
| children | e40bb5a6c6b9 |
comparison
equal
deleted
inserted
replaced
| 345:cc207d81c05f | 346:99471c57155a |
|---|---|
| 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 #include "pktmux.h" | |
| 8 #include "limits.h" | |
| 9 #include "localtypes.h" | |
| 10 #include "exitcodes.h" | |
| 11 | |
| 12 extern char *socket_pathname; | |
| 13 extern char *rvinterf_ttyport, *rvinterf_Bopt, *rvinterf_lopt, *rvinterf_wopt; | |
| 14 | |
| 15 extern u_char rvi_msg[]; | |
| 16 extern int rvi_msg_len; | |
| 17 | |
| 18 char command[256], message[256]; | |
| 19 int cmd_with_msg; | |
| 20 | |
| 21 read_command_input(buf) | |
| 22 char *buf; | |
| 23 { | |
| 24 char *nl; | |
| 25 | |
| 26 if (!fgets(buf, 256, stdin)) | |
| 27 return(0); | |
| 28 nl = index(buf, '\n'); | |
| 29 if (!nl) { | |
| 30 printf("Ecommand or message is too long\n"); | |
| 31 exit(1); | |
| 32 } | |
| 33 *nl = '\0'; | |
| 34 return(1); | |
| 35 } | |
| 36 | |
| 37 send_to_target(str) | |
| 38 char *str; | |
| 39 { | |
| 40 unsigned len; | |
| 41 u_char sendpkt[MAX_PKT_TO_TARGET+1]; | |
| 42 | |
| 43 len = strlen(str); | |
| 44 /* fill out the packet */ | |
| 45 sendpkt[0] = RVT_AT_HEADER; | |
| 46 strcpy(sendpkt + 1, str); | |
| 47 /* send it! */ | |
| 48 send_pkt_to_target(sendpkt, len + 1); | |
| 49 return(0); | |
| 50 } | |
| 51 | |
| 52 execute_command() | |
| 53 { | |
| 54 send_to_target(command); | |
| 55 if (cmd_with_msg) { | |
| 56 collect_pkt_from_target(); | |
| 57 if (rvi_msg_len != 4 || rvi_msg[2] != '>' || rvi_msg[3] != ' '){ | |
| 58 printf("F%.*s\n", rvi_msg_len - 2, rvi_msg + 2); | |
| 59 return; | |
| 60 } | |
| 61 send_to_target(message); | |
| 62 } | |
| 63 for (;;) { | |
| 64 collect_pkt_from_target(); | |
| 65 if (rvi_msg_len == 4 && !strncmp(rvi_msg + 2, "OK", 2) || | |
| 66 rvi_msg_len == 7 && !strncmp(rvi_msg + 2, "ERROR", 5) || | |
| 67 rvi_msg_len == 6 && !strncmp(rvi_msg + 2, "BUSY", 4) || | |
| 68 rvi_msg_len == 12 && | |
| 69 !strncmp(rvi_msg + 2, "NO CARRIER", 10) || | |
| 70 rvi_msg_len >= 12 && | |
| 71 !strncmp(rvi_msg + 2, "+CME ERROR", 10) || | |
| 72 rvi_msg_len >= 12 && | |
| 73 !strncmp(rvi_msg + 2, "+CMS ERROR", 10)) { | |
| 74 printf("F%.*s\n", rvi_msg_len - 2, rvi_msg + 2); | |
| 75 return; | |
| 76 } | |
| 77 printf("I%.*s\n", rvi_msg_len - 2, rvi_msg + 2); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 main(argc, argv) | |
| 82 char **argv; | |
| 83 { | |
| 84 extern int optind; | |
| 85 extern char *optarg; | |
| 86 int c, sopt = 0; | |
| 87 | |
| 88 while ((c = getopt(argc, argv, "B:l:p:s:w:")) != EOF) | |
| 89 switch (c) { | |
| 90 case 'B': | |
| 91 rvinterf_Bopt = optarg; | |
| 92 continue; | |
| 93 case 'l': | |
| 94 rvinterf_lopt = optarg; | |
| 95 continue; | |
| 96 case 'p': | |
| 97 rvinterf_ttyport = optarg; | |
| 98 continue; | |
| 99 case 's': | |
| 100 socket_pathname = optarg; | |
| 101 sopt++; | |
| 102 continue; | |
| 103 case 'w': | |
| 104 rvinterf_wopt = optarg; | |
| 105 continue; | |
| 106 case '?': | |
| 107 default: | |
| 108 /* error msg already printed */ | |
| 109 exit(ERROR_USAGE); | |
| 110 } | |
| 111 if (rvinterf_ttyport) { | |
| 112 if (sopt) { | |
| 113 fprintf(stderr, | |
| 114 "%s error: -p and -s options are mutually exclusive\n", | |
| 115 argv[0]); | |
| 116 exit(ERROR_USAGE); | |
| 117 } | |
| 118 launch_rvinterf(); | |
| 119 } else { | |
| 120 if (rvinterf_Bopt || rvinterf_lopt || rvinterf_wopt) { | |
| 121 fprintf(stderr, | |
| 122 "%s error: -B, -l and -w options are meaningful only when launching rvinterf\n", | |
| 123 argv[0]); | |
| 124 exit(ERROR_USAGE); | |
| 125 } | |
| 126 connect_local_socket(); | |
| 127 } | |
| 128 | |
| 129 while (read_command_input(command)) { | |
| 130 if (!strcasecmp(command, "c+m")) { | |
| 131 cmd_with_msg = 1; | |
| 132 if (!read_command_input(command)) | |
| 133 break; | |
| 134 if (!read_command_input(message)) | |
| 135 break; | |
| 136 } else | |
| 137 cmd_with_msg = 0; | |
| 138 rx_control(1); | |
| 139 execute_command(); | |
| 140 fflush(stdout); | |
| 141 } | |
| 142 exit(0); | |
| 143 } |
