This commit was generated by cvs2svn to compensate for changes in r158,
[gnokii.git] / common / links / atbus.c
1 /*
2
3   $Id$
4
5   G N O K I I
6
7   A Linux/Unix toolset and driver for mobile phones.
8
9   Copyright 2001 Manfred Jonsson <manfred.jonsson@gmx.de>
10
11   Released under the terms of the GNU GPL, see file COPYING for more details.
12
13   $Log$
14   Revision 1.1.1.2  2002/04/03 00:08:07  short
15   Found in "gnokii-working" directory, some November-patches version
16
17   Revision 1.4  2001/09/09 21:45:49  machek
18   Cleanups from Ladislav Michl <ladis@psi.cz>:
19
20   *) do *not* internationalize debug messages
21
22   *) some whitespace fixes, do not use //
23
24   *) break is unneccessary after return
25
26   Revision 1.3  2001/08/20 23:27:37  pkot
27   Add hardware shakehand to the link layer (Manfred Jonsson)
28
29   Revision 1.2  2001/08/09 11:51:39  pkot
30   Generic AT support updates and cleanup (Manfred Jonsson)
31
32   Revision 1.1  2001/07/27 00:02:21  pkot
33   Generic AT support for the new structure (Manfred Jonsson)
34
35 */
36
37 /* System header files */
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <errno.h>
42
43 /* Various header file */
44 #include "config.h"
45 #ifndef DEBUG
46 #define DEBUG
47 #endif
48 #include "misc.h"
49 #include "gsm-common.h"
50 #include "gsm-ringtones.h"
51 #include "gsm-networks.h"
52 #include "device.h"
53 #include "links/utils.h"
54 #include "gsm-statemachine.h"
55
56 #define __atbus_c
57 #include "links/atbus.h"
58
59 /* FIXME - pass device_* the link stuff?? */
60 /* FIXME - win32 stuff! */
61
62
63 /* Some globals */
64 static GSM_Link *glink;
65 static GSM_Statemachine *statemachine;
66 static int binlength = 0;
67 static char reply_buf[1024];
68 static int reply_buf_pos = 0;
69
70
71 static int xwrite(unsigned char *d, int len)
72 {
73         int res;
74         while (len) {
75                 res = device_write(d, len);
76                 if (res == -1) {
77                         if (errno != EAGAIN) {
78                                 perror("gnokii I/O error ");
79                                 return -1;
80                         }
81                 } else {
82                         d += res;
83                         len -= res;
84                 }
85         }
86         return 0;
87 }
88
89
90 static GSM_Error
91 AT_SendMessage(u16 message_length, u8 message_type, void *msg)
92 {
93         usleep(10000);
94         xwrite((char*)msg, message_length);
95         return GE_NONE;
96 }
97
98
99 /* RX_State machine for receive handling.  Called once for each character
100    received from the phone. */
101 void ATBUS_RX_StateMachine(unsigned char rx_char)
102 {
103         reply_buf[reply_buf_pos++] = rx_char;
104         reply_buf[reply_buf_pos] = '\0';
105
106         if (reply_buf_pos >= binlength) {
107                 if (((reply_buf_pos > 3) && (!strncmp(reply_buf+reply_buf_pos-4, "OK\r\n", 4)))
108                 || ((reply_buf_pos > 6) && (!strncmp(reply_buf+reply_buf_pos-7, "ERROR\r\n", 7)))) {
109                         SM_IncomingFunction(statemachine, 1, reply_buf, reply_buf_pos);
110                         reply_buf_pos = 0;
111                         binlength = 0;
112                         return;
113                 }
114 /* needed for binary date etc
115    TODO: correct reading of variable length integers
116                 if (reply_buf_pos == 12) {
117                         if (!strncmp(reply_buf + 3, "ABC", 3) {
118                                 binlength = atoi(reply_buf + 8);
119                         }
120                 }
121 */
122         }
123 }
124
125
126 bool ATBUS_OpenSerial(int hw_handshake)
127 {
128         int result;
129         result = device_open(glink->PortDevice, false, false, hw_handshake, GCT_Serial);
130         if (!result) {
131                 perror(_("Couldn't open ATBUS device"));
132                 return (false);
133         }
134         device_changespeed(19200);
135         if (hw_handshake) {
136                 device_setdtrrts(0, 1);
137                 sleep(1);
138                 device_setdtrrts(1, 1);
139                 /* make 7110 happy */
140                 sleep(1);
141         } else {
142                 device_setdtrrts(0, 0);
143         }
144         return (true);
145 }
146
147 GSM_Error ATBUS_Loop(struct timeval *timeout)
148 {
149         unsigned char buffer[255];
150         int count, res;
151                                 
152         res = device_select(timeout);
153         if (res > 0) {
154                 res = device_read(buffer, 255);
155                 for (count = 0; count < res; count++)
156                         ATBUS_RX_StateMachine(buffer[count]);
157         } else
158                 return GE_TIMEOUT;  
159         /* This traps errors from device_read */
160         if (res > 0)
161                 return GE_NONE;
162         else
163                 return GE_INTERNALERROR;
164 }
165
166
167 /* Initialise variables and start the link */
168 GSM_Error ATBUS_Initialise(GSM_Statemachine *state, int hw_handshake)
169 {
170         setvbuf(stdout, NULL, _IONBF, 0);
171         setvbuf(stderr, NULL, _IONBF, 0);
172
173         /* 'Copy in' the global structures */
174         glink = &(state->Link);
175         statemachine = state;
176
177         /* Fill in the link functions */
178         glink->Loop = &ATBUS_Loop;
179         glink->SendMessage = &AT_SendMessage;
180
181         if (glink->ConnectionType == GCT_Serial) {
182                 if (!ATBUS_OpenSerial(hw_handshake))
183                         return GE_DEVICEOPENFAILED;
184         } else {
185                 fprintf(stderr, "Device not supported by ATBUS");
186                 return GE_DEVICEOPENFAILED;
187         }
188
189         return GE_NONE;
190 }