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 OHOS_IPC_DBINDER_SOFTBUS_CLIENT_H
17 #define OHOS_IPC_DBINDER_SOFTBUS_CLIENT_H
18 
19 #include <mutex>
20 #include <string>
21 #include <atomic>
22 
23 #include "inner_socket.h"
24 #include "nocopyable.h"
25 #include "socket.h"
26 #include "softbus_bus_center.h"
27 
28 namespace OHOS {
29 enum {
30     SOFTBUS_CLIENT_SUCCESS = 0,
31     SOFTBUS_CLIENT_DLOPEN_FAILED,
32     SOFTBUS_CLIENT_DLSYM_FAILED,
33     SOFTBUS_CLIENT_INSTANCE_EXIT,
34     SOFTBUS_CLIENT_GET_DEVICE_INFO_FAILED,
35 };
36 
37 static constexpr int MAX_SEND_MESSAGE_LENGTH = 4 * 1024;
38 
39 class DBinderSoftbusClient {
40 public:
41     static DBinderSoftbusClient& GetInstance();
42     DBinderSoftbusClient();
43     ~DBinderSoftbusClient();
44 
45     int32_t DBinderGrantPermission(int32_t uid, int32_t pid, const std::string &socketName);
46     int32_t DBinderRemovePermission(const std::string &socketName);
47     int32_t GetLocalNodeDeviceId(const std::string &pkgName, std::string &devId);
48     int32_t Socket(SocketInfo info);
49     int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
50     int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener);
51     int32_t SendBytes(int32_t socket, const void *data, uint32_t len);
52     int32_t SendMessage(int32_t socket, const void *data, uint32_t len);
53     void Shutdown(int32_t socket);
54 
55 private:
56     DISALLOW_COPY_AND_MOVE(DBinderSoftbusClient);
57     bool OpenSoftbusClientSo();
58 
59     using DBinderGrantPermissionFunc = int32_t (*)(int32_t, int32_t, const char*);
60     using DBinderRemovePermissionFunc = int32_t (*)(const char*);
61     using GetLocalNodeDeviceInfoFunc = int32_t (*)(const char*, NodeBasicInfo*);
62     using SocketFunc = int32_t (*)(SocketInfo);
63     using ListenFunc = int32_t (*)(int32_t, const QosTV[], uint32_t, const ISocketListener*);
64     using BindFunc = int32_t (*)(int32_t, const QosTV[], uint32_t, const ISocketListener*);
65     using SendBytesFunc = int32_t (*)(int32_t, const void*, uint32_t);
66     using SendMessageFunc = int32_t (*)(int32_t, const void*, uint32_t);
67     using ShutdownFunc = void (*)(int32_t);
68 
69     DBinderGrantPermissionFunc grantPermissionFunc_ = nullptr;
70     DBinderRemovePermissionFunc removePermissionFunc_ = nullptr;
71     GetLocalNodeDeviceInfoFunc getLocalNodeDeviceInfoFunc_ = nullptr;
72     SocketFunc socketFunc_ = nullptr;
73     ListenFunc listenFunc_ = nullptr;
74     BindFunc bindFunc_ = nullptr;
75     SendBytesFunc sendBytesFunc_ = nullptr;
76     SendMessageFunc sendMessageFunc_ = nullptr;
77     ShutdownFunc shutdownFunc_ = nullptr;
78 
79     std::mutex loadSoMutex_;
80     std::atomic<bool> exitFlag_ = false;
81     bool isLoaded_ = false;
82     void *soHandle_ = nullptr;
83 };
84 } // namespace OHOS
85 #endif // OHOS_IPC_DBINDER_SOFTBUS_CLIENT_H
86