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 ¶
- Constants
- Variables
- func Answer(c net.Conn, timeout time.Duration) (int, []byte, error)
- func IdentifyRequest(device *uint16) []byte
- func ParseIdentifyRequest(b []byte) (device *uint16, err error)
- func ServerIdentify(c net.Conn, id Identity, device *uint16, interOctet time.Duration) ([]byte, error)
- func ServerIdentifyFunc(c net.Conn, id Identity, own func(*uint16) bool, interOctet time.Duration) ([]byte, error)
- type Config
- type Connection
- type Identity
- type ModemSim
- type Port
- func (p *Port) Close()
- func (p *Port) Connect() error
- func (p *Port) Connection() Connection
- func (p *Port) Disconnect() error
- func (p *Port) IsConnected() bool
- func (p *Port) Send(b []byte) error
- func (p *Port) SetAddress(int, int)
- func (p *Port) SetLogger(*log.Logger)
- func (p *Port) SetReception(dc gdlms.DataChannel)
- type Prefixed
Examples ¶
Constants ¶
const ( IdentifyRequestID = 0x20 // IDENTIFY-Request-ID IdentifyRequestIDAlt = 0x49 // 'I', accepted for existing implementations (footnote 1) )
Identification message values (6.3.3.2).
Variables ¶
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.
var ErrConnect = errors.New("the physical connection was not established")
ErrConnect is a PH-CONNECT.confirm with a negative result.
var ErrNotIdentify = errors.New("not an identification request")
ErrNotIdentify is returned for bytes that are not an IDENTIFY.request.
Functions ¶
func Answer ¶
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 ¶
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 ¶
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 ¶
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 ¶
ParseIdentity reads a four-octet response.
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 ¶
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.
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 (*Port) Connection ¶
func (p *Port) Connection() Connection
Connection is the last PH-CONNECT.confirm.
func (*Port) Disconnect ¶
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 ¶
IsConnected reports a physical connection.
func (*Port) SetAddress ¶
SetAddress does nothing: the physical layer has no addresses (the gosem transport interface asks for it).
func (*Port) SetLogger ¶
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.