Debian Codegraph @

examples/plugctl.c

raw
1 /* plugctl.c
2 * A program to get or set any MPR or PCR register value.
3 * Copyright 2002-2004 Dan Dennedy
4 *
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.
9 *
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 Library General Public License for more details.
14 *
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.
18 */
19
20 /* standard system includes */
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <strings.h>
26 #include <errno.h>
27
28 /* linux1394 includes */
29 #include <libraw1394/raw1394.h>
30
31 #include "../src/iec61883.h"
32
33 /************ DECLARE PRIVATE INTERFACE BITS OF LIBIEC61883!! **************/
34
35 /* standard CSR offsets for plugs */
36 #define CSR_O_MPR 0x900
37 #define CSR_O_PCR_0 0x904
38 #define CSR_O_PCR_1 0x908
39 #define CSR_O_PCR_2 0x90C
40 #define CSR_O_PCR_3 0x910
41
42 #define CSR_I_MPR 0x980
43 #define CSR_I_PCR_0 0x984
44 #define CSR_I_PCR_1 0x988
45 #define CSR_I_PCR_2 0x98C
46 #define CSR_I_PCR_3 0x990
47
48 #if ( __BYTE_ORDER == __BIG_ENDIAN )
49
50 struct iec61883_oMPR {
51 unsigned int data_rate:2;
52 unsigned int bcast_channel:6;
53 unsigned int non_persist_ext:8;
54 unsigned int persist_ext:8;
55 unsigned int reserved:3;
56 unsigned int n_plugs:5;
57 };
58
59 struct iec61883_iMPR {
60 unsigned int data_rate:2;
61 unsigned int reserved:6;
62 unsigned int non_persist_ext:8;
63 unsigned int persist_ext:8;
64 unsigned int reserved2:3;
65 unsigned int n_plugs:5;
66 };
67
68 struct iec61883_oPCR {
69 unsigned int online:1;
70 unsigned int bcast_connection:1;
71 unsigned int n_p2p_connections:6;
72 unsigned int reserved:2;
73 unsigned int channel:6;
74 unsigned int data_rate:2;
75 unsigned int overhead_id:4;
76 unsigned int payload:10;
77 };
78
79 struct iec61883_iPCR {
80 unsigned int online:1;
81 unsigned int bcast_connection:1;
82 unsigned int n_p2p_connections:6;
83 unsigned int reserved:2;
84 unsigned int channel:6;
85 unsigned int reserved2:16;
86 };
87
88 #else
89
90 struct iec61883_oMPR {
91 unsigned int n_plugs:5;
92 unsigned int reserved:3;
93 unsigned int persist_ext:8;
94 unsigned int non_persist_ext:8;
95 unsigned int bcast_channel:6;
96 unsigned int data_rate:2;
97 };
98
99 struct iec61883_iMPR {
100 unsigned int n_plugs:5;
101 unsigned int reserved2:3;
102 unsigned int persist_ext:8;
103 unsigned int non_persist_ext:8;
104 unsigned int reserved:6;
105 unsigned int data_rate:2;
106 };
107
108 struct iec61883_oPCR {
109 unsigned int payload:10;
110 unsigned int overhead_id:4;
111 unsigned int data_rate:2;
112 unsigned int channel:6;
113 unsigned int reserved:2;
114 unsigned int n_p2p_connections:6;
115 unsigned int bcast_connection:1;
116 unsigned int online:1;
117 };
118
119 struct iec61883_iPCR {
120 unsigned int reserved2:16;
121 unsigned int channel:6;
122 unsigned int reserved:2;
123 unsigned int n_p2p_connections:6;
124 unsigned int bcast_connection:1;
125 unsigned int online:1;
126 };
127
128 #endif
129
130 /**
131 * iec61883_plug_get - Read a node's plug register.
132 * @h: A raw1394 handle.
133 * @n: The node id of the node to read
134 * @a: The CSR offset address (relative to base) of the register to read.
135 * @value: A pointer to a quadlet where the plug register's value will be stored.
136 *
137 * This function handles bus to host endian conversion. It returns 0 for
138 * suceess or -1 for error (errno available).
139 **/
140 extern int
141 iec61883_plug_get(raw1394handle_t h, nodeid_t n, nodeaddr_t a, quadlet_t *value);
142
143
144 /**
145 * iec61883_plug_set - Write a node's plug register.
146 * @h: A raw1394 handle.
147 * @n: The node id of the node to read
148 * @a: The CSR offset address (relative to CSR base) of the register to write.
149 * @value: A quadlet containing the new register value.
150 *
151 * This uses a compare/swap lock operation to safely write the
152 * new register value, as required by IEC 61883-1.
153 * This function handles host to bus endian conversion. It returns 0 for success
154 * or -1 for error (errno available).
155 **/
156 extern int
157 iec61883_plug_set(raw1394handle_t h, nodeid_t n, nodeaddr_t a, quadlet_t value);
158
159 /**
160 * High level plug access macros
161 */
162
163 #define iec61883_get_oMPR(h,n,v) iec61883_plug_get((h), (n), CSR_O_MPR, (quadlet_t *)(v))
164 #define iec61883_set_oMPR(h,n,v) iec61883_plug_set((h), (n), CSR_O_MPR, *((quadlet_t *)&(v)))
165 #define iec61883_get_oPCR0(h,n,v) iec61883_plug_get((h), (n), CSR_O_PCR_0, (quadlet_t *)(v))
166 #define iec61883_set_oPCR0(h,n,v) iec61883_plug_set((h), (n), CSR_O_PCR_0, *((quadlet_t *)&(v)))
167 #define iec61883_get_oPCRX(h,n,v,x) iec61883_plug_get((h), (n), CSR_O_PCR_0+(4*(x)), (quadlet_t *)(v))
168 #define iec61883_set_oPCRX(h,n,v,x) iec61883_plug_set((h), (n), CSR_O_PCR_0+(4*(x)), *((quadlet_t *)&(v)))
169 #define iec61883_get_iMPR(h,n,v) iec61883_plug_get((h), (n), CSR_I_MPR, (quadlet_t *)(v))
170 #define iec61883_set_iMPR(h,n,v) iec61883_plug_set((h), (n), CSR_I_MPR, *((quadlet_t *)&(v)))
171 #define iec61883_get_iPCR0(h,n,v) iec61883_plug_get((h), (n), CSR_I_PCR_0, (quadlet_t *)(v))
172 #define iec61883_set_iPCR0(h,n,v) iec61883_plug_set((h), (n), CSR_I_PCR_0, *((quadlet_t *)&(v)))
173 #define iec61883_get_iPCRX(h,n,v,x) iec61883_plug_get((h), (n), CSR_I_PCR_0+(4*(x)), (quadlet_t *)(v))
174 #define iec61883_set_iPCRX(h,n,v,x) iec61883_plug_set((h), (n), CSR_I_PCR_0+(4*(x)), *((quadlet_t *)&(v)))
175
176 /************ END PRIVATE INTERFACE **************/
177
178 #define FAIL(s) {fprintf(stderr, "libiec61883 error: %s\n", s);}
179
180 static void usage()
181 {
182 printf("plugctl: [-p port}] [-n node] <plug>.<attribute>[=<value>]\n");
183 printf("The equal sign determines whether you want to get or set the value\n");
184 printf("of a plug's attribute.\n");
185 printf("<plug> is one of oMPR, iMPR, oPCR[n], or iPCR[n] (case insensitive).\n");
186 printf("Please supply a numerical index for [n]!\n");
187 }
188
189 int main(int argc, const char** argv)
190 {
191 raw1394handle_t handle;
192 struct iec61883_oMPR o_mpr;
193 struct iec61883_oPCR o_pcr;
194 struct iec61883_iMPR i_mpr;
195 struct iec61883_iPCR i_pcr;
196 int result = 0;
197 int port = 0;
198 int node = 0xffc0;
199 int is_got_ompr = 0, is_got_opcr[32], is_got_impr = 0, is_got_ipcr[32];
200 int is_set_ompr = 0, is_set_opcr[32], is_set_impr = 0, is_set_ipcr[32];
201 int i;
202
203 if (argc == 1) {
204 usage();
205 return 0;
206 }
207
208 for (i = 0; i < 32; i++) {
209 is_got_opcr[i] = is_got_ipcr[i] = 0;
210 is_set_opcr[i] = is_set_ipcr[i] = 0;
211 }
212
213 for (i = 1; i < argc; i++)
214 {
215 if (strcmp (argv[i], "-p") == 0)
216 port = atoi (argv[++i]);
217 }
218
219 if (!(handle = raw1394_new_handle_on_port(port))) {
220 perror("raw1394 - couldn't get handle");
221 printf("This error usually means that the raw1394 driver is not loaded or that /dev/raw1394 does not exist.\n");
222 return EFAULT;
223 }
224
225 for (i = 1; i < argc && result == 0; i++)
226 {
227 if (strcmp (argv[i], "-n") == 0) {
228 node |= atoi (argv[++i]);
229 } else if (strncmp (argv[i], "-h", 2) == 0) {
230 usage();
231 return 0;
232
233 } else {
234 char *plug = strdup (argv[i]);
235 char *attribute = strchr (plug, '.');
236
237 if (attribute != NULL) {
238 char *valuestr = strchr (attribute, '=');
239 *attribute++ = '\0';
240
241 if (valuestr != NULL) {
242 /* set */
243 int value;
244 *valuestr++ = '\0';
245 value = atoi (valuestr);
246
247 if (strcasecmp (plug, "ompr") == 0) {
248 if (!is_got_ompr) {
249 result = iec61883_get_oMPR (handle, node, &o_mpr);
250 if (result < 0)
251 continue;
252 is_got_ompr = 1;
253 }
254 is_set_ompr = 1;
255 if (strcasecmp (attribute, "data_rate") == 0) {
256 o_mpr.data_rate = value;
257 } else if (strcasecmp (attribute, "bcast_channel") == 0) {
258 o_mpr.bcast_channel = value;
259 } else if (strcasecmp (attribute, "non_persist_ext") == 0) {
260 o_mpr.non_persist_ext = value;
261 } else if (strcasecmp (attribute, "persist_ext") == 0) {
262 o_mpr.persist_ext = value;
263 } else if (strcasecmp (attribute, "reserved") == 0) {
264 o_mpr.reserved = value;
265 } else if (strcasecmp (attribute, "n_plugs") == 0) {
266 o_mpr.n_plugs = value;
267 } else {
268 result = EINVAL;
269 continue;
270 }
271
272 } else if (strcasecmp (plug, "impr") == 0) {
273 if (!is_got_impr) {
274 result = iec61883_get_iMPR (handle, node, &i_mpr);
275 if (result < 0)
276 continue;
277 is_got_impr = 1;
278 }
279 is_set_impr = 1;
280 if (strcasecmp (attribute, "data_rate") == 0) {
281 i_mpr.data_rate = value;
282 } else if (strcasecmp (attribute, "reserved") == 0) {
283 i_mpr.reserved = value;
284 } else if (strcasecmp (attribute, "non_persist_ext") == 0) {
285 i_mpr.non_persist_ext = value;
286 } else if (strcasecmp (attribute, "persist_ext") == 0) {
287 i_mpr.persist_ext = value;
288 } else if (strcasecmp (attribute, "reserved2") == 0) {
289 i_mpr.reserved2 = value;
290 } else if (strcasecmp (attribute, "n_plugs") == 0) {
291 i_mpr.n_plugs = value;
292 } else {
293 result = EINVAL;
294 continue;
295 }
296
297 } else if (strncasecmp (plug, "opcr[", 5) == 0) {
298 int idx = atoi (plug + 5);
299 if (!is_got_opcr[idx]) {
300 result = iec61883_get_oPCRX (handle, node, &o_pcr, idx);
301 if (result < 0)
302 continue;
303 is_got_opcr[idx] = 1;
304 }
305 is_set_opcr[idx] = 1;
306 if (strcasecmp (attribute, "online") == 0) {
307 o_pcr.online = value;
308 } else if (strcasecmp (attribute, "bcast_connection") == 0) {
309 o_pcr.bcast_connection = value;
310 } else if (strcasecmp (attribute, "n_p2p_connections") == 0) {
311 o_pcr.n_p2p_connections = value;
312 } else if (strcasecmp (attribute, "reserved") == 0) {
313 o_pcr.reserved = value;
314 } else if (strcasecmp (attribute, "channel") == 0) {
315 o_pcr.channel = value;
316 } else if (strcasecmp (attribute, "data_rate") == 0) {
317 o_pcr.data_rate = value;
318 } else if (strcasecmp (attribute, "overhead_id") == 0) {
319 o_pcr.overhead_id = value;
320 } else if (strcasecmp (attribute, "payload") == 0) {
321 o_pcr.payload = value;
322 } else {
323 result = EINVAL;
324 continue;
325 }
326
327 } else if (strncasecmp (plug, "ipcr[", 5) == 0) {
328 int idx = atoi (plug + 5);
329 if (!is_got_ipcr[idx]) {
330 result = iec61883_get_iPCRX (handle, node, &i_pcr, idx);
331 if (result < 0)
332 continue;
333 is_got_ipcr[idx] = 1;
334 }
335 is_set_ipcr[idx] = 1;
336 if (strcasecmp (attribute, "online") == 0) {
337 i_pcr.online = value;
338 } else if (strcasecmp (attribute, "bcast_connection") == 0) {
339 i_pcr.bcast_connection = value;
340 } else if (strcasecmp (attribute, "n_p2p_connections") == 0) {
341 i_pcr.n_p2p_connections = value;
342 } else if (strcasecmp (attribute, "reserved") == 0) {
343 i_pcr.reserved = value;
344 } else if (strcasecmp (attribute, "channel") == 0) {
345 i_pcr.channel = value;
346 } else if (strcasecmp (attribute, "reserved2") == 0) {
347 i_pcr.reserved2 = value;
348 } else {
349 result = EINVAL;
350 continue;
351 }
352
353 } else {
354 result = EINVAL;
355 continue;
356 }
357 } else {
358 /* get */
359 if (strcasecmp (plug, "ompr") == 0) {
360 result = iec61883_get_oMPR (handle, node, &o_mpr);
361 if (result < 0)
362 continue;
363 if (strcasecmp (attribute, "data_rate") == 0) {
364 printf ("%d\n", o_mpr.data_rate);
365 } else if (strcasecmp (attribute, "bcast_channel") == 0) {
366 printf ("%d\n", o_mpr.bcast_channel);
367 } else if (strcasecmp (attribute, "non_persist_ext") == 0) {
368 printf ("%d\n", o_mpr.non_persist_ext);
369 } else if (strcasecmp (attribute, "persist_ext") == 0) {
370 printf ("%d\n", o_mpr.persist_ext);
371 } else if (strcasecmp (attribute, "reserved") == 0) {
372 printf ("%d\n", o_mpr.reserved);
373 } else if (strcasecmp (attribute, "n_plugs") == 0) {
374 printf ("%d\n", o_mpr.n_plugs);
375 } else {
376 result = EINVAL;
377 continue;
378 }
379
380 } else if (strcasecmp (plug, "impr") == 0) {
381 result = iec61883_get_iMPR (handle, node, &i_mpr);
382 if (result < 0)
383 continue;
384 if (strcasecmp (attribute, "data_rate") == 0) {
385 printf ("%d\n", i_mpr.data_rate);
386 } else if (strcasecmp (attribute, "reserved") == 0) {
387 printf ("%d\n", i_mpr.reserved);
388 } else if (strcasecmp (attribute, "non_persist_ext") == 0) {
389 printf ("%d\n", i_mpr.non_persist_ext);
390 } else if (strcasecmp (attribute, "persist_ext") == 0) {
391 printf ("%d\n", i_mpr.persist_ext);
392 } else if (strcasecmp (attribute, "reserved2") == 0) {
393 printf ("%d\n", i_mpr.reserved2);
394 } else if (strcasecmp (attribute, "n_plugs") == 0) {
395 printf ("%d\n", i_mpr.n_plugs);
396 } else {
397 result = EINVAL;
398 continue;
399 }
400
401 } else if (strncasecmp (plug, "opcr[", 5) == 0) {
402 int idx = atoi (plug + 5);
403 result = iec61883_get_oPCRX (handle, node, &o_pcr, idx);
404 if (result < 0)
405 continue;
406 if (strcasecmp (attribute, "online") == 0) {
407 printf ("%d\n", o_pcr.online);
408 } else if (strcasecmp (attribute, "bcast_connection") == 0) {
409 printf ("%d\n", o_pcr.bcast_connection);
410 } else if (strcasecmp (attribute, "n_p2p_connections") == 0) {
411 printf ("%d\n", o_pcr.n_p2p_connections);
412 } else if (strcasecmp (attribute, "reserved") == 0) {
413 printf ("%d\n", o_pcr.reserved);
414 } else if (strcasecmp (attribute, "channel") == 0) {
415 printf ("%d\n", o_pcr.channel);
416 } else if (strcasecmp (attribute, "data_rate") == 0) {
417 printf ("%d\n", o_pcr.data_rate);
418 } else if (strcasecmp (attribute, "overhead_id") == 0) {
419 printf ("%d\n", o_pcr.overhead_id);
420 } else if (strcasecmp (attribute, "payload") == 0) {
421 printf ("%d\n", o_pcr.payload);
422 } else {
423 result = EINVAL;
424 continue;
425 }
426
427 } else if (strncasecmp (plug, "ipcr[", 5) == 0) {
428 int idx = atoi (plug + 5);
429 result = iec61883_get_iPCRX (handle, node, &i_pcr, idx);
430 if (result < 0)
431 continue;
432 if (strcasecmp (attribute, "online") == 0) {
433 printf ("%d\n", i_pcr.online);
434 } else if (strcasecmp (attribute, "bcast_connection") == 0) {
435 printf ("%d\n", i_pcr.bcast_connection);
436 } else if (strcasecmp (attribute, "n_p2p_connections") == 0) {
437 printf ("%d\n", i_pcr.n_p2p_connections);
438 } else if (strcasecmp (attribute, "reserved") == 0) {
439 printf ("%d\n", i_pcr.reserved);
440 } else if (strcasecmp (attribute, "channel") == 0) {
441 printf ("%d\n", i_pcr.channel);
442 } else if (strcasecmp (attribute, "reserved2") == 0) {
443 printf ("%d\n", i_pcr.reserved2);
444 } else {
445 result = EINVAL;
446 continue;
447 }
448
449 } else {
450 result = EINVAL;
451 continue;
452 }
453 }
454 }
455 free (plug);
456 }
457 }
458 if (result == 0 && is_set_ompr)
459 result = iec61883_set_oMPR (handle, node, o_mpr);
460 if (result == 0 && is_set_impr)
461 result = iec61883_set_iMPR (handle, node, i_mpr);
462 for (i = 0; result == 0 && i < 32; i++) {
463 if (is_set_opcr[i])
464 result = iec61883_set_oPCRX (handle, node, o_pcr, i);
465 if (is_set_ipcr[i])
466 result = iec61883_set_iPCRX (handle, node, i_pcr, i);
467 }
468 raw1394_destroy_handle(handle);
469
470 return result;
471 }