summaryrefslogtreecommitdiff
path: root/libgammu/phone/at/siemens.c
blob: 09063d1b76367979ba5a1ef132ef01ed852c1335 (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
/* (c) 2002-2003 by Walek, 2005 by Michal Cihar */

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

#ifdef GSM_ENABLE_ATGEN

#include <string.h>
#include <time.h>
#include <ctype.h>

#include "../../misc/coding/coding.h"
#include "../../gsmcomon.h"
#include "../pfunc.h"

#include "atgen.h"
#include "siemens.h"

#include "../../service/gsmlogo.h"


static GSM_Error GetSiemensFrame(GSM_Protocol_Message msg, GSM_StateMachine *s, const char *templ,
			    unsigned char *buffer, size_t *len)
{
	GSM_Phone_ATGENData 	*Priv = &s->Phone.Data.Priv.ATGEN;
	int			i=2, pos=0, length=0;
	unsigned char 		buf[512];

	if (strstr(GetLineString(msg.Buffer,&Priv->Lines,2), "OK")) {
		return ERR_EMPTY;
	}
	if (!strstr(GetLineString(msg.Buffer,&Priv->Lines,2), templ)) {
		return ERR_UNKNOWN;
	}

	while (1) {
		if (Priv->Lines.numbers[i*2+1]==0) break;
		if ((!strstr(GetLineString(msg.Buffer, &Priv->Lines, i + 1), templ)) &&
				(strstr(GetLineString(msg.Buffer, &Priv->Lines, i), templ))){
			length = strlen(GetLineString(msg.Buffer, &Priv->Lines, i + 1));
			DecodeHexBin(buf, GetLineString(msg.Buffer, &Priv->Lines, i + 1),length);
			length = length / 2;
			memcpy (buffer+pos,buf,length);
			pos+=length;
		}
		i++;
	}
	*len = pos;
	return ERR_NONE;
}

static GSM_Error SetSiemensFrame (GSM_StateMachine *s, unsigned char *buff, const char *templ,
			    int Location, GSM_Phone_RequestID RequestID, size_t len)
{
	GSM_Phone_Data		*Phone = &s->Phone.Data;
	GSM_Error		error;
	unsigned char 		req[60],req1[512],hexreq[10000];
	int			MaxFrame,CurrentFrame,size,sz,pos=0;
	size_t reqlen;

	if ((len * 2) > (sizeof(hexreq) - 1)) {
		smprintf(s, "Too long request!\n");
		return ERR_MOREMEMORY;
	}

	/* Encode request data */
	EncodeHexBin(hexreq, buff, len);

	/* Calculate number of frames */
	size	 = len * 2;
	MaxFrame = size / 352;
	if (size % 352) {
		MaxFrame++;
	}

	for (CurrentFrame = 0; CurrentFrame < MaxFrame; CurrentFrame++) {
		pos = CurrentFrame * 352;
		if (pos + 352 < size) {
			sz = 352;
		} else {
			sz = size - pos;
		}

		/* Write AT command */
		reqlen = sprintf(req, "AT^SBNW=\"%s\",%i,%i,%i\r",templ,Location,CurrentFrame+1,MaxFrame);
		s->Protocol.Data.AT.EditMode = TRUE;
		error = GSM_WaitFor(s, req, reqlen, 0x00, 3, RequestID);
		s->Phone.Data.DispatchError = ERR_TIMEOUT;
		s->Phone.Data.RequestID = RequestID;
		if (error != ERR_NONE) {
			return error;
		}

		/* Write data */
		memcpy(req1, hexreq + pos, sz);
		error = s->Protocol.Functions->WriteMessage(s, req1, sz, 0x00);
		if (error!=ERR_NONE) {
			return error;
		}

		/* Write termination mark */
		error = s->Protocol.Functions->WriteMessage(s,"\x1A", 1, 0x00);
		if (error != ERR_NONE) {
			return error;
		}

		/* Wait for transaction completion */
		error = GSM_WaitForOnce(s, NULL, 0x00, 0x00, 4);
		if (error == ERR_TIMEOUT) {
			return error;
		}
	}
	/* Give phone some time to process the request */
	/* @todo: This is probably way too big */
	usleep(500000);
	return Phone->DispatchError;
}

GSM_Error SIEMENS_ReplyGetBitmap(GSM_Protocol_Message msg, GSM_StateMachine *s)
{
	unsigned char 		buffer[4096];
	size_t			length;
	GSM_Error		error;

	error = GetSiemensFrame(msg,s,"bmp",buffer,&length);
	if (error!=ERR_NONE) return error;
	smprintf(s, "Operator logo received lenght=%ld\n", (long)length);
	error = BMP2Bitmap (buffer,NULL,s->Phone.Data.Bitmap);
	if (error==ERR_NONE) return error;
	else return ERR_UNKNOWN;
}

GSM_Error SIEMENS_ReplySetFunction (GSM_Protocol_Message msg UNUSED, GSM_StateMachine *s, const char *function)
{
	if (s->Protocol.Data.AT.EditMode) {
	    s->Protocol.Data.AT.EditMode = FALSE;
	    return ERR_NONE;
	}
	smprintf(s, "Written %s",function);
  	if (s->Phone.Data.Priv.ATGEN.ReplyState == AT_Reply_OK){
  		smprintf(s, " - OK\n");
  		return ERR_NONE;
	} else {
  		smprintf(s, " - error\n");
  		return ERR_UNKNOWN;
	}
}

GSM_Error SIEMENS_ReplySetBitmap(GSM_Protocol_Message msg, GSM_StateMachine *s)
{
    return SIEMENS_ReplySetFunction (msg, s, "Operator Logo");
}

GSM_Error SIEMENS_GetBitmap(GSM_StateMachine *s, GSM_Bitmap *Bitmap)
{
	unsigned char req[32];
	size_t len;

	if (Bitmap->Type!=GSM_OperatorLogo) return ERR_NOTSUPPORTED;
	if (Bitmap->Location-1 < 0) Bitmap->Location++;
	s->Phone.Data.Bitmap=Bitmap;
	len = sprintf(req, "AT^SBNR=\"bmp\",%i\r", Bitmap->Location-1);
	smprintf(s, "Getting Bitmap\n");
	return GSM_WaitFor (s, req, len, 0x00, 4, ID_GetBitmap);
}

GSM_Error SIEMENS_SetBitmap(GSM_StateMachine *s, GSM_Bitmap *Bitmap)
{
	unsigned char 	buffer[4096];
	int 		length;
	GSM_Error	error;

	if (Bitmap->Type!=GSM_OperatorLogo) return ERR_NOTSUPPORTED;

	error = Bitmap2BMP (buffer,NULL,Bitmap);
	if (error!=ERR_NONE) return error;
	length = 0x100 * buffer[3] + buffer[2];
	buffer[58]=0xff; buffer[59]=0xff; buffer[60]=0xff;
	if (Bitmap->Location-1 < 0) Bitmap->Location++;
	s->Phone.Data.Bitmap=Bitmap;
	return SetSiemensFrame(s, buffer,"bmp",Bitmap->Location-1,
				ID_SetBitmap,length);
}

GSM_Error SIEMENS_ReplyGetRingtone(GSM_Protocol_Message msg, GSM_StateMachine *s)
{
	char 		buffer[] = "Individual";
	size_t			length;
	GSM_Error		error;

        error = GetSiemensFrame(msg,s,"mid",s->Phone.Data.Ringtone->NokiaBinary.Frame,&length);
	if (error!=ERR_NONE) return error;
	smprintf(s, "Midi ringtone received\n");

	s->Phone.Data.Ringtone->Format			= RING_MIDI;
	s->Phone.Data.Ringtone->NokiaBinary.Length	= length;
	EncodeUnicode (s->Phone.Data.Ringtone->Name,buffer,strlen(buffer));
	return ERR_NONE;
}

GSM_Error SIEMENS_GetRingtone(GSM_StateMachine *s, GSM_Ringtone *Ringtone, gboolean PhoneRingtone UNUSED)
{
	unsigned char req[32];
	size_t len;

	s->Phone.Data.Ringtone=Ringtone;
	len = sprintf(req, "AT^SBNR=\"mid\",%i\r", Ringtone->Location-1);
	smprintf(s, "Getting RingTone\n");
	return GSM_WaitFor (s, req, len, 0x00, 4, ID_GetRingtone);
}

GSM_Error SIEMENS_ReplySetRingtone(GSM_Protocol_Message msg, GSM_StateMachine *s)
{
	return SIEMENS_ReplySetFunction (msg, s, "Ringtone");
}

GSM_Error SIEMENS_SetRingtone(GSM_StateMachine *s, GSM_Ringtone *Ringtone, int *maxlength UNUSED)
{
	GSM_Phone_Data *Phone = &s->Phone.Data;

	if (Ringtone->Location==255) Ringtone->Location=1;
	if (Ringtone->Location-1 > 1) return ERR_INVALIDLOCATION;

	s->Phone.Data.Ringtone	= Ringtone;
 	Phone->Ringtone		= Ringtone;
	return SetSiemensFrame(s, Ringtone->NokiaBinary.Frame,"mid",Ringtone->Location-1,
				ID_SetRingtone,Ringtone->NokiaBinary.Length);
}

GSM_Error SIEMENS_ReplyGetNextCalendar(GSM_Protocol_Message msg, GSM_StateMachine *s)
{
	GSM_Phone_ATGENData	*Priv = &s->Phone.Data.Priv.ATGEN;
	GSM_Phone_Data		*Data = &s->Phone.Data;
	GSM_CalendarEntry	*Calendar = Data->Cal;
	GSM_ToDoEntry		ToDo;
	GSM_Error		error;
	unsigned char 		buffer[354];
	size_t			pos=0;
	size_t			len;

	switch (Priv->ReplyState) {
	case AT_Reply_OK:
 		smprintf(s, "Calendar entry received\n");
		error = GetSiemensFrame(msg, s, "vcs", buffer, &len);
		if (error != ERR_NONE) return error;
		return GSM_DecodeVCALENDAR_VTODO(&(s->di), buffer,&pos,Calendar,&ToDo,Siemens_VCalendar,0);
	case AT_Reply_Error:
		smprintf(s, "Error - too high location ?\n");
		return ERR_INVALIDLOCATION;
	case AT_Reply_CMSError:
 	        return ATGEN_HandleCMSError(s);
	case AT_Reply_CMEError:
 		/* S55 say this way, that this is empty */
 		if (Priv->ErrorCode == 100) {
 			return ERR_EMPTY;
 		}
	        return ATGEN_HandleCMEError(s);
	default:
		break;
	}
	return ERR_UNKNOWNRESPONSE;
}

GSM_Error SIEMENS_GetNextCalendar(GSM_StateMachine *s, GSM_CalendarEntry *Note, gboolean start)
{
	GSM_Phone_ATGENData	*Priv = &s->Phone.Data.Priv.ATGEN;
	GSM_Error		error;
	unsigned char 		req[32];
	int			Location;
	size_t len;

	if (Priv->Manufacturer!=AT_Siemens) return ERR_NOTSUPPORTED;

	if (start) Note->Location = Priv->FirstCalendarPos;
	s->Phone.Data.Cal 	= Note;
	Note->EntriesNum 	= 0;
	smprintf(s, "Getting VCALENDAR\n");
	Location = Note->Location;
	while (1){
		Location++;
		len = sprintf(req, "AT^SBNR=\"vcs\",%i\r",Location);
		error = GSM_WaitFor (s, req, len, 0x00, 4, ID_GetCalendarNote);
		if ((error!=ERR_NONE) && (error!=ERR_EMPTY)) {
			error = ERR_INVALIDLOCATION;
			break;
		}
		Note->Location 		= Location;
		if (Location > MAX_VCALENDAR_LOCATION) {
			error = ERR_EMPTY;
			break;
		}
		if (error==ERR_NONE)
			break;
	}
	return error;
}

GSM_Error SIEMENS_GetCalendar(GSM_StateMachine *s, GSM_CalendarEntry *Note)
{
	GSM_Phone_ATGENData	*Priv = &s->Phone.Data.Priv.ATGEN;
	unsigned char 		req[32];
	size_t len;

	if (Priv->Manufacturer!=AT_Siemens) return ERR_NOTSUPPORTED;

	s->Phone.Data.Cal = Note;
	len = sprintf(req, "AT^SBNR=\"vcs\",%i\r",Note->Location);
	smprintf(s, "Getting calendar note\n");

	return GSM_WaitFor (s, req, len, 0x00, 4, ID_GetCalendarNote);
}

GSM_Error SIEMENS_ReplyAddCalendarNote(GSM_Protocol_Message msg, GSM_StateMachine *s)
{
    return SIEMENS_ReplySetFunction (msg, s, "Calendar Note");
}

GSM_Error SIEMENS_ReplySetMemory(GSM_Protocol_Message msg, GSM_StateMachine *s)
{
    return SIEMENS_ReplySetFunction (msg, s, "Memory entry");
}

GSM_Error SIEMENS_ReplyDelCalendarNote(GSM_Protocol_Message msg UNUSED, GSM_StateMachine *s)
{
	GSM_Phone_Data *Data = &s->Phone.Data;

	if (Data->Cal->Location > MAX_VCALENDAR_LOCATION) return ERR_UNKNOWN;

	if (Data->Priv.ATGEN.ReplyState== AT_Reply_OK) {
		smprintf(s, "Calendar note deleted\n");
		return ERR_NONE;
	} else {
		smprintf(s, "Can't delete calendar note\n");
		return ERR_UNKNOWN;
	}
}

GSM_Error SIEMENS_DelCalendarNote(GSM_StateMachine *s, GSM_CalendarEntry *Note)
{
	GSM_Phone_ATGENData	*Priv = &s->Phone.Data.Priv.ATGEN;
	unsigned char 		req[32];
	size_t len;

	if (s->Phone.Data.Priv.ATGEN.Manufacturer!=AT_Siemens) return ERR_NOTSUPPORTED;
	s->Phone.Data.Cal = Note;
	len = sprintf(req, "AT^SBNW=\"vcs\",%i,0\r",Note->Location);
	smprintf(s, "Deleting calendar note\n");
	Priv->FirstFreeCalendarPos = 0;
	return GSM_WaitFor(s, req, len, 0x00, 4, ID_DeleteCalendarNote);
}

GSM_Error SIEMENS_SetCalendarNote(GSM_StateMachine *s, GSM_CalendarEntry *Note)
{
	GSM_Phone_ATGENData	*Priv = &s->Phone.Data.Priv.ATGEN;
	GSM_Error		error;
	unsigned char 		req[500];
	size_t			size=0;

	if (Priv->Manufacturer!=AT_Siemens) return ERR_NOTSUPPORTED;

	s->Phone.Data.Cal = Note;
	error=GSM_EncodeVCALENDAR(req, sizeof(req) ,&size,Note,TRUE,Siemens_VCalendar);
	if (error != ERR_NONE) return error;

	return SetSiemensFrame (s,req,"vcs",Note->Location,ID_SetCalendarNote,size);
}

GSM_Error SIEMENS_AddCalendarNote(GSM_StateMachine *s, GSM_CalendarEntry *Note)
{
	GSM_Phone_ATGENData	*Priv = &s->Phone.Data.Priv.ATGEN;
	GSM_Error		error;
	unsigned char 		req[500], req1[32];
	size_t			size=0;
	int Location;
	size_t len;

	if (Priv->Manufacturer!=AT_Siemens) return ERR_NOTSUPPORTED;

	error=GSM_EncodeVCALENDAR(req, sizeof(req),&size,Note,TRUE,Siemens_VCalendar);
	if (error != ERR_NONE) {
		return error;
	}

	Note->Location		= Priv->FirstFreeCalendarPos;
	s->Phone.Data.Cal 	= Note;
	Note->EntriesNum 	= 0;
	smprintf(s, "Getting VCALENDAR\n");
	Location = Note->Location;
	while (1){
		Location++;
		len = sprintf(req1, "AT^SBNR=\"vcs\",%i\r",Location);
		error = GSM_WaitFor (s, req1, len, 0x00, 4, ID_GetCalendarNote);
		Note->Location			= Location;
		Priv->FirstFreeCalendarPos	= Location;
		if (error==ERR_EMPTY) break;
		if (Location > MAX_VCALENDAR_LOCATION) {
			Priv->FirstFreeCalendarPos = 0;
			return ERR_FULL;
		}
		if (error!=ERR_NONE) return error;
	}
	return SetSiemensFrame (s,req,"vcs",Note->Location,ID_SetCalendarNote,size);
}

GSM_Error SIEMENS_ReplyGetMemory(GSM_Protocol_Message msg, GSM_StateMachine *s)
{
 	GSM_Phone_ATGENData 	*Priv = &s->Phone.Data.Priv.ATGEN;
 	GSM_MemoryEntry		*Memory = s->Phone.Data.Memory;
	char			buffer[4096];
	size_t			length = 0;
	GSM_Error		error;

	switch (Priv->ReplyState) {
	case AT_Reply_OK:
 		smprintf(s, "Phonebook entry received\n");
		error = GetSiemensFrame(msg,s,"vcf", buffer, &length);
		if (error != ERR_NONE) return error;
 		Memory->EntriesNum = 0;
		length = 0;
 		return GSM_DecodeVCARD(&(s->di), buffer, &length, Memory, SonyEricsson_VCard21_Phone);
	case AT_Reply_Error:
                smprintf(s, "Error - too high location ?\n");
                return ERR_INVALIDLOCATION;
	case AT_Reply_CMSError:
 	        return ATGEN_HandleCMSError(s);
	case AT_Reply_CMEError:
 		/* S55 say this way, that this is empty */
 		if (Priv->ErrorCode == 100) {
 			return ERR_EMPTY;
 		}
	        return ATGEN_HandleCMEError(s);
	default:
		break;
	}
	return ERR_UNKNOWNRESPONSE;
}

GSM_Error SIEMENS_ReplyGetMemoryInfo(GSM_Protocol_Message msg, GSM_StateMachine *s)
{
 	GSM_Phone_ATGENData 	*Priv = &s->Phone.Data.Priv.ATGEN;
	char 			*pos;
	/* Text to parse: ^SBNR: ("vcs",(1-50)) */

	Priv->PBKSBNR = AT_NOTAVAILABLE;

 	switch (Priv->ReplyState) {
 	case AT_Reply_OK:
		smprintf(s, "Memory info received\n");

		/* Parse first location */
		pos = strstr(msg.Buffer, "\"vcf\"");
		if (!pos) return ERR_NOTSUPPORTED;
		pos = strchr(pos + 1, '(');
		if (!pos) return ERR_UNKNOWNRESPONSE;
		pos++;
		if (!isdigit((int)*pos)) return ERR_UNKNOWNRESPONSE;
		Priv->FirstMemoryEntry = atoi(pos);

		/* Parse last location*/
		pos = strchr(pos, '-');
		if (!pos) return ERR_UNKNOWNRESPONSE;
		pos++;
		if (!isdigit((int)*pos)) return ERR_UNKNOWNRESPONSE;
		Priv->MemorySize = atoi(pos) + 1 - Priv->FirstMemoryEntry;

		Priv->PBKSBNR = AT_AVAILABLE;

		return ERR_NONE;
	case AT_Reply_Error:
		return ERR_NONE;
	case AT_Reply_CMSError:
	        return ATGEN_HandleCMSError(s);
	case AT_Reply_CMEError:
	        return ATGEN_HandleCMEError(s);
 	default:
		return ERR_UNKNOWNRESPONSE;
	}
}

GSM_Error SIEMENS_SetMemory(GSM_StateMachine *s, GSM_MemoryEntry *entry)
{
	GSM_Phone_ATGENData	*Priv = &s->Phone.Data.Priv.ATGEN;
	GSM_Error		error;
	unsigned char 		req[5000];
	size_t			size=0;

	if (Priv->Manufacturer != AT_Siemens) {
		return ERR_NOTSUPPORTED;
	}
	if (entry->MemoryType != MEM_ME) {
		return ERR_NOTSUPPORTED;
	}

	/*
	 * Phone does not seem to be able to handle full vCard 2.1,
	 * it might be necessary to add some another vCard type specially
	 * tailored for Siemens.
	 */
	error = GSM_EncodeVCARD(&(s->di), req, sizeof(req) ,&size, entry, TRUE, SonyEricsson_VCard10);
	if (error != ERR_NONE) return error;

	return SetSiemensFrame(s, req, "vcf", entry->Location, ID_SetMemory, size);
}

#endif

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