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