Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / otherservice / rpc_library / tool / apidef.h
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file apidef.h
19  * @brief RPC tools--Function/macro definitions and APIDef class definitions
20  */
21 /** @addtogroup RPCtool
22  *
23  * @brief Using the RPC Tool (rpc_apidef)
24  *
25  * rpc_apidef [CPP option...] API definition file name
26  *
27  * - CPP option
28  *   The RPC tool calls the C preprocessor (CPP) internally 
29  *   for processing comments and #if directives.
30  *   Here, you can specify options to be given to the CPP, such as symbol definitions 
31  *   for conditional branch descriptions in the API definition file.
32  * - API definition file name
33  *   Pathname to the API definition file.
34  *   The file name must be thread ID + ".api".
35  *
36  * The output file is as follows.
37  * "*" is a lowercase version of the API definition file name preceding ".api".
38  * - *_api.h(Client header files)
39  * - *_api_stub.c(Client stub file)
40  * - *_srvr.h(Server header file)
41  * - *_srvr_stub.c(Server stub file)
42  *
43  * Examples:
44  * - Examples1: rpc_apidef -DTEST XXX.api
45  * - Examples2: rpc_apidef XXX.api
46  * - => Xxx_api.h, xxx_api_stub.c, ... To the output
47  */
48 #ifndef RPC_LIBRARY_TOOL_APIDEF_H_
49 #define RPC_LIBRARY_TOOL_APIDEF_H_
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif /* __cplusplus */
54
55 #ifdef DBG_ENABLE
56 extern char rpc_log_enable;
57 #endif
58
59 /* C++ to C I/F functions */
60 const char *TypeCodeString(const int code);
61 int IsTypeCodeNumeric(const int code);
62
63 /*
64  * C to C++ I/F functions (APIDef class)
65  */
66 void ApidefDefineId(const char *id);
67 void ApidefAddHeader(const char *filename);
68 int ApidefPushFunctionArg(int arg_code, int buffer_bytes, int is_pointer,
69            int is_vararray, int is_array_size,
70            const char *var_type_name, int in_out,
71            const char *var_name);
72 void ApidefProcessFunction(const char *funcname);
73 void ApidefListFunctions(int with_args);
74 void ApidefFreeAllocation(void);
75 int ApidefMakeStubs(void);
76
77 #ifdef __cplusplus
78 }
79 #endif /* __cplusplus */
80
81 #define RPC_IN_ARG 0x01
82 #define RPC_OUT_ARG 0x02
83 #define RPC_INOUT_ARG (RPC_IN_ARG|RPC_OUT_ARG)
84
85 #define MAX_FILENAME_LEN 63
86 #define SERVER_HEADER_FILE "_srvr.h"
87 #define CLIENT_HEADER_FILE "_api.h"
88 #define SERVER_STUB_FILE "_srvr_stub.c"
89 #define CLIENT_STUB_FILE "_api_stub.c"
90 #define SERVER_HEADER_DEFINE "_srvr_h_"
91 #define CLIENT_HEADER_DEFINE "_api_h_"
92
93 #define SERVER_HEADER_TITLE "Server header files"
94 #define CLIENT_HEADER_TITLE "Client header files"
95 #define SERVER_STUB_TITLE "Server stub file"
96 #define CLIENT_STUB_TITLE "Client stub file"
97
98 #define RPC_MARSHALL_FUNCTION "RPC_marshall_arguments"
99 #define RPC_DEMARSHALL_FUNCTION "RPC_demarshall_arguments"
100 #define RPC_MARSHALL_FREE_FUNCTION "RPC_marshall_free"
101 #define RPC_RETURN_FREE_FUNCTION "RPC_free_return_string"
102
103 #define RPC_API_SERVER_RETURN "RPC_Result"
104 #define RPC_API_SERVER_ERROR "RPC_ERR_Fatal"
105 #define RPC_API_CLIENT_RETURN "RPC_Result"
106 #define RPC_API_CLIENT_ERROR "RPC_ERR_Fatal"
107 #define RPC_API_DISPATCH_TYPE "RPC_dispatch_func_t"
108 #define RPC_API_DISPATCH_RETURN "RPC_Result"
109 #define RPC_API_DISPATCH_FUNC_FULL "_API_dispatch(UINT16 api_num, const char *args_string, unsigned int args_size, char **ret_string, unsigned int *ret_size)"  // NOLINT (readability/nolint)
110 #define RPC_API_DISPATCH_FUNC_NAME "_API_dispatch"
111 #define RPC_API_NUM_PREFIX "RPC_API_NUM_"
112 #define RPC_GLOBAL_HEADER_FILE "other_service/rpc.h"
113
114 #define RPCTOOL_WARNING_STRING \
115 " * The file created by the RPC tool.This file should not be edited."
116
117 #define EXTERN_C_START \
118 "#ifdef __cplusplus\n" \
119 "extern \"C\" {\n" \
120 "#endif\n"
121
122 #define EXTERN_C_END \
123 "#ifdef __cplusplus\n" \
124 "}\n" \
125 "#endif /* __cplusplus */\n"
126
127 #ifdef __cplusplus
128 #include <iostream>
129 #include <fstream>
130 #include <string>
131 #include <list>
132
133 using namespace std;  // NOLINT (readability/nolint)
134
135 /** @ingroup RPCtool
136  * @class Arg
137  * @brief Class that stores and processes the arguments of functions read from the API definition file
138  */
139 class Arg {
140  protected:
141   int m_code;
142   int m_bytes;
143   int m_is_pointer;
144   int m_in_out; /* Bit OR of _IN_ARG and _OUT_ARG */
145   int m_is_vararray;
146   int m_is_array_size;
147   string m_type_name;
148   string m_name;
149
150  public:
151   Arg() {
152     m_code = m_bytes = m_is_pointer = m_in_out = m_is_vararray = m_is_array_size = 0;
153   }
154   Arg(int code, int bytes, int is_pointer,
155        int is_vararray, int is_array_size,
156        const char *type_name, int in_out,
157        const char *var_name) {
158     m_code = code;
159     m_bytes = bytes;
160     m_is_pointer = is_pointer;
161     m_is_vararray = is_vararray;
162     m_is_array_size = is_array_size;
163     m_in_out = in_out;
164     if (type_name) {
165       m_type_name = type_name;
166     }
167     if (var_name) {
168       m_name = var_name;
169     }
170   }
171   ~Arg() {}
172
173   int Code(void) { return m_code; }
174   int Bytes(void) { return m_bytes; }
175   int IsPointer(void) { return m_is_pointer; }
176   int InOut(void) { return m_in_out; }
177   string& TypeName(void) { return m_type_name; }
178   void GetUndecolatedName(string& name, int num);  // NOLINT (readability/nolint)
179
180   int PrintUndecoratedName(fstream& out, int num);
181   int PrintPrototype(fstream& out, int num);
182   int PrintPrototype(fstream& out) { return PrintPrototype(out, 0); }
183
184   int CreateMarshallArgs(fstream& out, int num);
185   int CreateMarshallArgs(fstream& out, int num, string &array_size_name);  // NOLINT (readability/nolint)
186   int CreateMarshallArgs(fstream& out, int num, int in_out);
187   int CreateMarshallArgs(fstream& out, int num, int in_out, string &array_size_name);  // NOLINT (readability/nolint)
188   int CreateDemarshallArgs(fstream& out, int deliver_pointer, int num);
189   int CreateDemarshallArgs(fstream& out, int deliver_pointer, int num, string &array_size_name);  // NOLINT (readability/nolint)
190   int CreateDemarshallArgs(fstream& out, int deliver_pointer, int num, int in_out);
191   int CreateDemarshallArgs(fstream& out, int deliver_pointer, int num, int in_out, string &array_size_name);  // NOLINT (readability/nolint)
192
193   int IsVararray(void) { return m_is_vararray; }
194   int IsArraySize(void);
195   void Print(void);
196 };
197
198 /** @ingroup RPCtool
199  * @class Function
200  * @brief Class that stores and processes function definitions read from the API definition file
201  */
202 class Function {
203  protected:
204   string m_name;
205   list<Arg> m_args;
206   int m_array_size_pos;
207   string m_array_size_name;
208
209  public:
210   Function():m_array_size_name("") { m_array_size_pos = 0; }
211   ~Function() {
212     list<Arg>::size_type size = m_args.size();
213     for (list<Arg>::size_type i = 0 ; i < size ; i++) {
214       m_args.begin();
215       m_args.pop_front();
216     }
217   }
218
219   const char *Name(void) { return m_name.c_str(); }
220   void SetName(const char *funcname) {
221     m_name = funcname;
222   }
223
224   int NumOfArgs(void) {
225     return (int)(m_args.size());  // NOLINT (readability/nolint)
226   }
227   int NumOfInOutArgs(int in_out) {
228     int count = 0;
229     list<Arg>::iterator a;
230     for (a = m_args.begin(); a != m_args.end(); ++a) {
231       if ((a->InOut() & in_out) != 0) {
232         ++count;
233       }
234     }
235     return count;
236   }
237
238   int AppendArg(int code, int bytes, int is_pointer,
239      int is_vararray, int is_array_size,
240      const char *var_type_name, int in_out, const char *var_name);
241
242   int PrintPrototype(fstream& out);
243   int PrintPrototype(fstream& out, int server);
244 #ifdef DBG_ENABLE
245   int PrintMacro(fstream& out);
246 #endif
247
248   int PrintServerStub(fstream& out);
249   int PrintClientStub(const char *moduleid, fstream& out);
250
251   int CheckFuncArraySize(void);
252   void Print(int with_args);
253 };
254
255 /** @ingroup RPCtool
256  * @class APIDef
257  * @brief Class that stores and processes the result of reading the API definition file
258  */
259 class APIDef {
260  protected:
261   list<Function> m_funcs;
262   Function *m_work;
263   string m_id;
264   string m_lowerid;
265   list<string> m_headers;
266
267  public:
268   APIDef() { m_work = 0; }
269   ~APIDef() {
270     list<Function>::size_type size = m_funcs.size();
271     for (list<Function>::size_type i = 0; i < size; i++) {
272       m_funcs.begin();
273       m_funcs.pop_front();
274     }
275     if (m_work != NULL) {
276       delete m_work;
277     }
278     size = m_headers.size();
279     for (list<Function>::size_type i = 0; i < size; i++) {
280       m_headers.begin();
281       m_headers.pop_front();
282     }
283   }
284
285   void IdTolower(void);
286   void DefineId(const char *id) {
287     m_id = id;
288     IdTolower();
289   }
290   void AddHeader(const char *filename) {
291     string str = filename;
292     m_headers.push_back(str);
293   }
294
295   int AddFunctionArg(int arg_code, int buffer_bytes, int is_pointer,
296            int is_vararray, int is_array_size,
297            const char *var_type_name, int in_out,
298            const char *var_name) {
299     if (m_work == NULL) {
300       m_work = new Function();
301     }
302     return m_work->AppendArg(arg_code, buffer_bytes, is_pointer,
303             is_vararray, is_array_size,
304             var_type_name, in_out, var_name);
305   }
306   void NameFunction(const char *funcname) {
307     if (m_work == NULL) {
308       m_work = new Function();
309     }
310     m_work->SetName(funcname);
311
312     AppendFunction(m_work);
313     delete m_work;
314     m_work = NULL;
315   }
316
317   void AppendFunction(Function *pfunc) {
318     m_funcs.push_back(*pfunc);
319   }
320
321   int CheckAllArraySize(void);
322
323   int MakeStubs(void);
324   void Print(int with_args);
325
326  private:
327   int MakeHeaderFiles(int server);
328   int MakeServerStub(void);
329   int MakeClientStub(void);
330 };
331 #endif  /* __cplusplus */
332
333 #endif  // RPC_LIBRARY_TOOL_APIDEF_H_