forked from Romppanen/gammu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsmstate.c
1699 lines (1481 loc) · 44.4 KB
/
gsmstate.c
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
/* (c) 2002-2005 by Marcin Wiacek and Michal Cihar */
/* Phones ID (c) partially by Walek */
#include <stdarg.h>
#define _GNU_SOURCE /* For strcasestr */
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <gammu-call.h>
#include <gammu-settings.h>
#include <gammu-unicode.h>
#include <gammu-config.h>
#include <gammu-misc.h>
#include "debug.h"
#include "gsmcomon.h"
#include "gsmphones.h"
#include "gsmstate.h"
#include "misc/coding/coding.h"
#include "misc/misc.h"
#include "device/devfunc.h"
#include "../helper/string.h"
#if defined(HAVE_GETPWUID) && defined(HAVE_GETUID)
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#endif
#if defined(WIN32) || defined(DJGPP)
/* Needed for SHGFP_TYPE_CURRENT */
#define _WIN32_IE 0x0501
#include <shlobj.h>
#define FALLBACK_GAMMURC "gammurc"
#define GAMMURC_NAME "\\gammurc"
#else
#define FALLBACK_GAMMURC "/etc/gammurc"
#define GAMMURC_NAME "/.gammurc"
#endif
#define XDG_GAMMURC_NAME "/gammu/config"
/* Win32 compatibility */
#ifndef PATH_MAX
#define PATH_MAX (MAX_PATH)
#endif
/**
* Returns current debuging descriptor. It honors use_global
* flag.
*/
GSM_Debug_Info *GSM_GetDI(GSM_StateMachine *s)
{
GSM_Debug_Info *curdi;
curdi = &GSM_global_debug;
if (s != NULL && s->di.use_global == FALSE) {
curdi = &(s->di);
}
return curdi;
}
static void GSM_RegisterConnection(GSM_StateMachine *s, unsigned int connection,
GSM_Device_Functions *device, GSM_Protocol_Functions *protocol)
{
if ((unsigned int)s->ConnectionType == connection) {
s->Device.Functions = device;
s->Protocol.Functions = protocol;
}
}
/**
* Information about connection names and parameters.
*/
typedef struct {
/**
* Name of the connection used in config file.
*/
const char *Name;
/**
* Connection type.
*/
const GSM_ConnectionType Connection;
/**
* Whether to disable DTR/RTS handling on this connection.
*/
gboolean SkipDtrRts;
} GSM_ConnectionInfo;
/**
* Mapping of connection names to internal identifications.
*/
static const GSM_ConnectionInfo GSM_Connections[] = {
{"at", GCT_AT, FALSE},
/* cables */
{"mbus", GCT_MBUS2, FALSE},
{"fbus", GCT_FBUS2, FALSE},
{"fbususb", GCT_FBUS2USB, FALSE},
{"fbuspl2303", GCT_FBUS2PL2303, FALSE},
{"dlr3", GCT_FBUS2DLR3, FALSE},
{"fbusdlr3", GCT_FBUS2DLR3, FALSE},
{"dku5", GCT_DKU5FBUS2, FALSE},
{"fbusdku5", GCT_DKU5FBUS2, FALSE},
{"dku5fbus", GCT_DKU5FBUS2, FALSE},
{"ark3116fbus", GCT_DKU5FBUS2, TRUE},
#ifdef WIN32
{"dku2", GCT_DKU2PHONET, FALSE},
{"dku2phonet", GCT_DKU2PHONET, FALSE},
#else
{"dku2", GCT_FBUS2USB, FALSE},
{"dku2phonet", GCT_FBUS2USB, FALSE},
#endif
{"dku2at", GCT_DKU2AT, FALSE},
/* for serial ports assigned by bt stack */
{"fbusblue", GCT_FBUS2BLUE, FALSE},
{"phonetblue", GCT_PHONETBLUE, FALSE},
/* bt */
{"blueobex", GCT_BLUEOBEX, FALSE},
{"bluephonet", GCT_BLUEPHONET, FALSE},
{"blueat", GCT_BLUEAT, FALSE},
{"bluerfobex", GCT_BLUEOBEX, FALSE},
{"bluefbus", GCT_BLUEFBUS2, FALSE},
{"bluerffbus", GCT_BLUEFBUS2, FALSE},
{"bluerfphonet", GCT_BLUEPHONET, FALSE},
{"bluerfat", GCT_BLUEAT, FALSE},
{"bluerfgnapbus", GCT_BLUEGNAPBUS, FALSE},
/* old "serial" irda */
{"infrared", GCT_FBUS2IRDA, FALSE},
{"fbusirda", GCT_FBUS2IRDA, FALSE},
/* socket irda */
{"irda", GCT_IRDAPHONET, FALSE},
{"irdaphonet", GCT_IRDAPHONET, FALSE},
{"irdaat", GCT_IRDAAT, FALSE},
{"irdaobex", GCT_IRDAOBEX, FALSE},
{"irdagnapbus", GCT_IRDAGNAPBUS, FALSE},
/* testing purposes */
{"none", GCT_NONE, FALSE},
};
GSM_Device_Functions NoneDevice = {
NONEFUNCTION,
NONEFUNCTION,
NONEFUNCTION,
NONEFUNCTION,
NONEFUNCTION,
NONEFUNCTION,
NONEFUNCTION
};
GSM_Protocol_Functions NoProtocol = {
NONEFUNCTION,
NONEFUNCTION,
NONEFUNCTION,
NONEFUNCTION
};
static GSM_Error GSM_RegisterAllConnections(GSM_StateMachine *s, const char *connection)
{
size_t i;
char *buff, *nodtr_pos, *nopower_pos;
/* Copy connection name, so that we can play with it */
buff = strdup(connection);
if (buff == NULL) {
return ERR_MOREMEMORY;
}
/* We check here is used connection string type is correct for ANY
* OS. If not, we return with error, that string is incorrect at all
*/
s->ConnectionType = 0;
s->SkipDtrRts = FALSE;
s->NoPowerCable = FALSE;
/* Are we asked for connection using stupid cable? */
nodtr_pos = strcasestr(buff, "-nodtr");
if (nodtr_pos != NULL) {
*nodtr_pos = 0;
}
/* Are we asked for connection using cable which does not
* use DTR/RTS as power supply? */
nopower_pos = strcasestr(buff, "-nopower");
if (nopower_pos != NULL) {
*nopower_pos = 0;
s->NoPowerCable = TRUE;
}
/* Compare known connections to what we got */
for (i = 0; i < sizeof(GSM_Connections) / sizeof(GSM_Connections[0]); i++) {
/* Check connection name */
if (strcasecmp(GSM_Connections[i].Name, buff) == 0) {
s->ConnectionType = GSM_Connections[i].Connection;
s->SkipDtrRts = GSM_Connections[i].SkipDtrRts;
break;
}
}
/* If we were forced, set this flag */
if (nodtr_pos != NULL) {
s->SkipDtrRts = TRUE;
}
/* Special case - at can contains speed */
if (s->ConnectionType == 0 && strncasecmp("at", buff, 2) == 0) {
s->Speed = FindSerialSpeed(buff + 2);
if (s->Speed != 0) {
s->ConnectionType = GCT_AT;
}
}
/* Free allocated memory */
free(buff);
buff = NULL;
if (s->ConnectionType == 0) {
return ERR_UNKNOWNCONNECTIONTYPESTRING;
}
/* We check now if user gave connection type compiled & available
* for used OS (if not, we return, that source not available)
*/
s->Device.Functions = NULL;
s->Protocol.Functions = NULL;
GSM_RegisterConnection(s, GCT_NONE, &NoneDevice, &NoProtocol);
#ifdef GSM_ENABLE_MBUS2
GSM_RegisterConnection(s, GCT_MBUS2, &SerialDevice, &MBUS2Protocol);
#endif
#ifdef GSM_ENABLE_FBUS2
GSM_RegisterConnection(s, GCT_FBUS2, &SerialDevice, &FBUS2Protocol);
#endif
#ifdef GSM_ENABLE_FBUS2DLR3
GSM_RegisterConnection(s, GCT_FBUS2DLR3, &SerialDevice, &FBUS2Protocol);
#endif
#ifdef GSM_ENABLE_DKU5FBUS2
GSM_RegisterConnection(s, GCT_DKU5FBUS2, &SerialDevice, &FBUS2Protocol);
#endif
#ifdef GSM_ENABLE_FBUS2PL2303
GSM_RegisterConnection(s, GCT_FBUS2PL2303,&SerialDevice, &FBUS2Protocol);
#endif
#ifdef GSM_ENABLE_FBUS2BLUE
GSM_RegisterConnection(s, GCT_FBUS2BLUE, &SerialDevice, &FBUS2Protocol);
#endif
#ifdef GSM_ENABLE_FBUS2IRDA
GSM_RegisterConnection(s, GCT_FBUS2IRDA, &SerialDevice, &FBUS2Protocol);
#endif
#if defined(GSM_ENABLE_DKU2PHONET) && defined(GSM_ENABLE_USBDEVICE)
GSM_RegisterConnection(s, GCT_FBUS2USB, &FBUSUSBDevice, &PHONETProtocol);
#endif
#ifdef GSM_ENABLE_DKU2PHONET
GSM_RegisterConnection(s, GCT_DKU2PHONET, &SerialDevice, &PHONETProtocol);
#endif
#ifdef GSM_ENABLE_DKU2AT
GSM_RegisterConnection(s, GCT_DKU2AT, &SerialDevice, &ATProtocol);
#endif
#ifdef GSM_ENABLE_AT
GSM_RegisterConnection(s, GCT_AT, &SerialDevice, &ATProtocol);
#endif
#ifdef GSM_ENABLE_PHONETBLUE
GSM_RegisterConnection(s, GCT_PHONETBLUE, &SerialDevice, &PHONETProtocol);
#endif
#ifdef GSM_ENABLE_IRDAGNAPBUS
GSM_RegisterConnection(s, GCT_IRDAGNAPBUS,&IrdaDevice, &GNAPBUSProtocol);
#endif
#ifdef GSM_ENABLE_IRDAPHONET
GSM_RegisterConnection(s, GCT_IRDAPHONET, &IrdaDevice, &PHONETProtocol);
#endif
#ifdef GSM_ENABLE_IRDAAT
GSM_RegisterConnection(s, GCT_IRDAAT, &IrdaDevice, &ATProtocol);
#endif
#ifdef GSM_ENABLE_IRDAOBEX
GSM_RegisterConnection(s, GCT_IRDAOBEX, &IrdaDevice, &OBEXProtocol);
#endif
#ifdef GSM_ENABLE_BLUEGNAPBUS
GSM_RegisterConnection(s, GCT_BLUEGNAPBUS,&BlueToothDevice,&GNAPBUSProtocol);
#endif
#ifdef GSM_ENABLE_BLUEFBUS2
GSM_RegisterConnection(s, GCT_BLUEFBUS2, &BlueToothDevice,&FBUS2Protocol);
#endif
#ifdef GSM_ENABLE_BLUEPHONET
GSM_RegisterConnection(s, GCT_BLUEPHONET, &BlueToothDevice,&PHONETProtocol);
#endif
#ifdef GSM_ENABLE_BLUEAT
GSM_RegisterConnection(s, GCT_BLUEAT, &BlueToothDevice,&ATProtocol);
#endif
#ifdef GSM_ENABLE_BLUEOBEX
GSM_RegisterConnection(s, GCT_BLUEOBEX, &BlueToothDevice,&OBEXProtocol);
#endif
if (s->Device.Functions == NULL || s->Protocol.Functions == NULL) {
smprintf(s, "Connection %s is know but was disabled on compile time\n", connection);
return ERR_DISABLED;
}
return ERR_NONE;
}
static void GSM_RegisterModule(GSM_StateMachine *s,GSM_Phone_Functions *phone)
{
/* Auto model */
if (s->CurrentConfig->Model[0] == 0) {
if (strstr(phone->models,GetModelData(s, NULL, s->Phone.Data.Model, NULL)->model) != NULL) {
smprintf(s,"[Module - \"%s\"]\n",phone->models);
s->Phone.Functions = phone;
}
} else {
if (strstr(phone->models,s->CurrentConfig->Model) != NULL) {
smprintf(s,"[Module - \"%s\"]\n",phone->models);
s->Phone.Functions = phone;
}
}
}
/**
* Tries to register all modules to find one matching current configuration.
*
* \param s State machine pointer.
*
* \return Error code, ERR_NONE on success.
*/
GSM_Error GSM_RegisterAllPhoneModules(GSM_StateMachine *s)
{
GSM_PhoneModel *model;
/* Auto model */
if (s->CurrentConfig->Model[0] == 0) {
model = GetModelData(s, NULL, s->Phone.Data.Model, NULL);
#ifdef GSM_ENABLE_ATGEN
/* With ATgen and auto model we can work with unknown models too */
if (s->ConnectionType==GCT_AT || s->ConnectionType==GCT_BLUEAT || s->ConnectionType==GCT_IRDAAT || s->ConnectionType==GCT_DKU2AT) {
#ifdef GSM_ENABLE_ALCATEL
/* If phone provides Alcatel specific functions, enable them */
if (model->model[0] != 0 && GSM_IsPhoneFeatureAvailable(model, F_ALCATEL)) {
smprintf(s,"[Module - \"%s\"]\n",ALCATELPhone.models);
s->Phone.Functions = &ALCATELPhone;
return ERR_NONE;
}
#endif
#ifdef GSM_ENABLE_ATOBEX
/* If phone provides Sony-Ericsson specific functions, enable them */
if (model->model[0] != 0 && GSM_IsPhoneFeatureAvailable(model, F_OBEX)) {
smprintf(s,"[Module - \"%s\"]\n",ATOBEXPhone.models);
s->Phone.Functions = &ATOBEXPhone;
return ERR_NONE;
}
#endif
smprintf(s,"[Module - \"%s\"]\n",ATGENPhone.models);
s->Phone.Functions = &ATGENPhone;
return ERR_NONE;
}
#endif
/* With OBEXgen and auto model we can work with unknown models too */
#ifdef GSM_ENABLE_OBEXGEN
if (s->ConnectionType==GCT_BLUEOBEX || s->ConnectionType==GCT_IRDAOBEX) {
smprintf(s,"[Module - \"%s\"]\n",OBEXGENPhone.models);
s->Phone.Functions = &OBEXGENPhone;
return ERR_NONE;
}
#endif
#ifdef GSM_ENABLE_BACKUP
if (s->ConnectionType == GCT_NONE) {
smprintf(s,"[Module - \"%s\"]\n",DUMMYPhone.models);
s->Phone.Functions = &DUMMYPhone;
return ERR_NONE;
}
#endif
/* With GNAPgen and auto model we can work with unknown models too */
#ifdef GSM_ENABLE_GNAPGEN
if (s->ConnectionType == GCT_BLUEGNAPBUS || s->ConnectionType == GCT_IRDAGNAPBUS) {
smprintf(s,"[Module - \"%s\"]\n",GNAPGENPhone.models);
s->Phone.Functions = &GNAPGENPhone;
return ERR_NONE;
}
#endif
#ifdef GSM_ENABLE_NOKIA6510
if ( s->ConnectionType == GCT_MBUS2 ||
s->ConnectionType == GCT_FBUS2 ||
s->ConnectionType == GCT_FBUS2USB ||
s->ConnectionType == GCT_FBUS2DLR3 ||
s->ConnectionType == GCT_FBUS2PL2303 ||
s->ConnectionType == GCT_FBUS2BLUE ||
s->ConnectionType == GCT_FBUS2IRDA ||
s->ConnectionType == GCT_DKU5FBUS2 ||
s->ConnectionType == GCT_DKU2PHONET ||
s->ConnectionType == GCT_PHONETBLUE ||
s->ConnectionType == GCT_IRDAPHONET ||
s->ConnectionType == GCT_BLUEFBUS2 ||
s->ConnectionType == GCT_BLUEPHONET) {
/* Try to detect phone type */
if (strcmp(model->model, "unknown") == 0 && model->features[0] == 0) {
smprintf(s, "WARNING: phone not known, please report it to authors (see <http://wammu.eu/support/bugs/>). Thank you.\n");
if (strncmp(s->Phone.Data.Model, "RM-", 3) == 0) {
/* 167 is really a wild guess */
if (atoi(s->Phone.Data.Model + 3) > 167) {
smprintf(s, "WARNING: Guessed phone as S40/30 compatible (RM series)!\n");
GSM_AddPhoneFeature(model, F_SERIES40_30);
GSM_AddPhoneFeature(model, F_FILES2);
GSM_AddPhoneFeature(model, F_TODO66);
GSM_AddPhoneFeature(model, F_RADIO);
GSM_AddPhoneFeature(model, F_NOTES);
GSM_AddPhoneFeature(model, F_SMS_FILES);
}
}
if (strncmp(s->Phone.Data.Model, "RH-", 3) == 0) {
/* 63 is really a wild guess */
if (atoi(s->Phone.Data.Model + 3) > 63) {
smprintf(s, "WARNING: Guessed phone as S40/30 compatible (RH series)!\n");
GSM_AddPhoneFeature(model, F_SERIES40_30);
GSM_AddPhoneFeature(model, F_FILES2);
GSM_AddPhoneFeature(model, F_TODO66);
GSM_AddPhoneFeature(model, F_RADIO);
GSM_AddPhoneFeature(model, F_NOTES);
GSM_AddPhoneFeature(model, F_SMS_FILES);
}
}
}
/* If phone is S40, use 6510 */
if (GSM_IsPhoneFeatureAvailable(model, F_SERIES40_30)) {
smprintf(s,"[Module - \"%s\"]\n", N6510Phone.models);
s->Phone.Functions = &N6510Phone;
return ERR_NONE;
}
}
#endif
if (model->model[0] == 0) return ERR_UNKNOWNMODELSTRING;
}
s->Phone.Functions = NULL;
#ifdef GSM_ENABLE_ATGEN
/* AT module can have the same models ID to "normal" Nokia modules */
if (s->ConnectionType==GCT_AT || s->ConnectionType==GCT_BLUEAT || s->ConnectionType==GCT_IRDAAT || s->ConnectionType==GCT_DKU2AT) {
GSM_RegisterModule(s,&ATGENPhone);
if (s->Phone.Functions != NULL) return ERR_NONE;
}
#endif
#ifdef GSM_ENABLE_BACKUP
GSM_RegisterModule(s, &DUMMYPhone);
#endif
#ifdef GSM_ENABLE_OBEXGEN
GSM_RegisterModule(s,&OBEXGENPhone);
#endif
#ifdef GSM_ENABLE_GNAPGEN
GSM_RegisterModule(s,&GNAPGENPhone);
#endif
#ifdef GSM_ENABLE_NOKIA3320
GSM_RegisterModule(s,&N3320Phone);
#endif
#ifdef GSM_ENABLE_NOKIA3650
GSM_RegisterModule(s,&N3650Phone);
#endif
#ifdef GSM_ENABLE_NOKIA650
GSM_RegisterModule(s,&N650Phone);
#endif
#ifdef GSM_ENABLE_NOKIA6110
GSM_RegisterModule(s,&N6110Phone);
#endif
#ifdef GSM_ENABLE_NOKIA6510
GSM_RegisterModule(s,&N6510Phone);
#endif
#ifdef GSM_ENABLE_NOKIA7110
GSM_RegisterModule(s,&N7110Phone);
#endif
#ifdef GSM_ENABLE_NOKIA9210
GSM_RegisterModule(s,&N9210Phone);
#endif
#ifdef GSM_ENABLE_ALCATEL
GSM_RegisterModule(s,&ALCATELPhone);
#endif
#ifdef GSM_ENABLE_ATOBEX
GSM_RegisterModule(s,&ATOBEXPhone);
#endif
if (s->Phone.Functions == NULL) {
return ERR_UNKNOWNMODELSTRING;
}
return ERR_NONE;
}
/**
* Opens connection to device and initiates protocol layer.
*/
GSM_Error GSM_OpenConnection(GSM_StateMachine *s)
{
GSM_Error error;
if (s->CurrentConfig->LockDevice) {
error = lock_device(s, s->CurrentConfig->Device, &(s->LockFile));
if (error != ERR_NONE) return error;
}
/* Irda devices can set now model to some specific and
* we don't have to make auto detection later */
error=s->Device.Functions->OpenDevice(s);
if (error!=ERR_NONE) {
if (s->LockFile != NULL)
unlock_device(s, &(s->LockFile));
return error;
}
s->opened = TRUE;
error=s->Protocol.Functions->Initialise(s);
if (error!=ERR_NONE) return error;
return ERR_NONE;
}
/**
* Internal function which just closes connection and cleans up structures.
*/
GSM_Error GSM_CloseConnection(GSM_StateMachine *s)
{
GSM_Error error;
smprintf(s, "[Closing]\n");
/* Terminate protocol */
error = s->Protocol.Functions->Terminate(s);
if (error != ERR_NONE) return error;
/* Close the device */
error = s->Device.Functions->CloseDevice(s);
if (error != ERR_NONE) return error;
/* Release lock if there was any */
if (s->LockFile != NULL) {
unlock_device(s, &(s->LockFile));
}
/* Null all structures in case we will be asked for new initialisation */
s->Phone.Data.ModelInfo = NULL;
s->Phone.Data.Manufacturer[0] = 0;
s->Phone.Data.Model[0] = 0;
s->Phone.Data.Version[0] = 0;
s->Phone.Data.VerDate[0] = 0;
s->Phone.Data.VerNum = 0;
return ERR_NONE;
}
/**
* Tries to read model using configured phone connection.
*/
GSM_Error GSM_TryGetModel(GSM_StateMachine *s)
{
GSM_Error error;
error = GSM_OpenConnection(s);
if (error != ERR_NONE) return error;
/* If still auto model, try to get model by asking phone for it */
if (s->Phone.Data.Model[0]==0) {
smprintf(s,"[Module - \"auto\"]\n");
switch (s->ConnectionType) {
#ifdef GSM_ENABLE_BACKUP
case GCT_NONE:
s->Phone.Functions = &DUMMYPhone;
break;
#endif
#ifdef GSM_ENABLE_ATGEN
case GCT_AT:
case GCT_BLUEAT:
case GCT_IRDAAT:
case GCT_DKU2AT:
s->Phone.Functions = &ATGENPhone;
break;
#endif
#ifdef GSM_ENABLE_OBEXGEN
case GCT_IRDAOBEX:
case GCT_BLUEOBEX:
s->Phone.Functions = &OBEXGENPhone;
break;
#endif
#ifdef GSM_ENABLE_GNAPGEN
case GCT_BLUEGNAPBUS:
case GCT_IRDAGNAPBUS:
s->Phone.Functions = &GNAPGENPhone;
break;
#endif
#if defined(GSM_ENABLE_NOKIA_DCT3) || defined(GSM_ENABLE_NOKIA_DCT4)
case GCT_MBUS2:
case GCT_FBUS2:
case GCT_FBUS2USB:
case GCT_FBUS2DLR3:
case GCT_FBUS2PL2303:
case GCT_FBUS2BLUE:
case GCT_FBUS2IRDA:
case GCT_DKU5FBUS2:
case GCT_DKU2PHONET:
case GCT_PHONETBLUE:
case GCT_IRDAPHONET:
case GCT_BLUEFBUS2:
case GCT_BLUEPHONET:
s->Phone.Functions = &NAUTOPhone;
break;
#endif
default:
s->Phone.Functions = NULL;
}
/* Did we find matching phone driver? */
if (s->Phone.Functions == NULL) {
smprintf(s, "ERROR: Could not find proper module for autodetection!\n");
return ERR_UNKNOWN;
}
/* Initialize the phone driver */
error = s->Phone.Functions->Initialise(s);
if (error != ERR_NONE) return error;
/* Get model name from phone */
error = s->Phone.Functions->GetModel(s);
if (error != ERR_NONE) return error;
/* And terminate it again */
error = s->Phone.Functions->Terminate(s);
if (error != ERR_NONE) return error;
}
return ERR_NONE;
}
GSM_Error GSM_InitConnection_Log(GSM_StateMachine *s, int ReplyNum, GSM_Log_Function log_function, void *user_data)
{
GSM_Error error;
GSM_DateTime current_time;
int i;
for (i=0;i<s->ConfigNum;i++) {
s->CurrentConfig = &s->Config[i];
/* Skip non configured sections */
if (s->CurrentConfig->Connection == NULL) {
smprintf_level(s, D_ERROR, "[Empty section - %d]\n", i);
continue;
}
s->Speed = 0;
s->ReplyNum = ReplyNum;
s->Phone.Data.ModelInfo = GetModelData(s, "unknown", NULL, NULL);
s->Phone.Data.Manufacturer[0] = 0;
s->Phone.Data.Model[0] = 0;
s->Phone.Data.Version[0] = 0;
s->Phone.Data.VerDate[0] = 0;
s->Phone.Data.VerNum = 0;
s->Phone.Data.StartInfoCounter = 0;
s->Phone.Data.SentMsg = NULL;
s->Phone.Data.HardwareCache[0] = 0;
s->Phone.Data.ProductCodeCache[0] = 0;
s->Phone.Data.EnableIncomingCall = FALSE;
s->Phone.Data.EnableIncomingSMS = FALSE;
s->Phone.Data.EnableIncomingCB = FALSE;
s->Phone.Data.EnableIncomingUSSD = FALSE;
s->User.UserReplyFunctions = NULL;
s->User.IncomingCall = NULL;
s->User.IncomingSMS = NULL;
s->User.IncomingCB = NULL;
s->User.IncomingUSSD = NULL;
s->User.SendSMSStatus = NULL;
s->LockFile = NULL;
s->opened = FALSE;
s->Phone.Functions = NULL;
s->di = GSM_none_debug;
s->di.use_global = s->CurrentConfig->UseGlobalDebugFile;
if (!s->di.use_global) {
GSM_SetDebugFunction(log_function, user_data, &s->di);
GSM_SetDebugLevel(s->CurrentConfig->DebugLevel, &s->di);
error = GSM_SetDebugFile(s->CurrentConfig->DebugFile, &s->di);
if (error != ERR_NONE) {
GSM_LogError(s, "Init:GSM_SetDebugFile" , error);
return error;
}
}
smprintf_level(s, D_ERROR, "[Gammu - %s built %s %s using %s]\n",
VERSION,
__TIME__,
__DATE__,
GetCompiler()
);
StripSpaces(s->CurrentConfig->Connection);
StripSpaces(s->CurrentConfig->Model);
StripSpaces(s->CurrentConfig->Device);
smprintf_level(s, D_ERROR, "[Connection - \"%s\"]\n",
s->CurrentConfig->Connection);
smprintf_level(s, D_ERROR, "[Connection index - %d]\n", i);
smprintf_level(s, D_ERROR, "[Model type - \"%s\"]\n",
s->CurrentConfig->Model);
smprintf_level(s, D_ERROR, "[Device - \"%s\"]\n",
s->CurrentConfig->Device);
if (strlen(GetOS()) != 0) {
smprintf_level(s, D_ERROR, "[Runing on - %s]\n",
GetOS());
}
if (GSM_GetDI(s)->dl == DL_BINARY) {
smprintf(s,"%c",((unsigned char)strlen(VERSION)));
smprintf(s,"%s",VERSION);
}
error = GSM_RegisterAllConnections(s, s->CurrentConfig->Connection);
if (error != ERR_NONE) {
GSM_LogError(s, "Init:GSM_RegisterAllConnections" , error);
return error;
}
autodetect:
/* Model auto */
if (s->CurrentConfig->Model[0] == 0) {
error = GSM_TryGetModel(s);
if ((i != s->ConfigNum - 1) && (
(error == ERR_DEVICEOPENERROR) ||
(error == ERR_DEVICELOCKED) ||
(error == ERR_DEVICENOTEXIST) ||
(error == ERR_DEVICEBUSY) ||
(error == ERR_DEVICENOPERMISSION) ||
(error == ERR_DEVICENODRIVER) ||
(error == ERR_DEVICENOTWORK))) {
GSM_CloseConnection(s);
continue;
}
if (error != ERR_NONE) {
GSM_LogError(s, "Init:GSM_TryGetModel" , error);
return error;
}
}
/* Switching to "correct" module */
error = GSM_RegisterAllPhoneModules(s);
/* If user selected soemthing which is not supported, try autodetection */
if (s->CurrentConfig->Model[0] != 0 && error == ERR_UNKNOWNMODELSTRING) {
smprintf(s, "Configured model %s is not known, retrying with autodetection!\n",
s->CurrentConfig->Model);
s->CurrentConfig->Model[0] = 0;
goto autodetect;
}
if (error != ERR_NONE) {
GSM_LogError(s, "Init:GSM_RegisterAllPhoneModules" , error);
return error;
}
/* We didn't open device earlier ? Make it now */
if (!s->opened) {
error = GSM_OpenConnection(s);
if ((i != s->ConfigNum - 1) && (
(error == ERR_DEVICEOPENERROR) ||
(error == ERR_DEVICELOCKED) ||
(error == ERR_DEVICENOTEXIST) ||
(error == ERR_DEVICEBUSY) ||
(error == ERR_DEVICENOPERMISSION) ||
(error == ERR_DEVICENODRIVER) ||
(error == ERR_DEVICENOTWORK))) {
GSM_CloseConnection(s);
continue;
}
if (error != ERR_NONE) {
GSM_LogError(s, "Init:GSM_OpenConnection" , error);
return error;
}
}
/* Initialize phone layer */
error=s->Phone.Functions->Initialise(s);
if (error == ERR_TIMEOUT && i != s->ConfigNum - 1) {
GSM_CloseConnection(s);
continue;
}
if (error != ERR_NONE) {
GSM_LogError(s, "Init:Phone->Initialise" , error);
return error;
}
if (s->CurrentConfig->StartInfo) {
s->Phone.Functions->ShowStartInfo(s,TRUE);
s->Phone.Data.StartInfoCounter = 30;
}
if (s->CurrentConfig->SyncTime) {
GSM_GetCurrentDateTime (¤t_time);
s->Phone.Functions->SetDateTime(s,¤t_time);
}
/* For debug it's good to have firmware and real model version and manufacturer */
error=s->Phone.Functions->GetManufacturer(s);
if (error == ERR_TIMEOUT && i != s->ConfigNum - 1) {
GSM_CloseConnection(s);
continue;
}
if (error != ERR_NONE && error != ERR_NOTSUPPORTED) {
GSM_LogError(s, "Init:Phone->GetManufacturer" , error);
return error;
}
error=s->Phone.Functions->GetModel(s);
if (error != ERR_NONE && error != ERR_NOTSUPPORTED) {
GSM_LogError(s, "Init:Phone->GetModel" , error);
return error;
}
error=s->Phone.Functions->GetFirmware(s);
if (error != ERR_NONE && error != ERR_NOTSUPPORTED) {
GSM_LogError(s, "Init:Phone->GetFirmware" , error);
return error;
}
smprintf(s,"[Connected]\n");
return ERR_NONE;
}
return ERR_UNCONFIGURED;
}
GSM_Error GSM_InitConnection(GSM_StateMachine *s, int ReplyNum)
{
return GSM_InitConnection_Log(s, ReplyNum, GSM_none_debug.log_function, GSM_none_debug.user_data);
}
int GSM_ReadDevice (GSM_StateMachine *s, gboolean waitforreply)
{
GSM_DateTime Date;
unsigned char buff[65536]={'\0'};
int res=0,count=0,i=0;
if (!GSM_IsConnected(s)) {
return -1;
}
GSM_GetCurrentDateTime (&Date);
i=Date.Second;
while (i==Date.Second) {
res = s->Device.Functions->ReadDevice(s, buff, sizeof(buff));
if (!waitforreply) {
break;
}
if (res > 0) {
break;
}
usleep(5000);
GSM_GetCurrentDateTime(&Date);
}
for (count = 0; count < res; count++) {
s->Protocol.Functions->StateMachine(s,buff[count]);
}
return res;
}
GSM_Error GSM_TerminateConnection(GSM_StateMachine *s)
{
GSM_Error error;
if (!s->opened) return ERR_UNKNOWN;
smprintf(s,"[Terminating]\n");
if (s->CurrentConfig->StartInfo) {
if (s->Phone.Data.StartInfoCounter > 0) s->Phone.Functions->ShowStartInfo(s,FALSE);
}
if (s->Phone.Functions != NULL) {
error=s->Phone.Functions->Terminate(s);
if (error!=ERR_NONE) return error;
}
error = GSM_CloseConnection(s);
if (error != ERR_NONE) return error;
GSM_SetDebugFileDescriptor(NULL, FALSE, &(s->di));
s->opened = FALSE;
return ERR_NONE;
}
gboolean GSM_IsConnected(GSM_StateMachine *s) {
return (s != NULL) && s->Phone.Functions != NULL && s->opened;
}
GSM_Error GSM_WaitForOnce(GSM_StateMachine *s, unsigned const char *buffer,
int length, unsigned char type, int timeout)
{
GSM_Phone_Data *Phone = &s->Phone.Data;
GSM_Protocol_Message sentmsg;
int i = 0;
do {
if (length != 0) {
sentmsg.Length = length;
sentmsg.Type = type;
sentmsg.Buffer = (unsigned char *)malloc(length);
memcpy(sentmsg.Buffer,buffer,length);
Phone->SentMsg = &sentmsg;
}
/* Some data received. Reset timer */
if (GSM_ReadDevice(s, TRUE) > 0) {
i = 0;
} else {
usleep(10000);
}
if (length != 0) {
free(sentmsg.Buffer);
sentmsg.Buffer = NULL;
Phone->SentMsg = NULL;
}
/* Request completed */
if (Phone->RequestID==ID_None) {
return Phone->DispatchError;
}
i++;
} while (i<timeout);
return ERR_TIMEOUT;
}
GSM_Error GSM_WaitFor (GSM_StateMachine *s, unsigned const char *buffer,
int length, unsigned char type, int timeout,
GSM_Phone_RequestID request)
{
GSM_Phone_Data *Phone = &s->Phone.Data;
GSM_Error error;
int reply;
if (s->CurrentConfig->StartInfo) {
if (Phone->StartInfoCounter > 0) {
Phone->StartInfoCounter--;
if (Phone->StartInfoCounter == 0) s->Phone.Functions->ShowStartInfo(s,FALSE);
}
}
Phone->RequestID = request;
Phone->DispatchError = ERR_TIMEOUT;
for (reply=0;reply<s->ReplyNum;reply++) {
if (reply!=0) {
smprintf_level(s, D_ERROR, "[Retrying %i type 0x%02X]\n", reply, type);
}
error = s->Protocol.Functions->WriteMessage(s, buffer, length, type);
if (error!=ERR_NONE) return error;
error = GSM_WaitForOnce(s, buffer, length, type, timeout);
if (error != ERR_TIMEOUT) return error;
}
return ERR_TIMEOUT;
}
static GSM_Error CheckReplyFunctions(GSM_StateMachine *s, GSM_Reply_Function *Reply, int *reply)
{
GSM_Phone_Data *Data = &s->Phone.Data;
GSM_Protocol_Message *msg = s->Phone.Data.RequestMsg;
gboolean execute;
gboolean available = FALSE;
int i = 0;
while (Reply[i].requestID!=ID_None) {
execute=FALSE;
/* Binary frames like in Nokia */
if (strlen(Reply[i].msgtype) < 2) {
if (Reply[i].msgtype[0]==msg->Type) {
if (Reply[i].subtypechar!=0) {
if (Reply[i].subtypechar<=msg->Length) {
if (msg->Buffer[Reply[i].subtypechar]==Reply[i].subtype)
execute=TRUE;
}
} else execute=TRUE;
}
} else {
if (strlen(Reply[i].msgtype) < msg->Length) {
if (strncmp(Reply[i].msgtype,msg->Buffer,strlen(Reply[i].msgtype))==0) {
execute=TRUE;
}
}
}
if (execute) {
*reply=i;
if (Reply[i].requestID == ID_IncomingFrame ||
Reply[i].requestID == Data->RequestID ||
Data->RequestID == ID_EachFrame) {
return ERR_NONE;
}