regular status reporting disabled back again
[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   Released under the terms of the GNU GPL, see file COPYING for more details.
10
11   This file provides routines to handle processing of data when connected in
12   fax or data mode. Converts data from/to GSM phone to virtual modem
13   interface.
14
15 */
16
17 #include "config.h"
18
19 #define         __data_datapump_c
20
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <termios.h>
27 #include <grp.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #ifndef UCLINUX
33 #include <sys/poll.h>
34 #endif
35 #include <unistd.h>
36
37
38 #include "misc.h"
39 #include "gsm-common.h"
40 #include "gsm-api.h"
41 #include "data/at-emulator.h"
42 #include "data/virtmodem.h"
43 #include "data/datapump.h"
44 #include "data/rlp-common.h"
45
46 /* Global variables */
47 extern bool CommandMode;
48
49 /* Local variables */
50 extern int              PtyRDFD;        /* File descriptor for reading and writing to/from */
51 extern int              PtyWRFD;        /* pty interface - only different in debug mode. */ 
52 static u8 pluscount;
53 static bool connected;
54
55 static int DP_CallBack(RLP_UserInds ind, u8 *buffer, int length);
56
57 bool DP_Initialise(int read_fd, int write_fd)
58 {
59         PtyRDFD = read_fd;
60         PtyWRFD = write_fd;
61         RLP_Initialise(GSM->SendRLPFrame, DP_CallBack);
62         RLP_SetUserRequest(Attach_Req,true);
63         pluscount=0;
64         connected=false;
65         return true;
66 }
67
68
69 static int DP_CallBack(RLP_UserInds ind, u8 *buffer, int length)
70 {
71         int temp;
72
73         switch(ind) {
74         case Data:
75                 if (CommandMode==false) write(PtyWRFD, buffer, length);
76                 break;
77         case Conn_Ind:
78                 if (CommandMode==false) ATEM_ModemResult(MR_CARRIER);
79                 RLP_SetUserRequest(Conn_Req,true);
80                 break;
81         case StatusChange:
82                 if (buffer[0]==0) {
83                         connected=true;
84                         if (CommandMode==false) ATEM_ModemResult(MR_CONNECT);
85                 }
86                 break;
87         case Disc_Ind:
88                 if (CommandMode==false) ATEM_ModemResult(MR_NOCARRIER);
89                 connected=false;
90                 /* Set the call passup back to the at emulator */
91                 GSM->DialData(NULL,-1,&ATEM_CallPassup);
92                 CommandMode=true;
93                 break;
94         case Reset_Ind:
95                 RLP_SetUserRequest(Reset_Resp,true);
96                 break;
97         case GetData: {
98                 fd_set readfds;
99                 struct timeval timeout;
100
101                 FD_ZERO(&readfds);
102                 FD_SET(PtyRDFD,&readfds);
103                 timeout.tv_sec=0;
104                 timeout.tv_usec=0;
105
106                 if (select(PtyRDFD+1,&readfds,NULL/*writefds*/,NULL/*exceptfds*/,&timeout)) {
107
108                         /* Check if the program has closed */
109                         /* Return to command mode */
110                         /* Note that the call will still be in progress, */
111                         /* as with a normal modem (I think) */
112
113                         if (!FD_ISSET(PtyRDFD,&readfds)) { 
114                                 CommandMode=true;
115                                 /* Set the call passup back to the at emulator */
116                                 GSM->DialData(NULL,-1,&ATEM_CallPassup);
117                                 return 0;
118                         }
119
120                         temp = read(PtyRDFD, buffer, length);
121
122                         if (temp<0) return 0; /* FIXME - what do we do now? */
123
124                         /* This will only check +++ and the beginning of a read */
125                         /* But there should be a pause before it anyway */
126       
127                         if (buffer[0]=='+') {
128                                 pluscount++;
129                                 if (temp>1) {
130                                         if (buffer[1]=='+') pluscount++;
131                                         else pluscount=0;
132                                         if (temp>2) {
133                                                 if (buffer[2]=='+') pluscount++;
134                                                 else pluscount=0;
135                                                 if (temp>3) pluscount=0;
136                                         }
137                                 }
138                         } else pluscount=0;
139       
140                         if (pluscount==3) {
141                                 CommandMode=true;
142                                 /* Set the call passup back to the at emulator */
143                                 GSM->DialData(NULL,-1,&ATEM_CallPassup);
144                                 ATEM_ModemResult(MR_OK);
145                                 break;
146                         }
147       
148                         return temp;
149                 }
150                 return 0;
151                 } break;
152
153         default:
154
155         }
156         return 0;
157 }
158
159 void DP_CallPassup(char c)
160 {
161         switch (c) {
162         case 'D':
163                 if (CommandMode==false) ATEM_ModemResult(MR_CARRIER);
164                 RLP_SetUserRequest(Conn_Req,true);
165                 connected=true;
166                 break;
167         case ' ':
168                 CommandMode=true;
169                 /* Set the call passup back to the at emulator */
170                 GSM->DialData(NULL,-1,&ATEM_CallPassup);
171                 ATEM_ModemResult(MR_NOCARRIER);
172                 RLP_SetUserRequest(Disc_Req, true);
173                 connected=false;
174                 break;
175         default:
176                 break;
177         }
178 }