smalltalk
/
osmo-st-gsm
Archived
1
0
Fork 0

GSM: Add very simple encoding routine

This commit is contained in:
Holger Hans Peter Freyther 2011-03-31 19:18:10 +02:00
parent bf12ae6103
commit 60f1e3ec05
3 changed files with 72 additions and 0 deletions

57
GSMEncoding.st Normal file
View File

@ -0,0 +1,57 @@
"
(C) 2011 by Holger Hans Peter Freyther
All Rights Reserved
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"
String extend [
asGSM7Bit [
| bits rest bytes |
"I convert a string into a 7bit encoded string. I should
also be in UnicodeString but I am not. This impl. is just
basic and does not deal with difficult bits."
<category: '*-osmo-gsm'>
bits := OrderedCollection new.
"Split it into bits"
self do: [:char | | val |
val := char value.
1 to: 7 do: [:digit | bits add: (val bitAt: digit)].
].
"Turn bits into bytes again..."
rest := 8 - (bits size \\ 8).
rest to: 1 by: -1 do: [:each | bits add: 0].
bytes := ByteArray new: bits size / 8.
1 to: bits size by: 8 do: [:each | | byte |
byte := 0.
byte := (byte bitShift: 1) bitOr: (bits at: each + 7).
byte := (byte bitShift: 1) bitOr: (bits at: each + 6).
byte := (byte bitShift: 1) bitOr: (bits at: each + 5).
byte := (byte bitShift: 1) bitOr: (bits at: each + 4).
byte := (byte bitShift: 1) bitOr: (bits at: each + 3).
byte := (byte bitShift: 1) bitOr: (bits at: each + 2).
byte := (byte bitShift: 1) bitOr: (bits at: each + 1).
byte := (byte bitShift: 1) bitOr: (bits at: each + 0).
bytes at: (each // 8) + 1 put: byte.
].
^ bytes
]
]

View File

@ -535,3 +535,16 @@ TestCase subclass: SCCPHandlerTest [
self should: [serverCon next] raise: SystemExceptions.EndOfStream.
]
]
TestCase subclass: GSMEncodingTest [
<comment: 'I test the classic 7bit GSM encoding'>
test7BitEncode [
| wanted res |
wanted := #(16rD9 16r77 16r5D 16r0E 16r92 16r97 16rDB 16rE1 16rB4 16r3B 16rED 16r3E 16r83 16rC4 16r61 16r76 16rD8 16r3D 16r2E 16r83 16rD2 16r73 16r5D 16rEC 16r06 16rA3 16r81 16rDA 16r69 16r37 16rAB 16r8C 16r87 16rA7 16rE5 16r69 16rF7 16r19 16rF4 16r76 16rEB 16r62 16rB0 16r16 16rEC 16rD6 16r92 16rC1 16r62 16r30) asByteArray.
res := 'Your remaining balance is:1704 min,expiring on:10-07-2010' asGSM7Bit.
self assert: res = wanted
]
]

View File

@ -9,6 +9,7 @@
<filein>BSSAP.st</filein>
<filein>BSSMAP.st</filein>
<filein>GSM48.st</filein>
<filein>GSMEncoding.st</filein>
<filein>SCCPHandler.st</filein>
<test>
@ -17,6 +18,7 @@
<sunit>OsmoGSM.GSM48Test</sunit>
<sunit>OsmoGSM.TestMessages</sunit>
<sunit>OsmoGSM.SCCPHandlerTest</sunit>
<sunit>OsmoGSM.GSMEncodingTest</sunit>
<filein>Tests.st</filein>
</test>