1 /*
2  * Copyright (c) 2021 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 IADAPTER_H
17 #define IADAPTER_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <string>
23 #include "communicator_type_define.h"
24 #include "iprocess_communicator.h"
25 
26 namespace DistributedDB {
27 // SendableCallback only notify when status changed from unsendable to sendable
28 using BytesReceiveCallback = std::function<void(const std::string &srcTarget, const uint8_t *bytes, uint32_t length,
29     const std::string &userId)>;
30 using TargetChangeCallback = std::function<void(const std::string &target, bool isConnect)>;
31 using SendableCallback = std::function<void(const std::string &target, int softBusErrCode)>;
32 
33 class IAdapter {
34 public:
35     // Register all callback before call StartAdapter.
36     // Return 0 as success. Return negative as error
37     // The StartAdapter should only be called by its user not owner
38     virtual int StartAdapter() = 0;
39 
40     // The StopAdapter may be called by its user in precondition of StartAdapter success
41     // The StopAdapter should only be called by its user not owner
42     virtual void StopAdapter() = 0;
43 
44     // Should returns the multiples of 8
45     virtual uint32_t GetMtuSize() = 0;
46     virtual uint32_t GetMtuSize(const std::string &target) = 0;
47 
48     // Should returns timeout in range [5s, 60s]
49     virtual uint32_t GetTimeout() = 0;
50     virtual uint32_t GetTimeout(const std::string &target) = 0;
51 
52     // Get local target name for identify self
53     virtual int GetLocalIdentity(std::string &outTarget) = 0;
54 
55     // Not assume bytes to be heap memory. Not assume SendBytes to be not blocking
56     // Return 0 as success. Return negative as error
57     virtual int SendBytes(const std::string &dstTarget, const uint8_t *bytes, uint32_t length,
58         uint32_t totalLength) = 0;
59 
60     // Pass nullptr as inHandle to do unReg if need (inDecRef also nullptr)
61     // Return 0 as success. Return negative as error
62     virtual int RegBytesReceiveCallback(const BytesReceiveCallback &onReceive, const Finalizer &inOper) = 0;
63 
64     // Pass nullptr as inHandle to do unReg if need (inDecRef also nullptr)
65     // Return 0 as success. Return negative as error
66     virtual int RegTargetChangeCallback(const TargetChangeCallback &onChange, const Finalizer &inOper) = 0;
67 
68     // Pass nullptr as inHandle to do unReg if need (inDecRef also nullptr)
69     // Return 0 as success. Return negative as error
70     virtual int RegSendableCallback(const SendableCallback &onSendable, const Finalizer &inOper) = 0;
71 
72     virtual bool IsDeviceOnline(const std::string &device) = 0;
73 
74     virtual std::shared_ptr<ExtendHeaderHandle> GetExtendHeaderHandle(const ExtendInfo &paramInfo) = 0;
75 
~IAdapter()76     virtual ~IAdapter() {};
77 };
78 } // namespace DistributedDB
79 
80 #endif // IADAPTER_H