add method to COM to shutdown gracefully

master
Florian Thienel 3 years ago
parent 2af6ef6e27
commit 978f47a1c5

@ -26,6 +26,7 @@ func New(device io.ReadWriter) *COM {
commands := make(chan command) commands := make(chan command)
result := &COM{ result := &COM{
commands: commands, commands: commands,
closing: make(chan struct{}),
closed: make(chan struct{}), closed: make(chan struct{}),
indications: make(map[string]indicationConfig), indications: make(map[string]indicationConfig),
} }
@ -43,6 +44,8 @@ func New(device io.ReadWriter) *COM {
for { for {
select { select {
case <-result.closing:
return
case line, valid := <-lines: case line, valid := <-lines:
if !valid { if !valid {
return return
@ -102,6 +105,7 @@ func New(device io.ReadWriter) *COM {
// COM allows to communicate with a radio's PEI using AT commands. // COM allows to communicate with a radio's PEI using AT commands.
type COM struct { type COM struct {
commands chan<- command commands chan<- command
closing chan struct{}
closed chan struct{} closed chan struct{}
tracer io.Writer tracer io.Writer
@ -148,6 +152,14 @@ func readLoop(r io.Reader) <-chan string {
return lines return lines
} }
func (c *COM) Close() {
select {
case <-c.closing:
default:
close(c.closing)
}
}
func (c *COM) Closed() bool { func (c *COM) Closed() bool {
select { select {
case <-c.closed: case <-c.closed:
@ -157,6 +169,13 @@ func (c *COM) Closed() bool {
} }
} }
func (c *COM) WaitUntilClosed(ctx context.Context) {
select {
case <-c.closed:
case <-ctx.Done():
}
}
func (c *COM) AddIndication(prefix string, trailingLines int, handler func(lines []string)) error { func (c *COM) AddIndication(prefix string, trailingLines int, handler func(lines []string)) error {
config := indicationConfig{ config := indicationConfig{
prefix: strings.ToUpper(prefix), prefix: strings.ToUpper(prefix),

Loading…
Cancel
Save