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 struct EventTime { 38 int32_t id { 0 }; 39 int64_t eventTime { 0 }; 40 int32_t timerId { -1 }; 41 }; 42 GetUid()43 int32_t GetUid() const 44 { 45 return uid_; 46 } 47 GetPid()48 int32_t GetPid() const 49 { 50 return pid_; 51 } 52 GetModuleType()53 int32_t GetModuleType() const 54 { 55 return moduleType_; 56 } 57 GetSharedPtr()58 SessionPtr GetSharedPtr() 59 { 60 return shared_from_this(); 61 } 62 GetFd()63 int32_t GetFd() const 64 { 65 return fd_; 66 } 67 GetDescript()68 const std::string& GetDescript() const 69 { 70 return descript_; 71 } 72 GetProgramName()73 const std::string GetProgramName() const 74 { 75 return programName_; 76 } 77 SetAnrStatus(int32_t type,bool status)78 void SetAnrStatus(int32_t type, bool status) 79 { 80 isAnrProcess_[type] = status; 81 } 82 CheckAnrStatus(int32_t type)83 bool CheckAnrStatus(int32_t type) 84 { 85 return isAnrProcess_[type]; 86 } 87 SetTokenType(int32_t type)88 void SetTokenType(int32_t type) 89 { 90 tokenType_ = type; 91 } 92 GetTokenType()93 int32_t GetTokenType() const 94 { 95 return tokenType_; 96 } 97 IsSocketValid()98 bool IsSocketValid() 99 { 100 return !invalidSocket_; 101 } 102 void UpdateDescript(); 103 void SaveANREvent(int32_t type, int32_t id, int64_t time, int32_t timerId); 104 std::vector<int32_t> GetTimerIds(int32_t type); 105 std::list<int32_t> DelEvents(int32_t type, int32_t id); 106 int64_t GetEarliestEventTime(int32_t type = 0) const; 107 bool IsEventQueueEmpty(int32_t type = 0); 108 void ReportSocketBufferFull(); 109 std::vector<EventTime> GetEventsByType(int32_t type) const; 110 111 protected: 112 std::map<int32_t, std::vector<EventTime>> events_; 113 std::map<int32_t, bool> isAnrProcess_; 114 std::string descript_; 115 const std::string programName_; 116 const int32_t moduleType_ { -1 }; 117 int32_t fd_ { -1 }; 118 const int32_t uid_ { -1 }; 119 const int32_t pid_ { -1 }; 120 int32_t tokenType_ { TokenType::TOKEN_INVALID }; 121 mutable bool invalidSocket_ { false }; 122 int64_t lastReportTime_ = 0; 123 int32_t lastReportedPid_ = 0; 124 }; 125 } // namespace MMI 126 } // namespace OHOS 127 #endif // UDS_SESSION_H