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#ifndef STREAM_SESSION_H
17bae4d13cSopenharmony_ci#define STREAM_SESSION_H
18bae4d13cSopenharmony_ci
19bae4d13cSopenharmony_ci#include <map>
20bae4d13cSopenharmony_ci
21bae4d13cSopenharmony_ci#include "accesstoken_kit.h"
22bae4d13cSopenharmony_ci#include "nocopyable.h"
23bae4d13cSopenharmony_ci
24bae4d13cSopenharmony_ci#include "net_packet.h"
25bae4d13cSopenharmony_ci#include "proto.h"
26bae4d13cSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_RUST
27bae4d13cSopenharmony_ci#include "rust_binding.h"
28bae4d13cSopenharmony_ci#endif // OHOS_BUILD_ENABLE_RUST
29bae4d13cSopenharmony_ci
30bae4d13cSopenharmony_ci
31bae4d13cSopenharmony_cinamespace OHOS {
32bae4d13cSopenharmony_cinamespace Sensors {
33bae4d13cSopenharmony_ciclass StreamSession;
34bae4d13cSopenharmony_ciusing SessionPtr = std::shared_ptr<StreamSession>;
35bae4d13cSopenharmony_ciusing namespace Security::AccessToken;
36bae4d13cSopenharmony_ciclass StreamSession : public std::enable_shared_from_this<StreamSession> {
37bae4d13cSopenharmony_cipublic:
38bae4d13cSopenharmony_ci    StreamSession(const std::string &programName, const int32_t fd, const int32_t uid, const int32_t pid);
39bae4d13cSopenharmony_ci    ~StreamSession() = default;
40bae4d13cSopenharmony_ci    bool SendMsg(const char *buf, size_t size) const;
41bae4d13cSopenharmony_ci    bool SendMsg(const NetPacket &pkt) const;
42bae4d13cSopenharmony_ci    void Close();
43bae4d13cSopenharmony_ci    int32_t GetUid() const;
44bae4d13cSopenharmony_ci    int32_t GetPid() const;
45bae4d13cSopenharmony_ci    SessionPtr GetSharedPtr();
46bae4d13cSopenharmony_ci    int32_t GetFd() const;
47bae4d13cSopenharmony_ci    const std::string &GetDescript() const;
48bae4d13cSopenharmony_ci    const std::string GetProgramName() const;
49bae4d13cSopenharmony_ci    void SetTokenType(int32_t type);
50bae4d13cSopenharmony_ci    int32_t GetTokenType() const;
51bae4d13cSopenharmony_ci    void UpdateDescript();
52bae4d13cSopenharmony_ci    DISALLOW_COPY_AND_MOVE(StreamSession);
53bae4d13cSopenharmony_ci
54bae4d13cSopenharmony_ciprotected:
55bae4d13cSopenharmony_ci    struct EventTime {
56bae4d13cSopenharmony_ci        int32_t id { 0 };
57bae4d13cSopenharmony_ci        int64_t eventTime { 0 };
58bae4d13cSopenharmony_ci        int32_t timerId { -1 };
59bae4d13cSopenharmony_ci    };
60bae4d13cSopenharmony_ci    std::map<int32_t, std::vector<EventTime>> events_;
61bae4d13cSopenharmony_ci    std::string descript_;
62bae4d13cSopenharmony_ci    const std::string programName_;
63bae4d13cSopenharmony_ci#ifdef OHOS_BUILD_ENABLE_RUST
64bae4d13cSopenharmony_ci    std::unique_ptr<RustStreamSession, void(*)(RustStreamSession*)> streamSessionPtr_ { StreamSessionCreate(),
65bae4d13cSopenharmony_ci        StreamSessionDelete };
66bae4d13cSopenharmony_ci#else
67bae4d13cSopenharmony_ci    int32_t fd_ { -1 };
68bae4d13cSopenharmony_ci    const int32_t uid_ { -1 };
69bae4d13cSopenharmony_ci    const int32_t pid_ { -1 };
70bae4d13cSopenharmony_ci    int32_t tokenType_ { ATokenTypeEnum::TOKEN_INVALID };
71bae4d13cSopenharmony_ci#endif // OHOS_BUILD_ENABLE_RUST
72bae4d13cSopenharmony_ci};
73bae4d13cSopenharmony_ci} // namespace Sensors
74bae4d13cSopenharmony_ci} // namespace OHOS
75bae4d13cSopenharmony_ci#endif // STREAM_SESSION_H