Found in "gnokii-working" directory, some November-patches version
[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  * Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
10  * Copyright (C) 2000-2001  Marcel Holtmann <marcel@holtmann.org>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with this library; if not, write to the Free
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  *
26  * $Log$
27  * Revision 1.1.1.3  2002/04/03 00:08:06  short
28  * Found in "gnokii-working" directory, some November-patches version
29  *
30  * Revision 1.2  2001/02/21 19:57:03  chris
31  * More fiddling with the directory layout
32  *
33  * Revision 1.1  2001/02/16 14:29:51  chris
34  * Restructure of common/.  Fixed a problem in fbus-phonet.c
35  * Lots of dprintfs for Marcin
36  * Any size xpm can now be loaded (eg for 7110 startup logos)
37  * nk7110 code detects 7110/6210 and alters startup logo size to suit
38  * Moved Marcin's extended phonebook code into gnokii.c
39  *
40  * Revision 1.2  2001/02/12 15:13:46  chris
41  * Fixed my bug in xgnokii_contacts.c and added <string.h> to tekram.c
42  *
43  * Revision 1.1  2001/02/09 18:12:53  chris
44  * Marcel's tekram support
45  *
46  */
47
48 #include <stdio.h>
49 #include <fcntl.h>
50 #include <sys/ioctl.h>
51 #include <termios.h>
52 #include <string.h>
53
54 #ifndef WIN32
55 #include "devices/unixserial.h"
56 #else
57 #include "winserial.h"
58 #endif
59
60 #include "devices/tekram.h"
61
62
63
64
65 int tekram_open(__const char *__file) {
66
67   return (serial_open(__file, O_RDWR | O_NOCTTY | O_NONBLOCK));
68 }
69
70 void tekram_close(int __fd) {
71
72   serial_setdtrrts(__fd, 0, 0);
73
74   serial_close(__fd);
75 }
76
77 void tekram_reset(int __fd) {
78
79   serial_setdtrrts(__fd, 0, 0);
80
81   usleep(50000);
82
83   serial_setdtrrts(__fd, 1, 0);
84
85   usleep(1000);
86
87   serial_setdtrrts(__fd, 1, 1);
88
89   usleep(50);
90
91
92   serial_changespeed(__fd, 9600);
93 }
94
95 void tekram_changespeed(int __fd, int __speed) {
96
97   unsigned char speedbyte;
98
99
100   switch (__speed) {
101
102   default:
103   case 9600:   speedbyte = TEKRAM_PW | TEKRAM_B9600;   break;
104   case 19200:  speedbyte = TEKRAM_PW | TEKRAM_B19200;  break;
105   case 38400:  speedbyte = TEKRAM_PW | TEKRAM_B38400;  break;
106   case 57600:  speedbyte = TEKRAM_PW | TEKRAM_B57600;  break;
107   case 115200: speedbyte = TEKRAM_PW | TEKRAM_B115200; break;
108
109   }
110
111
112   tekram_reset(__fd);
113
114   serial_setdtrrts(__fd, 1, 0);
115
116   usleep(7);
117
118   serial_write(__fd, &speedbyte, 1);
119
120   usleep(100000);
121
122   serial_setdtrrts(__fd, 1, 1);
123
124
125   serial_changespeed(__fd, __speed);
126 }
127
128 size_t tekram_read(int __fd, __ptr_t __buf, size_t __nbytes) {
129
130   return (serial_read(__fd, __buf, __nbytes));
131 }
132
133 size_t tekram_write(int __fd, __const __ptr_t __buf, size_t __n) {
134
135   return (serial_write(__fd, __buf, __n));
136 }
137
138 int tekram_select(int fd, struct timeval *timeout) {
139
140   fd_set readfds;
141
142   FD_ZERO(&readfds);
143   FD_SET(fd, &readfds);
144
145   return (select(fd + 1, &readfds, NULL, NULL, timeout));
146
147 }