Skip to content

Commit

Permalink
can logger: gvret base support (work in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
markwj committed Jul 9, 2019
1 parent c69a021 commit 5c5273e
Show file tree
Hide file tree
Showing 2 changed files with 370 additions and 0 deletions.
167 changes: 167 additions & 0 deletions vehicle/OVMS.V3/components/can/src/canformat_gvret.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
; Project: Open Vehicle Monitor System
; Module: CAN dump framework
; Date: 18th January 2018
;
; (C) 2018 Mark Webb-Johnson
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
*/

#include "ovms_log.h"
static const char *TAG = "canformat-gvret";

#include <errno.h>
#include <endian.h>
#include "pcp.h"
#include "canformat_gvret.h"

////////////////////////////////////////////////////////////////////////
// Initialisation and Registration
////////////////////////////////////////////////////////////////////////

class OvmsCanFormatGVRETInit
{
public: OvmsCanFormatGVRETInit();
} MyOvmsCanFormatGVRETInit __attribute__ ((init_priority (4505)));

OvmsCanFormatGVRETInit::OvmsCanFormatGVRETInit()
{
ESP_LOGI(TAG, "Registering CAN Format: GVRET (4505)");

MyCanFormatFactory.RegisterCanFormat<canformat_gvret_ascii>("gvret-a");
MyCanFormatFactory.RegisterCanFormat<canformat_gvret_binary>("gvret-b");
}

////////////////////////////////////////////////////////////////////////
// Base GVRET implementation (utility)
////////////////////////////////////////////////////////////////////////

canformat_gvret::canformat_gvret(const char* type)
: canformat(type)
{
m_bufpos = 0;
m_discarding = false;
}

canformat_gvret::~canformat_gvret()
{
}

std::string canformat_gvret::get(CAN_log_message_t* message)
{
return std::string("");
}

std::string canformat_gvret::getheader(struct timeval *time)
{
return std::string("");
}

size_t canformat_gvret::put(CAN_log_message_t* message, uint8_t *buffer, size_t len)
{
return len;
}

////////////////////////////////////////////////////////////////////////
// ASCII format GVRET
////////////////////////////////////////////////////////////////////////

canformat_gvret_ascii::canformat_gvret_ascii(const char* type)
: canformat_gvret(type)
{
}

std::string canformat_gvret_ascii::get(CAN_log_message_t* message)
{
char buf[CANFORMAT_GVRET_MAXLEN];

if ((message->type != CAN_LogFrame_RX)&&
(message->type != CAN_LogFrame_TX))
{
return std::string("");
}

const char* busnumber;
if (message->origin != NULL)
{ busnumber = message->origin->GetName()+3; }
else
{ busnumber = "1"; }

sprintf(buf,"%u - %x %s %c %d",
(uint32_t)((message->timestamp.tv_sec * 1000000) + message->timestamp.tv_usec),
message->frame.MsgID,
(message->frame.FIR.B.FF == CAN_frame_std) ? "S" : "X",
busnumber[0]-1,
message->frame.FIR.B.DLC);
for (int k=0; k<message->frame.FIR.B.DLC; k++)
sprintf(buf+strlen(buf)," %02x", message->frame.data.u8[k]);

strcat(buf,"\n");
return std::string(buf);
}

size_t canformat_gvret_ascii::put(CAN_log_message_t* message, uint8_t *buffer, size_t len)
{
return len;
}

////////////////////////////////////////////////////////////////////////
// BINARY format GVRET
////////////////////////////////////////////////////////////////////////

canformat_gvret_binary::canformat_gvret_binary(const char* type)
: canformat_gvret(type)
{
m_expecting = 0;
}

std::string canformat_gvret_binary::get(CAN_log_message_t* message)
{
gvret_binary_frame_t frame;
memset(&frame,0,sizeof(frame));

if ((message->type != CAN_LogFrame_RX)&&
(message->type != CAN_LogFrame_TX))
{
return std::string("");
}

const char* busnumber;
if (message->origin != NULL)
{ busnumber = message->origin->GetName()+3; }
else
{ busnumber = "1"; }


frame.startbyte = GVRET_START_BYTE;
frame.command = BUILD_CAN_FRAME;
frame.microseconds = (uint32_t)((message->timestamp.tv_sec * 1000000) + message->timestamp.tv_usec);
frame.id = (uint32_t)message->frame.MsgID +
(message->frame.FIR.B.FF == CAN_frame_std)? 0 : 0x8000;
frame.lenbus = message->frame.FIR.B.DLC + ((busnumber[0]-1)<<4);
for (int k=0; k<message->frame.FIR.B.DLC; k++)
frame.data[k] = message->frame.data.u8[k];
return std::string((const char*)&frame,12 + message->frame.FIR.B.DLC);
}

size_t canformat_gvret_binary::put(CAN_log_message_t* message, uint8_t *buffer, size_t len)
{
return len;
}
203 changes: 203 additions & 0 deletions vehicle/OVMS.V3/components/can/src/canformat_gvret.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
; Project: Open Vehicle Monitor System
; Module: CAN dump CRTD format
; Date: 18th January 2018
;
; (C) 2018 Mark Webb-Johnson
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
*/

#ifndef __CANFORMAT_GVRET_H__
#define __CANFORMAT_GVRET_H__

#include "canformat.h"

#define CANFORMAT_GVRET_MAXLEN 48

#define GVRET_SET_BINARY 0xe7
#define GVRET_START_BYTE 0xf1
#define GVRET_NOTDEAD_1 0xde
#define GVRET_NOTDEAD_2 0xed

typedef enum
{
BUILD_CAN_FRAME = 0,
TIME_SYNC,
GET_DIG_INPUTS,
GET_ANALOG_INPUTS,
SET_DIG_OUTPUTS,
SETUP_CANBUS,
GET_CANBUS_PARAMS,
GET_DEVICE_INFO,
SET_SINGLEWIRE_MODE,
KEEP_ALIVE,
SET_SYSTEM_TYPE,
ECHO_CAN_FRAME,
GET_NUM_BUSES,
GET_EXT_BUSES
} gvret_cmd_t;

typedef struct __attribute__ ((__packed__))
{
uint8_t startbyte;
uint8_t command;
union
{
struct // 0 - Send a frame out one of the buses
{
uint32_t id; // Frame ID LSB to MSB
uint8_t bus; // which bus (0 = CAN0, 1 = CAN1, 2 = SWCAN, 3 = LIN1, 4 = LIN2)
uint8_t length; // Frame length
uint8_t data[8]; // Data bytes
} build_can_frame;
struct // 4 - Set state of digital outputs
{
uint8_t outputvals; // Bitfield - Bit 0 = DIgital output 1, etc. 0 = Low, 1 = High
} set_dig_outputs;
struct // 5 - Set CAN bus configuration
{
uint32_t can1; // CAN0 Speed. If Bit 31 set then Bit 30 = Enable Bit 29 = Listen Only
uint32_t can2; // CAN1 Speed. Same deal, if bit 31 set then use 30 and 29 for config options
} setup_canbus;
struct // 8 - Set singlewire mode
{
uint8_t mode; // if 0x10 then single wire mode, otherwise no. Not very applicable to M2
} set_singlewire_mode;
struct // 10 - Set system type
{
uint8_t type; // System type (right now only 0 for M2 project but GVRET takes 0-2)
} set_system_type;
struct // 11 - Echo CAN frame
{
uint32_t id; // Frame ID LSB to MSB
uint8_t bus; // which bus (0 = CAN0, 1 = CAN1, 2 = SWCAN, 3 = LIN1, 4 = LIN2)
uint8_t length; // Frame length
uint8_t data[8]; // Byte 6-? - Data bytes
} echo_can_frame;
} body;
} gvret_commandmsg_t;

typedef struct __attribute__ ((__packed__))
{
uint8_t startbyte;
uint8_t command;
union
{
struct // 1 - Time Sync
{
uint32_t microseconds; // Microseconds since start up LSB to MSB
} time_sync;
struct // 2 - Set state of digital inputs
{
uint8_t inputvals; // Bitfield of inputs (Bit 0 = Input 0, etc) 0 = Low, 1 = High
uint8_t checksum; // Checksum byte
} get_dig_inputs;
struct // 3 - Get state of analog input pins
{
uint16_t inputvals[4]; // Analog input LSB then MSB
uint8_t checksum; // Checksum byte
} get_analog_inputs;
struct // 6 - Get CAN bus config
{
uint8_t can1_mode; // Bit 0 - Enable Bit 4 - Listen only
uint32_t can1_speed; // CAN0 Speed LSB to MSB
uint8_t can2_mode; // Bit 0 - Enable Bit 4 - ListenOnly
uint32_t can2_speed; // CAN1 Speed LSB to MSB
} get_canbus_params;
struct // 7 - Get device info
{
uint16_t build; // Build number LSB to MSB
uint8_t eeprom; // EEPROM version
uint8_t filetype; // File output type
uint8_t autolog; // Auto start logging
uint8_t singlewire; // Single wire mode
} get_device_info;
struct // 9 - Keep alive
{
uint8_t notdead1; // 0xDE
uint8_t notdead2; // 0xAD - So, if it isn't dead it responds with DEAD.
} keep_alive;
struct
{
uint8_t buses; // Number of buses
} get_num_buses;
struct
{
uint8_t swcan_mode; // Bit 0 - Enable Bit 4 - Listen only
uint32_t swcan_speed; // CAN0 Speed LSB to MSB
uint8_t lin1_mode; // Bit 0 - Enable Bit 4 - ListenOnly
uint32_t lin1_speed; // CAN1 Speed LSB to MSB
uint8_t lin2_mode; // Bit 0 - Enable Bit 4 - ListenOnly
uint32_t lin2_speed; // CAN1 Speed LSB to MSB
} get_ext_buses;
} body;
} gvret_replymsg_t;

typedef struct __attribute__ ((__packed__))
{
uint8_t startbyte; // 0xF1
uint8_t command; // 0x00
uint32_t microseconds; // Microseconds since start up LSB to MSB
uint32_t id; // Frame ID, Bit 31 - Extended Frame
uint8_t lenbus; // Frame length in bottom 4 bits, Bus received on in upper 4 bits
uint8_t data[9]; // Data bytes (plus zero termination)
} gvret_binary_frame_t;

class canformat_gvret : public canformat
{
public:
canformat_gvret(const char* type);
virtual ~canformat_gvret();

protected:
union
{
gvret_commandmsg_t m_cmd;
uint8_t m_buf[CANFORMAT_GVRET_MAXLEN];
} m_msg;
size_t m_bufpos;
bool m_discarding;

public:
virtual std::string get(CAN_log_message_t* message);
virtual std::string getheader(struct timeval *time);
virtual size_t put(CAN_log_message_t* message, uint8_t *buffer, size_t len);
};

class canformat_gvret_ascii : public canformat_gvret
{
public:
canformat_gvret_ascii(const char* type);
virtual std::string get(CAN_log_message_t* message);
virtual size_t put(CAN_log_message_t* message, uint8_t *buffer, size_t len);
};

class canformat_gvret_binary : public canformat_gvret
{
public:
canformat_gvret_binary(const char* type);
virtual std::string get(CAN_log_message_t* message);
virtual size_t put(CAN_log_message_t* message, uint8_t *buffer, size_t len);

public:
size_t m_expecting;
};

#endif // __CANFORMAT_GVRET_H__

0 comments on commit 5c5273e

Please sign in to comment.