Skip to content

Commit c0bcc78

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 c0bcc78

3 files changed

Lines changed: 39 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,20 @@ 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+
serialMux = nil
592+
}
593+
if consoleBroker != nil {
594+
consoleBroker.Close()
595+
consoleBroker = nil
596+
}
587597
if port != nil {
588598
port.Close()
589599
}
@@ -598,6 +608,10 @@ func reopenSerialPort() error {
598608
return err
599609
}
600610

611+
return nil
612+
}
613+
614+
func mountSerialConsole() error {
601615
// new broker (no sink yet—set it in handleSerialChannel.OnOpen)
602616
norm := NormalizationOptions{
603617
Mode: ModeNames, LineEnding: LineEnding_LF, TabRender: "", PreserveANSI: true,
@@ -619,6 +633,11 @@ func reopenSerialPort() error {
619633
return nil
620634
}
621635

636+
func unmountSerialConsole() error {
637+
_ = reopenSerialPort()
638+
return nil
639+
}
640+
622641
func handleSerialChannel(dataChannel *webrtc.DataChannel) {
623642
scopedLogger := serialLogger.With().
624643
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)