Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CL: mos licensing implementation PUBLISHED_FROM=60db4b16af57d8938a8922fd78b435d5b34d3995
- Loading branch information
1 parent
35c712c
commit 36ec968
Showing
19 changed files
with
393 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package devutil | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"cesanta.com/common/go/mgrpc/codec" | ||
"cesanta.com/mos/dev" | ||
"cesanta.com/mos/flags" | ||
"cesanta.com/mos/watson" | ||
"github.com/cesanta/errors" | ||
) | ||
|
||
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: *flags.Timeout, Reconnect: *flags.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 *flags.CertFile != "" || | ||
strings.HasPrefix(port, "wss") || | ||
strings.HasPrefix(port, "https") || | ||
strings.HasPrefix(port, "mqtts") { | ||
|
||
tlsConfig, err = flags.TLSConfigFromFlags() | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
} | ||
|
||
codecOpts := &codec.Options{ | ||
AzureDM: codec.AzureDMCodecOptions{ | ||
ConnectionString: *flags.AzureConnectionString, | ||
}, | ||
MQTT: codec.MQTTCodecOptions{}, | ||
Serial: codec.SerialCodecOptions{ | ||
BaudRate: uint(*flags.BaudRate), | ||
HardwareFlowControl: *flags.HWFC, | ||
JunkHandler: junkHandler, | ||
SetControlLines: *flags.SetControlLines, | ||
InvertedControlLines: *flags.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 && | ||
!*flags.RPCUARTNoDelay { | ||
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, *flags.Reconnect, tlsConfig, codecOpts) | ||
return devConn, errors.Trace(err) | ||
} | ||
|
||
func CreateDevConnFromFlags(ctx context.Context) (*dev.DevConn, error) { | ||
return createDevConnWithJunkHandler(ctx, func(junk []byte) {}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
package main | ||
package devutil | ||
|
||
import ( | ||
"github.com/cesanta/errors" | ||
|
||
"cesanta.com/common/go/ourutil" | ||
"cesanta.com/mos/flags" | ||
) | ||
|
||
var defaultPort string | ||
|
||
func getPort() (string, error) { | ||
if *portFlag != "auto" { | ||
return *portFlag, nil | ||
func GetPort() (string, error) { | ||
if *flags.Port != "auto" { | ||
return *flags.Port, nil | ||
} | ||
if defaultPort == "" { | ||
defaultPort = getDefaultPort() | ||
if defaultPort == "" { | ||
return "", errors.Errorf("--port not specified and none were found") | ||
} | ||
reportf("Using port %s", defaultPort) | ||
ourutil.Reportf("Using port %s", defaultPort) | ||
} | ||
return defaultPort, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package devutil | ||
|
||
import ( | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
func EnumerateSerialPorts() []string { | ||
list, _ := filepath.Glob("/dev/cu.*") | ||
var filteredList []string | ||
for _, s := range list { | ||
if !strings.Contains(s, "Bluetooth-") && | ||
!strings.Contains(s, "-SPPDev") && | ||
!strings.Contains(s, "-WirelessiAP") { | ||
filteredList = append(filteredList, s) | ||
} | ||
} | ||
sort.Strings(filteredList) | ||
return filteredList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package devutil | ||
|
||
import ( | ||
"path/filepath" | ||
"sort" | ||
) | ||
|
||
func EnumerateSerialPorts() []string { | ||
// Note: Prefer ttyUSB* to ttyACM*. | ||
list1, _ := filepath.Glob("/dev/ttyUSB*") | ||
sort.Strings(list1) | ||
list2, _ := filepath.Glob("/dev/ttyACM*") | ||
sort.Strings(list2) | ||
return append(list1, list2...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.