]> git.neil.brown.name Git - plato.git/commitdiff
gsm-sms: add proper encoding of messages.
authorNeilBrown <neilb@suse.de>
Tue, 24 Mar 2015 03:45:07 +0000 (14:45 +1100)
committerNeilBrown <neilb@suse.de>
Tue, 24 Mar 2015 03:45:07 +0000 (14:45 +1100)
SMS doesn't use ASCII - we need to translate...

Signed-off-by: NeilBrown <neil@brown.name>
gsm/gsm-sms.py

index d410d92c5da175687a30c43490d874df4519dd00..26edf56ba26ba59c9d67a23a153c024d2bb102aa 100644 (file)
@@ -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)