7 A Linux/Unix toolset and driver for Nokia mobile phones.
9 Copyright (C) 2001 Pawe³ Kot <pkot@linuxnews.pl>
11 Released under the terms of the GNU GPL, see file COPYING for more details.
13 Library for parsing and creating Short Messages (SMS).
16 Revision 1.1.1.2 2001/11/27 22:01:13 short
17 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 22:58 CET 2001
19 Revision 1.14 2001/11/27 12:19:00 pkot
20 Cleanup, indentation, ANSI complaint preprocesor symbols (Jan Kratochvil, me)
22 Revision 1.13 2001/11/23 22:07:44 machek
23 Fix SMS receiving to work, again. Unfortunately, it is not possible to
24 reuse much of gsm-sms.c...
26 Revision 1.12 2001/11/22 17:56:53 pkot
27 smslib update. sms sending
29 Revision 1.11 2001/11/20 16:22:22 pkot
30 First attempt to read Picture Messages. They should appear when you enable DEBUG. Nokia seems to break own standards. :/ (Markus Plail)
32 Revision 1.10 2001/11/19 13:09:40 pkot
33 Begin work on sms sending
35 Revision 1.9 2001/11/18 00:54:32 pkot
36 Bugfixes. I18n of the user responses. UDH support in libsms. Business Card UDH Type
38 Revision 1.8 2001/11/17 20:19:29 pkot
39 smslib cleanups, fixes and debugging
41 Revision 1.7 2001/11/15 12:15:04 pkot
42 smslib updates. begin work on sms in 6100 series
44 Revision 1.6 2001/11/14 18:21:19 pkot
45 Fixing some problems with UDH and Unicode, but still doesn't work yet :-(
47 Revision 1.5 2001/11/14 14:26:18 pkot
48 Changed offset of DCS field to the right value in 6210/7110
50 Revision 1.4 2001/11/14 11:26:18 pkot
51 Getting SMS in 6210/7110 does finally work in some cases :)
53 Revision 1.3 2001/11/13 16:12:20 pkot
54 Preparing libsms to get to work. 6210/7110 SMS and SMS Folder updates
56 Revision 1.2 2001/11/09 14:25:04 pkot
59 Revision 1.1 2001/11/08 16:23:21 pkot
60 New version of libsms. Not functional yet, but it reasonably stable API.
62 Revision 1.1 2001/07/09 23:06:26 pkot
63 Moved sms.* files from my hard disk to CVS
70 #include "gsm-common.h"
71 #include "gsm-encoding.h"
72 #include "gsm-bitmaps.h"
79 /* Number of header specific octets in SMS header (before data) */
80 static unsigned short DataOffset[] = {
82 3, /* SMS Deliver Report */
84 3, /* SMS Submit Report */
86 3 /* SMS Status Report */
89 /* User data headers */
90 static struct udh_data headers[] = {
92 { 0x05, "\x00\x03\x01\x00\x00" }, /* Concatenated messages */
93 { 0x06, "\x05\x04\x15\x82\x00\x00" }, /* Operator logos */
94 { 0x06, "\x05\x04\x15\x83\x00\x00" }, /* Caller logos */
95 { 0x06, "\x05\x04\x15\x81\x00\x00" }, /* Ringtones */
96 { 0x04, "\x03\x01\x00\x00" }, /* Voice Messages */
97 { 0x04, "\x03\x01\x01\x00" }, /* Fax Messages */
98 { 0x04, "\x03\x01\x02\x00" }, /* Email Messages */
99 { 0x06, "\x05\x04\x23\xf4\x00\x00" }, /* Business Card */
108 /* This function implements packing of numbers (SMS Center number and
109 destination number) for SMS sending function. */
110 static int SemiOctetPack(char *Number, unsigned char *Output, SMS_NumberType type)
112 unsigned char *IN = Number; /* Pointer to the input number */
113 unsigned char *OUT = Output; /* Pointer to the output */
114 int count = 0; /* This variable is used to notify us about count of already
117 /* The first byte in the Semi-octet representation of the address field is
118 the Type-of-Address. This field is described in the official GSM
119 specification 03.40 version 6.1.0, section 9.1.2.5, page 33. We support
120 only international and unknown number. */
123 if (type == SMS_International) IN++; /* Skip '+' */
124 if ((type == SMS_Unknown) && (*IN == '+')) IN++; /* Optional '+' in Unknown number type */
126 /* The next field is the number. It is in semi-octet representation - see
127 GSM scpecification 03.40 version 6.1.0, section 9.1.2.3, page 31. */
130 *OUT = *OUT | ((*IN - '0') << 4);
138 /* We should also fill in the most significant bits of the last byte with
139 0x0f (1111 binary) if the number is represented with odd number of
146 return (2 * (OUT - Output - 1) - (count % 2));
149 char *GetBCDNumber(u8 *Number)
151 static char Buffer[20] = "";
152 int length = Number[0]; /* This is the length of BCD coded number */
155 memset(Buffer, 0, 20);
157 case SMS_Alphanumeric:
158 Unpack7BitCharacters(0, length, length, Number+2, Buffer);
161 case SMS_International:
162 sprintf(Buffer, "+");
167 case SMS_Abbreviated:
169 for (count = 0; count < length - 1; count++) {
170 Digit = Number[count+2] & 0x0f;
171 if (Digit < 10) sprintf(Buffer, "%s%d", Buffer, Digit);
172 Digit = Number[count+2] >> 4;
173 if (Digit < 10) sprintf(Buffer, "%s%d", Buffer, Digit);
180 static char *PrintDateTime(u8 *Number)
182 static char Buffer[23] = "";
184 memset(Buffer, 0, 23);
185 if (Number[0] < 70) sprintf(Buffer, "20");
186 else sprintf(Buffer, "19");
187 sprintf(Buffer, "%s%d%d-", Buffer, Number[0] & 0x0f, Number[0] >> 4);
188 sprintf(Buffer, "%s%d%d-", Buffer, Number[1] & 0x0f, Number[1] >> 4);
189 sprintf(Buffer, "%s%d%d ", Buffer, Number[2] & 0x0f, Number[2] >> 4);
190 sprintf(Buffer, "%s%d%d:", Buffer, Number[3] & 0x0f, Number[3] >> 4);
191 sprintf(Buffer, "%s%d%d:", Buffer, Number[4] & 0x0f, Number[4] >> 4);
192 sprintf(Buffer, "%s%d%d", Buffer, Number[5] & 0x0f, Number[5] >> 4);
193 if (Number[6] & 0x08)
194 sprintf(Buffer, "%s-", Buffer);
196 sprintf(Buffer, "%s+", Buffer);
197 sprintf(Buffer, "%s%02d00", Buffer, (10 * (Number[6] & 0x07) + (Number[6] >> 4)) / 4);
202 SMS_DateTime *UnpackDateTime(u8 *Number, SMS_DateTime *dt)
204 dt->Year = 10 * (Number[0] & 0x0f) + (Number[0] >> 4);
205 if (dt->Year < 70) dt->Year += 2000;
206 else dt->Year += 1900;
207 dt->Month = 10 * (Number[1] & 0x0f) + (Number[1] >> 4);
208 dt->Day = 10 * (Number[2] & 0x0f) + (Number[2] >> 4);
209 dt->Hour = 10 * (Number[3] & 0x0f) + (Number[3] >> 4);
210 dt->Minute = 10 * (Number[4] & 0x0f) + (Number[4] >> 4);
211 dt->Second = 10 * (Number[5] & 0x0f) + (Number[5] >> 4);
212 dt->Timezone = (10 * (Number[6] & 0x07) + (Number[6] >> 4)) / 4;
213 if (Number[6] & 0x08) dt->Timezone = -dt->Timezone;
222 static GSM_Error EncodeData(GSM_SMSMessage *SMS, char *dcs, char *message)
225 unsigned short length = strlen(SMS->MessageText);
227 switch (SMS->DCS.Type) {
228 case SMS_GeneralDataCoding:
229 switch (SMS->DCS.u.General.Class) {
230 case 1: dcs[0] |= 0xf0; break;
231 case 2: dcs[0] |= 0xf1; break;
232 case 3: dcs[0] |= 0xf2; break;
233 case 4: dcs[0] |= 0xf3; break;
236 if (SMS->DCS.u.General.Compressed) {
237 /* Compression not supported yet */
238 /* dcs[0] |= 0x20; */
240 al = SMS->DCS.u.General.Alphabet;
242 case SMS_MessageWaiting:
243 al = SMS->DCS.u.MessageWaiting.Alphabet;
244 if (SMS->DCS.u.MessageWaiting.Discard) dcs[0] |= 0xc0;
245 else if (SMS->DCS.u.MessageWaiting.Alphabet == SMS_UCS2) dcs[0] |= 0xe0;
248 if (SMS->DCS.u.MessageWaiting.Active) dcs[0] |= 0x08;
249 dcs[0] |= (SMS->DCS.u.MessageWaiting.Type & 0x03);
253 return GE_SMSWRONGFORMAT;
256 case SMS_DefaultAlphabet:
257 Pack7BitCharacters((7 - (SMS->UDH_Length % 7)) % 7, SMS->MessageText, message);
258 SMS->Length = 8 * SMS->UDH_Length + (7 - (SMS->UDH_Length % 7)) % 7 + length;
262 memcpy(message, SMS->MessageText + 1, SMS->MessageText[0]);
263 SMS->Length = SMS->UDH_Length + SMS->MessageText[0];
267 EncodeUnicode(message, SMS->MessageText, length);
268 SMS->Length = length;
271 return GE_SMSWRONGFORMAT;
276 /* This function encodes the UserDataHeader as described in:
277 - GSM 03.40 version 6.1.0 Release 1997, section 9.2.3.24
278 - Smart Messaging Specification, Revision 1.0.0, September 15, 1997
280 static GSM_Error EncodeUDH(SMS_UDHInfo UDHi, char *UDH)
288 case SMS_VoiceMessage:
290 case SMS_EmailMessage:
291 UDH[pos+4] = UDHi.u.SpecialSMSMessageIndication.MessageCount;
292 if (UDHi.u.SpecialSMSMessageIndication.Store) UDH[pos+3] |= 0x80;
293 case SMS_ConcatenatedMessages:
295 case SMS_CallerIDLogo:
297 UDH[0] += headers[UDHi.Type].length;
298 memcpy(UDH+pos+1, headers[UDHi.Type].header, headers[UDHi.Type].length);
301 dprintf("Not supported User Data Header type\n");
307 static GSM_Error EncodeSMSSubmitHeader(GSM_SMSMessage *SMS, char *frame)
309 GSM_Error error = GE_NONE;
311 /* Standard Header: */
312 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);
315 if (SMS->ReplyViaSameSMSC) frame[0] |= 0x80;
317 /* User Data Header Indicator */
318 if (SMS->UDH_No) frame[0] |= 0x40;
320 /* Status (Delivery) Report Request */
321 if (SMS->ReportStatus) frame[0] |= 0x20;
323 /* Validity Period Format: mask - 0x00, 0x10, 0x08, 0x18 */
324 frame[0] |= ((SMS->Validity.VPF & 0x03) << 3);
326 /* Reject Duplicates */
327 if (SMS->RejectDuplicates) frame[0] |= 0x04;
329 /* Message Type is already set */
331 /* Message Reference */
332 /* Can we set this? */
334 /* Protocol Identifier */
335 /* FIXME: allow to change this in better way.
336 currently only 0x5f == `Return Call Message' is used */
337 if (SMS->PID) frame[3] = SMS->PID;
339 /* Data Coding Scheme */
340 switch (SMS->DCS.Type) {
341 case SMS_GeneralDataCoding:
342 if (SMS->DCS.u.General.Compressed) frame[4] |= 0x20;
343 if (SMS->DCS.u.General.Class) frame[4] |= (0x10 | (SMS->DCS.u.General.Class - 1));
344 frame[4] |= ((SMS->DCS.u.General.Alphabet & 0x03) << 2);
346 case SMS_MessageWaiting:
347 if (SMS->DCS.u.MessageWaiting.Discard) frame[4] |= 0xc0;
348 else if (SMS->DCS.u.MessageWaiting.Alphabet == SMS_UCS2) frame[4] |= 0xe0;
349 else frame[4] |= 0xd0;
350 if (SMS->DCS.u.MessageWaiting.Active) frame[4] |= 0x80;
351 frame[4] |= (SMS->DCS.u.MessageWaiting.Type & 0x03);
354 dprintf("Wrong Data Coding Scheme (DCS) format\n");
355 return GE_SMSWRONGFORMAT;
358 /* Destination Address */
359 frame[5] = SemiOctetPack(SMS->RemoteNumber.number, frame + 6, SMS->RemoteNumber.type);
361 /* Validity Period */
362 switch (SMS->Validity.VPF) {
363 case SMS_EnhancedFormat:
365 case SMS_RelativeFormat:
367 case SMS_AbsoluteFormat:
376 static GSM_Error EncodeSMSDeliverHeader()
381 static GSM_Error EncodeSMSHeader(GSM_SMSMessage *SMS, char *frame)
382 /* We can create either SMS DELIVER (for saving in Inbox) or SMS SUBMIT
383 (for sending or saving in Outbox) message */
386 frame[12] |= (SMS->Type >> 1);
388 case SMS_Submit: /* we send SMS or save it in Outbox */
389 return EncodeSMSSubmitHeader(SMS, frame);
390 case SMS_Deliver: /* we save SMS in Inbox */
391 return EncodeSMSDeliverHeader(SMS, frame);
392 default: /* we don't create other formats of SMS */
393 return GE_SMSWRONGFORMAT;
397 /* This function encodes SMS as described in:
398 - GSM 03.40 version 6.1.0 Release 1997, section 9
400 int EncodePDUSMS(GSM_SMSMessage *SMS, char *message)
402 GSM_Error error = GE_NONE;
405 dprintf("Sending SMS to %s via message center %s\n", SMS->RemoteNumber.number, SMS->MessageCenter.Number);
408 dprintf("%d %s\n", SMS->MessageCenter.Type, SMS->MessageCenter.Number);
409 message[0] = SemiOctetPack(SMS->MessageCenter.Number, message + 1, SMS->MessageCenter.Type);
410 if (message[0] % 2) message[0]++;
411 message[0] = message[0] / 2 + 1;
414 error = EncodeSMSHeader(SMS, message + 12);
415 if (error != GE_NONE) return error;
417 /* User Data Header - if present */
418 // for (i = 0; i < SMS->UDH_No; i++) {
419 // error = EncodeUDH(SMS->UDH[i], message + 24);
420 // if (error != GE_NONE) return error;
425 EncodeData(SMS, message + 14, message + 36 + SMS->UDH_Length);
426 message[16] = SMS->Length;
427 return SMS->Length + 35;
430 /* This function does simple SMS encoding - no PDU coding */
431 GSM_Error EncodeTextSMS()
440 static GSM_Error SMSStatus(unsigned char status, GSM_SMSMessage *SMS)
443 strcpy(SMS->MessageText, _("Delivered"));
446 dprintf("SM received by the SME");
449 dprintf("SM forwarded by the SC to the SME but the SC is unable to confirm delivery");
452 dprintf("SM replaced by the SC");
455 SMS->Length = strlen(_("Delivered"));
456 } else if (status & 0x40) {
458 strcpy(SMS->MessageText, _("Failed"));
460 /* more detailed reason only for debug */
463 dprintf("Temporary error, SC is not making any more transfer attempts\n");
467 dprintf("Congestion");
473 dprintf("No response from SME");
476 dprintf("Service rejected");
479 dprintf("Quality of service not aviable");
482 dprintf("Error in SME");
485 dprintf("Reserved/Specific to SC: %x", status);
489 dprintf("Permanent error, SC is not making any more transfer attempts\n");
492 dprintf("Remote procedure error");
495 dprintf("Incompatibile destination");
498 dprintf("Connection rejected by SME");
501 dprintf("Not obtainable");
504 dprintf("Quality of service not aviable");
507 dprintf("No internetworking available");
510 dprintf("SM Validity Period Expired");
513 dprintf("SM deleted by originating SME");
516 dprintf("SM Deleted by SC Administration");
519 dprintf("SM does not exist");
522 dprintf("Reserved/Specific to SC: %x", status);
526 SMS->Length = strlen(_("Failed"));
527 } else if (status & 0x20) {
528 strcpy(SMS->MessageText, _("Pending"));
530 /* more detailed reason only for debug */
531 dprintf("Temporary error, SC still trying to transfer SM\n");
534 dprintf("Congestion");
540 dprintf("No response from SME");
543 dprintf("Service rejected");
546 dprintf("Quality of service not aviable");
549 dprintf("Error in SME");
552 dprintf("Reserved/Specific to SC: %x", status);
555 SMS->Length = strlen(_("Pending"));
557 strcpy(SMS->MessageText, _("Unknown"));
559 /* more detailed reason only for debug */
560 dprintf("Reserved/Specific to SC: %x", status);
561 SMS->Length = strlen(_("Unknown"));
567 static GSM_Error DecodeData(char *message, char *output, int length, int size, int udhlen, SMS_DataCodingScheme dcs)
570 if ((dcs.Type & 0x08) == 0x08) {
571 dprintf("Unicode message\n");
572 length = (length - udhlen)/2;
573 DecodeUnicode(output, message, length);
576 if ((dcs.Type & 0xf4) == 0xf4) {
577 dprintf("8bit message\n");
578 memcpy(output, message, length);
581 dprintf("Default Alphabet\n");
582 length = length - (udhlen * 8 + ((7-(udhlen%7))%7)) / 7;
583 Unpack7BitCharacters((7-udhlen)%7, size, length, message, output);
584 DecodeAscii(output, output, length);
587 dprintf("%s\n", output);
591 /* This function decodes UDH as described in:
592 - GSM 03.40 version 6.1.0 Release 1997, section 9.2.3.24
593 - Smart Messaging Specification, Revision 1.0.0, September 15, 1997
595 static GSM_Error DecodeUDH(char *message, GSM_SMSMessage *SMS)
597 unsigned char length, pos, nr;
599 SMS->UDH_Length = length = message[0] + 1;
603 unsigned char udh_length;
605 udh_length = message[pos+1];
606 switch (message[pos]) {
607 case 0x00: // Concatenated short messages
608 dprintf("Concatenated messages\n");
609 SMS->UDH[nr].Type = SMS_ConcatenatedMessages;
610 SMS->UDH[nr].u.ConcatenatedShortMessage.ReferenceNumber = message[pos + 2];
611 SMS->UDH[nr].u.ConcatenatedShortMessage.MaximumNumber = message[pos + 3];
612 SMS->UDH[nr].u.ConcatenatedShortMessage.CurrentNumber = message[pos + 4];
614 case 0x01: // Special SMS Message Indication
615 switch (message[pos + 2] & 0x03) {
617 dprintf("Voice Message\n");
618 SMS->UDH[nr].Type = SMS_VoiceMessage;
621 dprintf("Fax Message\n");
622 SMS->UDH[nr].Type = SMS_FaxMessage;
625 dprintf("Email Message\n");
626 SMS->UDH[nr].Type = SMS_EmailMessage;
629 dprintf("Unknown\n");
630 SMS->UDH[nr].Type = SMS_UnknownUDH;
633 SMS->UDH[nr].u.SpecialSMSMessageIndication.Store = (message[pos + 2] & 0x80) >> 7;
634 SMS->UDH[nr].u.SpecialSMSMessageIndication.MessageCount = message[pos + 3];
636 case 0x04: // Application port addression scheme, 8 bit address
638 case 0x05: // Application port addression scheme, 16 bit address
639 switch (((0x00ff & message[pos + 2]) << 8) | (0x00ff & message[pos + 3])) {
641 dprintf("Ringtone\n");
642 SMS->UDH[nr].Type = SMS_Ringtone;
645 dprintf("Operator Logo\n");
646 SMS->UDH[nr].Type = SMS_OpLogo;
649 dprintf("Caller Icon\n");
650 SMS->UDH[nr].Type = SMS_CallerIDLogo;
653 dprintf("Business Card\n");
654 SMS->UDH[nr].Type = SMS_BusinessCard;
657 dprintf("Unknown\n");
658 SMS->UDH[nr].Type = SMS_UnknownUDH;
662 case 0x06: // SMSC Control Parameters
664 case 0x07: // UDH Source Indicator
669 length -= (udh_length + 2);
670 pos += (udh_length + 2);
678 static GSM_Error DecodeSMSHeader(unsigned char *message, GSM_SMSMessage *SMS)
680 /* Short Message Type */
681 switch (SMS->Type = message[2]) {
683 dprintf("Mobile Terminated message:\n");
685 case SMS_Delivery_Report:
686 dprintf("Delivery Report:\n");
687 UnpackDateTime(message + 34 + DataOffset[SMS->Type], &(SMS->SMSCTime));
688 dprintf("\tDelivery date: %s\n", PrintDateTime(message + 34 + DataOffset[SMS->Type]));
691 dprintf("Mobile Originated message:\n");
694 dprintf("Not supported message type:\n");
698 /* Short Message location in memory */
699 SMS->Number = message[1];
700 dprintf("\tLocation: %d\n", SMS->Number);
702 /* Short Message Center */
703 strcpy(SMS->MessageCenter.Number, GetBCDNumber(message + 3));
704 dprintf("\tSMS center number: %s\n", SMS->MessageCenter.Number);
705 SMS->ReplyViaSameSMSC = false;
706 if (SMS->RemoteNumber.number[0] == 0 && (message[6] & 0x80)) {
707 SMS->ReplyViaSameSMSC = true;
711 message[15+DataOffset[SMS->Type]] = ((message[15+DataOffset[SMS->Type]])+1)/2+1;
712 dprintf("\tRemote number (recipient or sender): %s\n", GetBCDNumber(message + 15 + DataOffset[SMS->Type]));
713 strcpy(SMS->RemoteNumber.number, GetBCDNumber(message + 15 + DataOffset[SMS->Type]));
715 UnpackDateTime(message + 27 + DataOffset[SMS->Type], &(SMS->Time));
716 dprintf("\tDate: %s\n", PrintDateTime(message + 27 + DataOffset[SMS->Type]));
719 SMS->Length = message[14+DataOffset[SMS->Type]];
721 /* Data Coding Scheme */
722 if (SMS->Type != SMS_Delivery_Report && SMS->Type != SMS_Picture)
723 SMS->DCS.Type = message[13 + DataOffset[SMS->Type]];
727 /* User Data Header */
728 if (message[15] & 0x40) { /* UDH header available */
729 dprintf("UDH found\n");
730 DecodeUDH(message + 34 + DataOffset[SMS->Type], SMS);
731 } else { /* No UDH */
739 /* This function decodes SMS as described in:
740 - GSM 03.40 version 6.1.0 Release 1997, section 9
742 GSM_Error DecodePDUSMS(unsigned char *message, GSM_SMSMessage *SMS, int MessageLength)
747 DecodeSMSHeader(message, SMS);
749 case SMS_Delivery_Report:
750 SMSStatus(message[17], SMS);
753 dprintf("Picture!!!\n");
754 GSM_ReadSMSBitmap(SMS_Picture, message + 41, NULL, &bitmap);
755 GSM_PrintBitmap(&bitmap);
756 size = MessageLength - 45 - bitmap.size;
757 SMS->Length = message[45 + bitmap.size];
758 printf("%d %d %d\n", SMS->Length, bitmap.size, size);
759 DecodeData(message + 46 + bitmap.size,
760 (unsigned char *)&(SMS->MessageText),
761 SMS->Length, size, 0, SMS->DCS);
762 SMS->MessageText[SMS->Length] = 0;
765 size = MessageLength -
766 34 - /* Header Length */
767 DataOffset[SMS->Type] - /* offset */
768 SMS->UDH_Length - /* UDH Length */
770 DecodeData(message + 34 + DataOffset[SMS->Type] + SMS->UDH_Length,
771 (unsigned char *)&(SMS->MessageText),
772 SMS->Length, size, SMS->UDH_Length, SMS->DCS);
774 SMS->MessageText[SMS->Length] = 0;
781 /* This function does simple SMS decoding - no PDU coding */
782 GSM_Error DecodeTextSMS(unsigned char *message, GSM_SMSMessage *SMS)