1 /*
2  * Copyright (c) 2022 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 _LITE_IPC_H
17 #define _LITE_IPC_H
18 #include <stdint.h>
19 #include <sys/ioctl.h>
20 #include "serializer.h"
21 #include "ipc_types.h"
22 #include "utils_list.h"
23 
24 #ifdef __cplusplus
25 #if __cplusplus
26 extern "C" {
27 #endif /* __cplusplus */
28 #endif /* __cplusplus */
29 
30 typedef enum {
31     MT_REQUEST,
32     MT_REPLY,
33     MT_FAILED_REPLY,
34     MT_DEATH_NOTIFY,
35     MT_NUM
36 } MsgType;
37 
38 typedef enum {
39     CMS_GEN_HANDLE,
40     CMS_REMOVE_HANDLE,
41     CMS_ADD_ACCESS
42 } CmsCmd;
43 
44 typedef struct {
45     CmsCmd cmd;
46     uint32_t taskID;
47     uint32_t serviceHandle;
48 } CmsCmdContent;
49 
50 typedef struct {
51     MsgType type;          /**< cmd type, decide the data structure below*/
52     SvcIdentity target;    /**< serviceHandle or targetTaskId, depending on type */
53     uint32_t code;         /**< service function code */
54     uint32_t flag;
55     uint64_t timestamp;
56     uint32_t dataSz;       /**< size of data */
57     void *data;
58     uint32_t spObjNum;
59     void *offsets;
60     uint32_t processID;    /**< filled by kernel, processId of sender/reciever */
61     uint32_t taskID;       /**< filled by kernel, taskId of sender/reciever */
62     uint32_t userID;
63     uint32_t gid;
64 } IpcMsg;
65 
66 #define SEND (1 << 0)
67 #define RECV (1 << 1)
68 #define BUFF_FREE (1 << 2)
69 
70 typedef struct {
71     uint32_t flag;          /**< size of writeData */
72     IpcMsg *outMsg;         /**< data to send to target */
73     IpcMsg *inMsg;          /**< data reply by target */
74     void *buffToFree;
75 } IpcContent;
76 
77 typedef struct {
78     int32_t driverVersion;
79 } IpcVersion;
80 
81 /* lite ipc ioctl */
82 #define IPC_IOC_MAGIC       'i'
83 #define IPC_SET_CMS         _IO(IPC_IOC_MAGIC, 1)
84 #define IPC_CMS_CMD         _IOWR(IPC_IOC_MAGIC, 2, CmsCmdContent)
85 #define IPC_SET_IPC_THREAD  _IO(IPC_IOC_MAGIC, 3)
86 #define IPC_SEND_RECV_MSG   _IOWR(IPC_IOC_MAGIC, 4, IpcContent)
87 #define IPC_GET_VERSION     _IOR(IPC_IOC_MAGIC, 5, IpcVersion)
88 
89 typedef enum {
90     OBJ_FD,
91     OBJ_PTR,
92     OBJ_SVC
93 } ObjType;
94 
95 typedef union {
96     uint32_t fd;
97     SvcIdentity svc;
98 } ObjContent;
99 
100 typedef struct {
101     ObjType type;
102     ObjContent content;
103 } SpecialObj;
104 
105 typedef struct {
106     pthread_mutex_t mutex;
107     int32_t handleId;
108     bool threadWorking;
109     UTILS_DL_LIST apis;
110 } IpcCallback;
111 
112 typedef struct {
113     uint32_t num;
114     void *msg;
115     IpcIo io;
116     IpcObjectStub *cbs;
117     bool useFlag;
118 } HdlerArg;
119 
120 typedef struct {
121     UTILS_DL_LIST list;
122     uint32_t token;
123     IpcObjectStub hdlerPair;
124 } AnonymousApi;
125 
126 int32_t StartCallbackDispatch(void);
127 IpcCallback *GetIpcCb(void);
128 uint32_t GetThreadId(void);
129 uintptr_t GetObjectStub(uintptr_t cookie);
130 
131 #ifdef __cplusplus
132 #if __cplusplus
133 }
134 #endif /* __cplusplus */
135 #endif /* __cplusplus */
136 
137 #endif
138