6 A Linux/Unix toolset and driver for Nokia mobile phones.
8 Released under the terms of the GNU GPL, see file COPYING for more details.
10 This file provides a virtual modem interface to the GSM phone by calling
11 code in gsm-api.c, at-emulator.c and datapump.c. The code here provides
12 the overall framework and coordinates switching between command mode
13 (AT-emulator) and "online" mode where the data pump code translates data
14 from/to the GSM handset and the modem data/fax stream.
22 /* This is the right way to include stdlib with __USE_XOPEN defined */
24 # define _XOPEN_SOURCE 500
25 # include <features.h>
35 #include <sys/types.h>
46 #include "gsm-common.h"
47 #include "data/at-emulator.h"
48 #include "data/datapump.h"
49 #include "data/virtmodem.h"
50 #include "data/rlp-common.h"
52 /* Global variables */
54 //extern bool TerminateThread;
58 int PtyRDFD; /* File descriptor for reading and writing to/from */
59 int PtyWRFD; /* pty interface - only different in debug mode. */
61 static bool UseSTDIO; /* Use STDIO for debugging purposes instead of pty */
64 static bool VM_ThreadLoop(void);
65 static GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime);
66 static int VM_PtySetup(char *bindir);
68 /* If initialised in debug mode, stdin/out is used instead
69 of ptys for interface. */
70 bool VM_Initialise(char *model,char *port, char *initlength, GSM_ConnectionType connection, char *bindir, bool debug_mode, bool GSMInit,char *synchronizetime)
74 if (debug_mode == true) {
83 fprintf (stderr , "Initialising GSM\n");
85 if ((VM_GSMInitialise(model, port, initlength, connection, synchronizetime) != GE_NONE)) {
86 fprintf (stderr, _("VM_Initialise - VM_GSMInitialise failed!\n"));
93 if (VM_PtySetup(bindir) < 0) {
94 fprintf (stderr, _("VM_Initialise - VM_PtySetup failed!\n"));
98 if (ATEM_Initialise(PtyRDFD, PtyWRFD, model, port) != true) {
99 fprintf (stderr, _("VM_Initialise - ATEM_Initialise failed!\n"));
103 if (DP_Initialise(PtyRDFD, PtyWRFD) != true) {
104 fprintf (stderr, _("VM_Initialise - DP_Initialise failed!\n"));
108 /* Create and start thread, */
109 return VM_ThreadLoop();
112 static void VM_CharHandler(void);
114 static bool VM_ThreadLoop(void)
118 struct timeval timeout;
120 /* Note we can't use signals here as they are already used
121 in the FBUS code. This may warrant changing the FBUS
122 code around sometime to use select instead to free up
123 the SIGIO handler for mainline code. */
128 } else { /* If we are in data mode, leave it to datapump to get the data */
131 FD_SET(PtyRDFD,&readfds);
133 timeout.tv_usec=500*1000;
135 res = select(PtyRDFD+1,&readfds,NULL/*writefds*/,NULL/*exceptfds*/,&timeout);
138 case 0: /* Timeout */
144 perror("VM_ThreadLoop - select");
148 if (FD_ISSET(PtyRDFD,&readfds)) {
150 } else usleep(500); /* Probably the file has been closed */
158 /* Application should call VM_Terminate to shut down
159 the virtual modem thread */
160 void VM_Terminate(void)
168 static int VM_GetMasterPty(char **name);
170 /* Open pseudo tty interface and (in due course create a symlink
171 to be /dev/gnokii etc. ) */
173 static int VM_PtySetup(char *bindir)
177 char mgnokiidev[200];
186 strncpy(mgnokiidev, bindir, 200);
187 strcat(mgnokiidev, "/");
189 strncat(mgnokiidev, "mgnokiidev", 200 - strlen(bindir));
193 PtyRDFD = STDIN_FILENO;
194 PtyWRFD = STDOUT_FILENO;
198 PtyRDFD = VM_GetMasterPty(&slave_name);
200 fprintf (stderr, _("Couldn't open pty!\n"));
206 /* Check we haven't been installed setuid root for some reason
207 if so, don't create /dev/gnokii */
208 if (getuid() != geteuid()) {
209 fprintf(stderr, _("gnokiid should not be installed setuid root!\n"));
215 fprintf (stderr, _("Slave pty is %s, calling %s to create /dev/gnokii.\n"), slave_name,
225 /* Create command line, something line ./mkgnokiidev ttyp0 */
226 sprintf(cmdline, "%s %s", mgnokiidev, slave_name);
228 /* And use system to call it. */
229 err = system (cmdline);
232 /* Remove symlink in case it already exists. Don't care if it fails. */
233 unlink ("/dev/gnokii");
236 err = symlink(slave_name, "/dev/gnokii");
244 /* Handler called when characters received from serial port.
245 calls state machine code to process it. */
246 static void VM_CharHandler(void)
248 unsigned char buffer[255];
252 /* If we are in command mode, get the character, otherwise leave it */
254 if (CommandMode && ATEM_Initialised) {
256 res = read(PtyRDFD, buffer, 255);
258 /* A returned value of -1 means something serious has gone wrong - so quit!! */
259 /* Note that file closure etc. should have been dealt with in ThreadLoop */
262 // TerminateThread=true;
266 ATEM_HandleIncomingData(buffer, res);
270 /* Initialise GSM interface, returning GSM_Error as appropriate */
271 static GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime)
276 /* Initialise the code for the GSM interface. */
278 error = GSM_Initialise(model,port, initlength, connection, RLP_DisplayF96Frame, synchronizetime);
280 if (error != GE_NONE) {
281 fprintf(stderr, _("GSM/FBUS init failed! (Unknown model ?). Quitting.\n"));
285 /* First (and important!) wait for GSM link to be active. We allow 10
288 while (count++ < 200 && *GSM_LinkOK == false) {
292 if (*GSM_LinkOK == false) {
293 fprintf (stderr, _("Hmmm... GSM_LinkOK never went true. Quitting. \n"));
301 Takes a double-indirect character pointer in which to put a slave
302 name, and returns an integer file descriptor. If it returns < 0, an
303 error has occurred. Otherwise, it has returned the master pty
304 file descriptor, and fills in *name with the name of the
305 corresponding slave pty. Once the slave pty has been opened,
306 you are responsible to free *name. Code is from Developling Linux
307 Applications by Troan and Johnson */
310 static int VM_GetMasterPty(char **name) {
312 #ifdef USE_UNIX98PTYS
315 master = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_NONBLOCK);
317 err = grantpt(master);
318 err = err || unlockpt(master);
320 *name = ptsname(master);
325 #else /* USE_UNIX98PTYS */
327 /* default to returning error */
330 /* create a dummy name to fill in */
331 *name = strdup("/dev/ptyXX");
333 /* search for an unused pty */
334 for (i=0; i<16 && master <= 0; i++) {
335 for (j=0; j<16 && master <= 0; j++) {
336 (*name)[8] = "pqrstuvwxyzPQRST"[i];
337 (*name)[9] = "0123456789abcdef"[j];
338 /* open the master pty */
339 if ((master = open(*name, O_RDWR | O_NOCTTY | O_NONBLOCK )) < 0) {
340 if (errno == ENOENT) {
341 /* we are out of pty devices */
348 if ((master < 0) && (i == 16) && (j == 16)) {
349 /* must have tried every pty unsuccessfully */
354 /* By substituting a letter, we change the master pty
355 * name into the slave pty name.
359 #endif /* USE_UNIX98PTYS */