Initial commit of files
This commit is contained in:
63
include/libcan++/CanDatabase.h
Normal file
63
include/libcan++/CanDatabase.h
Normal file
@ -0,0 +1,63 @@
|
||||
//****************************************************************************
|
||||
// Copyright (C) 2007
|
||||
// Nissan North America
|
||||
// All Rights Reserved. Proprietary and Confidential.
|
||||
//============================================================================
|
||||
|
||||
/* prevent multiple inclusions */
|
||||
#ifndef __CanDatabase__
|
||||
#define __CanDatabase__
|
||||
|
||||
/* includes *****************************************************************/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <vector>
|
||||
|
||||
#include "CanSignal.h"
|
||||
#include "CanSocket.h"
|
||||
#include "DbcParser.h"
|
||||
|
||||
/* defines ******************************************************************/
|
||||
|
||||
/* macros *******************************************************************/
|
||||
|
||||
/* structs & typedefs *******************************************************/
|
||||
|
||||
/* c class definitions ******************************************************/
|
||||
|
||||
/**
|
||||
* CAN class to handle database of CAN signals from DBC (.dbc) file
|
||||
*/
|
||||
class CanDatabase : public DbcParser {
|
||||
// public data
|
||||
public:
|
||||
|
||||
// private data
|
||||
private:
|
||||
|
||||
// static data
|
||||
|
||||
// public methods
|
||||
public:
|
||||
// constructors
|
||||
CanDatabase();
|
||||
|
||||
// destructor
|
||||
virtual ~CanDatabase();
|
||||
|
||||
// virtual functions
|
||||
|
||||
// public methods
|
||||
int FormatHeader( char *buf, int max );
|
||||
int FormatMessage( char *buf, int max );
|
||||
|
||||
int SubscribeCan( CanSocket *pCan );
|
||||
int ReadCan( CanSocket *pCan );
|
||||
|
||||
// static methods
|
||||
|
||||
// private methods
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
74
include/libcan++/CanMessage.h
Normal file
74
include/libcan++/CanMessage.h
Normal file
@ -0,0 +1,74 @@
|
||||
//****************************************************************************
|
||||
// Copyright (C) 2007
|
||||
// Nissan North America
|
||||
// All Rights Reserved. Proprietary and Confidential.
|
||||
//============================================================================
|
||||
|
||||
/* prevent multiple inclusions */
|
||||
#ifndef __CanMessage__
|
||||
#define __CanMessage__
|
||||
|
||||
/* includes *****************************************************************/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "CanSignal.h"
|
||||
#include "CanSocket.h"
|
||||
|
||||
/* defines ******************************************************************/
|
||||
|
||||
/* macros *******************************************************************/
|
||||
|
||||
/* structs & typedefs *******************************************************/
|
||||
|
||||
// based on .dbc data
|
||||
|
||||
/* c class definitions ******************************************************/
|
||||
|
||||
//
|
||||
// Uses the Peak Systems CAN Linux driver and Nissan CAN data
|
||||
//
|
||||
class CanMessage {
|
||||
// public data
|
||||
public:
|
||||
std::string Name; // Signal Name
|
||||
std::string Module; // Module Name
|
||||
u_int32_t Address; // 29 bits (standard or extended)
|
||||
u_int8_t Data[64]; // Data
|
||||
u_int Size; // DLC (CAN-FD > 8)
|
||||
|
||||
|
||||
// list of signals in the message
|
||||
std::vector<CanSignal *> Signals;
|
||||
|
||||
// private data
|
||||
private:
|
||||
|
||||
// static data
|
||||
|
||||
// public methods
|
||||
public:
|
||||
// constructors
|
||||
CanMessage();
|
||||
|
||||
// destructor
|
||||
virtual ~CanMessage();
|
||||
|
||||
// virtual functions
|
||||
|
||||
// public methods
|
||||
int Read(CanFrame *frame);
|
||||
int Write(CanFrame *frame);
|
||||
int Convert(CanFrame *frame) {return Read(frame);};
|
||||
int Subscribe(void);
|
||||
int Unsubscribe(void);
|
||||
|
||||
// static methods
|
||||
|
||||
// private methods
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
116
include/libcan++/CanSignal.h
Normal file
116
include/libcan++/CanSignal.h
Normal file
@ -0,0 +1,116 @@
|
||||
//****************************************************************************
|
||||
// Copyright (C) 2007
|
||||
// Nissan North America
|
||||
// All Rights Reserved. Proprietary and Confidential.
|
||||
//============================================================================
|
||||
|
||||
/* prevent multiple inclusions */
|
||||
#ifndef __CanSignal__
|
||||
#define __CanSignal__
|
||||
|
||||
/* includes *****************************************************************/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
//#include "BaseSignal.h"
|
||||
//#include "VoltType.h"
|
||||
|
||||
#include "CanSocket.h"
|
||||
|
||||
/* defines ******************************************************************/
|
||||
|
||||
/* macros *******************************************************************/
|
||||
|
||||
/* structs & typedefs *******************************************************/
|
||||
|
||||
// based on .dbc data
|
||||
|
||||
/* c class definitions ******************************************************/
|
||||
|
||||
//
|
||||
// Uses the Peak Systems CAN Linux driver and Nissan CAN data
|
||||
//
|
||||
#ifdef __BaseSignal__
|
||||
class CanSignal : public BaseSignal, public VoltType {
|
||||
#else
|
||||
class CanSignal {
|
||||
#endif
|
||||
// public data
|
||||
public:
|
||||
std::string Name; // Signal Name
|
||||
std::string Units; // Signal Units
|
||||
u_int32_t Address; // 29 bits (standard or extended)
|
||||
|
||||
// data conversion (from .dbc file)
|
||||
u_char StartBit; // 0..63
|
||||
u_char NumBits; // 1..32
|
||||
u_char BigEndian; // 0 = little endian (intel), 1 = big endian (moto)
|
||||
u_char IsSigned; // 0 = unsigned, 1 = 2's Complement
|
||||
double Scale; // scale
|
||||
double Offset; // offset
|
||||
|
||||
// conversion help
|
||||
u_char StartByte; // 0..7
|
||||
u_char NumBytes; // 1..4
|
||||
u_char HighBit; // 0..7
|
||||
u_char LowBit; // #bits after
|
||||
u_int64_t Mask; // 64 bit mask
|
||||
u_int64_t Sign; // 64 bit sign mask
|
||||
|
||||
// status, data
|
||||
u_char doSubscribe; // CanSocket filter
|
||||
u_char isNew;
|
||||
u_char isValid;
|
||||
int32_t rawValue;
|
||||
int64_t currValue;
|
||||
int64_t lastValue;
|
||||
double realValue;
|
||||
|
||||
// misc
|
||||
u_int64_t activity; // activity counter
|
||||
void *userData; // user data
|
||||
int userIndex; // user index
|
||||
|
||||
|
||||
// private data
|
||||
private:
|
||||
|
||||
// static data
|
||||
|
||||
// public methods
|
||||
public:
|
||||
// constructors
|
||||
CanSignal();
|
||||
|
||||
// destructor
|
||||
virtual ~CanSignal();
|
||||
|
||||
// virtual functions
|
||||
|
||||
// public methods
|
||||
int Init( void );
|
||||
int Convert(CanFrame *frame) {return Read(frame->data);};
|
||||
int Read(CanFrame *frame) {return Read(frame->data);};
|
||||
int Read(const u_char *pbyData, long *ldata=NULL, double *ddata=NULL);
|
||||
int Write(CanFrame *frame) {return Write(frame->data);};
|
||||
int Write(u_char *pbyData);
|
||||
int Write(long value, u_char *pbyData);
|
||||
long Write(double value, u_char *pbyData);
|
||||
void Subscribe(void) {doSubscribe=1;};
|
||||
void Unsubscribe(void) {doSubscribe=0;};
|
||||
|
||||
#ifndef __VoltType__
|
||||
void SetScaling( double scale=1.0, double offset=0.0 ) { Scale=scale; Offset=offset; };
|
||||
double GetScale( void ) { return Scale; };
|
||||
double GetOffset( void ) { return Offset; };
|
||||
#endif
|
||||
|
||||
// static methods
|
||||
|
||||
// private methods
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
101
include/libcan++/CanSocket.h
Normal file
101
include/libcan++/CanSocket.h
Normal file
@ -0,0 +1,101 @@
|
||||
//****************************************************************************
|
||||
// Copyright (C) 2007
|
||||
// Nissan North America
|
||||
// All Rights Reserved. Proprietary and Confidential.
|
||||
//============================================================================
|
||||
|
||||
/* prevent multiple inclusions */
|
||||
#ifndef __CanSocket__
|
||||
#define __CanSocket__
|
||||
|
||||
/* includes *****************************************************************/
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "netlib.h"
|
||||
|
||||
#include <linux/can.h>
|
||||
#include <linux/can/raw.h>
|
||||
#include <linux/can/bcm.h>
|
||||
|
||||
|
||||
/* defines ******************************************************************/
|
||||
|
||||
#define CAN_MAX_FILTER 31
|
||||
#define CAN_MAX_ADDRESSES 256
|
||||
|
||||
/* macros *******************************************************************/
|
||||
|
||||
/* structs & typedefs *******************************************************/
|
||||
|
||||
typedef struct can_filter CanFilter;
|
||||
typedef struct can_frame CanFrame;
|
||||
|
||||
typedef struct _BcmFrame {
|
||||
struct bcm_msg_head head;
|
||||
struct can_frame frame;
|
||||
} BcmFrame;
|
||||
|
||||
/* c class definitions ******************************************************/
|
||||
|
||||
//
|
||||
// Uses the Peak Systems CAN Linux driver and Nissan CAN data
|
||||
//
|
||||
class CanSocket {
|
||||
// public data
|
||||
public:
|
||||
int m_sock;
|
||||
int m_proto;
|
||||
|
||||
// private data
|
||||
private:
|
||||
// device
|
||||
int m_flags;
|
||||
|
||||
// errors
|
||||
bool m_logerrs;
|
||||
long m_errcount;
|
||||
|
||||
// static data
|
||||
|
||||
// public methods
|
||||
public:
|
||||
// constructors
|
||||
CanSocket();
|
||||
|
||||
// destructor
|
||||
virtual ~CanSocket();
|
||||
|
||||
// virtual functions
|
||||
|
||||
// public methods
|
||||
void AddrFlags( u_long f ) { m_flags = f; };
|
||||
int GetSock(void) {return m_sock;};
|
||||
int GetSocket(void) {return m_sock;};
|
||||
int GetProto(void) {return m_proto;};
|
||||
int CanOpen( const char *dev="can0", int proto=CAN_RAW, int flags=0 );
|
||||
void CanClose(void);
|
||||
|
||||
// misc
|
||||
int ClearFilters( void );
|
||||
int Blocking(bool block=true);
|
||||
int NonBlocking(void) { return Blocking(false); };
|
||||
int Loopback(bool loop=true);
|
||||
int NoLoopback(void) { return Loopback(false); };
|
||||
void LogErrors(bool logerrs=true) {m_logerrs = logerrs;};
|
||||
|
||||
// rx filter
|
||||
int CanAddRx( u_int32_t addr );
|
||||
int CanAddRx( const CanFilter *filter, int max=1 );
|
||||
int CanDelRx( u_int32_t addr );
|
||||
|
||||
// raw
|
||||
int CanRawRead( CanFrame *buf, int max=1 );
|
||||
int CanRawWrite( const CanFrame *buf, int max=1 );
|
||||
|
||||
// bcm
|
||||
int CanBcmRead( BcmFrame *msg, int max=1 );
|
||||
int CanBcmWrite( BcmFrame *msg, int max=1 );
|
||||
};
|
||||
|
||||
#endif
|
||||
87
include/libcan++/DbcParser.h
Normal file
87
include/libcan++/DbcParser.h
Normal file
@ -0,0 +1,87 @@
|
||||
//****************************************************************************
|
||||
// Copyright (C) 2007
|
||||
// Nissan North America
|
||||
// All Rights Reserved. Proprietary and Confidential.
|
||||
//============================================================================
|
||||
|
||||
/* prevent multiple inclusions */
|
||||
#ifndef __DbcParser__
|
||||
#define __DbcParser__
|
||||
|
||||
/* includes *****************************************************************/
|
||||
|
||||
#include <regex.h>
|
||||
#include <vector>
|
||||
|
||||
#include "CanMessage.h"
|
||||
#include "CanSignal.h"
|
||||
|
||||
/* defines ******************************************************************/
|
||||
|
||||
/* macros *******************************************************************/
|
||||
|
||||
/* structs & typedefs *******************************************************/
|
||||
|
||||
// based on .dbc data
|
||||
typedef struct _DbcFilter {
|
||||
// signal information
|
||||
char Name[32]; // Signal name
|
||||
regex_t preg;
|
||||
|
||||
// address range
|
||||
u_int32_t CanAddrLo; // 29 bits
|
||||
u_int32_t CanAddrHi;
|
||||
|
||||
// data conversion
|
||||
u_char StartBit; // 0..63
|
||||
u_char NumBits; // 1..32
|
||||
|
||||
int found;
|
||||
} DbcFilter;
|
||||
|
||||
/* c class definitions ******************************************************/
|
||||
|
||||
//
|
||||
// Uses the Peak Systems CAN Linux driver and Nissan CAN data
|
||||
//
|
||||
class DbcParser {
|
||||
// public data
|
||||
public:
|
||||
std::vector<DbcFilter *> MsgFilters; // message filter
|
||||
std::vector<DbcFilter *> SigFilters; // signal filter
|
||||
std::vector<CanMessage *> Messages;
|
||||
std::vector<CanSignal *> Signals;
|
||||
|
||||
// private data
|
||||
private:
|
||||
|
||||
// static data
|
||||
|
||||
// private methods
|
||||
DbcFilter *AddFilter( char *name, u_int32_t addr1, u_int32_t addr2 );
|
||||
|
||||
// public methods
|
||||
public:
|
||||
// constructors
|
||||
DbcParser();
|
||||
|
||||
// destructor
|
||||
virtual ~DbcParser();
|
||||
|
||||
// virtual functions
|
||||
|
||||
// public methods
|
||||
int AddMsgFilter( char *name, u_int32_t addr1, u_int32_t addr2 );
|
||||
int AddSigFilter( char *name, u_int32_t addr1, u_int32_t addr2 );
|
||||
int AddMessage( char *name, u_int32_t addr, u_int32_t size, char *module );
|
||||
int AddSignal( char *name, char *units, u_int32_t addr, char start, char num, char endian, char sign, float scale, float offset );
|
||||
|
||||
int LoadDatabase( const char *file );
|
||||
|
||||
// static methods
|
||||
|
||||
// private methods
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user