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 "iam_check.h"
31 #include "iam_common_defines.h"
32 #include "iam_logger.h"
33 #include "iam_ptr.h"
34 #include "relative_timer.h"
35 #include "remote_connect_listener.h"
36 #include "soft_bus_message.h"
37 
38 #define LOG_TAG "USER_AUTH_SA"
39 namespace OHOS {
40 namespace UserIam {
41 namespace UserAuth {
42 using time_point = std::chrono::steady_clock::time_point;
43 class BaseSocket : public std::enable_shared_from_this<BaseSocket> {
44 public:
45     BaseSocket(const int32_t socketId);
46     virtual ~BaseSocket();
47     int32_t GetSocketId();
48     virtual ResultCode SendMessage(const std::string &connectionName, const std::string &srcEndPoint,
49         const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes, MsgCallback &callback) = 0;
50     ResultCode SendRequest(const int32_t socketId, const std::string &connectionName,
51         const std::string &srcEndPoint, const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes,
52         MsgCallback &callback);
53     ResultCode SendResponse(const int32_t socketId, const std::string &connectionName,
54         const std::string &srcEndPoint, const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes,
55         uint32_t messageSeq);
56     std::shared_ptr<SoftBusMessage> ParseMessage(const std::string &networkId,
57         void *message, uint32_t messageLen);
58     ResultCode ProcDataReceive(const int32_t socketId, std::shared_ptr<SoftBusMessage> &softBusMessage);
59     std::string GetConnectionName(uint32_t messageSeq);
60     MsgCallback GetMsgCallback(uint32_t messageSeq);
61 
62     virtual void OnBind(int32_t socketId, PeerSocketInfo info) = 0;
63     virtual void OnShutdown(int32_t socketId, ShutdownReason reason) = 0;
64     virtual void OnBytes(int32_t socketId, const void *data, uint32_t dataLen) = 0;
65     virtual void OnQos(int32_t socketId, QoSEvent eventId, const QosTV *qos, uint32_t qosCount) = 0;
66 
67     virtual std::string GetConnectionName() = 0;
68     virtual std::string GetNetworkId() = 0;
69 
70     struct CallbackInfo {
71         std::string connectionName;
72         MsgCallback msgCallback;
73         uint32_t timerId;
74         time_point sendTime;
75     };
76 
77 private:
78     void InsertMsgCallback(uint32_t messageSeq, const std::string &connectionName,
79         MsgCallback &callback, uint32_t timerId);
80     void RemoveMsgCallback(uint32_t messageSeq);
81 
82     uint32_t GetReplyTimer(uint32_t messageSeq);
83     uint32_t StartReplyTimer(uint32_t messageSeq);
84     void StopReplyTimer(uint32_t messageSeq);
85     void ReplyTimerTimeOut(uint32_t messageSeq);
86     int32_t GetMessageSeq();
87     ResultCode SetDeviceNetworkId(const std::string networkId, std::shared_ptr<Attributes> &attributes);
88     void PrintTransferDuration(uint32_t messageSeq);
89     void ProcessMessage(std::shared_ptr<SoftBusMessage> softBusMessage, std::shared_ptr<Attributes> response);
90 
91     std::recursive_mutex callbackMutex_;
92     /* <messageSeq, CallbackInfo> */
93     std::map<uint32_t, CallbackInfo> callbackMap_;
94 
95     int32_t socketId_;
96 };
97 } // namespace UserAuth
98 } // namespace UserIam
99 } // namespace OHOS
100 #endif // IAM_SOFT_BUS_SOCKET_H