canio.h
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2003 John Sweeney & Brian Gerkey
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20 
21 #ifndef _CANIO_H_
22 #define _CANIO_H_
23 
24 #define HAVE_STDINT_H 1
25 
26 #if HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 #if HAVE_STDINT_H
30 #include <stdint.h>
31 #endif
32 
33 #include <sys/types.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 
38 // Copied this from <canlib.h>. I assume that it's portable across CAN
39 // implementations.
40 #ifndef canMSG_STD
41 #define canMSG_STD 0x0002
42 #endif
43 
44 
45 class CanPacket
46 {
47 public:
48  long id;
49  uint8_t msg[8];
50  uint32_t dlc;
51  uint32_t flags;
52 
53  CanPacket()
54  {
55  memset(msg,0,sizeof(msg));
56 
57  flags = canMSG_STD;
58  dlc = 8;
59  }
60 
61  uint16_t GetSlot(int s) const
62  {
63  return (uint16_t) ((msg[s*2] << 8) | (msg[s*2+1]));
64  }
65 
66  void PutSlot(const int slot, const uint16_t val)
67  {
68  msg[slot*2] = (val >> 8) & 0xFF;
69  msg[slot*2+1] = val & 0xFF;
70  }
71 
72  void PutByte(const int byte, const uint16_t val)
73  {
74  msg[byte] = val & 0xFF;
75  }
76 
77  char* toString()
78  {
79  static char buf[256];
80  sprintf(buf, "id:%04lX %02X %02X %02X %02X %02X %02X %02X %02X",
81  id, msg[0], msg[1], msg[2], msg[3], msg[4], msg[5],
82  msg[6], msg[7]);
83 
84  return buf;
85  }
86 };
87 
88 #define DUALCAN_NR_CHANNELS 2
89 
90 /* this class encapsulates the low level CAN stuff.... so it deals
91  with reading and writing packets on the dual CAN channels.
92  We make the assumption that we only have to read off of one
93  channel though (looking at rmi_demo, it appears that this is
94  OK.)
95  A higher level entity will make sense of the packets, and call
96  the read/write methods with the required timing.
97 
98  It wouldn't take much to make this an abstract base class so that
99  the SegwayIO can use it, and then have different CAN hardwares
100  implement the virtual methods. So then we can just drop in
101  a new CAN hardware driver class and everything would still work.
102  Would also be able to split up the files, so we could keep
103  canio.[cc,h] in player, and the CAN hardware specific files
104  can be local.
105  */
106 
108 {
109 public:
110  DualCANIO() {}
111  virtual int Init(long channel_freq) = 0;
112  virtual int ReadPacket(CanPacket *pkt, int channel) = 0;
113  virtual int WritePacket(CanPacket &pkt) = 0;
114  virtual int Shutdown() = 0;
115 };
116 
117 #endif
#define PLAYER_WARN1(msg, a)
Definition: error.h:90
static bool MatchMessage(player_msghdr_t *hdr, int type, int subtype, player_devaddr_t addr)
Helper for message processing.
Definition: message.h:159
double ReadFloat(int section, const char *name, double value)
Read a floating point (double) value.
Definition: canio.h:46
int AddInterface(player_devaddr_t addr)
Add an interface.
Generic message header.
Definition: player.h:162
virtual int MainSetup(void)
Sets up the resources needed by the driver thread.
Definition: driver.h:658
virtual void MainQuit(void)
Cleanup method for driver thread (called when main exits)
Definition: driver.h:664
Encapsulates a device (i.e., a driver bound to an interface)
Definition: device.h:75
const char * ReadString(int section, const char *name, const char *value)
Read a string value.
virtual void Main(void)=0
Main method for driver thread.
int ReadInt(int section, const char *name, int value)
Read an integer value.
#define PLAYER_MSGTYPE_DATA
A data message.
Definition: player.h:95
#define PLAYER_ERROR2(msg, a, b)
Definition: error.h:83
#define PLAYER_MSGTYPE_RESP_ACK
A positive response message.
Definition: player.h:112
virtual int ProcessMessage(QueuePointer &resp_queue, player_msghdr *hdr, void *data)
Message handler.
static bool MatchDeviceAddress(player_devaddr_t addr1, player_devaddr_t addr2)
Compare two addresses.
Definition: device.h:201
#define PLAYER_MSGTYPE_REQ
A request message.
Definition: player.h:106
#define PLAYER_MSGTYPE_RESP_NACK
A negative response message.
Definition: player.h:125
int ReadDeviceAddr(player_devaddr_t *addr, int section, const char *name, int code, int index, const char *key)
Read a device id.
int GetTupleCount(int section, const char *name)
Get the number of values in a tuple.
Class for loading configuration file information.
Definition: configfile.h:197
int ReadTupleInt(int section, const char *name, int index, int value)
Read an integer from a tuple field.
A device address.
Definition: player.h:146
An autopointer for the message queue.
Definition: message.h:74
Definition: v4lcapture.h:100
Definition: v4lframe.h:64
void SetError(int code)
Set/reset error code.
Definition: driver.h:145
#define PLAYER_ERROR1(msg, a)
Definition: error.h:82
#define PLAYER_ERROR(msg)
Definition: error.h:81
Base class for drivers which oeprate with a thread.
Definition: driver.h:553
double timestamp
Time associated with message contents (seconds since epoch)
Definition: player.h:170
#define PLAYER_WARN(msg)
Warning message macros.
Definition: error.h:89
Definition: canio.h:108
Base class for all drivers.
Definition: driver.h:109
#define PLAYER_MSGQUEUE_DEFAULT_MAXLEN
Default maximum length for a message queue.
Definition: player.h:76
T max(T a, T b)
Return the maximum of a, b.
Definition: utility.h:104