Skip to content

Commit 0d37468

Browse files
committed
Correctly encode GSM messages.
1 parent a606d6e commit 0d37468

File tree

4 files changed

+171
-3
lines changed

4 files changed

+171
-3
lines changed

gsm.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ func (self *Modem) DeleteMessage(n int) error {
116116
}
117117

118118
func (self *Modem) SendMessage(telephone, body string) error {
119-
_, err := self.sendBody("+CMGS", body, telephone)
119+
enc := gsmEncode(body)
120+
_, err := self.sendBody("+CMGS", enc, telephone)
120121
return err
121122
}
122123

gsm_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func TestGetMissingMessage(t *testing.T) {
170170
var sendMessageReplay = []string{
171171
"->AT+CMGS=\"441234567890\"\r\n",
172172
"<-> \r\n",
173-
"->Body\x1a",
173+
"->Body\x00\x1a",
174174
"<-\r\nOK\r\n",
175175
}
176176

@@ -184,7 +184,7 @@ func TestSendMessage(t *testing.T) {
184184
t.Error("Expected: no error, got:", err)
185185
}
186186

187-
err = modem.SendMessage("441234567890", "Body")
187+
err = modem.SendMessage("441234567890", "Body@")
188188
if err != nil {
189189
t.Error("Expected: no error, got:", err)
190190
}

util.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,155 @@ func stringsUnquotes(s string) []string {
8585
return res
8686
}
8787

88+
var gsm0338 map[rune]string = map[rune]string{
89+
'@': "\x00",
90+
'£': "\x01",
91+
'$': "\x02",
92+
'¥': "\x03",
93+
'è': "\x04",
94+
'é': "\x05",
95+
'ù': "\x06",
96+
'ì': "\x07",
97+
'ò': "\x08",
98+
'Ç': "\x09",
99+
'\r': "\x0a",
100+
'Ø': "\x0b",
101+
'ø': "\x0c",
102+
'\n': "\x0d",
103+
'Å': "\x0e",
104+
'å': "\x0f",
105+
'Δ': "\x10",
106+
'_': "\x11",
107+
'Φ': "\x12",
108+
'Γ': "\x13",
109+
'Λ': "\x14",
110+
'Ω': "\x15",
111+
'Π': "\x16",
112+
'Ψ': "\x17",
113+
'Σ': "\x18",
114+
'Θ': "\x19",
115+
'Ξ': "\x1a",
116+
'Æ': "\x1c",
117+
'æ': "\x1d",
118+
'É': "\x1f",
119+
' ': " ",
120+
'!': "!",
121+
'"': "\"",
122+
'#': "#",
123+
'¤': "\x24",
124+
'%': "\x25",
125+
'&': "&",
126+
'\'': "'",
127+
'(': "(",
128+
')': ")",
129+
'*': "*",
130+
'+': "+",
131+
',': ",",
132+
'-': "-",
133+
'.': ".",
134+
'/': "/",
135+
'0': "0",
136+
'1': "1",
137+
'2': "2",
138+
'3': "3",
139+
'4': "4",
140+
'5': "5",
141+
'6': "6",
142+
'7': "7",
143+
'8': "8",
144+
'9': "9",
145+
':': ":",
146+
';': ";",
147+
'<': "<",
148+
'=': "=",
149+
'>': ">",
150+
'?': "?",
151+
'¡': "\x40",
152+
'A': "A",
153+
'B': "B",
154+
'C': "C",
155+
'D': "D",
156+
'E': "E",
157+
'F': "F",
158+
'G': "G",
159+
'H': "H",
160+
'I': "I",
161+
'J': "J",
162+
'K': "K",
163+
'L': "L",
164+
'M': "M",
165+
'N': "N",
166+
'O': "O",
167+
'P': "P",
168+
'Q': "Q",
169+
'R': "R",
170+
'S': "S",
171+
'T': "T",
172+
'U': "U",
173+
'V': "V",
174+
'W': "W",
175+
'X': "X",
176+
'Y': "Y",
177+
'Z': "Z",
178+
'Ä': "\x5b",
179+
'Ö': "\x5c",
180+
'Ñ': "\x5d",
181+
'Ü': "\x5e",
182+
'§': "\x5f",
183+
'a': "a",
184+
'b': "b",
185+
'c': "c",
186+
'd': "d",
187+
'e': "e",
188+
'f': "f",
189+
'g': "g",
190+
'h': "h",
191+
'i': "i",
192+
'j': "j",
193+
'k': "k",
194+
'l': "l",
195+
'm': "m",
196+
'n': "n",
197+
'o': "o",
198+
'p': "p",
199+
'q': "q",
200+
'r': "r",
201+
's': "s",
202+
't': "t",
203+
'u': "u",
204+
'v': "v",
205+
'w': "w",
206+
'x': "x",
207+
'y': "y",
208+
'z': "z",
209+
'ä': "\x7b",
210+
'ö': "\x7c",
211+
'ñ': "\x7d",
212+
'ü': "\x7e",
213+
'à': "\x7f",
214+
// escaped characters
215+
'€': "\x1be",
216+
'[': "\x1b<",
217+
'\\': "\x1b/",
218+
']': "\x1b>",
219+
'^': "\x1b^",
220+
'{': "\x1b(",
221+
'|': "\x1b@",
222+
'}': "\x1b)",
223+
'~': "\x1b=",
224+
}
225+
226+
// Encode the string to GSM03.38
227+
func gsmEncode(s string) string {
228+
res := ""
229+
for _, c := range s {
230+
if d, ok := gsm0338[c]; ok {
231+
res += string(d)
232+
}
233+
}
234+
return res
235+
}
236+
88237
// A logging ReadWriteCloser for debugging
89238
type LogReadWriteCloser struct {
90239
f io.ReadWriteCloser

util_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,21 @@ func ExampleUnquotes() {
2929
// Output:
3030
// [a,comma 1 b]
3131
}
32+
33+
func ExampleGsmEncode() {
34+
fmt.Printf("%q\n", gsmEncode("abcdefghijklmnopqrstuvwxyz"))
35+
fmt.Printf("%q\n", gsmEncode("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
36+
fmt.Printf("%q\n", gsmEncode("0123456789"))
37+
fmt.Printf("%q\n", gsmEncode(".,+-*/ "))
38+
fmt.Printf("%q\n", gsmEncode("°"))
39+
fmt.Printf("%q\n", gsmEncode("@£"))
40+
fmt.Printf("%q\n", gsmEncode("{}"))
41+
// Output:
42+
// "abcdefghijklmnopqrstuvwxyz"
43+
// "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
44+
// "0123456789"
45+
// ".,+-*/ "
46+
// ""
47+
// "\x00\x01"
48+
// "\x1b(\x1b)"
49+
}

0 commit comments

Comments
 (0)