Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
[gnokii.git] / common / gsm-sms.c
1 /*
2
3   $Id$
4
5   G N O K I I
6
7   A Linux/Unix toolset and driver for Nokia mobile phones.
8
9   Copyright (C) 2001 Pawe³ Kot <pkot@linuxnews.pl>
10
11   Released under the terms of the GNU GPL, see file COPYING for more details.
12
13   Library for parsing and creating Short Messages (SMS).
14
15   $Log$
16   Revision 1.1.1.1.2.1  2001/11/27 22:48:37  short
17   Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
18
19   Revision 1.1.1.2  2001/11/27 22:01:13  short
20   :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 22:58 CET 2001
21
22   Revision 1.14  2001/11/27 12:19:00  pkot
23   Cleanup, indentation, ANSI complaint preprocesor symbols (Jan Kratochvil, me)
24
25   Revision 1.13  2001/11/23 22:07:44  machek
26   Fix SMS receiving to work, again. Unfortunately, it is not possible to
27   reuse much of gsm-sms.c...
28
29   Revision 1.12  2001/11/22 17:56:53  pkot
30   smslib update. sms sending
31
32   Revision 1.11  2001/11/20 16:22:22  pkot
33   First attempt to read Picture Messages. They should appear when you enable DEBUG. Nokia seems to break own standards. :/ (Markus Plail)
34
35   Revision 1.10  2001/11/19 13:09:40  pkot
36   Begin work on sms sending
37
38   Revision 1.9  2001/11/18 00:54:32  pkot
39   Bugfixes. I18n of the user responses. UDH support in libsms. Business Card UDH Type
40
41   Revision 1.8  2001/11/17 20:19:29  pkot
42   smslib cleanups, fixes and debugging
43
44   Revision 1.7  2001/11/15 12:15:04  pkot
45   smslib updates. begin work on sms in 6100 series
46
47   Revision 1.6  2001/11/14 18:21:19  pkot
48   Fixing some problems with UDH and Unicode, but still doesn't work yet :-(
49
50   Revision 1.5  2001/11/14 14:26:18  pkot
51   Changed offset of DCS field to the right value in 6210/7110
52
53   Revision 1.4  2001/11/14 11:26:18  pkot
54   Getting SMS in 6210/7110 does finally work in some cases :)
55
56   Revision 1.3  2001/11/13 16:12:20  pkot
57   Preparing libsms to get to work. 6210/7110 SMS and SMS Folder updates
58
59   Revision 1.2  2001/11/09 14:25:04  pkot
60   DEBUG cleanups
61
62   Revision 1.1  2001/11/08 16:23:21  pkot
63   New version of libsms. Not functional yet, but it reasonably stable API.
64
65   Revision 1.1  2001/07/09 23:06:26  pkot
66   Moved sms.* files from my hard disk to CVS
67
68 */
69
70 #include <stdlib.h>
71 #include <string.h>
72
73 #include "gsm-common.h"
74 #include "gsm-encoding.h"
75 #include "gsm-bitmaps.h"
76
77 struct udh_data {
78         unsigned int length;
79         char *header;
80 };
81
82 /* Number of header specific octets in SMS header (before data) */
83 static unsigned short DataOffset[] = {
84         4, /* SMS Deliver */
85         3, /* SMS Deliver Report */
86         5, /* SMS Submit */
87         3, /* SMS Submit Report */
88         3, /* SMS Command */
89         3  /* SMS Status Report */
90 };
91
92 /* User data headers */
93 static struct udh_data headers[] = {
94         { 0x00, "" },
95         { 0x05, "\x00\x03\x01\x00\x00" },     /* Concatenated messages */
96         { 0x06, "\x05\x04\x15\x82\x00\x00" }, /* Operator logos */
97         { 0x06, "\x05\x04\x15\x83\x00\x00" }, /* Caller logos */
98         { 0x06, "\x05\x04\x15\x81\x00\x00" }, /* Ringtones */
99         { 0x04, "\x03\x01\x00\x00" },         /* Voice Messages */
100         { 0x04, "\x03\x01\x01\x00" },         /* Fax Messages */
101         { 0x04, "\x03\x01\x02\x00" },         /* Email Messages */
102         { 0x06, "\x05\x04\x23\xf4\x00\x00" }, /* Business Card */
103         { 0x00, "" }
104 };
105
106
107 /***
108  *** Util functions
109  ***/
110
111 /* This function implements packing of numbers (SMS Center number and
112    destination number) for SMS sending function. */
113 static int SemiOctetPack(char *Number, unsigned char *Output, SMS_NumberType type)
114 {
115         unsigned char *IN = Number;  /* Pointer to the input number */
116         unsigned char *OUT = Output; /* Pointer to the output */
117         int count = 0; /* This variable is used to notify us about count of already
118                           packed numbers. */
119
120         /* The first byte in the Semi-octet representation of the address field is
121            the Type-of-Address. This field is described in the official GSM
122            specification 03.40 version 6.1.0, section 9.1.2.5, page 33. We support
123            only international and unknown number. */
124
125         *OUT++ = type;
126         if (type == SMS_International) IN++; /* Skip '+' */
127         if ((type == SMS_Unknown) && (*IN == '+')) IN++; /* Optional '+' in Unknown number type */
128
129         /* The next field is the number. It is in semi-octet representation - see
130            GSM scpecification 03.40 version 6.1.0, section 9.1.2.3, page 31. */
131         while (*IN) {
132                 if (count & 0x01) {
133                         *OUT = *OUT | ((*IN - '0') << 4);
134                         OUT++;
135                 }
136                 else
137                         *OUT = *IN - '0';
138                 count++; IN++;
139         }
140
141         /* We should also fill in the most significant bits of the last byte with
142            0x0f (1111 binary) if the number is represented with odd number of
143            digits. */
144         if (count & 0x01) {
145                 *OUT = *OUT | 0xf0;
146                 OUT++;
147         }
148
149         return (2 * (OUT - Output - 1) - (count % 2));
150 }
151
152 char *GetBCDNumber(u8 *Number)
153 {
154         static char Buffer[20] = "";
155         int length = Number[0]; /* This is the length of BCD coded number */
156         int count, Digit;
157
158         memset(Buffer, 0, 20);
159         switch (Number[1]) {
160         case SMS_Alphanumeric:
161                 Unpack7BitCharacters(0, length, length, Number+2, Buffer);
162                 Buffer[length] = 0;
163                 break;
164         case SMS_International:
165                 sprintf(Buffer, "+");
166         case SMS_Unknown:
167         case SMS_National:
168         case SMS_Network:
169         case SMS_Subscriber:
170         case SMS_Abbreviated:
171         default:
172                 for (count = 0; count < length - 1; count++) {
173                         Digit = Number[count+2] & 0x0f;
174                         if (Digit < 10) sprintf(Buffer, "%s%d", Buffer, Digit);
175                         Digit = Number[count+2] >> 4;
176                         if (Digit < 10) sprintf(Buffer, "%s%d", Buffer, Digit);
177                 }
178                 break;
179         }
180         return Buffer;
181 }
182
183 static char *PrintDateTime(u8 *Number) 
184 {
185         static char Buffer[23] = "";
186
187         memset(Buffer, 0, 23);
188         if (Number[0] < 70) sprintf(Buffer, "20");
189         else sprintf(Buffer, "19");
190         sprintf(Buffer, "%s%d%d-", Buffer, Number[0] & 0x0f, Number[0] >> 4);
191         sprintf(Buffer, "%s%d%d-", Buffer, Number[1] & 0x0f, Number[1] >> 4);
192         sprintf(Buffer, "%s%d%d ", Buffer, Number[2] & 0x0f, Number[2] >> 4);
193         sprintf(Buffer, "%s%d%d:", Buffer, Number[3] & 0x0f, Number[3] >> 4);
194         sprintf(Buffer, "%s%d%d:", Buffer, Number[4] & 0x0f, Number[4] >> 4);
195         sprintf(Buffer, "%s%d%d",  Buffer, Number[5] & 0x0f, Number[5] >> 4);
196         if (Number[6] & 0x08) 
197                 sprintf(Buffer, "%s-", Buffer);
198         else
199                 sprintf(Buffer, "%s+", Buffer);
200         sprintf(Buffer, "%s%02d00", Buffer, (10 * (Number[6] & 0x07) + (Number[6] >> 4)) / 4);
201
202         return Buffer;
203 }
204
205 SMS_DateTime *UnpackDateTime(u8 *Number, SMS_DateTime *dt)
206 {
207         dt->Year     =  10 * (Number[0] & 0x0f) + (Number[0] >> 4);
208         if (dt->Year < 70) dt->Year += 2000;
209         else dt->Year += 1900;
210         dt->Month    =  10 * (Number[1] & 0x0f) + (Number[1] >> 4);
211         dt->Day      =  10 * (Number[2] & 0x0f) + (Number[2] >> 4);
212         dt->Hour     =  10 * (Number[3] & 0x0f) + (Number[3] >> 4);
213         dt->Minute   =  10 * (Number[4] & 0x0f) + (Number[4] >> 4);
214         dt->Second   =  10 * (Number[5] & 0x0f) + (Number[5] >> 4);
215         dt->Timezone = (10 * (Number[6] & 0x07) + (Number[6] >> 4)) / 4;
216         if (Number[6] & 0x08) dt->Timezone = -dt->Timezone;
217
218         return dt;
219 }
220
221 /***
222  *** ENCODING SMS
223  ***/
224
225 static GSM_Error EncodeData(GSM_SMSMessage *SMS, char *dcs, char *message)
226 {
227         SMS_AlphabetType al;
228         unsigned short length = strlen(SMS->MessageText);
229
230         switch (SMS->DCS.Type) {
231         case SMS_GeneralDataCoding:
232                 switch (SMS->DCS.u.General.Class) {
233                 case 1: dcs[0] |= 0xf0; break;
234                 case 2: dcs[0] |= 0xf1; break;
235                 case 3: dcs[0] |= 0xf2; break;
236                 case 4: dcs[0] |= 0xf3; break;
237                 default: break;
238                 }
239                 if (SMS->DCS.u.General.Compressed) {
240                         /* Compression not supported yet */
241                         /* dcs[0] |= 0x20; */
242                 }
243                 al = SMS->DCS.u.General.Alphabet;
244                 break;
245         case SMS_MessageWaiting:
246                 al = SMS->DCS.u.MessageWaiting.Alphabet;
247                 if (SMS->DCS.u.MessageWaiting.Discard) dcs[0] |= 0xc0;
248                 else if (SMS->DCS.u.MessageWaiting.Alphabet == SMS_UCS2) dcs[0] |= 0xe0;
249                 else dcs[0] |= 0xd0;
250
251                 if (SMS->DCS.u.MessageWaiting.Active) dcs[0] |= 0x08;
252                 dcs[0] |= (SMS->DCS.u.MessageWaiting.Type & 0x03);
253
254                 break;
255         default:
256                 return GE_SMSWRONGFORMAT;
257         }
258         switch (al) {
259         case SMS_DefaultAlphabet:
260                 Pack7BitCharacters((7 - (SMS->UDH_Length % 7)) % 7, SMS->MessageText, message);
261                 SMS->Length = 8 * SMS->UDH_Length + (7 - (SMS->UDH_Length % 7)) % 7 + length;
262                 break;
263         case SMS_8bit:
264                 dcs[0] |= 0xf4;
265                 memcpy(message, SMS->MessageText + 1, SMS->MessageText[0]);
266                 SMS->Length = SMS->UDH_Length + SMS->MessageText[0];
267                 break;
268         case SMS_UCS2:
269                 dcs[0] |= 0x08;
270                 EncodeUnicode(message, SMS->MessageText, length);
271                 SMS->Length = length;
272                 break;
273         default:
274                 return GE_SMSWRONGFORMAT;
275         }
276         return GE_NONE;
277 }
278
279 /* This function encodes the UserDataHeader as described in:
280    - GSM 03.40 version 6.1.0 Release 1997, section 9.2.3.24
281    - Smart Messaging Specification, Revision 1.0.0, September 15, 1997
282 */
283 static GSM_Error EncodeUDH(SMS_UDHInfo UDHi, char *UDH)
284 {
285         unsigned char pos;
286
287         pos = UDH[0];
288         switch (UDHi.Type) {
289         case SMS_NoUDH:
290                 break;
291         case SMS_VoiceMessage:
292         case SMS_FaxMessage:
293         case SMS_EmailMessage:
294                 UDH[pos+4] = UDHi.u.SpecialSMSMessageIndication.MessageCount;
295                 if (UDHi.u.SpecialSMSMessageIndication.Store) UDH[pos+3] |= 0x80;
296         case SMS_ConcatenatedMessages:
297         case SMS_OpLogo:
298         case SMS_CallerIDLogo:
299         case SMS_Ringtone:
300                 UDH[0] += headers[UDHi.Type].length;
301                 memcpy(UDH+pos+1, headers[UDHi.Type].header, headers[UDHi.Type].length);
302                 break;
303         default:
304                 dprintf("Not supported User Data Header type\n");
305                 break;
306         }
307         return GE_NONE;
308 }
309
310 static GSM_Error EncodeSMSSubmitHeader(GSM_SMSMessage *SMS, char *frame)
311 {
312         GSM_Error error = GE_NONE;
313
314         /* Standard Header: */
315         memcpy(frame, "\x11\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa9\x00\x00\x00\x00\x00\x00", 24);
316
317         /* Reply Path */
318         if (SMS->ReplyViaSameSMSC) frame[0] |= 0x80;
319
320         /* User Data Header Indicator */
321         if (SMS->UDH_No) frame[0] |= 0x40;
322
323         /* Status (Delivery) Report Request */
324         if (SMS->ReportStatus) frame[0] |= 0x20;
325
326         /* Validity Period Format: mask - 0x00, 0x10, 0x08, 0x18 */
327         frame[0] |= ((SMS->Validity.VPF & 0x03) << 3);
328
329         /* Reject Duplicates */
330         if (SMS->RejectDuplicates) frame[0] |= 0x04;
331
332         /* Message Type is already set */
333
334         /* Message Reference */
335         /* Can we set this? */
336
337         /* Protocol Identifier */
338         /* FIXME: allow to change this in better way.
339            currently only 0x5f == `Return Call Message' is used */
340         if (SMS->PID) frame[3] = SMS->PID;
341
342         /* Data Coding Scheme */
343         switch (SMS->DCS.Type) {
344         case SMS_GeneralDataCoding:
345                 if (SMS->DCS.u.General.Compressed) frame[4] |= 0x20;
346                 if (SMS->DCS.u.General.Class) frame[4] |= (0x10 | (SMS->DCS.u.General.Class - 1));
347                 frame[4] |= ((SMS->DCS.u.General.Alphabet & 0x03) << 2);
348                 break;
349         case SMS_MessageWaiting:
350                 if (SMS->DCS.u.MessageWaiting.Discard) frame[4] |= 0xc0;
351                 else if (SMS->DCS.u.MessageWaiting.Alphabet == SMS_UCS2) frame[4] |= 0xe0;
352                 else frame[4] |= 0xd0;
353                 if (SMS->DCS.u.MessageWaiting.Active) frame[4] |= 0x80;
354                 frame[4] |= (SMS->DCS.u.MessageWaiting.Type & 0x03);
355                 break;
356         default:
357                 dprintf("Wrong Data Coding Scheme (DCS) format\n");
358                 return GE_SMSWRONGFORMAT;
359         }
360
361         /* Destination Address */
362         frame[5] = SemiOctetPack(SMS->RemoteNumber.number, frame + 6, SMS->RemoteNumber.type);
363
364         /* Validity Period */
365         switch (SMS->Validity.VPF) {
366         case SMS_EnhancedFormat:
367                 break;
368         case SMS_RelativeFormat:
369                 break;
370         case SMS_AbsoluteFormat:
371                 break;
372         default:
373                 break;
374         }
375
376         return error;
377 }
378
379 static GSM_Error EncodeSMSDeliverHeader()
380 {
381         return GE_NONE;
382 }
383
384 static GSM_Error EncodeSMSHeader(GSM_SMSMessage *SMS, char *frame)
385 /* We can create either SMS DELIVER (for saving in Inbox) or SMS SUBMIT
386    (for sending or saving in Outbox) message */
387 {
388         /* Set SMS type */
389         frame[12] |= (SMS->Type >> 1);
390         switch (SMS->Type) {
391         case SMS_Submit: /* we send SMS or save it in Outbox */
392                 return EncodeSMSSubmitHeader(SMS, frame);
393         case SMS_Deliver: /* we save SMS in Inbox */
394                 return EncodeSMSDeliverHeader(SMS, frame);
395         default: /* we don't create other formats of SMS */
396                 return GE_SMSWRONGFORMAT;
397         }
398 }
399
400 /* This function encodes SMS as described in:
401    - GSM 03.40 version 6.1.0 Release 1997, section 9
402 */
403 int EncodePDUSMS(GSM_SMSMessage *SMS, char *message)
404 {
405         GSM_Error error = GE_NONE;
406         int i;
407
408         dprintf("Sending SMS to %s via message center %s\n", SMS->RemoteNumber.number, SMS->MessageCenter.Number);
409
410         /* SMSC number */
411         dprintf("%d %s\n", SMS->MessageCenter.Type, SMS->MessageCenter.Number);
412         message[0] = SemiOctetPack(SMS->MessageCenter.Number, message + 1, SMS->MessageCenter.Type);
413         if (message[0] % 2) message[0]++;
414         message[0] = message[0] / 2 + 1;
415
416         /* Common Header */
417         error = EncodeSMSHeader(SMS, message + 12);
418         if (error != GE_NONE) return error;
419
420         /* User Data Header - if present */
421 //      for (i = 0; i < SMS->UDH_No; i++) {
422 //              error = EncodeUDH(SMS->UDH[i], message + 24);
423 //              if (error != GE_NONE) return error;
424 //      }
425         SMS->UDH_Length = 0;
426
427         /* User Data */
428         EncodeData(SMS, message + 14, message + 36 + SMS->UDH_Length);
429         message[16] = SMS->Length;
430         return SMS->Length + 35;
431 }
432
433 /* This function does simple SMS encoding - no PDU coding */
434 GSM_Error EncodeTextSMS()
435 {
436         return GE_NONE;
437 }
438
439 /***
440  *** DECODING SMS
441  ***/
442
443 static GSM_Error SMSStatus(unsigned char status, GSM_SMSMessage *SMS)
444 {
445         if (status < 0x03) {
446                 strcpy(SMS->MessageText, _("Delivered"));
447                 switch (status) {
448                 case 0x00:
449                         dprintf("SM received by the SME");
450                         break;
451                 case 0x01:
452                         dprintf("SM forwarded by the SC to the SME but the SC is unable to confirm delivery");
453                         break;
454                 case 0x02:
455                         dprintf("SM replaced by the SC");
456                         break;
457                 }
458                 SMS->Length = strlen(_("Delivered"));
459         } else if (status & 0x40) {
460
461                 strcpy(SMS->MessageText, _("Failed"));
462
463                 /* more detailed reason only for debug */
464
465                 if (status & 0x20) {
466                         dprintf("Temporary error, SC is not making any more transfer attempts\n");
467
468                         switch (status) {
469                         case 0x60:
470                                 dprintf("Congestion");
471                                 break;
472                         case 0x61:
473                                 dprintf("SME busy");
474                                 break;
475                         case 0x62:
476                                 dprintf("No response from SME");
477                                 break;
478                         case 0x63:
479                                 dprintf("Service rejected");
480                                 break;
481                         case 0x64:
482                                 dprintf("Quality of service not aviable");
483                                 break;
484                         case 0x65:
485                                 dprintf("Error in SME");
486                                 break;
487                         default:
488                                 dprintf("Reserved/Specific to SC: %x", status);
489                                 break;
490                         }
491                 } else {
492                         dprintf("Permanent error, SC is not making any more transfer attempts\n");
493                         switch (status) {
494                         case 0x40:
495                                 dprintf("Remote procedure error");
496                                 break;
497                         case 0x41:
498                                 dprintf("Incompatibile destination");
499                                 break;
500                         case 0x42:
501                                 dprintf("Connection rejected by SME");
502                                 break;
503                         case 0x43:
504                                 dprintf("Not obtainable");
505                                 break;
506                         case 0x44:
507                                 dprintf("Quality of service not aviable");
508                                 break;
509                         case 0x45:
510                                 dprintf("No internetworking available");
511                                 break;
512                         case 0x46:
513                                 dprintf("SM Validity Period Expired");
514                                 break;
515                         case 0x47:
516                                 dprintf("SM deleted by originating SME");
517                                 break;
518                         case 0x48:
519                                 dprintf("SM Deleted by SC Administration");
520                                 break;
521                         case 0x49:
522                                 dprintf("SM does not exist");
523                                 break;
524                         default:
525                                 dprintf("Reserved/Specific to SC: %x", status);
526                                 break;
527                         }
528                 }
529                 SMS->Length = strlen(_("Failed"));
530         } else if (status & 0x20) {
531                 strcpy(SMS->MessageText, _("Pending"));
532
533                 /* more detailed reason only for debug */
534                 dprintf("Temporary error, SC still trying to transfer SM\n");
535                 switch (status) {
536                 case 0x20:
537                         dprintf("Congestion");
538                         break;
539                 case 0x21:
540                         dprintf("SME busy");
541                         break;
542                 case 0x22:
543                         dprintf("No response from SME");
544                         break;
545                 case 0x23:
546                         dprintf("Service rejected");
547                         break;
548                 case 0x24:
549                         dprintf("Quality of service not aviable");
550                         break;
551                 case 0x25:
552                         dprintf("Error in SME");
553                         break;
554                 default:
555                         dprintf("Reserved/Specific to SC: %x", status);
556                         break;
557                 }
558                 SMS->Length = strlen(_("Pending"));
559         } else {
560                 strcpy(SMS->MessageText, _("Unknown"));
561
562                 /* more detailed reason only for debug */
563                 dprintf("Reserved/Specific to SC: %x", status);
564                 SMS->Length = strlen(_("Unknown"));
565         }
566         dprintf("\n");
567         return GE_NONE;
568 }
569
570 static GSM_Error DecodeData(char *message, char *output, int length, int size, int udhlen, SMS_DataCodingScheme dcs)
571 {
572         /* Unicode */
573         if ((dcs.Type & 0x08) == 0x08) {
574                 dprintf("Unicode message\n");
575                 length = (length - udhlen)/2;
576                 DecodeUnicode(output, message, length);
577         } else {
578                 /* 8bit SMS */
579                 if ((dcs.Type & 0xf4) == 0xf4) {
580                         dprintf("8bit message\n");
581                         memcpy(output, message, length);
582                 /* 7bit SMS */
583                 } else {
584                         dprintf("Default Alphabet\n");
585                         length = length - (udhlen * 8 + ((7-(udhlen%7))%7)) / 7;
586                         Unpack7BitCharacters((7-udhlen)%7, size, length, message, output);
587                         DecodeAscii(output, output, length);
588                 }
589         }
590         dprintf("%s\n", output);
591         return GE_NONE;
592 }
593
594 /* This function decodes UDH as described in:
595    - GSM 03.40 version 6.1.0 Release 1997, section 9.2.3.24
596    - Smart Messaging Specification, Revision 1.0.0, September 15, 1997
597 */
598 static GSM_Error DecodeUDH(char *message, GSM_SMSMessage *SMS)
599 {
600         unsigned char length, pos, nr;
601
602         SMS->UDH_Length = length = message[0] + 1;
603         pos = 1;
604         nr = 0;
605         while (length > 1) {
606                 unsigned char udh_length;
607
608                 udh_length = message[pos+1];
609                 switch (message[pos]) {
610                 case 0x00: // Concatenated short messages
611                         dprintf("Concatenated messages\n");
612                         SMS->UDH[nr].Type = SMS_ConcatenatedMessages;
613                         SMS->UDH[nr].u.ConcatenatedShortMessage.ReferenceNumber = message[pos + 2];
614                         SMS->UDH[nr].u.ConcatenatedShortMessage.MaximumNumber   = message[pos + 3];
615                         SMS->UDH[nr].u.ConcatenatedShortMessage.CurrentNumber   = message[pos + 4];
616                         break;
617                 case 0x01: // Special SMS Message Indication
618                         switch (message[pos + 2] & 0x03) {
619                         case 0x00:
620                                 dprintf("Voice Message\n");
621                                 SMS->UDH[nr].Type = SMS_VoiceMessage;
622                                 break;
623                         case 0x01:
624                                 dprintf("Fax Message\n");
625                                 SMS->UDH[nr].Type = SMS_FaxMessage;
626                                 break;
627                         case 0x02:
628                                 dprintf("Email Message\n");
629                                 SMS->UDH[nr].Type = SMS_EmailMessage;
630                                 break;
631                         default:
632                                 dprintf("Unknown\n");
633                                 SMS->UDH[nr].Type = SMS_UnknownUDH;
634                                 break;
635                         }
636                         SMS->UDH[nr].u.SpecialSMSMessageIndication.Store = (message[pos + 2] & 0x80) >> 7;
637                         SMS->UDH[nr].u.SpecialSMSMessageIndication.MessageCount = message[pos + 3];
638                         break;
639                 case 0x04: // Application port addression scheme, 8 bit address
640                         break;
641                 case 0x05: // Application port addression scheme, 16 bit address
642                         switch (((0x00ff & message[pos + 2]) << 8) | (0x00ff & message[pos + 3])) {
643                         case 0x1581:
644                                 dprintf("Ringtone\n");
645                                 SMS->UDH[nr].Type = SMS_Ringtone;
646                                 break;
647                         case 0x1582:
648                                 dprintf("Operator Logo\n");
649                                 SMS->UDH[nr].Type = SMS_OpLogo;
650                                 break;
651                         case 0x1583:
652                                 dprintf("Caller Icon\n");
653                                 SMS->UDH[nr].Type = SMS_CallerIDLogo;
654                                 break;
655                         case 0x23f4:
656                                 dprintf("Business Card\n");
657                                 SMS->UDH[nr].Type = SMS_BusinessCard;
658                                 break;
659                         default:
660                                 dprintf("Unknown\n");
661                                 SMS->UDH[nr].Type = SMS_UnknownUDH;
662                                 break;
663                         }
664                         break;
665                 case 0x06: // SMSC Control Parameters
666                         break;
667                 case 0x07: // UDH Source Indicator
668                         break;
669                 default:
670                         break;
671                 }
672                 length -= (udh_length + 2);
673                 pos += (udh_length + 2);
674                 nr++;
675         }
676         SMS->UDH_No = nr;
677
678         return GE_NONE;
679 }
680
681 static GSM_Error DecodeSMSHeader(unsigned char *message, GSM_SMSMessage *SMS)
682 {
683         /* Short Message Type */
684         switch (SMS->Type = message[2]) {
685         case SMS_Deliver:
686                 dprintf("Mobile Terminated message:\n");
687                 break;
688         case SMS_Delivery_Report:
689                 dprintf("Delivery Report:\n");
690                 UnpackDateTime(message + 34 + DataOffset[SMS->Type], &(SMS->SMSCTime));
691                 dprintf("\tDelivery date: %s\n", PrintDateTime(message + 34 + DataOffset[SMS->Type]));
692                 break;
693         case SMS_Submit:
694                 dprintf("Mobile Originated message:\n");
695                 break;
696         default:
697                 dprintf("Not supported message type:\n");
698                 break;
699         }
700
701         /* Short Message location in memory */
702         SMS->Number = message[1];
703         dprintf("\tLocation: %d\n", SMS->Number);
704
705         /* Short Message Center */
706         strcpy(SMS->MessageCenter.Number, GetBCDNumber(message + 3));
707         dprintf("\tSMS center number: %s\n", SMS->MessageCenter.Number);
708         SMS->ReplyViaSameSMSC = false;
709         if (SMS->RemoteNumber.number[0] == 0 && (message[6] & 0x80)) {
710                 SMS->ReplyViaSameSMSC = true;
711         }
712
713         /* Remote number */
714         message[15+DataOffset[SMS->Type]] = ((message[15+DataOffset[SMS->Type]])+1)/2+1;
715         dprintf("\tRemote number (recipient or sender): %s\n", GetBCDNumber(message + 15 + DataOffset[SMS->Type]));
716         strcpy(SMS->RemoteNumber.number, GetBCDNumber(message + 15 + DataOffset[SMS->Type]));
717
718         UnpackDateTime(message + 27 + DataOffset[SMS->Type], &(SMS->Time));
719         dprintf("\tDate: %s\n", PrintDateTime(message + 27 + DataOffset[SMS->Type]));
720
721         /* Message length */
722         SMS->Length = message[14+DataOffset[SMS->Type]];
723
724         /* Data Coding Scheme */
725         if (SMS->Type != SMS_Delivery_Report && SMS->Type != SMS_Picture)
726                 SMS->DCS.Type = message[13 + DataOffset[SMS->Type]];
727         else
728                 SMS->DCS.Type = 0;
729
730         /* User Data Header */
731         if (message[15] & 0x40) { /* UDH header available */
732                 dprintf("UDH found\n");
733                 DecodeUDH(message + 34 + DataOffset[SMS->Type], SMS);
734         } else {                    /* No UDH */
735                 dprintf("No UDH\n");
736                 SMS->UDH_No = 0;
737         }
738
739         return GE_NONE;
740 }
741
742 /* This function decodes SMS as described in:
743    - GSM 03.40 version 6.1.0 Release 1997, section 9
744 */
745 GSM_Error DecodePDUSMS(unsigned char *message, GSM_SMSMessage *SMS, int MessageLength)
746 {
747         int size;
748         GSM_Bitmap bitmap;
749
750         DecodeSMSHeader(message, SMS);
751         switch (SMS->Type) {
752         case SMS_Delivery_Report:
753                 SMSStatus(message[17], SMS);
754                 break;
755         case SMS_Picture:
756                 dprintf("Picture!!!\n");
757                 GSM_ReadSMSBitmap(SMS_Picture, message + 41, NULL, &bitmap);
758                 GSM_PrintBitmap(&bitmap);
759                 size = MessageLength - 45 - bitmap.size;
760                 SMS->Length = message[45 + bitmap.size];
761                 printf("%d %d %d\n", SMS->Length, bitmap.size, size);
762                 DecodeData(message + 46 + bitmap.size,
763                            (unsigned char *)&(SMS->MessageText),
764                            SMS->Length, size, 0, SMS->DCS);
765                 SMS->MessageText[SMS->Length] = 0;
766                 break;
767         default:
768                 size = MessageLength -
769                            34 -                    /* Header Length */
770                            DataOffset[SMS->Type] - /* offset */
771                            SMS->UDH_Length -       /* UDH Length */
772                            5;                      /* checksum */
773                 DecodeData(message + 34 + DataOffset[SMS->Type] + SMS->UDH_Length,
774                            (unsigned char *)&(SMS->MessageText),
775                            SMS->Length, size, SMS->UDH_Length, SMS->DCS);
776                 /* Just in case */
777                 SMS->MessageText[SMS->Length] = 0;
778                 break;
779         }
780
781         return GE_NONE;
782 }
783
784 /* This function does simple SMS decoding - no PDU coding */
785 GSM_Error DecodeTextSMS(unsigned char *message, GSM_SMSMessage *SMS)
786 {
787         return GE_NONE;
788 }