aboutsummaryrefslogtreecommitdiffstats
path: root/TestPhone.st
blob: ffdc00afcf33b6bf0c2b689edd6c46a64f90a9a6 (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
"
 (C) 2010-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/>.
"

PackageLoader fileInPackage: 'OsmoNetwork'.
PackageLoader fileInPackage: 'OsmoGSM'.

Object subclass: IPAConnection [
    | socket demuxer queue muxer dispatcher sccp ipa sem |
    <import: OsmoGSM>

    IPAConnection class >> initWith: anAddr port: aPort token: aToken [
        ^ (self new)
            socket: (Sockets.Socket remote: anAddr port: aPort);
            setup: aToken;
            yourself
    ]

    socket [
        ^ socket
    ]

    socket: aSocket [
        socket := aSocket.
    ]

    setup: aToken [
        sem := Semaphore forMutualExclusion.

        demuxer := Osmo.IPADemuxer initOn: socket.
        queue := SharedQueue new.
        muxer := Osmo.IPAMuxer initOn: queue.

        dispatcher := Osmo.IPADispatcher new.

        sccp := SCCPHandler new.
        sccp registerOn: dispatcher.
        sccp connection: self.

        ipa := Osmo.IPAProtoHandler new.
        ipa registerOn: dispatcher.
        ipa muxer: muxer.
        ipa token: aToken
    ]

    serve [
        [true] whileTrue: [
            [
                | data |
                data := demuxer next.
                dispatcher dispatch: data first with: data second.

                self drainSendQueue.
            ]
            on: SystemExceptions.FileError do: [:e | ^ false ]
            on: SystemExceptions.EndOfStream do: [:e | ^ false ]
        ].

        sccp linkSetFailed.
    ]

    drainSendQueue [
        sem critical: [
            [queue isEmpty] whileFalse: [
                | msg |
                msg := queue next.
                socket nextPutAllFlush: msg.
            ]
        ]
    ]

    send: aMsg with: aType [
        muxer nextPut: aMsg with: aType.
        [
            self drainSendQueue.
        ] on: SystemExceptions.FileError do: [:e | sccp linkSetFailed ]
          on: SystemExceptions.EndOfStream do: [:e | sccp linkSetFailed ]
    ]

    sccpHandler [
        ^ sccp
    ]
]

Object subclass: IPAConfig [
    | addr port token connection sem |

    addr: anAddr port: aPort [
        addr := anAddr.
        port := aPort.
    ]

    token: aToken [
        token := aToken.
    ]

    connect [
        sem := Semaphore new.
        connection := IPAConnection initWith: addr port: port token: token.
    ]

    connection [
        ^ connection
    ] 

    serve [
        [
            [
                connection serve.
                'Connection disconnected' printNl.
            ] ensure: [
                connection := nil.
                sem signal.
            ]
        ] fork.
    ]

    isConnected [
        ^ connection isNil not
    ]

    semaphore [ ^ sem ]

    doLU: aPhone [
        ^ (GSMConnection on: connection sccpHandler withPhone: aPhone)
            setProc: LUProcedure new;
            yourself
    ]
    sendLU: aPhone [
        ^ (self doLU: aPhone)
            openConnection; waitForTermination; yourself
    ]

    doCallNumber: aPhone nr: aNr [
        ^ (GSMConnection on: connection sccpHandler withPhone: aPhone)
            setProc: (CallProcedure initWithNr: aNr);
            yourself
    ]

    callNumber: aPhone nr: aNumber [
        ^ (self doCallNumber: aPhone nr: aNumber)
            openConnection; waitForTermination; yourself
    ]

    doUSSD: aPhone nr: aNr [
        ^ (GSMConnection on: connection sccpHandler withPhone: aPhone)
            setProc: (USSDProcedure initWithNr: aNr);
            yourself
    ]

    sendUSSD: aPhone nr: aNr [
        ^ (self doUSSD: aPhone nr: aNr)
            openConnection; waitForTermination; yourself
    ]
]

Object subclass: PhoneConfig [
    | imsi auKey auVer |

    <comment: 'I am the config of a phone. I do have an IMSI and such.'>

    PhoneConfig class >> initWith: aImsi auKey: anAuKey [
        ^ self new
            imsi: aImsi;
            auKey: anAuKey;
            yourself
    ]

    imsi: aImsi [
        imsi := aImsi.
    ]

    imsi [ ^ imsi ]
    auKey [ ^ auKey ]
    auVer [ ^ auVer ]
    auKey: anAuKey [
        auKey := anAuKey.
	auVer := 3.
    ]

    auKeyV2: anAuKey [
        auKey := anAuKey.
        auVer := 2.
    ]

    auKeyV1: anAuKey [
        auKey := anAuKey.
        auVer := 1.
    ] 

    auKeyByteArray [
        ^ auKey isString
            ifTrue:  [
                | array |
                array := OrderedCollection new.
                1 to: auKey size by: 2 do: [:each |
                    array add: (Number readFrom:
                                    (auKey copyFrom: each to: each + 1) readStream
                                    radix: 16)
                ].

                array asByteArray.
            ]
            ifFalse: [auKey].
    ]
]