Indent
[bcusdk.git] / eibd / bcu / bcuread.cpp
blob9c3dbd58a016e71a783d1129ea56812939ff96c2
1 /*
2 EIBD eib bus access and management daemon
3 Copyright (C) 2005-2007 Martin Kögler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <argp.h>
24 #include "addrtab.h"
25 #include "lowlevelconf.h"
27 /** aborts program with a printf like message */
28 void
29 die (const char *msg, ...)
31 va_list ap;
32 va_start (ap, msg);
33 vprintf (msg, ap);
34 printf ("\n");
35 va_end (ap);
37 exit (1);
40 /** structure to store low level backends */
41 struct urldef
43 /** URL-prefix */
44 const char *prefix;
45 /** factory function */
46 LowLevel_Create_Func Create;
49 /** list of URLs */
50 struct urldef URLs[] = {
51 #undef L2_NAME
52 #define L2_NAME(a) { a##_PREFIX, a##_CREATE },
53 #include "lowlevelcreate.h"
54 {0, 0}
57 /** determines the right backend for the url and creates it */
58 LowLevelDriverInterface *
59 Create (const char *url, Trace * t)
61 unsigned int p = 0;
62 struct urldef *u = URLs;
63 while (url[p] && url[p] != ':')
64 p++;
65 if (url[p] != ':')
66 die ("not a valid url");
67 while (u->prefix)
69 if (strlen (u->prefix) == p && !memcmp (u->prefix, url, p))
71 return u->Create (url + p + 1, t);
73 u++;
75 die ("url not supported");
76 return 0;
79 /** version */
80 const char *argp_program_version = "bcuread " VERSION;
81 /** documentation */
82 static char doc[] =
83 "bcuread -- read BCU memory\n"
84 "(C) 2005-2007 Martin Kögler <mkoegler@auto.tuwien.ac.at>\n"
85 "supported URLs are:\n"
86 #undef L2_NAME
87 #define L2_NAME(a) a##_URL
88 #include "lowlevelcreate.h"
89 "\n"
90 #undef L2_NAME
91 #define L2_NAME(a) a##_DOC
92 #include "lowlevelcreate.h"
93 "\n";
95 /** structure to store the arguments */
96 struct arguments
98 /** trace level */
99 int tracelevel;
101 /** storage for the arguments*/
102 struct arguments arg;
104 unsigned
105 readHex (const char *addr)
107 int i;
108 sscanf (addr, "%x", &i);
109 return i;
112 /** documentation for arguments*/
113 static char args_doc[] = "URL addr len";
115 /** option list */
116 static struct argp_option options[] = {
118 {"trace", 't', "LEVEL", 0, "set trace level"},
123 /** parses and stores an option */
124 static error_t
125 parse_opt (int key, char *arg, struct argp_state *state)
127 struct arguments *arguments = (struct arguments *) state->input;
128 switch (key)
130 case 't':
131 arguments->tracelevel = (arg ? atoi (arg) : 0);
132 break;
133 default:
134 return ARGP_ERR_UNKNOWN;
136 return 0;
139 /** information for the argument parser*/
140 static struct argp argp = { options, parse_opt, args_doc, doc };
144 main (int ac, char *ag[])
146 int addr;
147 int len;
148 int index;
149 CArray result;
150 LowLevelDriverInterface *iface = 0;
151 memset (&arg, 0, sizeof (arg));
153 argp_parse (&argp, ac, ag, 0, &index, &arg);
154 if (index > ac - 3)
155 die ("more parameter expected");
156 if (index < ac - 3)
157 die ("unexpected parameter");
159 signal (SIGPIPE, SIG_IGN);
160 pth_init ();
162 Trace t;
163 t.SetTraceLevel (arg.tracelevel);
167 iface = Create (ag[index], &t);
169 catch (Exception e)
171 die ("initialisation failed");
174 addr = readHex (ag[index + 1]);
175 len = atoi (ag[index + 2]);
177 int res = readEMIMem (iface, addr, len, result);
178 if (!res)
180 printf ("Read failed");
182 else
184 for (int i = 0; i < result (); i++)
185 printf ("%02x ", result[i]);
186 printf ("\n");
189 delete iface;
191 pth_exit (0);
192 return 0;