Changeset 100
- Timestamp:
- 05/16/05 09:15:28 (19 years ago)
- Files:
-
- trunk/freebob/tests/stream_format.cpp (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/freebob/tests/stream_format.cpp
r98 r100 22 22 #include <libraw1394/raw1394.h> 23 23 24 #include <argp.h> 24 25 #include <stdio.h> 25 26 #include <string.h> … … 31 32 #include <iostream> 32 33 #include <iomanip> 34 #include <iterator> 33 35 34 36 using namespace std; … … 140 142 }; 141 143 144 ostream& operator<<( ostream& stream, ESamplingFrequency freq ) 145 { 146 char* str; 147 switch ( freq ) { 148 case eSF_22050Hz: 149 str = "22050"; 150 break; 151 case eSF_24000Hz: 152 str = "24000"; 153 break; 154 case eSF_32000Hz: 155 str = "32000"; 156 break; 157 case eSF_44100Hz: 158 str = "44100"; 159 break; 160 case eSF_48000Hz: 161 str = "48000"; 162 break; 163 case eSF_88200Hz: 164 str = "88200"; 165 break; 166 case eSF_96000Hz: 167 str = "96000"; 168 break; 169 case eSF_176400Hz: 170 str = "176400"; 171 break; 172 case eSF_192000Hz: 173 str = "192000"; 174 break; 175 case eSF_DontCare: 176 default: 177 str = "unknown"; 178 } 179 return stream << str; 180 }; 181 142 182 enum ERateControl { 143 183 eRC_Supported = 0x00, 144 184 eRC_DontCare = 0x01, 145 185 }; 186 187 188 //////////////////////////////////////////////// 189 // arg parsing 190 //////////////////////////////////////////////// 191 const char *argp_program_version = "stream_format 0.1"; 192 const char *argp_program_bug_address = "<freebob-devel@lists.sf.net>"; 193 static char doc[] = "stream_format -- get/set test program for FreeBob"; 194 static char args_doc[] = "NODE_ID PLUG_ID"; 195 static struct argp_option options[] = { 196 {"verbose", 'v', 0, 0, "Produce verbose output" }, 197 {"test", 't', 0, 0, "Do tests instead get/set action" }, 198 {"frequency", 'f', "FREQUENCY", 0, "Set frequency" }, 199 { 0 } 200 }; 201 202 struct arguments 203 { 204 arguments() 205 : verbose( false ) 206 , test( false ) 207 , frequency( 0 ) 208 { 209 args[0] = 0; 210 args[1] = 0; 211 } 212 213 char* args[2]; 214 bool verbose; 215 bool test; 216 int frequency; 217 } arguments; 218 219 // Parse a single option. 220 static error_t 221 parse_opt( int key, char* arg, struct argp_state* state ) 222 { 223 // Get the input argument from `argp_parse', which we 224 // know is a pointer to our arguments structure. 225 struct arguments* arguments = ( struct arguments* ) state->input; 226 227 char* tail; 228 switch (key) { 229 case 'v': 230 arguments->verbose = true; 231 break; 232 case 't': 233 arguments->test = true; 234 break; 235 case 'f': 236 errno = 0; 237 arguments->frequency = strtol(arg, &tail, 0); 238 if (errno) { 239 perror("argument parsing failed:"); 240 return errno; 241 } 242 case ARGP_KEY_ARG: 243 if (state->arg_num >= 2) { 244 // Too many arguments. 245 argp_usage (state); 246 } 247 arguments->args[state->arg_num] = arg; 248 break; 249 case ARGP_KEY_END: 250 if (state->arg_num < 2) { 251 // Not enough arguments. 252 argp_usage (state); 253 } 254 break; 255 default: 256 return ARGP_ERR_UNKNOWN; 257 } 258 return 0; 259 } 260 261 static struct argp argp = { options, parse_opt, args_doc, doc }; 262 263 //////////////////////////////////////////////// 146 264 147 265 … … 539 657 540 658 number_of_channels_t m_numberOfChannels; 541 stream_format_t m_streamFormat;659 stream_format_t m_streamFormat; 542 660 }; 543 661 … … 591 709 virtual FormatInformationStreamsSync* clone() const; 592 710 593 594 reserved_t m_reserved0; 711 reserved_t m_reserved0; 595 712 sampling_frequency_t m_samplingFrequency; 596 rate_control_t m_rateControl;597 reserved_t m_reserved1;713 rate_control_t m_rateControl; 714 reserved_t m_reserved1; 598 715 }; 599 716 … … 656 773 virtual FormatInformationStreamsCompound* clone() const; 657 774 658 sampling_frequency_t m_samplingFrequency; 659 rate_control_t m_rateControl; 660 number_of_stream_format_infos_t m_numberOfStreamFormatInfos; 775 sampling_frequency_t m_samplingFrequency; 776 rate_control_t m_rateControl; 777 number_of_stream_format_infos_t m_numberOfStreamFormatInfos; 778 661 779 typedef std::vector< StreamFormatInfo* > StreamFormatInfoVector; 662 StreamFormatInfoVector m_streamFormatInfos;780 StreamFormatInfoVector m_streamFormatInfos; 663 781 }; 664 782 … … 835 953 836 954 if (m_level2 == eFHL2_AM824_SYNC_STREAM ) { 837 printf( "+-------------------+\n" ); 838 printf( "| sync stream |\n" ); 839 printf( "+-------------------+\n" ); 955 if ( arguments.verbose ) { 956 printf( "+-------------------+\n" ); 957 printf( "| sync stream |\n" ); 958 printf( "+-------------------+\n" ); 959 } 840 960 m_streams = new FormatInformationStreamsSync(); 841 961 result = m_streams->deserialize( de ); … … 848 968 case eFHL1_AUDIOMUSIC_AM824_COMPOUND: 849 969 { 850 printf( "+-------------------+\n" ); 851 printf( "| compound stream |\n" ); 852 printf( "+-------------------+\n" ); 970 if ( arguments.verbose ) { 971 printf( "+-------------------+\n" ); 972 printf( "| compound stream |\n" ); 973 printf( "+-------------------+\n" ); 974 } 853 975 m_streams = new FormatInformationStreamsCompound(); 854 976 result = m_streams->deserialize( de ); … … 981 1103 eS_NotUsed = AVC1394_EXTENDED_STREAM_FORMAT_INFO_STATUS_NOT_USED, 982 1104 }; 983 984 ExtendedStreamFormatCmd( ESubFunction eSubFunction ); 1105 typedef byte_t status_t; 1106 typedef byte_t index_in_stream_format_t; 1107 1108 ExtendedStreamFormatCmd( ESubFunction eSubFunction = eSF_ExtendedStreamFormatInformationCommand ); 985 1109 virtual ~ExtendedStreamFormatCmd(); 986 1110 987 1111 bool setPlugAddress( const PlugAddress& plugAddress ); 988 1112 bool setIndexInStreamFormat( const int index ); 1113 bool setSubFunction( ESubFunction subFunction ); 989 1114 990 1115 virtual bool serialize( IOSSerialize& se ); … … 995 1120 unsigned int node_id ); 996 1121 1122 inline status_t getStatus(); 1123 FormatInformation* getFormatInformation(); 1124 inline index_in_stream_format_t getIndex(); 1125 997 1126 protected: 998 typedef byte_t index_in_stream_format_t;999 typedef byte_t status_t;1000 1001 1127 opcode_t m_opcode; 1002 1128 subfunction_t m_subFunction; … … 1105 1231 } 1106 1232 1107 // debug output 1108 puts("request:"); 1109 for (int i = 0; i < STREAM_FORMAT_REQUEST_SIZE; ++i) { 1110 printf(" %2d: 0x%08x\n", i, req.quadlet[i]); 1233 if ( arguments.verbose ) { 1234 // debug output 1235 puts("request:"); 1236 for (int i = 0; i < STREAM_FORMAT_REQUEST_SIZE; ++i) { 1237 printf(" %2d: 0x%08x\n", i, req.quadlet[i]); 1238 } 1111 1239 } 1112 1240 1113 1241 resp = reinterpret_cast<packet_t*> ( avc1394_transaction_block( handle, node_id, req.quadlet, STREAM_FORMAT_REQUEST_SIZE, 1 ) ); 1114 1242 if ( resp ) { 1115 // debug output 1116 puts("response:"); 1117 for ( int i = 0; i < STREAM_FORMAT_REQUEST_SIZE; ++i ) { 1118 printf( " %2d: 0x%08x\n", i, resp->quadlet[i] ); 1119 } 1243 if ( arguments.verbose ) { 1244 // debug output 1245 puts("response:"); 1246 for ( int i = 0; i < STREAM_FORMAT_REQUEST_SIZE; ++i ) { 1247 printf( " %2d: 0x%08x\n", i, resp->quadlet[i] ); 1248 } 1249 } 1120 1250 1121 1251 // reorder the bytes to the correct layout … … 1124 1254 } 1125 1255 1126 // a more detailed debug output 1127 printf( "\n" ); 1128 printf( " idx type value\n" ); 1129 printf( "-------------------------------------\n" ); 1130 printf( " %02d ctype: 0x%02x\n", 0, resp->byte[0] ); 1131 printf( " %02d subunit_type + subunit_id: 0x%02x\n", 1, resp->byte[1] ); 1132 printf( " %02d opcode: 0x%02x\n", 2, resp->byte[2] ); 1133 1134 for ( int i = 3; i < STREAM_FORMAT_REQUEST_SIZE * 4; ++i ) { 1135 printf( " %02d operand %2d: 0x%02x\n", i, i-3, resp->byte[i] ); 1256 if ( arguments.verbose ) { 1257 // a more detailed debug output 1258 printf( "\n" ); 1259 printf( " idx type value\n" ); 1260 printf( "-------------------------------------\n" ); 1261 printf( " %02d ctype: 0x%02x\n", 0, resp->byte[0] ); 1262 printf( " %02d subunit_type + subunit_id: 0x%02x\n", 1, resp->byte[1] ); 1263 printf( " %02d opcode: 0x%02x\n", 2, resp->byte[2] ); 1264 1265 for ( int i = 3; i < STREAM_FORMAT_REQUEST_SIZE * 4; ++i ) { 1266 printf( " %02d operand %2d: 0x%02x\n", i, i-3, resp->byte[i] ); 1267 } 1136 1268 } 1137 1269 … … 1149 1281 case eR_Rejected: 1150 1282 if ( m_subFunction == eSF_ExtendedStreamFormatInformationCommandList ) { 1151 printf( "no futher stream formats defined\n" ); 1283 if ( arguments.verbose ) { 1284 printf( "no futher stream formats defined\n" ); 1285 } 1152 1286 result = true; 1153 1287 } else { … … 1165 1299 } 1166 1300 1301 status_t 1302 ExtendedStreamFormatCmd::getStatus() 1303 { 1304 return m_status; 1305 } 1306 1307 FormatInformation* 1308 ExtendedStreamFormatCmd::getFormatInformation() 1309 { 1310 return m_formatInformation; 1311 } 1312 1313 ExtendedStreamFormatCmd::index_in_stream_format_t 1314 ExtendedStreamFormatCmd::getIndex() 1315 { 1316 return m_indexInStreamFormat; 1317 } 1318 1319 bool 1320 ExtendedStreamFormatCmd::setSubFunction( ESubFunction subFunction ) 1321 { 1322 m_subFunction = subFunction; 1323 return true; 1324 } 1167 1325 1168 1326 //////////////////////////////////////// … … 1170 1328 //////////////////////////////////////// 1171 1329 void 1172 doTest s(raw1394handle_t handle, int node_id, int plug_id)1330 doTest(raw1394handle_t handle, int node_id, int plug_id) 1173 1331 { 1174 1332 … … 1189 1347 extendedStreamFormatCmd.fire( AVCCommand::eCT_Status, handle, node_id ); 1190 1348 1191 ExtendedStreamFormatCmd extendedStreamFormatCmdS = ExtendedStreamFormatCmd( ExtendedStreamFormatCmd::eSF_ExtendedStreamFormatInformationCommand);1349 ExtendedStreamFormatCmd extendedStreamFormatCmdS = ExtendedStreamFormatCmd(); 1192 1350 extendedStreamFormatCmdS.setPlugAddress( PlugAddress( PlugAddress::eM_Subunit, 1193 1351 PlugAddress::ePD_Input, … … 1201 1359 } 1202 1360 1361 //////////////////////////////////////// 1362 // Main application 1363 //////////////////////////////////////// 1364 struct FormatInfo { 1365 FormatInfo() 1366 : m_freq( eSF_DontCare ) 1367 , m_format( 0 ) 1368 , m_audioChannels( 0 ) 1369 , m_midiChannels( 0 ) 1370 , m_index( 0 ) 1371 {} 1372 1373 ESamplingFrequency m_freq; 1374 int m_format; // 0 = compound, 1 = sync 1375 int m_audioChannels; 1376 int m_midiChannels; 1377 int m_index; 1378 }; 1379 typedef vector< struct FormatInfo > FormatInfoVector; 1380 1381 ostream& operator<<( ostream& stream, FormatInfo info ) 1382 { 1383 stream << info.m_freq << " Hz ("; 1384 if ( info.m_format ) { 1385 stream << "sync "; 1386 } else { 1387 stream << "compound "; 1388 } 1389 stream << "stream, "; 1390 stream << "audio channels: " << info.m_audioChannels 1391 << ", midi channels: " << info.m_midiChannels << ")"; 1392 return stream; 1393 } 1394 1395 1396 FormatInfo 1397 createFormatInfo( ExtendedStreamFormatCmd& cmd ) 1398 { 1399 FormatInfo fi; 1400 1401 FormatInformationStreamsSync* syncStream 1402 = dynamic_cast< FormatInformationStreamsSync* > ( cmd.getFormatInformation()->m_streams ); 1403 if ( syncStream ) { 1404 fi.m_freq = static_cast<ESamplingFrequency>( syncStream->m_samplingFrequency ); 1405 fi.m_format = 1; 1406 } 1407 1408 FormatInformationStreamsCompound* compoundStream 1409 = dynamic_cast< FormatInformationStreamsCompound* > ( cmd.getFormatInformation()->m_streams ); 1410 if ( compoundStream ) { 1411 fi.m_freq = static_cast<ESamplingFrequency>( compoundStream->m_samplingFrequency ); 1412 fi.m_format = 0; 1413 for ( int j = 0; j < compoundStream->m_numberOfStreamFormatInfos; ++j ) 1414 { 1415 switch ( compoundStream->m_streamFormatInfos[j]->m_streamFormat ) { 1416 case AVC1394_STREAM_FORMAT_AM824_MULTI_BIT_LINEAR_AUDIO_RAW: 1417 fi.m_audioChannels += compoundStream->m_streamFormatInfos[j]->m_numberOfChannels; 1418 break; 1419 case AVC1394_STREAM_FORMAT_AM824_MIDI_CONFORMANT: 1420 fi.m_midiChannels += compoundStream->m_streamFormatInfos[j]->m_numberOfChannels; 1421 break; 1422 default: 1423 cout << "addStreamFormat: unknown stream format for channel" << endl; 1424 } 1425 } 1426 } 1427 fi.m_index = cmd.getIndex(); 1428 return fi; 1429 } 1430 1431 1432 bool 1433 doApp(raw1394handle_t handle, int node_id, int plug_id, ESamplingFrequency frequency = eSF_DontCare) 1434 { 1435 ExtendedStreamFormatCmd extendedStreamFormatCmd = ExtendedStreamFormatCmd( ExtendedStreamFormatCmd::eSF_ExtendedStreamFormatInformationCommandList ); 1436 UnitPlugAddress unitPlugAddress( 0x00, plug_id ); 1437 extendedStreamFormatCmd.setPlugAddress( PlugAddress( PlugAddress::eM_Subunit, 1438 PlugAddress::ePD_Input, 1439 unitPlugAddress ) ); 1440 1441 int i = 0; 1442 FormatInfoVector supportedFormatInfos; 1443 bool cmdSuccess = false; 1444 1445 do { 1446 extendedStreamFormatCmd.setIndexInStreamFormat( i ); 1447 cmdSuccess = extendedStreamFormatCmd.fire( AVCCommand::eCT_Status, handle, node_id ); 1448 if ( cmdSuccess 1449 && ( extendedStreamFormatCmd.getResponse() == AVCCommand::eR_Implemented ) ) 1450 { 1451 supportedFormatInfos.push_back( createFormatInfo( extendedStreamFormatCmd ) ); 1452 } 1453 ++i; 1454 } while ( cmdSuccess && ( extendedStreamFormatCmd.getResponse() == ExtendedStreamFormatCmd::eR_Implemented ) ); 1455 1456 if ( !cmdSuccess ) { 1457 cerr << "Failed to retrieve format infos" << endl; 1458 return false; 1459 } 1460 1461 cout << "Supported frequencies:" << endl; 1462 for ( FormatInfoVector::iterator it = supportedFormatInfos.begin(); 1463 it != supportedFormatInfos.end(); 1464 ++it ) 1465 { 1466 cout << " " << *it << endl; 1467 } 1468 1469 if ( frequency != eSF_DontCare ) { 1470 cout << "Trying to set frequency (" << frequency << "): " << endl; 1471 1472 FormatInfoVector::iterator it; 1473 for ( it = supportedFormatInfos.begin(); 1474 it != supportedFormatInfos.end(); 1475 ++it ) 1476 { 1477 if ( it->m_freq == frequency ) { 1478 cout << " - frequency " << frequency << " is supported" << endl; 1479 1480 ExtendedStreamFormatCmd setFormatCmd = ExtendedStreamFormatCmd( ExtendedStreamFormatCmd::eSF_ExtendedStreamFormatInformationCommandList ); 1481 setFormatCmd.setPlugAddress( PlugAddress( PlugAddress::eM_Subunit, 1482 PlugAddress::ePD_Input, 1483 unitPlugAddress ) ); 1484 1485 setFormatCmd.setIndexInStreamFormat( it->m_index ); 1486 if ( !setFormatCmd.fire( AVCCommand::eCT_Status, handle, node_id ) ) { 1487 cout << " - failed to retrieve format for index " << it->m_index << endl; 1488 return false; 1489 } 1490 1491 cout << " - " << createFormatInfo( setFormatCmd ) << endl; 1492 1493 setFormatCmd.setSubFunction( ExtendedStreamFormatCmd::eSF_ExtendedStreamFormatInformationCommand ); 1494 if ( !setFormatCmd.fire( AVCCommand::eCT_Control, handle, node_id ) ) { 1495 cout << " - failed to set new format" << endl; 1496 return false; 1497 } 1498 cout << " - configuration operation succedded" << endl; 1499 break; 1500 } 1501 } 1502 if ( it == supportedFormatInfos.end() ) { 1503 cout << " - frequency not supported by device. Operation failed" << endl; 1504 } 1505 } 1506 1507 ExtendedStreamFormatCmd currentFormat = ExtendedStreamFormatCmd(); 1508 currentFormat.setPlugAddress( PlugAddress( PlugAddress::eM_Subunit, 1509 PlugAddress::ePD_Input, 1510 unitPlugAddress ) ); 1511 1512 if ( currentFormat.fire( AVCCommand::eCT_Status, handle, node_id ) ) { 1513 cout << "Configured frequency: " << createFormatInfo( currentFormat ) << endl; 1514 } 1515 1516 return true; 1517 } 1518 1203 1519 /////////////////////////////////////////////////////////////// 1520 ESamplingFrequency 1521 parseFrequencyString( int frequency ) 1522 { 1523 ESamplingFrequency efreq; 1524 switch ( frequency ) { 1525 case 22050: 1526 efreq = eSF_22050Hz; 1527 break; 1528 case 24000: 1529 efreq = eSF_24000Hz; 1530 break; 1531 case 32000: 1532 efreq = eSF_32000Hz; 1533 break; 1534 case 44100: 1535 efreq = eSF_44100Hz; 1536 break; 1537 case 48000: 1538 efreq = eSF_48000Hz; 1539 break; 1540 case 88200: 1541 efreq = eSF_88200Hz; 1542 break; 1543 case 96000: 1544 efreq = eSF_96000Hz; 1545 break; 1546 case 176400: 1547 efreq = eSF_176400Hz; 1548 break; 1549 case 192000: 1550 efreq = eSF_192000Hz; 1551 break; 1552 default: 1553 efreq = eSF_DontCare; 1554 } 1555 1556 return efreq; 1557 } 1204 1558 1205 1559 int 1206 1560 main(int argc, char **argv) 1207 1561 { 1208 if (argc < 3) { 1209 printf("usage: %s <NODE_ID> <PLUG_ID>\n", argv[0]); 1210 return 0; 1211 } 1562 // arg parsing 1563 argp_parse (&argp, argc, argv, 0, 0, &arguments); 1212 1564 1213 1565 errno = 0; 1214 1566 char* tail; 1215 int node_id = strtol(arg v[1], &tail, 0);1567 int node_id = strtol(arguments.args[0], &tail, 0); 1216 1568 if (errno) { 1217 1569 perror("argument parsing failed:"); 1218 1570 return -1; 1219 1571 } 1220 int plug_id = strtol(arg v[2], &tail, 0);1572 int plug_id = strtol(arguments.args[1], &tail, 0); 1221 1573 if (errno) { 1222 1574 perror("argument parsing failed:"); … … 1224 1576 } 1225 1577 1578 // create raw1394handle 1226 1579 raw1394handle_t handle; 1227 1580 handle = raw1394_new_handle (); … … 1242 1595 } 1243 1596 1244 doTests( handle, node_id, plug_id ); 1597 if ( arguments.test ) { 1598 doTest( handle, node_id, plug_id ); 1599 } else { 1600 ESamplingFrequency efreq = parseFrequencyString( arguments.frequency ); 1601 doApp( handle, node_id, plug_id, efreq ); 1602 } 1603 1245 1604 raw1394_destroy_handle( handle ); 1246 1605 1247 1606 return 0; 1248 1607 } 1249 1250 /* random stuff which might be still useful1251 1252 #define AVC_CMD_HEADER_SIZE 31253 #define EXTENDED_STREAM_FORMAT_OPERAND_POS_STATUS 61254 switch ( resp->byte[AVC_CMD_HEADER_SIZE + EXTENDED_STREAM_FORMAT_OPERAND_POS_STATUS] ) {1255 case eS_Active:1256 printf( "format is set and a signal is streaming through or to the plug\n" );1257 break;1258 case eS_Inactive:1259 printf( "format is set but no signal is stremaing through the plug\n" );1260 break;1261 case eS_NoStreamFormat:1262 printf( "there is no stream format set, plug has no streaming capabilities\n" );1263 break;1264 case eS_NotUsed:1265 printf( "not used: used for specific inquiry response\n" );1266 break;1267 default:1268 printf( "unknown status\n" );1269 }1270 1271 */