usb_packet.h
00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  Copyright (C) 2009
00004  *     Eric Grele and Goutham Mallapragda.
00005  *
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 
00023 #ifndef _USB_PACKET_H_
00024 #define _USB_PACKET_H_
00025 
00026 #include "canio.h"
00027 #include <libplayercommon/playercommon.h>
00028 
00029 typedef struct usb_packet {
00030         union {
00031                 unsigned char pkt_data[18];
00032                 struct {
00033                         unsigned char usb_message_header;
00034                         unsigned char usb_command_identifier;
00035                         unsigned char command_type;
00036                         unsigned char unused0;
00037                         unsigned char unused1;
00038                         unsigned char unused2;
00039                         unsigned short can_message_header;
00040                         unsigned char unused3;
00041                         unsigned char can_message[8];
00042                         unsigned char usb_message_checksum;
00043                 };
00044         };
00045 } usb_packet_t;
00046 
00047 class USBpacket {
00048 
00049 private:
00050         unsigned short make_can_header( long id, unsigned int dlc, unsigned int flags );
00051         unsigned char compute_checksum();
00052 
00053 
00054 public:
00055 
00056         typedef enum { CANA_DEV, USB_CMD_RESET } CommandType;
00057 
00058         usb_packet_t pkt;
00059 
00060         USBpacket();
00061         USBpacket( const CanPacket &pkt );
00062         operator CanPacket ();
00063         bool check();
00064 
00065         void print();
00066 
00067 };
00068 
00069 
00070 class USBIO {
00071 private:
00072         int fd;
00073         bool synced;
00074 public:
00075         USBIO() { fd = -1; synced = false; }
00076         int Init(const char *dev);
00077         int ReadPacket(CanPacket *pkt);
00078         int SyncRead( USBpacket &p );
00079         int WritePacket(CanPacket &pkt);
00080         int Shutdown();
00081 };
00082 
00083 #endif

Last updated 25 May 2011 21:17:00