@ -143,11 +143,10 @@ struct net_buf *mcumgr_serial_process_frag(
@@ -143,11 +143,10 @@ struct net_buf *mcumgr_serial_process_frag(
}
/**
* Base64 - encodes a small chunk of data and transmits it . The data must be no
* larger than three bytes .
* Base64 - encodes a small chunk of data and transmits it . The data must be no larger than three
* bytes .
*/
static int mcumgr_serial_tx_small ( const void * data , int len ,
mcumgr_serial_tx_cb cb , void * arg )
static int mcumgr_serial_tx_small ( const void * data , int len , mcumgr_serial_tx_cb cb )
{
uint8_t b64 [ 4 + 1 ] ; /* +1 required for null terminator. */
size_t dst_len ;
@ -157,36 +156,29 @@ static int mcumgr_serial_tx_small(const void *data, int len,
@@ -157,36 +156,29 @@ static int mcumgr_serial_tx_small(const void *data, int len,
__ASSERT_NO_MSG ( rc = = 0 ) ;
__ASSERT_NO_MSG ( dst_len = = 4 ) ;
return cb ( b64 , 4 , arg ) ;
return cb ( b64 , 4 ) ;
}
/**
* @ brief Transmits a single mcumgr frame over serial .
* @ brief Transmits a single mcumgr packet over serial , splits into multiple frames as needed .
*
* @ param data The frame payload to transmit . This does not
* include a header or CRC .
* @ param first Whether this is the first frame in the packet .
* @ param len The number of untransmitted data bytes in the
* packet .
* @ param crc The 16 - bit CRC of the entire packet .
* @ param data The packet payload to transmit . This does not include a header or
* CRC .
* @ param len The size of the packet payload .
* @ param cb A callback used for transmitting raw data .
* @ param arg An optional argument that gets passed to the
* callback .
* @ param out_data_bytes_txed On success , the number of data bytes
* transmitted gets written here .
*
* @ return 0 on success ; negative error code on failure .
*/
int mcumgr_serial_tx_frame ( const uint8_t * data , bool first , int len ,
uint16_t crc , mcumgr_serial_tx_cb cb , void * arg ,
int * out_data_bytes_txed )
int mcumgr_serial_tx_pkt ( const uint8_t * data , int len , mcumgr_serial_tx_cb cb )
{
bool first = true ;
bool last = false ;
uint8_t raw [ 3 ] ;
uint16_t u16 ;
uint16_t crc ;
int src_off = 0 ;
int rc = 0 ;
int to_process ;
bool last = false ;
int reminder ;
/*
@ -197,22 +189,23 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
@@ -197,22 +189,23 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
*/
int max_input = ( ( ( MCUMGR_SERIAL_MAX_FRAME - 3 ) > > 2 ) * 3 ) ;
if ( first ) {
/* Calculate the CRC16 checksum of the whole packet, prior to splitting it into frames */
crc = mcumgr_serial_calc_crc ( data , len ) ;
/* First frame marker */
u16 = sys_cpu_to_be16 ( MCUMGR_SERIAL_HDR_PKT ) ;
} else {
/* Continuation frame marker */
u16 = sys_cpu_to_be16 ( MCUMGR_SERIAL_HDR_FRAG ) ;
}
rc = cb ( & u16 , sizeof ( u16 ) , arg ) ;
while ( src_off < len ) {
/* Send first frame or continuation frame marker */
rc = cb ( & u16 , sizeof ( u16 ) ) ;
if ( rc ! = 0 ) {
return rc ;
}
/*
* Only the first fragment contains the packet length ; the packet length , which is two
* byte long is paired with first byte of input buffer to form triplet for Base64 encoding .
* Only the first fragment contains the packet length ; the packet length , which is
* two byte long is paired with first byte of input buffer to form triplet for
* Base64 encoding .
*/
if ( first ) {
/* The size of the CRC16 should be added to packet length */
@ -220,7 +213,7 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
@@ -220,7 +213,7 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
memcpy ( raw , & u16 , sizeof ( u16 ) ) ;
raw [ 2 ] = data [ 0 ] ;
rc = mcumgr_serial_tx_small ( raw , 3 , cb , arg ) ;
rc = mcumgr_serial_tx_small ( raw , 3 , cb ) ;
if ( rc ! = 0 ) {
return rc ;
}
@ -231,29 +224,36 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
@@ -231,29 +224,36 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
}
/*
* Only as much as fits into the frame can be processed , but we also need to fit in the
* two byte CRC . The frame can not be stretched and current logic does not allow to
* send CRC separately so if CRC would not fit as a whole , shrink to_process by one byte
* forcing one byte to the next frame , with the CRC .
* Only as much as fits into the frame can be processed , but we also need to fit
* in the two byte CRC . The frame can not be stretched and current logic does not
* allow to send CRC separately so if CRC would not fit as a whole , shrink
* to_process by one byte forcing one byte to the next frame , with the CRC .
*/
to_process = MIN ( max_input , len - src_off ) ;
reminder = max_input - ( len - src_off ) ;
if ( reminder = = 0 | | reminder = = 1 ) {
to_process - = 1 ;
/* This is the last frame but the checksum cannot be added, remove the
* last packet flag until the function runs again with the final piece of
* data and place the checksum there
*/
last = false ;
} else if ( reminder > = 2 ) {
last = true ;
}
/*
* Try to process input buffer by chunks of three bytes that will be output as four byte
* chunks , due to Base64 encoding ; the number of chunks that can be processed is calculated
* from number of three byte , complete , chunks in input buffer , but can not be greater
* than the number of four byte , complete , chunks that the frame can accept .
* Try to process input buffer by chunks of three bytes that will be output as
* four byte chunks , due to Base64 encoding ; the number of chunks that can be
* processed is calculated from number of three byte , complete , chunks in input
* buffer , but can not be greater than the number of four byte , complete , chunks
* that the frame can accept .
*/
while ( to_process > = 3 ) {
memcpy ( raw , data + src_off , 3 ) ;
rc = mcumgr_serial_tx_small ( raw , 3 , cb , arg ) ;
rc = mcumgr_serial_tx_small ( raw , 3 , cb ) ;
if ( rc ! = 0 ) {
return rc ;
}
@ -263,14 +263,14 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
@@ -263,14 +263,14 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
if ( last ) {
/*
* Process the reminder bytes of the input buffer , after sending it in three byte
* chunks , and CRC .
* Process the reminder bytes of the input buffer , after sending it in
* three byte chunks , and CRC .
*/
switch ( len - src_off ) {
case 0 :
raw [ 0 ] = ( crc & 0xff00 ) > > 8 ;
raw [ 1 ] = crc & 0x00ff ;
rc = mcumgr_serial_tx_small ( raw , 2 , cb , arg ) ;
rc = mcumgr_serial_tx_small ( raw , 2 , cb ) ;
break ;
case 1 :
@ -278,7 +278,7 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
@@ -278,7 +278,7 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
raw [ 1 ] = ( crc & 0xff00 ) > > 8 ;
raw [ 2 ] = crc & 0x00ff ;
rc = mcumgr_serial_tx_small ( raw , 3 , cb , arg ) ;
rc = mcumgr_serial_tx_small ( raw , 3 , cb ) ;
break ;
case 2 :
@ -286,13 +286,13 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
@@ -286,13 +286,13 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
raw [ 1 ] = data [ src_off + + ] ;
raw [ 2 ] = ( crc & 0xff00 ) > > 8 ;
rc = mcumgr_serial_tx_small ( raw , 3 , cb , arg ) ;
rc = mcumgr_serial_tx_small ( raw , 3 , cb ) ;
if ( rc ! = 0 ) {
return rc ;
}
raw [ 0 ] = crc & 0x00ff ;
rc = mcumgr_serial_tx_small ( raw , 1 , cb , arg ) ;
rc = mcumgr_serial_tx_small ( raw , 1 , cb ) ;
break ;
}
@ -301,35 +301,14 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
@@ -301,35 +301,14 @@ int mcumgr_serial_tx_frame(const uint8_t *data, bool first, int len,
}
}
rc = cb ( " \n " , 1 , arg ) ;
if ( rc ! = 0 ) {
return rc ;
}
* out_data_bytes_txed = src_off ;
return 0 ;
}
int mcumgr_serial_tx_pkt ( const uint8_t * data , int len , mcumgr_serial_tx_cb cb ,
void * arg )
{
uint16_t crc ;
int data_bytes_txed = 0 ;
int rc ;
/* Calculate CRC of entire packet. */
crc = mcumgr_serial_calc_crc ( data , len ) ;
/* Transmit packet as a sequence of frames. */
while ( len ) {
rc = mcumgr_serial_tx_frame ( data , data_bytes_txed = = 0 , len , crc , cb , arg ,
& data_bytes_txed ) ;
rc = cb ( " \n " , 1 ) ;
if ( rc ! = 0 ) {
return rc ;
}
data + = data_bytes_txed ;
len - = data_bytes_txed ;
/* Use a continuation frame marker for the next packet */
u16 = sys_cpu_to_be16 ( MCUMGR_SERIAL_HDR_FRAG ) ;
first = false ;
}
return 0 ;