-
Notifications
You must be signed in to change notification settings - Fork 19
/
ModemOma.go
executable file
·260 lines (226 loc) · 8.76 KB
/
ModemOma.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
package modemmanager
import (
"encoding/json"
"errors"
"fmt"
"github.com/godbus/dbus/v5"
)
// Paths of methods and properties
const (
ModemOmaInterface = ModemInterface + ".Oma"
/* Methods */
ModemOmaSetup = ModemOmaInterface + ".Setup"
ModemOmaStartClientInitiatedSession = ModemOmaInterface + ".StartClientInitiatedSession"
ModemOmaAcceptNetworkInitiatedSession = ModemOmaInterface + ".AcceptNetworkInitiatedSession"
ModemOmaCancelSession = ModemOmaInterface + ".CancelSession"
/* Property */
ModemOmaPropertyFeatures = ModemOmaInterface + ".Features" // readable u
ModemOmaPropertyPendingNetworkInitiatedSessions = ModemOmaInterface + ".PendingNetworkInitiatedSessions" // readable a(uu)
ModemOmaPropertySessionType = ModemOmaInterface + ".SessionType" // readable u
ModemOmaPropertySessionState = ModemOmaInterface + ".SessionState" // readable i
/* Signal */
ModemOmaSignalSessionStateChanged = "SessionStateChanged"
)
// ModemOma allows clients to handle device management operations as specified by the Open Mobile Alliance (OMA).
// Device management sessions are either on-demand (client-initiated), or automatically initiated by either the device
// itself or the network.
// This interface will only be available once the modem is ready to be registered in the cellular network.
// 3GPP devices will require a valid unlocked SIM card before any of the features in the interface can be used.
type ModemOma interface {
/* METHODS */
// Returns object path
GetObjectPath() dbus.ObjectPath
MarshalJSON() ([]byte, error)
// Configures which OMA device management features should be enabled.
// Bitmask of MMModemOmaFeature flags, specifying which device management
// features should get enabled or disabled. MM_OMA_FEATURE_NONE will disable all features.
Setup(features []MMOmaFeature) error
// Starts a client-initiated device management session.
// Type of client-initiated device management session,given as a MMModemOmaSessionType
StartClientInitiatedSession(sessionType MMOmaSessionType) error
// Accepts or rejects a network-initiated device management session.
// IN u session_id: Unique ID of the network-initiated device management session.
// IN b accept: Boolean specifying whether the session is accepted or rejected.
AcceptNetworkInitiatedSession(sessionId uint32, accept bool) error
// Cancels the current on-going device management session.
CancelSession() error
/* PROPERTIES */
// Bitmask of MMModemOmaFeature flags, specifying which device management features are enabled or disabled.
GetFeatures() ([]MMOmaFeature, error)
// List of network-initiated sessions which are waiting to be accepted or rejected, given as an array of unsigned integer pairs, where:
// The first integer is a MMOmaSessionType.
// The second integer is the unique session ID.
GetPendingNetworkInitiatedSessions() ([]ModemOmaInitiatedSession, error)
// Type of the current on-going device management session, given as a MMOmaSessionType.
GetSessionType() (MMOmaSessionType, error)
// State of the current on-going device management session, given as a MMOmaSessionState.
GetSessionState() (MMOmaSessionState, error)
/* SIGNALS */
// The session state changed.
// i old_session_state: Previous session state, given as a MMOmaSessionState.
// i new_session_state: Current session state, given as a MMOmaSessionState.
// u session_state_failed_reason: Reason of failure, given as a MMOmaSessionStateFailedReason, if session_state is MM_OMA_SESSION_STATE_FAILED.
SubscribeSessionStateChanged() <-chan *dbus.Signal
ParseSessionStateChanged(v *dbus.Signal) (oldState MMOmaSessionState, newState MMOmaSessionState, failureReason MMOmaSessionStateFailedReason, err error)
Unsubscribe()
}
// NewModemOma returns new ModemOma Interface
func NewModemOma(objectPath dbus.ObjectPath) (ModemOma, error) {
var om modemOma
return &om, om.init(ModemManagerInterface, objectPath)
}
type modemOma struct {
dbusBase
sigChan chan *dbus.Signal
}
type ModemOmaInitiatedSession struct {
SessionType MMOmaSessionType `json:"session-type"` // network-initiated session type
SessionId uint32 `json:"session-id"` // network-initiated session id
}
// MarshalJSON returns a byte array
func (mois ModemOmaInitiatedSession) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"SessionType": fmt.Sprint(mois.SessionType),
"SessionId": mois.SessionId,
})
}
func (mois ModemOmaInitiatedSession) String() string {
return "SessionType: " + fmt.Sprint(mois.SessionType) +
", SessionId: " + fmt.Sprint(mois.SessionId)
}
func (om modemOma) GetObjectPath() dbus.ObjectPath {
return om.obj.Path()
}
func (om modemOma) Setup(features []MMOmaFeature) error {
// todo: untested
var tmp MMOmaFeature
return om.call(ModemOmaSetup, tmp.SliceToBitmask(features))
}
func (om modemOma) StartClientInitiatedSession(sessionType MMOmaSessionType) error {
// todo: untested
return om.call(ModemOmaStartClientInitiatedSession, sessionType)
}
func (om modemOma) AcceptNetworkInitiatedSession(sessionId uint32, accept bool) error {
// todo: untested
return om.call(ModemOmaAcceptNetworkInitiatedSession, sessionId, accept)
}
func (om modemOma) CancelSession() error {
// todo: untested
return om.call(ModemOmaCancelSession)
}
func (om modemOma) GetFeatures() ([]MMOmaFeature, error) {
var tmp MMOmaFeature
res, err := om.getUint32Property(ModemOmaPropertyFeatures)
if err != nil {
return nil, err
}
return tmp.BitmaskToSlice(res), nil
}
func (om modemOma) GetPendingNetworkInitiatedSessions() (result []ModemOmaInitiatedSession, err error) {
res, err := om.getSliceSlicePairProperty(ModemOmaPropertyPendingNetworkInitiatedSessions)
if err != nil {
return nil, err
}
for _, e := range res {
var tmp ModemOmaInitiatedSession
sType, ok := e.GetLeft().(uint32)
if !ok {
return nil, errors.New("wrong type")
}
tmp.SessionType = MMOmaSessionType(sType)
sId, ok := e.GetLeft().(uint32)
if !ok {
return nil, errors.New("wrong type")
}
tmp.SessionId = sId
result = append(result, tmp)
}
return
}
func (om modemOma) GetSessionType() (MMOmaSessionType, error) {
res, err := om.getUint32Property(ModemOmaPropertySessionType)
if err != nil {
return MmOmaSessionTypeUnknown, err
}
return MMOmaSessionType(res), nil
}
func (om modemOma) GetSessionState() (MMOmaSessionState, error) {
res, err := om.getUint32Property(ModemOmaPropertySessionState)
if err != nil {
return MmOmaSessionStateUnknown, err
}
return MMOmaSessionState(res), nil
}
func (om modemOma) SubscribeSessionStateChanged() <-chan *dbus.Signal {
if om.sigChan != nil {
return om.sigChan
}
rule := fmt.Sprintf("type='signal', member='%s',path_namespace='%s'", ModemOmaSignalSessionStateChanged, fmt.Sprint(om.GetObjectPath()))
om.conn.BusObject().Call(dbusMethodAddMatch, 0, rule)
om.sigChan = make(chan *dbus.Signal, 10)
om.conn.Signal(om.sigChan)
return om.sigChan
}
func (om modemOma) ParseSessionStateChanged(v *dbus.Signal) (oldState MMOmaSessionState, newState MMOmaSessionState, failureReason MMOmaSessionStateFailedReason, err error) {
// todo: untested
if len(v.Body) < 2 {
err = errors.New("error by parsing session changed signal")
return
}
oState, ok := v.Body[0].(int32)
if !ok {
err = errors.New("error by parsing old state")
return
}
oldState = MMOmaSessionState(oState)
nState, ok := v.Body[1].(int32)
if !ok {
err = errors.New("error by parsing new state")
return
}
newState = MMOmaSessionState(nState)
if len(v.Body) == 3 {
rFailure, ok := v.Body[2].(uint32)
if !ok {
err = errors.New("error by parsing failure reason")
return
}
failureReason = MMOmaSessionStateFailedReason(rFailure)
}
return
}
func (om modemOma) Unsubscribe() {
om.conn.RemoveSignal(om.sigChan)
om.sigChan = nil
}
func (om modemOma) MarshalJSON() ([]byte, error) {
features, err := om.GetFeatures()
if err != nil {
return nil, err
}
var pendingNetworkInitiatedSessionsJson [][]byte
pendingNetworkInitiatedSessions, err := om.GetPendingNetworkInitiatedSessions()
if err != nil {
return nil, err
}
for _, x := range pendingNetworkInitiatedSessions {
tmpB, err := x.MarshalJSON()
if err != nil {
return nil, err
}
pendingNetworkInitiatedSessionsJson = append(pendingNetworkInitiatedSessionsJson, tmpB)
}
sessionType, err := om.GetSessionType()
if err != nil {
return nil, err
}
sessionState, err := om.GetSessionState()
if err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{
"Features": features,
"PendingNetworkInitiatedSessions": pendingNetworkInitiatedSessionsJson,
"SessionType": sessionType,
"SessionState": sessionState})
}