Skip to content

Commit cb1e6ba

Browse files
committed
fix(serial) keep serial console mux closed when not needed
Only open the serial console mux and broker when the extension is actually in use, otherwise the mux gets in a fight with the ATX or DC control extension in reading from the serial port. Closes: #1420
1 parent 38bb8c0 commit cb1e6ba

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

jsonrpc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,8 @@ func rpcSetActiveExtension(extensionId string) error {
770770
_ = unmountATXControl()
771771
case "dc-power":
772772
_ = unmountDCControl()
773+
case "serial-console":
774+
_ = unmountSerialConsole()
773775
}
774776
config.ActiveExtension = extensionId
775777
if err := SaveConfig(); err != nil {
@@ -780,6 +782,8 @@ func rpcSetActiveExtension(extensionId string) error {
780782
_ = mountATXControl()
781783
case "dc-power":
782784
_ = mountDCControl()
785+
case "serial-console":
786+
_ = mountSerialConsole()
783787
}
784788

785789
// Re-publish MQTT HA Discovery for the new extension

serial.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,18 @@ func initSerialPort() {
580580
_ = mountATXControl()
581581
case "dc-power":
582582
_ = mountDCControl()
583+
case "serial-console":
584+
_ = mountSerialConsole()
583585
}
584586
}
585587

586588
func reopenSerialPort() error {
589+
if serialMux != nil {
590+
serialMux.Close()
591+
}
592+
if consoleBroker != nil {
593+
consoleBroker.Close()
594+
}
587595
if port != nil {
588596
port.Close()
589597
}
@@ -598,6 +606,10 @@ func reopenSerialPort() error {
598606
return err
599607
}
600608

609+
return nil;
610+
}
611+
612+
func mountSerialConsole() error {
601613
// new broker (no sink yet—set it in handleSerialChannel.OnOpen)
602614
norm := NormalizationOptions{
603615
Mode: ModeNames, LineEnding: LineEnding_LF, TabRender: "", PreserveANSI: true,
@@ -619,6 +631,11 @@ func reopenSerialPort() error {
619631
return nil
620632
}
621633

634+
func unmountSerialConsole() error {
635+
_ = reopenSerialPort()
636+
return nil
637+
}
638+
622639
func handleSerialChannel(dataChannel *webrtc.DataChannel) {
623640
scopedLogger := serialLogger.With().
624641
Uint16("data_channel_id", *dataChannel.ID()).Str("service", "serial terminal channel").Logger()

serial_console_helpers.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,16 @@ func NewConsoleBroker(s Sink, norm NormalizationOptions) *ConsoleBroker {
273273
}
274274

275275
func (b *ConsoleBroker) Start() { go b.loop() }
276-
func (b *ConsoleBroker) Close() { close(b.done) }
277276
func (b *ConsoleBroker) SetSink(s Sink) { b.sink = s }
278277
func (b *ConsoleBroker) SetNormOptions(norm NormalizationOptions) { b.norm = norm }
278+
func (b *ConsoleBroker) Close() {
279+
select {
280+
case <-b.done:
281+
return
282+
default:
283+
close(b.done)
284+
}
285+
}
279286
func (b *ConsoleBroker) SetTerminalPaused(v bool) {
280287
if b == nil {
281288
return
@@ -626,7 +633,14 @@ func (m *SerialMux) Start() {
626633
go m.writer()
627634
}
628635

629-
func (m *SerialMux) Close() { close(m.done) }
636+
func (m *SerialMux) Close() {
637+
select {
638+
case <-m.done:
639+
return
640+
default:
641+
close(m.done)
642+
}
643+
}
630644

631645
func (m *SerialMux) SetEchoEnabled(v bool) { m.echoEnabled.Store(v) }
632646

0 commit comments

Comments
 (0)