FreeCalypso > hg > freecalypso-tools
comparison uptools/atcmd/smsend_main.c @ 366:9f856f843620
uptools/atcmd: smsend_basic.c renamed to smsend_main.c
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 08 Mar 2018 07:09:37 +0000 |
parents | uptools/atcmd/smsend_basic.c@4378d70b146b |
children | ed1ecc249eb3 |
comparison
equal
deleted
inserted
replaced
365:4378d70b146b | 366:9f856f843620 |
---|---|
1 /* | |
2 * This is the main module for the basic fcup-smsend utility. | |
3 */ | |
4 | |
5 #include <sys/types.h> | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include <unistd.h> | |
11 #include "../../rvinterf/include/exitcodes.h" | |
12 | |
13 #define MAX_MSG_CHARS 160 | |
14 | |
15 int sms_write_mode, text_mode, utf8_input; | |
16 u_char dest_addr[12]; | |
17 char msgtext[MAX_MSG_CHARS*2+2]; | |
18 u_char msgtext_gsm7[MAX_MSG_CHARS]; | |
19 unsigned msgtext_gsmlen; | |
20 | |
21 process_cmdline(argc, argv) | |
22 char **argv; | |
23 { | |
24 int c; | |
25 extern int optind; | |
26 | |
27 while ((c = getopt(argc, argv, "B:np:RtuwWX:")) != EOF) { | |
28 if (atinterf_cmdline_opt(c)) | |
29 continue; | |
30 switch (c) { | |
31 case 't': | |
32 text_mode = 1; | |
33 continue; | |
34 case 'u': | |
35 utf8_input = 1; | |
36 continue; | |
37 case 'w': | |
38 sms_write_mode = 1; | |
39 continue; | |
40 case 'W': | |
41 sms_write_mode = 2; | |
42 continue; | |
43 default: | |
44 /* error msg already printed */ | |
45 exit(ERROR_USAGE); | |
46 } | |
47 } | |
48 if (argc > optind + 2) { | |
49 fprintf(stderr, "usage: %s [options] dest-addr [message]\n", | |
50 argv[0]); | |
51 exit(ERROR_USAGE); | |
52 } | |
53 if (!argv[optind] || !argv[optind][0]) { | |
54 if (sms_write_mode == 2) { | |
55 dest_addr[0] = 0; | |
56 dest_addr[1] = 0x80; | |
57 } else { | |
58 fprintf(stderr, | |
59 "error: destination address must be specified\n"); | |
60 exit(ERROR_USAGE); | |
61 } | |
62 } else if (parse_and_encode_dest_addr(argv[optind], dest_addr) < 0) { | |
63 fprintf(stderr, | |
64 "error: destination address argument is invalid\n"); | |
65 exit(ERROR_USAGE); | |
66 } | |
67 if (!argv[optind+1]) | |
68 return(0); | |
69 if (strlen(argv[optind+1]) > MAX_MSG_CHARS*2) { | |
70 fprintf(stderr, "error: message argument is too long\n"); | |
71 exit(ERROR_USAGE); | |
72 } | |
73 strcpy(msgtext, argv[optind+1]); | |
74 return(1); | |
75 } | |
76 | |
77 read_msgtext_from_stdin() | |
78 { | |
79 unsigned pos, remain; | |
80 int cc; | |
81 | |
82 pos = 0; | |
83 remain = sizeof(msgtext); | |
84 for (;;) { | |
85 if (!remain) { | |
86 fprintf(stderr, | |
87 "error: message on stdin is too long\n"); | |
88 exit(ERROR_USAGE); | |
89 } | |
90 cc = read(0, msgtext + pos, remain); | |
91 if (cc < 0) { | |
92 fprintf(stderr, "error reading message from stdin\n"); | |
93 exit(ERROR_USAGE); | |
94 } | |
95 if (cc == 0) | |
96 break; | |
97 pos += cc; | |
98 remain -= cc; | |
99 } | |
100 msgtext[pos] = '\0'; | |
101 } | |
102 | |
103 trim_trailing_newlines() | |
104 { | |
105 char *cp; | |
106 | |
107 cp = index(msgtext, '\0'); | |
108 while (cp > msgtext && cp[-1] == '\n') | |
109 cp--; | |
110 *cp = '\0'; | |
111 } | |
112 | |
113 main(argc, argv) | |
114 char **argv; | |
115 { | |
116 int rc; | |
117 | |
118 if (!process_cmdline(argc, argv)) | |
119 read_msgtext_from_stdin(); | |
120 if (utf8_input && utf8_to_latin1(msgtext) < 0) { | |
121 fprintf(stderr, "error: invalid UTF-8 message\n"); | |
122 exit(ERROR_USAGE); | |
123 } | |
124 trim_trailing_newlines(); | |
125 if (text_mode) { | |
126 if (index(msgtext, '\n')) { | |
127 fprintf(stderr, | |
128 "error: multiline messages not supported in text mode\n"); | |
129 exit(ERROR_USAGE); | |
130 } | |
131 if (strlen(msgtext) > 160) { | |
132 toolong: fprintf(stderr, "error: message exceeds 160 chars\n"); | |
133 exit(ERROR_USAGE); | |
134 } | |
135 } else { | |
136 rc = latin1_to_gsm7(msgtext, msgtext_gsm7, 160, | |
137 &msgtext_gsmlen); | |
138 if (rc == -1) { | |
139 fprintf(stderr, | |
140 "error: message not valid for GSM7 charset\n"); | |
141 exit(ERROR_USAGE); | |
142 } | |
143 if (rc == -2) | |
144 goto toolong; | |
145 } | |
146 /* get to work */ | |
147 atinterf_init(); | |
148 /* enable verbose error messages */ | |
149 atinterf_exec_cmd_needok("AT+CMEE=2", 0, 0); | |
150 if (text_mode) { | |
151 prep_for_text_mode(); | |
152 send_in_text_mode(dest_addr, msgtext); | |
153 } else { | |
154 prep_for_pdu_mode(); | |
155 send_in_pdu_mode(dest_addr, msgtext_gsm7, msgtext_gsmlen, 0, 0); | |
156 } | |
157 if (sms_write_mode == 1) | |
158 sendafterwr_process(); | |
159 exit(0); | |
160 } |