1bae4d13cSopenharmony_ci/*
2bae4d13cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3bae4d13cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bae4d13cSopenharmony_ci * you may not use this file except in compliance with the License.
5bae4d13cSopenharmony_ci * You may obtain a copy of the License at
6bae4d13cSopenharmony_ci *
7bae4d13cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bae4d13cSopenharmony_ci *
9bae4d13cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bae4d13cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bae4d13cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bae4d13cSopenharmony_ci * See the License for the specific language governing permissions and
13bae4d13cSopenharmony_ci * limitations under the License.
14bae4d13cSopenharmony_ci */
15bae4d13cSopenharmony_ci
16bae4d13cSopenharmony_ci#include "net_packet.h"
17bae4d13cSopenharmony_ci
18bae4d13cSopenharmony_ci#include "sensor_errors.h"
19bae4d13cSopenharmony_ci
20bae4d13cSopenharmony_cinamespace OHOS {
21bae4d13cSopenharmony_cinamespace Sensors {
22bae4d13cSopenharmony_ciNetPacket::NetPacket(MessageId msgId) : msgId_(msgId)
23bae4d13cSopenharmony_ci{}
24bae4d13cSopenharmony_ci
25bae4d13cSopenharmony_ciNetPacket::NetPacket(const NetPacket &pkt) : NetPacket(pkt.GetMsgId())
26bae4d13cSopenharmony_ci{
27bae4d13cSopenharmony_ci    Clone(pkt);
28bae4d13cSopenharmony_ci}
29bae4d13cSopenharmony_ci
30bae4d13cSopenharmony_civoid NetPacket::MakeData(StreamBuffer &buf) const
31bae4d13cSopenharmony_ci{
32bae4d13cSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_RUST
33bae4d13cSopenharmony_ci    PACKHEAD head = {msgId_, StreamBufferGetWpos(streamBufferPtr_.get())};
34bae4d13cSopenharmony_ci    buf << head;
35bae4d13cSopenharmony_ci    if (StreamBufferGetWpos(streamBufferPtr_.get()) > 0) {
36bae4d13cSopenharmony_ci        if (!buf.Write(StreamBufferGetSzBuff(streamBufferPtr_.get()), StreamBufferGetWpos(streamBufferPtr_.get()))) {
37bae4d13cSopenharmony_ci            SEN_HILOGE("Write data to stream failed");
38bae4d13cSopenharmony_ci            return;
39bae4d13cSopenharmony_ci        }
40bae4d13cSopenharmony_ci    }
41bae4d13cSopenharmony_ci#else
42bae4d13cSopenharmony_ci    PACKHEAD head = {msgId_, wPos_};
43bae4d13cSopenharmony_ci    buf << head;
44bae4d13cSopenharmony_ci    if (wPos_ > 0) {
45bae4d13cSopenharmony_ci        if (!buf.Write(&szBuff_[0], wPos_)) {
46bae4d13cSopenharmony_ci            SEN_HILOGE("Write data to stream failed");
47bae4d13cSopenharmony_ci            return;
48bae4d13cSopenharmony_ci        }
49bae4d13cSopenharmony_ci    }
50bae4d13cSopenharmony_ci#endif // OHOS_BUILD_ENABLE_RUST
51bae4d13cSopenharmony_ci}
52bae4d13cSopenharmony_ci
53bae4d13cSopenharmony_cisize_t NetPacket::GetSize() const
54bae4d13cSopenharmony_ci{
55bae4d13cSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_RUST
56bae4d13cSopenharmony_ci    return StreamBufferSize(streamBufferPtr_.get());
57bae4d13cSopenharmony_ci#else
58bae4d13cSopenharmony_ci    return Size();
59bae4d13cSopenharmony_ci#endif // OHOS_BUILD_ENABLE_RUST
60bae4d13cSopenharmony_ci}
61bae4d13cSopenharmony_ci
62bae4d13cSopenharmony_cisize_t NetPacket::GetPacketLength() const
63bae4d13cSopenharmony_ci{
64bae4d13cSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_RUST
65bae4d13cSopenharmony_ci    return (static_cast<int32_t>(sizeof(PackHead)) + StreamBufferGetWpos(streamBufferPtr_.get()));
66bae4d13cSopenharmony_ci#else
67bae4d13cSopenharmony_ci    return sizeof(PackHead) + wPos_;
68bae4d13cSopenharmony_ci#endif // OHOS_BUILD_ENABLE_RUST
69bae4d13cSopenharmony_ci}
70bae4d13cSopenharmony_ci
71bae4d13cSopenharmony_ciconst char *NetPacket::GetData() const
72bae4d13cSopenharmony_ci{
73bae4d13cSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_RUST
74bae4d13cSopenharmony_ci    return StreamBufferData(streamBufferPtr_.get());
75bae4d13cSopenharmony_ci#else
76bae4d13cSopenharmony_ci    return Data();
77bae4d13cSopenharmony_ci#endif // OHOS_BUILD_ENABLE_RUST
78bae4d13cSopenharmony_ci}
79bae4d13cSopenharmony_ci
80bae4d13cSopenharmony_ciMessageId NetPacket::GetMsgId() const
81bae4d13cSopenharmony_ci{
82bae4d13cSopenharmony_ci    return msgId_;
83bae4d13cSopenharmony_ci}
84bae4d13cSopenharmony_ci} // namespace Sensors
85bae4d13cSopenharmony_ci} // namespace OHOS
86