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 char SigHandler_buffer[255];
162 static void SigHandler(int status)
169 res = device_read(SigHandler_buffer, sizeof(SigHandler_buffer));
171 for (count = 0; count < res ; count ++) {
172 Protocol->StateMachine(SigHandler_buffer[count]);
178 #if defined(__svr4__) || defined(__FreeBSD__)
179 /* thread for handling incoming data */
183 struct timeval timeout;
186 FD_SET(device_portfd,&readfds);
187 /* set timeout to 15 seconds */
190 while (!CurrentRequestTerminate) {
191 err=select(device_portfd+1,&readfds,NULL,NULL,&timeout);
193 /* call singal handler to process incoming data */
195 /* refresh timeout, just for linux :-( */
196 /* required for irda */
200 perror("Error in SelectLoop");
206 bool StartConnection (char *port_device, bool with_odd_parity, GSM_ConnectionType con)
212 #if defined(__svr4__) || defined(__FreeBSD__)
215 struct sigaction sig_io;
220 if ((strstr(GSM_Info->IrdaModels,"decode")!=NULL) && (CurrentConnectionType == GCT_Irda))
222 printf("DEBUG and Irda decode Model -> not realy open ;-)\n");
228 if (PortDevice[0]!=0x00) return true;
232 strncpy(PortDevice, port_device, GSM_MAX_DEVICE_NAME_LENGTH);
235 fprintf(stdout,_("Opening \"%s\" device...\n"),PortDevice);
238 strcpy(text,"\n\n\nMygnokii ");
239 sprintf(text+strlen(text), "%s",VERSION);
240 strcpy(text+strlen(text),"\n");
241 AppendLogText(text,false);
243 strcpy(text,"Port ");
244 strcpy(text+strlen(text),PortDevice);
245 strcpy(text+strlen(text),"\n");
246 AppendLogText(text,false);
248 strcpy(text,"Connection ");
250 case GCT_FBUS :strcpy(text+strlen(text),"FBUS");break;
252 case GCT_MBUS :strcpy(text+strlen(text),"MBUS");break;
253 case GCT_DLR3 :strcpy(text+strlen(text),"DLR3");break;
254 case GCT_AT :strcpy(text+strlen(text),"AT");break;
255 default :strcpy(text+strlen(text),"unknown");break;
258 strcpy(text+strlen(text),"\n");
259 AppendLogText(text,false);
261 /* Ralf Thelen: In this moment there is NO method of communication,
262 which require keepalive packets and GSM->KeepAlive is
263 always NULL_KeepAlive, I comment this thread, */
265 /* Create and start main thread. */
266 // rtn = pthread_create(&Thread, NULL,(void*)GSM->KeepAlive, (void *)NULL);
269 // fprintf(stdout,_("Error\n"));
273 #if defined(__svr4__) || defined(__FreeBSD__)
275 /* Set up and install handler before enabling async IO on port. */
276 sig_io.sa_handler = SigHandler;
278 sigaction (SIGIO, &sig_io, NULL);
282 result = device_open(PortDevice, with_odd_parity);
285 fprintf(stdout,_("Error\n"));
289 #if defined(__svr4__) || defined(__FreeBSD__)
290 /* create a thread to handle incoming data from mobile phone */
291 rtn=pthread_create(&selThread,NULL,(void*)SelectLoop,(void*)NULL);
293 fprintf(stdout,_("Error\n"));
301 /* ---------------- RTH: #ifdef WIN32 ------------------ */
305 extern HANDLE hPhone;
307 void device_close(void)
309 AppendLogText("CLOSE\n",false);
317 void device_dumpserial(void)
321 dcb.DCBlength = sizeof(DCB);
322 GetCommState(hPhone, &dcb);
324 fprintf(stdout, _("Serial device:"));
325 fprintf(stdout, _(" DTR is "));
326 switch (dcb.fDtrControl) {
327 case DTR_CONTROL_ENABLE : fprintf(stdout, _("up")); break;
328 case DTR_CONTROL_DISABLE : fprintf(stdout, _("down")); break;
329 case DTR_CONTROL_HANDSHAKE: fprintf(stdout, _("handshake"));break;
331 fprintf(stdout, _(", RTS is "));
332 switch (dcb.fRtsControl) {
333 case RTS_CONTROL_ENABLE : fprintf(stdout, _("up\n")); break;
334 case RTS_CONTROL_DISABLE : fprintf(stdout, _("down\n")); break;
335 case RTS_CONTROL_HANDSHAKE: fprintf(stdout, _("handshake\n"));break;
336 case RTS_CONTROL_TOGGLE : fprintf(stdout, _("toggle\n")); break;
341 void device_setdtrrts(int __dtr, int __rts)
345 dcb.DCBlength = sizeof(DCB);
346 GetCommState(hPhone, &dcb);
348 if (__dtr==1) dcb.fDtrControl = DTR_CONTROL_ENABLE;
349 else dcb.fDtrControl = DTR_CONTROL_DISABLE;
351 if (__rts==1) dcb.fRtsControl = RTS_CONTROL_ENABLE;
352 else dcb.fRtsControl = RTS_CONTROL_DISABLE;
354 SetCommState(hPhone, &dcb);
361 void device_changespeed(int __speed)
366 dcb.DCBlength = sizeof(DCB);
367 GetCommState(hPhone, &dcb);
370 case 115200: dcb.BaudRate=CBR_115200; break;
371 case 19200 : dcb.BaudRate=CBR_19200; break;
372 case 9600 : dcb.BaudRate=CBR_9600; break;
375 SetCommState(hPhone, &dcb);
378 fprintf(stdout,_("Serial device: changing speed to %i\n"),__speed);
382 bool StartConnection (char *port_device, bool with_odd_parity, GSM_ConnectionType con)
389 if (PortDevice[0]!=0x00) return true;
392 strncpy(PortDevice, port_device, GSM_MAX_DEVICE_NAME_LENGTH);
395 fprintf(stdout,_("Opening \"%s\" device...\n"),PortDevice);
398 strcpy(text,"\n\n\nMygnokii ");
399 sprintf(text+strlen(text), "%s",VERSION);
400 strcpy(text+strlen(text),"\n");
401 AppendLogText(text,false);
403 strcpy(text,"Port ");
404 strcpy(text+strlen(text),PortDevice);
405 strcpy(text+strlen(text),"\n");
406 AppendLogText(text,false);
408 strcpy(text,"Connection ");
410 case GCT_FBUS :strcpy(text+strlen(text),"FBUS");break;
411 case GCT_MBUS :strcpy(text+strlen(text),"MBUS");break;
412 case GCT_DLR3 :strcpy(text+strlen(text),"DLR3");break;
413 case GCT_AT :strcpy(text+strlen(text),"AT");break;
414 default :strcpy(text+strlen(text),"unknown");break;
416 strcpy(text+strlen(text),"\n");
417 AppendLogText(text,false);
419 CurrentDisableKeepAlive = true;
421 /* Create and start main thread. */
422 rtn = ! OpenConnection(PortDevice, Protocol->StateMachine, GSM->KeepAlive);
426 fprintf(stdout,_("Error\n"));
429 CurrentDisableKeepAlive = false;
432 if (with_odd_parity) {
433 dcb.DCBlength = sizeof(DCB);
434 GetCommState(hPhone, &dcb);
436 dcb.Parity=ODDPARITY;
438 SetCommState(hPhone, &dcb);
444 size_t device_write(const __ptr_t __buf, size_t __n) {
446 while (duringwrite) {}
448 AppendLog(__buf,__n,true);
449 i=WriteCommBlock(__buf,__n);
451 if (i) return __n; else return 0;