Debian Codegraph @

apps/gsmctl.cc

raw symbols SCIP
1 // *************************************************************************
2 // * GSM TA/ME library
3 // *
4 // * File: gsmctl.cc
5 // *
6 // * Purpose: GSM mobile phone control program
7 // *
8 // * Author: Peter Hofmann ([email protected])
9 // *
10 // * Created: 11.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 #if defined(HAVE_GETOPT_LONG) || defined(WIN32)
19 #include <getopt.h>
20 #endif
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <gsmlib/gsm_me_ta.h>
25 #include <gsmlib/gsm_util.h>
26 #include <gsmlib/gsm_sysdep.h>
27 #ifdef WIN32
28 #include <gsmlib/gsm_win32_serial.h>
29 #else
30 #include <gsmlib/gsm_unix_serial.h>
31 #include <unistd.h>
32 #endif
33 #include <iostream>
34
35 // my ME
36
37 static gsmlib::MeTa *m;
38
39 // information parameters
40
41 enum InfoParameter {AllInfo, // print all info
42 MeInfo, // MeInfo must be first!
43 FunctionalityInfo,
44 OperatorInfo,
45 CurrentOperatorInfo,
46 FacilityLockStateInfo,
47 FacilityLockCapabilityInfo,
48 PasswordInfo,
49 PINInfo,
50 CLIPInfo,
51 CallForwardingInfo,
52 BatteryInfo,
53 BitErrorInfo,
54 SCAInfo,
55 CharSetInfo,
56 SignalInfo}; // SignalInfo must be last!
57
58 // operation parameters
59
60 // FIXME operations not implemented yet
61
62 // options
63
64 #ifdef HAVE_GETOPT_LONG
65 static struct option longOpts[] =
66 {
67 {"xonxoff", no_argument, (int*)NULL, 'X'},
68 {"operation", required_argument, (int*)NULL, 'o'},
69 {"device", required_argument, (int*)NULL, 'd'},
70 {"baudrate", required_argument, (int*)NULL, 'b'},
71 {"init", required_argument, (int*)NULL, 'I'},
72 {"help", no_argument, (int*)NULL, 'h'},
73 {"version", no_argument, (int*)NULL, 'v'},
74 {(char*)NULL, 0, (int*)NULL, 0}
75 };
76 #else
77 #define getopt_long(argc, argv, options, longopts, indexptr) \
78 getopt(argc, argv, options)
79 #endif
80
81 // helper function, prints forwarding info
82
83 void printForwardReason(std::string s, gsmlib::ForwardInfo &info)
84 {
85 std::cout << s << " "
86 << (info._active ? _("active ") : _("inactive "))
87 << _("number: ") << info._number
88 << _(" subaddr: ") << info._subAddr
89 << _(" time: ") << info._time << std::endl;
90 }
91
92 // print information
93
94 static void printInfo(InfoParameter ip)
95 {
96 switch (ip)
97 {
98 case MeInfo:
99 {
100 gsmlib::MEInfo mei = m->getMEInfo();
101 std::cout << _("<ME0> Manufacturer: ") << mei._manufacturer << std::endl
102 << _("<ME1> Model: ") << mei._model << std::endl
103 << _("<ME2> Revision: ") << mei._revision << std::endl
104 << _("<ME3> Serial Number: ") << mei._serialNumber << std::endl;
105 break;
106 }
107 case FunctionalityInfo:
108 {
109 try {
110 int fun;
111 fun = m->getFunctionalityLevel();
112 std::cout << _("<FUN> Functionality Level: ") << fun << std::endl;
113 } catch (gsmlib::GsmException &x) {
114 std::cout << _("<FUN> Functionality Level: ") << _("unsupported") << std::endl;
115 }
116 break;
117 }
118 case OperatorInfo:
119 {
120 int count = 0;
121 std::vector<gsmlib::OPInfo> opis = m->getAvailableOPInfo();
122 for (std::vector<gsmlib::OPInfo>::iterator i = opis.begin(); i != opis.end(); ++i)
123 {
124 std::cout << "<OP" << count << _("> Status: ");
125 switch (i->_status)
126 {
127 case gsmlib::UnknownOPStatus: std::cout << _("unknown"); break;
128 case gsmlib::CurrentOPStatus: std::cout << _("current"); break;
129 case gsmlib::AvailableOPStatus: std::cout << _("available"); break;
130 case gsmlib::ForbiddenOPStatus: std::cout << _("forbidden"); break;
131 }
132 std::cout << _(" Long name: '") << i->_longName << "' "
133 << _(" Short name: '") << i->_shortName << "' "
134 << _(" Numeric name: ") << i->_numericName << std::endl;
135 ++count;
136 }
137 break;
138 }
139 case CurrentOperatorInfo:
140 {
141 gsmlib::OPInfo opi = m->getCurrentOPInfo();
142 std::cout << "<CURROP0>"
143 << _(" Long name: '") << opi._longName << "' "
144 << _(" Short name: '") << opi._shortName << "' "
145 << _(" Numeric name: ") << opi._numericName
146 << _(" Mode: ");
147 switch (opi._mode)
148 {
149 case gsmlib::AutomaticOPMode: std::cout << _("automatic"); break;
150 case gsmlib::ManualOPMode: std::cout << _("manual"); break;
151 case gsmlib::DeregisterOPMode: std::cout << _("deregister"); break;
152 case gsmlib::ManualAutomaticOPMode: std::cout << _("manual/automatic"); break;
153 }
154 std::cout << std::endl;
155 break;
156 }
157 case FacilityLockStateInfo:
158 {
159 int count = 0;
160 std::vector<std::string> fclc = m->getFacilityLockCapabilities();
161 for (std::vector<std::string>::iterator i = fclc.begin(); i != fclc.end(); ++i)
162 if (*i != "AB" && *i != "AG" && *i != "AC")
163 {
164 std::cout << "<FLSTAT" << count << "> '" << *i << "'";
165 try
166 {
167 if (m->getFacilityLockStatus(*i, gsmlib::VoiceFacility))
168 std::cout << _(" Voice");
169 }
170 catch (gsmlib::GsmException &e)
171 {
172 std::cout << _(" unknown");
173 }
174 try
175 {
176 if (m->getFacilityLockStatus(*i, gsmlib::DataFacility))
177 std::cout << _(" Data");
178 }
179 catch (gsmlib::GsmException &e)
180 {
181 std::cout << _(" unknown");
182 }
183 try
184 {
185 if (m->getFacilityLockStatus(*i, gsmlib::FaxFacility))
186 std::cout << _(" Fax");
187 }
188 catch (gsmlib::GsmException &e)
189 {
190 std::cout << _(" unknown");
191 }
192 std::cout << std::endl;
193 ++count;
194 }
195 break;
196 }
197 case FacilityLockCapabilityInfo:
198 {
199 std::cout << "<FLCAP0> ";
200 std::vector<std::string> fclc = m->getFacilityLockCapabilities();
201 for (std::vector<std::string>::iterator i = fclc.begin(); i != fclc.end(); ++i)
202 std::cout << "'" << *i << "' ";
203 std::cout << std::endl;
204 break;
205 }
206 case PasswordInfo:
207 {
208 std::vector<gsmlib::PWInfo> pwi = m->getPasswords();
209 int count = 0;
210 for (std::vector<gsmlib::PWInfo>::iterator i = pwi.begin(); i != pwi.end(); ++i)
211 {
212 std::cout << "<PW" << count << "> '"
213 << i->_facility << "' " << i->_maxPasswdLen << std::endl;
214 ++count;
215 }
216 break;
217 }
218 case PINInfo:
219 {
220 std::cout << "<PIN0> " << m->getPINStatus() << std::endl;
221 break;
222 }
223 case CLIPInfo:
224 {
225 std::cout << "<CLIP0> " << (m->getNetworkCLIP() ? _("on") : _("off")) << std::endl;
226 break;
227 }
228 case CallForwardingInfo:
229 {
230 for (int r = 0; r < 4; ++r)
231 {
232 std::string text;
233 switch (r)
234 {
235 case 0: text = _("UnconditionalReason"); break;
236 case 1: text = _("MobileBusyReason"); break;
237 case 2: text = _("NoReplyReason"); break;
238 case 3: text = _("NotReachableReason"); break;
239 }
240 gsmlib::ForwardInfo voice, fax, data;
241 m->getCallForwardInfo((gsmlib::ForwardReason)r, voice, fax, data);
242 std::cout << "<FORW" << r << ".";
243 printForwardReason("0> " + text + _(" Voice"), voice);
244 std::cout << "<FORW" << r << ".";
245 printForwardReason("1> " + text + _(" Data"), data);
246 std::cout << "<FORW" << r << ".";
247 printForwardReason("2> " + text + _(" Fax"), fax);
248 }
249 break;
250 }
251 case BatteryInfo:
252 {
253 std::cout << "<BATT0> ";
254 int bcs = m->getBatteryChargeStatus();
255 switch (bcs)
256 {
257 case 0: std::cout << _("0 ME is powered by the battery") << std::endl; break;
258 case 1: std::cout << _("1 ME has a battery connected, but is not powered by it")
259 << std::endl; break;
260 case 2: std::cout << _("2 ME does not have a battery connected") << std::endl; break;
261 case 3:
262 std::cout << _("3 Recognized power fault, calls inhibited") << std::endl;
263 break;
264 }
265 std::cout << "<BATT1> " << m->getBatteryCharge() << std::endl;
266 break;
267 }
268 case BitErrorInfo:
269 {
270 std::cout << "<BITERR0> " << m->getBitErrorRate() << std::endl;
271 break;
272 }
273 case SCAInfo:
274 {
275 std::cout << "<SCA0> " << m->getServiceCentreAddress() << std::endl;
276 break;
277 }
278 case CharSetInfo:
279 {
280 std::cout << "<CSET0> ";
281 std::vector<std::string> cs = m->getSupportedCharSets();
282 for (std::vector<std::string>::iterator i = cs.begin(); i != cs.end(); ++i)
283 std::cout << "'" << *i << "' ";
284 std::cout << std::endl;
285 std::cout << "<CSET1> '" << m->getCurrentCharSet() << "'" << std::endl;
286 break;
287 }
288 case SignalInfo:
289 {
290 std::cout << "<SIG0> " << m->getSignalStrength() << std::endl;
291 break;
292 }
293 default:
294 assert(0);
295 break;
296 }
297 }
298
299 // convert facility class string of the form "", "all", or any combination
300 // of "v" (voice), "d" (data), or "f" (fax) to numeric form
301
302 gsmlib::FacilityClass strToFacilityClass(std::string facilityClassS)
303 {
304 facilityClassS = gsmlib::lowercase(facilityClassS);
305 gsmlib::FacilityClass facilityClass = (gsmlib::FacilityClass)0;
306 if (facilityClassS == "all" || facilityClassS == "")
307 return (gsmlib::FacilityClass)gsmlib::ALL_FACILITIES;
308
309 // OR in facility class bits
310 for (unsigned int i = 0; i < facilityClassS.length(); ++i)
311 if (facilityClassS[i] == 'v')
312 facilityClass = (gsmlib::FacilityClass)(facilityClass | gsmlib::VoiceFacility);
313 else if (facilityClassS[i] == 'd')
314 facilityClass = (gsmlib::FacilityClass)(facilityClass | gsmlib::DataFacility);
315 else if (facilityClassS[i] == 'f')
316 facilityClass = (gsmlib::FacilityClass)(facilityClass | gsmlib::FaxFacility);
317 else
318 throw gsmlib::GsmException(
319 gsmlib::stringPrintf(_("unknown facility class parameter '%c'"),
320 facilityClassS[i]), gsmlib::ParameterError);
321
322 return facilityClass;
323 }
324
325 // check if argc - optind is in range min..max
326 // throw exception otherwise
327
328 void checkParamCount(int optind, int argc, int min, int max)
329 {
330 int paramCount = argc - optind;
331 if (paramCount < min)
332 throw gsmlib::GsmException(gsmlib::stringPrintf(_("not enough parameters, minimum number "
333 "of parameters is %d"), min),
334 gsmlib::ParameterError);
335 else if (paramCount > max)
336 throw gsmlib::GsmException(gsmlib::stringPrintf(_("too many parameters, maximum number "
337 "of parameters is %d"), max),
338 gsmlib::ParameterError);
339 }
340
341 // *** main program
342
343 int main(int argc, char *argv[])
344 {
345 try
346 {
347 // handle command line options
348 std::string device = "/dev/mobilephone";
349 std::string operation;
350 std::string baudrate;
351 std::string initString = gsmlib::DEFAULT_INIT_STRING;
352 bool swHandshake = false;
353
354 int opt;
355 int dummy;
356 while((opt = getopt_long(argc, argv, "I:o:d:b:hvX", longOpts, &dummy))
357 != -1)
358 switch (opt)
359 {
360 case 'X':
361 swHandshake = true;
362 break;
363 case 'I':
364 initString = optarg;
365 break;
366 case 'd':
367 device = optarg;
368 break;
369 case 'o':
370 operation = optarg;
371 break;
372 case 'b':
373 baudrate = optarg;
374 break;
375 case 'v':
376 std::cerr << argv[0] << gsmlib::stringPrintf(_(": version %s [compiled %s]"),
377 VERSION, __DATE__) << std::endl;
378 exit(0);
379 break;
380 case 'h':
381 std::cerr << argv[0] << _(": [-b baudrate][-d device][-h]"
382 "[-I init string][-o operation]\n"
383 " [-v][-X]{parameters}") << std::endl
384 << std::endl
385 << _(" -b, --baudrate baudrate to use for device "
386 "(default: 38400)")
387 << std::endl
388 << _(" -d, --device sets the destination device to "
389 "connect to") << std::endl
390 << _(" -h, --help prints this message") << std::endl
391 << _(" -I, --init device AT init sequence") << std::endl
392 << _(" -o, --operation operation to perform on the mobile \n"
393 " phone with the specified parameters")
394 << std::endl
395 << _(" -v, --version prints version and exits") << std::endl
396 << _(" -X, --xonxoff switch on software handshake") << std::endl
397 << std::endl
398 << _(" parameters parameters to use for the operation\n"
399 " (if an operation is given) or\n"
400 " a specification which kind of\n"
401 " information to read from the mobile "
402 "phone")
403 << std::endl << std::endl
404 << _("Refer to gsmctl(1) for details on the available parameters"
405 " and operations.")
406 << std::endl << std::endl;
407 exit(0);
408 break;
409 case '?':
410 throw gsmlib::GsmException(_("unknown option"), gsmlib::ParameterError);
411 break;
412 }
413
414 // open the port and ME/TA
415 m = new gsmlib::MeTa(new
416 #ifdef WIN32
417 gsmlib::Win32SerialPort
418 #else
419 gsmlib::UnixSerialPort
420 #endif
421 (device,
422 baudrate == "" ?
423 gsmlib::DEFAULT_BAUD_RATE :
424 gsmlib::baudRateStrToSpeed(baudrate),
425 initString, swHandshake));
426
427 if (operation == "")
428 { // process info parameters
429 for (int i = optind; i < argc; ++i)
430 {
431 std::string param = gsmlib::lowercase(argv[i]);
432 if (param == "all")
433 for (int ip = MeInfo; ip <= SignalInfo; ++ip)
434 printInfo((InfoParameter)ip);
435 else if (param == "me")
436 printInfo(MeInfo);
437 else if (param == "fun")
438 printInfo(FunctionalityInfo);
439 else if (param == "op")
440 printInfo(OperatorInfo);
441 else if (param == "currop")
442 printInfo(CurrentOperatorInfo);
443 else if (param == "flstat")
444 printInfo(FacilityLockStateInfo);
445 else if (param == "flcap")
446 printInfo(FacilityLockCapabilityInfo);
447 else if (param == "pw")
448 printInfo(PasswordInfo);
449 else if (param == "pin")
450 printInfo(PINInfo);
451 else if (param == "clip")
452 printInfo(CLIPInfo);
453 else if (param == "forw")
454 printInfo(CallForwardingInfo);
455 else if (param == "batt")
456 printInfo(BatteryInfo);
457 else if (param == "biterr")
458 printInfo(BitErrorInfo);
459 else if (param == "sig")
460 printInfo(SignalInfo);
461 else if (param == "sca")
462 printInfo(SCAInfo);
463 else if (param == "cset")
464 printInfo(CharSetInfo);
465 else
466 throw gsmlib::GsmException(
467 gsmlib::stringPrintf(_("unknown information parameter '%s'"),
468 param.c_str()),
469 gsmlib::ParameterError);
470 }
471 }
472 else
473 { // process operation
474 operation = gsmlib::lowercase(operation);
475 if (operation == "dial")
476 {
477 // dial: number
478 checkParamCount(optind, argc, 1, 1);
479
480 m->dial(argv[optind]);
481
482 // wait for keypress from stdin
483 char c;
484 read(1, &c, 1);
485 }
486 else if (operation == "on")
487 {
488 m->setFunctionalityLevel(1);
489 }
490 else if (operation == "off")
491 {
492 m->setFunctionalityLevel(0);
493 }
494 else if (operation == "pin")
495 {
496 // pin: PIN
497 checkParamCount(optind, argc, 1, 1);
498
499 m->setPIN(argv[optind]);
500 }
501 else if (operation == "setop")
502 {
503 // setop: opmode numeric FIXME allow long and numeric too
504 checkParamCount(optind, argc, 2, 2);
505 std::string opmodeS = gsmlib::lowercase(argv[optind]);
506 gsmlib::OPModes opmode;
507 if (opmodeS == "automatic")
508 opmode = gsmlib::AutomaticOPMode;
509 else if (opmodeS == "manual")
510 opmode = gsmlib::ManualOPMode;
511 else if (opmodeS == "deregister")
512 opmode = gsmlib::DeregisterOPMode;
513 else if (opmodeS == "manualautomatic")
514 opmode = gsmlib::ManualAutomaticOPMode;
515 else
516 throw gsmlib::GsmException(gsmlib::stringPrintf(_("unknown opmode parameter '%s'"),
517 opmodeS.c_str()), gsmlib::ParameterError);
518
519 m->setCurrentOPInfo(opmode, "" , "", gsmlib::checkNumber(argv[optind + 1]));
520 }
521 else if (operation == "lock")
522 {
523 // lock: facility [facilityclass] [passwd]
524 checkParamCount(optind, argc, 1, 3);
525 std::string passwd = (argc - optind == 3) ?
526 (std::string)argv[optind + 2] : (std::string)"";
527
528 m->lockFacility(argv[optind],
529 (argc - optind >= 2) ?
530 strToFacilityClass(argv[optind + 1]) :
531 (gsmlib::FacilityClass)gsmlib::ALL_FACILITIES,
532 passwd);
533 }
534 else if (operation == "unlock")
535 {
536 // unlock: facility [facilityclass] [passwd]
537 checkParamCount(optind, argc, 1, 3);
538 std::string passwd = argc - optind == 3 ? argv[optind + 2] : "";
539
540 m->unlockFacility(argv[optind],
541 (argc - optind >= 2) ?
542 strToFacilityClass(argv[optind + 1]) :
543 (gsmlib::FacilityClass)gsmlib::ALL_FACILITIES,
544 passwd);
545 }
546 else if (operation == "setpw")
547 {
548 // set password: facility oldpasswd newpasswd
549 checkParamCount(optind, argc, 1, 3);
550 std::string oldPasswd = argc - optind >= 2 ? argv[optind + 1] : "";
551 std::string newPasswd = argc - optind == 3 ? argv[optind + 2] : "";
552
553 m->setPassword(argv[optind], oldPasswd, newPasswd);
554 }
555 else if (operation == "forw")
556 {
557 // call forwarding: mode reason number [facilityclass] [forwardtime]
558 checkParamCount(optind, argc, 2, 5);
559
560 // get optional parameters facility class and forwardtime
561 int forwardTime = argc - optind == 5 ? gsmlib::checkNumber(argv[optind + 4]) :
562 gsmlib::NOT_SET;
563 gsmlib::FacilityClass facilityClass =
564 argc - optind >= 4 ? strToFacilityClass(argv[optind + 3]) :
565 (gsmlib::FacilityClass)gsmlib::ALL_FACILITIES;
566
567 // get forward reason
568 std::string reasonS = gsmlib::lowercase(argv[optind + 1]);
569 gsmlib::ForwardReason reason;
570 if (reasonS == "unconditional")
571 reason = gsmlib::UnconditionalReason;
572 else if (reasonS == "mobilebusy")
573 reason = gsmlib::MobileBusyReason;
574 else if (reasonS == "noreply")
575 reason = gsmlib::NoReplyReason;
576 else if (reasonS == "notreachable")
577 reason = gsmlib::NotReachableReason;
578 else if (reasonS == "all")
579 reason = gsmlib::AllReasons;
580 else if (reasonS == "allconditional")
581 reason = gsmlib::AllConditionalReasons;
582 else
583 throw gsmlib::GsmException(
584 gsmlib::stringPrintf(_("unknown forward reason parameter '%s'"),
585 reasonS.c_str()), gsmlib::ParameterError);
586
587 // get mode
588 std::string modeS = gsmlib::lowercase(argv[optind]);
589 gsmlib::ForwardMode mode;
590 if (modeS == "disable")
591 mode = gsmlib::DisableMode;
592 else if (modeS == "enable")
593 mode = gsmlib::EnableMode;
594 else if (modeS == "register")
595 mode = gsmlib::RegistrationMode;
596 else if (modeS == "erase")
597 mode = gsmlib::ErasureMode;
598 else
599 throw gsmlib::GsmException(
600 gsmlib::stringPrintf(_("unknown forward mode parameter '%s'"),
601 modeS.c_str()), gsmlib::ParameterError);
602
603 m->setCallForwarding(reason, mode,
604 (argc - optind >= 3) ? argv[optind + 2] : "",
605 "", // subaddr
606 facilityClass, forwardTime);
607 }
608 else if (operation == "setsca")
609 {
610 // set sca: number
611 checkParamCount(optind, argc, 1, 1);
612 m->setServiceCentreAddress(argv[optind]);
613 }
614 else if (operation == "cset")
615 {
616 // set charset: string
617 checkParamCount(optind, argc, 1, 1);
618 m->setCharSet(argv[optind]);
619 }
620 else
621 throw gsmlib::GsmException(gsmlib::stringPrintf(_("unknown operation '%s'"),
622 operation.c_str()), gsmlib::ParameterError);
623 }
624 }
625 catch (gsmlib::GsmException &ge)
626 {
627 std::cerr << argv[0] << _("[ERROR]: ") << ge.what() << std::endl;
628 return 1;
629 }
630 return 0;
631 }