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