linux/drivers/staging/comedi/drivers/ni_routing/tools/convert_c_to_py.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
   3
   4#include <stdint.h>
   5#include <stdbool.h>
   6#include <stddef.h>
   7#include <errno.h>
   8#include <stdlib.h>
   9
  10typedef uint8_t u8;
  11typedef uint16_t u16;
  12typedef int8_t  s8;
  13#define __user
  14#define BIT(x)  (1UL << (x))
  15
  16#define NI_ROUTE_VALUE_EXTERNAL_CONVERSION 1
  17
  18#include "../ni_route_values.c"
  19#include "../ni_device_routes.c"
  20#include "all_cfiles.c"
  21
  22#include <stdio.h>
  23
  24#define RVij(rv, src, dest)     ((rv)->register_values[(dest)][(src)])
  25
  26/*
  27 * write out
  28 * {
  29 *   "family" : "<family-name>",
  30 *   "register_values": {
  31 *      <destination0>:[src0, src1, ...],
  32 *      <destination0>:[src0, src1, ...],
  33 *      ...
  34 *   }
  35 * }
  36 */
  37void family_write(const struct family_route_values *rv, FILE *fp)
  38{
  39        fprintf(fp,
  40                "  \"%s\" : {\n"
  41                "    # dest -> {src0:val0, src1:val1, ...}\n"
  42                , rv->family);
  43        for (unsigned int dest = NI_NAMES_BASE;
  44             dest < (NI_NAMES_BASE + NI_NUM_NAMES);
  45             ++dest) {
  46                unsigned int src = NI_NAMES_BASE;
  47
  48                for (; src < (NI_NAMES_BASE + NI_NUM_NAMES) &&
  49                     RVij(rv, B(src), B(dest)) == 0; ++src)
  50                        ;
  51
  52                if (src >= (NI_NAMES_BASE + NI_NUM_NAMES))
  53                        continue; /* no data here */
  54
  55                fprintf(fp, "    %u : {\n", dest);
  56                for (src = NI_NAMES_BASE; src < (NI_NAMES_BASE + NI_NUM_NAMES);
  57                     ++src) {
  58                        register_type r = RVij(rv, B(src), B(dest));
  59                        const char *M;
  60
  61                        if (r == 0) {
  62                                continue;
  63                        } else if (MARKED_V(r)) {
  64                                M = "V";
  65                        } else if (MARKED_I(r)) {
  66                                M = "I";
  67                        } else if (MARKED_U(r)) {
  68                                M = "U";
  69                        } else {
  70                                fprintf(stderr,
  71                                        "Invalid register marking %s[%u][%u] = %u\n",
  72                                        rv->family, dest, src, r);
  73                                exit(1);
  74                        }
  75
  76                        fprintf(fp, "      %u : \"%s(%u)\",\n",
  77                                src, M, UNMARK(r));
  78                }
  79                fprintf(fp, "    },\n");
  80        }
  81        fprintf(fp, "  },\n\n");
  82}
  83
  84bool is_valid_ni_sig(unsigned int sig)
  85{
  86        return (sig >= NI_NAMES_BASE) && (sig < (NI_NAMES_BASE + NI_NUM_NAMES));
  87}
  88
  89/*
  90 * write out
  91 * {
  92 *   "family" : "<family-name>",
  93 *   "register_values": {
  94 *      <destination0>:[src0, src1, ...],
  95 *      <destination0>:[src0, src1, ...],
  96 *      ...
  97 *   }
  98 * }
  99 */
 100void device_write(const struct ni_device_routes *dR, FILE *fp)
 101{
 102        fprintf(fp,
 103                "  \"%s\" : {\n"
 104                "    # dest -> [src0, src1, ...]\n"
 105                , dR->device);
 106
 107        unsigned int i = 0;
 108
 109        while (dR->routes[i].dest != 0) {
 110                if (!is_valid_ni_sig(dR->routes[i].dest)) {
 111                        fprintf(stderr,
 112                                "Invalid NI signal value [%u] for destination %s.[%u]\n",
 113                                dR->routes[i].dest, dR->device, i);
 114                        exit(1);
 115                }
 116
 117                fprintf(fp, "    %u : [", dR->routes[i].dest);
 118
 119                unsigned int j = 0;
 120
 121                while (dR->routes[i].src[j] != 0) {
 122                        if (!is_valid_ni_sig(dR->routes[i].src[j])) {
 123                                fprintf(stderr,
 124                                        "Invalid NI signal value [%u] for source %s.[%u].[%u]\n",
 125                                        dR->routes[i].src[j], dR->device, i, j);
 126                                exit(1);
 127                        }
 128
 129                        fprintf(fp, "%u,", dR->routes[i].src[j]);
 130
 131                        ++j;
 132                }
 133                fprintf(fp, "],\n");
 134
 135                ++i;
 136        }
 137        fprintf(fp, "  },\n\n");
 138}
 139
 140int main(void)
 141{
 142        FILE *fp = fopen("ni_values.py", "w");
 143
 144        /* write route register values */
 145        fprintf(fp, "ni_route_values = {\n");
 146        for (int i = 0; ni_all_route_values[i]; ++i)
 147                family_write(ni_all_route_values[i], fp);
 148        fprintf(fp, "}\n\n");
 149
 150        /* write valid device routes */
 151        fprintf(fp, "ni_device_routes = {\n");
 152        for (int i = 0; ni_device_routes_list[i]; ++i)
 153                device_write(ni_device_routes_list[i], fp);
 154        fprintf(fp, "}\n");
 155
 156        /* finish; close file */
 157        fclose(fp);
 158        return 0;
 159}
 160