-
Notifications
You must be signed in to change notification settings - Fork 43
/
hilink.go
141 lines (126 loc) · 3.69 KB
/
hilink.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
// Package hilink provides a Hilink WebUI client.
package hilink
import (
"bytes"
"strconv"
"github.com/clbanning/mxj/v2"
)
// Error is the error type.
type Error string
// Error values.
const (
// ErrBadStatusCode is the bad status code error.
ErrBadStatusCode Error = "bad status code"
// ErrInvalidResponse is the invalid response error.
ErrInvalidResponse Error = "invalid response"
// ErrInvalidError is the invalid error error.
ErrInvalidError Error = "invalid error"
// ErrInvalidValue is the invalid value error.
ErrInvalidValue Error = "invalid value"
// ErrInvalidXML is the invalid xml error.
ErrInvalidXML Error = "invalid xml"
// ErrMissingRootElement is the missing root element error.
ErrMissingRootElement Error = "missing root element"
// ErrMessageTooLong is the message too long error.
ErrMessageTooLong Error = "message too long"
)
// Error satisfies the error interface.
func (err Error) Error() string {
return string(err)
}
// SmsBoxType represents the different inbox types available on a hilink
// device.
type SmsBoxType uint
// SmsBoxType values.
const (
SmsBoxTypeInbox SmsBoxType = iota + 1
SmsBoxTypeOutbox
SmsBoxTypeDraft
)
// PinType are the PIN types for a PIN command.
type PinType int
// PinType values.
const (
PinTypeEnter PinType = iota
PinTypeActivate
PinTypeDeactivate
PinTypeChange
PinTypeEnterPuk
)
// UssdState represents the different USSD states.
type UssdState int
// UssdState values.
const (
UssdStateNone UssdState = iota
UssdStateActive
UssdStateWaiting
)
// XMLData is a map of XML data to encode/decode.
type XMLData mxj.Map
// SimpleRequestXML creates an XML string from value pairs.
//
// Unfortunately the XML parser (or whatever underyling code) included with the
// WebUI on Hilink devices expects parameters in a specific order. This makes
// packages like mxj or other map based solutions not feasible for use, as Go
// has random key ordering for maps.
//
// On another note, XML sucks.
func SimpleRequestXML(vals ...string) []byte {
var buf bytes.Buffer
// write header
buf.WriteString(`<?xml version="1.0" encoding="UTF-8"?>`)
buf.WriteString("\n<request>\n")
// add pairs
buf.Write(xmlPairs(" ", vals...))
// end string
buf.WriteString("</request>\n")
return buf.Bytes()
}
// ErrorCodeMap contains the known message strings for Hilink devices.
//
// see: http://www.bez-kabli.pl/viewtopic.php?t=42168
func ErrorCodeMap() map[int]string {
return map[int]string{
-1: "system not available",
100002: "not supported by firmware or incorrect API path",
100003: "unauthorized",
100004: "system busy",
100005: "unknown error",
100006: "invalid parameter",
100009: "write error",
103002: "unknown error",
103015: "unknown error",
108001: "invalid username",
108002: "invalid password",
108003: "user already logged in",
108006: "invalid username or password",
108007: "invalid username, password, or session timeout",
110024: "battery charge less than 50%",
111019: "no network response",
111020: "network timeout",
111022: "network not supported",
113018: "system busy",
114001: "file already exists",
114002: "file already exists",
114003: "SD card currently in use",
114004: "path does not exist",
114005: "path too long",
114006: "no permission for specified file or directory",
115001: "unknown error",
117001: "incorrect WiFi password",
117004: "incorrect WISPr password",
120001: "voice busy",
125001: "invalid token",
}
}
// ErrorMessageFromString returns the error message from a string version of
// the error code.
func ErrorMessageFromString(code string) string {
m := ErrorCodeMap()
if c, err := strconv.Atoi(code); err == nil {
if msg, ok := m[c]; ok {
return msg
}
}
return m[-1]
}