Menu

PeakDriverLinuxTrait.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2008-2010 Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de>
00004  * All rights reserved.
00005  *
00006  *    Redistribution and use in source and binary forms, with or without
00007  *    modification, are permitted provided that the following conditions
00008  *    are met:
00009  *
00010  *    * Redistributions of source code must retain the above copyright
00011  *      notice, this list of conditions and the following disclaimer.
00012  *
00013  *    * Redistributions in binary form must reproduce the above copyright
00014  *      notice, this list of conditions and the following disclaimer in
00015  *      the documentation and/or other materials provided with the
00016  *      distribution.
00017  *
00018  *    * Neither the name of the copyright holders nor the names of
00019  *      contributors may be used to endorse or promote products derived
00020  *      from this software without specific prior written permission.
00021  *
00022  *
00023  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00024  *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00025  *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00026  *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *
00036  * $Id$
00037  *
00038  ******************************************************************************/
00039 
00040 #ifndef __PeakDriverLinuxTrait_h__
00041 #define __PeakDriverLinuxTrait_h__
00042 
00043 #include "debug.h"
00044 
00045 #include "devices/nic/can/peak/PeakDriverBase.h"
00046 #include "util/CommandLineParameterGenerator.h"
00047 
00048 namespace device {
00049     namespace nic {
00050         namespace CAN {
00051 
00052             CLP2(PEAKOptions,
00053                  "Peak CAN Driver",
00054                  "pcan,p",
00055                  "The device that is used and the baudrate in the form of device:baudrate"
00056                  "Values:\n"
00057                  "  Device:   \tdevice that has to be used\n"
00058                  "  Baudrate: \tlegal values are\n"
00059                  "               0 =   1 MBit/s\n"
00060                  "               1 = 500 kBit/s\n"
00061                  "               2 = 250 kBit/s\n"
00062                  "               3 = 125 kBit/s\n"
00063                  "               4 = 100 kBit/s\n"
00064                  "               5 =  50 kBit/s\n"
00065                  "               6 =  20 kBit/s\n"
00066                  "               7 =  10 kBit/s\n"
00067                  "               8 =   5 kBit/s\n"
00068                  "(e.g. /dev/pcan32:2 (default))",
00069                  std::string, device, "/dev/pcan32",
00070                  int, baudrate, 2);
00071 
00075             class PeakDriverLinuxTrait : public PeakDriverBase {
00077                     HANDLE handle;
00078                 public:
00079 
00081                     ~PeakDriverLinuxTrait() {
00082                         CAN_Close(handle);
00083                     }
00084 
00086                     void init() {
00087                         CLP::config::PEAKOptions::Parameter param;
00088                         CLP::config::PEAKOptions::instance().getParameter(param);
00089                         if ( (param.baudrate < 0) || (param.baudrate > 8) ) {
00090                             ::logging::log::emit< ::logging::Error>()
00091                                 << "Error: parameter baudrate out of Range"
00092                                 << ::logging::log::endl;
00093                             exit(0);
00094                         }
00095 
00096                         errno = 0;
00097                         /* open CAN port */
00098                         // this needs to be variable to allow using the interface
00099                         //  on linux and windows in the same way
00100                         handle = LINUX_CAN_Open(param.device.c_str(), O_RDWR);
00101                         if (!handle) {
00102                             ::logging::log::emit< ::logging::Error>()
00103                                 << "can't open CAN device " << param.device.c_str()
00104                                 << ::logging::log::endl;
00105                             exit(errno);
00106                         }
00107 
00108                         errno = CAN_Init(handle, baudrates[param.baudrate], CAN_INIT_TYPE_EX);
00109                         if (errno) {
00110                             ::logging::log::emit< ::logging::Error>()
00111                                 << "CAN_Init() fails"
00112                                 << ::logging::log::endl;
00113                             exit(errno);
00114                         }
00115                     }
00116 
00118                     DWORD read_mob(MOB &mob) {
00119                         return CAN_Read(handle, &mob);
00120                     }
00121 
00123                     DWORD write_mob(MOB &mob) {
00124                         return CAN_Write(handle, &mob);
00125                     }
00126 
00128                     DWORD status() {
00129                         return CAN_Status(handle);
00130                     }
00131 
00132             };
00133 
00137             typedef PeakDriverLinuxTrait PeakDriverTrait;
00138 
00139         } /* namespace CAN */
00140     } /* namespace nic */
00141 } /* namespace device */
00142 
00143 #endif
00144