blob: 5cf4d68224333a49f9f706c8328236d65b50b9ca [file] [log] [blame]
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Encoder functions for encoding bytes using HDLC-Lite protocol"""
from pw_hdlc_lite import protocol
_ESCAPE_BYTE = bytes([protocol.ESCAPE])
_FLAG_BYTE = bytes([protocol.FLAG])
_CONTROL = 0 # Currently, hard-coded to 0; no sequence numbers are used
def encode_information_frame(address: int, data: bytes) -> bytes:
"""Encodes an HDLC I-frame."""
frame = bytearray([address, _CONTROL]) + data
frame += protocol.frame_check_sequence(frame)
frame = frame.replace(_ESCAPE_BYTE, b'\x7d\x5d')
frame = frame.replace(_FLAG_BYTE, b'\x7d\x5e')
return b''.join([_FLAG_BYTE, frame, _FLAG_BYTE])