ocppj

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ocppj implements OCPP-J (JSON over WebSocket) framing.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnClosed       = errors.New("ocpp: connection closed")
	ErrConnDropped      = errors.New("ocpp: connection dropped")
	ErrNotConnected     = errors.New("ocpp: not connected")
	ErrAlreadyConnected = errors.New("ocpp: already connected")

	ErrCallTimeout         = errors.New("ocpp: call timeout")
	ErrCallCancelled       = errors.New("ocpp: call cancelled")
	ErrQueueFull           = errors.New("ocpp: outbound queue full")
	ErrConcurrentCallLimit = errors.New("ocpp: concurrent call limit exceeded")

	ErrUnknownAction        = errors.New("ocpp: unknown action")
	ErrHandlerNotRegistered = errors.New("ocpp: handler not registered")
	ErrInvalidDirection     = errors.New("ocpp: invalid message direction")
	ErrDuplicateHandler     = errors.New("ocpp: handler already registered")

	ErrUnsupportedVersion = errors.New("ocpp: unsupported version")
	ErrVersionMismatch    = errors.New("ocpp: version mismatch on connection")

	// ErrIgnoredMessageType is returned by Parse for a well-formed frame whose
	// MessageTypeId is not one this implementation handles. Per OCPP 2.1 Part 4
	// §4.4 the payload is ignored; the reader drops it without warning.
	ErrIgnoredMessageType = errors.New("ocpp: ignored message type")
)

Functions

func EncodeCall

func EncodeCall(msgID, action string, payload []byte) ([]byte, error)

EncodeCall builds a [2, msgID, action, payload] frame.

func EncodeCallError

func EncodeCallError(msgID, code, desc string, details []byte) ([]byte, error)

EncodeCallError builds a [4, msgID, code, desc, details] frame.

func EncodeCallResult

func EncodeCallResult(msgID string, payload []byte) ([]byte, error)

EncodeCallResult builds a [3, msgID, payload] frame.

func EncodeCallResultError added in v0.1.7

func EncodeCallResultError(msgID, code, desc string, details []byte) ([]byte, error)

EncodeCallResultError builds a [5, msgID, code, desc, details] frame (OCPP 2.1 CALLRESULTERROR).

func EncodeSend added in v0.1.7

func EncodeSend(msgID, action string, payload []byte) ([]byte, error)

EncodeSend builds a [6, msgID, action, payload] frame (OCPP 2.1 SEND).

func EncodeSignedCall added in v0.1.7

func EncodeSignedCall(msgID, action string, signedPayload []byte) ([]byte, error)

EncodeSignedCall builds a [2, msgID, action+"-Signed", signedPayload] frame (OCPP 2.1 Part 4 Ch 7). signedPayload is a Flattened JWS JSON object.

func EncodeSignedSend added in v0.1.7

func EncodeSignedSend(msgID, action string, signedPayload []byte) ([]byte, error)

EncodeSignedSend builds a [6, msgID, action+"-Signed", signedPayload] frame.

func NewMsgID

func NewMsgID() string

NewMsgID returns a unique, lexicographically sortable message ID.

Types

type CallError

type CallError struct {
	Code        ErrorCode
	Description string
	Details     map[string]any
	// IsResultError marks a CallError that arrived as a CALLRESULTERROR (type 5)
	// rather than a CALLERROR (type 4). Wire content is identical; this is for
	// observability only.
	IsResultError bool
	// contains filtered or unexported fields
}

CallError is an OCPP-J error response (MessageType 4).

func NewCallError

func NewCallError(code ErrorCode, desc string, details map[string]any) *CallError

NewCallError builds a CallError.

func WrapCallError

func WrapCallError(code ErrorCode, cause error, details map[string]any) *CallError

WrapCallError builds a CallError wrapping an underlying cause.

func (*CallError) Error

func (e *CallError) Error() string

func (*CallError) Unwrap

func (e *CallError) Unwrap() error

func (*CallError) WireCode

func (e *CallError) WireCode(v Version) string

WireCode returns the on-the-wire error code string, applying the OCPP 1.6 spec's misspelled OccurenceConstraintViolation code for 1.6 connections. //nolint:misspell // OCPP 1.6 spec wire spelling

type Direction

type Direction int

Direction indicates which peer originates a given action.

const (
	SentByCP   Direction = iota + 1 // Charge Point initiates (e.g. BootNotification)
	SentByCSMS                      // Central System initiates (e.g. ChangeConfiguration)
	SentByBoth                      // either peer may initiate (e.g. DataTransfer)
)

func (Direction) String

func (d Direction) String() string

type ErrorCode

type ErrorCode string

ErrorCode is an OCPP-J CallError code.

const (
	ErrorCodeNotImplemented                ErrorCode = "NotImplemented"
	ErrorCodeNotSupported                  ErrorCode = "NotSupported"
	ErrorCodeInternalError                 ErrorCode = "InternalError"
	ErrorCodeProtocolError                 ErrorCode = "ProtocolError"
	ErrorCodeSecurityError                 ErrorCode = "SecurityError"
	ErrorCodeFormatViolation               ErrorCode = "FormatViolation"    // 2.x
	ErrorCodeFormationViolation            ErrorCode = "FormationViolation" // 1.6
	ErrorCodePropertyConstraintViolation   ErrorCode = "PropertyConstraintViolation"
	ErrorCodeOccurenceConstraintViolation  ErrorCode = "OccurenceConstraintViolation"  //nolint:misspell // OCPP 1.6 spec wire spelling
	ErrorCodeOccurrenceConstraintViolation ErrorCode = "OccurrenceConstraintViolation" // 2.x
	ErrorCodeTypeConstraintViolation       ErrorCode = "TypeConstraintViolation"
	ErrorCodeGenericError                  ErrorCode = "GenericError"
	ErrorCodeMessageTypeNotSupported       ErrorCode = "MessageTypeNotSupported" // 2.x
	ErrorCodeRPCFrameworkError             ErrorCode = "RpcFrameworkError"       // 2.x
)

type Frame

type Frame struct {
	Type    MessageType
	Signed  bool // OCPP 2.1: action carried a "-Signed" suffix; Payload is a JWS object
	MsgID   string
	Action  string // Call / Send
	Payload []byte // Call / CallResult / Send: raw JSON object
	ErrCode string // CallError / CallResultError
	ErrDesc string // CallError / CallResultError
	ErrData []byte // CallError / CallResultError: raw JSON object
}

Frame is a parsed OCPP-J message.

func Parse

func Parse(raw []byte) (Frame, error)

Parse decodes a raw OCPP-J message into a Frame.

type Message

type Message[Req, Resp any] struct {
	Action    string
	Direction Direction
}

Message binds an OCPP action to its request and response types at compile time. Generated code creates package-level Message values; user code passes them to On/Call so Req/Resp are inferred.

type MessageType

type MessageType int

MessageType is the OCPP-J MessageTypeId (first array element).

const (
	Call                       MessageType = 2
	CallResult                 MessageType = 3
	MessageTypeCallError       MessageType = 4
	MessageTypeCallResultError MessageType = 5 // OCPP 2.1
	Send                       MessageType = 6 // OCPP 2.1
)

func (MessageType) String

func (m MessageType) String() string

type ProtocolError

type ProtocolError struct {
	Stage   string // "parse", "type", "shape"
	Raw     string
	Message string
}

ProtocolError indicates an OCPP-J frame that does not conform to the wire format.

func (*ProtocolError) Error

func (e *ProtocolError) Error() string

type SendMessage added in v0.1.7

type SendMessage[Req any] struct {
	Action    string
	Direction Direction
}

SendMessage binds an OCPP 2.1 SEND action to its request type at compile time. SEND messages are unconfirmed and have no response type. Generated code creates package-level SendMessage values; user code passes them to Send/OnSend.

type Version

type Version string

Version identifies an OCPP protocol version.

const (
	V16  Version = "1.6"
	V201 Version = "2.0.1"
	V21  Version = "2.1"
)

Directories

Path Synopsis
Package signing implements OCPP 2.1 Signed Messages (Part 4 Chapter 7): JWS signing and verification of OCPP-J payloads.
Package signing implements OCPP 2.1 Signed Messages (Part 4 Chapter 7): JWS signing and verification of OCPP-J payloads.

Jump to

Keyboard shortcuts

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