1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_PACKET_H_
16 #define CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_PACKET_H_
17 
18 #include <limits>
19 
20 #include "platform/base/byte_array.h"
21 
22 namespace location {
23 namespace nearby {
24 namespace connections {
25 namespace mediums {
26 
27 // Represents the format of data sent over Ble sockets.
28 //
29 // [SERVICE_ID_HASH][DATA]
30 //
31 // See go/nearby-ble-design for more information.
32 class BlePacket {
33  public:
34   static const std::uint32_t kServiceIdHashLength = 3;
35 
36   BlePacket() = default;
37   BlePacket(const ByteArray& service_id_hash, const ByteArray& data);
38   explicit BlePacket(const ByteArray& ble_packet_byte);
39   BlePacket(const BlePacket&) = default;
40   BlePacket& operator=(const BlePacket&) = default;
41   BlePacket(BlePacket&&) = default;
42   BlePacket& operator=(BlePacket&&) = default;
43   ~BlePacket() = default;
44 
45   explicit operator ByteArray() const;
46 
IsValid()47   bool IsValid() const { return !service_id_hash_.Empty(); }
GetServiceIdHash()48   ByteArray GetServiceIdHash() const { return service_id_hash_; }
GetData()49   ByteArray GetData() const { return data_; }
50 
51  private:
52   static const std::uint32_t kMaxDataSize =
53       std::numeric_limits<int32_t>::max() - kServiceIdHashLength;
54 
55   ByteArray service_id_hash_;
56   ByteArray data_;
57 };
58 
59 }  // namespace mediums
60 }  // namespace connections
61 }  // namespace nearby
62 }  // namespace location
63 
64 #endif  // CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_PACKET_H_
65