From c28b116689b03dcbd426e8028914bd3b6fa7b878 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Mon, 22 Nov 2010 17:11:08 +0100 Subject: [PATCH] Decode: Work on decoding the messages... This is a lot of work in progress to recursively decode the messages. --- BSSAP.st | 41 +++++++++++++++++++++++++----- Messages.st | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Tests.st | 18 +++++++++++++ 3 files changed, 126 insertions(+), 6 deletions(-) diff --git a/BSSAP.st b/BSSAP.st index 58569d0..b5128ae 100644 --- a/BSSAP.st +++ b/BSSAP.st @@ -28,24 +28,53 @@ Object subclass: BSSAPHelper [ ] ] -Object subclass: BSSAPManagement [ - | payload | +Object subclass: BSSAPMessage [ + BSSAPMessage class >> decode: bssap [ + | type | + type := bssap at: 1. + BSSAPMessage allSubclassesDo: [:each | + each msgType = type + ifTrue: [ + ^ each parseFrom: bssap. + ] + ]. + + ^ Error signal: 'No handler for: ', type asString. + ] +] + +BSSAPMessage subclass: BSSAPManagement [ + | data | + + BSSAPManagement class >> msgType [ ^ BSSAPHelper msgManagemnt ] BSSAPManagement class >> initWith: data [ ^ (self new) - payload: data; + data: data; yourself. ] - payload: aPayload [ - payload := aPayload. + BSSAPMessage class >> parseFrom: aByteArray [ + | size data | + size := aByteArray at: 2. + data := aByteArray copyFrom: 3 to: 2 + size. + + ^ BSSAPManagement initWith: data. + ] + + data: aPayload [ + data := aPayload. + ] + + data [ + ^ data ] writeOn: aMsg [ | dat | aMsg putByte: BSSAPHelper msgManagemnt. - dat := payload toMessageOrByteArray. + dat := data toMessageOrByteArray. aMsg putByte: dat size. aMsg putByteArray: dat. ] diff --git a/Messages.st b/Messages.st index 6976296..bc8c908 100644 --- a/Messages.st +++ b/Messages.st @@ -1,4 +1,38 @@ "General IE based message handling" +Object subclass: DataIE [ + | type data | + + + DataIE class >> initWith: aType data: aData [ + ^ self new + type: aType; + data: aData; + yourself + ] + + type [ + ^ type + ] + + type: aType [ + type := aType. + ] + + data [ + ^ data + ] + + data: aData [ + data := aData. + ] + + writeOn: aMsg [ + aMsg putByte: type. + aMsg putByte: data size. + aMsg putByteArray: data. + ] +] + Object subclass: IEMessage [ | ies type | @@ -10,11 +44,50 @@ Object subclass: IEMessage [ yourself ] + IEMessage class >> findIE: type with: data from: IEBase [ + "TODO: This needs to move some basic dispatch class" + "Find the IE that handles the type specified" + + ^ DataIE initWith: type data: data. +" + IEBase allSubclassesDo: [:each | + each elementId = type + ifTrue: [ + ^ each parseFrom: data. + ]. + ]. + + ^ Exception signal: 'Unsupported IE type: ', type. +" + ] + + IEMessage class >> decode: aByteArray with: IEBase [ + | msg dat | + msg := IEMessage initWith: (aByteArray at: 1). + + dat := aByteArray copyFrom: 2. + [dat isEmpty not] whileTrue: [ + | type size data | + type := dat at: 1. + size := dat at: 2. + data := dat copyFrom: 3 to: 2 + size. + dat := dat copyFrom: 3 + size. + + msg addIe: (self findIE: type with: data from: IEBase). + ]. + + ^ msg + ] + type: aType [ type := aType. ] + type [ + ^ type + ] + addIe: aIe [ self ies add: aIe. diff --git a/Tests.st b/Tests.st index b25afb8..19fbca7 100644 --- a/Tests.st +++ b/Tests.st @@ -44,6 +44,16 @@ TestCase subclass: GSM0808Test [ 16r75 16r30 16r17 16r03 16r01 16r02 16r03) asByteArray. self assert: buf asByteArray = res ] + + testIEDecoding [ + | inp res | + inp := #(16r57 16r05 16r08 16r00 16r72 16rF4 16r80 16r20 16r12 + 16r75 16r30 16r17 16r03 16r01 16r02 16r03) asByteArray. + + res := IEMessage decode: inp with: GSM0808IE. + self assert: res type = GSM0808Helper msgComplL3. + self assert: res ies size = 2. + ] ] TestCase subclass: BSSAPTest [ @@ -63,6 +73,14 @@ TestCase subclass: BSSAPTest [ self assert: man toMessage asByteArray = #(0 3 1 2 3) asByteArray. ] + testParseManagement [ + | man | + + man := BSSAPMessage decode: #(0 3 1 2 3) asByteArray. + self assert: (man isKindOf: BSSAPManagement). + self assert: man data = #(1 2 3) asByteArray. + ] + testPrependDTAP [ | msg | msg := Osmo.MessageBuffer new.