11#include " FujiBusPacket.h"
22
3- #include " ../../include/debug.h"
4- #include " utils.h"
5-
63typedef struct {
74 uint8_t device; /* Destination Device */
85 uint8_t command; /* Command */
@@ -19,8 +16,7 @@ typedef struct {
1916static uint8_t fieldSizeTable[] = {0 , 1 , 1 , 1 , 1 , 2 , 2 , 4 };
2017static uint8_t numFieldsTable[] = {0 , 1 , 2 , 3 , 4 , 1 , 2 , 1 };
2118
22-
23- std::string FujiBusPacket::decodeSLIP (const std::string &input)
19+ std::string FujiBusPacket::decodeSLIP (std::string_view input)
2420{
2521 unsigned int idx;
2622 uint8_t val;
@@ -56,7 +52,7 @@ std::string FujiBusPacket::decodeSLIP(const std::string &input)
5652 return output;
5753}
5854
59- std::string FujiBusPacket::encodeSLIP (const std::string & input)
55+ std::string FujiBusPacket::encodeSLIP (std::string_view input)
6056{
6157 unsigned int idx;
6258 uint8_t val;
@@ -83,36 +79,36 @@ std::string FujiBusPacket::encodeSLIP(const std::string &input)
8379 return output;
8480}
8581
86- uint8_t FujiBusPacket::calcChecksum (const std::string &input )
82+ uint8_t FujiBusPacket::calcChecksum (const std::string &buf )
8783{
8884 uint16_t idx, chk;
89- uint8_t *buf = (uint8_t *) input .data ();
85+ uint8_t *ptr = (uint8_t *) buf .data ();
9086
91- for (idx = chk = 0 ; idx < input .size (); idx++)
92- chk = ((chk + buf [idx]) >> 8 ) + ((chk + buf [idx]) & 0xFF );
87+ for (idx = chk = 0 ; idx < buf .size (); idx++)
88+ chk = ((chk + ptr [idx]) >> 8 ) + ((chk + ptr [idx]) & 0xFF );
9389 return (uint8_t ) chk;
9490}
9591
96- bool FujiBusPacket::parse (const std::string & input)
92+ bool FujiBusPacket::parse (std::string_view input)
9793{
9894 std::string decoded;
9995 fujibus_header *hdr;
96+ std::string_view slipEncoded = input;
10097
101- Debug_printv (" Incoming:\n %s\n " , util_hexdump (input.data (), input.size ()).c_str ());
98+ size_t slipMarker = input.find (SLIP_END);
99+ if (slipMarker != std::string::npos)
100+ slipEncoded = std::string_view (input).substr (slipMarker);
102101
103- if (input .size () < sizeof (fujibus_header) + 2 )
102+ if (slipEncoded .size () < sizeof (fujibus_header) + 2 )
104103 return false ;
105- if (((uint8_t ) input [0 ]) != SLIP_END || ((uint8_t ) input .back ()) != SLIP_END)
104+ if (((uint8_t ) slipEncoded [0 ]) != SLIP_END || ((uint8_t ) slipEncoded .back ()) != SLIP_END)
106105 return false ;
107106
108- decoded = decodeSLIP (input);
109- Debug_printv (" Decoded:\n %s\n " , util_hexdump (decoded.data (), decoded.size ()).c_str ());
107+ decoded = decodeSLIP (slipEncoded);
110108
111109 if (decoded.size () < sizeof (fujibus_header))
112110 return false ;
113111 hdr = (fujibus_header *) &decoded[0 ];
114- Debug_printv (" Header: dev:%02x cmd:%02x len:%d chk:%02x fld:%02x" ,
115- hdr->device , hdr->command , hdr->length , hdr->checksum , hdr->descr );
116112
117113 if (hdr->length != decoded.size ())
118114 return false ;
@@ -231,12 +227,11 @@ std::string FujiBusPacket::serialize()
231227 hptr = (fujibus_header *) output.data ();
232228 *hptr = hdr;
233229 hptr->checksum = calcChecksum (output);
234- Debug_printv (" Packet header: dev:%02x cmd:%02x len:%d chk:%02x fld:%02x" ,
235- hptr->device , hptr->command , hptr->length , hptr->checksum , hptr->descr );
236- return encodeSLIP (output);
230+ auto encoded = encodeSLIP (output);
231+ return encoded;
237232}
238233
239- std::unique_ptr<FujiBusPacket> FujiBusPacket::fromSerialized (const std::string & input)
234+ std::unique_ptr<FujiBusPacket> FujiBusPacket::fromSerialized (std::string_view input)
240235{
241236 auto packet = std::make_unique<FujiBusPacket>();
242237 if (!packet->parse (input))
0 commit comments