~ubuntu-branches/ubuntu/dapper/gsmlib/dapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
// *************************************************************************
// * GSM TA/ME library
// *
// * File:    gsm_me_ta.cc
// *
// * Purpose: Mobile Equipment/Terminal Adapter functions
// *          (ETSI GSM 07.07)
// *
// * Author:  Peter Hofmann (software@pxh.de)
// *
// * Created: 10.5.1999
// *************************************************************************

#ifdef HAVE_CONFIG_H
#include <gsm_config.h>
#endif
#include <gsmlib/gsm_nls.h>
#include <gsmlib/gsm_me_ta.h>
#include <gsmlib/gsm_parser.h>
#include <gsmlib/gsm_sysdep.h>

using namespace std;
using namespace gsmlib;

// Capabilities members

Capabilities::Capabilities() :
  _hasSMSSCAprefix(true),
  _cpmsParamCount(-1),          // initialize to -1, must be set later by
                                // setSMSStore() function
  _omitsColon(true),            // FIXME
  _veryShortCOPSanswer(false),  // Falcom A2-1
  _wrongSMSStatusCode(false),   // Motorola Timeport 260
  _CDSmeansCDSI(false),         // Nokia Cellular Card Phone RPE-1 GSM900 and
                                // Nokia Card Phone RPM-1 GSM900/1800
  _sendAck(false)               // send ack for directly routed SMS
{
}

// MeTa members

void MeTa::init() throw(GsmException)
{
  // switch on extended error codes
  // caution: may be ignored by some TAs, so allow it to fail
  _at->chat("+CMEE=1", "", true, true);
  
  // select SMS pdu mode
  _at->chat("+CMGF=0");

  // now fill in capability object
  MEInfo info = getMEInfo();
  
  // Ericsson model 6050102
  if ((info._manufacturer == "ERICSSON" &&
      (info._model == "1100801" ||
       info._model == "1140801")) ||
      getenv("GSMLIB_SH888_FIX") != NULL)
  {
    // the Ericsson leaves out the service centre address
    _capabilities._hasSMSSCAprefix = false;
  }

  // handle Falcom strangeness
  if ((info._manufacturer == "Funkanlagen Leipoldt OHG" &&
      info._revision == "01.95.F2") ||
      getenv("GSMLIB_FALCOM_A2_1_FIX") != NULL)
  {
    _capabilities._veryShortCOPSanswer = true;
  }

  // handle Motorola SMS store bug - wrong status code
  if ((info._manufacturer == "Motorola" &&
       info._model == "L Series"))
  {
    _capabilities._wrongSMSStatusCode = true;
  } 
 
  // handle Nokia Cellular Card Phone RPE-1 GSM900 and
  // Nokia Card Phone RPM-1 GSM900/1800 bug - CDS means CDSI
  if ((info._manufacturer == "Nokia Mobile Phones" &&
       (info._model == "Nokia Cellular Card Phone RPE-1 GSM900" ||
        info._model == "Nokia Card Phone RPM-1 GSM900/1800")))
  {
    _capabilities._CDSmeansCDSI = true;
  } 

  // find out whether we are supposed to send an acknowledgment
  Parser p(_at->chat("+CSMS?", "+CSMS:"));
  _capabilities._sendAck = p.parseInt() >= 1;
      
  // set GSM default character set
  try
  {
    setCharSet("GSM");
  }
  catch (GsmException)
  {
    // ignore errors, some devices don't support this
  }

  // set default event handler
  // necessary to handle at least RING indications that might
  // otherwise confuse gsmlib
  _at->setEventHandler(&_defaultEventHandler);
}

MeTa::MeTa(Ref<Port> port) throw(GsmException) : _port(port)
{
  // initialize AT handling
  _at = new GsmAt(*this);

  init();
}

// MeTa::MeTa(Ref<GsmAt> at) throw(GsmException) :
//   _at(at)
// {
//   init();
// }

void MeTa::setPIN(string pin) throw(GsmException)
{
  _at->chat("+CPIN=\"" + pin + "\"");
}

string MeTa::getPINStatus() throw(GsmException)
{
  Parser p(_at->chat("+CPIN?", "+CPIN:"));
  return p.parseString();
}

void MeTa::setPhonebook(string phonebookName) throw(GsmException)
{
  if (phonebookName != _lastPhonebookName)
  {
    _at->chat("+CPBS=\"" + phonebookName + "\"");
    _lastPhonebookName = phonebookName;
  }
}

string MeTa::setSMSStore(string smsStore, int storeTypes, bool needResultCode)
  throw(GsmException)
{
  if (_capabilities._cpmsParamCount == -1)
  {
    // count the number of parameters for the CPMS AT sequences
    _capabilities._cpmsParamCount = 1;
    Parser p(_at->chat("+CPMS=?", "+CPMS:"));
    p.parseStringList();
    while (p.parseComma(true))
    {
      ++_capabilities._cpmsParamCount;
      p.parseStringList();
    }
  }

  // optimatization: only set current SMS store if different from last call
  // or the result code is needed
  if (needResultCode || _lastSMSStoreName != smsStore)
  {
    _lastSMSStoreName = smsStore;

    // build chat string
    string chatString = "+CPMS=\"" + smsStore + "\"";
    for (int i = 1; i < min(_capabilities._cpmsParamCount, storeTypes); ++i)
      chatString += ",\"" + smsStore + "\"";

    return _at->chat(chatString, "+CPMS:");
  }
  return "";
}

void MeTa::getSMSStore(string &readDeleteStore,
                       string &writeSendStore,
                       string &receiveStore) throw(GsmException)
{
  Parser p(_at->chat("+CPMS?", "+CPMS:"));
  writeSendStore = receiveStore = "";
  readDeleteStore = p.parseString();
  p.parseComma();
  p.parseInt();
  p.parseComma();
  p.parseInt();
  if (p.parseComma(true))
  {
    writeSendStore = p.parseString();
    p.parseComma();
    p.parseInt();
    p.parseComma();
    p.parseInt();
    if (p.parseComma(true))
    {
      receiveStore = p.parseString();
    }
  }
}

void MeTa::waitEvent(GsmTime timeout) throw(GsmException)
{
  if (_at->wait(timeout))
    _at->chat();                // send AT, wait for OK, handle events
}

// aux function for MeTa::getMEInfo()

static string stringVectorToString(const vector<string>& v,
                                   char separator = '\n')
{
  if (v.empty())
    return "";

  // concatenate string in vector as rows
  string result;
  for (vector<string>::const_iterator i = v.begin();;)
  {
    string s = *i;
    // remove leading and trailing "s
    if (s.length() > 0 && s[0] == '"')
      s.erase(s.begin());
    if (s.length() > 0 && s[s.length() - 1] == '"')
      s.erase(s.end() - 1);

    result += s;
    // don't add end line to last
    if ( ++i == v.end() || !separator)
      break;
    result += separator;
  }
  return result;
}

MEInfo MeTa::getMEInfo() throw(GsmException)
{
  MEInfo result;
  // some TAs just return OK and no info line
  // leave the info empty in this case
  // some TAs return multirows with info like address, firmware version
  result._manufacturer =
    stringVectorToString(_at->chatv("+CGMI", "+CGMI:", false));
  result._model = stringVectorToString(_at->chatv("+CGMM", "+CGMM:", false));
  result._revision =
    stringVectorToString(_at->chatv("+CGMR", "+CGMR:", false));
  result._serialNumber =
    stringVectorToString(_at->chatv("+CGSN", "+CGSN:", false),0);
  return result;
}

vector<string> MeTa::getSupportedCharSets() throw(GsmException)
{
  Parser p(_at->chat("+CSCS=?", "+CSCS:"));
  return p.parseStringList();
}
    
string MeTa::getCurrentCharSet() throw(GsmException)
{
  if (_lastCharSet == "")
  {
    Parser p(_at->chat("+CSCS?", "+CSCS:"));
    _lastCharSet = p.parseString();
  }
  return _lastCharSet;
}

void MeTa::setCharSet(string charSetName) throw(GsmException)
{
  _at->chat("+CSCS=\"" + charSetName + "\"");
  _lastCharSet = "";
}

string MeTa::getExtendedErrorReport() throw(GsmException)
{
  return _at->chat("+CEER", "+CEER:");
}

void MeTa::dial(string number) throw(GsmException)
{
  _at->chat("D" + number + ";");
}

void MeTa::answer() throw(GsmException)
{
  _at->chat("A");
}

void MeTa::hangup() throw(GsmException)
{
  _at->chat("H");

}

vector<OPInfo> MeTa::getAvailableOPInfo() throw(GsmException)
{
  vector<OPInfo> result;
  vector<string> responses = _at->chatv("+COPS=?", "+COPS:");

  // special treatment for Falcom A2-1, answer looks like
  //   responses.push_back("(1,29341),(3,29340)");
  if (_capabilities._veryShortCOPSanswer)
  {
    if (responses.size() == 1)
    {
      Parser p(responses[0]);
      while (p.parseChar('(', true))
      {
        OPInfo opi;
        opi._status = (OPStatus)p.parseInt();
        p.parseComma();
        opi._numericName = p.parseInt();
        p.parseChar(')');
        p.parseComma(true);
        result.push_back(opi);
      }
    }
  }
  else
    // some formats I have encountered...
    //responses.push_back("2,,,31017,,(0,1),(2)");
    //responses.push_back("(3,\"UK CELLNET\",\"CLNET\",\"23410\")," 
    //                    "(3,\"ONE2 ONE\",\"ONE2ONE\",\"23430\"),"
    //                    "(3,\"ORANGE\",\"ORANGE\",\"23433\")");
    //responses.push_back("(2,\"D1-TELEKOM\",,26201),"
    //                    "(3,\"D2  PRIVAT\",,26202),,(0,1,3,4),(0,2)");
    // some phones arbitrarily split the response into several lines
    //responses.push_back("(1,\"AMENA\",,\"21403\"),"
    //                    "(3,\"MOVISTAR\",,\"21407\"),");
    //responses.push_back("(3,\"E VODAFONE\",,\"21401\"),,(0,1),(2)");

    // GSM modems might return
    // 1. quadruplets of info enclosed in brackets separated by comma
    // 2. several lines of quadruplets of info enclosed in brackets
    // 3. several lines of quadruplets without brackets and additional
    //    info at EOL (e.g. Nokia 8290)
    for (vector<string>::iterator i = responses.begin();
         i != responses.end(); ++i)
    {
//       while (i->length() > 0 && ! isprint((*i)[i->length() - 1]))
//         i->erase(i->length() - 1, 1);

      bool expectClosingBracket = false;
      Parser p(*i);
      while (1)
      {
        OPInfo opi;
        expectClosingBracket = p.parseChar('(', true);
        int status = p.parseInt(true);
        opi._status = (status == NOT_SET ? UnknownOPStatus : (OPStatus)status);
        p.parseComma();
        opi._longName = p.parseString(true);
        p.parseComma();
        opi._shortName = p.parseString(true);
        p.parseComma();
        try
        {
          opi._numericName = p.parseInt(true);
        }
        catch (GsmException &e)
        {
          if (e.getErrorClass() == ParserError)
          {
            // the Ericsson GM12 GSM modem returns the numeric ID as string
            string s = p.parseString();
            opi._numericName = checkNumber(s);
          }
          else
            throw e;
        }
        if (expectClosingBracket) p.parseChar(')');
        result.push_back(opi);
        if (! p.parseComma(true)) break;
        // two commas ",," mean the list is finished
        if (p.getEol() == "" || p.parseComma(true)) break;
      }
      // without brackets, the ME/TA must use format 3.
      if (! expectClosingBracket) break;
    }
  return result;
}

OPInfo MeTa::getCurrentOPInfo() throw(GsmException)
{
  OPInfo result;

  // 1. This exception thing is necessary because not all ME/TA combinations
  // might support all the formats and then return "ERROR".
  // 2. Additionally some modems return "ERROR" for all "COPS=3,n" command
  // and report only one format with the "COPS?" command (e.g. Nokia 8290).

  // get long format
  try
  {
    try
    {
      _at->chat("+COPS=3,0");
    }
    catch (GsmException &e)
    {
      if (e.getErrorClass() != ChatError) throw;
    }
    Parser p(_at->chat("+COPS?", "+COPS:"));
    result._mode = (OPModes)p.parseInt();
    // some phones (e.g. Nokia Card Phone 2.0) just return "+COPS: 0"
    // if no network connection
    if (p.parseComma(true))
    {
      if (p.parseInt() == 0)
      {
        p.parseComma();
        result._longName = p.parseString();
      }
    }
  }
  catch (GsmException &e)
  {
    if (e.getErrorClass() != ChatError) throw;
  }

  // get short format
  try
  {
    try
    {
      _at->chat("+COPS=3,1");
    }
    catch (GsmException &e)
    {
      if (e.getErrorClass() != ChatError) throw;
    }
    Parser p(_at->chat("+COPS?", "+COPS:"));
    result._mode = (OPModes)p.parseInt();
    // some phones (e.g. Nokia Card Phone 2.0) just return "+COPS: 0"
    // if no network connection
    if (p.parseComma(true))
    {
      if (p.parseInt() == 1)
      {
        p.parseComma();
        result._shortName = p.parseString();
      }
    }
  }
  catch (GsmException &e)
  {
    if (e.getErrorClass() != ChatError) throw;
  }

  // get numeric format
  try
  {
    try
    {
      _at->chat("+COPS=3,2");
    }
    catch (GsmException &e)
    {
      if (e.getErrorClass() != ChatError) throw;
    }
    Parser p(_at->chat("+COPS?", "+COPS:"));
    result._mode = (OPModes)p.parseInt();
    // some phones (e.g. Nokia Card Phone 2.0) just return "+COPS: 0"
    // if no network connection
    if (p.parseComma(true))
    {
      if (p.parseInt() == 2)
      {
        p.parseComma();
        try
        {
          result._numericName = p.parseInt();
        }
        catch (GsmException &e)
        {
          if (e.getErrorClass() == ParserError)
          {
            // the Ericsson GM12 GSM modem returns the numeric ID as string
            string s = p.parseString();
            result._numericName = checkNumber(s);
          }
          else
            throw e;
        }
      }
    }
  }
  catch (GsmException &e)
  {
    if (e.getErrorClass() != ChatError) throw;
  }
  return result;
}

void MeTa::setCurrentOPInfo(OPModes mode,
                            string longName,
                            string shortName,
                            int numericName) throw(GsmException)
{
  bool done = false;
  if (longName != "")
  {
    try
    {
      _at->chat("+COPS=" + intToStr((int)mode) + ",0,\"" + longName + "\"");
      done = true;
    }
    catch (GsmException &e)
    {
      if (e.getErrorClass() != ChatError) throw;
    }
  }
  if (shortName != "" && ! done)
  {
    try
    {
      _at->chat("+COPS=" + intToStr((int)mode) + ",1,\"" + shortName + "\"");
      done = true;
    }
    catch (GsmException &e)
    {
      if (e.getErrorClass() != ChatError) throw;
    }
  }
  if (numericName != NOT_SET && ! done)
  {
    try
    {
      _at->chat("+COPS=" + intToStr((int)mode) + ",2," +
                intToStr(numericName));
      done = true;
    }
    catch (GsmException &e)
    {
      if (e.getErrorClass() != ChatError) throw;
    }
  }
  if (! done)
    throw GsmException(_("unable to set operator"), OtherError);
}

vector<string> MeTa::getFacilityLockCapabilities() throw(GsmException)
{
  string locks = _at->chat("+CLCK=?", "+CLCK:");
  // some TA don't add '(' and ')' (Option FirstFone)
  if (locks.length() && locks[0] != '(')
  {
    locks.insert(locks.begin(),'(');
    locks += ')';
  }
  Parser p(locks);
  return p.parseStringList();
}

bool MeTa::getFacilityLockStatus(string facility, FacilityClass cl)
  throw(GsmException)
{
  // some TA return always multiline response with all classes
  // (Option FirstFone)
  // !!! errors handling is correct (responses.empty() true) ?
  vector<string> responses = 
    _at->chatv("+CLCK=\"" + facility + "\",2,," + intToStr((int)cl),"+CLCK:",true);
  for (vector<string>::iterator i = responses.begin();
       i != responses.end(); ++i)
  {
    Parser p(*i);
    int enabled = p.parseInt();

    // if the first time and there is no comma this 
    // return direct state of classes
    // else return all classes
    if (i == responses.begin())
    {
      if (!p.parseComma(true))
        return enabled == 1;
    }
    else
      p.parseComma();

    if ( p.parseInt() == (int)cl )
      return enabled == 1;
  }
  return false;

//  Parser p(_at->chat("+CLCK=\"" + facility + "\",2,," + intToStr((int)cl),
//                     "+CLCK:"));
//  return p.parseInt() == 1;
}

void MeTa::lockFacility(string facility, FacilityClass cl, string passwd)
  throw(GsmException)
{
  if (passwd == "")
    _at->chat("+CLCK=\"" + facility + "\",1,," + intToStr((int)cl));
  else
    _at->chat("+CLCK=\"" + facility + "\",1,\"" + passwd + "\","
              + intToStr((int)cl));
}

void MeTa::unlockFacility(string facility, FacilityClass cl, string passwd)
  throw(GsmException)
{
  if (passwd == "")
    _at->chat("+CLCK=\"" + facility + "\",0,," + intToStr((int)cl));
  else
    _at->chat("+CLCK=\"" + facility + "\",0,\"" + passwd + "\","
              + intToStr((int)cl));
}

vector<PWInfo> MeTa::getPasswords() throw(GsmException)
{
  vector<PWInfo> result;
  Parser p(_at->chat("+CPWD=?", "+CPWD:"));
  while (1)
  {
    PWInfo pwi;
    if (!p.parseChar('(', true)) break; // exit if no new tuple
    pwi._facility = p.parseString();
    p.parseComma();
    pwi._maxPasswdLen = p.parseInt();
    p.parseChar(')');
    p.parseComma(true);
    result.push_back(pwi);
  }
  return result;
}

void MeTa::setPassword(string facility, string oldPasswd, string newPasswd)
  throw(GsmException)
{
  _at->chat("+CPWD=\"" + facility + "\",\"" + oldPasswd + "\",\"" +
            newPasswd + "\"");
}

bool MeTa::getNetworkCLIP() throw(GsmException)
{
  Parser p(_at->chat("+CLIP?", "+CLIP:"));
  p.parseInt();                 // ignore result code presentation
  p.parseComma();
  return p.parseInt() == 1;
}

void MeTa::setCLIPPresentation(bool enable) throw(GsmException)
{
  if (enable)
    _at->chat("+CLIP=1");
  else
    _at->chat("+CLIP=0");
}

bool MeTa::getCLIPPresentation() throw(GsmException)
{
  Parser p(_at->chat("+CLIP?", "+CLIP:"));
  return p.parseInt() == 1;     // ignore rest of line
}

void MeTa::setCallForwarding(ForwardReason reason,
                             ForwardMode mode,
                             string number,
                             string subaddr,
                             FacilityClass cl,
                             int forwardTime) throw(GsmException)
{
  // FIXME subaddr is currently ignored
  if (forwardTime != NOT_SET && (forwardTime < 0 || forwardTime > 30))
    throw GsmException(_("call forward time must be in the range 0..30"),
                       ParameterError);
  
  int numberType;
  number = removeWhiteSpace(number);
  if (number.length() > 0 && number[0] == '+')
  {
    numberType = InternationalNumberFormat;
    number = number.substr(1);  // skip the '+' at the beginning
  }
  else
    numberType = UnknownNumberFormat;
  _at->chat("+CCFC=" + intToStr(reason) + "," +  intToStr(mode) + "," 
            "\"" + number + "\"," +
            (number.length() > 0 ? intToStr(numberType) : "") +
            "," +  intToStr(cl) +
                                // FIXME subaddr and type
            (forwardTime == NOT_SET ? "" :
             (",,," + intToStr(forwardTime))));
}
                           
void MeTa::getCallForwardInfo(ForwardReason reason,
                              ForwardInfo &voice,
                              ForwardInfo &fax,
                              ForwardInfo &data) throw(GsmException)
{
  // Initialize to some sensible values:
  voice._active = false;
  voice._cl = VoiceFacility;
  voice._time = -1;
  voice._reason = NoReason;
  data._active = false;
  data._cl = DataFacility;
  data._time = -1;
  data._reason = NoReason;
  fax._active = false;
  fax._cl = FaxFacility;
  fax._time = -1;
  fax._reason = NoReason;

  vector<string> responses =
    _at->chatv("+CCFC=" + intToStr(reason) + ",2", "+CCFC:");
  if (responses.size() == 1)
  {
    // only one line was returned. We have to ask for all three classes
    // (voice, data, fax) separately
    responses.clear();
    responses.push_back(_at->chat("+CCFC=" + intToStr(reason) +
                                  ",2,,,1", "+CCFC:"));
    responses.push_back(_at->chat("+CCFC=" + intToStr(reason) +
                                  ",2,,,2", "+CCFC:"));
    responses.push_back(_at->chat("+CCFC=" + intToStr(reason) +
                                  ",2,,,4", "+CCFC:"));
  }

  for (vector<string>::iterator i = responses.begin();
       i != responses.end(); ++i)
  {
    Parser p(*i);
    int status = p.parseInt();
    p.parseComma();
    FacilityClass cl = (FacilityClass)p.parseInt();
    string number;
    string subAddr;
    int forwardTime = NOT_SET;
      
    // parse number
    if (p.parseComma(true))
    {
      number = p.parseString();
      p.parseComma();
      unsigned int numberType = p.parseInt();
      if (numberType == InternationalNumberFormat) number = "+" + number;

      // parse subaddr
      if (p.parseComma(true))
      {
        // FIXME subaddr type not handled
        subAddr = p.parseString(true);
        p.parseComma();
        p.parseInt(true);
          
        // parse forwardTime
        if (p.parseComma(true))
        {
          forwardTime = p.parseInt();
        }
      }
    }
    switch (cl)
    {
    case VoiceFacility:
      voice._active = (status == 1);
      voice._cl = VoiceFacility;
      voice._number = number;
      voice._subAddr = subAddr;
      voice._time = forwardTime;
      voice._reason = reason;
      break;
    case DataFacility:
      data._active = (status == 1);
      data._cl = DataFacility;
      data._number = number;
      data._subAddr = subAddr;
      data._time = forwardTime;
      data._reason =  reason;
      break;
    case FaxFacility:
      fax._active = (status == 1);
      fax._cl = FaxFacility;
      fax._number = number;
      fax._subAddr = subAddr;
      fax._time = forwardTime;
      fax._reason = reason;
      break;
    }
  }
}

int MeTa::getBatteryChargeStatus() throw(GsmException)
{
  Parser p(_at->chat("+CBC", "+CBC:"));
  return p.parseInt();
}

int MeTa::getBatteryCharge() throw(GsmException)
{
  Parser p(_at->chat("+CBC", "+CBC:"));
  p.parseInt();
  p.parseComma();
  return p.parseInt();
}

int MeTa::getFunctionalityLevel() throw(GsmException)
{
  try {
    Parser p(_at->chat("+CFUN?", "+CFUN:"));
    // some phones return functionality level like "(2)"
    bool expectClosingParen = p.parseChar('(', true);
    int result = p.parseInt();
    if (expectClosingParen)
      p.parseChar(')');
    return result;
  }
  catch (GsmException &x)
  {
    if (x.getErrorClass() == ChatError)
    {
      throw GsmException(_("Functionality Level commands not supported by ME"),
			 MeTaCapabilityError);
    } else {
      throw;
    }
  }
}

void MeTa::setFunctionalityLevel(int level) throw(GsmException)
{
  try {
    Parser p(_at->chat("+CFUN="  + intToStr(level)));
  } catch (GsmException &x) {
    if (x.getErrorClass() == ChatError)
    {
      // If the command AT+CFUN commands really aren't supported by the ME,
      // then this will throw an appropriate exception for us.
      getFunctionalityLevel();
      // If the number was just out of range, we get here.
      throw GsmException(_("Requested Functionality Level out of range"),
			 ParameterError);
    }
    throw;
  }
}

int MeTa::getSignalStrength() throw(GsmException)
{
  Parser p(_at->chat("+CSQ", "+CSQ:"));
  return p.parseInt();
}

int MeTa::getBitErrorRate() throw(GsmException)
{
  Parser p(_at->chat("+CSQ", "+CSQ:"));
  p.parseInt();
  p.parseComma();
  return p.parseInt();
}

vector<string> MeTa::getPhoneBookStrings() throw(GsmException)
{
  Parser p(_at->chat("+CPBS=?", "+CPBS:"));
  return p.parseStringList();
}

PhonebookRef MeTa::getPhonebook(string phonebookString,
                                bool preload) throw(GsmException)
{
  for (PhonebookVector::iterator i = _phonebookCache.begin();
       i !=  _phonebookCache.end(); ++i)
  {
    if ((*i)->name() == phonebookString)
      return *i;
  }
  PhonebookRef newPb(new Phonebook(phonebookString, _at, *this, preload));
  _phonebookCache.push_back(newPb);
  return newPb;
}

string MeTa::getServiceCentreAddress() throw(GsmException)
{
  Parser p(_at->chat("+CSCA?", "+CSCA:"));
  return p.parseString();
}

void MeTa::setServiceCentreAddress(string sca) throw(GsmException)
{
  int type;
  sca = removeWhiteSpace(sca);
  if (sca.length() > 0 && sca[0] == '+')
  {
    type = InternationalNumberFormat;
    sca = sca.substr(1, sca.length() - 1);
  }
  else
    type = UnknownNumberFormat;
  Parser p(_at->chat("+CSCA=\"" + sca + "\"," + intToStr(type)));
}

vector<string> MeTa::getSMSStoreNames() throw(GsmException)
{
  Parser p(_at->chat("+CPMS=?", "+CPMS:"));
  // only return <mem1> values
  return p.parseStringList();
}

SMSStoreRef MeTa::getSMSStore(string storeName) throw(GsmException)
{
  for (SMSStoreVector::iterator i = _smsStoreCache.begin();
       i !=  _smsStoreCache.end(); ++i)
  {
    if ((*i)->name() == storeName)
      return *i;
  }
  SMSStoreRef newSs(new SMSStore(storeName, _at, *this));
  _smsStoreCache.push_back(newSs);
  return newSs;
}

void MeTa::sendSMS(Ref<SMSSubmitMessage> smsMessage) throw(GsmException)
{
  smsMessage->setAt(_at);
  smsMessage->send();
}

void MeTa::sendSMSs(Ref<SMSSubmitMessage> smsTemplate, string text,
                    bool oneSMS,
                    int concatenatedMessageId)
  throw(GsmException)
{
  assert(! smsTemplate.isnull());

  // compute maximum text length for normal SMSs and concatenated SMSs
  unsigned int maxTextLength, concMaxTextLength;
  switch (smsTemplate->dataCodingScheme().getAlphabet())
  {
  case DCS_DEFAULT_ALPHABET:
    maxTextLength = 160;
    concMaxTextLength = 152;
    break;
  case DCS_EIGHT_BIT_ALPHABET:
    maxTextLength = 140;
    concMaxTextLength = 134;
    break;
  case DCS_SIXTEEN_BIT_ALPHABET:
    maxTextLength = 70;
    concMaxTextLength = 67;
    break;
  default:
    throw GsmException(_("unsupported alphabet for SMS"),
                       ParameterError);
    break;
  }

  // simple case, only send one SMS
  if (oneSMS || text.length() <= maxTextLength)
  {
    if (text.length() > maxTextLength)
      throw GsmException(_("SMS text is larger than allowed"),
                         ParameterError);
    smsTemplate->setUserData(text);
    sendSMS(smsTemplate);
  }
  else                          // send multiple SMSs
  {
    if (concatenatedMessageId != -1)
      maxTextLength = concMaxTextLength;

    int numMessages = (text.length() + maxTextLength - 1) / maxTextLength;
    if (numMessages > 255)
      throw GsmException(_("not more than 255 concatenated SMSs allowed"),
                         ParameterError);
    unsigned char numMessage = 0;
    while (true)
    {
      if (concatenatedMessageId != -1)
      {
        unsigned char udhs[] = {0x00, 0x03, concatenatedMessageId,
                                numMessages, ++numMessage};
        UserDataHeader udh(string((char*)udhs, 5));
        smsTemplate->setUserDataHeader(udh);
      }
      smsTemplate->setUserData(text.substr(0, maxTextLength));
      sendSMS(smsTemplate);
      if (text.length() < maxTextLength)
        break;
      text.erase(0, maxTextLength);
    }
  }
}

void MeTa::setMessageService(int serviceLevel) throw(GsmException)
{
  string s;
  switch (serviceLevel)
  {
  case 0:
    s = "0";
    break;
  case 1:
    s = "1";
    break;
  default:
    throw GsmException(_("only serviceLevel 0 or 1 supported"),
                       ParameterError);
  }
  // some devices (eg. Origo 900) don't support service level setting
  _at->chat("+CSMS=" + s, "+CSMS:", true);
}

unsigned int MeTa::getMessageService() throw(GsmException)
{
  Parser p(_at->chat("+CSMS?", "+CSMS:"));
  return p.parseInt();
}

void MeTa::getSMSRoutingToTA(bool &smsRouted,
                             bool &cbsRouted,
                             bool &statusReportsRouted) throw(GsmException)
{
  Parser p(_at->chat("+CNMI?", "+CNMI:"));
  p.parseInt();
  int smsMode = 0;
  int cbsMode = 0;
  int statMode = 0;
  int bufferMode = 0;

  if (p.parseComma(true))
  {
    smsMode = p.parseInt();
    if (p.parseComma(true))
    {
      cbsMode = p.parseInt();
      if (p.parseComma(true))
      {
        statMode = p.parseInt();
        if (p.parseComma(true))
        {
          bufferMode = p.parseInt();
        }
      }
    }
  }
  
  smsRouted = (smsMode == 2) || (smsMode == 3);
  cbsRouted = (cbsMode == 2) || (cbsMode == 3);
  statusReportsRouted = (statMode == 1);
}

void MeTa::setSMSRoutingToTA(bool enableSMS, bool enableCBS,
                             bool enableStatReport,
                             bool onlyReceptionIndication)
  throw(GsmException)
{
  bool smsModesSet = false;
  bool cbsModesSet = false;
  bool statModesSet = false;
  bool bufferModesSet = false;

  // find out capabilities
  Parser p(_at->chat("+CNMI=?", "+CNMI:"));
  vector<bool> modes = p.parseIntList();
  vector<bool> smsModes(1);
  vector<bool> cbsModes(1);
  vector<bool> statModes(1);
  vector<bool> bufferModes(1);
  if (p.parseComma(true))
  {
    smsModes = p.parseIntList();
    smsModesSet = true;
    if (p.parseComma(true))
    {
      cbsModes = p.parseIntList();
      cbsModesSet = true;
      if (p.parseComma(true))
      {
        statModes = p.parseIntList();
        statModesSet = true;
        if (p.parseComma(true))
        {
          bufferModes = p.parseIntList();
          bufferModesSet = true;
        }
      }
    }
  }

  // now set the mode vectors to the default if not set
  if (! smsModesSet) smsModes[0] = true;
  if (! cbsModesSet) cbsModes[0] = true;
  if (! statModesSet) statModes[0] = true;
  if (! bufferModesSet) bufferModes[0] = true;
  
  string chatString;
    
  // now try to set some optimal combination depending on
  // ME/TA's capabilities

  // handle modes
  if (isSet(modes, 2))
    chatString = "2";
  else if (isSet(modes, 1))
    chatString = "1";
  else if (isSet(modes, 0))
    chatString = "0";
  else if (isSet(modes, 3))
    chatString = "3";

  if (onlyReceptionIndication)
  {
    // handle sms mode
    if (enableSMS)
    {
      if (isSet(smsModes, 1))
        chatString += ",1";
      else 
        throw GsmException(_("cannot route SMS messages to TE"),
                           MeTaCapabilityError);
    }
    else
      chatString += ",0";
      
    // handle cbs mode
    if (enableCBS)
    {
      if (isSet(cbsModes, 1))
        chatString += ",1";
      else if (isSet(cbsModes, 2))
        chatString += ",2";
      else 
        throw GsmException(_("cannot route cell broadcast messages to TE"),
                           MeTaCapabilityError);
    }
    else
      chatString += ",0";

    // handle stat mode
    if (enableStatReport)
    {
      if (isSet(statModes, 2))
        chatString += ",2";
      else 
        throw GsmException(_("cannot route status reports messages to TE"),
                           MeTaCapabilityError);
    }
    else
      chatString += ",0";
  }
  else
  {
    // handle sms mode
    if (enableSMS)
    {
      if (isSet(smsModes, 2))
        chatString += ",2";
      else if (isSet(smsModes, 3))
        chatString += ",3";
      else 
        throw GsmException(_("cannot route SMS messages to TE"),
                           MeTaCapabilityError);
    }
    else
      chatString += ",0";
      
    // handle cbs mode
    if (enableCBS)
    {
      if (isSet(cbsModes, 2))
        chatString += ",2";
      else if (isSet(cbsModes, 3))
        chatString += ",3";
      else 
        throw GsmException(_("cannot route cell broadcast messages to TE"),
                           MeTaCapabilityError);
    }
    else
      chatString += ",0";

    // handle stat mode
    if (enableStatReport)
    {
      if (isSet(statModes, 1))
        chatString += ",1";
      else if (isSet(statModes, 2))
        chatString += ",2";
      else 
        throw GsmException(_("cannot route status report messages to TE"),
                           MeTaCapabilityError);
    }
    else
      chatString += ",0";
  }

  // handle buffer mode but only if it was reported by the +CNMI=? command
  // the Ericsson GM12 GSM modem does not like it otherwise
  if (bufferModesSet)
    if (isSet(bufferModes, 1))
      chatString += ",1";
    else
      chatString += ",0";

  _at->chat("+CNMI=" + chatString);
}

bool MeTa::getCallWaitingLockStatus(FacilityClass cl)
  throw(GsmException)
{
  // some TA return always multiline response with all classes
  // (Option FirstFone)
  // !!! errors handling is correct (responses.empty() true) ?
  vector<string> responses = 
    _at->chatv("+CCWA=0,2," + intToStr((int)cl),"+CCWA:",true);
  for (vector<string>::iterator i = responses.begin();
       i != responses.end(); ++i)
  {
    Parser p(*i);
    int enabled = p.parseInt();

    // if the first time and there is no comma this 
    // return direct state of classes
    // else return all classes
    if (i == responses.begin())
    {
      if (! p.parseComma(true))
        return enabled == 1;
    }
    else
      p.parseComma();

    if (p.parseInt() == (int)cl)
      return enabled == 1;
  }
  return false;

}
void MeTa::setCallWaitingLockStatus(FacilityClass cl, bool lock)
  throw(GsmException)
{
  if(lock)
    _at->chat("+CCWA=0,1," + intToStr((int)cl));
  else
    _at->chat("+CCWA=0,0," + intToStr((int)cl));
}

void MeTa::setCLIRPresentation(bool enable) throw(GsmException)
{
  if (enable)
    _at->chat("+CLIR=1");
  else
    _at->chat("+CLIR=0");
}

int MeTa::getCLIRPresentation() throw(GsmException)
{
  // 0:according to the subscription of the CLIR service
  // 1:CLIR invocation
  // 2:CLIR suppression
  Parser p(_at->chat("+CLIR?", "+CLIR:"));
  return p.parseInt();
}