nmc-utils  0.1.1
easynmc-filters.c
Go to the documentation of this file.
1 /*
2  * libEasyNMC DSP communication library.
3  * Copyright (C) 2014 RC "Module"
4  * Witten by Andrew 'Necromant' Andrianov <andrew@ncrmnt.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * ut WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21 
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/ioctl.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <getopt.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <libelf.h>
34 #include <gelf.h>
35 #include <easynmc.h>
36 
37 
38 #define dbg(fmt, ...) if (g_libeasynmc_debug) { \
39  fprintf(stderr, "libeasynmc: " fmt, ##__VA_ARGS__); \
40  }
41 
42 #define err(fmt, ...) if (g_libeasynmc_errors) { \
43  fprintf(stderr, "libeasynmc: " fmt, ##__VA_ARGS__); \
44  }
45 
46 static int stdio_handle_section(struct easynmc_handle *h, char* name, FILE *rfd, GElf_Shdr shdr)
47 {
48  int type = -1;
49 
50  if (strcmp(name, ".easynmc_stdin")==0)
51  type = 0;
52  else if (strcmp(name, ".easynmc_stdout")==0)
53  type = 1;
54 
55  if (shdr.sh_size == 0)
56  return 0; /* If section optimized out - only name remains */
57 
58  if (-1 == type)
59  return 0;
60 
61 
62  int rq = (type) ? IOCTL_NMC3_ATTACH_STDOUT : IOCTL_NMC3_ATTACH_STDIN;
63  uint32_t addr = shdr.sh_addr << 2;
64  dbg("Attaching %s io buffer size %d words\n", name, h->imem32[shdr.sh_addr + 1]);
65 
66  int ret = ioctl(h->iofd, rq, &addr);
67  if (ret != 0) {
68  perror("ioctl");
69  return 0;
70  }
71 
72  rq = (type) ? IOCTL_NMC3_REFORMAT_STDOUT : IOCTL_NMC3_REFORMAT_STDIN;
73  uint32_t rfmt = 1; /* reformat stdio by default */
74 
75  ret = ioctl(h->iofd, rq, &rfmt);
76  if (ret != 0) {
77  perror("ioctl");
78  return 0;
79  }
80 
81  return 1; /* Handled! */
82 }
83 
84 static struct easynmc_section_filter stdio_filter = {
85  .name = "stdio",
86  .handle_section = stdio_handle_section
87 };
88 
89 
90 static int arg_handle_section(struct easynmc_handle *h, char* name, FILE *rfd, GElf_Shdr shdr)
91 {
92  if (strcmp(name, ".easynmc_args")!=0)
93  return 0;
94 
95  if (shdr.sh_size == 0)
96  return 0; /* If section optimized out - only name remains */
97 
98  h->argoffset = shdr.sh_addr;
99  h->argdatalen = shdr.sh_size - 2;
100 
101  dbg("Arguments @0x%x size %d words\n", h->argoffset, h->argdatalen);
102  return 1; /* Handled! */
103 }
104 
105 static struct easynmc_section_filter arg_filter = {
106  .name = "args",
107  .handle_section = arg_handle_section
108 };
109 
110 
111 
113 {
114  easynmc_register_section_filter(h, &stdio_filter);
115  easynmc_register_section_filter(h, &arg_filter);
116 }