This commit was generated by cvs2svn to compensate for changes in r150,
[gnokii.git] / common / devices / tekram.c
1 /*
2  * $Id$
3  *
4  *
5  * G N O K I I
6  *
7  * A Linux/Unix toolset and driver for Nokia mobile phones.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the Free
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27
28 #ifndef WIN32
29   #include <fcntl.h>
30   #include <sys/ioctl.h>
31   #include <termios.h>
32   #include "devices/unixserial.h"
33 #else
34   #include <windows.h>
35   #include "devices/winserial.h"
36 #endif
37
38 #include "devices/tekram.h"
39
40 int tekram_open(__const char *__file) {
41
42   return (serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK));
43 }
44
45 void tekram_close(int __fd) {
46
47   serial_setdtrrts(__fd, 0, 0);
48
49   serial_close(__fd);
50 }
51
52 void tekram_reset(int __fd) {
53
54   serial_setdtrrts(__fd, 0, 0); usleep(50000);
55   serial_setdtrrts(__fd, 1, 0); usleep(1000);
56   serial_setdtrrts(__fd, 1, 1); usleep(50);
57
58   serial_changespeed(__fd, 9600);
59 }
60
61 void tekram_changespeed(int __fd, int __speed) {
62
63   unsigned char speedbyte;
64
65
66   switch (__speed) {
67
68   default:
69   case 9600:   speedbyte = TEKRAM_PW | TEKRAM_B9600;   break;
70   case 19200:  speedbyte = TEKRAM_PW | TEKRAM_B19200;  break;
71   case 38400:  speedbyte = TEKRAM_PW | TEKRAM_B38400;  break;
72   case 57600:  speedbyte = TEKRAM_PW | TEKRAM_B57600;  break;
73   case 115200: speedbyte = TEKRAM_PW | TEKRAM_B115200; break;
74
75   }
76
77
78   tekram_reset(__fd);
79
80   serial_setdtrrts(__fd, 1, 0);
81
82   usleep(7);
83
84   serial_write(__fd, &speedbyte, 1);
85
86   usleep(100000);
87
88   serial_setdtrrts(__fd, 1, 1);
89
90
91   serial_changespeed(__fd, __speed);
92 }
93
94 size_t tekram_read(int __fd, __ptr_t __buf, size_t __nbytes) {
95
96   return (serial_read(__fd, __buf, __nbytes));
97 }
98
99 size_t tekram_write(int __fd, __const __ptr_t __buf, size_t __n) {
100
101   return (serial_write(__fd, __buf, __n));
102 }
103
104 int tekram_select(int fd, struct timeval *timeout) {
105
106   fd_set readfds;
107
108   FD_ZERO(&readfds);
109   FD_SET(fd, &readfds);
110
111   return (select(fd + 1, &readfds, NULL, NULL, timeout));
112
113 }