1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
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  *     http://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 
16 #ifndef OBEX_PACKET_H
17 #define OBEX_PACKET_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include "base_def.h"
22 #include "packet.h"
23 
24 class ObexPacket {
25 public:
ObexPacket(Packet & obexPacket)26     explicit ObexPacket(Packet &obexPacket)
27     {
28         obexPacket_ = &obexPacket;
29     }
ObexPacket(uint16_t size)30     explicit ObexPacket(uint16_t size)
31     {
32         obexPacket_ = PacketMalloc(0, 0, size);
33     }
~ObexPacket()34     virtual ~ObexPacket()
35     {
36         if (obexPacket_ != nullptr) {
37             PacketFree(obexPacket_);
38         }
39     }
Write(uint8_t * buf,uint16_t bufLen)40     void Write(uint8_t *buf, uint16_t bufLen) const
41     {
42         PacketPayloadWrite(obexPacket_, buf, 0, bufLen);
43     }
44 
GetPacket()45     Packet &GetPacket()
46     {
47         return *obexPacket_;
48     }
GetBuffer()49     uint8_t *GetBuffer()
50     {
51         if (obexPacket_ == nullptr) {
52             return nullptr;
53         }
54         Buffer *tmpBuffer = PacketContinuousPayload(obexPacket_);
55         return (uint8_t *)BufferPtr(tmpBuffer);
56     }
GetSize()57     size_t GetSize()
58     {
59         if (obexPacket_ == nullptr) {
60             return 0;
61         }
62         return PacketSize(obexPacket_);
63     }
64 
65 private:
66     Packet *obexPacket_ = nullptr;
67     BT_DISALLOW_COPY_AND_ASSIGN(ObexPacket);
68 };
69 #endif  // OBEX_PACKET_H
70