build_micropython_skeletons.c 7.58 KB
Newer Older
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
/* Buildsupport is (c) 2008-2016 European Space Agency
 * 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.

  updated 10/06/2016: properly align params and use FOREACH
  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_py = NULL;

static bool gen_this_ri(Interface * i)
{
    /* 
     * 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 false;
            }

        });
    }

    return true;
}

/* Import the RI into the module-level namespace of the Python script */
void import_RI_to_MicroPython_skel(Interface * i)
{
    if (NULL == user_code_py)
        return;

    bool comma = false;

    fprintf(user_code_py, "# %s_RI_%s(", i->parent_fv->name, i->name);

    FOREACH (p, Parameter, i->in, {
        fprintf(user_code_py, "%sIN_%s: asn1Scc%s",
59
            comma ? ", " : "", p->name, p->type);
60 61 62 63 64
        comma = true;
    });

    FOREACH (p, Parameter, i->out, {
        fprintf(user_code_py,"%sOUT_%s: asn1Scc%s",
65
            comma ? ", " : "", p->name, p->type);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        comma = true;
    });

    fprintf(user_code_py, ")\n");

    fprintf(user_code_py, "from taste import %s_RI_%s\n", i->parent_fv->name, i->name);
}

/* Generate the preamble of the skeleton script */
void micropython_skel_preamble(FV * fv)
{
    int hasparam = 0;

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

    /* c. user_code.py preamble (if applicable) */
    if (NULL != user_code_py) {
        fprintf(user_code_py,
                "# User code: This file will not be overwritten by TASTE.\n\n");
88 89
        fprintf(user_code_py, "import micropython\n");
        fprintf(user_code_py, "from taste import * # import all ASN types\n\n");
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

        FOREACH(i, Interface, fv->interfaces, {
            if (i->direction == RI && gen_this_ri(i)) {
                import_RI_to_MicroPython_skel(i);
            }
        });
        fprintf(user_code_py, "\n");

        // TODO need to generate dummy variables for the types

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

        fprintf(user_code_py, "def %s_startup():\n", fv->name);

        fprintf(user_code_py,
                "    # Write your initialization code here,\n"
                "    # but do not make any call to a required interface.\n"
115
                "    # You can construct ASN.1 instances using the type name as a function call.\n"
116
                "    # You can construct an object reference using Access(...).\n"
117 118 119 120
                "    # It's recommended to lock the heap once initialisation is done, but\n"
                "    # note that without the heap some Python operations are not possible.\n"
                "    micropython.heap_lock()\n"
                "\n");
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
    }
}

/* Creates user_code.h, and if necessary user_code.c (if it did not exist) */
int Init_MicroPython_GW_Backend(FV * fv)
{
    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"));

    filename[strlen(filename) - 1] = 'p';       /* change from .c to .p */
    build_string(&filename, "y", 1);            /* change from .p to .py */
    if (!file_exists(path, filename)) {
        printf("MicroPython: creating fresh skeleton %s/%s\n", path, filename);
        create_file(path, filename, &user_code_py);
    }

    free(path);

    micropython_skel_preamble(fv);

    return 0;
}


void close_micropython_skel_files()
{
    close_file(&user_code_py);
}

/* 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_MicroPython_skel(Interface * i)
{
    if (NULL == user_code_py)
        return;

    char *signature_py = make_string("def %s_PI_%s(",
                                     i->parent_fv->name,
                                     i->name);
    bool comma = false;

    fprintf(user_code_py, "%s", signature_py);

174 175
#if 1
    // This code generates type hints for the parameters
176
    FOREACH (p, Parameter, i->in, {
177 178
        fprintf(user_code_py, "%sIN_%s: asn1Scc%s",
            comma ? ", " : "", p->name, p->type);
179 180 181 182
        comma = true;
    });

    FOREACH (p, Parameter, i->out, {
183 184
        fprintf(user_code_py, "%sOUT_%s: asn1Scc%s",
            comma ? ", " : "", p->name, p->type);
185 186 187 188 189
        comma = true;
    });
#else
    // This code generates the parameter list without type hints.
    FOREACH (p, Parameter, i->in, {
190
        fprintf(user_code_py, "%sIN_%s", comma ? ", " : "", p->name);
191 192 193 194
        comma = true;
    });

    FOREACH (p, Parameter, i->out, {
195
        fprintf(user_code_py, "%sOUT_%s", comma ? ", " : "", p->name);
196 197 198 199
        comma = true;
    });
#endif

200 201 202 203 204 205 206 207 208 209 210 211 212
    fprintf(user_code_py,
        "):\n"
        "    # Write your code here!\n");

    /* Add helpful comment for IN parameters */
    if (i->in != NULL) {
        fprintf(user_code_py,
            "    # For IN parameters you can reference the value's members using . and [] notation\n");
    }

    /* Add helpful comment for OUT parameters */
    if (i->out != NULL) {
        fprintf(user_code_py,
213
            "    # For OUT parameters you must use the .all member to access the value, ie: ");
214 215
        comma = false;
        FOREACH (p, Parameter, i->out, {
216
            fprintf(user_code_py, "%sOUT_%s.all", comma ? ", " : "", p->name);
217 218 219 220 221 222
            comma = true;
        });
        fprintf(user_code_py, "\n");
    }

    fprintf(user_code_py, "    pass\n\n");
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

    free(signature_py);
}

/* Add timer declarations to the C code skeletons */
void MicroPython_Add_timers(FV *fv)
{
    (void)fv;
}


void End_MicroPython_GW_Backend()
{
    close_micropython_skel_files();
}

/* External interface (the one and unique) */
void GW_MicroPython_Backend(FV * fv)
{
    if (get_context()->onlycv)
        return;
    if (micropython == fv->language) {
        Init_MicroPython_GW_Backend(fv);
        FOREACH(i, Interface, fv->interfaces, {
            if (i->direction == PI) {
                add_PI_to_MicroPython_skel(i);
            }
        });
        // If any timers, add declarations in code skeleton
        MicroPython_Add_timers(fv);

        End_MicroPython_GW_Backend();
    }
}