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 OHOS_IPC_DBINDER_SESSION_OBJECT_H
17 #define OHOS_IPC_DBINDER_SESSION_OBJECT_H
18 
19 #include <string>
20 #include <mutex>
21 #include "nocopyable.h"
22 #include "buffer_object.h"
23 #include "databus_socket_listener.h"
24 #include "ipc_object_proxy.h"
25 
26 namespace OHOS {
27 constexpr int DEVICEID_LENGTH = 64;
28 constexpr int NOT_SUPPORT_TOKENID_SERVICENAME_LENGTH = 200;
29 constexpr int SUPPORT_TOKENID_SERVICENAME_LENGTH = 64;
30 constexpr int RESERVED_FROM_SERVICENAME_LENGTH = 125;
31 
32 /* struct FlatDBinderSession is for flat DatabusSessionObject to transfer to another device */
33 struct FlatDBinderSession {
34     uint64_t stubIndex;
35     uint16_t deviceIdLength;
36     uint16_t serviceNameLength;
37     char deviceId[DEVICEID_LENGTH + 1];
38     char serviceName[SUPPORT_TOKENID_SERVICENAME_LENGTH + 1];
39     uint16_t version; // for alignment
40     uint32_t magic;
41     uint32_t tokenId;
42     char reserved[RESERVED_FROM_SERVICENAME_LENGTH];
43     // if not support tokenid, this is end of serviceName, which is '\0', this position equal to
44     // NOT_SUPPORT_TOKENID_SERVICENAME_LENGTH = 200 + 1
45     char canNotUse;
46 };
47 
48 class DBinderSessionObject {
49 public:
50     static uint32_t GetFlatSessionLen();
51     explicit DBinderSessionObject(const std::string &serviceName, const std::string &serverDeviceId,
52         uint64_t stubIndex, IPCObjectProxy *proxy, uint32_t tokenId);
53     ~DBinderSessionObject();
54 
55     void SetServiceName(const std::string &serviceName);
56     void SetDeviceId(const std::string &serverDeviceId);
57     void SetProxy(IPCObjectProxy *proxy);
58     std::shared_ptr<BufferObject> GetSessionBuff();
59     std::string GetServiceName() const;
60     std::string GetDeviceId() const;
61     IPCObjectProxy *GetProxy() const;
62     uint64_t GetStubIndex() const;
63 
64     void CloseDatabusSession();
65     uint32_t GetTokenId() const;
66     int32_t GetSocketId() const;
67     void SetSocketId(int32_t socketId);
68     void SetPeerPid(int peerPid);
69     void SetPeerUid(int peerUid);
70     int GetPeerPid() const;
71     int GetPeerUid() const;
72 
73 private:
74     DISALLOW_COPY_AND_MOVE(DBinderSessionObject);
75 
76     int32_t socket_ = SOCKET_ID_INVALID;
77     std::mutex buffMutex_;
78     std::shared_ptr<BufferObject> buff_;
79     std::string serviceName_;
80     std::string serverDeviceId_;
81     uint64_t stubIndex_;
82     IPCObjectProxy *proxy_;
83     uint32_t tokenId_;
84     int pid_;
85     int uid_;
86 };
87 } // namespace OHOS
88 #endif // OHOS_IPC_DBINDER_SESSION_OBJECT_H
89