summaryrefslogtreecommitdiff
path: root/libgammu/phone/at/atgen.h
blob: 9932e6cd3b06c5cec2825c1540b56f78c48b9740 (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* (c) 2002-2007 by Marcin Wiacek and Michal Cihar */

/**
 * @file atgen.h
 * @author Michal Čihař
 * @author Marcin Wiacek
 */
/**
 * @addtogroup Phone
 * @{
 */
/**
 * @defgroup ATPhone AT phones communication
 *
 * This module implements standard AT commands.
 *
 * @see http://www.etsi.org
 * @see http://www.3gpp.org
 *
 * @{
 */

#ifndef atgen_h
#define atgen_h

#include <gammu-types.h>
#include <gammu-error.h>
#include <gammu-statemachine.h>

#include "../../gsmreply.h" /* For GSM_Phone_RequestID */
#include "../../misc/misc.h" /* For GSM_CutLines */

#include "motorola.h"

#ifndef GSM_USED_AT
#  define GSM_USED_AT
#endif
#ifndef GSM_USED_BLUEAT
#  define GSM_USED_BLUEAT
#endif
#ifndef GSM_USED_IRDAAT
#  define GSM_USED_IRDAAT
#endif

#define MAX_VCALENDAR_LOCATION 50

typedef enum {
	SMS_AT_PDU = 1,
	SMS_AT_TXT
} GSM_AT_SMS_Modes;

typedef enum {
	AT_Reply_OK = 1,
	AT_Reply_Connect,
	AT_Reply_Error,
	AT_Reply_Unknown,
	AT_Reply_CMSError,
	AT_Reply_CMEError,
	AT_Reply_SMSEdit
} GSM_AT_Reply_State;

/**
 * Manufacturer identification. This is usually used to enable some
 * hacks which are specific for all phones from one manufacturer.
 */
typedef enum {
	AT_Nokia = 1,
	AT_Alcatel,
	AT_Siemens,
	AT_HP,
	AT_Falcom,
	AT_Ericsson,
	AT_Sagem,
	AT_Samsung,
	AT_Mitsubishi,
	AT_Sharp,
	AT_Option,
	AT_Motorola,
	AT_Wavecom,
	AT_Philips,
	AT_Huawei,
	AT_Qualcomm,
	AT_Telit,
	AT_ZTE,
	AT_Unknown
} GSM_AT_Manufacturer;

/**
 * Character set definitions.
 */
typedef enum {
	/**
	 * Hex encoded GSM.
	 */
	AT_CHARSET_HEX = 1,
	/**
	 * GSM charset.
	 */
	AT_CHARSET_GSM,
	/**
	 * Hex encoded UCS2.
	 */
	AT_CHARSET_UCS2,
	/**
	 * Hex encoded UCS2.
	 */
	AT_CHARSET_UCS_2,
	/**
	 * UTF-8.
	 */
	AT_CHARSET_UTF8,
	/**
	 * UTF-8.
	 */
	AT_CHARSET_UTF_8,
	/**
	 * CP-437, not fully supported.
	 */
	AT_CHARSET_PCCP437,
	/**
	 * IRA, what is same as 7-bit ASCII.
	 */
	AT_CHARSET_IRA,
	/**
	 * 7-bit ASCII.
	 */
	AT_CHARSET_ASCII,
	/**
	 * ISO-8859-1, supported only with iconv.
	 */
	AT_CHARSET_ISO88591,
	/**
	 * ISO-8859-2, supported only with iconv.
	 */
	AT_CHARSET_ISO88592,
	/**
	 * ISO-8859-3, supported only with iconv.
	 */
	AT_CHARSET_ISO88593,
	/**
	 * ISO-8859-4, supported only with iconv.
	 */
	AT_CHARSET_ISO88594,
	/**
	 * ISO-8859-5, supported only with iconv.
	 */
	AT_CHARSET_ISO88595,
	/**
	 * ISO-8859-6, supported only with iconv.
	 */
	AT_CHARSET_ISO88596,
} GSM_AT_Charset;

/**
 * Character set preference indication.
 */
typedef enum {
	/**
	 * Prefer standard GSM character set.
	 */
	AT_PREF_CHARSET_NORMAL = 1,
	/**
	 * Prefer Unicode character set to get text without any possible
	 * garbling caused by conversion.
	 */
	AT_PREF_CHARSET_UNICODE,
	/**
	 * IRA is ASCII character set. This might be useful for some
	 * operations where we only want to transmit ASCII characters
	 * and we don't want to take care about encoding or encoding of
	 * some transmitted characters is not possible to GSM.
	 */
	AT_PREF_CHARSET_IRA,
	/**
	 * For some operations, using standard GSM charset is best as it
	 * avoids problems with various broken phones and we don't need
	 * full charset anyway.
	 */
	AT_PREF_CHARSET_GSM,
	/**
	 * This option just sets again charset in phone. Useful after
	 * something what resets this information in phone.
	 */
	AT_PREF_CHARSET_RESET,
} GSM_AT_Charset_Preference;

typedef enum {
	AT_AVAILABLE = 1,
	AT_NOTAVAILABLE
} GSM_AT_Feature;

typedef enum {
	SAMSUNG_NONE = 1,
	SAMSUNG_ORG,
	SAMSUNG_SSH,
} GSM_SamsungCalendar;

typedef enum {
	AT_Status,
	AT_NextEmpty,
	AT_Total,
	AT_First,
	AT_Sizes
} GSM_AT_NeededMemoryInfo;

/**
 * Maximal length of PDU cache line. It has to be at least
 * 160 * 2 (message can be 160 bytes and hex representation needs
 * twice more space).
 */
#define GSM_AT_MAXPDULEN 400

/**
 * Structure for SMS cache.
 */
typedef struct {
	/**
	 * Location of SMS (translated).
	 */
	int Location;
	/**
	 * State of message.
	 */
	int State;
	/**
	 * PDU data.
	 */
	char PDU[GSM_AT_MAXPDULEN];
} GSM_AT_SMS_Cache;

/**
 * Maximal length of phonebook memories list.
 */
#define AT_PBK_MAX_MEMORIES	200

typedef struct {
	/**
	 * Who is manufacturer
	 */
	GSM_AT_Manufacturer	Manufacturer;
	/**
	 * Allow to simply get each line in response
	 */
	GSM_CutLines		Lines;
	/**
	 * What response type - error, OK, etc.
	 */
	GSM_AT_Reply_State	ReplyState;
	/**
	 * Error codes from responses
	 */
	int			ErrorCode;
	/**
	 * Error description
	 */
    	char			*ErrorText;

	/**
	 * Last read PBK memory
	 */
	GSM_MemoryType		PBKMemory;
	/**
	 * Supported by phone PBK memories
	 */
	char			PBKMemories[AT_PBK_MAX_MEMORIES + 1];
	/**
	 * Next empty memory entry
	 */
	int			NextMemoryEntry;
	/**
	 * First memory entry to be read
	 */
	int			FirstMemoryEntry;
	/**
	 * First memory entry to be read for Motorola
	 */
	int			MotorolaFirstMemoryEntry;
	/**
	 * Charset set in phone
	 */
	GSM_AT_Charset		Charset;
	/**
	 * True if phone encodes also commands and not only values
	 */
	gboolean			EncodedCommands;
	/**
	 * Charset to use for unicode mode
	 */
	GSM_AT_Charset		UnicodeCharset;
	/**
	 * Charset to use for non unicode mode
	 */
	GSM_AT_Charset		NormalCharset;
	/**
	 * Charset to use for IRA mode
	 */
	GSM_AT_Charset		IRACharset;
	/**
	 * Charset to use for GSM mode
	 */
	GSM_AT_Charset		GSMCharset;
	GSM_AT_Feature		PBKSBNR;
	GSM_AT_Feature		PBK_SPBR;
	GSM_AT_Feature		PBK_MPBR;
	GSM_SamsungCalendar	SamsungCalendar;
	size_t			NumberLength;
	size_t			TextLength;
	int			MemorySize;
	int			MotorolaMemorySize;
	int			MemoryUsed;

	GSM_SMSMemoryStatus	LastSMSStatus;
	int			LastSMSRead;
	int			FirstCalendarPos;
	int			FirstFreeCalendarPos;
	GSM_CalendarStatus	CalendarStatus;
	int			CalendarRead;
	/**
	 * Can we write messages to SIM memory?
	 */
	GSM_AT_Feature		SIMSaveSMS;
	/**
	 * Can we write messages to phone memory?
	 */
	GSM_AT_Feature		PhoneSaveSMS;
	/**
	 * Is phone SMS memory available ?
	 */
	GSM_AT_Feature		PhoneSMSMemory;
	/**
	 * Whether to use Motorola like SMS folders.
	 */
	gboolean			MotorolaSMS;
	/**
	 * Is SIM SMS memory available ?
	 */
	GSM_AT_Feature		SIMSMSMemory;
	/**
	 * Last read SMS memory
	 */
	GSM_MemoryType		SMSMemory;
	/**
	 * Has the memory type been set for writing?
	 */
	gboolean			SMSMemoryWrite;
	/**
	 * PDU or TEXT mode for SMS ?
	 */
	GSM_AT_SMS_Modes	SMSMode;
	/**
	 * Does phone provide enhanced text mode readout of SMS flags
	 * in text mode?
	 */
	gboolean			SMSTextDetails;

	/**
	 * Mode used for incoming message notification.
	 */
	int			CNMIMode;
	/**
	 * Procedure used for incoming message notification.
	 */
	int			CNMIProcedure;
	/**
	 * Procedure used for incoming delivery report message notification.
	 */
	int			CNMIDeliverProcedure;
#ifdef GSM_ENABLE_CELLBROADCAST
	/**
	 * Mode used for incoming broadcast message notification.
	 */
	int			CNMIBroadcastProcedure;
#endif

	/**
	 * Whether phone support Motorola like mode switching.
	 */
	gboolean			Mode;
	/**
	 * Current Motorola mode.
	 */
	int			CurrentMode;
	GSM_File		file;
	/**
	 * Number of entries in SMSCache.
	 */
	int			SMSCount;
	/**
	 * Locations of non empty SMSes.
	 */
	GSM_AT_SMS_Cache	*SMSCache;
	/**
	 * Which folder do we read SMS from.
	 */
	int			SMSReadFolder;
	/**
	 * Mode of SQWE (Siemens phones and switching to OBEX).
	 */
	int			SQWEMode;
	/**
	 * Screen width and heigth for screenshot.
	 */
	int			ScreenWidth;
	int			ScreenHeigth;
} GSM_Phone_ATGENData;

/**
 * Generates error code from current CMS error according to
 * specifications.
 *
 * \param s State machine structure.
 */
GSM_Error ATGEN_HandleCMSError	(GSM_StateMachine *s);

/**
 * Generates error code from current CME error according to
 * specifications.
 *
 * \param s State machine structure.
 */
GSM_Error ATGEN_HandleCMEError	(GSM_StateMachine *s);

GSM_Error ATGEN_DispatchMessage	(GSM_StateMachine *s);

/**
 * Wrapper around \ref GSM_WaitFor, which automatically sets
 * correct Motorola mode. It accepts same parameters as
 * \ref GSM_WaitFor.
 */
GSM_Error ATGEN_WaitFor(GSM_StateMachine *s, const char * cmd, size_t len,
			int type, int time, GSM_Phone_RequestID request);

/**
 * Wrapper around \ref ATGEN_WaitFor which automatically sets
 * length of request.
 * It accepts same parameters as \ref GSM_WaitFor except length.
 */
#define ATGEN_WaitForAutoLen(s, cmd, type, time, request) \
	ATGEN_WaitFor(s, cmd, strlen(cmd), type, time, request)

/**
 * Parses AT formatted reply. This is a bit like sprintf parser, but
 * specially focused on AT replies and can automatically convert text
 * encoding and decode some special fields.
 *
 * \par Parser tokens:
 * - \b \@i - Number, expects pointer to int.
 * - \b \@n - Quoted number, expects pointer to int.
 * - \b \@I - Optional number (string can be empty), expects pointer to int.
 * - \b \@l - Number, expects pointer to long int.
 * - \b \@s - String, will be converted from phone encoding, stripping
 *   quotes, expects pointer to unsigned char and size of storage.
 *   Encoding is somehow guessed.
 * - \b \@u - String in utf-8, expects pointer to unsigned char and size of storage.
 * - \b \@t - String with length as first element (eg. "5,test1"), will be
 *   converted from phone encoding, stripping quotes, expects pointer to
 *   unsigned char and size of storage.
 *   Encoding is somehow guessed.
 * - \b \@T - String with length as first element (eg. "5,test1"), same
 *   as \@t, just with utf-8 encoding.
 * - \b \@e - String, will be converted from phone encoding, stripping
 *   quotes, expects pointer to unsigned char and size of storage.
 *   No encoding guessing.
 * - \b \@S - String with Samsung specials (0x02 at beginning and 0x03
 *   at the end, encoded in utf-8), otherwise same as \@s.
 * - \b \@p - String with phone number hint for heuristics, otherwise
 *   same as \@s.
 * - \b \@r - Raw string, no conversion will be done, only stripped
 *   quotes, expects pointer to char and size of storage.
 * - \b \@d - Date, expects pointer to GSM_DateTime.
 * - \b \@\@ - \@ literal.
 * - \b \@0 - Ignore rest of input, same as .* regular expression.
 *
 * \par Special behaviour:
 * Any space is automatically treated as [[:space:]]* regular
 * expression. So use space whenever some weird implementation in phone
 * can insert it space.
 *
 * \param s State machine structure.
 * \param input Input string to parse.
 * \param format Format string for parser.
 * \param ... Pointers to various buffers as defined by format string.
 */
GSM_Error ATGEN_ParseReply(GSM_StateMachine *s, const unsigned char *input, const char *format, ...);

/**
 * Encodes text to current phone charset.
 *
 * \param s State machine structure.
 * \param input Input string.
 * \param inlength Length of string to convert.
 * \param output Storage for converted text.
 * \param outlength Size of output storage.
 * \param resultlength Lengt of output text will be stored here.
 *
 * \return Error code.
 */
GSM_Error ATGEN_EncodeText(GSM_StateMachine *s,
		const unsigned char *input,
		const size_t inlength,
		unsigned char *output,
		const size_t outlength,
		size_t *resultlength
		);

/**
 * Decodes text from phone encoding to internal representation.
 *
 * \param s State machine structure.
 * \param input Input string.
 * \param length Length of string to convert.
 * \param output Storage for converted text.
 * \param outlength Size of output storage.
 * \param guess Allow guessing whether input is really encoded.
 * \param phone Whether input is phone number, used only when guessing.
 *
 * \return Error code.
 */
GSM_Error ATGEN_DecodeText(GSM_StateMachine *s,
		const unsigned char *input,
		const size_t length,
		unsigned char *output,
		const size_t outlength,
		const gboolean guess,
		const gboolean phone);

/**
 * Sets charset in phone according to preference.
 *
 * \param s State machine structure.
 * \param Prefer What charset setting is prefered.
 *
 * \return Error code.
 */
GSM_Error ATGEN_SetCharset(GSM_StateMachine *s, GSM_AT_Charset_Preference Prefer);

int ATGEN_ExtractOneParameter(unsigned char *input, unsigned char *output);

/**
 * This function parses datetime strings in the format:
 * [YY[YY]/MM/DD,]hh:mm[:ss[+TZ]] , [] enclosed parts are optional
 * (or the same hex/unicode encoded).
 *
 * @todo Too many static buffers are used here.
 */
GSM_Error ATGEN_DecodeDateTime(GSM_StateMachine *s, GSM_DateTime *dt, unsigned char *_input);

#endif
/*@}*/
/*@}*/

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