summaryrefslogtreecommitdiffstats
path: root/Master/Real-Time Systems/mki/src/libcanio.h
blob: 8f57b14c6b0f9a5527734b6677476388b8413450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//============================================================================
// Name:
//   libcanio.h
//
// Summary:
//   A pretty simple interface to the SocketCan layer,
//   which trys to make the communication with the can interfaces easier.
//
// Created on:
//   Sep 24, 2010
//
// Author:
//   Christian Steiger
//============================================================================

#ifndef LIBCANIO_H_
#define LIBCANIO_H_

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>

#include <unistd.h>
#include <net/if.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <linux/can/error.h>

class CanIO
{
	public:
		// constructor and destructor
		CanIO();
		virtual ~CanIO();

		// connect and disconnect
		int connect(const char* const interface = "can0");
		int disconnect(void);

		// read and write messages
		ssize_t readMsg(can_frame* frame, timeval* timestamp = 0);
		ssize_t writeMsg(const can_frame* const frame);

		// socketcan options
		int showCanErrors(const can_err_mask_t err_mask = 0);
		int hideCanErrors(void);
		int enableLoopBack(void);
		int disableLoopBack(void);
		int enableRecvOwnMsgs(void);
		int disableRecvOwnMsgs(void);

		// filter functions
		int addFilter(const can_filter* const filter, const unsigned int size);
		int enableFilter(void);
		int disableFilter(void);
		int clearFilter(void);

		// debugging help
		void hideErrors(void);
		void showErrors(void);

	protected:
		// class members
		int	m_socket;
		char m_print_errors;
		char m_filter_active;
		can_filter*	m_filter;
		unsigned short int m_filter_size;

	private:
		// dont use this function, it will be obsolete when
		// the filter functions get a better implementation
		char isVisible(const unsigned int id);

};

#endif