-
Notifications
You must be signed in to change notification settings - Fork 11
/
msg.go
115 lines (107 loc) · 2.87 KB
/
msg.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
// Copyright 2019 py60800.
// Use of this source code is governed by Apache-2 licence
// license that can be found in the LICENSE file.
// Tuya high level communication library
package tuya
import (
"encoding/json"
"fmt"
"log"
"time"
"errors"
)
// create base messages
func (d *Appliance) makeBaseMsg() map[string]interface{} {
d.mutex.RLock()
defer d.mutex.RUnlock()
m := make(map[string]interface{})
m["devId"] = d.GwId
m["uid"] = d.GwId
m["t"] = time.Now().Unix()
return m
}
func (d *Appliance) makeStatusMsg() map[string]interface{} {
d.mutex.RLock()
defer d.mutex.RUnlock()
return map[string]interface{}{"gwId": d.GwId, "devId": d.GwId}
}
func (d *Appliance) initialStatusMsg() []byte {
m := map[string]interface{}{"gwId": d.GwId, "devId": d.GwId}
data, _ := json.Marshal(m)
return data
}
// -------------------------------
func (d *Appliance) SendEncryptedCommand(cmd int, jdata interface{}) error {
d.mutex.RLock()
data, er1 := json.Marshal(jdata)
if er1 != nil {
d.mutex.RUnlock()
return fmt.Errorf("Json Marshal (%v)", er1)
}
cipherText, er2 := aesEncrypt(data, d.key)
if er2 != nil {
d.mutex.RUnlock()
return fmt.Errorf("Encrypt error(%v)", er2)
}
sig := md5Sign(cipherText, d.key, d.Version)
b := make([]byte, 0, len(sig)+len(d.Version)+len(cipherText))
b = append(b, []byte(d.Version)...)
b = append(b, sig...)
b = append(b, cipherText...)
d.mutex.RUnlock()
select {
case d.tcpChan <- query{cmd, b}:
default:
return errors.New("Device no ready")
}
return nil
}
func (d *Appliance) processResponse(code int, b []byte) {
var i int
for i = 0; i < len(b) && b[i] == byte(0); i++ {
}
b = b[i:]
if len(b) == 0 { // can be an ack
d.device.processResponse(code, b)
return
} // empty
var data []byte
if b[0] == byte('{') {
// Message in clear text
data = b
} else {
encrypted := false
if len(b) > (len(d.Version) + 16) {
// Check if message starts with version number
encrypted = true
for i, vb := range d.Version {
encrypted = encrypted && b[i] == byte(vb)
}
}
if !encrypted {
// can be an error message
log.Println("Resp:", code, string(b))
return
}
var err error
data, err = aesDecrypt(b[len(d.Version)+16:], d.key) // ignore signature
if err != nil {
log.Println("Decrypt error")
return
}
}
d.device.processResponse(code, data)
}
// Send message unencrypted
func (d *Appliance) SendCommand(cmd int, jdata interface{}) error {
data, er1 := json.Marshal(jdata)
if er1 != nil {
return fmt.Errorf("Json Marshal(%v)", er1)
}
select {
case d.tcpChan <- query{cmd, data}:
default:
return errors.New("Device no ready")
}
return nil
}