2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief RPC tools--Function/macro definitions and APIDef class definitions
21 /** @addtogroup RPCtool
23 * @brief Using the RPC Tool (rpc_apidef)
25 * rpc_apidef [CPP option...] API definition file name
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".
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)
44 * - Examples1: rpc_apidef -DTEST XXX.api
45 * - Examples2: rpc_apidef XXX.api
46 * - => Xxx_api.h, xxx_api_stub.c, ... To the output
48 #ifndef RPC_LIBRARY_TOOL_APIDEF_H_
49 #define RPC_LIBRARY_TOOL_APIDEF_H_
53 #endif /* __cplusplus */
56 extern char rpc_log_enable;
59 /* C++ to C I/F functions */
60 const char *TypeCodeString(const int code);
61 int IsTypeCodeNumeric(const int code);
64 * C to C++ I/F functions (APIDef class)
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);
79 #endif /* __cplusplus */
81 #define RPC_IN_ARG 0x01
82 #define RPC_OUT_ARG 0x02
83 #define RPC_INOUT_ARG (RPC_IN_ARG|RPC_OUT_ARG)
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_"
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"
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"
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"
114 #define RPCTOOL_WARNING_STRING \
115 " * The file created by the RPC tool.This file should not be edited."
117 #define EXTERN_C_START \
118 "#ifdef __cplusplus\n" \
122 #define EXTERN_C_END \
123 "#ifdef __cplusplus\n" \
125 "#endif /* __cplusplus */\n"
133 using namespace std; // NOLINT (readability/nolint)
137 * @brief Class that stores and processes the arguments of functions read from the API definition file
144 int m_in_out; /* Bit OR of _IN_ARG and _OUT_ARG */
152 m_code = m_bytes = m_is_pointer = m_in_out = m_is_vararray = m_is_array_size = 0;
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) {
160 m_is_pointer = is_pointer;
161 m_is_vararray = is_vararray;
162 m_is_array_size = is_array_size;
165 m_type_name = type_name;
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)
180 int PrintUndecoratedName(fstream& out, int num);
181 int PrintPrototype(fstream& out, int num);
182 int PrintPrototype(fstream& out) { return PrintPrototype(out, 0); }
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)
193 int IsVararray(void) { return m_is_vararray; }
194 int IsArraySize(void);
200 * @brief Class that stores and processes function definitions read from the API definition file
206 int m_array_size_pos;
207 string m_array_size_name;
210 Function():m_array_size_name("") { m_array_size_pos = 0; }
212 list<Arg>::size_type size = m_args.size();
213 for (list<Arg>::size_type i = 0 ; i < size ; i++) {
219 const char *Name(void) { return m_name.c_str(); }
220 void SetName(const char *funcname) {
224 int NumOfArgs(void) {
225 return (int)(m_args.size()); // NOLINT (readability/nolint)
227 int NumOfInOutArgs(int in_out) {
229 list<Arg>::iterator a;
230 for (a = m_args.begin(); a != m_args.end(); ++a) {
231 if ((a->InOut() & in_out) != 0) {
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);
242 int PrintPrototype(fstream& out);
243 int PrintPrototype(fstream& out, int server);
245 int PrintMacro(fstream& out);
248 int PrintServerStub(fstream& out);
249 int PrintClientStub(const char *moduleid, fstream& out);
251 int CheckFuncArraySize(void);
252 void Print(int with_args);
257 * @brief Class that stores and processes the result of reading the API definition file
261 list<Function> m_funcs;
265 list<string> m_headers;
268 APIDef() { m_work = 0; }
270 list<Function>::size_type size = m_funcs.size();
271 for (list<Function>::size_type i = 0; i < size; i++) {
275 if (m_work != NULL) {
278 size = m_headers.size();
279 for (list<Function>::size_type i = 0; i < size; i++) {
281 m_headers.pop_front();
285 void IdTolower(void);
286 void DefineId(const char *id) {
290 void AddHeader(const char *filename) {
291 string str = filename;
292 m_headers.push_back(str);
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();
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);
306 void NameFunction(const char *funcname) {
307 if (m_work == NULL) {
308 m_work = new Function();
310 m_work->SetName(funcname);
312 AppendFunction(m_work);
317 void AppendFunction(Function *pfunc) {
318 m_funcs.push_back(*pfunc);
321 int CheckAllArraySize(void);
324 void Print(int with_args);
327 int MakeHeaderFiles(int server);
328 int MakeServerStub(void);
329 int MakeClientStub(void);
331 #endif /* __cplusplus */
333 #endif // RPC_LIBRARY_TOOL_APIDEF_H_