phy

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package phy is our physical layer for the IEC 62056-7-6 profile, after IEC 62056-42 (Ed 1.0, 2002): the PH-CONNECT, PH-ABORT and PH-DATA services over a byte stream (a serial-to-Ethernet converter, a direct line or a Hayes modem on the PSTN), the Hayes procedures of its annex A for dialling, answering and hanging up, and the optional identification service of 6.3.3 on both sides. Clause numbers are those of IEC 62056-42 unless stated.

Index

Examples

Constants

View Source
const (
	IdentifyRequestID    = 0x20 // IDENTIFY-Request-ID
	IdentifyRequestIDAlt = 0x49 // 'I', accepted for existing implementations (footnote 1)
)

Identification message values (6.3.3.2).

Variables

View Source
var COSEM = Identity{Success: 0x00, Protocol: 0x04, Version: 0x01, Revision: 0x00}

COSEM is the answer 6.3.3.2 prescribes for this stack (the NEMA C12.21 codes): success 0x00, protocol 0x04, version 0x01, revision 0x00.

View Source
var ErrConnect = errors.New("the physical connection was not established")

ErrConnect is a PH-CONNECT.confirm with a negative result.

View Source
var ErrNotIdentify = errors.New("not an identification request")

ErrNotIdentify is returned for bytes that are not an IDENTIFY.request.

Functions

func Answer

func Answer(c net.Conn, timeout time.Duration) (int, []byte, error)

Answer is the called side's PH-CONNECT (annex A.2.3) on a line from a Hayes modem not in auto-answer mode: wait for RING, send ATA, and take CONNECT as PH-CONNECT.indication. It returns the rate CONNECT reports and the octets that already followed it (the start of the data).

func IdentifyRequest

func IdentifyRequest(device *uint16) []byte

IdentifyRequest is the IDENTIFY.request: one octet, or three with the multidrop device ID of one physical device (F-1138: we send the device's lower HDLC address as a 16-bit number, most significant octet first).

Example

The identification request before HDLC, to any device and to the device with lower HDLC address 0x11 on a multidrop line.

package main

import (
	"fmt"

	"github.com/cB-Sigma/dlms/phy"
)

func main() {
	fmt.Printf("% X\n", phy.IdentifyRequest(nil))
	dev := uint16(0x11)
	fmt.Printf("% X\n", phy.IdentifyRequest(&dev))
}
Output:
20
20 00 11

func ParseIdentifyRequest

func ParseIdentifyRequest(b []byte) (device *uint16, err error)

ParseIdentifyRequest reads a request of one or three octets and returns its multidrop device ID, if any.

func ServerIdentify

func ServerIdentify(c net.Conn, id Identity, device *uint16, interOctet time.Duration) ([]byte, error)

ServerIdentify is the server side physical layer's identification stage (6.3.3.3, Figure 6) on a connection just set up. It waits for the first octet, gathers octets until a gap longer than interOctet: up to three octets are an IDENTIFY.request, answered with id when it is one and addressed to us (device is our multidrop device ID, nil when we have none), then it waits again; a fourth octet ends identification for good, and the octets so far are returned to go to the data link layer (NOTE 3: a client not using identification starts with a longer frame). A malformed request is discarded without an answer.

func ServerIdentifyFunc

func ServerIdentifyFunc(c net.Conn, id Identity, own func(*uint16) bool, interOctet time.Duration) ([]byte, error)

ServerIdentifyFunc is ServerIdentify for a line with several physical devices: own reports whether a request (with its device ID, or nil) is for a device on it.

Types

type Config

type Config struct {
	// Open opens the line: a TCP connection to a serial converter, or to a
	// converter with a Hayes modem behind it.
	Open func() (net.Conn, error)
	// Dial, when set, is the number the modem dials (annex A.2.1); Init
	// are Hayes commands sent first, each answered OK.
	Dial string
	Init []string
	// Guard is the escape guard time (annex A.2.5, default 1 s);
	// CommandTimeout bounds a Hayes command (default 5 s), ConnectTimeout
	// the dialling (default 60 s).
	Guard, CommandTimeout, ConnectTimeout time.Duration
	// Identify runs the identification service after the physical
	// connection is set up (6.3.3), for Device when set (a multidrop
	// physical device). Expect is the answer we take (default COSEM).
	Identify bool
	Device   *uint16
	Expect   *Identity
	// IdentifyTimeout waits for the response (default 2 s: the server has
	// 1 500 ms).
	IdentifyTimeout time.Duration
	// OnAbort is PH-ABORT.indication to the physical connection manager:
	// the line went down without being asked to.
	OnAbort func(error)
}

Config sets up a client Port.

type Connection

type Connection struct {
	Rate     int       // the rate the modem's CONNECT named, 0 when none or no modem
	Identity *Identity // the identification response, when the service was used
}

Connection is what PH-CONNECT.confirm reported.

type Identity

type Identity struct {
	Success, Protocol, Version, Revision byte
}

Identity is an IDENTIFY.response: success code, then the identifier, version and revision of the protocol stack.

func Identify

func Identify(c net.Conn, device *uint16, timeout time.Duration) (Identity, []byte, error)

Identify runs the identification service on a connection nothing else reads yet (a meter that called us); it also returns octets that came after the response.

func ParseIdentity

func ParseIdentity(b []byte) (Identity, error)

ParseIdentity reads a four-octet response.

func (Identity) Bytes

func (id Identity) Bytes() []byte

Bytes is the response as sent: four octets, not A-XDR.

func (Identity) String

func (id Identity) String() string

String describes the identification answer.

type ModemSim

type ModemSim struct {
	Number string
	Rate   int           // reported in CONNECT (default 9600)
	Busy   bool          // the number is busy
	Guard  time.Duration // escape guard time (default 1 s)
	// Remote serves the far end of a call on its connection and returns
	// when that connection ends.
	Remote func(net.Conn)
	// contains filtered or unexported fields
}

ModemSim plays an intelligent Hayes modem and the line behind it, for tests and the meter simulator: AT commands are answered OK, ATD with Number connects to Remote (CONNECT and the rate), other numbers get NO CARRIER (or BUSY); in data mode octets pass to and from Remote until the escape sequence (guard time, "+++", guard time) returns to command mode, where ATH ends the call (NO CARRIER). Call rings a client instead (RING, ATA, CONNECT).

func (*ModemSim) Call

func (m *ModemSim) Call(c net.Conn) error

Call rings the client on c (a called client, annex A.2.3): RING until ATA, then CONNECT, and the call is passed to Remote until c closes.

func (*ModemSim) Calls

func (m *ModemSim) Calls() (calls, hangups int)

Calls is how many calls were connected, Hangups how many ATH ended one.

func (*ModemSim) Commands

func (m *ModemSim) Commands() []string

Commands are the command lines received.

func (*ModemSim) Serve

func (m *ModemSim) Serve(c net.Conn) error

Serve plays the modem on the DTE's connection c until it closes.

type Port

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

Port is the client side physical layer (6.3, Figure 7) as a gosem transport: Connect is PH-CONNECT.request (open the line, dial, identify), Send is PH-DATA.request, received octets are PH-DATA.indication to the data link layer once identification is over (the Destination_process parameter of 6.3.3.3), Disconnect is PH-ABORT.request (hang up, close).

func NewPort

func NewPort(cfg Config) (*Port, error)

NewPort builds a client physical layer.

func (*Port) Close

func (p *Port) Close()

Close disconnects and closes the reception channel.

func (*Port) Connect

func (p *Port) Connect() error

Connect is PH-CONNECT.request.

func (*Port) Connection

func (p *Port) Connection() Connection

Connection is the last PH-CONNECT.confirm.

func (*Port) Disconnect

func (p *Port) Disconnect() error

Disconnect is PH-ABORT.request: over a modem, escape and hang up (annex A.2.5), then close the line. It is confirmed locally.

func (*Port) IsConnected

func (p *Port) IsConnected() bool

IsConnected reports a physical connection.

func (*Port) Send

func (p *Port) Send(b []byte) error

Send is PH-DATA.request for each octet.

func (*Port) SetAddress

func (p *Port) SetAddress(int, int)

SetAddress does nothing: the physical layer has no addresses (the gosem transport interface asks for it).

func (*Port) SetLogger

func (p *Port) SetLogger(*log.Logger)

SetLogger does nothing: the port does not log (the gosem transport interface asks for it).

func (*Port) SetReception

func (p *Port) SetReception(dc gdlms.DataChannel)

SetReception sets the data link layer's channel.

type Prefixed

type Prefixed struct {
	net.Conn
	Prefix []byte
}

Prefixed is a connection whose first octets were already read.

func (*Prefixed) Read

func (p *Prefixed) Read(b []byte) (int, error)

Read returns the prefix first, then reads the connection.

Jump to

Keyboard shortcuts

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