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

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

@return True if this was successfully decoded, false otherwise
This decodes a SL Command @param buf The buffer to use @param length The length of the buffer @param cmd The command struct to put the data into @return True if this was successfully decoded false otherwise
bool decodeSLPacket(uint8_t *buf, uint8_t length, SLPacket *pkt)
{
    if ((length == 0) || (buf == NULL) || (pkt == NULL)) {
        return false;
    }
    memset(pkt, 0, sizeof(SLPacket));
    pkt->cmd = buf[0];
    pkt->type = Bad;
    pkt->data = 0;
    switch (buf[0]) {
        case 'O':    
            pkt->type = Open;
            pkt->data = 0;
            break;
        case 'C':    
            pkt->type = Close;
            pkt->data = 0;
            break;
        case 'L':    
            pkt->type = Listen;
            pkt->data = 0;
            break;
        case 'S':    
            if ((length > 1) && (buf[1] != '\r')) {
                pkt->data = decodeNumber(&buf[1], 1);
                if (pkt->data < 9) {
                    pkt->type = Speed;
                }
            }
            break;
        case 'T':
        case 't':
        case 'R':
        case 'r':
            if (decodeSLCANFrame(buf, length, &pkt->frame)) {
                pkt->type = Frame;
            }
            break;
        default:
            break;
    }
    return pkt->type != Bad;
}
bool decodeSLPacket ( uint8_t * buf , uint8_t length , SLPacket * pkt ) { if ( ( length == 0 ) || ( buf == NULL ) || ( pkt == NULL ) ) { return false ; } memset ( pkt , 0 , sizeof ( SLPacket ) ) ; pkt -> cmd = buf [ 0 ] ; pkt -> type = Bad ; pkt -> data = 0 ; switch ( buf [ 0 ] ) { case ' ' : pkt -> type = Open ; pkt -> data = 0 ; break ; case ' ' : pkt -> type = Close ; pkt -> data = 0 ; break ; case ' ' : pkt -> type = Listen ; pkt -> data = 0 ; break ; case ' ' : if ( ( length > 1 ) && ( buf [ 1 ] != ' \r ' ) ) { pkt -> data = decodeNumber ( & buf [ 1 ] , 1 ) ; if ( pkt -> data < 9 ) { pkt -> type = Speed ; } } break ; case ' ' : case ' ' : case ' ' : case ' ' : if ( decodeSLCANFrame ( buf , length , & pkt -> frame ) ) { pkt -> type = Frame ; } break ; default : break ; } return pkt -> type != Bad ; }
This decodes a SL Command
This decodes a SL Command
// Packet Too Short // Open // Close // Listen // Speed