build_c_skeletons.c 9.26 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
1
/* Buildsupport is (c) 2008-2016 European Space Agency
Maxime Perrotin's avatar
Maxime Perrotin committed
2
3
4
5
6
7
8
 * contact: maxime.perrotin@esa.int
 * License is LGPL, check LICENSE file */
/* build_c_skeletons.c

  this program generates empty C functions respecting the interfaces defined
  in the interface view. it provides functions to invoke RI.

Maxime Perrotin's avatar
Maxime Perrotin committed
9
  updated 10/06/2016: properly align params and use FOREACH
Maxime Perrotin's avatar
Maxime Perrotin committed
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
  updated 20/04/2009 to disable in case "-onlycv" flag is set
  updated 8/10/2009 to rename the user_code.h/c to FV_name.h/c like in the Ada backend
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <assert.h>

#include "my_types.h"
#include "practical_functions.h"

static FILE *user_code_c = NULL, *user_code_h = NULL;


/* Adds header to user_code.h and (if new) user_code.c */
void c_gw_preamble(FV * fv)
{
    int hasparam = 0;
    assert (NULL != user_code_h);

    /* Check if any interface needs ASN.1 types */
    FOREACH(i, Interface, fv->interfaces, {
            CheckForAsn1Params(i, &hasparam);}
    );

    /* a. user_code.h preamble */
    fprintf(user_code_h,
            "/* This file was generated automatically: DO NOT MODIFY IT ! */\n\n");
    fprintf(user_code_h,
            "/* Declaration of the functions that have to be provided by the user */\n\n");
    fprintf(user_code_h,
            "#ifndef __USER_CODE_H_%s__\n#define __USER_CODE_H_%s__\n\n",
            fv->name, fv->name);

    if (hasparam) {
        fprintf(user_code_h, "#include \"C_ASN1_Types.h\"\n\n");
    }

    fprintf(user_code_h,
            "#ifdef __cplusplus\n"
            "extern \"C\" {\n" 
            "#endif\n\n");


    fprintf(user_code_h, "void %s_startup();\n\n", fv->name);

    /* b. user_code.c preamble (if applicable) */
    if (NULL != user_code_c) {
        fprintf(user_code_c,
Maxime Perrotin's avatar
Maxime Perrotin committed
62
                "/* User code: This file will not be overwritten by TASTE. */\n\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
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
        fprintf(user_code_c, "#include \"%s.h\"\n\n", fv->name);


        if (has_context_param(fv)) {
            char *fv_no_underscore =
                underscore_to_dash(fv->name, strlen(fv->name));
            fprintf(user_code_c,
                    "/* Function static data is declared in this file : */\n");
            fprintf(user_code_c, "#include \"Context-%s.h\"\n\n",
                    fv_no_underscore);
            free(fv_no_underscore);
        }

        fprintf(user_code_c, "void %s_startup()\n{\n", fv->name);

        fprintf(user_code_c,
                "    /* Write your initialization code here,\n"
                "       but do not make any call to a required interface."
                " */\n}\n\n");
    }
}

/* Creates user_code.h, and if necessary user_code.c (if it did not exist) */
int Init_C_GW_Backend(FV * fv, bool generateC)
{
    char *path = NULL;
    char *filename = NULL;

    if (NULL != fv->system_ast->context->output) {
        build_string(&path, fv->system_ast->context->output,
                     strlen(fv->system_ast->context->output));
    }
    build_string(&path, fv->name, strlen(fv->name));
    build_string(&filename, fv->name, strlen(fv->name));
    build_string(&filename, ".h", strlen(".h"));

Maxime Perrotin's avatar
Maxime Perrotin committed
99
    create_file(path, filename, &user_code_h);
Maxime Perrotin's avatar
Maxime Perrotin committed
100
    filename[strlen(filename) - 1] = 'c';       /* change from .h to .c */
Maxime Perrotin's avatar
Maxime Perrotin committed
101
102
103
    if (cpp == fv->language) {
        build_string(&filename, "c", 1);       /* change from .c to .cc */
    }
Maxime Perrotin's avatar
Maxime Perrotin committed
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

    if (!file_exists(path, filename) && true == generateC)
        create_file(path, filename, &user_code_c);

    free(path);
    if (NULL == user_code_h)
        return -1;

    c_gw_preamble(fv);

    return 0;
}


void close_c_gw_files()
{
    if (NULL != user_code_h) {
        fprintf(user_code_h,
            "#ifdef __cplusplus\n"
            "}\n" 
            "#endif\n\n");
        fprintf(user_code_h, "\n#endif\n");
        close_file(&user_code_h);
    }
    close_file(&user_code_c);
}

/* Add a Provided interface. Can contain in and out parameters
 * Write in user_code.h the declaration of the user functions
 * and if user_code.c is new, copy these declarations there too.
 */
void add_PI_to_C_gw(Interface * i)
{
    if (NULL == user_code_h)
        return;

Maxime Perrotin's avatar
Maxime Perrotin committed
140
141
142
    char *signature = make_string("void %s_PI_%s(",
                                  i->parent_fv->name,
                                  i->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
143

Maxime Perrotin's avatar
Maxime Perrotin committed
144
145
146
    size_t sig_len = strlen(signature);
    char *sep = make_string(",\n%*s", sig_len, "");
    bool comma = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
147

Maxime Perrotin's avatar
Maxime Perrotin committed
148
    fprintf(user_code_h, "%s", signature);
Maxime Perrotin's avatar
Maxime Perrotin committed
149

Maxime Perrotin's avatar
Maxime Perrotin committed
150
151
    if (NULL != user_code_c) {
        fprintf(user_code_c, "%s", signature);
Maxime Perrotin's avatar
Maxime Perrotin committed
152
153
    }

Maxime Perrotin's avatar
Maxime Perrotin committed
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
    FOREACH (p, Parameter, i->in, {
        char *sort = make_string("%sconst asn1Scc%s *",
                                 comma? sep: "",
                                 p->type);
        fprintf(user_code_h, "%s", sort);
        if(NULL != user_code_c) {
            fprintf(user_code_c, "%sIN_%s",
                                 sort,
                                 p->name);
        }
        free(sort);
        comma = true;
    });

    FOREACH (p, Parameter, i->out, {
        char *sort = make_string("%sasn1Scc%s *",
                                 comma? sep: "",
                                 p->type);
        fprintf(user_code_h, "%s", sort);
        if(NULL != user_code_c) {
            fprintf(user_code_c, "%sOUT_%s",
                                 sort,
                                 p->name);
        }
        free(sort);
        comma = true;
    });

Maxime Perrotin's avatar
Maxime Perrotin committed
182
183
184
    fprintf(user_code_h, ");\n\n");
    if (NULL != user_code_c)
        fprintf(user_code_c, ")\n{\n    /* Write your code here! */\n}\n\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
185
186
187

    free(signature);
    free(sep);
Maxime Perrotin's avatar
Maxime Perrotin committed
188
189
190
191
192
193
194
195
}

/* Declaration of the RI in user_code.h */
void add_RI_to_C_gw(Interface * i)
{
    if (NULL == user_code_h)
        return;

Maxime Perrotin's avatar
Maxime Perrotin committed
196
197
198
    char *signature = make_string("extern void %s_RI_%s(",
                                  i->parent_fv->name,
                                  i->name);
Maxime Perrotin's avatar
Maxime Perrotin committed
199

Maxime Perrotin's avatar
Maxime Perrotin committed
200
201
202
    size_t sig_len = strlen(signature);
    char *sep = make_string(",\n%*s", sig_len, "");
    bool comma = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
203

Maxime Perrotin's avatar
Maxime Perrotin committed
204
    fprintf(user_code_h, "%s", signature);
Maxime Perrotin's avatar
Maxime Perrotin committed
205

Maxime Perrotin's avatar
Maxime Perrotin committed
206
207
208
209
210
211
212
213
214
215
216
217
218
    FOREACH (p, Parameter, i->in, {
        fprintf(user_code_h, "%sconst asn1Scc%s *",
                             comma? sep: "",
                             p->type);
        comma = true;
    });

    FOREACH (p, Parameter, i->out, {
        fprintf(user_code_h,"%sconst asn1Scc%s *",
                            comma? sep: "",
                            p->type);
        comma = true;
    });
Maxime Perrotin's avatar
Maxime Perrotin committed
219
220

    fprintf(user_code_h, ");\n\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
221
222
    free(signature);
    free(sep);
Maxime Perrotin's avatar
Maxime Perrotin committed
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
}

/* Add timer declarations to the C code skeletons */
void C_Add_timers(FV *fv)
{
    if (NULL != fv->timer_list) {
        fprintf (user_code_h, "/* Timers management */\n\n");
    }
    FOREACH(timer, String, fv->timer_list, {
        fprintf(user_code_h,
            "/* This function is called when the timer expires */\n"
            "void %s_PI_%s();\n\n"
            "/* Call this function to set (enable) the timer\n"
            " * Value is in milliseconds, and must be a multiple of 100 */\n"
            "void %s_RI_SET_%s(asn1SccT_UInt32 *val);\n\n"
            "/* Call this function to reset (disable) the timer */\n"
            "void %s_RI_RESET_%s();\n\n",
            fv->name,
            timer,
            fv->name,
            timer,
            fv->name,
            timer);
        if (NULL != user_code_c) {
            fprintf(user_code_c,
              "/* This function is called when the timer \"%s\" expires */\n"
              "void %s_PI_%s()\n"
              "{\n"
              "    /* Write your code here! */\n"
              "}\n\n",
              timer,
              fv->name,
              timer);
        }
    });
}


void End_C_GW_Backend()
{
    close_c_gw_files();
}

/* Function to process one interface of the FV */
void GW_C_Interface(Interface * i)
{
    switch (i->direction) {
    case PI:
        add_PI_to_C_gw(i);
        break;

    case RI:
        /* 
         * There can be duplicate RI name but one sync, the other async
         * In that case, discard the async one (generated by VT to allow
         * a mix of sync and async PI in one block
         */
        if (asynch == i->synchronism) {
            FOREACH(interface, Interface, i->parent_fv->interfaces, {
                if (RI == interface->direction &&
                    !strcmp(interface->name, i->name) &&
                    synch == interface->synchronism) {
                    return;
                }

            });
        }
        add_RI_to_C_gw(i);
        break;
    default:
        break;
    }
}

/* External interface (the one and unique) */
void GW_C_Backend(FV * fv)
{
    if (fv->system_ast->context->onlycv)
        return;
    if ((ada == fv->language || qgenada == fv->language)
            && fv->system_ast->context->glue
            && fv->system_ast->context->polyorb_hi_c) {
        /* Only generate the header file if user code is in Ada,
         * for inclusion in POHIC headers
         * This is done to avoid a warning due to missing function declarations
         */
        Init_C_GW_Backend(fv, false);
        End_C_GW_Backend();
    }
    else if (c == fv->language || cpp == fv->language) {
        Init_C_GW_Backend(fv, true);
        FOREACH(i, Interface, fv->interfaces, {
            GW_C_Interface(i);
        });
        // If any timers, add declarations in code skeleton
        C_Add_timers(fv);

        End_C_GW_Backend();
    }
}