• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "net_packet.h"
17 
18 namespace OHOS {
19 namespace Sensors {
NetPacket(MessageId msgId)20 NetPacket::NetPacket(MessageId msgId) : msgId_(msgId)
21 {}
22 
NetPacket(const NetPacket & pkt)23 NetPacket::NetPacket(const NetPacket &pkt) : NetPacket(pkt.GetMsgId())
24 {
25     Clone(pkt);
26 }
27 
MakeData(StreamBuffer & buf) const28 void NetPacket::MakeData(StreamBuffer &buf) const
29 {
30 #ifdef OHOS_BUILD_ENABLE_RUST
31     PACKHEAD head = {msgId_, StreamBufferGetWpos(streamBufferPtr_.get())};
32     buf << head;
33     if (StreamBufferGetWpos(streamBufferPtr_.get()) > 0) {
34         if (!buf.Write(StreamBufferGetSzBuff(streamBufferPtr_.get()), StreamBufferGetWpos(streamBufferPtr_.get()))) {
35             SEN_HILOGE("Write data to stream failed");
36             return;
37         }
38     }
39 #else
40     PACKHEAD head = {msgId_, wPos_};
41     buf << head;
42     if (wPos_ > 0) {
43         if (!buf.Write(&szBuff_[0], wPos_)) {
44             SEN_HILOGE("Write data to stream failed");
45             return;
46         }
47     }
48 #endif // OHOS_BUILD_ENABLE_RUST
49 }
50 
GetSize() const51 size_t NetPacket::GetSize() const
52 {
53 #ifdef OHOS_BUILD_ENABLE_RUST
54     return StreamBufferSize(streamBufferPtr_.get());
55 #else
56     return Size();
57 #endif // OHOS_BUILD_ENABLE_RUST
58 }
59 
GetPacketLength() const60 size_t NetPacket::GetPacketLength() const
61 {
62 #ifdef OHOS_BUILD_ENABLE_RUST
63     return (static_cast<int32_t>(sizeof(PackHead)) + StreamBufferGetWpos(streamBufferPtr_.get()));
64 #else
65     return sizeof(PackHead) + wPos_;
66 #endif // OHOS_BUILD_ENABLE_RUST
67 }
68 
GetData() const69 const char *NetPacket::GetData() const
70 {
71 #ifdef OHOS_BUILD_ENABLE_RUST
72     return StreamBufferData(streamBufferPtr_.get());
73 #else
74     return Data();
75 #endif // OHOS_BUILD_ENABLE_RUST
76 }
77 
GetMsgId() const78 MessageId NetPacket::GetMsgId() const
79 {
80     return msgId_;
81 }
82 } // namespace Sensors
83 } // namespace OHOS
84