rfkill

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2019 License: MIT Imports: 7 Imported by: 1

README

rfkill

A golang client for rfkill. It supports reading available rfkill devices, subscribing to events and blocking/unblocking devices.

Documentation

Detailed API documentation can be found here.

Usage

package main

import (
	"fmt"
	"os"

	"github.com/amenzhinsky/rfkill"
)

func main() {
	if err := rfkill.Each(func(ev rfkill.Event) error {
		if ev.Soft == 0 {
			return nil
		}
		name, err := rfkill.NameByIdx(ev.Idx)
		if err != nil {
			return err
		}
		fmt.Printf("unblocking: %s\n", name)
		return rfkill.BlockByIdx(ev.Idx, false)
	}); err != nil {
		fmt.Fprintf(os.Stderr, "error: %s\n", err)
		os.Exit(1)
	}
}

Documentation

Overview

This is a rfkill client library for golang, works only on linux.

For implementation details see: https://github.com/torvalds/linux/blob/master/include/uapi/linux/rfkill.h

Index

Constants

View Source
const (
	// OpAdd a device is added.
	OpAdd = iota

	// OpDel a device is deleted.
	OpDel

	// OpChange a device's state is changed.
	OpChange

	// OpChangeAll userspace changes in all devices.
	OpChangeAll
)
View Source
const (
	// TypeAll toggles all switches, useless in this library.
	TypeAll = iota

	// TypeWLAN switch is on a 802.11 wireless network device.
	TypeWLAN

	// TypeBluetooth switch is on a bluetooth device.
	TypeBluetooth

	// TypeUWB switch is on a ultra wideband device.
	TypeUWB

	// TypeWiMAX switch is on a WiMAX device.
	TypeWiMAX

	// TypeWWAN switch is on a wireless WAN device.
	TypeWWAN

	// TypeGPS switch is on a GPS device.
	TypeGPS

	// TypeFM switch is on a FM radio device.
	TypeFM

	// TypeNFC switch is on an NFC device.
	TypeNFC
)

Variables

View Source
var ErrClosed = errors.New("rfkill: closed")

ErrClosed denotes closed watcher.

Functions

func BlockByIdx

func BlockByIdx(idx uint32, block bool) error

BlockByIdx soft blocks or unblocks a device by the given idx.

func Each

func Each(fn func(ev Event) error) error

Each iterates over all registered devices yielding them as OpAdd events. If fn returns an error the function immediately propagates it.

Example how to unblock all devices:

if err := rfkill.Each(func(ev rfkill.Event) error {
	return rfkill.BlockByIdx(ev.Idx, false)
}); err != nil {
	return err
}

func NameByIdx

func NameByIdx(idx uint32) (string, error)

NameByIdx returns system name for the named device idx.

The value is read from /sys/class/rfkill/rfkill{IDX}/name.

Types

type Event

type Event struct {
	// Idx is device index.
	Idx uint32

	// Type of the event.
	Type Type

	// Op operation code.
	Op Op

	// Soft state.
	Soft uint8

	// Hard state.
	Hard uint8
}

Event is a rfkill event read from /dev/rfkill.

type Op

type Op uint8

Op is operation type.

func (Op) String

func (op Op) String() string

type Type

type Type uint8

Type is type of rfkill switch.

func (Type) String

func (typ Type) String() string

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher is a event watching instance.

func Watch

func Watch(ops ...Op) (*Watcher, error)

Watch monitors the rfkill events.

If ops is not empty it acts as a filter, otherwise it delivers everything.

Example:

w, err := rfkill.Watch()
if err != nil {
	return err
}
defer w.Close()

for ev := range w.C() {
	fmt.Printf("idx=%d type=%s soft=%t hard=%t",
		ev.Idx, ev.Type, ev.Soft != 0, ev.Hard != 0)
}
if err = w.Err(); err != nil {
	return err
}

func (*Watcher) C

func (w *Watcher) C() <-chan Event

C is a rfkill events stream.

func (*Watcher) Close

func (w *Watcher) Close() error

Close makes the watcher to stop automatically closing the events stream channel.

func (*Watcher) Err

func (w *Watcher) Err() error

Err is the watcher's error, it makes sense to call it only after the channel returned from C gets closed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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