Updated comments and fixed tabbing
[profile/ivi/automotive-message-broker.git] / plugins / common / canbusimpl.cpp
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 #include "canbusimpl.h"
22 #include "canobserver.h"
23 #include "canadapter.h"
24 #include "logger.h"
25
26 CANBusImpl::CANBusImpl(CANObserver& observer) :
27     mObserver(observer),
28     mAdapter(NULL)
29 {
30     LOG_TRACE("");
31 }
32
33 CANBusImpl::~CANBusImpl()
34 {
35     LOG_TRACE("");
36
37     stop();
38 }
39
40 bool CANBusImpl::start(const char* name)
41 {
42     LOG_TRACE("");
43
44     if(!mAdapter) {
45         init();
46     }
47     return mAdapter ? mAdapter->start(name) : false;
48 }
49
50 void CANBusImpl::stop()
51 {
52     LOG_TRACE("");
53
54     if(mAdapter) {
55         mAdapter->stop();
56         delete mAdapter;
57         mAdapter = 0;
58     }
59 }
60
61 bool CANBusImpl::sendStandardFrame(const can_frame& frame)
62 {
63     LOG_TRACE("");
64
65     if(mAdapter) {
66         struct can_frame frm(frame);
67         frm.can_id &= CAN_SFF_MASK;
68         return mAdapter->sendFrame(frm);
69     }
70     return false;
71 }
72
73 bool CANBusImpl::sendExtendedFrame(const can_frame& frame)
74 {
75     LOG_TRACE("");
76
77     if(mAdapter) {
78         struct can_frame frm(frame);
79         frm.can_id &= CAN_EFF_MASK;
80         frm.can_id |= CAN_EFF_FLAG;
81         return mAdapter->sendFrame(frm);
82     }
83     return false;
84 }
85
86 void CANBusImpl::init()
87 {
88     mAdapter = CANAdapter::createCANAdapter(mObserver);
89 }
90
91 bool CANBusImpl::registerCyclicMessageForReceive(int canId, double minCycleTime, double maxCycleTime)
92 {
93     if(mAdapter) {
94         return mAdapter->registerCyclicMessageForReceive(canId, minCycleTime, maxCycleTime);
95     }
96     return false;
97 }
98
99 bool CANBusImpl::unregisterMessageForReceive(int canId)
100 {
101     if(mAdapter) {
102      return mAdapter->unregisterMessageForReceive(canId);
103     }
104     return false;
105 }
106