github.com/la5nta/wl2k-go@v0.11.8/transport/ardop/ardop.go (about)

     1  // Copyright 2015 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
     2  // Use of this source code is governed by the MIT-license that can be
     3  // found in the LICENSE file.
     4  
     5  // Package ardop provides means of establishing a connection to a remote node using ARDOP TNC
     6  package ardop
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  	"os"
    12  	"strings"
    13  	"time"
    14  )
    15  
    16  const (
    17  	DefaultAddr       = "localhost:8515" // The default address Ardop TNC listens on
    18  	DefaultARQTimeout = 90 * time.Second // The default ARQ session idle timout
    19  )
    20  
    21  const (
    22  	ModeARQ = "ARQ" // ARQ mode
    23  	ModeFEC = "FEC" // FEC mode
    24  )
    25  
    26  // TNC states
    27  const (
    28  	//go:generate stringer -type=State .
    29  	Unknown      State = iota
    30  	Offline            // Sound card disabled and all sound card resources are released
    31  	Disconnected       // The session is disconnected, the sound card remains active
    32  	ISS                // Information Sending Station (Sending Data)
    33  	IRS                // Information Receiving Station (Receiving data)
    34  	Idle               // ??
    35  	FECSend            // ??
    36  	FECReceive         // Receiving FEC (unproto) data
    37  )
    38  
    39  var (
    40  	ErrBusy                 = errors.New("TNC control port is busy.")
    41  	ErrConnectInProgress    = errors.New("A connect is in progress.")
    42  	ErrFlushTimeout         = errors.New("Flush timeout.")
    43  	ErrActiveListenerExists = errors.New("An active listener is already registered with this TNC.")
    44  	ErrDisconnectTimeout    = errors.New("Disconnect timeout: aborted connection.")
    45  	ErrConnectTimeout       = errors.New("Connect timeout")
    46  	ErrChecksumMismatch     = errors.New("Control protocol checksum mismatch")
    47  	ErrTNCClosed            = errors.New("TNC closed")
    48  	ErrUnsupportedBandwidth = errors.New("Unsupported ARQ bandwidth")
    49  )
    50  
    51  // Bandwidth definitions of all supported ARQ bandwidths.
    52  var (
    53  	Bandwidth200Max     = Bandwidth{false, 200}
    54  	Bandwidth500Max     = Bandwidth{false, 500}
    55  	Bandwidth1000Max    = Bandwidth{false, 1000}
    56  	Bandwidth2000Max    = Bandwidth{false, 2000}
    57  	Bandwidth200Forced  = Bandwidth{true, 200}
    58  	Bandwidth500Forced  = Bandwidth{true, 500}
    59  	Bandwidth1000Forced = Bandwidth{true, 1000}
    60  	Bandwidth2000Forced = Bandwidth{true, 2000}
    61  )
    62  
    63  type State uint8
    64  
    65  // Bandwidth represents the ARQ bandwidth.
    66  type Bandwidth struct {
    67  	Forced bool // Force use of max bandwidth.
    68  	Max    uint // Max bandwidh to use.
    69  }
    70  
    71  // Stringer for Bandwidth returns a valid bandwidth parameter that can be sent to the TNC.
    72  func (bw Bandwidth) String() string {
    73  	str := fmt.Sprintf("%d", bw.Max)
    74  	if bw.Forced {
    75  		str += "FORCED"
    76  	} else {
    77  		str += "MAX"
    78  	}
    79  	return str
    80  }
    81  
    82  // IsZero returns true if bw is it's zero value.
    83  func (bw Bandwidth) IsZero() bool { return bw.Max == 0 }
    84  
    85  // BandwidthFromString returns a Bandwidth representation of the given string.
    86  //
    87  // The string must be a valid ARDOP ARQ bandwidth string (e.g. "2000MAX", "2000FORCED").
    88  // The MAX/FORCED suffix may be omitted. Defaults to MAX.
    89  func BandwidthFromString(str string) (Bandwidth, error) {
    90  	//  Default to MAX if MAX/FORCED is not given.
    91  	if strings.HasSuffix(str, "0") {
    92  		str += "MAX"
    93  	}
    94  	for _, bw := range Bandwidths() {
    95  		if bw.String() == str {
    96  			return bw, nil
    97  		}
    98  	}
    99  	return Bandwidth{}, ErrUnsupportedBandwidth
   100  }
   101  
   102  // Bandwidths returns a list of all ARDOP ARQ bandwidths.
   103  func Bandwidths() []Bandwidth {
   104  	return []Bandwidth{
   105  		Bandwidth200Max,
   106  		Bandwidth500Max,
   107  		Bandwidth1000Max,
   108  		Bandwidth2000Max,
   109  		Bandwidth200Forced,
   110  		Bandwidth500Forced,
   111  		Bandwidth1000Forced,
   112  		Bandwidth2000Forced,
   113  	}
   114  }
   115  
   116  var stateMap = map[string]State{
   117  	"":        Unknown,
   118  	"OFFLINE": Offline,
   119  	"DISC":    Disconnected,
   120  	"ISS":     ISS,
   121  	"IRS":     IRS,
   122  	"IDLE":    Idle,
   123  	"FECRcv":  FECReceive,
   124  	"FECSend": FECSend,
   125  }
   126  
   127  func strToState(str string) (State, bool) {
   128  	state, ok := stateMap[strings.ToUpper(str)]
   129  	return state, ok
   130  }
   131  
   132  func debugEnabled() bool {
   133  	return os.Getenv("ARDOP_DEBUG") != ""
   134  }