Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
gc_format.cxx File Reference
#include <stdint.h>
#include "utils/logging.h"
#include "utils/gc_format.h"
#include "can_frame.h"

Go to the source code of this file.

Functions

static char nibble_to_ascii (int nibble)
 Build an ASCII character representation of a nibble value (uppercase hex).
 
static int ascii_to_nibble (const char c)
 Tries to parse a hex character to a nibble.
 
int gc_format_parse (const char *buf, struct can_frame *can_frame)
 Parses a GridConnect packet.
 
static void output_single (char *&dst, char value)
 Helper function for appending to a buffer ONCE.
 
static void output_double (char *&dst, char value)
 Helper function for appending to a buffer TWICE.
 
char * gc_format_generate (const struct can_frame *can_frame, char *buf, int double_format)
 Formats a can frame in the GridConnect protocol.
 

Detailed Description

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Implementations of GridConnect packet parser and formatter routines.

Author
Balazs Racz
Date
20 May 2013

Definition in file gc_format.cxx.

Function Documentation

◆ ascii_to_nibble()

static int ascii_to_nibble ( const char  c)
static

Tries to parse a hex character to a nibble.

Understands both upper and lowercase hex.

Parameters
cis the character to convert.
Returns
a converted value, or -1 if an invalid character was encountered.

Definition at line 64 of file gc_format.cxx.

◆ gc_format_generate()

char * gc_format_generate ( const struct can_frame *  can_frame,
char *  buf,
int  double_format 
)

Formats a can frame in the GridConnect protocol.

If requested, it can create the double protocol with leading !!, trailing ;; and all the characters doubled.

If the input frame is an error frame, then does not output anything and returns the input pointer.

Parameters
can_frameis the input frame.
bufis the output buffer. The caller must ensure this is big enough to hold the resulting frame (28 or 56 bytes).
double_formatif non-zero, the doubling format will be generated.
Returns
the pointer to the buffer character after the formatted can frame.

Definition at line 199 of file gc_format.cxx.

◆ gc_format_parse()

int gc_format_parse ( const char *  buf,
struct can_frame *  can_frame 
)

Parses a GridConnect packet.

Parameters
bufpoints to a character buffer that contains the packet. The leading ":" must be already removed, the tailing ';' must be replaced by a \0 char.
can_frameis the CAN frame that will be filled based on the source packet.
Returns
0 in case of success, -1 if there was a packet format error (in this case the frame is set to an error frame).

Definition at line 82 of file gc_format.cxx.

◆ nibble_to_ascii()

static char nibble_to_ascii ( int  nibble)
static

Build an ASCII character representation of a nibble value (uppercase hex).

Parameters
nibbleto convert
Returns
converted value

Definition at line 47 of file gc_format.cxx.

◆ output_double()

static void output_double ( char *&  dst,
char  value 
)
static

Helper function for appending to a buffer TWICE.

Used in the implementation of the double-byte gridconnect output protocol.

Parameters
dstbuffer to append data to
valueone character to append to.

Definition at line 176 of file gc_format.cxx.

◆ output_single()

static void output_single ( char *&  dst,
char  value 
)
static

Helper function for appending to a buffer ONCE.

Parameters
dstbuffer to append data to
valueone character to append to.

Definition at line 165 of file gc_format.cxx.