-
Notifications
You must be signed in to change notification settings - Fork 41
/
gsm0710.c
1705 lines (1545 loc) · 42 KB
/
gsm0710.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
/*
*
* GSM 07.10 Implementation with User Space Serial Ports
*
* Copyright (C) 2003 Tuukka Karvonen <[email protected]>
*
* Version 1.0 October 2003
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Modified November 2004 by David Jander <[email protected]>
* - Hacked to use Pseudo-TTY's instead of the (obsolete?) USSP driver.
* - Fixed some bugs which prevented it from working with Sony-Ericsson modems
* - Seriously broke hardware handshaking.
* - Changed commandline interface to use getopts:
*
* Modified January 2006 by Tuukka Karvonen <[email protected]> and
* Antti Haapakoski <[email protected]>
* - Applied patches received from Ivan S. Dubrov
* - Disabled possible CRLF -> LFLF conversions in serial port initialization
* - Added minicom like serial port option setting if baud rate is configured.
* This was needed to get the options right on some platforms and to
* wake up some modems.
* - Added possibility to pass PIN code for modem in initialization
* (Sometimes WebBox modems seem to hang if PIN is given on a virtual channel)
* - Removed old code that was commented out
* - Added support for Unix98 scheme pseudo terminals (/dev/ptmx)
* and creation of symlinks for slave devices
* - Corrected logging of slave port names
* - at_command looks for AT/ERROR responses with findInBuf function instead
* of strstr function so that incoming carbage won't confuse it
*
* Modified March 2006 by Tuukka Karvonen <[email protected]>
* - Added -r option which makes the mux driver to restart itself in case
* the modem stops responding. This should make the driver more fault
* tolerant.
* - Some code restructuring that was required by the automatic restarting
* - buffer.c to use syslog instead of PDEBUG
* - fixed open_pty function to grant right for Unix98 scheme pseudo
* terminals even though symlinks are not in use
*
* New Usage:
* gsmMuxd [options] <pty1> <pty2> ...
*
* To see the options, type:
* ./gsmMuxd -h
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef _GNU_SOURCE
// To get ptsname grandpt and unlockpt definitions from stdlib.h
#define _GNU_SOURCE
#endif
#include <features.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <string.h>
#include <paths.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
//syslog
#include <syslog.h>
#include "buffer.h"
#include "gsm0710.h"
#define DEFAULT_NUMBER_OF_PORTS 3
#define WRITE_RETRIES 5
#define MAX_CHANNELS 32
//vitorio, only to use if necessary (don't ask in what i was thinking when i wrote this)
#define TRUE 1
#define FALSE 0
#define UNKNOW_MODEM 0
#define MC35 1
#define GENERIC 2
#define IRZ52IT 3
// Defines how often the modem is polled when automatic restarting is enabled
// The value is in seconds
#define POLLING_INTERVAL 5
#define MAX_PINGS 4
static volatile int terminate = 0;
static int terminateCount = 0;
static char* devSymlinkPrefix = 0;
static int *ussp_fd;
static int serial_fd;
static Channel_Status *cstatus;
static int max_frame_size = 31; // The limit of Sony-Ericsson GM47
static int wait_for_daemon_status = 0;
static GSM0710_Buffer *in_buf; // input buffer
static int _debug = 0;
static pid_t the_pid;
int _priority;
int _modem_type;
static char *serportdev;
static int pin_code = 0;
static char *ptydev[MAX_CHANNELS];
static int numOfPorts;
static int maxfd;
static int baudrate = 0;
static int *remaining;
static int faultTolerant = 0;
static int restart = 0;
/* The following arrays must have equal length and the values must
* correspond.
*/
static int baudrates[] = {
0, 9600, 19200, 38400, 57600, 115200, 230400, 460800 };
static speed_t baud_bits[] = {
0, B9600, B19200, B38400, B57600, B115200, B230400, B460800 };
#if 0
/* Opens USSP port for use.
*
* PARAMS:
* port - port number
* RETURNS
* file descriptor or -1 on error
*/
int ussp_open(int port)
{
int fd;
char name[] = "ser0\0";
name[3] = (char) (0x30 + port);
PDEBUG("Open serial port %s ", name);
fd = open(name, O_RDWR | O_NONBLOCK);
PDEBUG("done.\n");
return fd;
}
#endif
/**
* Returns success, when an ussp is opened.
*/
int ussp_connected(int port)
{
#if 0
struct ussp_operation op;
op.op = USSP_OPEN_RESULT;
if (cstatus[port + 1].opened)
op.arg = 0;
else
op.arg = -1;
op.len = 0;
write(ussp_fd[port], &op, sizeof(op));
PDEBUG("USSP port %d opened.\n", port);
return 0;
#else
return 0;
#endif
}
/** Writes a frame to a logical channel. C/R bit is set to 1.
* Doesn't support FCS counting for UI frames.
*
* PARAMS:
* channel - channel number (0 = control)
* input - the data to be written
* count - the length of the data
* type - the type of the frame (with possible P/F-bit)
*
* RETURNS:
* number of characters written
*/
int write_frame(int channel, const char *input, int count, unsigned char type)
{
// flag, EA=1 C channel, frame type, length 1-2
unsigned char prefix[5] = { F_FLAG, EA | CR, 0, 0, 0 };
unsigned char postfix[2] = { 0xFF, F_FLAG };
int prefix_length = 4, c;
if(_debug)
syslog(LOG_DEBUG,"send frame to ch: %d \n", channel);
// EA=1, Command, let's add address
prefix[1] = prefix[1] | ((63 & (unsigned char) channel) << 2);
// let's set control field
prefix[2] = type;
// let's not use too big frames
count = min(max_frame_size, count);
// length
if (count > 127)
{
prefix_length = 5;
prefix[3] = ((127 & count) << 1);
prefix[4] = (32640 & count) >> 7;
}
else
{
prefix[3] = 1 | (count << 1);
}
// CRC checksum
postfix[0] = make_fcs(prefix + 1, prefix_length - 1);
c = write(serial_fd, prefix, prefix_length);
if (c != prefix_length)
{
if(_debug)
syslog(LOG_DEBUG,"Couldn't write the whole prefix to the serial port for the virtual port %d. Wrote only %d bytes.", channel, c);
return 0;
}
if (count > 0)
{
c = write(serial_fd, input, count);
if (count != c)
{
if(_debug)
syslog(LOG_DEBUG,"Couldn't write all data to the serial port from the virtual port %d. Wrote only %d bytes.\n", channel, c);
return 0;
}
}
c = write(serial_fd, postfix, 2);
if (c != 2)
{
if(_debug)
syslog(LOG_DEBUG,"Couldn't write the whole postfix to the serial port for the virtual port %d. Wrote only %d bytes.", channel, c);
return 0;
}
return count;
}
/* Handles received data from ussp device.
*
* This function is derived from a similar function in RFCOMM Implementation
* with USSPs made by Marcel Holtmann.
*
* PARAMS:
* buf - buffer, which contains received data
* len - the length of the buffer
* port - the number of ussp device (logical channel), where data was
* received
* RETURNS:
* the number of remaining bytes in partial packet
*/
int ussp_recv_data(unsigned char *buf, int len, int port)
{
#if 0
int n, written;
unsigned char pkt_buf[4096];
struct ussp_operation *op = (struct ussp_operation *) pkt_buf, *top;
struct termios *tiosp;
int i; // size
unsigned char msc[5] = { CR | C_MSC, 0x5, 0, 0, 1 };
PDEBUG( "(DEBUG) %s chamada\n", __FUNCTION__);
memcpy(pkt_buf, buf, len);
n = len;
op = (struct ussp_operation *) pkt_buf;
for (top = op;
/* check for partial packet - first, make sure top->len is actually in pkt_buf */
((char *) top + sizeof(struct ussp_operation) <= ((char *) op) + n)
&& ((char *) top + sizeof(struct ussp_operation) + top->len <= ((char *) op) + n);
top = (struct ussp_operation *) (((char *) top) + top->len + sizeof(struct ussp_operation)))
{
switch (top->op)
{
case USSP_OPEN:
ussp_connected(port);
break;
case USSP_CLOSE:
PDEBUG("Close ussp port %d\n", port);
break;
case USSP_WRITE:
written = 0;
i = 0;
// try to write 5 times
while ((written += write_frame(port + 1, top->data + written,
top->len - written, UIH)) != top->len && i < WRITE_RETRIES)
{
i++;
}
if (i == WRITE_RETRIES)
{
PDEBUG("Couldn't write data to channel %d. Wrote only %d bytes, when should have written %ld.\n",
(port + 1), written, top->len);
}
break;
case USSP_SET_TERMIOS:
tiosp = (struct termios *) (top + 1);
if ((tiosp->c_cflag & CBAUD) == B0 && (cstatus[(port + 1)].v24_signals & S_RTC) > 0)
{
// drop DTR
PDEBUG("Drop DTR.\n");
msc[2] = 3 | ((63 & (port + 1)) << 2);
msc[3] = cstatus[(port + 1)].v24_signals & ~S_RTC;
cstatus[(port + 1)].v24_signals = msc[3];
write_frame(0, msc, 4, UIH);
}
else if ((tiosp->c_cflag & CBAUD) != B0 && (cstatus[(port + 1)].v24_signals & S_RTC) == 0)
{
// DTR up
PDEBUG("DTR up.\n");
msc[2] |= ((63 & (port + 1)) << 2);
msc[3] = cstatus[(port + 1)].v24_signals | S_RTC;
cstatus[(port + 1)].v24_signals = msc[3];
write_frame(0, msc, 4, UIH);
}
#ifdef DEBUG
PDEBUG("Set termios for ussp port %d\n", port);
PDEBUG("\tinput mode flags: 0x%04x\n", tiosp->c_iflag);
PDEBUG("\toutput mode flags: 0x%04x\n", tiosp->c_oflag);
PDEBUG("\tcontrol mode flags: 0x%04x\n", tiosp->c_cflag);
PDEBUG("\tlocal mode flags: 0x%04x\n", tiosp->c_lflag);
PDEBUG("\tline discipline: 0x%02x\n", tiosp->c_line);
PDEBUG("\tcontrol characters: ");
for (i = 0; i < NCCS; i++)
PDEBUG("0x%02x ", tiosp->c_cc[i]);
PDEBUG("\n");
PDEBUG("\tinput speed: 0x%02x (%i)\n", tiosp->c_ispeed, tiosp->c_ispeed);
PDEBUG("\toutput speed: 0x%02x (%i)\n", tiosp->c_ospeed, tiosp->c_ospeed);
#endif
break;
case USSP_MSC:
PDEBUG("Modem signal change\n");
msc[2] = 3 | ((63 & (port + 1)) << 2);
msc[3] = S_DV;
if ((top->arg & USSP_DTR) == USSP_DTR)
{
msc[3] |= S_RTC;
PDEBUG("RTC\n");
}
if ((top->arg & USSP_RTS) == USSP_RTS)
{
msc[3] |= S_RTR;
PDEBUG("RTR\n");
}
else
{
msc[3] |= S_FC;
PDEBUG("FC\n");
}
cstatus[(port + 1)].v24_signals = msc[3]; // save the signals
write_frame(0, msc, 4, UIH);
break;
default:
PDEBUG("Unknown code: %d\n", top->op);
break;
}
}
/* remaining bytes in partial packet */
return ((char *) op + n) - (char *) top;
#else
int written = 0;
int i = 0;
int last = 0;
// try to write 5 times
while (written != len && i < WRITE_RETRIES)
{
last = write_frame(port + 1, buf + written,
len - written, UIH);
written += last;
if (last == 0) {
i++;
}
}
if (i == WRITE_RETRIES)
{
if(_debug)
syslog(LOG_DEBUG,"Couldn't write data to channel %d. Wrote only %d bytes, when should have written %ld.\n",
(port + 1), written, (long)len);
}
return 0;
#endif
}
int ussp_send_data(unsigned char *buf, int n, int port)
{
#if 0
struct ussp_operation *op;
op = malloc(sizeof(struct ussp_operation) + n);
op->op = USSP_READ;
op->arg = 0;
op->len = n;
memcpy(op->data, buf, n);
write(ussp_fd[port], op, sizeof(struct ussp_operation) + n);
free(op);
#else
if(_debug)
syslog(LOG_DEBUG,"send data to port virtual port %d\n", port);
write(ussp_fd[port], buf, n);
#endif
return n;
}
// Returns 1 if found, 0 otherwise. needle must be null-terminated.
// strstr might not work because WebBox sends garbage before the first OK
int findInBuf(char* buf, int len, char* needle) {
int i;
int needleMatchedPos=0;
if (needle[0] == '\0') {
return 1;
}
for (i=0;i<len;i++) {
if (needle[needleMatchedPos] == buf[i]) {
needleMatchedPos++;
if (needle[needleMatchedPos] == '\0') {
// Entire needle was found
return 1;
}
} else {
needleMatchedPos=0;
}
}
return 0;
}
/* Sends an AT-command to a given serial port and waits
* for reply.
*
* PARAMS:
* fd - file descriptor
* cmd - command
* to - how many microseconds to wait for response (this is done 100 times)
* RETURNS:
* 1 on success (OK-response), 0 otherwise
*/
int at_command(int fd, char *cmd, int to)
{
fd_set rfds;
struct timeval timeout;
unsigned char buf[1024];
int sel, len, i;
int returnCode = 0;
int wrote = 0;
if(_debug)
syslog(LOG_DEBUG, "is in %s\n", __FUNCTION__);
wrote = write(fd, cmd, strlen(cmd));
if(_debug)
syslog(LOG_DEBUG, "Wrote %s \n", cmd);
tcdrain(fd);
sleep(1);
//memset(buf, 0, sizeof(buf));
//len = read(fd, buf, sizeof(buf));
for (i = 0; i < 100; i++)
{
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
timeout.tv_sec = 0;
timeout.tv_usec = to;
if ((sel = select(fd + 1, &rfds, NULL, NULL, &timeout)) > 0)
//if ((sel = select(fd + 1, &rfds, NULL, NULL, NULL)) > 0)
{
if (FD_ISSET(fd, &rfds))
{
memset(buf, 0, sizeof(buf));
len = read(fd, buf, sizeof(buf));
if(_debug)
syslog(LOG_DEBUG, " read %d bytes == %s\n", len, buf);
//if (strstr(buf, "\r\nOK\r\n") != NULL)
if (findInBuf(buf, len, "OK"))
{
returnCode = 1;
break;
}
if (findInBuf(buf, len, "ERROR"))
break;
}
}
}
return returnCode;
}
char *createSymlinkName(int idx) {
if (devSymlinkPrefix == NULL) {
return NULL;
}
char* symLinkName = malloc(strlen(devSymlinkPrefix)+255);
sprintf(symLinkName, "%s%d", devSymlinkPrefix, idx);
return symLinkName;
}
int open_pty(char* devname, int idx) {
struct termios options;
int fd = open(devname, O_RDWR | O_NONBLOCK);
char *symLinkName = createSymlinkName(idx);
if (fd != -1) {
if (symLinkName) {
char* ptsSlaveName = ptsname(fd);
// Create symbolic device name, e.g. /dev/mux0
unlink(symLinkName);
if (symlink(ptsSlaveName, symLinkName) != 0) {
syslog(LOG_ERR,"Can't create symbolic link %s -> %s. %s (%d).\n", symLinkName, ptsSlaveName, strerror(errno), errno);
}
}
// get the parameters
tcgetattr(fd, &options);
// set raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag &= ~(INLCR | ICRNL | IGNCR);
// set raw output
options.c_oflag &= ~OPOST;
options.c_oflag &= ~OLCUC;
options.c_oflag &= ~ONLRET;
options.c_oflag &= ~ONOCR;
options.c_oflag &= ~OCRNL;
tcsetattr(fd, TCSANOW, &options);
if (strcmp(devname, "/dev/ptmx") == 0) {
// Otherwise programs cannot access the pseudo terminals
grantpt(fd);
unlockpt(fd);
// set permissions 660
chmod(symLinkName, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
}
}
free(symLinkName);
return fd;
}
/**
* Determine baud rate index for CMUX command
*/
int indexOfBaud(int baudrate) {
int i;
for (i = 0; i < sizeof(baudrates) / sizeof(baudrates[0]); ++i) {
if (baudrates[i] == baudrate)
return i;
}
return 0;
}
/**
* Set serial port options. Then switch baudrate to zero for a while
* and then back up. This is needed to get some modems
* (such as Siemens MC35i) to wake up.
*/
void setAdvancedOptions(int fd, speed_t baud) {
struct termios options;
struct termios options_cpy;
fcntl(fd, F_SETFL, 0);
// get the parameters
tcgetattr(fd, &options);
// Do like minicom: set 0 in speed options
cfsetispeed(&options, 0);
cfsetospeed(&options, 0);
options.c_iflag = IGNBRK;
// Enable the receiver and set local mode and 8N1
options.c_cflag = (CLOCAL | CREAD | CS8 | HUPCL);
// enable hardware flow control (CNEW_RTCCTS)
// options.c_cflag |= CRTSCTS;
// Set speed
options.c_cflag |= baud;
/*
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE; // Could this be wrong!?!?!?
*/
// set raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag &= ~(INLCR | ICRNL | IGNCR);
// set raw output
options.c_oflag &= ~OPOST;
options.c_oflag &= ~OLCUC;
options.c_oflag &= ~ONLRET;
options.c_oflag &= ~ONOCR;
options.c_oflag &= ~OCRNL;
// Set the new options for the port...
options_cpy = options;
tcsetattr(fd, TCSANOW, &options);
options = options_cpy;
// Do like minicom: set speed to 0 and back
options.c_cflag &= ~baud;
tcsetattr(fd, TCSANOW, &options);
options = options_cpy;
sleep(1);
options.c_cflag |= baud;
tcsetattr(fd, TCSANOW, &options);
}
/* Opens serial port, set's it to 57600bps 8N1 RTS/CTS mode.
*
* PARAMS:
* dev - device name
* RETURNS :
* file descriptor or -1 on error
*/
int open_serialport(char *dev)
{
int fd;
if(_debug)
syslog(LOG_DEBUG, "is in %s\n" , __FUNCTION__);
fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd != -1)
{
int index = indexOfBaud(baudrate);
if(_debug)
syslog(LOG_DEBUG, "serial opened\n" );
if (index > 0) {
// Switch the baud rate to zero and back up to wake up
// the modem
setAdvancedOptions(fd, baud_bits[index]);
} else {
struct termios options;
// The old way. Let's not change baud settings
fcntl(fd, F_SETFL, 0);
// get the parameters
tcgetattr(fd, &options);
// Set the baud rates to 57600...
// cfsetispeed(&options, B57600);
// cfsetospeed(&options, B57600);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
// No parity (8N1):
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// enable hardware flow control (CNEW_RTCCTS)
// options.c_cflag |= CRTSCTS;
// set raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag &= ~(INLCR | ICRNL | IGNCR);
// set raw output
options.c_oflag &= ~OPOST;
options.c_oflag &= ~OLCUC;
options.c_oflag &= ~ONLRET;
options.c_oflag &= ~ONOCR;
options.c_oflag &= ~OCRNL;
// Set the new options for the port...
tcsetattr(fd, TCSANOW, &options);
}
}
return fd;
}
// Prints information on a frame
void print_frame(GSM0710_Frame * frame)
{
if(_debug)
{
syslog(LOG_DEBUG, "is in %s\n" , __FUNCTION__);
syslog(LOG_DEBUG,"Received ");
}
switch((frame->control & ~PF))
{
case SABM:
if(_debug)
syslog(LOG_DEBUG,"SABM ");
break;
case UIH:
if(_debug)
syslog(LOG_DEBUG,"UIH ");
break;
case UA:
if(_debug)
syslog(LOG_DEBUG,"UA ");
break;
case DM:
if(_debug)
syslog(LOG_DEBUG,"DM ");
break;
case DISC:
if(_debug)
syslog(LOG_DEBUG,"DISC ");
break;
case UI:
if(_debug)
syslog(LOG_DEBUG,"UI ");
break;
default:
if(_debug)
syslog(LOG_DEBUG,"unkown (control=%d) ", frame->control);
break;
}
if(_debug)
syslog(LOG_DEBUG," frame for channel %d.\n", frame->channel);
if (frame->data_length > 0)
{
if(_debug)
{
syslog(LOG_DEBUG,"frame->data = %s / size = %d\n",frame->data, frame->data_length);
//fwrite(frame->data, sizeof(char), frame->data_length, stdout);
syslog(LOG_DEBUG,"\n");
}
}
}
/* Handles commands received from the control channel.
*/
void handle_command(GSM0710_Frame * frame)
{
#if 1
unsigned char type, signals;
int length = 0, i, type_length, channel, supported = 1;
unsigned char *response;
// struct ussp_operation op;
if(_debug)
syslog(LOG_DEBUG, "is in %s\n" , __FUNCTION__);
if (frame->data_length > 0)
{
type = frame->data[0]; // only a byte long types are handled now
// skip extra bytes
for (i = 0; (frame->data_length > i && (frame->data[i] & EA) == 0); i++);
i++;
type_length = i;
if ((type & CR) == CR)
{
// command not ack
// extract frame length
while (frame->data_length > i)
{
length = (length * 128) + ((frame->data[i] & 254) >> 1);
if ((frame->data[i] & 1) == 1)
break;
i++;
}
i++;
switch((type & ~CR))
{
case C_CLD:
syslog(LOG_INFO,"The mobile station requested mux-mode termination.\n");
if (faultTolerant) {
// Signal restart
restart = 1;
} else {
terminate = 1;
terminateCount = -1; // don't need to close down channels
}
break;
case C_TEST:
#ifdef DEBUG
if(_debug)
syslog(LOG_DEBUG,"Test command: ");
if(_debug)
syslog(LOG_DEBUG,"frame->data = %s / frame->data_length = %d\n",frame->data + i, frame->data_length - i);
//fwrite(frame->data + i, sizeof(char), frame->data_length - i, stdout);
#endif
break;
case C_MSC:
if (i + 1 < frame->data_length)
{
channel = ((frame->data[i] & 252) >> 2);
i++;
signals = (frame->data[i]);
// op.op = USSP_MSC;
// op.arg = USSP_RTS;
// op.len = 0;
if(_debug)
syslog(LOG_DEBUG,"Modem status command on channel %d.\n", channel);
if ((signals & S_FC) == S_FC)
{
if(_debug)
syslog(LOG_DEBUG,"No frames allowed.\n");
}
else
{
// op.arg |= USSP_CTS;
if(_debug)
syslog(LOG_DEBUG,"Frames allowed.\n");
}
if ((signals & S_RTC) == S_RTC)
{
// op.arg |= USSP_DSR;
if(_debug)
syslog(LOG_DEBUG,"RTC\n");
}
if ((signals & S_IC) == S_IC)
{
// op.arg |= USSP_RI;
if(_debug)
syslog(LOG_DEBUG,"Ring\n");
}
if ((signals & S_DV) == S_DV)
{
// op.arg |= USSP_DCD;
if(_debug)
syslog(LOG_DEBUG,"DV\n");
}
// if (channel > 0)
// write(ussp_fd[(channel - 1)], &op, sizeof(op));
}
else
{
syslog(LOG_ERR,"ERROR: Modem status command, but no info. i: %d, len: %d, data-len: %d\n", i, length,
frame->data_length);
}
break;
default:
syslog(LOG_ALERT,"Unknown command (%d) from the control channel.\n", type);
response = malloc(sizeof(char) * (2 + type_length));
response[0] = C_NSC;
// supposes that type length is less than 128
response[1] = EA & ((127 & type_length) << 1);
i = 2;
while (type_length--)
{
response[i] = frame->data[(i - 2)];
i++;
}
write_frame(0, response, i, UIH);
free(response);
supported = 0;
break;
}
if (supported)
{
// acknowledge the command
frame->data[0] = frame->data[0] & ~CR;
write_frame(0, frame->data, frame->data_length, UIH);
}
}
else
{
// received ack for a command
if (COMMAND_IS(C_NSC, type))
{
syslog(LOG_ALERT,"The mobile station didn't support the command sent.\n");
}
else
{
if(_debug)
syslog(LOG_DEBUG,"Command acknowledged by the mobile station.\n");
}
}
}
#endif
}
// shows how to use this program
void usage(char *_name)
{
fprintf(stderr,"\nUsage: %s [options] <pty1> <pty2> ...\n",_name);
fprintf(stderr," <ptyN> : pty devices (e.g. /dev/ptya0)\n\n");
fprintf(stderr,"options:\n");
fprintf(stderr," -p <serport> : Serial port device to connect to [/dev/modem]\n");
fprintf(stderr," -f <framsize> : Maximum frame size [32]\n");
fprintf(stderr," -d : Debug mode, don't fork\n");
fprintf(stderr," -m <modem> : Modem (mc35, mc75, generic, ...)\n");
fprintf(stderr," -b <baudrate> : MUX mode baudrate (0,9600,19200, ...)\n");
fprintf(stderr," -P <PIN-code> : PIN code to fed to the modem\n");
fprintf(stderr," -s <symlink-prefix> : Prefix for the symlinks of slave devices (e.g. /dev/mux)\n");
fprintf(stderr," -w : Wait for deamon startup success/failure\n");
fprintf(stderr," -r : Restart automatically if the modem stops responding\n");
fprintf(stderr," -h : Show this help message\n");
}
/* Extracts and handles frames from the receiver buffer.
*
* PARAMS:
* buf - the receiver buffer
*/
int extract_frames(GSM0710_Buffer * buf)
{
// version test for Siemens terminals to enable version 2 functions
static char version_test[] = "\x23\x21\x04TEMUXVERSION2\0\0";
int framesExtracted = 0;
GSM0710_Frame *frame;
if(_debug)
syslog(LOG_DEBUG, "is in %s\n" , __FUNCTION__);
while ((frame = gsm0710_buffer_get_frame(buf)))
{
++framesExtracted;
if ((FRAME_IS(UI, frame) || FRAME_IS(UIH, frame)))
{
if(_debug)
syslog(LOG_DEBUG, "is (FRAME_IS(UI, frame) || FRAME_IS(UIH, frame))\n");
if (frame->channel > 0)
{
if(_debug)
syslog(LOG_DEBUG,"frame->channel > 0\n");
// data from logical channel
ussp_send_data(frame->data, frame->data_length, frame->channel - 1);
}
else
{
// control channel command
if(_debug)
syslog(LOG_DEBUG,"control channel command\n");
handle_command(frame);
}
}
else
{
// not an information frame
if(_debug)
syslog(LOG_DEBUG,"not an information frame\n");
#ifdef DEBUG
print_frame(frame);
#endif
switch((frame->control & ~PF))
{
case UA:
if(_debug)
syslog(LOG_DEBUG,"is FRAME_IS(UA, frame)\n");
if (cstatus[frame->channel].opened == 1)
{
syslog(LOG_INFO,"Logical channel %d closed.\n", frame->channel);
cstatus[frame->channel].opened = 0;
}
else
{
cstatus[frame->channel].opened = 1;
if (frame->channel == 0)
{
syslog(LOG_INFO,"Control channel opened.\n");
// send version Siemens version test
write_frame(0, version_test, 18, UIH);
}
else
{