docs/assets/js/b6d70f94.366c3dfa.js
"use strict";(self.webpackChunkdocs_openc3_com=self.webpackChunkdocs_openc3_com||[]).push([[9005],{9077:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>d,contentTitle:()=>i,default:()=>h,frontMatter:()=>a,metadata:()=>o,toc:()=>c});var s=n(4848),r=n(8453);const a={sidebar_position:7,title:"Protocols"},i=void 0,o={id:"configuration/protocols",title:"Protocols",description:"Protocols process data on behalf of an Interface. They can modify the data being written, data being read, or both. Protocols can also mark a packet as stored instead of real-time which means COSMOS will not update the current value table with the packet data. Protocols can be layered and will be processed in order. For example, if you have a low-level encryption layer that must be first removed before processing a higher level buffer length protocol.",source:"@site/docs/configuration/protocols.md",sourceDirName:"configuration",slug:"/configuration/protocols",permalink:"/docs/configuration/protocols",draft:!1,unlisted:!1,editUrl:"https://github.com/OpenC3/cosmos/tree/main/docs.openc3.com/docs/configuration/protocols.md",tags:[],version:"current",sidebarPosition:7,frontMatter:{sidebar_position:7,title:"Protocols"},sidebar:"defaultSidebar",previous:{title:"Interfaces",permalink:"/docs/configuration/interfaces"},next:{title:"Tables",permalink:"/docs/configuration/table"}},d={},c=[{value:"Packet Delineation Protocols",id:"packet-delineation-protocols",level:2},{value:"COBS Protocol",id:"cobs-protocol",level:3},{value:"SLIP Protocol",id:"slip-protocol",level:3},{value:"Burst Protocol",id:"burst-protocol",level:3},{value:"Fixed Protocol",id:"fixed-protocol",level:3},{value:"Length Protocol",id:"length-protocol",level:3},{value:"Terminated Protocol",id:"terminated-protocol",level:3},{value:"Template Protocol",id:"template-protocol",level:3},{value:"Preidentified Protocol",id:"preidentified-protocol",level:3},{value:"Helper Protocols",id:"helper-protocols",level:2},{value:"CmdResponse Protocol",id:"cmdresponse-protocol",level:3},{value:"CRC Protocol",id:"crc-protocol",level:3},{value:"Ignore Packet Protocol",id:"ignore-packet-protocol",level:3},{value:"Custom Protocols",id:"custom-protocols",level:2},{value:"Method discussions",id:"method-discussions",level:2},{value:"initialize or <strong>init</strong>",id:"initialize-or-init",level:3},{value:"reset",id:"reset",level:3},{value:"connect_reset",id:"connect_reset",level:3},{value:"disconnect_reset",id:"disconnect_reset",level:3},{value:"read_data",id:"read_data",level:3},{value:"read_packet",id:"read_packet",level:3},{value:"write_packet",id:"write_packet",level:3},{value:"write_data",id:"write_data",level:3},{value:"post_write_interface",id:"post_write_interface",level:3},{value:"protocol_cmd",id:"protocol_cmd",level:3},{value:"Examples",id:"examples",level:2}];function l(e){const t={a:"a",admonition:"admonition",code:"code",em:"em",h2:"h2",h3:"h3",img:"img",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,r.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(t.p,{children:"Protocols process data on behalf of an Interface. They can modify the data being written, data being read, or both. Protocols can also mark a packet as stored instead of real-time which means COSMOS will not update the current value table with the packet data. Protocols can be layered and will be processed in order. For example, if you have a low-level encryption layer that must be first removed before processing a higher level buffer length protocol."}),"\n",(0,s.jsx)(t.admonition,{title:"Protocol Run Order",type:"info",children:(0,s.jsx)(t.p,{children:"Read protocols execute in the order specified (First specified runs first). Write protocols execute in the reverse order (Last specified executes first)."})}),"\n",(0,s.jsx)(t.p,{children:"Protocols are typically used to define the logic to delineate packets and manipulate data as it written to and read from Interfaces. COSMOS includes Interfaces for TCP/IP Client, TCP/IP Server, Udp Client / Server, and Serial connections. For 99% of use cases these Interfaces should not require any changes as they universally handle the low-level details of reading and writing from these types of connections. All unique behavior should now be defined in Protocols."}),"\n",(0,s.jsx)(t.p,{children:"At a minimum, any byte stream based Interface will require a Protocol to delineate packets. TCP/IP and Serial are examples of byte stream based Interfaces. A byte stream is just a simple stream of bytes and thus you need some way to know where packets begin and end within the stream."}),"\n",(0,s.jsx)(t.p,{children:"TCP/IP is a friendly byte stream. Unless you are dealing with a very poorly written system, the first byte received on a TCP/IP connection will always be the start of a packet. Also, TCP/IP is a reliable connection in that it ensures that all data is received in the correct order, that no data is lost, and that the data is not corrupted (TCP/IP is protected by a CRC32 which is pretty good for avoiding unrecognized data corruption)."}),"\n",(0,s.jsx)(t.p,{children:"Serial is a much less friendly byte stream. With serial connections, it is very likely that when you open a serial port and start receiving data you will receive the middle of a message. (This problem is only avoided when interfacing with a system that only writes to the serial port in response to a command). For this reason, sync patterns are highly beneficial for serial interfaces. Additionally, serial interfaces may use some method to protect against unrecognized data corruption (Checksums, CRCs, etc.)"}),"\n",(0,s.jsx)(t.p,{children:"UDP is an inherently packet based connection. If you read from a UDP socket, you will always receive back an entire packet. The best UDP based Protocols take advantage of this fact. Some implementations try to make UDP act like a byte stream, but this is a misuse of the protocol because it is highly likely that you will lose data and have no way to recover."}),"\n",(0,s.jsx)(t.h2,{id:"packet-delineation-protocols",children:"Packet Delineation Protocols"}),"\n",(0,s.jsx)(t.p,{children:"COSMOS provides the following packet delineation protocols: COBS, SLIP, Burst, Fixed, Length, Template (deprecated), Terminated and Preidentified. Each of these protocols has the primary purpose of separating out packets from a byte stream."}),"\n",(0,s.jsxs)(t.p,{children:['Note that all protocols take a final parameter called "Allow Empty Data". This indicates whether the protocol will allow an empty string to be passed down to later Protocols (instead of returning ',":STOP","). Can be true, false, or nil, where nil is interpreted as true unless the Protocol is the last Protocol of the chain. End users of a protocol will almost always simply leave off this parameter. For more information read the ",(0,s.jsx)(t.a,{href:"/docs/configuration/protocols#custom-protocols",children:"Custom Protocols"})," documentation."]}),"\n",(0,s.jsx)(t.h3,{id:"cobs-protocol",children:"COBS Protocol"}),"\n",(0,s.jsxs)(t.p,{children:["The Consistent Overhead Byte Stuffing (COBS) Protocol is an algorithm for encoding data bytes that results in efficient, reliable, unambiguous packet framing regardless of packet content, thus making it easy for receiving applications to recover from malformed packets. It employs the zero byte value to serve as a packet delimiter (a special value that indicates the boundary between packets). The algorithm replaces each zero data byte with a non-zero value so that no zero data bytes will appear in the packet and thus be misinterpreted as packet boundaries (See ",(0,s.jsx)(t.a,{href:"https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing",children:"https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing"})," for more)."]}),"\n",(0,s.jsx)(t.h3,{id:"slip-protocol",children:"SLIP Protocol"}),"\n",(0,s.jsxs)(t.p,{children:["The Serial Line IP (SLIP) Protocol defines a sequence of characters that frame IP packets on a serial line. It defines two special characters: END and ESC. END is 0xC0 and ESC is 0xDB. To send a packet, a SLIP host simply starts sending the data in the packet. If a data byte is the same code as END character, a two byte sequence of ESC and 0xDC is sent instead. If a data bytes is the same as an ESC character, an two byte sequence of ESC and 0xDD is sent instead. When the last byte in the packet has been sent, an END character is then transmitted (See ",(0,s.jsx)(t.a,{href:"https://datatracker.ietf.org/doc/html/rfc1055",children:"https://datatracker.ietf.org/doc/html/rfc1055"})," for more)."]}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Start Char"}),(0,s.jsx)(t.td,{children:"Character to place at the start of frames"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no character)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Read Strip Characters"}),(0,s.jsx)(t.td,{children:"Strip off start_char and end_char from reads"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"true"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Read Enable Escaping"}),(0,s.jsx)(t.td,{children:"Whether to enable character escaping on reads"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"true"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Write Enable Escaping"}),(0,s.jsx)(t.td,{children:"Whether to enable character escaping on writes"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"true"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"End Char"}),(0,s.jsx)(t.td,{children:"Character to place at the end of frames"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0xC0"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Esc Char"}),(0,s.jsx)(t.td,{children:"Escape character"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0xDB"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Escape End Char"}),(0,s.jsx)(t.td,{children:"Character to escape End character"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0xDC"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Escape Esc Char"}),(0,s.jsx)(t.td,{children:"Character to escape Esc character"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0xDD"})]})]})]}),"\n",(0,s.jsx)(t.h3,{id:"burst-protocol",children:"Burst Protocol"}),"\n",(0,s.jsx)(t.p,{children:"The Burst Protocol simply reads as much data as it can from the interface before returning the data as a COSMOS Packet (It returns a packet for each burst of data read). This Protocol relies on regular bursts of data delimited by time and thus is not very robust. However, it can utilize a sync pattern which does allow it to re-sync if necessary. It can also discard bytes from the incoming data to remove the sync pattern. Finally, it can add sync patterns to data being written out of the Interface."}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Discard Leading Bytes"}),(0,s.jsx)(t.td,{children:"The number of bytes to discard from the binary data after reading. Note that this applies to bytes starting with the sync pattern if the sync pattern is being used."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0 (do not discard bytes)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Sync Pattern"}),(0,s.jsx)(t.td,{children:"Hex string representing a byte pattern that will be searched for in the raw data. This pattern represents a packet delimiter and all data found including the sync pattern will be returned"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no sync pattern)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Fill Fields"}),(0,s.jsx)(t.td,{children:"Whether to fill in the sync pattern on outgoing packets"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"false"})]})]})]}),"\n",(0,s.jsx)(t.h3,{id:"fixed-protocol",children:"Fixed Protocol"}),"\n",(0,s.jsx)(t.p,{children:"The Fixed Protocol reads a preset minimum amount of data which is necessary to properly identify all the defined packets using the interface. It then identifies the packet and proceeds to read as much data from the interface as necessary to create the packet which it then returns. This protocol relies on all the packets on the interface being fixed in length. For example, all the packets using the interface are a fixed size and contain a simple header with a 32-bit sync pattern followed by a 16 bit ID. The Fixed Protocol would elegantly handle this case with a minimum read size of 6 bytes. The Fixed Protocol also supports a sync pattern, discarding leading bytes, and filling the sync pattern similar to the Burst Protocol."}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Minimum ID Size"}),(0,s.jsx)(t.td,{children:"The minimum number of bytes needed to identify a packet. All the packet definitions must declare their ID_ITEM(s) within this given number of bytes."}),(0,s.jsx)(t.td,{children:"Yes"}),(0,s.jsx)(t.td,{})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Discard Leading Bytes"}),(0,s.jsx)(t.td,{children:"The number of bytes to discard from the binary data after reading. Note that this applies to bytes starting with the sync pattern if the sync pattern is being used."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0 (do not discard bytes)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Sync Pattern"}),(0,s.jsx)(t.td,{children:"Hex string representing a byte pattern that will be searched for in the raw data. This pattern represents a packet delimiter and all data found including the sync pattern will be returned."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no sync pattern)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Telemetry"}),(0,s.jsx)(t.td,{children:"Whether the data is telemetry"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"true (false means command)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Fill Fields"}),(0,s.jsx)(t.td,{children:"Whether to fill in the sync pattern on outgoing packets"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"false"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Unknown Raise"}),(0,s.jsx)(t.td,{children:"Whether to raise an exception for an unknown packet"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"false"})]})]})]}),"\n",(0,s.jsx)(t.h3,{id:"length-protocol",children:"Length Protocol"}),"\n",(0,s.jsx)(t.p,{children:"The Length Protocol depends on a length field at a fixed location in the defined packets using the interface. It then reads enough data to grab the length field, decodes it, and reads the remaining length of the packet. For example, all the packets using the interface contain a CCSDS header with a length field. The Length Protocol can be set up to handle the length field and even the length offset CCSDS uses. The Length Protocol also supports a sync pattern, discarding leading bytes, and filling the length and sync pattern similar to the Burst Protocol."}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Length Bit Offset"}),(0,s.jsx)(t.td,{children:"The bit offset from the start of the packet to the length field. Every packet using this interface must have the same structure such that the length field is the same size at the same location. Be sure to account for the length of the Sync Pattern in this value (if present)."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0 bits"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Length Bit Size"}),(0,s.jsx)(t.td,{children:"The size in bits of the length field"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"16 bits"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Length Value Offset"}),(0,s.jsx)(t.td,{children:"The offset to apply to the length field value. The actual value of the length field plus this offset should equal the exact number of bytes required to read all data for the packet (including the length field itself, sync pattern, etc). For example, if the length field indicates packet length minus one, this value should be one. Be sure to account for the length of the Sync Pattern in this value (if present)."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Bytes per Count"}),(0,s.jsx)(t.td,{children:"The number of bytes per each length field 'count'. This is used if the units of the length field is something other than bytes, e.g. if the length field count is in words."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"1 byte"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Length Endianness"}),(0,s.jsx)(t.td,{children:"The endianness of the length field. Must be either 'BIG_ENDIAN' or 'LITTLE_ENDIAN'."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"'BIG_ENDIAN'"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Discard Leading Bytes"}),(0,s.jsx)(t.td,{children:"The number of bytes to discard from the binary data after reading. Note that this applies to bytes including the sync pattern if the sync pattern is being used. Discarding is one of the very last steps so any size and offsets above need to account for all the data before discarding."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0 (do not discard bytes)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Sync Pattern"}),(0,s.jsx)(t.td,{children:"Hex string representing a byte pattern that will be searched for in the raw data. This pattern represents a packet delimiter and all data found including the sync pattern will be returned."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no sync pattern)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Max Length"}),(0,s.jsx)(t.td,{children:"The maximum allowed value in the length field"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no maximum length)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Fill Length and Sync Pattern"}),(0,s.jsx)(t.td,{children:"Setting this flag to true causes the length field and sync pattern (if present) to be filled automatically on outgoing packets."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"false"})]})]})]}),"\n",(0,s.jsx)(t.p,{children:"The most confusing aspect of the Length Protocol is calculating the Length Value Offset. This is especially true in the commonly used CCSDS Space Packet Protocol. The best way to illustrate this is with an example. Suppose you have CCSDS Space Packets prepended with a Sync Pattern of 0x1ACFFC1D. This would look like the following:"}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Sync (4 bytes)"}),(0,s.jsx)(t.th,{children:"Header (4 bytes)"}),(0,s.jsx)(t.th,{children:"Length (2 bytes)"}),(0,s.jsx)(t.th,{children:"Data (4 bytes)"})]})}),(0,s.jsx)(t.tbody,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"0x1ACFFC1D"}),(0,s.jsx)(t.td,{children:"0x0001CADB"}),(0,s.jsx)(t.td,{children:"0x0003"}),(0,s.jsx)(t.td,{children:"0xDEADBEEF"})]})})]}),"\n",(0,s.jsxs)(t.p,{children:["In this case the total length of the packet is 14 bytes: ",(0,s.jsx)(t.strong,{children:"4 + 4 + 2 + 4 = 14"}),". With 4 bytes of data, the length field is 3 because in CCSDS the length field is calculated as (data length - 1). So how would we calculate the Length Value Offset? COSMOS reads all the bytes in the packet (including the Sync Pattern) so the total length is 14 bytes. The length field is 3 so the Length Value Offset (offset to apply to the length field value) should be 11 (",(0,s.jsx)(t.strong,{children:"3 + 11 = 14"}),")."]}),"\n",(0,s.jsx)(t.h3,{id:"terminated-protocol",children:"Terminated Protocol"}),"\n",(0,s.jsx)(t.p,{children:"The Terminated Protocol delineates packets using termination characters found at the end of every packet. It continuously reads data until the termination characters are found at which point it returns the packet data. For example, all the packets using the interface are followed by 0xABCD. This data can either be a part of each packet that is kept or something which is known only by the Terminated Protocol and simply thrown away."}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Write Termination Characters"}),(0,s.jsx)(t.td,{children:"The data to write after writing a command packet. Given as a hex string such as 0xABCD."}),(0,s.jsx)(t.td,{children:"Yes"}),(0,s.jsx)(t.td,{})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Read Termination Characters"}),(0,s.jsx)(t.td,{children:"The characters which delineate the end of a telemetry packet. Given as a hex string such as 0xABCD."}),(0,s.jsx)(t.td,{children:"Yes"}),(0,s.jsx)(t.td,{})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Strip Read Termination"}),(0,s.jsx)(t.td,{children:"Whether to remove the read termination characters before returning the telemetry packet"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"true"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Discard Leading Bytes"}),(0,s.jsx)(t.td,{children:"The number of bytes to discard from the binary data after reading. Note that this applies to bytes including the sync pattern if the sync pattern is being used."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0 (do not discard bytes)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Sync Pattern"}),(0,s.jsx)(t.td,{children:"Hex string representing a byte pattern that will be searched for in the raw data. This pattern represents a packet delimiter and all data found including the sync pattern will be returned."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no sync pattern)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Fill Fields"}),(0,s.jsx)(t.td,{children:"Whether to fill in the sync pattern on outgoing packets"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"false"})]})]})]}),"\n",(0,s.jsx)(t.h3,{id:"template-protocol",children:"Template Protocol"}),"\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.strong,{children:"Deprecated"})}),"\n",(0,s.jsx)(t.p,{children:"This protocol is now deprecated because it is not able to capture the original SCPI messages in COSMOS raw logging. Please use the TemplateAccessor with the CmdResponseProtocol instead."}),"\n",(0,s.jsxs)(t.p,{children:["The Template Protocol works much like the Terminated Protocol except it is designed for text-based command and response type interfaces such as SCPI (Standard Commands for Programmable Instruments). It delineates packets in the same way as the Terminated Protocol except each packet is referred to as a line (because each usually contains a line of text). For outgoing packets, a CMD_TEMPLATE field is expected to exist in the packet. This field contains a template string with items to be filled in delineated within HTML tag style brackets ",(0,s.jsx)(t.code,{children:'"<EXAMPLE>"'}),". The Template Protocol will read the named items from within the packet and fill in the CMD_TEMPLATE. This filled in string is then sent out rather than the originally passed in packet. Correspondingly, if a response is expected the outgoing packet should include a RSP_TEMPLATE and RSP_PACKET field. The RSP_TEMPLATE is used to extract data from the response string and build a corresponding RSP_PACKET. See the TEMPLATE target within the COSMOS Demo configuration for an example of usage."]}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Write Termination Characters"}),(0,s.jsx)(t.td,{children:"The data to write after writing a command packet. Given as a hex string such as 0xABCD."}),(0,s.jsx)(t.td,{children:"Yes"}),(0,s.jsx)(t.td,{})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Read Termination Characters"}),(0,s.jsx)(t.td,{children:"The characters which delineate the end of a telemetry packet. Given as a hex string such as 0xABCD."}),(0,s.jsx)(t.td,{children:"Yes"}),(0,s.jsx)(t.td,{})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Ignore Lines"}),(0,s.jsx)(t.td,{children:"Number of response lines to ignore (completely drop)"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0 lines"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Initial Read Delay"}),(0,s.jsx)(t.td,{children:"An initial delay after connecting after which the interface will be read till empty and data dropped. Useful for discarding connect headers and initial prompts."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no initial read)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Response Lines"}),(0,s.jsx)(t.td,{children:"The number of lines that make up expected responses"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"1 line"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Strip Read Termination"}),(0,s.jsx)(t.td,{children:"Whether to remove the read termination characters before returning the telemetry packet"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"true"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Discard Leading Bytes"}),(0,s.jsx)(t.td,{children:"The number of bytes to discard from the binary data after reading. Note that this applies to bytes including the sync pattern if the sync pattern is being used."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0 (do not discard bytes)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Sync Pattern"}),(0,s.jsx)(t.td,{children:"Hex string representing a byte pattern that will be searched for in the raw data. This pattern represents a packet delimiter and all data found including the sync pattern will be returned."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no sync pattern)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Fill Fields"}),(0,s.jsx)(t.td,{children:"Whether to fill in the sync pattern on outgoing packets"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"false"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Response Timeout"}),(0,s.jsx)(t.td,{children:"Number of seconds to wait for a response before timing out"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"5.0"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Response Polling Period"}),(0,s.jsx)(t.td,{children:"Number of seconds to wait between polling for a response"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0.02"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Raise Exceptions"}),(0,s.jsx)(t.td,{children:"Whether to raise exceptions when errors occur like timeouts or unexpected responses"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"false"})]})]})]}),"\n",(0,s.jsx)(t.h3,{id:"preidentified-protocol",children:"Preidentified Protocol"}),"\n",(0,s.jsx)(t.p,{children:"The Preidentified Protocol delineates packets using a custom COSMOS header. This Protocol is created to allow tools to connect and receive the entire packet stream. It can also be used to chain COSMOS instances together although that should rarely be needed with the new web native implementation."}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Sync Pattern"}),(0,s.jsx)(t.td,{children:"Hex string representing a byte pattern that will be searched for in the raw data. This pattern represents a packet delimiter and all data found AFTER the sync pattern will be returned. The sync pattern itself is discarded."}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no sync pattern)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Max Length"}),(0,s.jsx)(t.td,{children:"The maximum allowed value in the length field"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (no maximum length)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Mode"}),(0,s.jsx)(t.td,{children:"The Version of the preidentified protocol to support (2 or 4).3"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"4"})]})]})]}),"\n",(0,s.jsx)(t.h2,{id:"helper-protocols",children:"Helper Protocols"}),"\n",(0,s.jsx)(t.p,{children:"COSMOS provides the following helper protocols: CmdResponse, Crc and Ignore. These protocols provide helper functionality to Interfaces."}),"\n",(0,s.jsx)(t.h3,{id:"cmdresponse-protocol",children:"CmdResponse Protocol"}),"\n",(0,s.jsx)(t.p,{children:"The CmdResponse Protocol waits for a response for any commands with a defined response packet (TODO: More documentation and examples)."}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Response Timeout"}),(0,s.jsx)(t.td,{children:"Number of seconds to wait before timing out when waiting for a response"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"5"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Response Polling Period"}),(0,s.jsx)(t.td,{children:"Number of seconds to wait between polling for a response"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"0.02"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Raise Exceptions"}),(0,s.jsx)(t.td,{children:"Whether to raise exceptions when errors occur in the protocol like unexpected responses or response timeouts"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"false"})]})]})]}),"\n",(0,s.jsx)(t.h3,{id:"crc-protocol",children:"CRC Protocol"}),"\n",(0,s.jsx)(t.p,{children:"The CRC protocol can add CRCs to outgoing commands and verify CRCs on incoming telemetry packets."}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Write Item Name"}),(0,s.jsx)(t.td,{children:"Item to fill with calculated CRC value for outgoing packets (nil = don't fill)"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Strip CRC"}),(0,s.jsx)(t.td,{children:"Whether to remove the CRC from incoming packets"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"false"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Bad Strategy"}),(0,s.jsx)(t.td,{children:"How to handle CRC errors on incoming packets. ERROR = Just log the error, DISCONNECT = Disconnect interface"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:'"ERROR"'})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Bit Offset"}),(0,s.jsx)(t.td,{children:"Bit offset of the CRC in the data. Can be negative to indicate distance from end of packet"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"-32"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Bit Size"}),(0,s.jsx)(t.td,{children:"Bit size of the CRC - Must be 16, 32, or 64"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"32"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Endianness"}),(0,s.jsx)(t.td,{children:"Endianness of the CRC (BIG_ENDIAN/LITTLE_ENDIAN)"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:'"BIG_ENDIAN"'})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Poly"}),(0,s.jsx)(t.td,{children:"Polynomial to use when calculating the CRC expressed as an integer"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (use default polynomial - 16-bit=0x1021, 32-bit=0x04C11DB7, 64-bit=0x42F0E1EBA9EA3693)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Seed"}),(0,s.jsx)(t.td,{children:"Seed value to start the calculation"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (use default seed - 16-bit=0xFFFF, 32-bit=0xFFFFFFFF, 64-bit=0xFFFFFFFFFFFFFFFF)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Xor"}),(0,s.jsx)(t.td,{children:"Whether to XOR the CRC result with 0xFFFF"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (use default value - 16-bit=false, 32-bit=true, 64-bit=true)"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Reflect"}),(0,s.jsx)(t.td,{children:"Whether to bit reverse each byte of data before calculating the CRC"}),(0,s.jsx)(t.td,{children:"No"}),(0,s.jsx)(t.td,{children:"nil (use default value - 16-bit=false, 32-bit=true, 64-bit=true)"})]})]})]}),"\n",(0,s.jsx)(t.h3,{id:"ignore-packet-protocol",children:"Ignore Packet Protocol"}),"\n",(0,s.jsx)(t.p,{children:"The Ignore Packet protocol drops specified command packets sent by COSMOS or drops incoming telemetry packets."}),"\n",(0,s.jsxs)(t.table,{children:[(0,s.jsx)(t.thead,{children:(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.th,{children:"Parameter"}),(0,s.jsx)(t.th,{children:"Description"}),(0,s.jsx)(t.th,{children:"Required"}),(0,s.jsx)(t.th,{children:"Default"})]})}),(0,s.jsxs)(t.tbody,{children:[(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Target Name"}),(0,s.jsx)(t.td,{children:"Target name of the packet to ignore"}),(0,s.jsx)(t.td,{children:"Yes"}),(0,s.jsx)(t.td,{children:"nil"})]}),(0,s.jsxs)(t.tr,{children:[(0,s.jsx)(t.td,{children:"Packet Name"}),(0,s.jsx)(t.td,{children:"Packet name of the packet to ignore"}),(0,s.jsx)(t.td,{children:"Yes"}),(0,s.jsx)(t.td,{children:"nil"})]})]})]}),"\n",(0,s.jsx)(t.h2,{id:"custom-protocols",children:"Custom Protocols"}),"\n",(0,s.jsx)(t.p,{children:"Creating a custom protocol is easy and should be the default solution for customizing COSMOS Interfaces (rather than creating a new Interface class). However, creating custom Interfaces is still useful for defaulting parameters to values that always are fixed for your target and for including the necessary Protocols. The base COSMOS Interfaces take a lot of parameters that can be confusing to your end users. Thus you may want to create a custom Interface just to hard coded these values and cut the available parameters down to something like the hostname and port to connect to."}),"\n",(0,s.jsxs)(t.p,{children:["All custom Protocols should derive from the Protocol class ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/lib/openc3/interfaces/protocols/protocol.rb",children:"openc3/interfaces/protocols/protocol.rb"})," (Ruby) and ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/python/openc3/interfaces/protocols/protocol.py",children:"openc3/interfaces/protocols/protocol.py"})," (Python). This class defines the 9 methods that are relevant to writing your own protocol. The base class implementation for each method is included below as well as a discussion as to how the methods should be overridden and used in your own Protocols."]}),"\n",(0,s.jsx)(t.admonition,{title:"Ruby Protocol APIs",type:"info",children:(0,s.jsxs)(t.p,{children:["Protocols should not ",(0,s.jsx)(t.code,{children:"require 'openc3/script'"})," since they are part of a COSMOS interface. They should use the COSMOS library code directly like ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/lib/openc3/system/system.rb",children:"System"}),", ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/lib/openc3/packets/packet.rb",children:"Packet"}),", ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/lib/openc3/utilities/bucket.rb",children:"Bucket"}),", ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/lib/openc3/accessors/binary_accessor.rb",children:"BinaryAccessor"}),", etc. When in doubt, consult the existing COSMOS ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/tree/main/openc3/lib/openc3/interfaces/protocols",children:"protocol"})," classes."]})}),"\n",(0,s.jsx)(t.admonition,{title:"Python Protocol APIs",type:"info",children:(0,s.jsxs)(t.p,{children:["Protocols should not ",(0,s.jsx)(t.code,{children:"from openc3.script import *"})," since they are part of a COSMOS interface. They should use the COSMOS library code directly like ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/python/openc3/system/system.py",children:"System"}),", ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/python/openc3/packets/packet.py",children:"Packet"}),", ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/python/openc3/utilities/bucket.py",children:"Bucket"}),", ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/python/openc3/accessors/binary_accessor.py",children:"BinaryAccessor"}),", etc. When in doubt, consult the existing COSMOS ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/tree/main/openc3/python/openc3/interfaces/protocols",children:"protocol"})," classes."]})}),"\n",(0,s.jsx)(t.p,{children:"To really understand how Protocols work, you first must understand the logic within the base Interface class read and write methods."}),"\n",(0,s.jsx)(t.p,{children:"Let's first discuss the read method."}),"\n",(0,s.jsx)(t.admonition,{title:"Ruby Symbols, Python Strings",type:"info",children:(0,s.jsxs)(t.p,{children:["In the following discussions an all caps word is a symbol in Ruby and a string in Python. So a reference to STOP means ",":STOP",' in Ruby and "STOP" in Python.']})}),"\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.img,{alt:"Interface Read Logic",src:n(5946).A+"",width:"1063",height:"542"})}),"\n",(0,s.jsxs)(t.p,{children:["On ",(0,s.jsx)(t.em,{children:"every"}),' call to read, an empty string "" is first passed down to each of the read Protocol\'s ',(0,s.jsx)(t.code,{children:"read_data()"})," method ",(0,s.jsx)(t.em,{children:"before"})," new raw data is attempted to be read using the Interface's ",(0,s.jsx)(t.code,{children:"read_interface()"})," method. This is a signal to Protocols that have cached up more than one packet worth of data to output those cached packets before any new data is read from the Interface. Typically no data will be cached up and one of the Protocols ",(0,s.jsx)(t.code,{children:"read_data()"})," methods will return STOP in response to the empty string, indicating that more data is required to generate a packet. Each Protocol's ",(0,s.jsx)(t.code,{children:"read_data()"})," method can return one of three things: data that will be passed down to any additional Protocols or turned into a Packet, STOP which means more data is required from the Interface for the Protocol to continue, or DISCONNECT which means that something has happened that requires disconnecting the Interface (and by default trying to reconnect). Each Protocol's ",(0,s.jsx)(t.code,{children:"read_data()"})," method is passed the data that will eventually be turned into a packet and returns a possibly modified set of data. If the data passes through all Protocol's ",(0,s.jsx)(t.code,{children:"read_data()"})," methods it is then converted into a COSMOS packet using the Interface's convert_data_to_packet() method. This packet is then run in a similar fashion through each Read Protocol's read_packet() method. This method has essentially the same return possibilities: a Packet (instead of data as in ",(0,s.jsx)(t.code,{children:"read_data()"}),"), STOP, or DISCONNECT. If the Packet makes it through all read_packet() methods then the Interface packet read counter is incremented and the Packet is returned to the Interface."]}),"\n",(0,s.jsx)(t.p,{children:(0,s.jsx)(t.img,{alt:"Interface Write Logic",src:n(887).A+"",width:"931",height:"944"})}),"\n",(0,s.jsx)(t.p,{children:"The Interface write() method works very similarly to read. (It should be mentioned that by default write protocols run in the reverse order of read protocols. This makes sense because when reading you're typically stripping layers of data and when writing you're typically adding on layers in reverse order.)"}),"\n",(0,s.jsx)(t.p,{children:"First, the packet write counter is incremented. Then each write Protocol is given a chance to modify the packet by its write_packet() method being called. This method can either return a potentially modified packet, STOP, or DISCONNECT. If a write Protocol returns STOP no data will be written out the Interface and it is assumed that more packets are necessary before a final packet can be output. DISCONNECT will disconnect the Interface. If the packet makes it through all the write Protocol's write_packet() methods, then it is converted to binary data using the Interface's convert_packet_to_data() method. Next the write_data() method is called for each write Protocol giving it a chance to modify the lower level data. The same return options are available except a Ruby string of data is returned instead of a COSMOS packet. If the data makes it through all write_data() methods, then it is written out on the Interface using the write_interface() method. Afterwards, each Protocol's post_write_interface() method is called with both the final modified Packet, and the actual data written out to the Interface. This method allows follow-up such as waiting for a response after writing out a message."}),"\n",(0,s.jsx)(t.h2,{id:"method-discussions",children:"Method discussions"}),"\n",(0,s.jsxs)(t.h3,{id:"initialize-or-init",children:["initialize or ",(0,s.jsx)(t.strong,{children:"init"})]}),"\n",(0,s.jsx)(t.p,{children:"This is the constructor for your custom Protocol. It should always call super(allow_empty_data) to initialize the base Protocol class."}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"# @param allow_empty_data [true/false] Whether STOP should be returned on empty data\ndef initialize(allow_empty_data = false)\n @interface = nil\n @allow_empty_data = ConfigParser.handle_true_false(allow_empty_data)\n reset()\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:"def __init__(self, allow_empty_data=None):\n self.interface = None\n self.allow_empty_data = ConfigParser.handle_true_false_none(allow_empty_data)\n self.reset()\n"})}),"\n",(0,s.jsxs)(t.p,{children:["As you can see, every Protocol maintains state on at least two items. The interface variable holds the Interface class instance that the protocol is associated with. This is sometimes necessary to introspect details that only the Interface knows. allow_empty_data is a flag used by the ",(0,s.jsx)(t.code,{children:"read_data(data)"})," method that is discussed later in this document."]}),"\n",(0,s.jsx)(t.h3,{id:"reset",children:"reset"}),"\n",(0,s.jsx)(t.p,{children:"The reset method is used to reset internal protocol state when the Interface is connected and/or disconnected. This method should be used for common resetting logic. Connect and Disconnect specific logic are handled in the next two methods."}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"def reset\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:"def reset(self):\n pass\n"})}),"\n",(0,s.jsx)(t.p,{children:"As you can see, the base class reset implementation doesn't do anything."}),"\n",(0,s.jsx)(t.h3,{id:"connect_reset",children:"connect_reset"}),"\n",(0,s.jsx)(t.p,{children:"The connect_reset method is used to reset internal Protocol state each time the Interface is connected."}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"def connect_reset\n reset()\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:"def connect_reset(self):\n self.reset()\n"})}),"\n",(0,s.jsx)(t.p,{children:"The base class connect_reset implementation just calls the reset method to ensure common reset logic is run."}),"\n",(0,s.jsx)(t.h3,{id:"disconnect_reset",children:"disconnect_reset"}),"\n",(0,s.jsx)(t.p,{children:"The disconnect_reset method is used to reset internal Protocol state each time the Interface is disconnected."}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"def disconnect_reset\n reset()\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:"def disconnect_reset(self):\n self.reset()\n"})}),"\n",(0,s.jsx)(t.p,{children:"The base class disconnect_reset implementation just calls the reset method to ensure common reset logic is run."}),"\n",(0,s.jsx)(t.h3,{id:"read_data",children:"read_data"}),"\n",(0,s.jsx)(t.p,{children:"The read_data method is used to analyze and potentially modify any raw data read by an Interface. It takes one parameter as the current state of the data to be analyzed. It can return either a string of data, STOP, or DISCONNECT. If it returns a string, then it believes that data may be ready to be a full packet, and is ready for processing by any following Protocols. If STOP is returned then the Protocol believes it needs more data to complete a full packet. If DISCONNECT is returned then the Protocol believes the Interface should be disconnected (and typically automatically reconnected)."}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"def read_data(data)\n if (data.length <= 0)\n if @allow_empty_data.nil?\n if @interface and @interface.read_protocols[-1] == self # Last read interface in chain with auto @allow_empty_data\n return :STOP\n end\n elsif !@allow_empty_data # Don't @allow_empty_data means STOP\n return :STOP\n end\n end\n data\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:'def read_data(self, data, extra=None):\n if len(data) <= 0:\n if self.allow_empty_data is None:\n if self.interface and self.interface.read_protocols[-1] == self:\n # Last read interface in chain with auto self.allow_empty_data\n return ("STOP", extra)\n elif self.allow_empty_data:\n # Don\'t self.allow_empty_data means STOP\n return ("STOP", extra)\n return (data, extra)\n'})}),"\n",(0,s.jsxs)(t.p,{children:["The base class implementation does nothing except return the data it was given. The only exception to this is when handling an empty string. If the allow_empty_data flag is false / False or if it is nil / None and the Protocol is the last in the chain, then the base implementation will return STOP to indicate that it is time to call the Interface ",(0,s.jsx)(t.code,{children:"read_interface()"})," method to get more data. Blank strings are used to signal Protocols that they have an opportunity to return a cached packet."]}),"\n",(0,s.jsx)(t.h3,{id:"read_packet",children:"read_packet"}),"\n",(0,s.jsx)(t.p,{children:"The read_packet method is used to analyze and potentially modify a COSMOS packet before it is returned by the Interface. It takes one parameter as the current state of the packet to be analyzed. It can return either a COSMOS packet, STOP, or DISCONNECT. If it returns a COSMOS packet, then it believes that the packet is valid, should be returned, and is ready for processing by any following Protocols. If STOP is returned then the Protocol believes the packet should be silently dropped. If DISCONNECT is returned then the Protocol believes the Interface should be disconnected (and typically automatically reconnected). This method is where a Protocol would set the stored flag on a packet if it determines that the packet is stored telemetry instead of real-time telemetry."}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"def read_packet(packet)\n return packet\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:"def read_packet(self, packet):\n return packet\n"})}),"\n",(0,s.jsx)(t.p,{children:"The base class always just returns the packet given."}),"\n",(0,s.jsx)(t.h3,{id:"write_packet",children:"write_packet"}),"\n",(0,s.jsx)(t.p,{children:"The write_packet method is used to analyze and potentially modify a COSMOS packet before it is output by the Interface. It takes one parameter as the current state of the packet to be analyzed. It can return either a COSMOS packet, STOP, or DISCONNECT. If it returns a COSMOS packet, then it believes that the packet is valid, should be written out the Interface, and is ready for processing by any following Protocols. If STOP is returned then the Protocol believes the packet should be silently dropped. If DISCONNECT is returned then the Protocol believes the Interface should be disconnected (and typically automatically reconnected)."}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"def write_packet(packet)\n return packet\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:"def write_packet(self, packet):\n return packet\n"})}),"\n",(0,s.jsx)(t.p,{children:"The base class always just returns the packet given."}),"\n",(0,s.jsx)(t.h3,{id:"write_data",children:"write_data"}),"\n",(0,s.jsx)(t.p,{children:"The write_data method is used to analyze and potentially modify data before it is written out by the Interface. It takes one parameter as the current state of the data to be analyzed and sent. It can return either a string of data, STOP, or DISCONNECT. If it returns a string of data, then it believes that the data is valid, should be written out the Interface, and is ready for processing by any following Protocols. If STOP is returned then the Protocol believes the data should be silently dropped. If DISCONNECT is returned then the Protocol believes the Interface should be disconnected (and typically automatically reconnected)."}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"def write_data(data)\n return data\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:"def write_data(self, data, extra=None):\n return (data, extra)\n"})}),"\n",(0,s.jsx)(t.p,{children:"The base class always just returns the data given."}),"\n",(0,s.jsx)(t.h3,{id:"post_write_interface",children:"post_write_interface"}),"\n",(0,s.jsxs)(t.p,{children:["The post_write_interface method is called after data has been written out the Interface. The typical use of this method is to provide a hook to implement command/response type interfaces where a response is always immediately expected in response to a command. It takes two parameters, the packet after all modifications by ",(0,s.jsx)(t.code,{children:"write_packet()"})," and the data that was actually written out the Interface. It can return either the same pair of packet/data, STOP, or DISCONNECT. If it returns a packet/data pair then they are passed on to any other Protocols. If STOP is returned then the Interface write() call completes and no further Protocols ",(0,s.jsx)(t.code,{children:"post_write_interface()"}),' methods are called. If DISCONNECT is returned then the Protocol believes the Interface should be disconnected (and typically automatically reconnected). Note that only the first parameter "packet", is checked to be STOP, or DISCONNECT on the return.']}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"def post_write_interface(packet, data)\n return packet, data\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:"def post_write_interface(self, packet, data, extra=None):\n return (packet, data, extra)\n"})}),"\n",(0,s.jsx)(t.p,{children:"The base class always just returns the packet/data given."}),"\n",(0,s.jsx)(t.h3,{id:"protocol_cmd",children:"protocol_cmd"}),"\n",(0,s.jsxs)(t.p,{children:["The protocol_cmd method is used to send commands to the protocol itself. This is useful to change protocol behavior during runtime. See ",(0,s.jsx)(t.a,{href:"../guides/scripting-api#interface_protocol_cmd",children:"interface_protocol_cmd"})," for more information."]}),"\n",(0,s.jsx)(t.p,{children:"Base class Ruby implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ruby",children:"def protocol_cmd(cmd_name, *cmd_args)\n # Default do nothing - Implemented by subclasses\n return false\nend\n"})}),"\n",(0,s.jsx)(t.p,{children:"Base class Python implementation:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-python",children:"def protocol_cmd(self, cmd_name, *cmd_args):\n # Default do nothing - Implemented by subclasses\n return False\n"})}),"\n",(0,s.jsx)(t.p,{children:"The base class does nothing as this is special functionality implemented by subclasses."}),"\n",(0,s.jsx)(t.h2,{id:"examples",children:"Examples"}),"\n",(0,s.jsxs)(t.p,{children:["Please see the linked ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/lib/openc3/interfaces/protocols",children:"Ruby Protocol"})," and ",(0,s.jsx)(t.a,{href:"https://github.com/OpenC3/cosmos/blob/main/openc3/python/openc3/interfaces/protocols",children:"Python Protocol"})," code for examples of the above methods in action."]})]})}function h(e={}){const{wrapper:t}={...(0,r.R)(),...e.components};return t?(0,s.jsx)(t,{...e,children:(0,s.jsx)(l,{...e})}):l(e)}},5946:(e,t,n)=>{n.d(t,{A:()=>s});const s=n.p+"assets/images/interface_read_logic-0deef27c1d2fec9de2a3720a3c9ecedb21a00dca7aabd13da0b8815c037e4c26.png"},887:(e,t,n)=>{n.d(t,{A:()=>s});const s=n.p+"assets/images/interface_write_logic-3360afe1b4af20b10dafba848a21e09baccaacc06e2ec8b6c132ac7a3c99e994.png"},8453:(e,t,n)=>{n.d(t,{R:()=>i,x:()=>o});var s=n(6540);const r={},a=s.createContext(r);function i(e){const t=s.useContext(a);return s.useMemo((function(){return"function"==typeof e?e(t):{...t,...e}}),[t,e])}function o(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:i(e.components),s.createElement(a.Provider,{value:t},e.children)}}}]);