Found in "gnokii-working" directory, some November-patches version
[gnokii.git] / common / data / datapump.c
1 /*
2
3   $Id$
4
5   G N O K I I
6
7   A Linux/Unix toolset and driver for Nokia mobile phones.
8
9   Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
10
11   Released under the terms of the GNU GPL, see file COPYING for more details.
12
13   This file provides routines to handle processing of data when connected in
14   fax or data mode. Converts data from/to GSM phone to virtual modem
15   interface.
16
17   $Log$
18   Revision 1.1.1.3  2002/04/03 00:08:04  short
19   Found in "gnokii-working" directory, some November-patches version
20
21   Revision 1.3  2001/02/21 19:57:00  chris
22   More fiddling with the directory layout
23
24   Revision 1.2  2001/02/17 22:40:51  chris
25   ATA support
26
27
28 */
29
30 #define         __data_datapump_c
31
32
33 #include <stdio.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <signal.h>
37 #include <termios.h>
38 #include <grp.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/poll.h>
44 #include <unistd.h>
45
46
47 #include "misc.h"
48 #include "gsm-common.h"
49 #include "gsm-api.h"
50 #include "data/at-emulator.h"
51 #include "data/virtmodem.h"
52 #include "data/datapump.h"
53 #include "data/rlp-common.h"
54
55 /* Global variables */
56 extern bool CommandMode;
57
58 /* Local variables */
59 int             PtyRDFD;        /* File descriptor for reading and writing to/from */
60 int             PtyWRFD;        /* pty interface - only different in debug mode. */ 
61 struct pollfd ufds;
62 u8 pluscount;
63 bool connected;
64
65 bool DP_Initialise(int read_fd, int write_fd)
66 {
67         PtyRDFD = read_fd;
68         PtyWRFD = write_fd;
69         ufds.fd=PtyRDFD;
70         ufds.events=POLLIN;
71         RLP_Initialise(GSM->SendRLPFrame, DP_CallBack);
72         RLP_SetUserRequest(Attach_Req,true);
73         pluscount=0;
74         connected=false;
75         return true;
76 }
77
78
79 int DP_CallBack(RLP_UserInds ind, u8 *buffer, int length)
80 {
81         int temp;
82
83         switch(ind) {
84         case Data:
85                 if (CommandMode==false) write(PtyWRFD, buffer, length);
86                 break;
87         case Conn_Ind:
88                 if (CommandMode==false) ATEM_ModemResult(MR_CARRIER);
89                 RLP_SetUserRequest(Conn_Req,true);
90                 break;
91         case StatusChange:
92                 if (buffer[0]==0) {
93                         connected=true;
94                         if (CommandMode==false) ATEM_ModemResult(MR_CONNECT);
95                 }
96                 break;
97         case Disc_Ind:
98                 if (CommandMode==false) ATEM_ModemResult(MR_NOCARRIER);
99                 connected=false;
100                 /* Set the call passup back to the at emulator */
101                 GSM->DialData(NULL,-1,&ATEM_CallPassup);
102                 CommandMode=true;
103                 break;
104         case Reset_Ind:
105                 RLP_SetUserRequest(Reset_Resp,true);
106                 break;
107         case GetData:
108                 if (poll(&ufds,1,0)) {
109
110                         /* Check if the program has closed */
111                         /* Return to command mode */
112                         /* Note that the call will still be in progress, */
113                         /* as with a normal modem (I think) */
114
115                         if (ufds.revents!=POLLIN) { 
116                                 CommandMode=true;
117                                 /* Set the call passup back to the at emulator */
118                                 GSM->DialData(NULL,-1,&ATEM_CallPassup);
119                                 return 0;
120                         }
121
122                         temp = read(PtyRDFD, buffer, length);
123
124                         if (temp<0) return 0; /* FIXME - what do we do now? */
125
126                         /* This will only check +++ and the beginning of a read */
127                         /* But there should be a pause before it anyway */
128       
129                         if (buffer[0]=='+') {
130                                 pluscount++;
131                                 if (temp>1) {
132                                         if (buffer[1]=='+') pluscount++;
133                                         else pluscount=0;
134                                         if (temp>2) {
135                                                 if (buffer[2]=='+') pluscount++;
136                                                 else pluscount=0;
137                                                 if (temp>3) pluscount=0;
138                                         }
139                                 }
140                         } else pluscount=0;
141       
142                         if (pluscount==3) {
143                                 CommandMode=true;
144                                 /* Set the call passup back to the at emulator */
145                                 GSM->DialData(NULL,-1,&ATEM_CallPassup);
146                                 ATEM_ModemResult(MR_OK);
147                                 break;
148                         }
149       
150                         return temp;
151                 }
152                 return 0;
153                 break;
154
155         default:
156
157         }
158         return 0;
159 }
160
161 void DP_CallPassup(char c)
162 {
163         switch (c) {
164         case 'D':
165                 if (CommandMode==false) ATEM_ModemResult(MR_CARRIER);
166                 RLP_SetUserRequest(Conn_Req,true);
167                 connected=true;
168                 break;
169         case ' ':
170                 CommandMode=true;
171                 /* Set the call passup back to the at emulator */
172                 GSM->DialData(NULL,-1,&ATEM_CallPassup);
173                 ATEM_ModemResult(MR_NOCARRIER);
174                 RLP_SetUserRequest(Disc_Req, true);
175                 connected=false;
176                 break;
177         default:
178                 break;
179         }
180 }