Updated comments and fixed tabbing
[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
47     /**
48     * Opens and initialize CAN socket
49     * @fn start
50     * @param ifName Name of the CAN bus network interface.
51     * @return True if no error occurs.
52     */
53     virtual bool start(const char* ifName);
54     /**
55     * Closes socket
56     * @fn stop
57     */
58     virtual void stop();
59     /**
60     * Writes CAN frame using the socket
61     * @fn write
62     * @param message CAN frame with additional information
63     * @param bytesWritten Number of written bytes.
64     * @return True if no error occurs.
65     */
66     virtual bool write(const struct CANFrameInfo &message);
67     /**
68     * Try to read CAN frame
69     * @fn read
70     * @param message Buffer for CAN frame with additional information
71     * @param timeout Timeout for reading in [ms].
72     * @return Reading operation status code.
73     */
74     virtual CANSocket::CANSocketReadSuccess read(struct CANFrameInfo& message, unsigned int timeout = 1000);
75     /**
76      * Registers CAN ID of a cyclic message for receiving
77      * @fn registerCyclicMessageForReceive
78      * @param canId CAN ID of the message.
79      * @param minCycleTime Minimal interval between messages in seconds. Set to 0 if not used.
80      * @param maxCycleTime Maximum interval between messages for timeout detection in seconds. Set to 0 if no timeout detection is necessary.
81      * @return True if registration succeeds.
82      */
83     virtual bool registerCyclicMessageForReceive(int canId, double minCycleTime, double maxCycleTime);
84     /**
85      * Unregisters CAN ID for receiving
86      * @fn unregisterMessageForReceive
87      * @param canId CAN ID of the message.
88      * @return True if de-registration succeeds.
89      */
90     virtual bool unregisterMessageForReceive(int canId);
91
92 private:
93     /**
94     * @internal
95     */
96     bool createSocket();
97     bool locateInterfaceIndex(struct ifreq& ifr);
98     bool connectSocket(struct sockaddr_can& addr);
99     bool closeSocket();
100     int  waitData(unsigned int timeout);
101     bool writeFrameOneTime(const can_frame& frame);
102     CANSocket::CANSocketReadSuccess readMessage(CANFrameInfo& message);
103
104 private:
105     /**
106     * Socket file descriptor.
107     * @property mSocket
108     * @private
109     */
110     int mSocket;
111     /**
112     * Data structure describing a polling request.
113     * @property mPoll
114     * @private
115     */
116     struct pollfd mPoll;
117 };
118
119 #endif // CANSOCKETBCM_H
120
121 /** @} */