summaryrefslogtreecommitdiff
path: root/hs100.go
blob: c972f8a5ae06ec7abef92e471b88d357ea303514 (plain)
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
// vim: shiftwidth=4 tabstop=4 noexpandtab

package main

import (
	"context"
	"encoding/binary"
	"fmt"
	"net"
	"time"
)

const (
	MAX_PAYLOAD = 4294967295 // TP-Link WiFi plug protocol: max. 2^32-1 bytes
)

// Hs100 bundles every data associated with one TP-Link HS100 smart plug.
type Hs100 struct {
	Config Hs100Conf
}

// Hs100Conf is the configuration of one TP-Link HS100 smart plug.
type Hs100Conf struct {
	Ip net.IP
	Name string
}

// encrypt() encrypts data for a TP-Link WiFi plug.
func encrypt(data []byte) ([]byte, error) {

	// assert maximum payload size to cast data length safely
	if len(data) > MAX_PAYLOAD {
		return []byte{}, fmt.Errorf("Too many bytes to encrypt (%d > %d)!\n",
			len(data), MAX_PAYLOAD)
	}
	length := uint32(len(data))

	// encode payload length as header
	out := make([]byte, 4) // header buffer
	binary.BigEndian.PutUint32(out, length)

	// encryption algorithm
	key := byte(171)
	for _, value := range data {
		key = key ^ value
		out = append(out, byte(key))
	}

	return out, nil
}

// decrypt() decrypts data coming from a TP-Link WiFi plug.
func decrypt(data []byte) []byte {

	// TODO check if length given in header is correct

	// cut-off header
	data = data[4:]

	// decryption algorithm
	key := byte(171)
	for index, value := range data {
		data[index] = key ^ value
		key = value
	}

	return data
}

// send() sends data via TCP to an address (like "192.168.1.42:9999").
func send(address string, data []byte) error {

	// create a Dialer with context
	var d net.Dialer
	ctx, cancel := context.WithTimeout(context.Background(), 3 * time.Second)
	defer cancel()

	// establish connection
	conn, err := d.DialContext(ctx, "tcp", address)
	if err != nil {
		return fmt.Errorf("Failed to dial: %v", err)
	}
	defer conn.Close()

	// writing data
	_, err = conn.Write(data)
	if err != nil {
		return fmt.Errorf("Could not write data: %v", err)
	}

	return nil

}

// set() sets the relay state of a TP-Link WiFi plug.
func set(host string, state string) error {

	cmd := ""

	// modify command according to state
	if state == "on" {
		cmd = `{"system":{"set_relay_state":{"state":1}}}`
	} else if state == "off" {
		cmd = `{"system":{"set_relay_state":{"state":0}}}`
	} else {
		return fmt.Errorf("set() just accepts values 'on' and 'off'!")
	}

	// format address, encrypt data and send it
	address := fmt.Sprintf("%s:9999", host)
	data, err := encrypt([]byte(cmd))
	if err != nil {
		return err
	}
	err = send(address, data)
	
	return err
}