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