forked from S1mpleTheBest/hilink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hilink.go
1064 lines (892 loc) · 27.4 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package hilink provides a Hilink WebUI client.
package hilink
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/clbanning/mxj"
)
// see: https://blog.hqcodeshop.fi/archives/259-Huawei-E5186-AJAX-API.html
// also see: https://github.com/BlackyPanther/Huawei-HiLink/blob/master/hilink.class.php
// also see: http://www.bez-kabli.pl/viewtopic.php?t=42168
const (
// DefaultURL is the default URL endpoint for the Hilink WebUI.
DefaultURL = "http://192.168.8.1/"
//DefaultHost default host header
DefaultHost = "192.168.8.1"
// DefaultTimeout is the default timeout.
DefaultTimeout = 30 * time.Second
// TokenHeader is the header used by the WebUI for CSRF tokens.
TokenHeader = "__RequestVerificationToken"
//TokenHeaderLogin .
TokenHeaderLogin = TokenHeader + "one"
)
// Client represents a Hilink client connection.
type Client struct {
rawhost string
rawurl string
url *url.URL
authID string
authPW string
nostart bool
client *http.Client
token string
transport http.RoundTripper
sync.Mutex
}
// WifiDefaultConfig returns the default configuration of the wireless interface.
func WifiDefaultConfig() map[string]string {
return map[string]string{
"Index": "0",
"WifiEnable": "0",
"WifiSsid": "",
"WifiMac": "",
"WifiBroadcast": "0",
"WifiIsolate": "0",
"WifiAuthmode": "WPA2-PSK",
"WifiBasicencryptionmodes": "WEP",
"WifiWpaencryptionmodes": "AES",
"WifiWepKey1": "",
"WifiWepKey2": "",
"WifiWepKey3": "",
"WifiWepKey4": "",
"WifiWep128Key1": "",
"WifiWep128Key2": "",
"WifiWep128Key3": "",
"WifiWep128Key4": "",
"WifiWepKeyIndex": "1",
"WifiWpapsk": "73634297",
"MixWifiWpapsk": "73634297",
"WifiWpsenbl": "1",
"WifiWpscfg": "0",
"WifiRotationInterval": "60",
"WifiAssociatedStationNum": "0",
"wifitotalswitch": "1",
"wifiguestofftime": "0",
}
}
// LoginResponse represents the response message of the login
// endpoint. Contains the session data.
type loginResponse struct {
tokenID string
sessionID string
}
// NewClient creates a new client a Hilink device.
func NewClient(opts ...Option) (*Client, error) {
var err error
// create client
c := &Client{
client: &http.Client{
Timeout: DefaultTimeout,
},
}
// process options
for _, o := range opts {
err = o(c)
if err != nil {
return nil, err
}
}
// set default url
if c.rawurl == "" || c.url == nil {
err = URL(DefaultURL)(c)
if err != nil {
return nil, err
}
}
// set default host
if c.rawhost == "" {
err = Host(DefaultHost)(c)
if err != nil {
return nil, err
}
}
// start session
if !c.nostart {
// retrieve session id
sessID, tokID, err := c.NewSessionAndTokenID()
if err != nil {
return nil, err
}
// set session id
err = c.SetSessionAndTokenID(sessID, tokID)
if err != nil {
return nil, err
}
// try login, ignore the OK value
_, err = c.login()
if err != nil {
return nil, err
}
}
return c, nil
}
// createRequest creates a request for use with the Client.
func (c *Client) createRequest(urlstr string, v interface{}) (*http.Request, error) {
if v == nil {
req, err := http.NewRequest("GET", urlstr, nil)
if err != nil {
return nil, err
}
req.Host = c.rawhost
return req, nil
}
// encode xml
body, err := encodeXML(v)
if err != nil {
return nil, err
}
// build req
req, err := http.NewRequest("POST", urlstr, body)
if err != nil {
return nil, err
}
// set content type and CSRF token
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header[TokenHeader] = []string{c.token}
req.Host = c.rawhost
return req, nil
}
// doReq sends a request to the server with the provided path. If data is nil,
// then GET will be used as the HTTP method, otherwise POST will be used.
func (c *Client) doReq(path string, v interface{}, takeFirstEl bool) (interface{}, error) {
c.Lock()
defer c.Unlock()
var err error
// create http request
q, err := c.createRequest(c.rawurl+path, v)
if err != nil {
return nil, err
}
// do request
r, err := c.client.Do(q)
if err != nil {
return nil, err
}
defer r.Body.Close()
// check status code
if r.StatusCode != http.StatusOK {
return nil, ErrBadStatusCode
}
// retrieve and save csrf token header
tok := r.Header.Get(TokenHeader)
if tok != "" {
c.token = tok
}
// read body
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
// decode
m, err := decodeXML(body, takeFirstEl)
if err != nil {
return nil, err
}
return m, nil
}
// doReqLogin sends a request to the server with the provided path. If data is nil,
// then GET will be used as the HTTP method, otherwise POST will be used. Takes
// the token number one and the new session id and replaces the current ones.
func (c *Client) doReqLogin(path string, v interface{}) (*loginResponse, error) {
c.Lock()
defer c.Unlock()
var err error
// create http request
q, err := c.createRequest(c.rawurl+path, v)
if err != nil {
return nil, err
}
// do request
r, err := c.client.Do(q)
if err != nil {
return nil, err
}
defer r.Body.Close()
// check status code
if r.StatusCode != http.StatusOK {
return nil, ErrBadStatusCode
}
// read body
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
// decode
res, err := decodeXML(body, false)
if err != nil {
return nil, err
}
// expect mxj.Map
m, ok := res.(mxj.Map)
if !ok {
return nil, ErrInvalidResponse
}
// check response present
o := map[string]interface{}(m)
resp, ok := o["response"]
if !ok {
return nil, ErrInvalidResponse
}
// convert
s, ok := resp.(string)
if !ok {
return nil, ErrInvalidValue
}
if s != "OK" {
return nil, ErrInvalidResponse
}
// retrieve and save new cookie and token
var out loginResponse
// saving token
out.tokenID = r.Header.Get(TokenHeaderLogin)
// saving cookie
setcookie := r.Header.Get("Set-Cookie")
cookie := strings.Split(setcookie, ";")[0]
sessID := strings.TrimPrefix(cookie, "SessionID=")
out.sessionID = sessID
return &out, nil
}
// doReqString wraps a request operation, returning the data of the specified
// child node named elName as a string.
func (c *Client) doReqString(path string, v interface{}, elName string) (string, error) {
// send request
res, err := c.doReq(path, v, true)
if err != nil {
return "", err
}
// convert
d, ok := res.(map[string]interface{})
if !ok {
return "", ErrInvalidXML
}
l, ok := d[elName]
if !ok {
return "", ErrInvalidResponse
}
s, ok := l.(string)
if !ok {
return "", ErrInvalidValue
}
return s, nil
}
// doReqCheckOK wraps a request operation (ie, connect, disconnect, etc),
// checking success via the presence of 'OK' in the XML <response/>.
func (c *Client) doReqCheckOK(path string, v interface{}) (bool, error) {
res, err := c.doReq(path, v, false)
if err != nil {
return false, err
}
// expect mxj.Map
m, ok := res.(mxj.Map)
if !ok {
return false, ErrInvalidResponse
}
// check response present
o := map[string]interface{}(m)
r, ok := o["response"]
if !ok {
return false, ErrInvalidResponse
}
// convert
s, ok := r.(string)
if !ok {
return false, ErrInvalidValue
}
return s == "OK", nil
}
// login authentifies the user using the user identifier and password given
// with the Auth option. Return nil if succeeded, or no Auth option
// was given, or the identifier is an empty string.
func (c *Client) login() (bool, error) {
if c.authID == "" {
return false, nil
}
// encode hashed password
tokenizedPW := HashPw(c.authID + HashPw(c.authPW) + c.token)
resp, err := c.doReqLogin("api/user/login", SimpleRequestXML(
"Username", c.authID,
"Password", tokenizedPW,
"password_type", "4",
))
if err != nil {
return false, err
}
if err = c.SetSessionAndTokenID(resp.sessionID, resp.tokenID); err != nil {
return false, err
}
return true, nil
}
// Do sends a request to the server with the provided path. If data is nil,
// then GET will be used as the HTTP method, otherwise POST will be used.
func (c *Client) Do(path string, v interface{}) (XMLData, error) {
// send request
res, err := c.doReq(path, v, true)
if err != nil {
return nil, err
}
// convert
d, ok := res.(map[string]interface{})
if !ok {
return nil, ErrInvalidXML
}
return d, nil
}
// NewSessionAndTokenID starts a session with the server, and returns the
// session and token.
func (c *Client) NewSessionAndTokenID() (string, string, error) {
res, err := c.doReq("api/webserver/SesTokInfo", nil, true)
if err != nil {
return "", "", err
}
// convert
vals, ok := res.(map[string]interface{})
if !ok {
return "", "", ErrInvalidResponse
}
// check ses/tokInfo present
sesInfo, ok := vals["SesInfo"]
if !ok {
return "", "", ErrInvalidResponse
}
tokInfo, ok := vals["TokInfo"]
if !ok {
return "", "", ErrInvalidResponse
}
// convert to strings
s, ok := sesInfo.(string)
if !ok {
return "", "", ErrInvalidResponse
}
t, ok := tokInfo.(string)
if !ok {
return "", "", ErrInvalidResponse
}
return strings.TrimPrefix(s, "SessionID="), t, nil
}
// SetSessionAndTokenID sets the sessionID and tokenID for the Client.
func (c *Client) SetSessionAndTokenID(sessionID, tokenID string) error {
c.Lock()
defer c.Unlock()
var err error
// create cookie jar
c.client.Jar, err = cookiejar.New(nil)
if err != nil {
return err
}
// set values on client
c.client.Jar.SetCookies(c.url, []*http.Cookie{&http.Cookie{
Name: "SessionID",
Value: sessionID,
}})
c.token = tokenID
return nil
}
// ChangePassword changes the current user password
func (c *Client) ChangePassword(newPassword string) (bool, error) {
oldPasswordHash := HashPw(c.authID + HashPw(c.authPW) + c.token)
newPasswordHash := base64.StdEncoding.EncodeToString([]byte(newPassword))
return c.doReqCheckOK("api/user/password", SimpleRequestXML(
"Username", c.authID,
"CurrentPassword", oldPasswordHash,
"NewPassword", newPasswordHash,
"encryption_enable", "1",
))
}
// GlobalConfig retrieves global Hilink configuration.
func (c *Client) GlobalConfig() (XMLData, error) {
return c.Do("config/global/config.xml", nil)
}
// NetworkTypes retrieves available network types.
func (c *Client) NetworkTypes() (XMLData, error) {
return c.Do("config/global/net-type.xml", nil)
}
// PCAssistantConfig retrieves PC Assistant configuration.
func (c *Client) PCAssistantConfig() (XMLData, error) {
return c.Do("config/pcassistant/config.xml", nil)
}
// DeviceConfig retrieves device configuration.
func (c *Client) DeviceConfig() (XMLData, error) {
return c.Do("config/deviceinformation/config.xml", nil)
}
// WebUIConfig retrieves WebUI configuration.
func (c *Client) WebUIConfig() (XMLData, error) {
return c.Do("config/webuicfg/config.xml", nil)
}
// SmsConfig retrieves device SMS configuration.
func (c *Client) SmsConfig() (XMLData, error) {
return c.Do("api/sms/config", nil)
}
// WlanConfig retrieves basic WLAN settings.
func (c *Client) WlanConfig() (XMLData, error) {
return c.Do("api/wlan/basic-settings", nil)
}
// WlanDisable disables the WLAN interface that matches the given ssid.
func (c *Client) WlanDisable(ssid string, config map[string]string) (bool, error) {
// WifiSsid has to be set up before modifying settings
var wifiConfig map[string]string
if config == nil {
wifiConfig = WifiDefaultConfig()
} else {
wifiConfig = config
}
wifiConfig["WifiSsid"] = ssid
return c.doReqCheckOK("api/wlan/multi-basic-settings", SimpleRequestXML(
"Ssids", xmlPairsString("", "Ssid", XMLMapString("", wifiConfig)),
"WifiRestart", "1",
))
}
// DhcpConfig retrieves DHCP configuration.
func (c *Client) DhcpConfig() (XMLData, error) {
return c.Do("api/dhcp/settings", nil)
}
// CradleStatusInfo retrieves cradle status information.
func (c *Client) CradleStatusInfo() (XMLData, error) {
return c.Do("api/cradle/status-info", nil)
}
// CradleMACSet sets the MAC address for the cradle.
func (c *Client) CradleMACSet(addr string) (bool, error) {
return c.doReqCheckOK("api/cradle/current-mac", XMLData{
"currentmac": addr,
})
}
// CradleMAC retrieves cradle MAC address.
func (c *Client) CradleMAC() (string, error) {
return c.doReqString("api/cradle/current-mac", nil, "currentmac")
}
// AutorunVersion retrieves device autorun version.
func (c *Client) AutorunVersion() (string, error) {
return c.doReqString("api/device/autorun-version", nil, "Version")
}
// DeviceBasicInfo retrieves basic device information.
func (c *Client) DeviceBasicInfo() (XMLData, error) {
return c.Do("api/device/basic_information", nil)
}
// PublicKey retrieves webserver public key.
func (c *Client) PublicKey() (string, error) {
return c.doReqString("api/webserver/publickey", nil, "encpubkeyn")
}
// DeviceControl sends a control code to the device.
func (c *Client) DeviceControl(code uint) (bool, error) {
return c.doReqCheckOK("api/device/control", XMLData{
"Control": fmt.Sprintf("%d", code),
})
}
// DeviceReboot restarts the device.
func (c *Client) DeviceReboot() (bool, error) {
return c.DeviceControl(1)
}
// DeviceReset resets the device configuration.
func (c *Client) DeviceReset() (bool, error) {
return c.DeviceControl(2)
}
// DeviceBackup backups device configuration and retrieves backed up
// configuration data as a base64 encoded string.
func (c *Client) DeviceBackup() (string, error) {
// cause backup to be generated
ok, err := c.DeviceControl(3)
if err != nil {
return "", err
}
if !ok {
return "", errors.New("unable to backup device configuration")
}
// retrieve data
//res, err := c.doReq("nvram.bak")
return " -- not implemented -- ", nil
}
// DeviceShutdown shuts down the device.
func (c *Client) DeviceShutdown() (bool, error) {
return c.DeviceControl(4)
}
// DeviceFeatures retrieves device feature information.
func (c *Client) DeviceFeatures() (XMLData, error) {
return c.Do("api/device/device-feature-switch", nil)
}
// DeviceInfo retrieves general device information.
func (c *Client) DeviceInfo() (XMLData, error) {
return c.Do("api/device/information", nil)
}
// DeviceModeSet sets the device mode (0-project, 1-debug).
func (c *Client) DeviceModeSet(mode uint) (bool, error) {
return c.doReqCheckOK("api/device/mode", XMLData{
"mode": fmt.Sprintf("%d", mode),
})
}
// FastbootFeatures retrieves fastboot feature information.
func (c *Client) FastbootFeatures() (XMLData, error) {
return c.Do("api/device/fastbootswitch", nil)
}
// PowerFeatures retrieves power feature information.
func (c *Client) PowerFeatures() (XMLData, error) {
return c.Do("api/device/powersaveswitch", nil)
}
// TetheringFeatures retrieves USB tethering feature information.
func (c *Client) TetheringFeatures() (XMLData, error) {
return c.Do("api/device/usb-tethering-switch", nil)
}
// SignalInfo retrieves network signal information.
func (c *Client) SignalInfo() (XMLData, error) {
return c.Do("api/device/signal", nil)
}
// ConnectionInfo retrieves connection (dialup) information.
func (c *Client) ConnectionInfo() (XMLData, error) {
return c.Do("api/dialup/connection", nil)
}
// GlobalFeatures retrieves global feature information.
func (c *Client) GlobalFeatures() (XMLData, error) {
return c.Do("api/global/module-switch", nil)
}
// Language retrieves current language.
func (c *Client) Language() (string, error) {
return c.doReqString("api/language/current-language", nil, "CurrentLanguage")
}
// LanguageSet sets the language.
func (c *Client) LanguageSet(lang string) (bool, error) {
return c.doReqCheckOK("api/language/current-language", XMLData{
"CurrentLanguage": lang,
})
}
// NotificationInfo retrieves notification information.
func (c *Client) NotificationInfo() (XMLData, error) {
return c.Do("api/monitoring/check-notifications", nil)
}
// SimInfo retrieves SIM card information.
func (c *Client) SimInfo() (XMLData, error) {
return c.Do("api/monitoring/converged-status", nil)
}
// StatusInfo retrieves general device status information.
func (c *Client) StatusInfo() (XMLData, error) {
return c.Do("api/monitoring/status", nil)
}
// TrafficInfo retrieves traffic statistic information.
func (c *Client) TrafficInfo() (XMLData, error) {
return c.Do("api/monitoring/traffic-statistics", nil)
}
// TrafficClear clears the current traffic statistics.
func (c *Client) TrafficClear() (bool, error) {
return c.doReqCheckOK("api/monitoring/clear-traffic", XMLData{
"ClearTraffic": "1",
})
}
// MonthInfo retrieves the month download statistic information.
func (c *Client) MonthInfo() (XMLData, error) {
return c.Do("api/monitoring/month_statistics", nil)
}
// WlanMonthInfo retrieves the WLAN month download statistic information.
func (c *Client) WlanMonthInfo() (XMLData, error) {
return c.Do("api/monitoring/month_statistics_wlan", nil)
}
// NetworkInfo retrieves network provider information.
func (c *Client) NetworkInfo() (XMLData, error) {
return c.Do("api/net/current-plmn", nil)
}
// WifiFeatures retrieves wifi feature information.
func (c *Client) WifiFeatures() (XMLData, error) {
return c.Do("api/wlan/wifi-feature-switch", nil)
}
// ModeList retrieves available network modes.
func (c *Client) ModeList() (XMLData, error) {
return c.Do("api/net/net-mode-list", nil)
}
// ModeInfo retrieves network mode settings information.
func (c *Client) ModeInfo() (XMLData, error) {
return c.Do("api/net/net-mode", nil)
}
// ModeNetworkInfo retrieves current network mode information.
func (c *Client) ModeNetworkInfo() (XMLData, error) {
return c.Do("api/net/network", nil)
}
// ModeSet sets the network mode.
func (c *Client) ModeSet(netMode, netBand, lteBand string) (bool, error) {
return c.doReqCheckOK("api/net/net-mode", SimpleRequestXML(
"NetworkMode", netMode,
"NetworkBand", netBand,
"LTEBand", lteBand,
))
}
// PinInfo retrieves SIM PIN status information.
func (c *Client) PinInfo() (XMLData, error) {
return c.Do("api/pin/status", nil)
}
// doReqPin wraps a SIM PIN manipulation request.
func (c *Client) doReqPin(pt PinType, cur, new, puk string) (bool, error) {
return c.doReqCheckOK("api/pin/operate", SimpleRequestXML(
"OperateType", fmt.Sprintf("%d", pt),
"CurrentPin", cur,
"NewPin", new,
"PukCode", puk,
))
}
// PinEnter enters a SIM PIN.
func (c *Client) PinEnter(pin string) (bool, error) {
return c.doReqPin(PinTypeEnter, pin, "", "")
}
// PinActivate activates a SIM PIN.
func (c *Client) PinActivate(pin string) (bool, error) {
return c.doReqPin(PinTypeActivate, pin, "", "")
}
// PinDeactivate deactivates a SIM PIN.
func (c *Client) PinDeactivate(pin string) (bool, error) {
return c.doReqPin(PinTypeDeactivate, pin, "", "")
}
// PinChange changes a SIM PIN.
func (c *Client) PinChange(pin, new string) (bool, error) {
return c.doReqPin(PinTypeChange, pin, new, "")
}
// PinEnterPuk enters a SIM PIN puk.
func (c *Client) PinEnterPuk(puk, new string) (bool, error) {
return c.doReqPin(PinTypeEnterPuk, new, new, puk)
}
// PinSaveInfo retrieves SIM PIN save information.
func (c *Client) PinSaveInfo() (XMLData, error) {
return c.Do("api/pin/save-pin", nil)
}
// PinSimlockInfo retrieves SIM lock information.
func (c *Client) PinSimlockInfo() (XMLData, error) {
return c.Do("api/pin/simlock", nil)
}
// Connect connects the Hilink device to the network provider.
func (c *Client) Connect() (bool, error) {
return c.doReqCheckOK("api/dialup/dial", XMLData{
"Action": "1",
})
}
// Disconnect disconnects the Hilink device from the network provider.
func (c *Client) Disconnect() (bool, error) {
return c.doReqCheckOK("api/dialup/dial", XMLData{
"Action": "0",
})
}
// ProfileInfo retrieves profile information (ie, APN).
func (c *Client) ProfileInfo() (XMLData, error) {
return c.Do("api/dialup/profiles", nil)
}
// SmsFeatures retrieves SMS feature information.
func (c *Client) SmsFeatures() (XMLData, error) {
return c.Do("api/sms/sms-feature-switch", nil)
}
// SmsList retrieves list of SMS in an inbox.
func (c *Client) SmsList(boxType, page, count uint, sortByName, ascending, unreadPreferred bool) (XMLData, error) {
// execute request -- note: the order is important!
return c.Do("api/sms/sms-list", SimpleRequestXML(
"PageIndex", fmt.Sprintf("%d", page),
"ReadCount", fmt.Sprintf("%d", count),
"BoxType", fmt.Sprintf("%d", boxType),
"SortType", boolToString(sortByName),
"Ascending", boolToString(ascending),
"UnreadPreferred", boolToString(unreadPreferred),
))
}
// SmsCount retrieves count of SMS per inbox type.
func (c *Client) SmsCount() (XMLData, error) {
return c.Do("api/sms/sms-count", nil)
}
// SmsSend sends an SMS.
func (c *Client) SmsSend(msg string, to ...string) (bool, error) {
if len(msg) >= 160 {
return false, ErrMessageTooLong
}
// build phones
phones := []string{}
for _, t := range to {
phones = append(phones, "Phone", t)
}
// send request (order matters below!)
return c.doReqCheckOK("api/sms/send-sms", SimpleRequestXML(
"Index", "-1",
"Phones", "\n"+string(xmlPairs(" ", phones...)),
"Sca", "",
"Content", msg,
"Length", fmt.Sprintf("%d", len(msg)),
"Reserved", "1",
"Date", time.Now().Format("2006-01-02 15:04:05"),
))
}
// SmsSendStatus retrieves SMS send status information.
func (c *Client) SmsSendStatus() (XMLData, error) {
return c.Do("api/sms/send-status", nil)
}
// SmsReadSet sets the read status of a SMS.
func (c *Client) SmsReadSet(id string) (bool, error) {
return c.doReqCheckOK("api/sms/set-read", SimpleRequestXML(
"Index", id,
))
}
// SmsDelete deletes a specified SMS.
func (c *Client) SmsDelete(id uint) (bool, error) {
return c.doReqCheckOK("api/sms/delete-sms", SimpleRequestXML(
"Index", fmt.Sprintf("%d", id),
))
}
// doReqConn wraps a connection manipulation request.
/*func (c *Client) doReqConn(
cm ConnectMode,
autoReconnect, roamAutoConnect, roamAutoReconnect bool,
interval, idle int,
) (bool, error) {
boolToString()
return c.doReqCheckOK("api/dialup/connection", SimpleRequestXML(
"RoamAutoConnectEnable", boolToString(roamAutoConnect),
"AutoReconnect", boolToString(autoReconnect),
"RoamAutoReconnectEnable", boolToString(roamAutoReconnect),
"ReconnectInterval", fmt.Sprintf("%d", interval),
"MaxIdleTime", fmt.Sprintf("%d", idle),
"ConnectMode", cm.String(),
))
}*/
// UssdStatus retrieves current USSD session status information.
func (c *Client) UssdStatus() (UssdState, error) {
s, err := c.doReqString("api/ussd/status", nil, "result")
if err != nil {
return UssdStateNone, err
}
i, err := strconv.Atoi(s)
if err != nil {
return UssdStateNone, ErrInvalidResponse
}
return UssdState(i), nil
}
// UssdCode sends a USSD code to the Hilink device.
func (c *Client) UssdCode(code string) (bool, error) {
return c.doReqCheckOK("api/ussd/send", SimpleRequestXML(
"content", code,
"codeType", "CodeType",
"timeout", "",
))
}
// UssdContent retrieves content buffer of the active USSD session.
func (c *Client) UssdContent() (string, error) {
return c.doReqString("api/ussd/get", nil, "content")
}
// UssdRelease releases the active USSD session.
func (c *Client) UssdRelease() (bool, error) {
return c.doReqCheckOK("api/ussd/release", nil)
}
// DdnsList retrieves list of DDNS providers.
func (c *Client) DdnsList() (XMLData, error) {
return c.Do("api/ddns/ddns-list", nil)
}
// LogPath retrieves device log path (URL).
func (c *Client) LogPath() (string, error) {
return c.doReqString("api/device/compresslogfile", nil, "LogPath")
}
// LogInfo retrieves current log setting information.
func (c *Client) LogInfo() (XMLData, error) {
return c.Do("api/device/logsetting", nil)
}
// PhonebookGroupList retrieves list of the phonebook groups.
func (c *Client) PhonebookGroupList(page, count uint, sortByName, ascending bool) (XMLData, error) {
return c.Do("api/pb/group-list", SimpleRequestXML(
"PageIndex", fmt.Sprintf("%d", page),
"ReadCount", fmt.Sprintf("%d", count),
"SortType", boolToString(sortByName),
"Ascending", boolToString(ascending),
))
}
// PhonebookCount retrieves count of phonebook entries per group.
func (c *Client) PhonebookCount() (XMLData, error) {
return c.Do("api/pb/pb-count", nil)
}
// PhonebookImport imports SIM contacts into specified phonebook group.
func (c *Client) PhonebookImport(group uint) (XMLData, error) {
return c.Do("api/pb/pb-copySIM", XMLData{
"GroupID": fmt.Sprintf("%d", group),
})
}
// PhonebookDelete deletes a specified phonebook entry.
func (c *Client) PhonebookDelete(id uint) (bool, error) {
return c.doReqCheckOK("api/pb/delete-pb", SimpleRequestXML(
"Index", fmt.Sprintf("%d", id),
))
}
// PhonebookList retrieves list of phonebook entries from a specified group.
func (c *Client) PhonebookList(group, page, count uint, sim, sortByName, ascending bool, keyword string) (XMLData, error) {
// execute request -- note: the order is important!
return c.Do("api/pb/pb-list", SimpleRequestXML(
"GroupID", fmt.Sprintf("%d", group),
"PageIndex", fmt.Sprintf("%d", page),
"ReadCount", fmt.Sprintf("%d", count),
"SaveType", boolToString(sim),
"SortType", boolToString(sortByName),
"Ascending", boolToString(ascending),
"KeyWord", keyword,
))
}
// PhonebookCreate creates a new phonebook entry.
func (c *Client) PhonebookCreate(group uint, name, phone string, sim bool) (XMLData, error) {
return c.Do("api/pb/pb-new", SimpleRequestXML(
"GroupID", fmt.Sprintf("%d", group),
"SaveType", boolToString(sim),
"Field", xmlNvp("FormattedName", name),
"Field", xmlNvp("MobilePhone", phone),
"Field", xmlNvp("HomePhone", ""),
"Field", xmlNvp("WorkPhone", ""),
"Field", xmlNvp("WorkEmail", ""),
))
}
// FirewallFeatures retrieves firewall security feature information.
func (c *Client) FirewallFeatures() (XMLData, error) {
return c.Do("api/security/firewall-switch", nil)