discovery

package module
v0.0.0-...-a501779 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2021 License: GPL-2.0 Imports: 9 Imported by: 1

README

= Arduino Board Discovery (Golang)
Alessandro Sanino <a.sanino@bcmi-labs.cc>

This package allows to discover and monitor Arduino boards connected to the network or the serial ports.

=== Getting the package
[source, bash]
----
$ go get github.com/arduino/board-discovery
----
Then import it to use it:
[source, go]
----
import "github.com/arduino/board-discovery"
----

====== GODOC can be found link:https://godoc.org/github.com/arduino/board-discovery[HERE]

== Discovering devices
To discover devices you must first create a new Monitor (specifying the polling interval):
[source, go]
----
monitor := discovery.New(time.Millisecond)
----
Then start, get data and stop it when you don't need it anymore:
[source, go]
----
monitor.Start()
//Get devices connected
monitor.Stop()
----
You can get connected devices (via Serial or Network ports) by using the following functions:
[source, go]
----
serialDevices := monitor.Serial()
networkDevices := monitor.Network()
----

=== The Monitor

Monitor periodically (period specified by *Interval*) checks the serial ports and the network in order to have
an updated list of Serial and Network ports.

You can subscribe to the *Events* channel to get realtime notification of what's changed.
[source, go]
----
type Monitor struct {
	Interval time.Duration
	Events   chan Event
}
----

You can `Start`, `Stop` and `Restart` a Monitor, using the specified functions.
[source, go]
----
monitor.Start() // Starts monitoring
monitor.Stop()  // Stops monitoring
monitor.Restart // Restarts the monitor (Stop, then Start)
----

To have the list of devices discovered, you can use (assuming a started monitor `m`):

 . `m.Serial()` to get the list of devices connected via serial interface.
 . `m.Network()` to get the list of devices connected via the network.

=== The device types

`SerialDevice` represents a device connected via serial port:
[source, go]
----
type SerialDevice struct {
	Port         string
	SerialNumber string
	ProductID    string
	VendorID     string 
	Serial       *serial.Info
}

type SerialDevices map[string]*SerialDevice
----

`NetworkDevice` represents a device connected via the network:
[source, go]
----
type NetworkDevice struct {
	Address string
	Info    string
	Name    string
	Port    int
}

type NetworkDevices map[string]*NetworkDevice
----

== Security

If you think you found a vulnerability or other security-related bug in this project, please read our
https://github.com/arduino/board-discovery/security/policy[security policy] and report the bug to our Security Team 🛡️
Thank you!

e-mail contact: security@arduino.cc

Documentation

Overview

Package discovery keeps an updated list of the devices connected to the computer, via serial ports or found in the network

Usage:

monitor := discovery.New(time.Millisecond)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
monitor.Start(ctx)
time.Sleep(10 * time.Second)
fmt.Println(monitor.Serial())
fmt.Println(monitor.Network())

Output:

map[/dev/ttyACM0:0x2341/0x8036]
map[192.168.1.107:YunShield]

You may also decide to subscribe to the Events channel of the Monitor:

monitor := discovery.New(time.Millisecond)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
monitor.Start(ctx)
for ev := range monitor.Events {
	fmt.Println(ev)
}

Output:

{add 0x2341/0x8036 <nil>}
{add <nil> YunShield}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Name          string         `json:"name"`
	SerialDevice  *SerialDevice  `json:"serial_device,omitempty"`
	NetworkDevice *NetworkDevice `json:"network_device,omitempty"`
}

Event tells you that something has changed in the list of connected devices. Name can be one of ["Add", "Change", "Remove"] SerialDevice or NetworkDevice can be present and they refer to the device that has been added, changed, or removed

type Monitor

type Monitor struct {
	Interval time.Duration
	// contains filtered or unexported fields
}

Monitor periodically checks the serial ports and the network in order to have an updated list of Serial and Network ports.

You can subscribe to the Events channel to get realtime notification of what's changed

func New

func New(interval time.Duration) *Monitor

New Creates a new monitor that can start querying the serial ports and the local network for devices

func (Monitor) MarshalJSON

func (m Monitor) MarshalJSON() ([]byte, error)

MarshalJSON allows a monitor to be parsed as a JSON object.

func (*Monitor) Network

func (m *Monitor) Network() NetworkDevices

Network returns a cached list of devices found on the local network

func (*Monitor) Restart

func (m *Monitor) Restart()

Restart restarts the monitor.

func (*Monitor) Serial

func (m *Monitor) Serial() SerialDevices

Serial returns a cached list of devices connected to the serial ports

func (*Monitor) Start

func (m *Monitor) Start()

Start begins the loop that queries the serial ports and the local network.

func (*Monitor) Stop

func (m *Monitor) Stop()

Stop stops the monitor, waiting for the last operation to be completed.

func (Monitor) String

func (m Monitor) String() string

type NetworkDevice

type NetworkDevice struct {
	Address string `json:"address"`
	Info    string `json:"info"`
	Name    string `json:"name"`
	Port    int    `json:"port"`
}

NetworkDevice is something connected to the Network Ports

func (NetworkDevice) String

func (d NetworkDevice) String() string

type NetworkDevices

type NetworkDevices map[string]*NetworkDevice

NetworkDevices is a list of currently connected devices to the computer

func (NetworkDevices) MarshalJSON

func (nds NetworkDevices) MarshalJSON() ([]byte, error)

MarshalJSON allows to convert this object to a JSON string.

func (NetworkDevices) String

func (nds NetworkDevices) String() string

type SerialDevice

type SerialDevice struct {
	Port         string `json:"port"`
	SerialNumber string `json:"serial_number"`
	ProductID    string `json:"pid"`
	VendorID     string `json:"vid"`
}

SerialDevice is something connected to the Serial Ports

func (SerialDevice) String

func (d SerialDevice) String() string

type SerialDevices

type SerialDevices map[string]*SerialDevice

SerialDevices is a list of currently connected devices to the computer

func (SerialDevices) MarshalJSON

func (sds SerialDevices) MarshalJSON() ([]byte, error)

MarshalJSON allows to convert this object to a JSON string.

func (SerialDevices) String

func (sds SerialDevices) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL