runtime.messaging
¶
Smart Device messaging.
This library provides low-level bindings for building Message
objects and
encoding them to or decoding them from a binary format. This module’s API adheres
closely to that of the underlying native C++ code and manipulates parameters using
bitmaps and memory. To generate or digest messages at a higher level (for example, using
parameter names), use runtime.buffer.DeviceBuffer
.
- exception runtime.messaging.MessageError(message, /, **context)¶
Bases:
runtime.exception.RuntimeBaseException
General message error.
- class runtime.messaging.ErrorCode(value)¶
Bases:
enum.IntEnum
Error codes produced by the Smart Device or during message encoding/decoding.
- OK¶
No error occurred.
- BACKOFF¶
The receiver is overwhelmed. The peer should transmit less data.
- INVALID_TYPE¶
The receiver received a message type it does not handle.
- BUFFER_OVERFLOW¶
Message was too large to encode/decode.
- UNEXPECTED_DELIMETER¶
Message was incomplete or otherwise unable to be decoded.
- BAD_CHECKSUM¶
Checksum computed by the receiver did not match the sender’s claim.
- GENERIC_ERROR¶
General error.
- class runtime.messaging.Message¶
Bases:
object
A Smart Device message.
The
len()
of a message is the length of its payload in bytes.- static decode(buf)¶
Decode a raw binary buffer, as it is transported on the wire.
- Parameters
buf – An object that implements Python’s writeable buffer protocol.
- Returns
A message containing the parsed data.
- Return type
- Raises
MessageError – If the message was not able to be decoded.
- encode()¶
Encode this message into a newly allocated buffer.
- Returns
The encoded message, ready to be put on the wire.
- Return type
- Raises
MessageError – If the message was not able to be encoded.
- encode_into_buf(buf)¶
Encode this message into an existing buffer.
- Parameters
buf – A object that implements Python’s writeable buffer protocol.
- Returns
The number of bytes used, which may be less than the length of
buf
.- Return type
- Raises
MessageError – If the message was not able to be encoded.
- static make_dev_data(msg, params, param_map)¶
Make a
MessageType.DEV_DATA
message.- Parameters
params (int) – A bitmap of parameters to write.
param_map (ParameterMap) – A parameter map specifying where in memory the parameter values should be read from.
- Returns
A Smart Device message.
- Return type
- static make_dev_disable(msg)¶
Make a
MessageType.DEV_DISABLE
message.- Returns
A Smart Device message.
- Return type
- static make_dev_read(msg, params)¶
Make a
MessageType.DEV_READ
message.
- static make_dev_write(msg, params, param_map)¶
Make a
MessageType.DEV_WRITE
message.- Parameters
params (int) – A bitmap of parameters to write.
param_map (ParameterMap) – A parameter map specifying where in memory the parameter values should be read from.
- Returns
A Smart Device message.
- Return type
- static make_error(msg, error)¶
Make a
MessageType.ERROR
message.
- static make_hb_req(msg, hb_id)¶
Make a
MessageType.HB_REQ
message.
- static make_hb_res(msg, hb_id)¶
Make a
MessageType.HB_RES
message.
- static make_ping(msg)¶
Make a
MessageType.PING
message.- Returns
A Smart Device message.
- Return type
- static make_sub_req(msg, params, interval)¶
Make a
MessageType.SUB_REQ
message.
- static make_sub_res(msg, params, interval, device_id, year, random)¶
Make a
MessageType.SUB_RES
message.- Parameters
- Returns
A Smart Device message.
- Return type
- classmethod make_unsubscribe()¶
Make a message to unsubscribe from all parameters.
- Returns
A
MessageType.SUB_REQ
message.- Return type
- read_dev_data(param_map)¶
Read the parameter values of a
MessageType.DEV_DATA
message.- Parameters
param_map (ParameterMap) – A parameter map specifying the addresses to write the values to.
- Returns
A bitmap of the parameters read.
- Return type
- read_dev_read()¶
Read the parameter values of a
MessageType.DEV_READ
message.- Returns
A bitmap of the parameters contained.
- Return type
- read_dev_write(param_map)¶
Read the parameter values of a
MessageType.DEV_WRITE
message.- Parameters
param_map (ParameterMap) – A parameter map specifying the addresses to write the values to.
- Returns
A bitmap of the parameters read.
- Return type
- read_error()¶
Read the error code of a
MessageType.ERROR
message.- Returns
The error code.
- Return type
- read_hb_req()¶
Read the heartbeat identifier of a
MessageType.HB_REQ
message.- Returns
The heartbeat ID.
- Return type
- read_hb_res()¶
Read the heartbeat identifier of a
MessageType.HB_RES
message.- Returns
The heartbeat ID.
- Return type
- read_sub_req()¶
Read the fields of a
MessageType.SUB_REQ
message.
- read_sub_res()¶
Read the fields of a
MessageType.SUB_RES
message.
- verify_checksum()¶
Determine whether the claimed checksum of the payload is valid.
- type¶
The type of this message.
- Type
- class runtime.messaging.MessageType(value)¶
Bases:
enum.IntEnum
The Smart Device message’s type identifies what kind of payload it contains.
- PING¶
Ping request from Runtime to the Smart Device.
- SUB_REQ¶
Subscription request.
- SUB_RES¶
Subscription response.
- DEV_READ¶
Device read.
- DEV_WRITE¶
Device write.
- DEV_DATA¶
Device data.
- DEV_DISABLE¶
Device disable.
- HB_REQ¶
Heartbeat request.
- HB_RES¶
Heartbeat response.
- ERROR¶
Error.
- class runtime.messaging.ParameterMap¶
Bases:
object
A map of parameters in memory.
This object is used by
Message
to determine where and how many bytes should be written to or read from for each parameter.- clear_param(index)¶
Clear an existing parameter.
- set_param(index, base, size)¶
Set the address and size of a parameter.
- Parameters
- Raises
MessageError – If the index is out of bounds.
Warning
Since this method accepts addresses that a
Message
will eventually dereference, a bad address may cause a segfault. Use with caution.