parcel_uuid.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // Copyright 2017, 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. #include "android/os/parcel_uuid.h"
  17. using android::OK;
  18. using android::Parcel;
  19. using android::status_t;
  20. using ::bluetooth::Uuid;
  21. namespace android {
  22. namespace os {
  23. namespace {
  24. static uint64_t uuid_lsb(const Uuid& uuid) {
  25. uint64_t lsb = 0;
  26. auto uu = uuid.To128BitBE();
  27. for (int i = 8; i <= 15; i++) {
  28. lsb <<= 8;
  29. lsb |= uu[i];
  30. }
  31. return lsb;
  32. }
  33. static uint64_t uuid_msb(const Uuid& uuid) {
  34. uint64_t msb = 0;
  35. auto uu = uuid.To128BitBE();
  36. for (int i = 0; i <= 7; i++) {
  37. msb <<= 8;
  38. msb |= uu[i];
  39. }
  40. return msb;
  41. }
  42. } // namespace
  43. status_t ParcelUuid::writeToParcel(Parcel* parcel) const {
  44. status_t status = parcel->writeInt64(uuid_msb(uuid));
  45. if (status != OK) return status;
  46. status = parcel->writeInt64(uuid_lsb(uuid));
  47. return status;
  48. }
  49. status_t ParcelUuid::readFromParcel(const Parcel* parcel) {
  50. int64_t uuid_msb, uuid_lsb;
  51. status_t status = parcel->readInt64(&uuid_msb);
  52. if (status != OK) return status;
  53. status = parcel->readInt64(&uuid_lsb);
  54. if (status != OK) return status;
  55. std::array<uint8_t, Uuid::kNumBytes128> uu;
  56. for (int i = 0; i < 8; i++) {
  57. uu[7 - i] = (uuid_msb >> (8 * i)) & 0xFF;
  58. uu[15 - i] = (uuid_lsb >> (8 * i)) & 0xFF;
  59. }
  60. uuid = Uuid::From128BitBE(uu);
  61. return OK;
  62. }
  63. } // namespace os
  64. } // namespace android