reverted varianttype
[profile/ivi/automotive-message-broker.git] / plugins / common / cansocketadapter.cpp
1 /*****************************************************************
2  * INTEL CONFIDENTIAL
3  * Copyright 2011 - 2013 Intel Corporation All Rights Reserved.
4  * 
5  * The source code contained or described herein and all documents related to the
6  * source code("Material") are owned by Intel Corporation or its suppliers or
7  * licensors.Title to the Material remains with Intel Corporation or its
8  * suppliers and licensors.The Material may contain trade secrets and proprietary
9  * and confidential information of Intel Corporation and its suppliers and
10  * licensors, and is protected by worldwide copyright and trade secret laws and
11  * treaty provisions.No part of the Material may be used, copied, reproduced,
12  * modified, published, uploaded, posted, transmitted, distributed, or disclosed
13  * in any way without Intels prior express written permission.
14  * No license under any patent, copyright, trade secret or other intellectual
15  * property right is granted to or conferred upon you by disclosure or delivery
16  * of the Materials, either expressly, by implication, inducement, estoppel or
17  * otherwise.Any license under such intellectual property rights must be
18  * express and approved by Intel in writing. 
19  * 
20  * Unless otherwise agreed by Intel in writing, you may not remove or alter this
21  * notice or any other notice embedded in Materials by Intel or Intels suppliers
22  * or licensors in any way.
23  *****************************************************************/
24
25 #include "cansocketadapter.h"
26 #include "cansocket.h"
27 #include "canobserver.h"
28 #include "cansocketreader.h"
29 #include "logger.h"
30
31 // TODO: handle socket errors
32
33 CANSocketAdapter::CANSocketAdapter(CANObserver& observer) :
34     CANAdapter(observer),
35     mSocket(NULL),
36     mReader(NULL)
37 {
38     LOG_TRACE("");
39 }
40
41 CANSocketAdapter::~CANSocketAdapter()
42 {
43     LOG_TRACE("");
44
45     stop();
46 }
47
48 bool CANSocketAdapter::start(const char* ifName)
49 {
50     LOG_TRACE("");
51
52     if(!mSocket || !mReader) {
53         init();
54     }
55
56     if(mSocket && mReader && mSocket->start(ifName) && mReader->start()) {
57                 return true;
58         }
59
60         stop();
61     return false;
62 }
63
64 void CANSocketAdapter::stop()
65 {
66     LOG_TRACE("");
67
68     if(mReader) {
69         mReader->stop();
70         delete mReader;
71         mReader = 0;
72     }
73     if(mSocket) {
74         mSocket->stop();
75         delete mSocket;
76         mSocket = 0;
77     }
78 }
79
80 bool CANSocketAdapter::sendFrame(const can_frame& frame)
81 {
82     LOG_TRACE("");
83
84     if(mSocket) {
85                 int bytesWritten(0);
86                 return mSocket->write(frame, bytesWritten);
87     }
88     return false;
89 }
90
91 void CANSocketAdapter::init()
92 {
93     if(!mSocket)
94         mSocket = new CANSocket();
95     if(!mReader)
96         mReader = new CANSocketReader(mObserver, *mSocket);
97 }
98