-
Notifications
You must be signed in to change notification settings - Fork 21
/
ModemCdma.go
executable file
·276 lines (235 loc) · 9.51 KB
/
ModemCdma.go
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package modemmanager
import (
"encoding/json"
"errors"
"fmt"
"github.com/godbus/dbus/v5"
"reflect"
)
// Paths of methods and properties
const (
ModemCdmaInterface = ModemInterface + ".ModemCdma"
/* Methods */
ModemCdmaActivate = ModemCdmaInterface + ".Activate"
ModemCdmaActivateManual = ModemCdmaInterface + ".ActivateManual"
/* Property */
ModemCdmaPropertyActivationState = ModemCdmaInterface + ".ActivationState" // readable u
ModemCdmaPropertyMeid = ModemCdmaInterface + ".Meid" // readable s
ModemCdmaPropertyEsn = ModemCdmaInterface + ".Esn" // readable s
ModemCdmaPropertySid = ModemCdmaInterface + ".Sid" // readable u
ModemCdmaPropertyNid = ModemCdmaInterface + ".Nid" // readable u
ModemCdmaPropertyCdma1xRegistrationState = ModemCdmaInterface + ".Cdma1xRegistrationState" // readable u
ModemCdmaPropertyEvdoRegistrationState = ModemCdmaInterface + ".EvdoRegistrationState" // readable u
/* Signal */
ModemCdmaSignalActivationStateChanged = "ActivationStateChanged"
)
// ModemCdma interface provides access to specific actions that may be performed in modems with CDMA capabilities.
// This interface will only be available once the modem is ready to be registered in the cellular network.
// Mixed 3GPP+3GPP2 devices will require a valid unlocked SIM card before any of the features in the interface can be used.
type ModemCdma interface {
/* METHODS */
// get object path
GetObjectPath() dbus.ObjectPath
//Provisions the modem for use with a given carrier using the modem's Over-The-Air (OTA) activation functionality, if any.
//Some modems will reboot after this call is made.
// IN s carrier_code: Name of carrier, or carrier-specific code.
Activate(carrierCode string) error
// Sets the modem provisioning data directly, without contacting the carrier over the air.
// Some modems will reboot after this call is made.
ActivateManual(property CdmaProperty) error
/* PROPERTIES */
// A MMModemCdmaActivationState value specifying the state of the activation in the 3GPP2 network.
GetActivationState() (MMModemCdmaActivationState, error)
// The modem's Mobile Equipment Identifier.
GetMeid() (string, error)
// The modem's Electronic Serial Number (superceded by MEID but still used by older devices).
GetEsn() (string, error)
// The System Identifier of the serving CDMA 1x network, if known, and if the modem is registered with a CDMA 1x network.
// See ifast.org or the mobile broadband provider database for mappings of SIDs to network providers.
GetSid() (uint32, error)
// The Network Identifier of the serving CDMA 1x network, if known, and if the modem is registered with a CDMA 1x network.
GetNid() (uint32, error)
// A MMModemCdmaRegistrationState value specifying the CDMA 1x registration state.
GetCdma1xRegistrationState() (MMModemCdmaRegistrationState, error)
// A MMModemCdmaRegistratiCdmaProperty onState value specifying the EVDO registration state.
GetEvdoRegistrationState() (MMModemCdmaRegistrationState, error)
MarshalJSON() ([]byte, error)
/* SIGNALS */
// The device activation state changed.
// u activation_state: Current activation state, given as a MMModemCdmaActivationState.
// u activation_error: Carrier-specific error code, given as a MMCdmaActivationError.
// a{sv} status_changes:Properties that have changed as a result of this activation state change, including "mdn" and "min". The dictionary may be empty if the changed properties are unknown.
SubscribeActivationStateChanged() <-chan *dbus.Signal
// ParsePropertiesChanged parses the dbus signal
ParseActivationStateChanged(v *dbus.Signal) (activationState MMModemCdmaActivationState, activationError MMCdmaActivationError, changedProperties map[string]dbus.Variant, err error)
Unsubscribe()
}
// NewModemCdma returns new ModemCdma Interface
func NewModemCdma(objectPath dbus.ObjectPath) (ModemCdma, error) {
var mc modemCdma
return &mc, mc.init(ModemManagerInterface, objectPath)
}
type modemCdma struct {
dbusBase
sigChan chan *dbus.Signal
}
// CdmaProperty describes the parameters for activating manually the modem
type CdmaProperty struct {
Spc string `json:"spc"` // The Service Programming Code, given as a string of exactly 6 digit characters. Mandatory parameter.
Sid uint16 `json:"sid"` // The System Identification Number, given as a 16-bit unsigned integer (signature "q"). Mandatory parameter.
Mdn string `json:"mdn"` // The Mobile Directory Number, given as a string of maximum 15 characters. Mandatory parameter.
Min string `json:"min"` // The Mobile Identification Number, given as a string of maximum 15 characters. Mandatory parameter.
MnHaKey string `json:"mn-ha-key"` // The MN-HA key, given as a string of maximum 16 characters.
MnAaaKey string `json:"mn-aaa-key"` // The MN-AAA key, given as a string of maximum 16 characters.
Prl []byte `json:"prl"` // The Preferred Roaming List, given as an array of maximum 16384 bytes.
}
// MarshalJSON returns a byte array
func (cdma CdmaProperty) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"Spc": cdma.Spc,
"Sid": cdma.Sid,
"Mdn": cdma.Mdn,
"Min": cdma.Min,
"MnHaKey": cdma.MnHaKey,
"MnAaaKey": cdma.MnAaaKey,
"Prl": cdma.Prl,
})
}
func (cdma CdmaProperty) String() string {
return returnString(cdma)
}
func (mc modemCdma) GetObjectPath() dbus.ObjectPath {
return mc.obj.Path()
}
func (mc modemCdma) Activate(carrierCode string) error {
// todo: untested
return mc.call(ModemCdmaActivate, &carrierCode)
}
func (mc modemCdma) ActivateManual(property CdmaProperty) error {
// todo: untested
v := reflect.ValueOf(property)
st := reflect.TypeOf(property)
type dynMap interface{}
var myMap map[string]dynMap
myMap = make(map[string]dynMap)
for i := 0; i < v.NumField(); i++ {
field := st.Field(i)
tag := field.Tag.Get("json")
value := v.Field(i).Interface()
if v.Field(i).IsZero() {
continue
}
myMap[tag] = value
}
return mc.call(ModemCdmaActivateManual, &myMap)
}
func (mc modemCdma) GetActivationState() (MMModemCdmaActivationState, error) {
res, err := mc.getUint32Property(ModemCdmaPropertyActivationState)
if err != nil {
return MmModemCdmaActivationStateUnknown, err
}
return MMModemCdmaActivationState(res), nil
}
func (mc modemCdma) GetMeid() (string, error) {
return mc.getStringProperty(ModemCdmaPropertyMeid)
}
func (mc modemCdma) GetEsn() (string, error) {
return mc.getStringProperty(ModemCdmaPropertyEsn)
}
func (mc modemCdma) GetSid() (uint32, error) {
return mc.getUint32Property(ModemCdmaPropertySid)
}
func (mc modemCdma) GetNid() (uint32, error) {
return mc.getUint32Property(ModemCdmaPropertyNid)
}
func (mc modemCdma) GetCdma1xRegistrationState() (MMModemCdmaRegistrationState, error) {
res, err := mc.getUint32Property(ModemCdmaPropertyCdma1xRegistrationState)
if err != nil {
return MmModemCdmaRegistrationStateUnknown, err
}
return MMModemCdmaRegistrationState(res), nil
}
func (mc modemCdma) GetEvdoRegistrationState() (MMModemCdmaRegistrationState, error) {
res, err := mc.getUint32Property(ModemCdmaPropertyEvdoRegistrationState)
if err != nil {
return MmModemCdmaRegistrationStateUnknown, err
}
return MMModemCdmaRegistrationState(res), nil
}
func (mc modemCdma) SubscribeActivationStateChanged() <-chan *dbus.Signal {
if mc.sigChan != nil {
return mc.sigChan
}
rule := fmt.Sprintf("type='signal', member='%s',path_namespace='%s'", ModemCdmaSignalActivationStateChanged, fmt.Sprint(mc.GetObjectPath()))
mc.conn.BusObject().Call(dbusMethodAddMatch, 0, rule)
mc.sigChan = make(chan *dbus.Signal, 10)
mc.conn.Signal(mc.sigChan)
return mc.sigChan
}
func (mc modemCdma) ParseActivationStateChanged(v *dbus.Signal) (activationState MMModemCdmaActivationState, activationError MMCdmaActivationError, changedProperties map[string]dbus.Variant, err error) {
if len(v.Body) != 3 {
err = errors.New("error by parsing activation changed signal")
return
}
aState, ok := v.Body[0].(uint32)
if !ok {
err = errors.New("error by parsing activation state")
return
}
activationState = MMModemCdmaActivationState(aState)
eState, ok := v.Body[1].(uint32)
if !ok {
err = errors.New("error by parsing activation error state")
return
}
activationError = MMCdmaActivationError(eState)
changedProperties, ok = v.Body[2].(map[string]dbus.Variant)
if !ok {
err = errors.New("error by parsing changed")
return
}
return
}
func (mc modemCdma) Unsubscribe() {
mc.conn.RemoveSignal(mc.sigChan)
mc.sigChan = nil
}
func (mc modemCdma) MarshalJSON() ([]byte, error) {
activationState, err := mc.GetActivationState()
if err != nil {
return nil, err
}
meid, err := mc.GetMeid()
if err != nil {
return nil, err
}
esn, err := mc.GetEsn()
if err != nil {
return nil, err
}
sid, err := mc.GetSid()
if err != nil {
return nil, err
}
nid, err := mc.GetNid()
if err != nil {
return nil, err
}
cdma1xRegistrationState, err := mc.GetCdma1xRegistrationState()
if err != nil {
return nil, err
}
evdoRegistrationState, err := mc.GetEvdoRegistrationState()
if err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{
"ActivationState": activationState,
"Meid": meid,
"Esn": esn,
"Sid": sid,
"Nid": nid,
"Cdma1xRegistrationState": cdma1xRegistrationState,
"EvdoRegistrationState": evdoRegistrationState,
})
}