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 */
55 #define GNOKII_DEV "/var/gnokii-dev"
57 //extern bool TerminateThread;
61 int PtyRDFD; /* File descriptor for reading and writing to/from */
62 int PtyWRFD; /* pty interface - only different in debug mode. */
64 static bool UseSTDIO; /* Use STDIO for debugging purposes instead of pty */
67 static GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime);
68 static int VM_PtySetup(char *bindir);
70 /* If initialised in debug mode, stdin/out is used instead
71 of ptys for interface. */
72 bool VM_Initialise(char *model,char *port, char *initlength, GSM_ConnectionType connection, char *bindir, bool debug_mode, bool GSMInit,char *synchronizetime)
76 if (debug_mode == true) {
85 fprintf (stderr , "Initialising GSM\n");
87 if ((VM_GSMInitialise(model, port, initlength, connection, synchronizetime) != GE_NONE)) {
88 fprintf (stderr, _("VM_Initialise - VM_GSMInitialise failed!\n"));
95 if (VM_PtySetup(bindir) < 0) {
96 fprintf (stderr, _("VM_Initialise - VM_PtySetup failed!\n"));
100 if (ATEM_Initialise(PtyRDFD, PtyWRFD, model, port) != true) {
101 fprintf (stderr, _("VM_Initialise - ATEM_Initialise failed!\n"));
105 if (DP_Initialise(PtyRDFD, PtyWRFD) != true) {
106 fprintf (stderr, _("VM_Initialise - DP_Initialise failed!\n"));
110 /* Create and start thread, */
111 return VM_ThreadLoop();
114 static void VM_CharHandler(void);
116 bool VM_ThreadLoop(void)
120 struct timeval timeout;
122 /* Note we can't use signals here as they are already used
123 in the FBUS code. This may warrant changing the FBUS
124 code around sometime to use select instead to free up
125 the SIGIO handler for mainline code. */
130 } else { /* If we are in data mode, leave it to datapump to get the data */
133 FD_SET(PtyRDFD,&readfds);
135 timeout.tv_usec=500*1000;
137 res = select(PtyRDFD+1,&readfds,NULL/*writefds*/,NULL/*exceptfds*/,&timeout);
140 case 0: /* Timeout */
146 perror("VM_ThreadLoop - select");
150 if (FD_ISSET(PtyRDFD,&readfds)) {
152 } else usleep(500); /* Probably the file has been closed */
160 /* Application should call VM_Terminate to shut down
161 the virtual modem thread */
162 void VM_Terminate(void)
170 static int VM_GetMasterPty(char **name);
172 /* Open pseudo tty interface and (in due course create a symlink
173 to be /dev/gnokii etc. ) */
175 static int VM_PtySetup(char *bindir)
179 char mgnokiidev[200];
188 strncpy(mgnokiidev, bindir, 200);
189 strcat(mgnokiidev, "/");
191 strncat(mgnokiidev, "mgnokiidev", 200 - strlen(bindir));
195 PtyRDFD = STDIN_FILENO;
196 PtyWRFD = STDOUT_FILENO;
200 PtyRDFD = VM_GetMasterPty(&slave_name);
202 fprintf (stderr, _("Couldn't open pty!\n"));
208 /* Check we haven't been installed setuid root for some reason
209 if so, don't create /dev/gnokii */
210 if (getuid() != geteuid()) {
211 fprintf(stderr, _("gnokiid should not be installed setuid root!\n"));
217 fprintf (stderr, _("Slave pty is %s, calling %s to create \"%s\".\n"), slave_name,
227 /* Create command line, something line ./mkgnokiidev ttyp0 */
228 sprintf(cmdline, "%s %s", mgnokiidev, slave_name);
230 /* And use system to call it. */
231 err = system (cmdline);
234 /* Remove symlink in case it already exists. Don't care if it fails. */
238 err = symlink(slave_name, GNOKII_DEV);
246 /* Handler called when characters received from serial port.
247 calls state machine code to process it. */
248 static void VM_CharHandler(void)
250 unsigned char buffer[255];
254 /* If we are in command mode, get the character, otherwise leave it */
256 if (CommandMode && ATEM_Initialised) {
258 res = read(PtyRDFD, buffer, 255);
260 /* A returned value of -1 means something serious has gone wrong - so quit!! */
261 /* Note that file closure etc. should have been dealt with in ThreadLoop */
264 // TerminateThread=true;
268 ATEM_HandleIncomingData(buffer, res);
272 /* Initialise GSM interface, returning GSM_Error as appropriate */
273 static GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime)
278 /* Initialise the code for the GSM interface. */
280 error = GSM_Initialise(model,port, initlength, connection, RLP_DisplayF96Frame, synchronizetime);
282 if (error != GE_NONE) {
283 fprintf(stderr, _("GSM/FBUS init failed! (Unknown model ?). Quitting.\n"));
287 /* First (and important!) wait for GSM link to be active. We allow 10
290 while (count++ < 200 && *GSM_LinkOK == false) {
294 if (*GSM_LinkOK == false) {
295 fprintf (stderr, _("Hmmm... GSM_LinkOK never went true. Quitting. \n"));
303 Takes a double-indirect character pointer in which to put a slave
304 name, and returns an integer file descriptor. If it returns < 0, an
305 error has occurred. Otherwise, it has returned the master pty
306 file descriptor, and fills in *name with the name of the
307 corresponding slave pty. Once the slave pty has been opened,
308 you are responsible to free *name. Code is from Developling Linux
309 Applications by Troan and Johnson */
312 static int VM_GetMasterPty(char **name) {
314 #ifdef USE_UNIX98PTYS
317 master = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_NONBLOCK);
319 err = grantpt(master);
320 err = err || unlockpt(master);
322 *name = ptsname(master);
327 #else /* USE_UNIX98PTYS */
329 /* default to returning error */
332 /* create a dummy name to fill in */
333 *name = strdup("/dev/ptyXX");
335 /* search for an unused pty */
336 for (i=0; i<16 && master <= 0; i++) {
337 for (j=0; j<16 && master <= 0; j++) {
338 (*name)[8] = "pqrstuvwxyzPQRST"[i];
339 (*name)[9] = "0123456789abcdef"[j];
340 /* open the master pty */
341 if ((master = open(*name, O_RDWR | O_NOCTTY | O_NONBLOCK )) < 0) {
342 if (errno == ENOENT) {
343 /* we are out of pty devices */
350 if ((master < 0) && (i == 16) && (j == 16)) {
351 /* must have tried every pty unsuccessfully */
356 /* By substituting a letter, we change the master pty
357 * name into the slave pty name.
361 #endif /* USE_UNIX98PTYS */