Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <android-base/macros.h> |
| 20 | #include <android-base/unique_fd.h> |
| 21 | #include <linux/can.h> |
| 22 | |
| 23 | #include <atomic> |
| 24 | #include <chrono> |
| 25 | #include <thread> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace automotive { |
| 30 | namespace can { |
| 31 | namespace V1_0 { |
| 32 | namespace implementation { |
| 33 | |
| 34 | /** |
| 35 | * Wrapper around SocketCAN socket. |
| 36 | */ |
| 37 | struct CanSocket { |
| 38 | using ReadCallback = std::function<void(const struct canfd_frame&, std::chrono::nanoseconds)>; |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame^] | 39 | using ErrorCallback = std::function<void(int errnoVal)>; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Open and bind SocketCAN socket. |
| 43 | * |
| 44 | * \param ifname SocketCAN network interface name (such as can0) |
| 45 | * \param rdcb Callback on received messages |
| 46 | * \param errcb Callback on socket failure |
| 47 | * \return Socket instance, or nullptr if it wasn't possible to open one |
| 48 | */ |
| 49 | static std::unique_ptr<CanSocket> open(const std::string& ifname, ReadCallback rdcb, |
| 50 | ErrorCallback errcb); |
| 51 | virtual ~CanSocket(); |
| 52 | |
| 53 | /** |
| 54 | * Send CAN frame. |
| 55 | * |
| 56 | * \param frame Frame to send |
| 57 | * \return true in case of success, false otherwise |
| 58 | */ |
| 59 | bool send(const struct canfd_frame& frame); |
| 60 | |
| 61 | private: |
| 62 | CanSocket(base::unique_fd socket, ReadCallback rdcb, ErrorCallback errcb); |
| 63 | void readerThread(); |
| 64 | |
| 65 | ReadCallback mReadCallback; |
| 66 | ErrorCallback mErrorCallback; |
| 67 | |
| 68 | const base::unique_fd mSocket; |
| 69 | std::thread mReaderThread; |
| 70 | std::atomic<bool> mStopReaderThread = false; |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame^] | 71 | std::atomic<bool> mReaderThreadFinished = false; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(CanSocket); |
| 74 | }; |
| 75 | |
| 76 | } // namespace implementation |
| 77 | } // namespace V1_0 |
| 78 | } // namespace can |
| 79 | } // namespace automotive |
| 80 | } // namespace hardware |
| 81 | } // namespace android |