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

Revision 825, 6.9 kB (checked in by wagi, 15 years ago)

- moved cmd_serialize.h Util:: namespace to Util::Cmd:: in order to avoid ambiguities with serialize.h

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