• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 IAM_SOFT_BUS_SOCKET_H
17 #define IAM_SOFT_BUS_SOCKET_H
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <map>
22 #include <mutex>
23 #include <set>
24 #include <string>
25 #include <vector>
26 #include "socket.h"
27 
28 #include "attributes.h"
29 #include "device_manager.h"
30 #include "hisysevent_adapter.h"
31 #include "iam_check.h"
32 #include "iam_common_defines.h"
33 #include "iam_logger.h"
34 #include "iam_ptr.h"
35 #include "relative_timer.h"
36 #include "remote_connect_listener.h"
37 #include "soft_bus_message.h"
38 
39 #define LOG_TAG "USER_AUTH_SA"
40 namespace OHOS {
41 namespace UserIam {
42 namespace UserAuth {
43 using time_point = std::chrono::steady_clock::time_point;
44 class BaseSocket : public std::enable_shared_from_this<BaseSocket> {
45 public:
46     struct ConnectionInfo {
47         int32_t socketId;
48         std::string connectionName;
49         std::string srcEndPoint;
50         std::string destEndPoint;
51         std::shared_ptr<Attributes> attributes;
52         MsgCallback callback;
53     };
54 
55     BaseSocket(const int32_t socketId);
56     virtual ~BaseSocket();
57     int32_t GetSocketId();
58     virtual ResultCode SendMessage(const std::string &connectionName, const std::string &srcEndPoint,
59         const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes, MsgCallback &callback) = 0;
60     ResultCode SendRequest(const ConnectionInfo &connectionInfo);
61     ResultCode SendResponse(const int32_t socketId, const std::string &connectionName,
62         const std::string &srcEndPoint, const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes,
63         uint32_t messageSeq);
64     std::shared_ptr<SoftBusMessage> ParseMessage(const std::string &networkId,
65         void *message, uint32_t messageLen);
66     ResultCode ProcDataReceive(const int32_t socketId, std::shared_ptr<SoftBusMessage> &softBusMessage);
67     std::string GetConnectionName(uint32_t messageSeq);
68     MsgCallback GetMsgCallback(uint32_t messageSeq);
69     RemoteConnectFaultTrace GetCurrTraceInfo();
70 
71     virtual void OnBind(int32_t socketId, PeerSocketInfo info) = 0;
72     virtual void OnShutdown(int32_t socketId, ShutdownReason reason) = 0;
73     virtual void OnBytes(int32_t socketId, const void *data, uint32_t dataLen) = 0;
74     virtual void OnQos(int32_t socketId, QoSEvent eventId, const QosTV *qos, uint32_t qosCount) = 0;
75 
76     virtual std::string GetConnectionName() = 0;
77     virtual std::string GetNetworkId() = 0;
78 
79     struct CallbackInfo {
80         std::string connectionName;
81         MsgCallback msgCallback;
82         uint32_t timerId;
83         time_point sendTime;
84     };
85 
86 private:
87     void InsertMsgCallback(uint32_t messageSeq, const std::string &connectionName,
88         const MsgCallback &callback, uint32_t timerId);
89     void RemoveMsgCallback(uint32_t messageSeq);
90 
91     uint32_t GetReplyTimer(uint32_t messageSeq);
92     uint32_t StartReplyTimer(uint32_t messageSeq);
93     void StopReplyTimer(uint32_t messageSeq);
94     void ReplyTimerTimeOut(uint32_t messageSeq);
95     int32_t GetMessageSeq();
96     ResultCode SetDeviceNetworkId(const std::string networkId, std::shared_ptr<Attributes> &attributes);
97     void PrintTransferDuration(uint32_t messageSeq);
98     void ProcessMessage(std::shared_ptr<SoftBusMessage> softBusMessage, std::shared_ptr<Attributes> response);
99     void RefreshTraceInfo(const std::string &connectionName, int32_t msgType, bool ack, uint32_t messageSeq);
100 
101     std::recursive_mutex callbackMutex_;
102     /* <messageSeq, CallbackInfo> */
103     std::map<uint32_t, CallbackInfo> callbackMap_;
104 
105     int32_t socketId_ = -1;
106     RemoteConnectFaultTrace currTraceInfo_ = {};
107 };
108 } // namespace UserAuth
109 } // namespace UserIam
110 } // namespace OHOS
111 #endif // IAM_SOFT_BUS_SOCKET_H