5 A Linux/Unix toolset and driver for Nokia mobile phones.
7 Released under the terms of the GNU GPL, see file COPYING for more details.
14 #include "devices/unixserial.h"
15 #include <sys/ioctl.h>
21 #include "devices/winserial.h"
25 #include "devices/device.h"
30 /* for VC6 make scripts save VERSION constant in mversion.h file */
34 static char PortDevice[GSM_MAX_DEVICE_NAME_LENGTH]={0x00};
36 static bool duringwrite;
41 #if defined(__svr4__) || defined(__FreeBSD__)
45 static int device_portfd = -1;
48 * Structure to store the filedescriptor we use.
52 static int device_getfd(void)
58 static int device_open(__const char *__file, int __with_odd_parity) {
60 switch (CurrentConnectionType) {
62 device_portfd = serial_opendevice(__file, __with_odd_parity, true, false);
66 return (device_portfd >= 0);
69 void device_close(void)
71 AppendLogText("CLOSE\n",false);
73 /* Now wait for thread to terminate. */
74 //pthread_join(Thread, NULL);
76 switch (CurrentConnectionType) {
77 default : serial_close(device_portfd); break;
85 void device_reset(void) {
91 static void device_dumpserial(void);
94 void device_setdtrrts(int __dtr, int __rts)
96 switch (CurrentConnectionType) {
98 serial_setdtrrts(device_portfd, __dtr, __rts);
106 void device_changespeed(int __speed)
108 switch (CurrentConnectionType) {
110 serial_changespeed(device_portfd, __speed);
112 fprintf(stdout,_("Serial device: changing speed to %i\n"),__speed);
118 static size_t device_read(__ptr_t __buf, size_t __nbytes)
120 switch (CurrentConnectionType) {
121 default : return (serial_read(device_portfd, __buf, __nbytes)); break;
125 size_t device_write(__const __ptr_t __buf, size_t __n) {
129 while (duringwrite) {}
131 memcpy(buffer,__buf,__n);
132 AppendLog(buffer,__n,true);
135 switch (CurrentConnectionType) {
136 default : mysize = serial_write(device_portfd, __buf, __n); break;
143 static void device_dumpserial(void)
146 unsigned int Flags=0;
148 PortFD = device_getfd();
150 ioctl(PortFD, TIOCMGET, &Flags);
152 fprintf(stdout, _("Serial device:"));
153 fprintf(stdout, _(" DTR is %s"), Flags&TIOCM_DTR?_("up"):_("down"));
154 fprintf(stdout, _(", RTS is %s"), Flags&TIOCM_RTS?_("up"):_("down"));
155 fprintf(stdout, _(", CAR is %s"), Flags&TIOCM_CAR?_("up"):_("down"));
156 fprintf(stdout, _(", CTS is %s\n"), Flags&TIOCM_CTS?_("up"):_("down"));
160 static void SigHandler(int status)
163 unsigned char buffer[2048];
167 res = device_read(buffer, 255);
169 for (count = 0; count < res ; count ++) {
170 Protocol->StateMachine(buffer[count]);
175 #if defined(__svr4__) || defined(__FreeBSD__)
176 /* thread for handling incoming data */
180 struct timeval timeout;
183 FD_SET(device_portfd,&readfds);
184 /* set timeout to 15 seconds */
187 while (!CurrentRequestTerminate) {
188 err=select(device_portfd+1,&readfds,NULL,NULL,&timeout);
190 /* call singal handler to process incoming data */
192 /* refresh timeout, just for linux :-( */
\r
193 /* required for irda */
197 perror("Error in SelectLoop");
203 bool StartConnection (char *port_device, bool with_odd_parity, GSM_ConnectionType con)
209 #if defined(__svr4__) || defined(__FreeBSD__)
212 struct sigaction sig_io;
217 if ((strstr(GSM_Info->IrdaModels,"decode")!=NULL) && (CurrentConnectionType == GCT_Irda))
219 printf("DEBUG and Irda decode Model -> not realy open ;-)\n");
225 if (PortDevice[0]!=0x00) return true;
229 strncpy(PortDevice, port_device, GSM_MAX_DEVICE_NAME_LENGTH);
232 fprintf(stdout,_("Opening \"%s\" device...\n"),PortDevice);
235 strcpy(text,"\n\n\nMygnokii ");
236 sprintf(text+strlen(text), "%s",VERSION);
237 strcpy(text+strlen(text),"\n");
238 AppendLogText(text,false);
240 strcpy(text,"Port ");
241 strcpy(text+strlen(text),PortDevice);
242 strcpy(text+strlen(text),"\n");
243 AppendLogText(text,false);
245 strcpy(text,"Connection ");
247 case GCT_FBUS :strcpy(text+strlen(text),"FBUS");break;
249 case GCT_MBUS :strcpy(text+strlen(text),"MBUS");break;
250 case GCT_DLR3 :strcpy(text+strlen(text),"DLR3");break;
251 case GCT_AT :strcpy(text+strlen(text),"AT");break;
252 default :strcpy(text+strlen(text),"unknown");break;
255 strcpy(text+strlen(text),"\n");
256 AppendLogText(text,false);
258 /* Ralf Thelen: In this moment there is NO method of communication,
259 which require keepalive packets and GSM->KeepAlive is
260 always NULL_KeepAlive, I comment this thread, */
262 /* Create and start main thread. */
263 // rtn = pthread_create(&Thread, NULL,(void*)GSM->KeepAlive, (void *)NULL);
266 // fprintf(stdout,_("Error\n"));
270 #if defined(__svr4__) || defined(__FreeBSD__)
272 /* Set up and install handler before enabling async IO on port. */
273 sig_io.sa_handler = SigHandler;
275 sigaction (SIGIO, &sig_io, NULL);
279 result = device_open(PortDevice, with_odd_parity);
282 fprintf(stdout,_("Error\n"));
286 #if defined(__svr4__) || defined(__FreeBSD__)
287 /* create a thread to handle incoming data from mobile phone */
288 rtn=pthread_create(&selThread,NULL,(void*)SelectLoop,(void*)NULL);
290 fprintf(stdout,_("Error\n"));
298 /* ---------------- RTH: #ifdef WIN32 ------------------ */
302 extern HANDLE hPhone;
304 void device_close(void)
306 AppendLogText("CLOSE\n",false);
314 void device_dumpserial(void)
318 dcb.DCBlength = sizeof(DCB);
319 GetCommState(hPhone, &dcb);
321 fprintf(stdout, _("Serial device:"));
322 fprintf(stdout, _(" DTR is "));
323 switch (dcb.fDtrControl) {
324 case DTR_CONTROL_ENABLE : fprintf(stdout, _("up")); break;
325 case DTR_CONTROL_DISABLE : fprintf(stdout, _("down")); break;
326 case DTR_CONTROL_HANDSHAKE: fprintf(stdout, _("handshake"));break;
328 fprintf(stdout, _(", RTS is "));
329 switch (dcb.fRtsControl) {
330 case RTS_CONTROL_ENABLE : fprintf(stdout, _("up\n")); break;
331 case RTS_CONTROL_DISABLE : fprintf(stdout, _("down\n")); break;
332 case RTS_CONTROL_HANDSHAKE: fprintf(stdout, _("handshake\n"));break;
333 case RTS_CONTROL_TOGGLE : fprintf(stdout, _("toggle\n")); break;
338 void device_setdtrrts(int __dtr, int __rts)
342 dcb.DCBlength = sizeof(DCB);
343 GetCommState(hPhone, &dcb);
345 if (__dtr==1) dcb.fDtrControl = DTR_CONTROL_ENABLE;
346 else dcb.fDtrControl = DTR_CONTROL_DISABLE;
348 if (__rts==1) dcb.fRtsControl = RTS_CONTROL_ENABLE;
349 else dcb.fRtsControl = RTS_CONTROL_DISABLE;
351 SetCommState(hPhone, &dcb);
358 void device_changespeed(int __speed)
363 dcb.DCBlength = sizeof(DCB);
364 GetCommState(hPhone, &dcb);
367 case 115200: dcb.BaudRate=CBR_115200; break;
368 case 19200 : dcb.BaudRate=CBR_19200; break;
369 case 9600 : dcb.BaudRate=CBR_9600; break;
372 SetCommState(hPhone, &dcb);
375 fprintf(stdout,_("Serial device: changing speed to %i\n"),__speed);
379 bool StartConnection (char *port_device, bool with_odd_parity, GSM_ConnectionType con)
386 if (PortDevice[0]!=0x00) return true;
389 strncpy(PortDevice, port_device, GSM_MAX_DEVICE_NAME_LENGTH);
392 fprintf(stdout,_("Opening \"%s\" device...\n"),PortDevice);
395 strcpy(text,"\n\n\nMygnokii ");
396 sprintf(text+strlen(text), "%s",VERSION);
397 strcpy(text+strlen(text),"\n");
398 AppendLogText(text,false);
400 strcpy(text,"Port ");
401 strcpy(text+strlen(text),PortDevice);
402 strcpy(text+strlen(text),"\n");
403 AppendLogText(text,false);
405 strcpy(text,"Connection ");
407 case GCT_FBUS :strcpy(text+strlen(text),"FBUS");break;
408 case GCT_MBUS :strcpy(text+strlen(text),"MBUS");break;
409 case GCT_DLR3 :strcpy(text+strlen(text),"DLR3");break;
410 case GCT_AT :strcpy(text+strlen(text),"AT");break;
411 default :strcpy(text+strlen(text),"unknown");break;
413 strcpy(text+strlen(text),"\n");
414 AppendLogText(text,false);
416 CurrentDisableKeepAlive = true;
418 /* Create and start main thread. */
419 rtn = ! OpenConnection(PortDevice, Protocol->StateMachine, GSM->KeepAlive);
423 fprintf(stdout,_("Error\n"));
426 CurrentDisableKeepAlive = false;
429 if (with_odd_parity) {
430 dcb.DCBlength = sizeof(DCB);
431 GetCommState(hPhone, &dcb);
433 dcb.Parity=ODDPARITY;
435 SetCommState(hPhone, &dcb);
441 size_t device_write(const __ptr_t __buf, size_t __n) {
443 while (duringwrite) {}
445 AppendLog(__buf,__n,true);
446 i=WriteCommBlock(__buf,__n);
448 if (i) return __n; else return 0;