reverted varianttype
[profile/ivi/automotive-message-broker.git] / plugins / common / canbusimpl.cpp
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 #include "canbusimpl.h"
20 #include "canobserver.h"
21 #include "canadapter.h"
22 #include "logger.h"
23
24 //----------------------------------------------------------------------------
25 // CANBusImpl
26 //----------------------------------------------------------------------------
27
28 CANBus::Impl::Impl(CANObserver& observer) :
29     mObserver(observer),
30     mAdapter(NULL)
31 {
32     LOG_TRACE("");
33 }
34
35 CANBus::Impl::~Impl()
36 {
37     LOG_TRACE("");
38
39     stop();
40 }
41
42 bool CANBus::Impl::start(const char* name)
43 {
44     LOG_TRACE("");
45
46     if(!mAdapter) {
47         init();
48     }
49     return mAdapter ? mAdapter->start(name) : false;
50 }
51
52 void CANBus::Impl::stop()
53 {
54     LOG_TRACE("");
55
56     if(mAdapter) {
57         mAdapter->stop();
58         delete mAdapter;
59         mAdapter = 0;
60     }
61 }
62
63 bool CANBus::Impl::sendStandardFrame(const can_frame& frame)
64 {
65     LOG_TRACE("");
66
67     if(mAdapter) {
68         struct can_frame frm(frame);
69         frm.can_id &= CAN_SFF_MASK;
70         return mAdapter->sendFrame(frm);
71     }
72     return false;
73 }
74
75 bool CANBus::Impl::sendExtendedFrame(const can_frame& frame)
76 {
77     LOG_TRACE("");
78
79     if(mAdapter) {
80         struct can_frame frm(frame);
81         frm.can_id &= CAN_EFF_MASK;
82         frm.can_id |= CAN_EFF_FLAG;
83         return mAdapter->sendFrame(frm);
84     }
85     return false;
86 }
87
88 void CANBus::Impl::init()
89 {
90     mAdapter = CANAdapter::createCANAdapter(mObserver);
91 }
92
93 //----------------------------------------------------------------------------
94 // CANBus
95 //----------------------------------------------------------------------------
96
97 CANBus::CANBus(CANObserver& observer) :
98     d(new CANBus::Impl(observer))
99 {
100     LOG_TRACE("");
101 }
102
103 CANBus::~CANBus()
104 {
105     LOG_TRACE("");
106
107     if(d) {
108         delete d;
109         d = 0;
110     }
111 }
112
113 bool CANBus::start(const char* name)
114 {
115     LOG_TRACE("");
116
117     return d ? d->start(name) : false;
118 }
119
120 void CANBus::stop()
121 {
122     LOG_TRACE("");
123
124     if(d)
125         d->stop();
126 }
127
128 bool CANBus::sendStandardFrame(const can_frame& frame)
129 {
130     LOG_TRACE("");
131
132     return d ? d->sendStandardFrame(frame) : false;
133 }
134
135 bool CANBus::sendExtendedFrame(const can_frame& frame)
136 {
137     LOG_TRACE("");
138
139     return d ? d->sendExtendedFrame(frame) : false;
140 }