reverted varianttype
[profile/ivi/automotive-message-broker.git] / plugins / common / cansocket.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 CANSOCKET_H
20 #define CANSOCKET_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 /**
33 * \brief CAN Socket wrapper.
34 * @class CANSocket
35 */
36 class CANSocket
37 {
38 public:
39
40     /**
41     * CAN bus socket error.
42     * @enum CANSocketReadSuccess
43     * @public
44     */
45     enum CANSocketReadSuccess {
46         READING_FAILED = -1,
47         READING_TIMED_OUT,
48         READING_SUCCEEDED
49     };
50
51 public:
52     CANSocket();
53     virtual ~CANSocket();
54
55     /**
56     * Opens and initialize CAN socket
57     * @fn start
58     * @param ifName Name of the CAN bus network interface.
59     * @return True if no error occurs.
60     */
61     virtual bool start(const char* ifName);
62     /**
63     * Closes socket
64     * @fn stop
65     */
66     virtual void stop();
67     /**
68     * Writes CAN frame using the socket
69     * @fn write
70     * @param frame CAN frame buffer
71     * @param bytesWritten Number of written bytes.
72     * @return True if no error occurs.
73     */
74     virtual bool write(const struct can_frame &frame, int &bytesWritten);
75
76     /**
77     * Try to read CAN frame
78     * @fn read
79     * @param frame CAN frame buffer
80     * @param bytesRead Number of read bytes.
81     * @param timeout Timeout for reading.
82     * @return Reading operation status code.
83     */
84     virtual CANSocket::CANSocketReadSuccess read( struct can_frame& frame, int &bytesRead, unsigned int timeout = 1000);
85
86 private:
87     /**
88     * @internal
89     */
90     virtual bool createSocket();
91     virtual bool enableCANErrors(can_err_mask_t errorMask);
92     virtual bool locateInterfaceIndex(struct ifreq& ifr);
93     virtual bool bindSocket(struct sockaddr_can &addr);
94     virtual bool closeSocket();
95     virtual int waitData(unsigned int timeout);
96     virtual ssize_t writeFrame(const struct can_frame &frame);
97     virtual ssize_t readFrame(struct can_frame& frame);
98
99 private:
100     /**
101     * Socket file descriptor.
102     * @property mSocket
103     * @private
104     */
105     int mSocket;
106     /**
107     * Data structure describing a polling request.
108     * @property mPoll
109     * @private
110     */
111     struct pollfd mPoll;
112 };
113
114 #endif // CANSOCKET_H
115
116 /** @} */