src/slcan_decode.c
/**
 * 
 * This decodes a data packet
 * 
 * @param buf    The buffer to use
 * @param length The length of the buffer
 * @param frame  The frame struct to put the data into
 * 
 * @return True if this was successfully decoded, false otherwise
 */
This decodes a data packet

@param buf    The buffer to use
@param length The length of the buffer
@param frame  The frame struct to put the data into

@return True if this was successfully decoded, false otherwise
This decodes a data packet @param buf The buffer to use @param length The length of the buffer @param frame The frame struct to put the data into @return True if this was successfully decoded false otherwise
bool decodeSLCANFrame(uint8_t *buf, uint8_t length, SLCANFrame *frame)
{
    initCANFrame(frame);
    if ((length < 5) || (buf == NULL) || (frame == NULL)) {
        return false;
    }
    length = (buf[length - 1] == '\r') ? length - 1 : length;
    if ((buf[0] == 'T') && (length >= 10)) {
        frame->ext = true;
        frame->rtr = false;
        frame->id = decodeNumber(&buf[1], 8) & SLCAN_EXT_ID_MASK;
        frame->length = (uint8_t)decodeNumber(&buf[9], 1);
        if (length >= (frame->length + frame->length + 10)) {
            return decodeByteData(&buf[10], frame->length * 2, frame->data);
        }
        return false;
    } else if ((buf[0] == 'R') && (length >= 10)) {
        frame->ext = true;
        frame->rtr = true;
        frame->id = decodeNumber(&buf[1], 8) & SLCAN_EXT_ID_MASK;
        frame->length = (uint8_t)decodeNumber(&buf[9], 1);
        return true;
    } else if (buf[0] == 't') {
        frame->ext = false;
        frame->rtr = false;
        frame->id = decodeNumber(&buf[1], 3) & SLCAN_ID_MASK;
        frame->length = (uint8_t)decodeNumber(&buf[4], 1);
        if (length >= (frame->length + frame->length + 5)) {
            return decodeByteData(&buf[5], frame->length * 2, frame->data);
        }
        return false;
    } else if ((buf[0] == 'r')  && (length >= 5)) {
        frame->ext = false;
        frame->rtr = true;
        frame->id = decodeNumber(&buf[1], 3) & SLCAN_ID_MASK;
        frame->length = (uint8_t)decodeNumber(&buf[4], 1);
        return true;
    }
    return false;
}
bool decodeSLCANFrame ( uint8_t * buf , uint8_t length , SLCANFrame * frame ) { initCANFrame ( frame ) ; if ( ( length < 5 ) || ( buf == NULL ) || ( frame == NULL ) ) { return false ; } length = ( buf [ length - 1 ] == ' \r ' ) ? length - 1 : length ; if ( ( buf [ 0 ] == ' ' ) && ( length >= 10 ) ) { frame -> ext = true ; frame -> rtr = false ; frame -> id = decodeNumber ( & buf [ 1 ] , 8 ) & SLCAN_EXT_ID_MASK ; frame -> length = ( uint8_t ) decodeNumber ( & buf [ 9 ] , 1 ) ; if ( length >= ( frame -> length + frame -> length + 10 ) ) { return decodeByteData ( & buf [ 10 ] , frame -> length * 2 , frame -> data ) ; } return false ; } else if ( ( buf [ 0 ] == ' ' ) && ( length >= 10 ) ) { frame -> ext = true ; frame -> rtr = true ; frame -> id = decodeNumber ( & buf [ 1 ] , 8 ) & SLCAN_EXT_ID_MASK ; frame -> length = ( uint8_t ) decodeNumber ( & buf [ 9 ] , 1 ) ; return true ; } else if ( buf [ 0 ] == ' ' ) { frame -> ext = false ; frame -> rtr = false ; frame -> id = decodeNumber ( & buf [ 1 ] , 3 ) & SLCAN_ID_MASK ; frame -> length = ( uint8_t ) decodeNumber ( & buf [ 4 ] , 1 ) ; if ( length >= ( frame -> length + frame -> length + 5 ) ) { return decodeByteData ( & buf [ 5 ] , frame -> length * 2 , frame -> data ) ; } return false ; } else if ( ( buf [ 0 ] == ' ' ) && ( length >= 5 ) ) { frame -> ext = false ; frame -> rtr = true ; frame -> id = decodeNumber ( & buf [ 1 ] , 3 ) & SLCAN_ID_MASK ; frame -> length = ( uint8_t ) decodeNumber ( & buf [ 4 ] , 1 ) ; return true ; } return false ; }
This decodes a data packet
This decodes a data packet
// Packet Too Short