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 event_fd)24 LocalChannelHandle ChannelManager::CreateHandle(LocalHandle data_fd,
25                                                 LocalHandle event_fd) {
26   if (data_fd && event_fd) {
27     std::lock_guard<std::mutex> autolock(mutex_);
28     int32_t handle = data_fd.Get();
29     channels_.emplace(handle,
30                       ChannelData{std::move(data_fd), std::move(event_fd)});
31     return LocalChannelHandle(this, handle);
32   }
33   return LocalChannelHandle(nullptr, -1);
34 }
35 
GetChannelData(int32_t handle)36 ChannelManager::ChannelData* ChannelManager::GetChannelData(int32_t handle) {
37   std::lock_guard<std::mutex> autolock(mutex_);
38   auto channel = channels_.find(handle);
39   return channel != channels_.end() ? &channel->second : nullptr;
40 }
41 
42 }  // namespace uds
43 }  // namespace pdx
44 }  // namespace android
45