forked from py60800/tuya
/
msg.go
141 lines (133 loc) · 3.81 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
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
// 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)
}
var b []byte
switch d.Version {
case "3.1":
cipherText, er2 := aesEncrypt(data, d.key, true)
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...)
case "3.3":
cipherText, er2 := aesEncrypt(data, d.key, false)
if er2 != nil {
d.mutex.RUnlock()
return fmt.Errorf("Encrypt error(%v)", er2)
}
padding := "\000\000\000\000\000\000\000\000\000\000\000\000"
b = make([]byte, 0, len(padding)+len(d.Version)+len(cipherText))
b = append(b, []byte(d.Version)...)
b = append(b, padding...)
b = append(b, cipherText...)
default:
return errors.New("Unknown version")
}
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
switch d.Version {
case "3.1":
// ignore 16-byte signature
data, err = aesDecrypt(b[len(d.Version)+16:], d.key, true)
case "3.3":
// https://github.com/codetheweb/tuyapi/blob/5a08481689c5062e17ff9a280d0e51815e2cafb7/lib/cipher.js#L54
data, err = aesDecrypt(b[15:], d.key, false)
default:
log.Fatal("Unknown version")
}
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
}