-
Notifications
You must be signed in to change notification settings - Fork 21
/
ModemSignal.go
executable file
·307 lines (263 loc) · 8.24 KB
/
ModemSignal.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package modemmanager
import (
"encoding/json"
"fmt"
"github.com/godbus/dbus/v5"
"reflect"
)
// Paths of methods and properties
const (
ModemSignalInterface = ModemInterface + ".Signal"
/* Methods */
ModemSignalSetup = ModemSignalInterface + ".Setup"
/* Property */
ModemSignalPropertyRate = ModemSignalInterface + ".Rate"
ModemSignalPropertyCdma = ModemSignalInterface + ".Cdma"
ModemSignalPropertyEvdo = ModemSignalInterface + ".Evdo"
ModemSignalPropertyGsm = ModemSignalInterface + ".Gsm"
ModemSignalPropertyUmts = ModemSignalInterface + ".Umts"
ModemSignalPropertyLte = ModemSignalInterface + ".Lte"
)
// ModemSignal provides access to extended signal quality information.
// 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 ModemSignal interface {
/* METHODS */
// Returns object path
GetObjectPath() dbus.ObjectPath
MarshalJSON() ([]byte, error)
// Setup extended signal quality information retrieval.
// refresh rate to set, in seconds. 0 to disable retrieval.
Setup(rate uint32) error
/* PROPERTIES */
//Refresh rate for the extended signal quality information updates, in seconds. A value of 0 disables the retrieval of the values.
GetRate() (rate uint32, err error)
// Returns all available cmda,evdo, gsm,umts or lte signal properties objects where rssi is set
GetCurrentSignals() (sp []SignalProperty, err error)
// The CDMA1x access technology.
GetCdma() (SignalProperty, error)
// The CDMA EV-DO access technology.
GetEvdo() (SignalProperty, error)
// The GSM/GPRS access technology.
GetGsm() (SignalProperty, error)
// The UMTS (WCDMA) access technology.
GetUmts() (SignalProperty, error)
// The LTE access technology.
GetLte() (SignalProperty, error)
}
// NewModemSignal returns new ModemSignal Interface
func NewModemSignal(objectPath dbus.ObjectPath) (ModemSignal, error) {
var si modemSignal
return &si, si.init(ModemManagerInterface, objectPath)
}
type modemSignal struct {
dbusBase
}
// SignalProperty represents all available signal properties
type SignalProperty struct {
Type MMSignalPropertyType `json:"property-type"` // define the Signal Property Type
Rssi float64 `json:"rssi"` // The CDMA1x / CDMA EV-DO / GSM / UMTS / LTE RSSI (Received Signal Strength Indication), in dBm, given as a floating point value (Applicable for all types).
Ecio float64 `json:"ecio"` // The CDMA1x Ec/Io / CDMA EV-DO Ec/Io / UMTS Ec/Io level in dBm, given as a floating point value (Only applicable for type Cdma, Evdo, Umts).
Sinr float64 `json:"sinr"` // CDMA EV-DO SINR level, in dB, given as a floating point value (Only applicable for type Evdo).
Io float64 `json:"io"` // The CDMA EV-DO Io, in dBm, given as a floating point value (Only applicable for type Evdo).
Rscp float64 `json:"rscp"` // The UMTS RSCP (Received Signal Code Power), in dBm, given as a floating point value (Only applicable for type Umts).
Rsrq float64 `json:"rsrq"` // The LTE RSRP (Reference Signal Received Power), in dBm, given as a floating point value (Only applicable for type LTE).
Rsrp float64 `json:"rsrp"` // The LTE RSRP (Reference Signal Received Power), in dBm, given as a floating point value (Only applicable for type LTE).
Snr float64 `json:"snr"` // The LTE S/R ratio, in dB, given as a floating point value (Only applicable for type LTE).
}
// MarshalJSON returns a byte array
func (sp SignalProperty) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"Type": fmt.Sprint(sp.Type),
"Rssi": sp.Rssi,
"Ecio": sp.Ecio,
"Sinr": sp.Sinr,
"Io": sp.Io,
"Rscp": sp.Rscp,
"Rsrq": sp.Rsrq,
"Rsrp": sp.Rsrp,
"Snr": sp.Snr,
})
}
func (sp SignalProperty) String() string {
return "Type: " + fmt.Sprint(sp.Type) +
", Rssi: " + fmt.Sprint(sp.Rssi) +
", Ecio: " + fmt.Sprint(sp.Ecio) +
", Sinr: " + fmt.Sprint(sp.Sinr) +
", Io: " + fmt.Sprint(sp.Io) +
", Rscp: " + fmt.Sprint(sp.Rscp) +
", Rsrq: " + fmt.Sprint(sp.Rsrq) +
", Rsrp: " + fmt.Sprint(sp.Rsrp) +
", Snr: " + fmt.Sprint(sp.Snr)
}
func convertMapToSignalProperty(inputMap map[string]dbus.Variant, signalType MMSignalPropertyType) (sp SignalProperty) {
sp.Type = signalType
for key, element := range inputMap {
switch key {
case "rssi":
tmpValue, ok := element.Value().(float64)
if ok {
sp.Rssi = tmpValue
}
case "sinr":
tmpValue, ok := element.Value().(float64)
if ok {
sp.Sinr = tmpValue
}
case "ecio":
tmpValue, ok := element.Value().(float64)
if ok {
sp.Ecio = tmpValue
}
case "io":
tmpValue, ok := element.Value().(float64)
if ok {
sp.Io = tmpValue
}
case "rscp":
tmpValue, ok := element.Value().(float64)
if ok {
sp.Rscp = tmpValue
}
case "rsrq":
tmpValue, ok := element.Value().(float64)
if ok {
sp.Rsrq = tmpValue
}
case "rsrp":
tmpValue, ok := element.Value().(float64)
if ok {
sp.Rsrp = tmpValue
}
case "snr":
tmpValue, ok := element.Value().(float64)
if ok {
sp.Snr = tmpValue
}
}
}
return
}
func (si modemSignal) GetObjectPath() dbus.ObjectPath {
return si.obj.Path()
}
func (si modemSignal) Setup(rate uint32) error {
return si.call(ModemSignalSetup, &rate)
}
func (si modemSignal) GetRate() (rate uint32, err error) {
return si.getUint32Property(ModemSignalPropertyRate)
}
func (si modemSignal) GetCdma() (sp SignalProperty, err error) {
res, err := si.getMapStringVariantProperty(ModemSignalPropertyCdma)
if err != nil {
return
}
sp = convertMapToSignalProperty(res, MMSignalPropertyTypeCdma)
return
}
func (si modemSignal) GetEvdo() (sp SignalProperty, err error) {
res, err := si.getMapStringVariantProperty(ModemSignalPropertyEvdo)
if err != nil {
return
}
sp = convertMapToSignalProperty(res, MMSignalPropertyTypeEvdo)
return
}
func (si modemSignal) GetGsm() (sp SignalProperty, err error) {
res, err := si.getMapStringVariantProperty(ModemSignalPropertyGsm)
if err != nil {
return
}
sp = convertMapToSignalProperty(res, MMSignalPropertyTypeGsm)
return
}
func (si modemSignal) GetUmts() (sp SignalProperty, err error) {
res, err := si.getMapStringVariantProperty(ModemSignalPropertyUmts)
if err != nil {
return
}
sp = convertMapToSignalProperty(res, MMSignalPropertyTypeUmts)
return
}
func (si modemSignal) GetLte() (sp SignalProperty, err error) {
res, err := si.getMapStringVariantProperty(ModemSignalPropertyLte)
if err != nil {
return
}
sp = convertMapToSignalProperty(res, MMSignalPropertyTypeLte)
return
}
func (si modemSignal) isRssiSet(sp SignalProperty) bool {
v := reflect.ValueOf(sp)
st := reflect.TypeOf(sp)
for i := 0; i < v.NumField(); i++ {
field := st.Field(i)
tag := field.Tag.Get("json")
if tag == "rssi" {
if !v.Field(i).IsZero() {
return true
}
}
}
return false
}
func (si modemSignal) GetCurrentSignals() (sp []SignalProperty, err error) {
mSignalCdma, err := si.GetCdma()
if err != nil {
return sp, err
}
if si.isRssiSet(mSignalCdma) {
sp = append(sp, mSignalCdma)
}
mSignalEvdo, err := si.GetEvdo()
if err != nil {
return sp, err
}
if si.isRssiSet(mSignalEvdo) {
sp = append(sp, mSignalEvdo)
}
mSignalGsm, err := si.GetGsm()
if err != nil {
return sp, err
}
if si.isRssiSet(mSignalGsm) {
sp = append(sp, mSignalGsm)
}
mSignalUmts, err := si.GetUmts()
if err != nil {
return sp, err
}
if si.isRssiSet(mSignalUmts) {
sp = append(sp, mSignalUmts)
}
mSignalLte, err := si.GetLte()
if err != nil {
return sp, err
}
if si.isRssiSet(mSignalLte) {
sp = append(sp, mSignalLte)
}
return
}
func (si modemSignal) MarshalJSON() ([]byte, error) {
rate, err := si.GetRate()
if err != nil {
return nil, err
}
var currentSignalsJson [][]byte
currentSignals, err := si.GetCurrentSignals()
if err != nil {
return nil, err
}
for _, x := range currentSignals {
tmpB, err := x.MarshalJSON()
if err != nil {
return nil, err
}
currentSignalsJson = append(currentSignalsJson, tmpB)
}
return json.Marshal(map[string]interface{}{
"Rate": rate,
"CurrentSignals": currentSignalsJson,
})
}