1 #include <uds/channel_manager.h>
2
3 #include <log/log.h>
4
5 namespace android {
6 namespace pdx {
7 namespace uds {
8
Get()9 ChannelManager& ChannelManager::Get() {
10 static ChannelManager instance;
11 return instance;
12 }
13
CloseHandle(int32_t handle)14 void ChannelManager::CloseHandle(int32_t handle) {
15 std::lock_guard<std::mutex> autolock(mutex_);
16 auto channel = channels_.find(handle);
17 if (channel == channels_.end()) {
18 ALOGE("Invalid channel handle: %d", handle);
19 } else {
20 channels_.erase(channel);
21 }
22 }
23
CreateHandle(LocalHandle data_fd,LocalHandle pollin_event_fd,LocalHandle pollhup_event_fd)24 LocalChannelHandle ChannelManager::CreateHandle(LocalHandle data_fd,
25 LocalHandle pollin_event_fd,
26 LocalHandle pollhup_event_fd) {
27 if (data_fd && pollin_event_fd && pollhup_event_fd) {
28 std::lock_guard<std::mutex> autolock(mutex_);
29 const int32_t handle = data_fd.Get();
30 channels_.emplace(
31 handle,
32 ChannelEventReceiver{std::move(data_fd), std::move(pollin_event_fd),
33 std::move(pollhup_event_fd)});
34 return LocalChannelHandle(this, handle);
35 } else {
36 ALOGE(
37 "ChannelManager::CreateHandle: Invalid arguments: data_fd=%d "
38 "pollin_event_fd=%d pollhup_event_fd=%d",
39 data_fd.Get(), pollin_event_fd.Get(), pollhup_event_fd.Get());
40 return LocalChannelHandle(nullptr, -1);
41 }
42 }
43
GetChannelData(int32_t handle)44 ChannelEventReceiver* ChannelManager::GetChannelData(int32_t handle) {
45 std::lock_guard<std::mutex> autolock(mutex_);
46 auto channel = channels_.find(handle);
47 return channel != channels_.end() ? &channel->second : nullptr;
48 }
49
50 } // namespace uds
51 } // namespace pdx
52 } // namespace android
53