| 1 | // This file is part of dummy-monitor. |
| 2 | // |
| 3 | // SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies |
| 4 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | |
| 6 | package main |
| 7 | |
| 8 | import ( |
| 9 | "io" |
| 10 | ) |
| 11 | |
| 12 | type dummyPort struct { |
| 13 | echo chan string |
| 14 | portName string |
| 15 | } |
| 16 | |
| 17 | func newDummyPort(portName string) *dummyPort { |
| 18 | return &dummyPort{ |
| 19 | echo: make(chan string, 20), |
| 20 | portName: portName, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func (d *dummyPort) Close() error { |
| 25 | close(d.echo) |
| 26 | return nil |
| 27 | } |
| 28 | |
| 29 | func (d *dummyPort) Read(p []byte) (int, error) { |
| 30 | data, ok := <-d.echo |
| 31 | if !ok { |
| 32 | return 0, io.EOF |
| 33 | } |
| 34 | n := copy(p, []byte(data)) |
| 35 | // if p is too small data loss may happen.. ¯\_(ツ)_/¯ |
| 36 | return n, nil |
| 37 | } |
| 38 | |
| 39 | func (d *dummyPort) Write(p []byte) (int, error) { |
| 40 | d.echo <- string(p) |
| 41 | return len(p), nil |
| 42 | } |