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