Updated comments and fixed tabbing
[profile/ivi/automotive-message-broker.git] / plugins / common / cansocketadapter.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 "cansocketadapter.h"
20 #include "cansocket.h"
21 #include "canobserver.h"
22 #include "cansocketreader.h"
23 #include "logger.h"
24 #include "cansocketbcm.h"
25 #include "cansocketraw.h"
26
27 // TODO: handle socket errors
28
29 CANSocketAdapter::CANSocketAdapter(CANObserver& observer) :
30     CANAdapter(observer),
31     mSocket(NULL),
32     mReader(NULL)
33 {
34     LOG_TRACE("");
35 }
36
37 CANSocketAdapter::~CANSocketAdapter()
38 {
39     LOG_TRACE("");
40
41     stop();
42 }
43
44 bool CANSocketAdapter::start(const char* ifName)
45 {
46     LOG_TRACE("");
47
48     if(!mSocket || !mReader) {
49         init();
50     }
51
52     if(mSocket && mReader && mSocket->start(ifName) && mReader->start()) {
53                 return true;
54         }
55
56         stop();
57     return false;
58 }
59
60 void CANSocketAdapter::stop()
61 {
62     LOG_TRACE("");
63
64     if(mReader) {
65         mReader->stop();
66         delete mReader;
67         mReader = 0;
68     }
69     if(mSocket) {
70         mSocket->stop();
71         delete mSocket;
72         mSocket = 0;
73     }
74 }
75
76 bool CANSocketAdapter::sendFrame(const can_frame& frame)
77 {
78     LOG_TRACE("");
79
80     if(mSocket) {
81                 CANFrameInfo message(frame);
82                 return mSocket->write(message);
83     }
84     return false;
85 }
86
87 void CANSocketAdapter::init()
88 {
89     if(!mSocket)
90         mSocket = new CANSocketBCM();
91     if(!mReader)
92         mReader = new CANSocketReader(mObserver, *mSocket);
93 }
94
95 bool CANSocketAdapter::registerCyclicMessageForReceive(int canId, double minCycleTime, double maxCycleTime)
96 {
97     if(mSocket) {
98                 return mSocket->registerCyclicMessageForReceive(canId, minCycleTime, maxCycleTime);
99     }
100     return false;
101 }
102
103 bool CANSocketAdapter::unregisterMessageForReceive(int canId)
104 {
105     if(mSocket) {
106         return mSocket->unregisterMessageForReceive(canId);
107     }
108     return false;
109 }