raw_builder.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "packet/raw_builder.h"
  17. #include <algorithm>
  18. #include "os/log.h"
  19. using std::vector;
  20. using bluetooth::common::Address;
  21. namespace bluetooth {
  22. namespace packet {
  23. RawBuilder::RawBuilder(size_t max_bytes) : max_bytes_(max_bytes) {}
  24. bool RawBuilder::AddOctets(size_t octets, const vector<uint8_t>& bytes) {
  25. if (payload_.size() + octets > max_bytes_) return false;
  26. if (octets != bytes.size()) return false;
  27. payload_.insert(payload_.end(), bytes.begin(), bytes.end());
  28. return true;
  29. }
  30. bool RawBuilder::AddOctets(const vector<uint8_t>& bytes) {
  31. return AddOctets(bytes.size(), bytes);
  32. }
  33. bool RawBuilder::AddOctets(size_t octets, uint64_t value) {
  34. vector<uint8_t> val_vector;
  35. uint64_t v = value;
  36. if (octets > sizeof(uint64_t)) return false;
  37. for (size_t i = 0; i < octets; i++) {
  38. val_vector.push_back(v & 0xff);
  39. v = v >> 8;
  40. }
  41. if (v != 0) return false;
  42. return AddOctets(octets, val_vector);
  43. }
  44. bool RawBuilder::AddAddress(const Address& address) {
  45. if (payload_.size() + Address::kLength > max_bytes_) return false;
  46. for (size_t i = 0; i < Address::kLength; i++) {
  47. payload_.push_back(address.address[i]);
  48. }
  49. return true;
  50. }
  51. bool RawBuilder::AddOctets1(uint8_t value) {
  52. return AddOctets(1, value);
  53. }
  54. bool RawBuilder::AddOctets2(uint16_t value) {
  55. return AddOctets(2, value);
  56. }
  57. bool RawBuilder::AddOctets3(uint32_t value) {
  58. return AddOctets(3, value);
  59. }
  60. bool RawBuilder::AddOctets4(uint32_t value) {
  61. return AddOctets(4, value);
  62. }
  63. bool RawBuilder::AddOctets6(uint64_t value) {
  64. return AddOctets(6, value);
  65. }
  66. bool RawBuilder::AddOctets8(uint64_t value) {
  67. return AddOctets(8, value);
  68. }
  69. bool RawBuilder::CanAddOctets(size_t num_bytes) const {
  70. return payload_.size() + num_bytes <= max_bytes_;
  71. }
  72. void RawBuilder::Serialize(BitInserter& it) const {
  73. for (const auto& val : payload_) {
  74. insert(val, it);
  75. }
  76. }
  77. size_t RawBuilder::size() const {
  78. return payload_.size();
  79. }
  80. } // namespace packet
  81. } // namespace bluetooth