• 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 #include "sensors_errors.h"
19 
20 namespace OHOS {
21 namespace Sensors {
NetPacket(MessageId msgId)22 NetPacket::NetPacket(MessageId msgId) : msgId_(msgId)
23 {}
24 
NetPacket(const NetPacket & pkt)25 NetPacket::NetPacket(const NetPacket &pkt) : NetPacket(pkt.GetMsgId())
26 {
27     Clone(pkt);
28 }
29 
MakeData(StreamBuffer & buf) const30 void NetPacket::MakeData(StreamBuffer &buf) const
31 {
32 #ifdef OHOS_BUILD_ENABLE_RUST
33     PACKHEAD head = {msgId_, StreamBufferGetWpos(streamBufferPtr_.get())};
34     buf << head;
35     if (StreamBufferGetWpos(streamBufferPtr_.get()) > 0) {
36         if (!buf.Write(StreamBufferGetSzBuff(streamBufferPtr_.get()), StreamBufferGetWpos(streamBufferPtr_.get()))) {
37             SEN_HILOGE("Write data to stream failed");
38             return;
39         }
40     }
41 #else
42     PACKHEAD head = {msgId_, wPos_};
43     buf << head;
44     if (wPos_ > 0) {
45         if (!buf.Write(&szBuff_[0], wPos_)) {
46             SEN_HILOGE("Write data to stream failed");
47             return;
48         }
49     }
50 #endif // OHOS_BUILD_ENABLE_RUST
51 }
52 
GetSize() const53 size_t NetPacket::GetSize() const
54 {
55 #ifdef OHOS_BUILD_ENABLE_RUST
56     return StreamBufferSize(streamBufferPtr_.get());
57 #else
58     return Size();
59 #endif // OHOS_BUILD_ENABLE_RUST
60 }
61 
GetPacketLength() const62 size_t NetPacket::GetPacketLength() const
63 {
64 #ifdef OHOS_BUILD_ENABLE_RUST
65     return (static_cast<int32_t>(sizeof(PackHead)) + StreamBufferGetWpos(streamBufferPtr_.get()));
66 #else
67     return sizeof(PackHead) + wPos_;
68 #endif // OHOS_BUILD_ENABLE_RUST
69 }
70 
GetData() const71 const char* NetPacket::GetData() const
72 {
73 #ifdef OHOS_BUILD_ENABLE_RUST
74     return StreamBufferData(streamBufferPtr_.get());
75 #else
76     return Data();
77 #endif // OHOS_BUILD_ENABLE_RUST
78 }
79 
GetMsgId() const80 MessageId NetPacket::GetMsgId() const
81 {
82     return msgId_;
83 }
84 } // namespace Sensors
85 } // namespace OHOS
86