Debian Codegraph @

apps/gsmsendsms.cc

raw symbols SCIP
1 // *************************************************************************
2 // * GSM TA/ME library
3 // *
4 // * File: gsmsendsms.cc
5 // *
6 // * Purpose: GSM sms send program
7 // *
8 // * Author: Peter Hofmann ([email protected])
9 // *
10 // * Created: 16.7.1999
11 // *************************************************************************
12
13 #ifdef HAVE_CONFIG_H
14 #include <gsm_config.h>
15 #endif
16 #include <gsmlib/gsm_nls.h>
17 #include <string>
18 #ifdef WIN32
19 #include <gsmlib/gsm_win32_serial.h>
20 #else
21 #include <gsmlib/gsm_unix_serial.h>
22 #include <unistd.h>
23 #endif
24 #if defined(HAVE_GETOPT_LONG) || defined(WIN32)
25 #include <getopt.h>
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <gsmlib/gsm_me_ta.h>
31 #include <gsmlib/gsm_util.h>
32 #include <iostream>
33
34 // options
35
36 #ifdef HAVE_GETOPT_LONG
37 static struct option longOpts[] =
38 {
39 {"requeststat", no_argument, (int*)NULL, 'r'},
40 {"xonxoff", no_argument, (int*)NULL, 'X'},
41 {"sca", required_argument, (int*)NULL, 'C'},
42 {"device", required_argument, (int*)NULL, 'd'},
43 {"init", required_argument, (int*)NULL, 'I'},
44 {"concatenate", required_argument, (int*)NULL, 'c'},
45 {"baudrate", required_argument, (int*)NULL, 'b'},
46 {"test", no_argument, (int*)NULL, 't'},
47 {"help", no_argument, (int*)NULL, 'h'},
48 {"version", no_argument, (int*)NULL, 'v'},
49 {(char*)NULL, 0, (int*)NULL, 0}
50 };
51 #else
52 #define getopt_long(argc, argv, options, longopts, indexptr) \
53 getopt(argc, argv, options)
54 #endif
55
56 // convert /r and /n to CR and LF
57
58 static std::string unescapeString(char *line)
59 {
60 std::string result;
61 bool escaped = false;
62 int index = 0;
63
64 while (line[index] != 0 &&
65 line[index] != gsmlib::CR && line[index] != gsmlib::LF)
66 {
67 if (escaped)
68 {
69 escaped = false;
70 if (line[index] == 'r')
71 result += gsmlib::CR;
72 else if (line[index] == 'n')
73 result += gsmlib::LF;
74 else if (line[index] == '\\')
75 result += '\\';
76 else
77 result += line[index];
78 }
79 else
80 if (line[index] == '\\')
81 escaped = true;
82 else
83 result += line[index];
84
85 ++index;
86 }
87 return result;
88 }
89
90 // *** main program
91
92 int main(int argc, char *argv[])
93 {
94 try
95 {
96 // handle command line options
97 std::string device = "/dev/mobilephone";
98 bool test = false;
99 std::string baudrate;
100 gsmlib::Ref<gsmlib::GsmAt> at;
101 std::string initString = gsmlib::DEFAULT_INIT_STRING;
102 bool swHandshake = false;
103 bool requestStatusReport = false;
104 // service centre address (set on command line)
105 std::string serviceCentreAddress;
106 gsmlib::MeTa *m = NULL;
107 std::string concatenatedMessageIdStr;
108 int concatenatedMessageId = -1;
109
110 int opt;
111 int dummy;
112 while((opt = getopt_long(argc, argv, "c:C:I:d:b:thvXr", longOpts, &dummy))
113 != -1)
114 switch (opt)
115 {
116 case 'c':
117 concatenatedMessageIdStr = optarg;
118 break;
119 case 'C':
120 serviceCentreAddress = optarg;
121 break;
122 case 'X':
123 swHandshake = true;
124 break;
125 case 'I':
126 initString = optarg;
127 break;
128 case 'd':
129 device = optarg;
130 break;
131 case 'b':
132 baudrate = optarg;
133 break;
134 case 't':
135 test = true;
136 break;
137 case 'r':
138 requestStatusReport = true;
139 break;
140 case 'v':
141 std::cerr << argv[0] << gsmlib::stringPrintf(_(": version %s [compiled %s]"),
142 VERSION, __DATE__) << std::endl;
143 exit(0);
144 break;
145 case 'h':
146 std::cerr << argv[0] << _(": [-b baudrate][-c concatenatedID]"
147 "[-C sca][-d device][-h][-I init string]\n"
148 " [-t][-v][-X] phonenumber [text]") << std::endl
149 << std::endl
150 << _(" -b, --baudrate baudrate to use for device "
151 "(default: 38400)")
152 << std::endl
153 << _(" -c, --concatenate ID for concatenated SMS messages")
154 << std::endl
155 << _(" -C, --sca SMS service centre address") << std::endl
156 << _(" -d, --device sets the destination device to connect "
157 "to") << std::endl
158 << _(" -h, --help prints this message") << std::endl
159 << _(" -I, --init device AT init sequence") << std::endl
160 << _(" -r, --requeststat request SMS status report") << std::endl
161 << _(" -t, --test convert text to GSM alphabet and "
162 "vice\n"
163 " versa, no SMS message is sent") << std::endl
164 << _(" -v, --version prints version and exits")
165 << std::endl
166 << _(" -X, --xonxoff switch on software handshake") << std::endl
167 << std::endl
168 << _(" phonenumber recipient's phone number") << std::endl
169 << _(" text optional text of the SMS message\n"
170 " if omitted: read from stdin")
171 << std::endl << std::endl;
172 exit(0);
173 break;
174 case '?':
175 throw gsmlib::GsmException(_("unknown option"), gsmlib::ParameterError);
176 break;
177 }
178
179 if (!test)
180 {
181 // open the port and ME/TA
182 gsmlib::Ref<gsmlib::Port> port = new
183 #ifdef WIN32
184 gsmlib::Win32SerialPort
185 #else
186 gsmlib::UnixSerialPort
187 #endif
188 (device,
189 baudrate == "" ? gsmlib::DEFAULT_BAUD_RATE :
190 gsmlib::baudRateStrToSpeed(baudrate),
191 initString, swHandshake);
192 // switch message service level to 1
193 // this enables acknowledgement PDUs
194 m = new gsmlib::MeTa(port);
195 m->setMessageService(1);
196
197 at = new gsmlib::GsmAt(*m);
198 }
199
200 // check parameters
201 if (optind == argc)
202 throw gsmlib::GsmException(_("phone number and text missing"), gsmlib::ParameterError);
203
204 if (optind + 2 < argc)
205 throw gsmlib::GsmException(_("more than two parameters given"), gsmlib::ParameterError);
206
207 if (concatenatedMessageIdStr != "")
208 concatenatedMessageId = gsmlib::checkNumber(concatenatedMessageIdStr);
209
210 // get phone number
211 std::string phoneNumber = argv[optind];
212
213 // get text
214 std::string text;
215 if (optind + 1 == argc)
216 { // read from stdin
217 char s[1000];
218 std::cin.get(s, 1000);
219 text = unescapeString(s);
220 if (text.length() > 160)
221 throw gsmlib::GsmException(_("text is larger than 160 characters"),
222 gsmlib::ParameterError);
223 }
224 else
225 text = argv[optind + 1];
226
227 if (test)
228 std::cout << gsmlib::gsmToLatin1(gsmlib::latin1ToGsm(text)) << std::endl;
229 else
230 {
231 // send SMS
232 gsmlib::Ref<gsmlib::SMSSubmitMessage> submitSMS = new gsmlib::SMSSubmitMessage();
233 // set service centre address in new submit PDU if requested by user
234 if (serviceCentreAddress != "")
235 {
236 gsmlib::Address sca(serviceCentreAddress);
237 submitSMS->setServiceCentreAddress(sca);
238 }
239 submitSMS->setStatusReportRequest(requestStatusReport);
240 gsmlib::Address destAddr(phoneNumber);
241 submitSMS->setDestinationAddress(destAddr);
242 if (concatenatedMessageId == -1)
243 m->sendSMSs(submitSMS, text, true);
244 else
245 m->sendSMSs(submitSMS, text, false, concatenatedMessageId);
246 }
247 }
248 catch (gsmlib::GsmException &ge)
249 {
250 std::cerr << argv[0] << _("[ERROR]: ") << ge.what() << std::endl;
251 return 1;
252 }
253 return 0;
254 }