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