-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathdriver_handler.hpp
83 lines (73 loc) · 2.88 KB
/
driver_handler.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// driver_handler.hpp
//
// Copyright (c) 2019 2020 Andrea Bondavalli. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef _DRIVER_HANDLER_HPP_
#define _DRIVER_HANDLER_HPP_
#include <future>
#include "MT_ALSA_message_defs.h"
#include "config.hpp"
#include "error_code.hpp"
#include "log.hpp"
#include "netlink_client.hpp"
class DriverHandler {
public:
static constexpr size_t max_payload = MAX_PAYLOAD;
static constexpr off_t data_offset =
sizeof(struct MT_ALSA_msg) /*+ sizeof(int)*/;
static constexpr int reply_timeout_secs = 1; // 1sec in driver
static constexpr size_t buffer_size =
NLMSG_SPACE(max_payload) + sizeof(struct MT_ALSA_msg);
DriverHandler() = default;
DriverHandler(const DriverHandler&) = delete;
DriverHandler& operator=(const DriverHandler&) = delete;
virtual ~DriverHandler(){};
virtual bool init(const Config& config);
virtual bool terminate(const Config& config);
protected:
virtual void send_command(enum MT_ALSA_msg_id id,
size_t size = 0,
const uint8_t* data = nullptr);
virtual void on_command_done(enum MT_ALSA_msg_id id,
size_t size = 0,
const uint8_t* data = nullptr) = 0;
virtual void on_command_error(enum MT_ALSA_msg_id id,
std::error_code error) = 0;
virtual void on_event(enum MT_ALSA_msg_id id,
size_t& res_size,
uint8_t* res,
size_t req_size = 0,
const uint8_t* req = nullptr) = 0;
virtual void on_event_error(enum MT_ALSA_msg_id id,
std::error_code error) = 0;
private:
void send(enum MT_ALSA_msg_id id,
NetlinkClient& client,
uint8_t* buffer,
size_t data_size,
const uint8_t* data) const;
bool event_receiver();
std::future<bool> res_;
std::atomic_bool running_{false};
uint8_t command_buffer_[buffer_size];
uint8_t event_buffer_[buffer_size];
uint8_t response_buffer_[buffer_size];
NetlinkClient client_u2k_{"commands"}; /* u2k for commands */
NetlinkClient client_k2u_{"events"}; /* k2u for events */
std::mutex mutex_; /* one command at a time */
};
#endif