utils.cc 1.8 KB

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