root/trunk/libffado/tests/test-avccmd.cpp

Revision 627, 6.9 kB (checked in by ppalmers, 17 years ago)

- add test application to send a custom AV/C command to a node
- add test application to examine focusrite vendordep cmds

Line 
1 /*
2  * Copyright (C) 2007 by Pieter Palmers
3  * Copyright (C) 2005-2007 by Daniel Wagner
4  *
5  * This file is part of FFADO
6  * FFADO = Free Firewire (pro-)audio drivers for linux
7  *
8  * FFADO is based upon FreeBoB
9  *
10  * FFADO is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * FFADO is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with FFADO; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA.
23  *
24  */
25
26 #include <libraw1394/raw1394.h>
27 #include <libiec61883/iec61883.h>
28
29 #include "debugmodule/debugmodule.h"
30
31 #include "libieee1394/configrom.h"
32 #include "libieee1394/ieee1394service.h"
33 #include "libutil/cmd_serialize.h"
34 #include "libavc/general/avc_generic.h"
35
36 #include <argp.h>
37 #include <stdlib.h>
38 #include <iostream>
39
40 using namespace std;
41 using namespace AVC;
42 using namespace Util;
43
44 DECLARE_GLOBAL_DEBUG_MODULE;
45
46 #define MAX_ARGS 1000
47
48 class TestCmd: public AVCCommand
49 {
50 public:
51     TestCmd(Ieee1394Service& ieee1394service, opcode_t opcode );
52     virtual ~TestCmd();
53
54     virtual bool serialize( Util::IOSSerialize& se );
55     virtual bool deserialize( Util::IISDeserialize& de );
56
57     virtual const char* getCmdName() const
58     { return "TestCmd"; }
59    
60     byte_t args[MAX_ARGS];
61     int nargs;
62 };
63
64 ////////////////////////////////////////////////
65 // arg parsing
66 ////////////////////////////////////////////////
67 const char *argp_program_version = "test-avccmd 0.1";
68 const char *argp_program_bug_address = "<ffado-devel@lists.sf.net>";
69 static char doc[] = "test-avccmd -- test program to send a custom avc command to a specific node.";
70 static char args_doc[] = "NODE_ID [avc cmd byte sequence]";
71 static struct argp_option options[] = {
72     {"verbose",   'v', 0,           0,  "Produce verbose output" },
73     {"port",      'p', "PORT",      0,  "Set port" },
74     {"node",      'n', "NODE",      0,  "Set node" },
75    { 0 }
76 };
77
78 struct arguments
79 {
80     arguments()
81         : nargs ( 0 )
82         , verbose( false )
83         , test( false )
84         , port( 0 )
85         {
86             args[0] = 0;
87         }
88
89     char* args[MAX_ARGS];
90     int   nargs;
91     bool  verbose;
92     bool  test;
93     int   port;
94     int   node;
95 } arguments;
96
97 // Parse a single option.
98 static error_t
99 parse_opt( int key, char* arg, struct argp_state* state )
100 {
101     // Get the input argument from `argp_parse', which we
102     // know is a pointer to our arguments structure.
103     struct arguments* arguments = ( struct arguments* ) state->input;
104
105     char* tail;
106     switch (key) {
107     case 'v':
108         arguments->verbose = true;
109         break;
110     case 't':
111         arguments->test = true;
112         break;
113     case 'p':
114         errno = 0;
115         arguments->port = strtol(arg, &tail, 0);
116         if (errno) {
117             perror("argument parsing failed:");
118             return errno;
119         }
120         break;
121     case 'n':
122         errno = 0;
123         arguments->node = strtol(arg, &tail, 0);
124         if (errno) {
125             perror("argument parsing failed:");
126             return errno;
127         }
128         break;
129     case ARGP_KEY_ARG:
130         if (state->arg_num >= MAX_ARGS) {
131             // Too many arguments.
132             argp_usage (state);
133         }
134         arguments->args[state->arg_num] = arg;
135         arguments->nargs++;
136         break;
137     case ARGP_KEY_END:
138         if(arguments->nargs<4) {
139             printf("not enough arguments\n");
140             return -1;
141         }
142        
143         break;
144     default:
145         return ARGP_ERR_UNKNOWN;
146     }
147     return 0;
148 }
149
150 static struct argp argp = { options, parse_opt, args_doc, doc };
151
152 ///////////////////////////
153 // main
154 //////////////////////////
155 int
156 main(int argc, char **argv)
157 {
158     // arg parsing
159     if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) {
160         fprintf( stderr, "Could not parse command line\n" );
161         exit(-1);
162     }
163     errno = 0;
164     char* tail;
165
166
167     Ieee1394Service *m_1394Service = new Ieee1394Service();
168     if ( !m_1394Service ) {
169         debugFatal( "Could not create Ieee1349Service object\n" );
170         return false;
171     }
172
173     if ( !m_1394Service->initialize( arguments.port ) ) {
174         debugFatal( "Could not initialize Ieee1349Service object\n" );
175         delete m_1394Service;
176         m_1394Service = 0;
177         return false;
178     }
179
180     char cmdtype = strtol(arguments.args[0], &tail, 0);
181     if (errno) {
182         perror("argument parsing failed:");
183         return -1;
184     }
185     char unit_subunit = strtol(arguments.args[1], &tail, 0);
186     if (errno) {
187         perror("argument parsing failed:");
188         return -1;
189     }
190     opcode_t opcode = strtol(arguments.args[2], &tail, 0);
191     if (errno) {
192         perror("argument parsing failed:");
193         return -1;
194     }
195    
196     TestCmd cmd( *m_1394Service, opcode );
197     switch (cmdtype) {
198         case AVC::AVCCommand::eCT_Control: cmd.setCommandType( AVC::AVCCommand::eCT_Control ); break;
199         case AVC::AVCCommand::eCT_Status: cmd.setCommandType( AVC::AVCCommand::eCT_Status ); break;
200         case AVC::AVCCommand::eCT_SpecificInquiry: cmd.setCommandType( AVC::AVCCommand::eCT_SpecificInquiry ); break;
201         case AVC::AVCCommand::eCT_Notify: cmd.setCommandType( AVC::AVCCommand::eCT_Notify ); break;
202         case AVC::AVCCommand::eCT_GeneralInquiry: cmd.setCommandType( AVC::AVCCommand::eCT_GeneralInquiry ); break;
203         default: printf("Invalid command type: 0x%02X.\n", cmdtype); exit(-1);
204     }
205     cmd.setNodeId( arguments.node );
206    
207     cmd.setSubunitType( byteToSubunitType(unit_subunit >> 3) );
208     cmd.setSubunitId( (unit_subunit & 0x7) );
209    
210     cmd.setVerbose( DEBUG_LEVEL_VERY_VERBOSE );
211    
212     int i=0;
213
214     for (;i<arguments.nargs-3;i++) {
215         char tmp = strtol(arguments.args[i+3], &tail, 0);
216         if (errno) {
217             perror("argument parsing failed:");
218             break;
219         }
220         cmd.args[i]=tmp;
221     }
222
223     cmd.nargs=i;
224
225     if ( !cmd.fire() ) {
226         debugError( "TestCmd info command failed\n" );
227         return 0;
228     }
229
230     delete m_1394Service;
231
232     return 0;
233 }
234
235 TestCmd::TestCmd(Ieee1394Service& ieee1394service, opcode_t opcode )
236     : AVCCommand( ieee1394service, opcode )
237 {
238 }
239
240 TestCmd::~TestCmd()
241 {
242 }
243
244 bool
245 TestCmd::serialize( Util::IOSSerialize& se )
246 {
247     bool result=true;
248     result &= AVCCommand::serialize( se );
249     for (int i=0;i<nargs;i++) {
250         byte_t byte=args[i];
251         result &= se.write(byte, "arg");
252     }
253     return result;
254 }
255
256 bool
257 TestCmd::deserialize( Util::IISDeserialize& de )
258 {
259     bool result=true;
260     result &= AVCCommand::deserialize( de );
261    
262     bool tmpresult=true;
263     nargs=0;
264     while(tmpresult=de.read(&args[nargs])) {
265         nargs++;
266     }
267    
268     return result;
269 }
270
Note: See TracBrowser for help on using the browser.