-
Notifications
You must be signed in to change notification settings - Fork 15
/
devutil.go
127 lines (113 loc) · 3.83 KB
/
devutil.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
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"io/ioutil"
"runtime"
"strings"
"time"
"context"
"cesanta.com/common/go/mgrpc/codec"
"cesanta.com/mos/dev"
"cesanta.com/mos/watson"
"github.com/cesanta/errors"
)
var (
azureConnectionString = ""
certFile = ""
keyFile = ""
caFile = ""
rpcUARTNoDelayFlag = false
)
func init() {
flag.StringVar(&azureConnectionString, "azure-connection-string", "", "Azure connection string")
flag.StringVar(&certFile, "cert-file", "", "Certificate file name")
flag.StringVar(&keyFile, "key-file", "", "Key file name")
flag.StringVar(&caFile, "ca-cert-file", "", "CA cert for TLS server verification")
flag.BoolVar(&rpcUARTNoDelayFlag, "rpc-uart-no-delay", false, "Do not introduce delay into UART over RPC")
hiddenFlags = append(hiddenFlags, "ca-cert-file")
}
func createDevConn(ctx context.Context) (*dev.DevConn, error) {
return createDevConnWithJunkHandler(ctx, func(junk []byte) {})
}
func tlsConfigFromFlags() (*tls.Config, error) {
tlsConfig := &tls.Config{
// TODO(rojer): Ship default CA bundle with mos.
InsecureSkipVerify: caFile == "",
}
// Load client cert / key if specified
if certFile != "" && keyFile == "" {
return nil, errors.Errorf("Please specify --key-file")
}
if certFile != "" {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, errors.Trace(err)
}
tlsConfig.Certificates = []tls.Certificate{cert}
}
// Load CA cert if specified
if caFile != "" {
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
return nil, errors.Trace(err)
}
tlsConfig.RootCAs = x509.NewCertPool()
tlsConfig.RootCAs.AppendCertsFromPEM(caCert)
}
return tlsConfig, nil
}
func createDevConnWithJunkHandler(ctx context.Context, junkHandler func(junk []byte)) (*dev.DevConn, error) {
port, err := getPort()
if err != nil {
return nil, errors.Trace(err)
}
c := dev.Client{Port: port, Timeout: *timeout, Reconnect: *reconnect}
prefix := "serial://"
if strings.Index(port, "://") > 0 {
prefix = ""
}
addr := prefix + port
// Init and pass TLS config if --cert-file and --key-file are specified
var tlsConfig *tls.Config
if certFile != "" || strings.HasPrefix(port, "wss") || strings.HasPrefix(port, "https") || strings.HasPrefix(port, "mqtts") {
tlsConfig, err = tlsConfigFromFlags()
if err != nil {
return nil, errors.Trace(err)
}
}
codecOpts := &codec.Options{
AzureDM: codec.AzureDMCodecOptions{
ConnectionString: azureConnectionString,
},
MQTT: codec.MQTTCodecOptions{},
Serial: codec.SerialCodecOptions{
BaudRate: baudRateFlag,
HardwareFlowControl: hwFCFlag,
JunkHandler: junkHandler,
SetControlLines: setControlLines,
InvertedControlLines: *invertedControlLines,
},
Watson: codec.WatsonCodecOptions{
APIKey: watson.WatsonAPIKeyFlag,
APIAuthToken: watson.WatsonAPIAuthTokenFlag,
},
}
// Due to lack of flow control, we send data in chunks and wait after each.
// At non-default baud rate we assume user knows what they are doing.
if !codecOpts.Serial.HardwareFlowControl && codecOpts.Serial.BaudRate == 115200 && !rpcUARTNoDelayFlag {
codecOpts.Serial.SendChunkSize = 16
codecOpts.Serial.SendChunkDelay = 5 * time.Millisecond
// So, this is weird. ST-Link serial device seems to have issues on Mac OS X if we write too fast.
// Yes, even 16 bytes at 5 ms interval is too fast, and 8 bytes too. It looks like this:
// processes trying to access /dev/cu.usbmodemX get stuck and the only re-plugging
// (or re-enumerating) gets it out of this state.
// Hence, the following kludge.
if runtime.GOOS == "darwin" && strings.Contains(port, "cu.usbmodem") {
codecOpts.Serial.SendChunkSize = 6
}
}
devConn, err := c.CreateDevConnWithOpts(ctx, addr, *reconnect, tlsConfig, codecOpts)
return devConn, errors.Trace(err)
}