• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 UDS_SESSION_H
17 #define UDS_SESSION_H
18 
19 #include <list>
20 
21 #include "net_packet.h"
22 
23 namespace OHOS {
24 namespace MMI {
25 class UDSSession;
26 using SessionPtr = std::shared_ptr<UDSSession>;
27 class UDSSession : public std::enable_shared_from_this<UDSSession> {
28 public:
29     UDSSession(const std::string &programName, const int32_t moduleType, const int32_t fd, const int32_t uid,
30                const int32_t pid);
31     DISALLOW_COPY_AND_MOVE(UDSSession);
32     virtual ~UDSSession() = default;
33 
34     bool SendMsg(const char *buf, size_t size);
35     bool SendMsg(NetPacket &pkt);
36     void Close();
37 
GetUid()38     int32_t GetUid() const
39     {
40         return uid_;
41     }
42 
GetPid()43     int32_t GetPid() const
44     {
45         return pid_;
46     }
47 
GetModuleType()48     int32_t GetModuleType() const
49     {
50         return moduleType_;
51     }
52 
GetSharedPtr()53     SessionPtr GetSharedPtr()
54     {
55         return shared_from_this();
56     }
57 
GetFd()58     int32_t GetFd() const
59     {
60         return fd_;
61     }
62 
GetDescript()63     const std::string& GetDescript() const
64     {
65         return descript_;
66     }
67 
GetProgramName()68     const std::string GetProgramName() const
69     {
70         return programName_;
71     }
72 
SetAnrStatus(int32_t type,bool status)73     void SetAnrStatus(int32_t type, bool status)
74     {
75         isAnrProcess_[type] = status;
76     }
77 
CheckAnrStatus(int32_t type)78     bool CheckAnrStatus(int32_t type)
79     {
80         return isAnrProcess_[type];
81     }
82 
SetTokenType(int32_t type)83     void SetTokenType(int32_t type)
84     {
85         tokenType_ = type;
86     }
87 
GetTokenType()88     int32_t GetTokenType() const
89     {
90         return tokenType_;
91     }
92 
IsSocketValid()93     bool IsSocketValid()
94     {
95         return !invalidSocket_;
96     }
97     void UpdateDescript();
98     void SaveANREvent(int32_t type, int32_t id, int64_t time, int32_t timerId);
99     std::vector<int32_t> GetTimerIds(int32_t type);
100     std::list<int32_t> DelEvents(int32_t type, int32_t id);
101     int64_t GetEarliestEventTime(int32_t type = 0) const;
102     bool IsEventQueueEmpty(int32_t type = 0);
103     void ReportSocketBufferFull();
104 
105 protected:
106     struct EventTime {
107         int32_t id { 0 };
108         int64_t eventTime { 0 };
109         int32_t timerId { -1 };
110     };
111     std::map<int32_t, std::vector<EventTime>> events_;
112     std::map<int32_t, bool> isAnrProcess_;
113     std::string descript_;
114     const std::string programName_;
115     const int32_t moduleType_ { -1 };
116     int32_t fd_ { -1 };
117     const int32_t uid_ { -1 };
118     const int32_t pid_ { -1 };
119     int32_t tokenType_ { TokenType::TOKEN_INVALID };
120     mutable bool invalidSocket_ { false };
121     int64_t lastReportTime_ = 0;
122     int32_t lastReportedPid_ = 0;
123 };
124 } // namespace MMI
125 } // namespace OHOS
126 #endif // UDS_SESSION_H