-
Notifications
You must be signed in to change notification settings - Fork 0
/
daghead.go
82 lines (71 loc) · 2.14 KB
/
daghead.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
/*
daghead: RPL DODAG router app for an OpenWSN 6TiSCH network.
Reads incoming data from root mote, and performs routine management.
* Prints error notifications from mote to daghead log.
* Sets the root mote as DODAG root. Assumes root mote is not already DODAG root.
Presently avoids use of Constrained Join Protocol for network motes by using a
static network key hardcoded into mote firmware.
Since RPL operates in non-storing mode, reads ICMPv6 RPL messages to maintain a
routing table for the network motes.
*/
package main
import (
"github.com/kb2ma/daghead/internal/log"
"github.com/mikepb/go-serial"
toml "github.com/pelletier/go-toml"
"github.com/snksoft/crc"
"sync"
"time"
)
func setDagRoot(wg *sync.WaitGroup, port *serial.Port) {
defer wg.Done()
// Slice [12:28] (16 bytes) should be generated randomly; requires random seed also
data := [31]byte{ 0x7E, 'R', 'T', 0xBB, 0XBB, 0, 0, 0, 0, 0, 0, 0x1, 0x15, 0x38,
0xb6, 0x9a, 0x00, 0xbd, 0xa9, 0x17, 0x14, 0x50, 0x1c, 0xf6,
0x67, 0x76, 0x62, 0xc1, 0, 0, 0x7E }
hash := crc.CalculateCRC(crc.X25, data[1:28])
data[28] = byte(hash & 0xFF)
data[29] = byte((hash & 0xFF00) >> 8)
log.Printf(log.INFO, "setDagRoot % X\n", data)
_, err := port.Write(data[:])
if err != nil {
log.Panic(err)
}
//log.Printf("hash [%X] [%X]\n", hash & 0xFF, (hash & 0xFF00) >> 8)
}
func main() {
// read config file for logging level
config, err := toml.LoadFile("daghead.conf")
if err != nil {
log.Fatal(err)
}
levelStr := config.Get("log.level").(string)
switch levelStr {
case "ERROR":
log.SetLevel(log.ERROR)
case "WARN":
log.SetLevel(log.WARN)
case "DEBUG":
log.SetLevel(log.DEBUG)
default:
log.SetLevel(log.INFO)
}
log.Println(log.INFO, "Starting daghead")
// open serial port to root mote
options := serial.RawOptions
options.BitRate = 19200
options.FlowControl = serial.FLOWCONTROL_XONXOFF
options.Mode = serial.MODE_READ_WRITE
port, err := options.Open("/dev/ttyUSB0")
if err != nil {
log.Panic(err)
}
defer port.Close()
var wg sync.WaitGroup
wg.Add(1)
go readSerial(&wg, port)
time.Sleep(5 * time.Second)
wg.Add(1)
go setDagRoot(&wg, port)
wg.Wait()
}