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 bool VM_ThreadLoop(void);
68 static GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime);
69 static int VM_PtySetup(char *bindir);
71 /* If initialised in debug mode, stdin/out is used instead
72 of ptys for interface. */
73 bool VM_Initialise(char *model,char *port, char *initlength, GSM_ConnectionType connection, char *bindir, bool debug_mode, bool GSMInit,char *synchronizetime)
77 if (debug_mode == true) {
86 fprintf (stderr , "Initialising GSM\n");
88 if ((VM_GSMInitialise(model, port, initlength, connection, synchronizetime) != GE_NONE)) {
89 fprintf (stderr, _("VM_Initialise - VM_GSMInitialise failed!\n"));
96 if (VM_PtySetup(bindir) < 0) {
97 fprintf (stderr, _("VM_Initialise - VM_PtySetup failed!\n"));
101 if (ATEM_Initialise(PtyRDFD, PtyWRFD, model, port) != true) {
102 fprintf (stderr, _("VM_Initialise - ATEM_Initialise failed!\n"));
106 if (DP_Initialise(PtyRDFD, PtyWRFD) != true) {
107 fprintf (stderr, _("VM_Initialise - DP_Initialise failed!\n"));
111 /* Create and start thread, */
112 return VM_ThreadLoop();
115 static void VM_CharHandler(void);
117 static bool VM_ThreadLoop(void)
121 struct timeval timeout;
123 /* Note we can't use signals here as they are already used
124 in the FBUS code. This may warrant changing the FBUS
125 code around sometime to use select instead to free up
126 the SIGIO handler for mainline code. */
131 } else { /* If we are in data mode, leave it to datapump to get the data */
134 FD_SET(PtyRDFD,&readfds);
136 timeout.tv_usec=500*1000;
138 res = select(PtyRDFD+1,&readfds,NULL/*writefds*/,NULL/*exceptfds*/,&timeout);
141 case 0: /* Timeout */
147 perror("VM_ThreadLoop - select");
151 if (FD_ISSET(PtyRDFD,&readfds)) {
153 } else usleep(500); /* Probably the file has been closed */
161 /* Application should call VM_Terminate to shut down
162 the virtual modem thread */
163 void VM_Terminate(void)
171 static int VM_GetMasterPty(char **name);
173 /* Open pseudo tty interface and (in due course create a symlink
174 to be /dev/gnokii etc. ) */
176 static int VM_PtySetup(char *bindir)
180 char mgnokiidev[200];
189 strncpy(mgnokiidev, bindir, 200);
190 strcat(mgnokiidev, "/");
192 strncat(mgnokiidev, "mgnokiidev", 200 - strlen(bindir));
196 PtyRDFD = STDIN_FILENO;
197 PtyWRFD = STDOUT_FILENO;
201 PtyRDFD = VM_GetMasterPty(&slave_name);
203 fprintf (stderr, _("Couldn't open pty!\n"));
209 /* Check we haven't been installed setuid root for some reason
210 if so, don't create /dev/gnokii */
211 if (getuid() != geteuid()) {
212 fprintf(stderr, _("gnokiid should not be installed setuid root!\n"));
218 fprintf (stderr, _("Slave pty is %s, calling %s to create \"%s\".\n"), slave_name,
228 /* Create command line, something line ./mkgnokiidev ttyp0 */
229 sprintf(cmdline, "%s %s", mgnokiidev, slave_name);
231 /* And use system to call it. */
232 err = system (cmdline);
235 /* Remove symlink in case it already exists. Don't care if it fails. */
239 err = symlink(slave_name, GNOKII_DEV);
247 /* Handler called when characters received from serial port.
248 calls state machine code to process it. */
249 static void VM_CharHandler(void)
251 unsigned char buffer[255];
255 /* If we are in command mode, get the character, otherwise leave it */
257 if (CommandMode && ATEM_Initialised) {
259 res = read(PtyRDFD, buffer, 255);
261 /* A returned value of -1 means something serious has gone wrong - so quit!! */
262 /* Note that file closure etc. should have been dealt with in ThreadLoop */
265 // TerminateThread=true;
269 ATEM_HandleIncomingData(buffer, res);
273 /* Initialise GSM interface, returning GSM_Error as appropriate */
274 static GSM_Error VM_GSMInitialise(char *model, char *port, char *initlength, GSM_ConnectionType connection, char *synchronizetime)
279 /* Initialise the code for the GSM interface. */
281 error = GSM_Initialise(model,port, initlength, connection, RLP_DisplayF96Frame, synchronizetime);
283 if (error != GE_NONE) {
284 fprintf(stderr, _("GSM/FBUS init failed! (Unknown model ?). Quitting.\n"));
288 /* First (and important!) wait for GSM link to be active. We allow 10
291 while (count++ < 200 && *GSM_LinkOK == false) {
295 if (*GSM_LinkOK == false) {
296 fprintf (stderr, _("Hmmm... GSM_LinkOK never went true. Quitting. \n"));
304 Takes a double-indirect character pointer in which to put a slave
305 name, and returns an integer file descriptor. If it returns < 0, an
306 error has occurred. Otherwise, it has returned the master pty
307 file descriptor, and fills in *name with the name of the
308 corresponding slave pty. Once the slave pty has been opened,
309 you are responsible to free *name. Code is from Developling Linux
310 Applications by Troan and Johnson */
313 static int VM_GetMasterPty(char **name) {
315 #ifdef USE_UNIX98PTYS
318 master = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_NONBLOCK);
320 err = grantpt(master);
321 err = err || unlockpt(master);
323 *name = ptsname(master);
328 #else /* USE_UNIX98PTYS */
330 /* default to returning error */
333 /* create a dummy name to fill in */
334 *name = strdup("/dev/ptyXX");
336 /* search for an unused pty */
337 for (i=0; i<16 && master <= 0; i++) {
338 for (j=0; j<16 && master <= 0; j++) {
339 (*name)[8] = "pqrstuvwxyzPQRST"[i];
340 (*name)[9] = "0123456789abcdef"[j];
341 /* open the master pty */
342 if ((master = open(*name, O_RDWR | O_NOCTTY | O_NONBLOCK )) < 0) {
343 if (errno == ENOENT) {
344 /* we are out of pty devices */
351 if ((master < 0) && (i == 16) && (j == 16)) {
352 /* must have tried every pty unsuccessfully */
357 /* By substituting a letter, we change the master pty
358 * name into the slave pty name.
362 #endif /* USE_UNIX98PTYS */