1 /*
2  * Copyright 2018 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 <map>
20 
21 namespace bluetooth {
22 namespace avrcp {
23 
24 // A helper class to convert Media ID's (represented as strings) that are
25 // received from the AVRCP Media Interface layer into UID's to be used
26 // with connected devices.
27 class MediaIdMap {
28  public:
clear()29   void clear() {
30     media_id_to_uid_.clear();
31     uid_to_media_id_.clear();
32   }
33 
get_media_id(uint64_t uid)34   std::string get_media_id(uint64_t uid) {
35     const auto& uid_it = uid_to_media_id_.find(uid);
36     if (uid_it == uid_to_media_id_.end()) return "";
37     return uid_it->second;
38   }
39 
get_uid(std::string media_id)40   uint64_t get_uid(std::string media_id) {
41     const auto& media_id_it = media_id_to_uid_.find(media_id);
42     if (media_id_it == media_id_to_uid_.end()) return 0;
43     return media_id_it->second;
44   }
45 
insert(std::string media_id)46   uint64_t insert(std::string media_id) {
47     if (media_id_to_uid_.find(media_id) != media_id_to_uid_.end()) {
48       return media_id_to_uid_[media_id];
49     }
50 
51     uint64_t uid = media_id_to_uid_.size() + 1;
52     media_id_to_uid_.insert(std::pair<std::string, uint64_t>(media_id, uid));
53     uid_to_media_id_.insert(std::pair<uint64_t, std::string>(uid, media_id));
54     return uid;
55   }
56 
57  private:
58   std::map<std::string, uint64_t> media_id_to_uid_;
59   std::map<uint64_t, std::string> uid_to_media_id_;
60 };
61 
62 }  // namespace avrcp
63 }  // namespace bluetooth
64