1 /*
2  * Copyright 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 #define LOG_TAG "BTAudioHalUtils"
18 
19 #include "utils.h"
20 
21 #include <android-base/logging.h>
22 #include <android-base/strings.h>
23 #include <log/log.h>
24 #include <stdlib.h>
25 #include <sstream>
26 #include <vector>
27 
28 namespace android {
29 namespace bluetooth {
30 namespace audio {
31 namespace utils {
32 
ParseAudioParams(const std::string & params)33 std::unordered_map<std::string, std::string> ParseAudioParams(
34     const std::string& params) {
35   std::vector<std::string> segments = android::base::Split(params, ";");
36   std::unordered_map<std::string, std::string> params_map;
37   for (const auto& segment : segments) {
38     if (segment.length() == 0) {
39       continue;
40     }
41     std::vector<std::string> kv = android::base::Split(segment, "=");
42     if (kv[0].empty()) {
43       LOG(WARNING) << __func__ << ": Invalid audio parameter " << segment;
44       continue;
45     }
46     params_map[kv[0]] = (kv.size() > 1 ? kv[1] : "");
47   }
48   return params_map;
49 }
50 
GetAudioParamString(std::unordered_map<std::string,std::string> & params_map)51 std::string GetAudioParamString(
52     std::unordered_map<std::string, std::string>& params_map) {
53   std::ostringstream sout;
54   for (const auto& ptr : params_map) {
55     sout << "key: '" << ptr.first << "' value: '" << ptr.second << "'\n";
56   }
57   return sout.str();
58 }
59 
60 }  // namespace utils
61 }  // namespace audio
62 }  // namespace bluetooth
63 }  // namespace android
64