aboutsummaryrefslogtreecommitdiffstats
path: root/BSSMAP.st
blob: 4b78034a8f083c1687af47ade2c4bdefa60c0a12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
Object subclass: GSM0808IE [
    <category: 'osmo-message'>
    <comment: 'Base class of IEs for GSM0808'>

    type [
        "Go through the elementId of the class"
        ^ self class elementId
    ]
]

Object subclass: GSM0808Helper [
    GSM0808Helper class >> msgComplL3   [ <category: 'spec'> ^ 16r57 ]
    GSM0808Helper class >> msgReset     [ <category: 'spec'> ^ 16r30 ]
    GSM0808Helper class >> msgResetAck  [ <category: 'spec'> ^ 16r31 ]
    GSM0808Helper class >> msgClear     [ <category: 'spec'> ^ 16r20 ]
    GSM0808Helper class >> msgClearComp [ <category: 'spec'> ^ 16r21 ]
]

Object subclass: LAI [
    | mcc mnc |
    <category: 'osmo-message'>
    <comment: 'Generate a Location Area Identifier'>

    LAI class >> initWith: mcc mnc: mnc [
        ^ self new
            mcc: mcc;
            mnc: mnc;
            yourself
    ]

    LAI class >> parseFrom: aByteArray [
        | mcc mnc |

        mcc := ByteArray new: 3.
        mcc at: 1 put: ((aByteArray at: 1) bitAnd: 16rF).
        mcc at: 2 put: (((aByteArray at: 1) bitAnd: 16rF0) bitShift: -4).
        mcc at: 3 put: ((aByteArray at: 2) bitAnd: 16rF).
        mcc := BCD decode: mcc.

        mnc := ByteArray new: 3.
        mnc at: 1 put: ((aByteArray at: 3) bitAnd: 16rF).
        mnc at: 2 put: (((aByteArray at: 3) bitAnd: 16rF0) bitShift: -4).
        mnc at: 3 put: (((aByteArray at: 2) bitAnd: 16rF0) bitShift: -4).

        "Need to check if we have two or three bytes here."
        (mnc at: 3) = 16rF
            ifTrue: [
                mnc := BCD decode: (mnc copyFrom: 1 to: 2).
            ]
            ifFalse: [
                mnc := BCD decode: mnc.
            ].

        ^ LAI initWith: mcc mnc: mnc.
    ]

    LAI class >> generateLAI: mcc mnc: mnc [
        <category: 'creation'>
        | lai |

        lai := LAI initWith: mcc mnc: mnc.
        ^ lai toMessage asByteArray.
    ]

    writeOn: aMsg [
        | mcc_bcd mnc_bcd lai_0 lai_1 lai_2 |
        mcc_bcd := BCD encode: mcc.
        mnc_bcd := BCD encode: mnc.

        lai_0 := (mcc_bcd at: 1) bitOr: ((mcc_bcd at: 2) bitShift: 4).
        lai_1 := mcc_bcd at: 3.

        mnc > 99
            ifTrue: [
                lai_1 := lai_1 bitOr: ((mnc_bcd at: 3) bitShift: 4).
                lai_2 := (mnc_bcd at: 1) bitOr: ((mnc_bcd at: 2) bitShift: 4)
            ]
            ifFalse: [
                lai_1 := lai_1 bitOr: (16rF bitShift: 4).
                lai_2 := (mnc_bcd at: 2) bitOr: ((mnc_bcd at: 3) bitShift: 4)
            ].

        aMsg putByte: lai_0.
        aMsg putByte: lai_1.
        aMsg putByte: lai_2.
    ]

    mcc [
        ^ mcc
    ]

    mcc: aMcc [
        mcc := aMcc.
    ]

    mnc [
        ^ mnc
    ]

    mnc: aMnc [
        mnc := aMnc.
    ]

]

GSM0808IE subclass: GSMCellIdentifier [
    <category: 'osmo-message'>
    <comment: 'Generate a GSM0808 Cell Identifier'>

    | lai lac ci |
    GSMCellIdentifier class >> elementId [ <category: 'spec'> ^ 5 ]
    GSMCellIdentifier class >> initWith: mcc mnc: mnc lac: lac ci: ci [
        <category: 'creation'>
        ^ (self new)
            mcc: mcc mnc: mnc lac: lac ci: ci;
            yourself
    ]

    GSMCellIdentifier class >> parseFrom: aByteArray [
        | lai lac ci |
        (aByteArray at: 3) = 0
            ifFalse: [
            Error signal: 'Can not handle Cell Identifier of type != 0'.
        ].

        lai := LAI parseFrom: (aByteArray copyFrom: 4).
        lac := (aByteArray ushortAt: 7) swap16.
        ci  := (aByteArray ushortAt: 9) swap16.

        ^ self new
            mcc: lai mcc mnc: lai mnc lac: lac ci: ci;
            yourself
    ]

    mcc: aMcc mnc: aMnc lac: aLac ci: aCi [
        <category: 'creation'>
        lai := LAI initWith: aMcc mnc: aMnc.
        lac := aLac.
        ci := aCi.
    ]

    mcc [
        <category: 'access'>
        ^ lai mcc
    ]

    mnc [
        <category: 'access'>
        ^ lai mnc
    ]

    lac [
        <category: 'access'>
        ^ lac
    ]

    ci [
        <category: 'access'>
        ^ ci
    ]

    writeOn: aMsg [
        <category: 'creation'>
        | lai_data |
        lai_data := lai toMessageOrByteArray.

        aMsg putByte: self class elementId.
        aMsg putByte: 1 + lai_data size + 2 + 2.
        aMsg putByte: 0.
        aMsg putByteArray: lai_data.
        aMsg putLen16: lac.
        aMsg putLen16: ci.
    ]
]

GSM0808IE subclass: GSMLayer3Info [
    <category: 'osmo-message'>
    <comment: 'Generate a Layer3 IE'>
    | data |
    GSMLayer3Info class >> elementId [ <category: 'spec'> ^ 23 ]
    GSMLayer3Info class >> initWith: data [
        <category: 'creation'>
        ^ (self new)
            data: data;
            yourself
    ]

    GSMLayer3Info class >> parseFrom: aByteArray [
        | size |
        size := aByteArray at: 2.
        ^ GSMLayer3Info initWith: (aByteArray copyFrom: 3 to: 2 + size)
    ]

    data: aData [
        <category: 'creation'>
        data := aData
    ]

    data [
        ^ data
    ]

    writeOn: aMsg [
        | dat |
        <category: 'creation'>
        aMsg putByte: self class elementId.

        dat := data toMessageOrByteArray.
        aMsg putByte: dat size.
        aMsg putByteArray: dat.
    ]
]

GSM0808IE subclass: GSMCauseIE [
    | cause |

    <category: 'osmo-message'>
    <comment: 'Generate a CauseIE'>
    "TODO: Only simple ones are supported right now"

    GSMCauseIE class >> elementId [ <category: 'spec'> ^ 4 ]

    GSMCauseIE class >> initWith: aCause [
        ^ self new
            cause: aCause;
            yourself
    ]
    GSMCauseIE class >> parseFrom: aByteArray [
        | size |
        size := aByteArray at: 2.
        size = 1
            ifFalse: [
            ^ Error signal: 'Extended error codes are not supported.'.
        ].

        ^ GSMCauseIE initWith: (aByteArray at: 3)
    ]

    cause [ ^ cause ]
    cause: aCause [ cause := aCause ]

    writeOn: aMsg [
        aMsg putByte: self class elementId.
        aMsg putByte: 1.
        aMsg putByte: cause.
    ]
]