From c2a2c4b90f51253ff3b17774ba9c5b4681b87c44 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Tue, 24 Mar 2015 14:45:07 +1100 Subject: [PATCH] gsm-sms: add proper encoding of messages. SMS doesn't use ASCII - we need to translate... Signed-off-by: NeilBrown --- gsm/gsm-sms.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/gsm/gsm-sms.py b/gsm/gsm-sms.py index d410d92..26edf56 100644 --- a/gsm/gsm-sms.py +++ b/gsm/gsm-sms.py @@ -78,6 +78,28 @@ def encode_number(recipient): recipient = recipient[2:] return leng + type + swap + +# GSM uses a 7-bit code that is not the same as ASCII... +# -*- coding: utf8 -*- +gsm = (u"@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?" + u"¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑÜ`¿abcdefghijklmnopqrstuvwxyzäöñüà") +ext = (u"````````````````````^```````````````````{}`````\\````````````[~]`" + u"|````````````````````````````````````€``````````````````````````") + +# Take a unicode string and produce a byte string for GSM +def gsm_encode(plaintext): + res = "" + for c in plaintext: + idx = gsm.find(c); + if idx != -1: + res += chr(idx) + continue + idx = ext.find(c) + if idx != -1: + res += chr(27) + res += chr(idx) + return res + def code7(pad, mesg): # Encode the message as 8 chars in 7 bytes. # pad with 'pad' 0 bits at the start (low in the byte) @@ -149,6 +171,7 @@ REF = '00' # ask network to assign ref number phone_num = encode_number(recipient) proto = '00' # don't know what this means encode = '00' # 7 bit ascii +mesg = gsm_encode(mesg) if len(mesg) <= 160: # single SMS mesg m1 = code7(0,mesg) -- 2.39.5