1 // Copyright 2020 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include <cstddef> 17 #include <cstdint> 18 19 #include "pw_bytes/span.h" 20 #include "pw_checksum/crc32.h" 21 #include "pw_hdlc/internal/protocol.h" 22 #include "pw_status/status.h" 23 #include "pw_stream/stream.h" 24 25 namespace pw::hdlc { 26 27 /// @brief Writes an HDLC unnumbered information frame (UI frame) to the 28 /// provided ``pw::stream`` writer. 29 /// 30 /// This function is a convenience alias for the more general ``Encoder`` 31 /// type and set of functions. 32 /// 33 /// @param address The frame address. 34 /// 35 /// @param payload The frame data to encode. 36 /// 37 /// @param writer The ``pw::stream`` to write the frame to. The frame contains 38 /// the following bytes. See [Design](/pw_hdlc/design.html) for more 39 /// information. 40 /// * HDLC flag byte (``0x7e``) 41 /// * Address (variable length, up to 10 bytes) 42 /// * UI-frame control (metadata) byte 43 /// * Payload (0 or more bytes) 44 /// * Frame check sequence (CRC-32, 4 bytes) 45 /// * HDLC flag byte (``0x7e``) 46 /// 47 /// @returns @rst 48 /// 49 /// .. pw-status-codes:: 50 /// 51 /// OK: The write finished successfully. 52 /// 53 /// RESOURCE_EXHAUSTED: The write failed because the size of 54 /// the frame would be larger than the writer's conservative limit. 55 /// 56 /// INVALID_ARGUMENT: The start of the write failed. Check 57 /// for problems in your ``address`` argument's value. 58 /// 59 /// @endrst 60 Status WriteUIFrame(uint64_t address, 61 ConstByteSpan payload, 62 stream::Writer& writer); 63 64 /// Encodes and writes HDLC frames. 65 class Encoder { 66 public: 67 /// Construct an encoder which will write data to ``output``. Encoder(stream::Writer & output)68 constexpr Encoder(stream::Writer& output) : writer_(output) {} 69 70 /// Writes the header for an U-frame. After successfully calling 71 /// StartUnnumberedFrame, WriteData may be called any number of times. StartUnnumberedFrame(uint64_t address)72 Status StartUnnumberedFrame(uint64_t address) { 73 return StartFrame(address, UFrameControl::UnnumberedInformation().data()); 74 } 75 76 /// Writes data for an ongoing frame. Must only be called after a successful 77 /// StartInformationFrame call, and prior to a FinishFrame() call. 78 Status WriteData(ConstByteSpan data); 79 80 /// Finishes a frame. Writes the frame check sequence and a terminating flag. 81 Status FinishFrame(); 82 83 private: 84 // Indicates this an information packet with sequence numbers set to 0. 85 static constexpr std::byte kUnusedControl = std::byte{0}; 86 87 Status StartFrame(uint64_t address, std::byte control); 88 89 stream::Writer& writer_; 90 checksum::Crc32 fcs_; 91 }; 92 93 } // namespace pw::hdlc 94