summaryrefslogtreecommitdiffstats
path: root/otherservice/rpc_library/tool/apidef.h
blob: a73fc49c7c2a039bdce1850483afd138b1e98854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * @copyright Copyright (c) 2016-2019 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file apidef.h
 * @brief RPC tools--Function/macro definitions and APIDef class definitions
 */
/** @addtogroup RPCtool
 *
 * @brief Using the RPC Tool (rpc_apidef)
 *
 * rpc_apidef [CPP option...] API definition file name
 *
 * - CPP option
 *   The RPC tool calls the C preprocessor (CPP) internally 
 *   for processing comments and #if directives.
 *   Here, you can specify options to be given to the CPP, such as symbol definitions 
 *   for conditional branch descriptions in the API definition file.
 * - API definition file name
 *   Pathname to the API definition file.
 *   The file name must be thread ID + ".api".
 *
 * The output file is as follows.
 * "*" is a lowercase version of the API definition file name preceding ".api".
 * - *_api.h(Client header files)
 * - *_api_stub.c(Client stub file)
 * - *_srvr.h(Server header file)
 * - *_srvr_stub.c(Server stub file)
 *
 * Examples:
 * - Examples1: rpc_apidef -DTEST XXX.api
 * - Examples2: rpc_apidef XXX.api
 * - => Xxx_api.h, xxx_api_stub.c, ... To the output
 */
#ifndef RPC_LIBRARY_TOOL_APIDEF_H_
#define RPC_LIBRARY_TOOL_APIDEF_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#ifdef DBG_ENABLE
extern char rpc_log_enable;
#endif

/* C++ to C I/F functions */
const char *TypeCodeString(const int code);
int IsTypeCodeNumeric(const int code);

/*
 * C to C++ I/F functions (APIDef class)
 */
void ApidefDefineId(const char *id);
void ApidefAddHeader(const char *filename);
int ApidefPushFunctionArg(int arg_code, int buffer_bytes, int is_pointer,
           int is_vararray, int is_array_size,
           const char *var_type_name, int in_out,
           const char *var_name);
void ApidefProcessFunction(const char *funcname);
void ApidefListFunctions(int with_args);
void ApidefFreeAllocation(void);
int ApidefMakeStubs(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#define RPC_IN_ARG 0x01
#define RPC_OUT_ARG 0x02
#define RPC_INOUT_ARG (RPC_IN_ARG|RPC_OUT_ARG)

#define MAX_FILENAME_LEN 63
#define SERVER_HEADER_FILE "_srvr.h"
#define CLIENT_HEADER_FILE "_api.h"
#define SERVER_STUB_FILE "_srvr_stub.c"
#define CLIENT_STUB_FILE "_api_stub.c"
#define SERVER_HEADER_DEFINE "_srvr_h_"
#define CLIENT_HEADER_DEFINE "_api_h_"

#define SERVER_HEADER_TITLE "Server header files"
#define CLIENT_HEADER_TITLE "Client header files"
#define SERVER_STUB_TITLE "Server stub file"
#define CLIENT_STUB_TITLE "Client stub file"

#define RPC_MARSHALL_FUNCTION "RPC_marshall_arguments"
#define RPC_DEMARSHALL_FUNCTION "RPC_demarshall_arguments"
#define RPC_MARSHALL_FREE_FUNCTION "RPC_marshall_free"
#define RPC_RETURN_FREE_FUNCTION "RPC_free_return_string"

#define RPC_API_SERVER_RETURN "RPC_Result"
#define RPC_API_SERVER_ERROR "RPC_ERR_Fatal"
#define RPC_API_CLIENT_RETURN "RPC_Result"
#define RPC_API_CLIENT_ERROR "RPC_ERR_Fatal"
#define RPC_API_DISPATCH_TYPE "RPC_dispatch_func_t"
#define RPC_API_DISPATCH_RETURN "RPC_Result"
#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)
#define RPC_API_DISPATCH_FUNC_NAME "_API_dispatch"
#define RPC_API_NUM_PREFIX "RPC_API_NUM_"
#define RPC_GLOBAL_HEADER_FILE "other_service/rpc.h"

#define RPCTOOL_WARNING_STRING \
" * The file created by the RPC tool.This file should not be edited."

#define EXTERN_C_START \
"#ifdef __cplusplus\n" \
"extern \"C\" {\n" \
"#endif\n"

#define EXTERN_C_END \
"#ifdef __cplusplus\n" \
"}\n" \
"#endif /* __cplusplus */\n"

#ifdef __cplusplus
#include <iostream>
#include <fstream>
#include <string>
#include <list>

using namespace std;  // NOLINT (readability/nolint)

/** @ingroup RPCtool
 * @class Arg
 * @brief Class that stores and processes the arguments of functions read from the API definition file
 */
class Arg {
 protected:
  int m_code;
  int m_bytes;
  int m_is_pointer;
  int m_in_out; /* Bit OR of _IN_ARG and _OUT_ARG */
  int m_is_vararray;
  int m_is_array_size;
  string m_type_name;
  string m_name;

 public:
  Arg() {
    m_code = m_bytes = m_is_pointer = m_in_out = m_is_vararray = m_is_array_size = 0;
  }
  Arg(int code, int bytes, int is_pointer,
       int is_vararray, int is_array_size,
       const char *type_name, int in_out,
       const char *var_name) {
    m_code = code;
    m_bytes = bytes;
    m_is_pointer = is_pointer;
    m_is_vararray = is_vararray;
    m_is_array_size = is_array_size;
    m_in_out = in_out;
    if (type_name) {
      m_type_name = type_name;
    }
    if (var_name) {
      m_name = var_name;
    }
  }
  ~Arg() {}

  int Code(void) { return m_code; }
  int Bytes(void) { return m_bytes; }
  int IsPointer(void) { return m_is_pointer; }
  int InOut(void) { return m_in_out; }
  string& TypeName(void) { return m_type_name; }
  void GetUndecolatedName(string& name, int num);  // NOLINT (readability/nolint)

  int PrintUndecoratedName(fstream& out, int num);
  int PrintPrototype(fstream& out, int num);
  int PrintPrototype(fstream& out) { return PrintPrototype(out, 0); }

  int CreateMarshallArgs(fstream& out, int num);
  int CreateMarshallArgs(fstream& out, int num, string &array_size_name);  // NOLINT (readability/nolint)
  int CreateMarshallArgs(fstream& out, int num, int in_out);
  int CreateMarshallArgs(fstream& out, int num, int in_out, string &array_size_name);  // NOLINT (readability/nolint)
  int CreateDemarshallArgs(fstream& out, int deliver_pointer, int num);
  int CreateDemarshallArgs(fstream& out, int deliver_pointer, int num, string &array_size_name);  // NOLINT (readability/nolint)
  int CreateDemarshallArgs(fstream& out, int deliver_pointer, int num, int in_out);
  int CreateDemarshallArgs(fstream& out, int deliver_pointer, int num, int in_out, string &array_size_name);  // NOLINT (readability/nolint)

  int IsVararray(void) { return m_is_vararray; }
  int IsArraySize(void);
  void Print(void);
};

/** @ingroup RPCtool
 * @class Function
 * @brief Class that stores and processes function definitions read from the API definition file
 */
class Function {
 protected:
  string m_name;
  list<Arg> m_args;
  int m_array_size_pos;
  string m_array_size_name;

 public:
  Function():m_array_size_name("") { m_array_size_pos = 0; }
  ~Function() {
    list<Arg>::size_type size = m_args.size();
    for (list<Arg>::size_type i = 0 ; i < size ; i++) {
      m_args.begin();
      m_args.pop_front();
    }
  }

  const char *Name(void) { return m_name.c_str(); }
  void SetName(const char *funcname) {
    m_name = funcname;
  }

  int NumOfArgs(void) {
    return (int)(m_args.size());  // NOLINT (readability/nolint)
  }
  int NumOfInOutArgs(int in_out) {
    int count = 0;
    list<Arg>::iterator a;
    for (a = m_args.begin(); a != m_args.end(); ++a) {
      if ((a->InOut() & in_out) != 0) {
        ++count;
      }
    }
    return count;
  }

  int AppendArg(int code, int bytes, int is_pointer,
     int is_vararray, int is_array_size,
     const char *var_type_name, int in_out, const char *var_name);

  int PrintPrototype(fstream& out);
  int PrintPrototype(fstream& out, int server);
#ifdef DBG_ENABLE
  int PrintMacro(fstream& out);
#endif

  int PrintServerStub(fstream& out);
  int PrintClientStub(const char *moduleid, fstream& out);

  int CheckFuncArraySize(void);
  void Print(int with_args);
};

/** @ingroup RPCtool
 * @class APIDef
 * @brief Class that stores and processes the result of reading the API definition file
 */
class APIDef {
 protected:
  list<Function> m_funcs;
  Function *m_work;
  string m_id;
  string m_lowerid;
  list<string> m_headers;

 public:
  APIDef() { m_work = 0; }
  ~APIDef() {
    list<Function>::size_type size = m_funcs.size();
    for (list<Function>::size_type i = 0; i < size; i++) {
      m_funcs.begin();
      m_funcs.pop_front();
    }
    if (m_work != NULL) {
      delete m_work;
    }
    size = m_headers.size();
    for (list<Function>::size_type i = 0; i < size; i++) {
      m_headers.begin();
      m_headers.pop_front();
    }
  }

  void IdTolower(void);
  void DefineId(const char *id) {
    m_id = id;
    IdTolower();
  }
  void AddHeader(const char *filename) {
    string str = filename;
    m_headers.push_back(str);
  }

  int AddFunctionArg(int arg_code, int buffer_bytes, int is_pointer,
           int is_vararray, int is_array_size,
           const char *var_type_name, int in_out,
           const char *var_name) {
    if (m_work == NULL) {
      m_work = new Function();
    }
    return m_work->AppendArg(arg_code, buffer_bytes, is_pointer,
            is_vararray, is_array_size,
            var_type_name, in_out, var_name);
  }
  void NameFunction(const char *funcname) {
    if (m_work == NULL) {
      m_work = new Function();
    }
    m_work->SetName(funcname);

    AppendFunction(m_work);
    delete m_work;
    m_work = NULL;
  }

  void AppendFunction(Function *pfunc) {
    m_funcs.push_back(*pfunc);
  }

  int CheckAllArraySize(void);

  int MakeStubs(void);
  void Print(int with_args);

 private:
  int MakeHeaderFiles(int server);
  int MakeServerStub(void);
  int MakeClientStub(void);
};
#endif  /* __cplusplus */

#endif  // RPC_LIBRARY_TOOL_APIDEF_H_