[libamb] - added value quality, removed deprecated GetFoo call, made updateFrequency...
[profile/ivi/automotive-message-broker.git] / plugins / common / cansocketraw.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #ifndef CANSOCKETRAW_H
20 #define CANSOCKETRAW_H
21
22 /**
23  *  \addtogroup libcanbus
24  *  @{
25  */
26
27 #include <net/if.h>
28 #include <sys/poll.h>
29 #include <string>
30 #include <linux/can/raw.h>
31
32 #include "cansocket.h"
33
34 /**
35 * \brief CAN Socket wrapper.
36 * @class CANSocket
37 */
38 class CANSocketRaw : public CANSocket
39 {
40 public:
41     CANSocketRaw();
42     virtual ~CANSocketRaw(){} /*LCOV_EXCL_LINE*/
43
44     /**
45     * Opens and initializes CAN socket
46     * @fn start
47     * @param ifName Name of the CAN bus network interface.
48     * @return True if no error occurs.
49     */
50     virtual bool start(const char* ifName);
51     /**
52     * Closes socket
53     * @fn stop
54     */
55     virtual void stop();
56     /**
57     * Writes CAN frame using the socket
58     * @fn write
59     * @param message CAN frame with additional information
60     * @return True if no error occurs.
61     */
62     virtual bool write(const struct CANFrameInfo &message);
63     /**
64     * Try to read CAN frame
65     * @fn read
66     * @param message Buffer for CAN frame with additional information
67     * @param timeout Timeout for reading in [ms].
68     * @return Reading operation status code.
69     */
70     virtual CANSocket::CANSocketReadSuccess read(struct CANFrameInfo& message, unsigned int timeout = 1000);
71
72 private:
73     /**
74     * @internal
75     */
76     bool createSocket();
77     bool enableCANErrors(can_err_mask_t errorMask);
78     bool enableTimestamps();
79     bool locateInterfaceIndex(struct ifreq& ifr);
80     bool bindSocket(struct sockaddr_can &addr);
81     bool closeSocket();
82     int waitData(unsigned int timeout);
83     bool writeFrame(const struct can_frame &frame);
84     ssize_t readFrame(can_frame& frame, double &timestamp);
85
86 private:
87     /**
88     * Socket file descriptor.
89     * @property mSocket
90     * @private
91     */
92     int mSocket;
93     /**
94     * Data structure describing a polling request.
95     * @property mPoll
96     * @private
97     */
98     struct pollfd mPoll;
99 };
100
101 #endif // CANSOCKETRAW_H
102
103 /** @} */