1 /* (c) 2002-2007 by Marcin Wiacek and Michal Cihar */
2 
3 /**
4  * @file atgen.h
5  * @author Michal Čihař
6  * @author Marcin Wiacek
7  */
8 /**
9  * @addtogroup Phone
10  * @{
11  */
12 /**
13  * @defgroup ATPhone AT phones communication
14  *
15  * This module implements standard AT commands.
16  *
17  * @see http://www.etsi.org
18  * @see http://www.3gpp.org
19  *
20  * @{
21  */
22 
23 #ifndef atgen_h
24 #define atgen_h
25 
26 #include <gammu-types.h>
27 #include <gammu-error.h>
28 #include <gammu-statemachine.h>
29 
30 #include "../../gsmreply.h" /* For GSM_Phone_RequestID */
31 #include "../../misc/misc.h" /* For GSM_CutLines */
32 
33 #include "motorola.h"
34 
35 #ifndef GSM_USED_AT
36 #  define GSM_USED_AT
37 #endif
38 #ifndef GSM_USED_BLUEAT
39 #  define GSM_USED_BLUEAT
40 #endif
41 #ifndef GSM_USED_IRDAAT
42 #  define GSM_USED_IRDAAT
43 #endif
44 
45 #define MAX_VCALENDAR_LOCATION 50
46 
47 typedef enum {
48 	SMS_AT_PDU = 1,
49 	SMS_AT_TXT
50 } GSM_AT_SMS_Modes;
51 
52 typedef enum {
53 	AT_Reply_OK = 1,
54 	AT_Reply_Connect,
55 	AT_Reply_Error,
56 	AT_Reply_Unknown,
57 	AT_Reply_CMSError,
58 	AT_Reply_CMEError,
59 	AT_Reply_SMSEdit
60 } GSM_AT_Reply_State;
61 
62 /**
63  * Manufacturer identification. This is usually used to enable some
64  * hacks which are specific for all phones from one manufacturer.
65  */
66 typedef enum {
67 	AT_Nokia = 1,
68 	AT_Alcatel,
69 	AT_Siemens,
70 	AT_HP,
71 	AT_Falcom,
72 	AT_Ericsson,
73 	AT_Sagem,
74 	AT_Samsung,
75 	AT_Mitsubishi,
76 	AT_Sharp,
77 	AT_Option,
78 	AT_Motorola,
79 	AT_Wavecom,
80 	AT_Philips,
81 	AT_Huawei,
82 	AT_Qualcomm,
83 	AT_Telit,
84 	AT_ZTE,
85 	AT_Unknown
86 } GSM_AT_Manufacturer;
87 
88 /**
89  * Character set definitions.
90  */
91 typedef enum {
92 	/**
93 	 * Hex encoded GSM.
94 	 */
95 	AT_CHARSET_HEX = 1,
96 	/**
97 	 * GSM charset.
98 	 */
99 	AT_CHARSET_GSM,
100 	/**
101 	 * Hex encoded UCS2.
102 	 */
103 	AT_CHARSET_UCS2,
104 	/**
105 	 * Hex encoded UCS2.
106 	 */
107 	AT_CHARSET_UCS_2,
108 	/**
109 	 * UTF-8.
110 	 */
111 	AT_CHARSET_UTF8,
112 	/**
113 	 * UTF-8.
114 	 */
115 	AT_CHARSET_UTF_8,
116 	/**
117 	 * CP-437, not fully supported.
118 	 */
119 	AT_CHARSET_PCCP437,
120 	/**
121 	 * IRA, what is same as 7-bit ASCII.
122 	 */
123 	AT_CHARSET_IRA,
124 	/**
125 	 * 7-bit ASCII.
126 	 */
127 	AT_CHARSET_ASCII,
128 	/**
129 	 * ISO-8859-1, supported only with iconv.
130 	 */
131 	AT_CHARSET_ISO88591,
132 	/**
133 	 * ISO-8859-2, supported only with iconv.
134 	 */
135 	AT_CHARSET_ISO88592,
136 	/**
137 	 * ISO-8859-3, supported only with iconv.
138 	 */
139 	AT_CHARSET_ISO88593,
140 	/**
141 	 * ISO-8859-4, supported only with iconv.
142 	 */
143 	AT_CHARSET_ISO88594,
144 	/**
145 	 * ISO-8859-5, supported only with iconv.
146 	 */
147 	AT_CHARSET_ISO88595,
148 	/**
149 	 * ISO-8859-6, supported only with iconv.
150 	 */
151 	AT_CHARSET_ISO88596,
152 } GSM_AT_Charset;
153 
154 /**
155  * Character set preference indication.
156  */
157 typedef enum {
158 	/**
159 	 * Prefer standard GSM character set.
160 	 */
161 	AT_PREF_CHARSET_NORMAL = 1,
162 	/**
163 	 * Prefer Unicode character set to get text without any possible
164 	 * garbling caused by conversion.
165 	 */
166 	AT_PREF_CHARSET_UNICODE,
167 	/**
168 	 * IRA is ASCII character set. This might be useful for some
169 	 * operations where we only want to transmit ASCII characters
170 	 * and we don't want to take care about encoding or encoding of
171 	 * some transmitted characters is not possible to GSM.
172 	 */
173 	AT_PREF_CHARSET_IRA,
174 	/**
175 	 * For some operations, using standard GSM charset is best as it
176 	 * avoids problems with various broken phones and we don't need
177 	 * full charset anyway.
178 	 */
179 	AT_PREF_CHARSET_GSM,
180 	/**
181 	 * This option just sets again charset in phone. Useful after
182 	 * something what resets this information in phone.
183 	 */
184 	AT_PREF_CHARSET_RESET,
185 } GSM_AT_Charset_Preference;
186 
187 typedef enum {
188 	AT_AVAILABLE = 1,
189 	AT_NOTAVAILABLE
190 } GSM_AT_Feature;
191 
192 typedef enum {
193 	SAMSUNG_NONE = 1,
194 	SAMSUNG_ORG,
195 	SAMSUNG_SSH,
196 } GSM_SamsungCalendar;
197 
198 typedef enum {
199 	AT_Status,
200 	AT_NextEmpty,
201 	AT_Total,
202 	AT_First,
203 	AT_Sizes
204 } GSM_AT_NeededMemoryInfo;
205 
206 /**
207  * Maximal length of PDU cache line. It has to be at least
208  * 160 * 2 (message can be 160 bytes and hex representation needs
209  * twice more space).
210  */
211 #define GSM_AT_MAXPDULEN 400
212 
213 /**
214  * Structure for SMS cache.
215  */
216 typedef struct {
217 	/**
218 	 * Location of SMS (translated).
219 	 */
220 	int Location;
221 	/**
222 	 * State of message.
223 	 */
224 	int State;
225 	/**
226 	 * PDU data.
227 	 */
228 	char PDU[GSM_AT_MAXPDULEN];
229 } GSM_AT_SMS_Cache;
230 
231 /**
232  * Structure for SMS Info cache.
233  */
234 typedef struct {
235 	size_t cache_size;
236 	unsigned int cache_used;
237 	GSM_SMSMessage *smsInfo_records;
238 } GSM_AT_SMSInfo_Cache;
239 
240 /**
241  * Maximal length of phonebook memories list.
242  */
243 #define AT_PBK_MAX_MEMORIES	200
244 
245 typedef struct {
246 	/**
247 	 * Who is manufacturer
248 	 */
249 	GSM_AT_Manufacturer	Manufacturer;
250 	/**
251 	 * Allow to simply get each line in response
252 	 */
253 	GSM_CutLines		Lines;
254 	/**
255 	 * What response type - error, OK, etc.
256 	 */
257 	GSM_AT_Reply_State	ReplyState;
258 	/**
259 	 * Error codes from responses
260 	 */
261 	int			ErrorCode;
262 	/**
263 	 * Error description
264 	 */
265     	char			*ErrorText;
266 
267 	/**
268 	 * Last read PBK memory
269 	 */
270 	GSM_MemoryType		PBKMemory;
271 	/**
272 	 * Supported by phone PBK memories
273 	 */
274 	char			PBKMemories[AT_PBK_MAX_MEMORIES + 1];
275 	/**
276 	 * Next empty memory entry
277 	 */
278 	int			NextMemoryEntry;
279 	/**
280 	 * First memory entry to be read
281 	 */
282 	int			FirstMemoryEntry;
283 	/**
284 	 * First memory entry to be read for Motorola
285 	 */
286 	int			MotorolaFirstMemoryEntry;
287 	/**
288 	 * Charset set in phone
289 	 */
290 	GSM_AT_Charset		Charset;
291 	/**
292 	 * True if phone encodes also commands and not only values
293 	 */
294 	gboolean			EncodedCommands;
295 	/**
296 	 * Charset to use for unicode mode
297 	 */
298 	GSM_AT_Charset		UnicodeCharset;
299 	/**
300 	 * Charset to use for non unicode mode
301 	 */
302 	GSM_AT_Charset		NormalCharset;
303 	/**
304 	 * Charset to use for IRA mode
305 	 */
306 	GSM_AT_Charset		IRACharset;
307 	/**
308 	 * Charset to use for GSM mode
309 	 */
310 	GSM_AT_Charset		GSMCharset;
311 	GSM_AT_Feature		PBKSBNR;
312 	GSM_AT_Feature		PBK_SPBR;
313 	GSM_AT_Feature		PBK_MPBR;
314 	GSM_SamsungCalendar	SamsungCalendar;
315 	size_t			NumberLength;
316 	size_t			TextLength;
317 	int			MemorySize;
318 	int			MotorolaMemorySize;
319 	int			MemoryUsed;
320   int			NumFolders;
321 
322 	GSM_SMSMemoryStatus	LastSMSStatus;
323 	int			LastSMSRead;
324 	int			FirstCalendarPos;
325 	int			FirstFreeCalendarPos;
326 	GSM_CalendarStatus	CalendarStatus;
327 	int			CalendarRead;
328 	/**
329 	 * Can we write messages to SIM memory?
330 	 */
331 	GSM_AT_Feature		SIMSaveSMS;
332 	/**
333 	 * Can we write messages to phone memory?
334 	 */
335 	GSM_AT_Feature		PhoneSaveSMS;
336 	/**
337 	 * Is phone SMS memory available ?
338 	 */
339 	GSM_AT_Feature		PhoneSMSMemory;
340 	/**
341 	 * Whether to use Motorola like SMS folders.
342 	 */
343 	gboolean			MotorolaSMS;
344 	/**
345 	 * Is SIM SMS memory available ?
346 	 */
347 	GSM_AT_Feature		SIMSMSMemory;
348 	/**
349 	 * Last read SMS memory
350 	 */
351 	GSM_MemoryType		SMSMemory;
352 	/**
353 	 * Has the memory type been set for writing?
354 	 */
355 	gboolean			SMSMemoryWrite;
356 	/**
357 	 * PDU or TEXT mode for SMS ?
358 	 */
359 	GSM_AT_SMS_Modes	SMSMode;
360 	/**
361 	 * Does phone provide enhanced text mode readout of SMS flags
362 	 * in text mode?
363 	 */
364 	gboolean			SMSTextDetails;
365 
366 	/**
367 	 * Mode used for incoming message notification.
368 	 */
369 	int			CNMIMode;
370 	/**
371 	 * Procedure used for incoming message notification.
372 	 */
373 	int			CNMIProcedure;
374 	/**
375 	 * Procedure used for incoming delivery report message notification.
376 	 */
377 	int			CNMIDeliverProcedure;
378 #ifdef GSM_ENABLE_CELLBROADCAST
379 	/**
380 	 * Mode used for incoming broadcast message notification.
381 	 */
382 	int			CNMIBroadcastProcedure;
383 #endif
384 	/**
385 	 * Mode used to indicate unsolicited result code handling.
386 	 */
387         int CNMIClearUnsolicitedResultCodes;
388 	/**
389 	 * Whether phone support Motorola like mode switching.
390 	 */
391 	gboolean			Mode;
392 	/**
393 	 * Current Motorola mode.
394 	 */
395 	int			CurrentMode;
396 	GSM_File		file;
397 	/**
398 	 * Number of entries in SMSCache.
399 	 */
400 	int			SMSCount;
401 	/**
402 	 * Locations of non empty SMSes.
403 	 */
404 	GSM_AT_SMS_Cache	*SMSCache;
405 	/**
406 	 * Which folder do we read SMS from.
407 	 */
408 	int			SMSReadFolder;
409 	/**
410 	 * Mode of SQWE (Siemens phones and switching to OBEX).
411 	 */
412 	int			SQWEMode;
413   /**
414    * Screen width and height for screenshot.
415    */
416 	int			ScreenWidth;
417 	int			ScreenHeigth;
418   /**
419    * Is phone SR memory available ?
420    */
421   GSM_AT_Feature		SRSMSMemory;
422   /**
423    * Can we write to phone SR memory?
424    */
425   GSM_AT_Feature		SRSaveSMS;
426 	/**
427 	 * Cache for incoming SMS info.
428 	 */
429 	GSM_AT_SMSInfo_Cache SMSInfoCache;
430   /**
431    * Whether device supports CHUP for hanging up calls.
432    */
433   gboolean HasCHUP;
434   /**
435    * TRUE if an incoming call is in the process of being dropped.
436    */
437   gboolean CancellingCall;
438 } GSM_Phone_ATGENData;
439 
440 /**
441  * Determine if the memory type is available.
442  *
443  * \param data AT generic phone data.
444  * \param type GSM memory type to check.
445  *
446  * \return TRUE if the memory is available.
447  */
448 gboolean ATGEN_IsMemoryAvailable (const GSM_Phone_ATGENData *data, GSM_MemoryType type);
449 
450 /**
451  * Determine if the memory is configured for writing.
452  *
453  * \param data AT generic phone data.
454  * \param type GSM memory type to check.
455  *
456  * \return TRUE if the memory can be written to.
457  */
458 gboolean ATGEN_IsMemoryWriteable(const GSM_Phone_ATGENData *data, GSM_MemoryType type);
459 
460 /**
461  * Sets the requested memory type on the MT
462  *
463  * @param s State machine (SM) structure
464  * @param memoryType The memory type to set
465  * @param writeable Request memory set to writeable
466  * @param requestId The request ID for SM routing, usually ID_SetMemoryType
467  */
468 GSM_Error ATGEN_SetRequestedSMSMemory(GSM_StateMachine *s, GSM_MemoryType memoryType, gboolean writeable,
469                                       GSM_Phone_RequestID requestId);
470 
471 /**
472  * Generates error code from current CMS error according to
473  * specifications.
474  *
475  * \param s State machine structure.
476  */
477 GSM_Error ATGEN_HandleCMSError	(GSM_StateMachine *s);
478 
479 /**
480  * Generates error code from current CME error according to
481  * specifications.
482  *
483  * \param s State machine structure.
484  */
485 GSM_Error ATGEN_HandleCMEError	(GSM_StateMachine *s);
486 
487 GSM_Error ATGEN_DispatchMessage	(GSM_StateMachine *s);
488 
489 /**
490  * Wrapper around \ref GSM_WaitFor, which automatically sets
491  * correct Motorola mode. It accepts same parameters as
492  * \ref GSM_WaitFor.
493  */
494 GSM_Error ATGEN_WaitFor(GSM_StateMachine *s, const char * cmd, size_t len,
495 			int type, int time, GSM_Phone_RequestID request);
496 
497 /**
498  * Wrapper around \ref ATGEN_WaitFor which automatically sets
499  * length of request.
500  * It accepts same parameters as \ref GSM_WaitFor except length.
501  */
502 #define ATGEN_WaitForAutoLen(s, cmd, type, time, request) \
503 	ATGEN_WaitFor(s, cmd, strlen(cmd), type, time, request)
504 
505 /**
506  * Parses AT formatted reply. This is a bit like sprintf parser, but
507  * specially focused on AT replies and can automatically convert text
508  * encoding and decode some special fields.
509  *
510  * \par Parser tokens:
511  * - \b \@i - Number, expects pointer to int.
512  * - \b \@n - Quoted number, expects pointer to int.
513  * - \b \@I - Optional number (string can be empty), expects pointer to int.
514  * - \b \@l - Number, expects pointer to long int.
515  * - \b \@s - String, will be converted from phone encoding, stripping
516  *   quotes, expects pointer to unsigned char and size of storage.
517  *   Encoding is somehow guessed.
518  * - \b \@u - String in utf-8, expects pointer to unsigned char and size of storage.
519  * - \b \@t - String with length as first element (eg. "5,test1"), will be
520  *   converted from phone encoding, stripping quotes, expects pointer to
521  *   unsigned char and size of storage.
522  *   Encoding is somehow guessed.
523  * - \b \@T - String with length as first element (eg. "5,test1"), same
524  *   as \@t, just with utf-8 encoding.
525  * - \b \@e - String, will be converted from phone encoding, stripping
526  *   quotes, expects pointer to unsigned char and size of storage.
527  *   No encoding guessing.
528  * - \b \@S - String with Samsung specials (0x02 at beginning and 0x03
529  *   at the end, encoded in utf-8), otherwise same as \@s.
530  * - \b \@p - String with phone number hint for heuristics, otherwise
531  *   same as \@s.
532  * - \b \@r - Raw string, no conversion will be done, only stripped
533  *   quotes, expects pointer to char and size of storage.
534  * - \b \@d - Date, expects pointer to GSM_DateTime.
535  * - \b \@\@ - \@ literal.
536  * - \b \@0 - Ignore rest of input, same as .* regular expression.
537  *
538  * \par Special behaviour:
539  * Any space is automatically treated as [[:space:]]* regular
540  * expression. So use space whenever some weird implementation in phone
541  * can insert it space.
542  *
543  * \param s State machine structure.
544  * \param input Input string to parse.
545  * \param format Format string for parser.
546  * \param ... Pointers to various buffers as defined by format string.
547  */
548 GSM_Error ATGEN_ParseReply(GSM_StateMachine *s, const unsigned char *input, const char *format, ...);
549 
550 /**
551  * Encodes text to current phone charset.
552  *
553  * \param s State machine structure.
554  * \param input Input string.
555  * \param inlength Length of string to convert.
556  * \param output Storage for converted text.
557  * \param outlength Size of output storage.
558  * \param resultlength Lengt of output text will be stored here.
559  *
560  * \return Error code.
561  */
562 GSM_Error ATGEN_EncodeText(GSM_StateMachine *s,
563 		const unsigned char *input,
564 		const size_t inlength,
565 		unsigned char *output,
566 		const size_t outlength,
567 		size_t *resultlength
568 		);
569 
570 /**
571  * Decodes text from phone encoding to internal representation.
572  *
573  * \param s State machine structure.
574  * \param input Input string.
575  * \param length Length of string to convert.
576  * \param output Storage for converted text.
577  * \param outlength Size of output storage.
578  * \param guess Allow guessing whether input is really encoded.
579  * \param phone Whether input is phone number, used only when guessing.
580  *
581  * \return Error code.
582  */
583 GSM_Error ATGEN_DecodeText(GSM_StateMachine *s,
584 		const unsigned char *input,
585 		const size_t length,
586 		unsigned char *output,
587 		const size_t outlength,
588 		const gboolean guess,
589 		const gboolean phone);
590 
591 /**
592  * Sets charset in phone according to preference.
593  *
594  * \param s State machine structure.
595  * \param Prefer What charset setting is prefered.
596  *
597  * \return Error code.
598  */
599 GSM_Error ATGEN_SetCharset(GSM_StateMachine *s, GSM_AT_Charset_Preference Prefer);
600 
601 int ATGEN_ExtractOneParameter(unsigned char *input, unsigned char *output);
602 
603 /**
604  * This function parses datetime strings in the format:
605  * [YY[YY]/MM/DD,]hh:mm[:ss[+TZ]] , [] enclosed parts are optional
606  * (or the same hex/unicode encoded).
607  *
608  * @todo Too many static buffers are used here.
609  */
610 GSM_Error ATGEN_DecodeDateTime(GSM_StateMachine *s, GSM_DateTime *dt, unsigned char *_input);
611 
612 #endif
613 /*@}*/
614 /*@}*/
615 
616 /* How should editor hadle tabs in this file? Add editor commands here.
617  * vim: noexpandtab sw=8 ts=8 sts=8:
618  */
619