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.1.8.1 2001/11/27 23:06:09 short
17 Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
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
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
25 Revision 1.14 2001/11/27 12:19:00 pkot
26 Cleanup, indentation, ANSI complaint preprocesor symbols (Jan Kratochvil, me)
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...
32 Revision 1.12 2001/11/22 17:56:53 pkot
33 smslib update. sms sending
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)
38 Revision 1.10 2001/11/19 13:09:40 pkot
39 Begin work on sms sending
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
44 Revision 1.8 2001/11/17 20:19:29 pkot
45 smslib cleanups, fixes and debugging
47 Revision 1.7 2001/11/15 12:15:04 pkot
48 smslib updates. begin work on sms in 6100 series
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 :-(
53 Revision 1.5 2001/11/14 14:26:18 pkot
54 Changed offset of DCS field to the right value in 6210/7110
56 Revision 1.4 2001/11/14 11:26:18 pkot
57 Getting SMS in 6210/7110 does finally work in some cases :)
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
62 Revision 1.2 2001/11/09 14:25:04 pkot
65 Revision 1.1 2001/11/08 16:23:21 pkot
66 New version of libsms. Not functional yet, but it reasonably stable API.
68 Revision 1.1 2001/07/09 23:06:26 pkot
69 Moved sms.* files from my hard disk to CVS
76 #include "gsm-common.h"
77 #include "gsm-encoding.h"
78 #include "gsm-bitmaps.h"
85 /* Number of header specific octets in SMS header (before data) */
86 static unsigned short DataOffset[] = {
88 3, /* SMS Deliver Report */
90 3, /* SMS Submit Report */
92 3 /* SMS Status Report */
95 /* User data headers */
96 static struct udh_data headers[] = {
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 */
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)
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
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. */
129 if (type == SMS_International) IN++; /* Skip '+' */
130 if ((type == SMS_Unknown) && (*IN == '+')) IN++; /* Optional '+' in Unknown number type */
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. */
136 *OUT = *OUT | ((*IN - '0') << 4);
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
152 return (2 * (OUT - Output - 1) - (count % 2));
155 char *GetBCDNumber(u8 *Number)
157 static char Buffer[20] = "";
158 int length = Number[0]; /* This is the length of BCD coded number */
161 memset(Buffer, 0, 20);
163 case SMS_Alphanumeric:
164 Unpack7BitCharacters(0, length, length, Number+2, Buffer);
167 case SMS_International:
168 sprintf(Buffer, "+");
173 case SMS_Abbreviated:
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);
186 static char *PrintDateTime(u8 *Number)
188 static char Buffer[23] = "";
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);
202 sprintf(Buffer, "%s+", Buffer);
203 sprintf(Buffer, "%s%02d00", Buffer, (10 * (Number[6] & 0x07) + (Number[6] >> 4)) / 4);
208 SMS_DateTime *UnpackDateTime(u8 *Number, SMS_DateTime *dt)
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;
228 static GSM_Error EncodeData(GSM_SMSMessage *SMS, char *dcs, char *message)
231 unsigned short length = strlen(SMS->MessageText);
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;
242 if (SMS->DCS.u.General.Compressed) {
243 /* Compression not supported yet */
244 /* dcs[0] |= 0x20; */
246 al = SMS->DCS.u.General.Alphabet;
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;
254 if (SMS->DCS.u.MessageWaiting.Active) dcs[0] |= 0x08;
255 dcs[0] |= (SMS->DCS.u.MessageWaiting.Type & 0x03);
259 return GE_SMSWRONGFORMAT;
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;
268 memcpy(message, SMS->MessageText + 1, SMS->MessageText[0]);
269 SMS->Length = SMS->UDH_Length + SMS->MessageText[0];
273 EncodeUnicode(message, SMS->MessageText, length);
274 SMS->Length = length;
277 return GE_SMSWRONGFORMAT;
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
286 static GSM_Error EncodeUDH(SMS_UDHInfo UDHi, char *UDH)
294 case SMS_VoiceMessage:
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:
301 case SMS_CallerIDLogo:
303 UDH[0] += headers[UDHi.Type].length;
304 memcpy(UDH+pos+1, headers[UDHi.Type].header, headers[UDHi.Type].length);
307 dprintf("Not supported User Data Header type\n");
313 static GSM_Error EncodeSMSSubmitHeader(GSM_SMSMessage *SMS, char *frame)
315 GSM_Error error = GE_NONE;
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);
321 if (SMS->ReplyViaSameSMSC) frame[0] |= 0x80;
323 /* User Data Header Indicator */
324 if (SMS->UDH_No) frame[0] |= 0x40;
326 /* Status (Delivery) Report Request */
327 if (SMS->ReportStatus) frame[0] |= 0x20;
329 /* Validity Period Format: mask - 0x00, 0x10, 0x08, 0x18 */
330 frame[0] |= ((SMS->Validity.VPF & 0x03) << 3);
332 /* Reject Duplicates */
333 if (SMS->RejectDuplicates) frame[0] |= 0x04;
335 /* Message Type is already set */
337 /* Message Reference */
338 /* Can we set this? */
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;
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);
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);
360 dprintf("Wrong Data Coding Scheme (DCS) format\n");
361 return GE_SMSWRONGFORMAT;
364 /* Destination Address */
365 frame[5] = SemiOctetPack(SMS->RemoteNumber.number, frame + 6, SMS->RemoteNumber.type);
367 /* Validity Period */
368 switch (SMS->Validity.VPF) {
369 case SMS_EnhancedFormat:
371 case SMS_RelativeFormat:
373 case SMS_AbsoluteFormat:
382 static GSM_Error EncodeSMSDeliverHeader()
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 */
392 frame[12] |= (SMS->Type >> 1);
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;
403 /* This function encodes SMS as described in:
404 - GSM 03.40 version 6.1.0 Release 1997, section 9
406 int EncodePDUSMS(GSM_SMSMessage *SMS, char *message)
408 GSM_Error error = GE_NONE;
411 dprintf("Sending SMS to %s via message center %s\n", SMS->RemoteNumber.number, SMS->MessageCenter.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;
420 error = EncodeSMSHeader(SMS, message + 12);
421 if (error != GE_NONE) return error;
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;
431 EncodeData(SMS, message + 14, message + 36 + SMS->UDH_Length);
432 message[16] = SMS->Length;
433 return SMS->Length + 35;
436 /* This function does simple SMS encoding - no PDU coding */
437 GSM_Error EncodeTextSMS()
446 static GSM_Error SMSStatus(unsigned char status, GSM_SMSMessage *SMS)
449 strcpy(SMS->MessageText, _("Delivered"));
452 dprintf("SM received by the SME");
455 dprintf("SM forwarded by the SC to the SME but the SC is unable to confirm delivery");
458 dprintf("SM replaced by the SC");
461 SMS->Length = strlen(_("Delivered"));
462 } else if (status & 0x40) {
464 strcpy(SMS->MessageText, _("Failed"));
466 /* more detailed reason only for debug */
469 dprintf("Temporary error, SC is not making any more transfer attempts\n");
473 dprintf("Congestion");
479 dprintf("No response from SME");
482 dprintf("Service rejected");
485 dprintf("Quality of service not aviable");
488 dprintf("Error in SME");
491 dprintf("Reserved/Specific to SC: %x", status);
495 dprintf("Permanent error, SC is not making any more transfer attempts\n");
498 dprintf("Remote procedure error");
501 dprintf("Incompatibile destination");
504 dprintf("Connection rejected by SME");
507 dprintf("Not obtainable");
510 dprintf("Quality of service not aviable");
513 dprintf("No internetworking available");
516 dprintf("SM Validity Period Expired");
519 dprintf("SM deleted by originating SME");
522 dprintf("SM Deleted by SC Administration");
525 dprintf("SM does not exist");
528 dprintf("Reserved/Specific to SC: %x", status);
532 SMS->Length = strlen(_("Failed"));
533 } else if (status & 0x20) {
534 strcpy(SMS->MessageText, _("Pending"));
536 /* more detailed reason only for debug */
537 dprintf("Temporary error, SC still trying to transfer SM\n");
540 dprintf("Congestion");
546 dprintf("No response from SME");
549 dprintf("Service rejected");
552 dprintf("Quality of service not aviable");
555 dprintf("Error in SME");
558 dprintf("Reserved/Specific to SC: %x", status);
561 SMS->Length = strlen(_("Pending"));
563 strcpy(SMS->MessageText, _("Unknown"));
565 /* more detailed reason only for debug */
566 dprintf("Reserved/Specific to SC: %x", status);
567 SMS->Length = strlen(_("Unknown"));
573 static GSM_Error DecodeData(char *message, char *output, int length, int size, int udhlen, SMS_DataCodingScheme dcs)
576 if ((dcs.Type & 0x08) == 0x08) {
577 dprintf("Unicode message\n");
578 length = (length - udhlen)/2;
579 DecodeUnicode(output, message, length);
582 if ((dcs.Type & 0xf4) == 0xf4) {
583 dprintf("8bit message\n");
584 memcpy(output, message, length);
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);
593 dprintf("%s\n", output);
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
601 static GSM_Error DecodeUDH(char *message, GSM_SMSMessage *SMS)
603 unsigned char length, pos, nr;
605 SMS->UDH_Length = length = message[0] + 1;
609 unsigned char udh_length;
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];
620 case 0x01: // Special SMS Message Indication
621 switch (message[pos + 2] & 0x03) {
623 dprintf("Voice Message\n");
624 SMS->UDH[nr].Type = SMS_VoiceMessage;
627 dprintf("Fax Message\n");
628 SMS->UDH[nr].Type = SMS_FaxMessage;
631 dprintf("Email Message\n");
632 SMS->UDH[nr].Type = SMS_EmailMessage;
635 dprintf("Unknown\n");
636 SMS->UDH[nr].Type = SMS_UnknownUDH;
639 SMS->UDH[nr].u.SpecialSMSMessageIndication.Store = (message[pos + 2] & 0x80) >> 7;
640 SMS->UDH[nr].u.SpecialSMSMessageIndication.MessageCount = message[pos + 3];
642 case 0x04: // Application port addression scheme, 8 bit address
644 case 0x05: // Application port addression scheme, 16 bit address
645 switch (((0x00ff & message[pos + 2]) << 8) | (0x00ff & message[pos + 3])) {
647 dprintf("Ringtone\n");
648 SMS->UDH[nr].Type = SMS_Ringtone;
651 dprintf("Operator Logo\n");
652 SMS->UDH[nr].Type = SMS_OpLogo;
655 dprintf("Caller Icon\n");
656 SMS->UDH[nr].Type = SMS_CallerIDLogo;
659 dprintf("Business Card\n");
660 SMS->UDH[nr].Type = SMS_BusinessCard;
663 dprintf("Unknown\n");
664 SMS->UDH[nr].Type = SMS_UnknownUDH;
668 case 0x06: // SMSC Control Parameters
670 case 0x07: // UDH Source Indicator
675 length -= (udh_length + 2);
676 pos += (udh_length + 2);
684 static GSM_Error DecodeSMSHeader(unsigned char *message, GSM_SMSMessage *SMS)
686 /* Short Message Type */
687 switch (SMS->Type = message[2]) {
689 dprintf("Mobile Terminated message:\n");
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]));
697 dprintf("Mobile Originated message:\n");
700 dprintf("Not supported message type:\n");
704 /* Short Message location in memory */
705 SMS->Number = message[1];
706 dprintf("\tLocation: %d\n", SMS->Number);
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;
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]));
721 UnpackDateTime(message + 27 + DataOffset[SMS->Type], &(SMS->Time));
722 dprintf("\tDate: %s\n", PrintDateTime(message + 27 + DataOffset[SMS->Type]));
725 SMS->Length = message[14+DataOffset[SMS->Type]];
727 /* Data Coding Scheme */
728 if (SMS->Type != SMS_Delivery_Report && SMS->Type != SMS_Picture)
729 SMS->DCS.Type = message[13 + DataOffset[SMS->Type]];
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 */
745 /* This function decodes SMS as described in:
746 - GSM 03.40 version 6.1.0 Release 1997, section 9
748 GSM_Error DecodePDUSMS(unsigned char *message, GSM_SMSMessage *SMS, int MessageLength)
753 DecodeSMSHeader(message, SMS);
755 case SMS_Delivery_Report:
756 SMSStatus(message[17], SMS);
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;
771 size = MessageLength -
772 34 - /* Header Length */
773 DataOffset[SMS->Type] - /* offset */
774 SMS->UDH_Length - /* UDH Length */
776 DecodeData(message + 34 + DataOffset[SMS->Type] + SMS->UDH_Length,
777 (unsigned char *)&(SMS->MessageText),
778 SMS->Length, size, SMS->UDH_Length, SMS->DCS);
780 SMS->MessageText[SMS->Length] = 0;
787 /* This function does simple SMS decoding - no PDU coding */
788 GSM_Error DecodeTextSMS(unsigned char *message, GSM_SMSMessage *SMS)