summaryrefslogtreecommitdiff
path: root/libgammu/protocol/at/at.c
blob: a0713a73451ddd42fb21f07a460abd45afa9f2b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* (c) 2002-2004 by Marcin Wiacek and Michal Cihar */

#include "../../gsmstate.h"

#if defined(GSM_ENABLE_AT) || defined(GSM_ENABLE_BLUEAT) || defined(GSM_ENABLE_IRDAAT) || defined(GSM_ENABLE_DKU2AT)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "../../gsmcomon.h"
#include "at.h"

static GSM_Error AT_WriteMessage (GSM_StateMachine *s, unsigned const char *buffer,
				     int length, int type)
{
	int sent=0, write_data=0, i=0;

	GSM_DumpMessageText(s, buffer, length, type);
	GSM_DumpMessageBinary(s, buffer, length, type);

	if (s->Protocol.Data.AT.FastWrite) {
		while (sent != length) {
			write_data = s->Device.Functions->WriteDevice(s,buffer + sent, length - sent);

			if (write_data == 0 || write_data < 0) {
				return ERR_DEVICEWRITEERROR;
			}
			sent += write_data;
		}
	} else {
		for (i = 0; i < length; i++) {
			/* For some phones like Siemens M20 we need to wait a little
			 * after writing each char. Possible reason: these phones
			 * can't receive so fast chars or there is bug here in Gammu */
			write_data = s->Device.Functions->WriteDevice(s, buffer + i, 1);

			if (write_data != 1) {
				return ERR_DEVICEWRITEERROR;
			}
			usleep(1000);
		}
		usleep(400000);
	}
	return ERR_NONE;
}

typedef struct {
	const char	*text;
	int	lines;
	GSM_Phone_RequestID requestid;
} SpecialAnswersStruct;

GSM_Error AT_StateMachine(GSM_StateMachine *s, unsigned char rx_char)
{
	GSM_Protocol_Message 	Msg2;
	GSM_Protocol_ATData 	*d = &s->Protocol.Data.AT;
	size_t			i;

	/* These are lines with end of "normal" answers */
	static const char 		*StatusStrings[] = {
		/* Standard AT */
		"OK\r",
		"ERROR\r",

		/* AT with bad end of lines */
		"OK\n",
		"ERROR\n",

		/* Standard GSM */
		"+CME ERROR:",
		"+CMS ERROR:",

		/* Motorola A1200 */
		"MODEM ERROR:",

		/* Huawei */
		"COMMAND NOT SUPPORT",

		NULL};

	/* Some info from phone can be inside "normal" answers
	 * It starts with strings written here
	 */
	static const SpecialAnswersStruct SpecialAnswers[] = {
		/* Standard GSM */
		{"+CGREG:"	,1, ID_GetNetworkInfo},
		{"+CBM:"	,1, ID_All},
		{"+CMT:"	,2, ID_All},
		{"+CMTI:"	,1, ID_All},
		{"+CDS:"	,2, ID_All},
		{"+CDSI:"	,1, ID_All},
		{"+CREG:"	,1, ID_GetNetworkInfo},
		{"+CUSD"	,1, ID_All},
		{"+COLP"	,1, ID_All},
		{"+CLIP"	,1, ID_All},
		{"+CRING"	,1, ID_All},
		{"+CCWA"	,1, ID_All},
		{"+CLCC"	,1, ID_All},

		/* Standard AT */
		{"RING"		,1, ID_All},
		{"NO CARRIER"	,1, ID_All},
		{"NO ANSWER"	,1, ID_All},

		/* GlobeTrotter */
		{"_OSIGQ:"	,1, ID_All},
		{"_OBS:"	,1, ID_All},

		{"^SCN:"	,1, ID_All},

		/* Sony-Ericsson */
		{"*EBCA"	,1, ID_All},

		/* Samsung binary transfer end */
		{"SDNDCRC ="	,1, ID_All},
		/* Samsung reply to SSHT in some cases */
		{"SAMSUNG PTS DG Test", 1, ID_All},

		/* Cross PD1101wi reply to almost anything */
		{"NOT FOND ^,NOT CUSTOM AT", 1, ID_All},

		/* Motorola banner */
		{"+MBAN:"	,1, ID_All},

		/* HSPA CORPORATION */
		{"+ZEND"	,1, ID_All},

		/* Huawei */
		{"^RSSI:"	,1, ID_All}, /* ^RSSI:18 */
		{"^HCSQ:"	,1, ID_All}, /* ^HCSQ:"WCDMA",39,29,45 */
		{"^DSFLOWRPT:"	,1, ID_All}, /* ^DSFLOWRPT:00000124,00000082,00000EA6,0000000000012325,000000000022771D,0000BB80,0001F400 */
		{"^BOOT:"	,1, ID_All}, /* ^BOOT:27710117,0,0,0,75 */
		{"^MODE:"	,1, ID_All}, /* ^MODE:3,3 */
		{"^CSNR:"	,1, ID_All}, /* ^CSNR:-93,-23 */
		{"^HCSQ:"	,1, ID_All}, /* ^HCSQ:"LTE",59,50,161,24 */
		{"^SRVST:"	,1, ID_All}, /* ^SRVST:0 */
		{"^SIMST:"	,1, ID_All}, /* ^SIMST:1 */
		{"^STIN:"	,1, ID_All}, /* ^STIN: 7, 0, 0 */

		/* ONDA */
		{"+ZUSIMR:"	,1, ID_All}, /* +ZUSIMR:2 */

		{NULL		,1, ID_All}};

	/* We're starting new message */
	if (d->Msg.Length == 0) {
		/* Ignore leading CR, LF and ESC */
		if (rx_char == 10 || rx_char == 13 || rx_char == 27) {
			return ERR_NONE;
		}
		d->LineStart = 0;
	}

	/* Allocate more memory if needed */
	if (d->Msg.BufferUsed < d->Msg.Length + 2) {
		d->Msg.BufferUsed	= d->Msg.Length + 200;
		d->Msg.Buffer 		= (unsigned char *)realloc(d->Msg.Buffer,d->Msg.BufferUsed);
		if (d->Msg.Buffer == NULL) {
			return ERR_MOREMEMORY;
		}
	}

	/* Store current char in the buffer */
	d->Msg.Buffer[d->Msg.Length++] = rx_char;
	d->Msg.Buffer[d->Msg.Length  ] = 0;

	/* Parse char */
	switch (rx_char) {
	case 0:
		break;
	case 10:
	case 13:
		/* Store line end (if we did not do it in last char */
		if (! d->wascrlf) {
			d->LineEnd = d->Msg.Length - 1;
		}
		d->wascrlf = TRUE;

		/* Process line after \r\n */
		if (d->Msg.Length > 0 && rx_char == 10 && d->Msg.Buffer[d->Msg.Length - 2] == 13) {
			/* Process standard responses */
			for (i = 0; StatusStrings[i] != NULL; i++) {
				if (strncmp(StatusStrings[i],
							d->Msg.Buffer + d->LineStart,
							strlen(StatusStrings[i])) == 0) {
					s->Phone.Data.RequestMsg	= &d->Msg;
					s->Phone.Data.DispatchError	= s->Phone.Functions->DispatchMessage(s);
					d->Msg.Length			= 0;
					break;
				}
			}
			/* Generally hack for A2D */
			if (d->CPINNoOK) {
				if (strncmp("+CPIN: ",
						d->Msg.Buffer + d->LineStart,
						7) == 0) {
					s->Phone.Data.RequestMsg	= &d->Msg;
					s->Phone.Data.DispatchError	= s->Phone.Functions->DispatchMessage(s);
					d->Msg.Length			= 0;
					break;
				}
			}

			/* Check for incoming frames */
			for (i = 0; SpecialAnswers[i].text != NULL; i++) {
				if (strncmp(SpecialAnswers[i].text,
							d->Msg.Buffer + d->LineStart,
							strlen(SpecialAnswers[i].text)) == 0) {
					/* We need something better here */
					if (s->Phone.Data.RequestID == SpecialAnswers[i].requestid) {
						i++;
						continue;
					}
					if ((s->Phone.Data.RequestID == ID_SetOBEX || s->Phone.Data.RequestID == ID_DialVoice)&&
							strcmp(SpecialAnswers[i].text, "NO CARRIER") == 0) {
						i++;
						continue;
					}
					d->SpecialAnswerStart 	= d->LineStart;
					d->SpecialAnswerLines	= SpecialAnswers[i].lines;
				}
			}

			/* Last line of incoming frame */
			if (d->SpecialAnswerLines == 1) {
				/* This is end of special answer. We copy it and send to phone module */
				Msg2.Buffer = (unsigned char *)malloc(d->LineEnd - d->SpecialAnswerStart + 3);
				memcpy(Msg2. Buffer, d->Msg.Buffer + d->SpecialAnswerStart, d->LineEnd - d->SpecialAnswerStart + 2);
				Msg2.Length = d->LineEnd - d->SpecialAnswerStart + 2;
				Msg2.Buffer[Msg2.Length] = '\0';
				Msg2.Type = 0;

				s->Phone.Data.RequestMsg	= &Msg2;
				s->Phone.Data.DispatchError	= s->Phone.Functions->DispatchMessage(s);
				free(Msg2.Buffer);
				Msg2.Buffer = NULL;

				/* We cut special answer from main buffer */
				d->Msg.Length			= d->SpecialAnswerStart;
				if (d->Msg.Length != 0) {
					d->Msg.Length = d->Msg.Length - 2;
				}

				/* We need to find earlier values of all variables */
				d->wascrlf 			= FALSE;
				d->LineStart			= 0;
				for (i = 0;i < d->Msg.Length; i++) {
					switch (d->Msg.Buffer[i]) {
					case 0:
						break;
					case 10:
					case 13:
						if (!d->wascrlf) {
							d->LineEnd = d->Msg.Length - 1;
						}
						d->wascrlf = TRUE;
						break;
					default:
						if (d->wascrlf) {
							d->LineStart	= d->Msg.Length - 1;
							d->wascrlf 	= FALSE;
						}
					}
				}
				d->Msg.Buffer[d->Msg.Length] = 0;
			}
			if (d->SpecialAnswerLines > 0) {
				d->SpecialAnswerLines--;
			}
		}
		break;
	case 'T':
		/* When CONNECT string received, we know there will not follow
		 * anything AT related, after CONNECT can follow ppp data, alcabus
		 * data and also other things.
		 */
		if (strncmp(d->Msg.Buffer + d->LineStart, "CONNECT", 7) == 0) {
			s->Phone.Data.RequestMsg   	= &d->Msg;
			s->Phone.Data.DispatchError	= s->Phone.Functions->DispatchMessage(s);
			d->LineStart              	= -1;
			d->Msg.Length			= 0;
			break;
		}
	default:
		if (d->wascrlf) {
			d->LineStart	= d->Msg.Length - 1;
			d->wascrlf 	= FALSE;
		}
		if (d->EditMode) {
			if (strlen(d->Msg.Buffer+d->LineStart) == 2 &&
					strncmp(d->Msg. Buffer + d->LineStart, "> ", 2) == 0) {
				s->Phone.Data.RequestMsg	= &d->Msg;
				s->Phone.Data.DispatchError	= s->Phone.Functions->DispatchMessage(s);
			}
		}
	}
	return ERR_NONE;
}

GSM_Error AT_Initialise(GSM_StateMachine *s)
{
	GSM_Protocol_ATData *d = &s->Protocol.Data.AT;
	GSM_Error		error;

	d->Msg.Buffer 		= NULL;
	d->Msg.BufferUsed	= 0;
	d->Msg.Length		= 0;
	d->Msg.Type		= 0;

	d->SpecialAnswerLines	= 0;
	d->LineStart		= -1;
	d->LineEnd		= -1;
	d->wascrlf 		= FALSE;
	d->EditMode		= FALSE;
	/* Slow write makes sense only on cable for some phones */
	d->FastWrite		= (s->ConnectionType != GCT_AT);
	d->CPINNoOK		= FALSE;

	error = s->Device.Functions->DeviceSetParity(s, FALSE);
	if (error != ERR_NONE) return error;

	error = s->Device.Functions->DeviceSetDtrRts(s, TRUE, TRUE);
	if (error != ERR_NONE) return error;

	return s->Device.Functions->DeviceSetSpeed(s, s->Speed);
}

static GSM_Error AT_Terminate(GSM_StateMachine *s)
{
	free(s->Protocol.Data.AT.Msg.Buffer);
	s->Protocol.Data.AT.Msg.Buffer = NULL;
	return ERR_NONE;
}

GSM_Protocol_Functions ATProtocol = {
	AT_WriteMessage,
	AT_StateMachine,
	AT_Initialise,
	AT_Terminate
};

#endif

/* How should editor hadle tabs in this file? Add editor commands here.
 * vim: noexpandtab sw=8 ts=8 sts=8:
 */