I'm making a mouse to joystick converter.The solution is map the maximum and minimum value that the mouse can report to the joystick.How can I get the theoretically maximum or minimum value(not logically,regardless of error) of the mouse HID report according to the parameters of the mouse sensor?
My mouse is logitech G403
I dump the HID descriptor:
Usage Page (Generic Desktop) 05 01
Usage (Mouse) 09 02
Collection (Application) A1 01
Usage (Pointer) 09 01
Collection (Physical) A1 00
Usage Page (Button) 05 09
Usage Minimum (Button 1) 19 01
Usage Maximum (Button 16) 29 10
Logical Minimum (0) 15 00
Logical Maximum (1) 25 01
Report Count (16) 95 10
Report Size (1) 75 01
Input (Data,Var,Abs,NWrp,Lin,Pref,NNul,Bit) 81 02
Usage Page (Generic Desktop) 05 01
Logical Minimum (-32767) 16 01 80
Logical Maximum (32767) 26 FF 7F
Report Size (16) 75 10
Report Count (2) 95 02
Usage (X) 09 30
Usage (Y) 09 31
Input (Data,Var,Rel,NWrp,Lin,Pref,NNul,Bit) 81 06
Logical Minimum (-127) 15 81
Logical Maximum (127) 25 7F
Report Size (8) 75 08
Report Count (1) 95 01
Usage (Wheel) 09 38
Input (Data,Var,Rel,NWrp,Lin,Pref,NNul,Bit) 81 06
Usage Page (Consumer Devices) 05 0C
Usage (AC Pan) 0A 38 02
Report Count (1) 95 01
Input (Data,Var,Rel,NWrp,Lin,Pref,NNul,Bit) 81 06
End Collection C0
End Collection
I can only know the logical maximum and minimum of X and Y from this descriptor,No physical values or units are given here.
Through some rough tests I got the following data:
12000dpi,1000 report rate: the maximum value I measured was about 900
6000dpi,1000 report rate: the maximum value I measured was about 450
12000dpi,125 report rate: the maximum value I measured was about 8000
Although you can see here that the report values and DPI/report rate vary linearly but I still don't know what the report value really means.
This mouse fps(sampling rate?) is 12000,That's up to 12000 counts per second?
But obviously the report value is not number of counts.
A byte stored as two hex numbers contains a set of flags. I need to extract these flags, as 0's and 1's. Iterating over entries in a file with:
foreach(<>)
{
#line = split(/ /,$_);
$torR = !!((hex $line[4]) & 0x3); # bit 0 or 1
$torY = !!((hex $line[4]) & 0x4); # bit 2
$torG = !!((hex $line[4]) & 0x8); # bit 3
print "$torR,$torY,$torG\n";
}
run on data file:
796.129 [1f/01] len:7< 02 01 D5 01 8B 0A 8E
796.224 [1f/01] len:7< 02 01 D4 03 09 A9 B8
796.320 [1f/01] len:7< 00 01 D4 03 07 49 5A
796.415 [1f/01] len:7< 00 01 D4 00 11 A0 EE
796.515 [1f/01] len:7< 00 01 D4 00 00 31 4C
796.627 [1f/01] len:7< 02 01 D4 01 89 C1 FD
796.724 [1f/01] len:7< 02 01 D3 03 06 39 FD
796.820 [1f/01] len:7< 08 01 D4 03 08 40 6F
796.915 [1f/01] len:7< 08 01 D5 00 13 3D A4
797.015 [1f/01] len:7< 08 01 D4 00 00 34 04
Actual Result -
1,,
1,,
,,
,,
,,
1,,
1,,
,,1
,,1
,,1
Desired result:
1,0,0
1,0,0
0,0,0
0,0,0
0,0,0
1,0,0
1,0,0
0,0,1
0,0,1
0,0,1
Seems like 'false' gets stored as empty string instead of '0'.
Is there a neat trick to get this right at once, or do I need to convert empty strings to zeros "manually"?
If you want the true/false values to be numeric, you need to coerce them to be numeric:
$torR = 0 + !!((hex $line[4]) & 0x3); # bit 0 or 1
$torY = 0 + !!((hex $line[4]) & 0x4); # bit 2
$torG = 0 + !!((hex $line[4]) & 0x8); # bit 3
Keep in mind that the empty string '' is also a false value.
On the other hand, I might be inclined to write that as:
my (#ryg) = map 0 + !!((hex $line[4]) & $_), 0x3, 0x4, 0x5;
print join(', ', #ryg), "\n";
In addition, you would probably benefit from not using plain numbers in your program. Consider, for example, having a %FLAG structure that gives names to these constants, and a %COL structure that gives names to the columns you are interested in. Using the data you posted:
use Const::Fast;
const my %FLAG => (
torR => 0x3,
torY => 0x4,
torG => 0x5,
);
const my %COL => (
# ...
tor => 4,
);
while (my $line = <DATA>) {
my #line = split ' ', $line;
my %set_flags = map +($_ => 0 + !!((hex $line[$COL{tor}]) & $FLAG{$_})), qw(torR torY torG);
print join(', ', #set_flags{qw(torR torY torG)}), "\n";
}
__DATA__
796.129 [1f/01] len:7< 02 01 D5 01 8B 0A 8E
796.224 [1f/01] len:7< 02 01 D4 03 09 A9 B8
796.320 [1f/01] len:7< 00 01 D4 03 07 49 5A
796.415 [1f/01] len:7< 00 01 D4 00 11 A0 EE
796.515 [1f/01] len:7< 00 01 D4 00 00 31 4C
796.627 [1f/01] len:7< 02 01 D4 01 89 C1 FD
796.724 [1f/01] len:7< 02 01 D3 03 06 39 FD
796.820 [1f/01] len:7< 08 01 D4 03 08 40 6F
796.915 [1f/01] len:7< 08 01 D5 00 13 3D A4
797.015 [1f/01] len:7< 08 01 D4 00 00 34 04
I think I would use split and unpack to turn each value into an array of zeroes and ones, and then examine them individually
use strict;
use warnings 'all';
for my $val ( qw/ 02 02 00 00 00 01 01 08 08 08 / ) {
my #bits = split //, unpack 'b8', chr hex $val;
my $torR = $bits[0] || $bits[1] ? 1 : 0;
my $torY = $bits[2] ? 1 : 0;
my $torG = $bits[3] ? 1 : 0;
print "$torR,$torY,$torG\n";
}
output
1,0,0
1,0,0
0,0,0
0,0,0
0,0,0
1,0,0
1,0,0
0,0,1
0,0,1
0,0,1
Or here's a way using the Bit::Vector which produces the same result
use strict;
use warnings 'all';
use Bit::Vector;
for my $val ( qw/ 02 02 00 00 00 01 01 08 08 08 / ) {
my $vec = Bit::Vector->new_Hex(8, $val);
my $torR = $vec->Chunk_Read(2, 0) ? 1 : 0;
my $torY = $vec->Chunk_Read(1, 2) ? 1 : 0;
my $torG = $vec->Chunk_Read(1, 3) ? 1 : 0;
print "$torR,$torY,$torG\n";
}
I am creating a program that is reading in a file which consists of companies and their information. It is to reject companies with a rating lower that 3.5 and then display the highest quote and the lowest quote and then the average among the companies. I am new to Cobol so I am not sure how to find the highest, lowest and the average. I have coded what I though would work in Module 2012 to find the highest but when I run my program it does not display the information. Any help with this would be greatly appreciated. Thank you
IDENTIFICATION DIVISION.
PROGRAM-ID. MOVING-QUOTES.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MOVING-QUOTES-FILE ASSIGN TO 'Moving-Quotes.txt'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT MOVING-QUOTES-RESULTS-FILE ASSIGN TO "MOVING.TXT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD MOVING-QUOTES-FILE.
01 FS-MOVING-QUOTES-RECORD PIC X(45).
FD MOVING-QUOTES-RESULTS-FILE.
01 MOVING-QUOTES-RESULTS-RECORD PIC X(80).
WORKING-STORAGE SECTION.
01 WS-MOVING-QUOTES-RECORD.
05 FILLER PIC X(2).
05 MQR-COMPANY-NAME PIC X(15).
05 FILLER PIC X(1).
05 MQR-COMPANY-PHONE PIC X(13).
05 FILLER PIC X(1).
05 MQR-MOVING-QUOTE PIC 999V99.
05 FILLER PIC X(1).
05 MQR-NUMBER-ITEMS PIC 999.
05 FILLER PIC X(1).
05 MQR-RATING PIC 9V9.
01 PRT-MOVING-QUOTES-FILE.
05 FILLER PIC X(2).
05 PRT-MQR-COMPANY-NAME PIC X(15).
05 FILLER PIC X(1) VALUE SPACES.
05 PRT-MQR-COMPANY-PHONE PIC X(13).
05 FILLER PIC X(5).
05 PRT-MQR-MOVING-QUOTE PIC $zz99.99.
05 FILLER PIC X(7).
05 PRT-MQR-NUMBER-ITEMS PIC 9(3).
05 FILLER PIC X(15).
05 PRT-MQR-RATING PIC 9.9.
01 PAGE-TITLE.
05 PIC X(22) VALUE SPACES.
05 PIC X(40) VALUE 'MOVING COMPANY QUOTES'.
05 PIC X(18) VALUE SPACES.
05 PRT-PAGE-NUM PIC Z9.
01 QUOTE-HEADING-1.
05 PIC X(7) VALUE SPACES.
05 PIC X(7) VALUE 'COMPANY'.
05 PIC X(9) VALUE SPACES.
05 PIC X(5) VALUE 'PHONE'.
05 PIC X(21) VALUE SPACES.
05 PIC X(9) VALUE 'NUMBER OF'.
05 PIC X(11) VALUE SPACES.
05 PIC X(4) VALUE 'YELP'.
05 PIC X(7).
01 QUOTE-HEADING-2.
05 PIC X(8).
05 PIC X(4) VALUE 'NAME'.
05 PIC X(11) VALUE SPACES.
05 PIC X(6) VALUE 'NUMBER'.
05 PIC X(9) VALUE SPACES.
05 PIC X(5) VALUE 'QUOTE'.
05 PIC X(7) VALUE SPACES.
05 PIC X(5) VALUE 'ITEMS'.
05 PIC X(13) VALUE SPACES.
05 PIC X(6) VALUE 'RATING'.
05 PIC X(2) VALUE SPACES.
01 QUOTE-HEADING-3.
05 PIC X(6) VALUE SPACES.
05 PIC X(9) VALUE ALL '-'.
05 PIC X(7) VALUE SPACES.
05 PIC X(8) VALUE ALL '-'.
05 PIC X(7) VALUE SPACES.
05 PIC X(7) VALUE ALL '-'.
05 PIC X(5) VALUE SPACES.
05 PIC X(11) VALUE ALL '-'.
05 PIC X(7) VALUE SPACES.
05 PIC X(8) VALUE ALL '-'.
01 WS-SWITCHES-AND-COUNTERS.
05 WS-EOF PIC X VALUE 'F'.
05 NUMBER-OF-PAGES PIC S9(4) COMP VALUE 0.
05 NUMBER-OF-LINES-USED PIC S9(4) COMP VALUE 0.
05 WS-HI-BID PIC 999V99 VALUE 0.
05 WS-LO-BID PIC 999V99 VALUE 999.99.
05 WS-TOTAL-BID-SUM PIC 999V99 VALUE 0.
05 WS-NUMBER-OF-RECORDS PIC 9(4) VALUE 4.
05 WS-AVERAGE PIC 999V99.
01 HI-COMPANY-INFO.
05 FILLER PIC X(6) VALUE SPACES.
05 SAV-HI-MQR-COMPANY-NAME PIC X(15).
05 FILLER PIC X(2) VALUE SPACES.
05 SAV-HI-MQR-COMPANY-PHONE PIC X(13).
05 FILLER PIC X(2) VALUE SPACES.
05 SAV-HI-MQR-QUOTE PIC $ZZ99.99.
01 LO-COMPANY-INFO.
05 FILLER PIC X(5) VALUE SPACES.
05 SAV-LO-MQR-COMPANY-NAME PIC X(15).
05 FILLER PIC X(2) VALUE SPACES.
05 SAV-LO-MQR-COMPANY-PHONE PIC X(13).
05 FILLER PIC X(2) VALUE SPACES.
05 SAV-LO-MQR-QUOTE PIC $ZZ99.99.
01 AVG-INFO.
05 PIC X(6) VALUE SPACES.
05 SAV-AVG PIC $ZZ99.99.
01 HI-INFO-HD1.
05 PIC X(8).
05 PIC X(7) VALUE 'HIGH'.
05 PIC X(8).
05 PIC X(5) VALUE 'PHONE'.
05 PIC X(52).
01 HI-INFO-HD2.
05 PIC X(6).
05 PIC X(7) VALUE 'COMPANY'.
05 PIC X(10).
05 PIC X(6) VALUE 'NUMBER'.
05 PIC X(9).
05 PIC X(5) VALUE 'QUOTE'.
05 PIC X(37).
01 HI-INFO-HD3.
05 PIC X(5).
05 PIC X(9) VALUE ALL '-'.
05 PIC X(8).
05 PIC X(8) VALUE ALL '-'.
05 PIC X(7).
05 PIC X(7) VALUE ALL '-'.
05 PIC X(36).
01 LO-INFO-HD1.
05 PIC X(7).
05 PIC X(3) VALUE 'LOW'.
05 PIC X(13).
05 PIC X(5) VALUE 'PHONE'.
05 PIC X(52).
01 LO-INFO-HD2.
05 PIC X(6).
05 PIC X(7) VALUE 'COMPANY'.
05 PIC X(10).
05 PIC X(6) VALUE 'NUMBER'.
05 PIC X(9).
05 PIC X(5) VALUE 'QUOTE'.
05 PIC X(37).
01 LO-INFO-HD3.
05 PIC X(5).
05 PIC X(9) VALUE ALL '-'.
05 PIC X(8).
05 PIC X(8) VALUE ALL '-'.
05 PIC X(7).
05 PIC X(7) VALUE ALL '-'.
05 PIC X(36).
01 AVG-INFO-HD1.
05 PIC X(7).
05 PIC X(7) VALUE 'AVERAGE'.
01 AVG-INFO-HD2.
05 PIC X(6).
05 PIC X(9) VALUE ALL '-'.
05 PIC X(65).
PROCEDURE DIVISION.
0000-MAINLINE.
PERFORM 1000-START-MOVING-QUOTES
PERFORM 2000-READ-N-PRINT-MQ
UNTIL WS-EOF = 'T'
PERFORM 3000-FINISH-UP
STOP RUN
.
*END OF MAIN BLOCK
1000-START-MOVING-QUOTES.
OPEN INPUT MOVING-QUOTES-FILE
OUTPUT MOVING-QUOTES-RESULTS-FILE
PERFORM 8000-READ-MOVING-QUOTES-FILE
IF WS-EOF NOT = 'T'
PERFORM 8020-PRINT-TITLE-AND-HEADINGS.
.
8000-READ-MOVING-QUOTES-FILE.
READ MOVING-QUOTES-FILE
AT END MOVE 'T' TO WS-EOF
end-READ
MOVE FS-MOVING-QUOTES-RECORD TO
WS-MOVING-QUOTES-RECORD
.
8010-CHECK-LINES.
ADD 2 TO NUMBER-OF-LINES-USED
IF NUMBER-OF-LINES-USED > 90
PERFORM 8020-PRINT-TITLE-AND-HEADINGS
.
8020-PRINT-TITLE-AND-HEADINGS.
ADD 1 TO NUMBER-OF-PAGES
MOVE NUMBER-OF-PAGES TO PRT-PAGE-NUM
MOVE SPACES TO MOVING-QUOTES-RESULTS-RECORD
MOVE PAGE-TITLE TO MOVING-QUOTES-RESULTS-RECORD
WRITE MOVING-QUOTES-RESULTS-RECORD AFTER ADVANCING 3 LINES
WRITE MOVING-QUOTES-RESULTS-RECORD FROM NUMBER-OF-PAGES
AFTER ADVANCING 1 LINE
WRITE MOVING-QUOTES-RESULTS-RECORD FROM QUOTE-HEADING-1
AFTER ADVANCING 1 LINE
WRITE MOVING-QUOTES-RESULTS-RECORD FROM QUOTE-HEADING-2
AFTER ADVANCING 1 LINE
WRITE MOVING-QUOTES-RESULTS-RECORD FROM QUOTE-HEADING-3
AFTER ADVANCING 1 LINE
.
8025-PRINT-HI-HD.
MOVE SPACES TO MOVING-QUOTES-RESULTS-RECORD
WRITE MOVING-QUOTES-RESULTS-RECORD FROM HI-INFO-HD1
AFTER ADVANCING 1 LINE
WRITE MOVING-QUOTES-RESULTS-RECORD FROM HI-INFO-HD2
AFTER ADVANCING 1 LINE
WRITE MOVING-QUOTES-RESULTS-RECORD FROM HI-INFO-HD3
AFTER ADVANCING 1 LINE
.
8026-PRINT-LO-HD.
WRITE MOVING-QUOTES-RESULTS-RECORD FROM LO-INFO-HD1
AFTER ADVANCING 1 LINE
WRITE MOVING-QUOTES-RESULTS-RECORD FROM LO-INFO-HD2
AFTER ADVANCING 1 LINE
WRITE MOVING-QUOTES-RESULTS-RECORD FROM LO-INFO-HD3
AFTER ADVANCING 1 LINE
.
8027-PRINT-AVG-HD.
WRITE MOVING-QUOTES-RESULTS-RECORD FROM AVG-INFO-HD1
AFTER ADVANCING 1 LINE
WRITE MOVING-QUOTES-RESULTS-RECORD FROM AVG-INFO-HD2
AFTER ADVANCING 1 LINE
.
2000-READ-N-PRINT-MQ.
* PERFORM 8010-CHECK-LINES
PERFORM 8000-READ-MOVING-QUOTES-FILE
IF (MQR-RATING >= 3.5)
MOVE WS-MOVING-QUOTES-RECORD TO
PRT-MOVING-QUOTES-FILE
PERFORM 2001-PRINT-COMP
PERFORM 2010-FIND-HI-LO-AVG
END-IF
.
2001-PRINT-COMP.
MOVE MQR-COMPANY-NAME TO PRT-MQR-COMPANY-NAME
MOVE MQR-COMPANY-PHONE TO PRT-MQR-COMPANY-PHONE
MOVE MQR-MOVING-QUOTE TO PRT-MQR-MOVING-QUOTE
MOVE MQR-NUMBER-ITEMS TO PRT-MQR-NUMBER-ITEMS
MOVE MQR-RATING TO PRT-MQR-RATING
MOVE PRT-MOVING-QUOTES-FILE TO MOVING-QUOTES-RESULTS-RECORD
WRITE MOVING-QUOTES-RESULTS-RECORD
AFTER ADVANCING 2 LINES
ADD 1 TO WS-NUMBER-OF-RECORDS
.
2010-FIND-HI-LO-AVG.
PERFORM 2012-FIND-HI
PERFORM 2013-STORING-VAL-LO
PERFORM 2014-FIND-LO
PERFORM 2015-GET-SUM
PERFORM 2016-GET-AVERAGE
* PERFORM 8000-READ-MOVING-QUOTES-FILE
.
2011-STORING-VAL-HI.
MOVE MQR-MOVING-QUOTE TO WS-HI-BID
.
2012-FIND-HI.
PERFORM 2011-STORING-VAL-HI
IF (MQR-MOVING-QUOTE > WS-HI-BID)
MOVE MQR-MOVING-QUOTE TO WS-HI-BID
MOVE MQR-COMPANY-NAME TO SAV-HI-MQR-COMPANY-NAME
MOVE MQR-COMPANY-PHONE TO SAV-HI-MQR-COMPANY-PHONE
MOVE WS-HI-BID TO SAV-HI-MQR-QUOTE
MOVE HI-COMPANY-INFO TO MOVING-QUOTES-RESULTS-RECORD
WRITE MOVING-QUOTES-RESULTS-RECORD
AFTER ADVANCING 2 LINES
END-IF
.
2013-STORING-VAL-LO.
MOVE MQR-MOVING-QUOTE TO WS-LO-BID
.
2014-FIND-LO.
IF (MQR-MOVING-QUOTE < WS-LO-BID)
MOVE MQR-MOVING-QUOTE TO WS-LO-BID
MOVE MQR-COMPANY-NAME TO SAV-LO-MQR-COMPANY-NAME
MOVE MQR-COMPANY-PHONE TO SAV-LO-MQR-COMPANY-PHONE
MOVE MQR-MOVING-QUOTE TO SAV-LO-MQR-QUOTE
.
2015-GET-SUM.
ADD MQR-MOVING-QUOTE GIVING WS-TOTAL-BID-SUM
.
2016-GET-AVERAGE.
DIVIDE WS-TOTAL-BID-SUM BY WS-NUMBER-OF-RECORDS GIVING
WS-AVERAGE
MOVE WS-AVERAGE TO SAV-AVG
.
3000-FINISH-UP.
PERFORM 8025-PRINT-HI-HD
PERFORM 2012-FIND-HI
PERFORM 3010-PRT-HI
PERFORM 8026-PRINT-LO-HD
PERFORM 2014-FIND-LO
PERFORM 3020-PRT-LO
PERFORM 8027-PRINT-AVG-HD
PERFORM 2016-GET-AVERAGE
PERFORM 3030-PRT-AVG
CLOSE MOVING-QUOTES-FILE
MOVING-QUOTES-RESULTS-FILE
.
3010-PRT-HI.
MOVE HI-COMPANY-INFO TO MOVING-QUOTES-RESULTS-RECORD
.
3020-PRT-LO.
MOVE LO-COMPANY-INFO TO MOVING-QUOTES-RESULTS-RECORD
.
3030-PRT-AVG.
MOVE SAV-AVG TO MOVING-QUOTES-RESULTS-RECORD
.
Taken from the update in Finding Highest , Lowest and the Average in a Cobol program
Some more issues in 2000- the perform 8000- should be at the end of the procedure
Currently:
2000-READ-N-PRINT-MQ.
* PERFORM 8010-CHECK-LINES
PERFORM 8000-READ-MOVING-QUOTES-FILE
IF (MQR-RATING >= 3.5)
MOVE WS-MOVING-QUOTES-RECORD TO
PRT-MOVING-QUOTES-FILE
PERFORM 2001-PRINT-COMP
PERFORM 2010-FIND-HI-LO-AVG
END-IF
Should be:
2000-READ-N-PRINT-MQ.
* PERFORM 8010-CHECK-LINES
IF (MQR-RATING >= 3.5)
MOVE WS-MOVING-QUOTES-RECORD TO
PRT-MOVING-QUOTES-FILE
PERFORM 2001-PRINT-COMP
PERFORM 2010-FIND-HI-LO-AVG
END-IF
PERFORM 8000-READ-MOVING-QUOTES-FILE
also in 2012- why are you doing a write, surely this should be done at the end (section 3000-).
the 2012-, 2013- do not make any sense
in 2000, 2011, 2012 you need to do
if value-from-input-file > current-high
move input-file-details to current-High-details
end-if
if value-from-input-file < current-low
move input-file-details to current-Low-details
end-if
You would then write out the hi/lo results in 3000-
It is also very important to initialize the hi/low values appropriately ???
Try either
putting in Display's at the start of procedure's (+ relavent details)
Using a debugger
either will tell you what the program flow is
I am trying to read the following data into MATLAB:
'0.000000 1 18EFFA59x Rx D 8 AD 09 02 00 00 00 00 30'
'0.004245 1 14EFF01Cx Rx D 6 DB 00 FF FF 00 71'
'0.004640 1 CEF801Cx Rx D 3 3F 00 3B'
'0.005130 1 14EF131Cx Rx D 6 DB 00 FF FF 00 71'
'0.005630 1 CEF801Cx Rx D 3 3F 00 C3'
'0.010015 1 18EFFA59x Rx D 8 AD 07 01 00 00 00 00 30'
'0.014145 1 CF004F0x Rx D 8 F0 FF 7D 00 00 FF FF FF'
'0.015060 1 18EFFA59x Rx D 8 AD 07 02 00 00 00 00 30'
'0.018235 1 18EF1CF0x Rx D 8 F2 1E 05 FF FF 00 71 FF'
'0.018845 1 18EA5941x Rx D 3 09 FF 00'
I can easily read in each line as a string - but to make post-processing more efficient I'd like to separate each line by its delimiter - which is whitespace. In other words, the end result should be a non-singleton cell array. I can't seem to find a very efficient way of doing this. Efficiency is important because these files are several million lines long and processing in MATLAB with strings/cells takes a long time.
Any help would be appreciated. Thanks.
You appear to have fixed-width fields, so I would treat it as such and let textscan do the most of the pre-processing for you by turning off delimiters and whitespace and defining the field widths and types explicitly:
test = {...
'0.000000 1 18EFFA59x Rx D 8 AD 09 02 00 00 00 00 30'
'0.004245 1 14EFF01Cx Rx D 6 DB 00 FF FF 00 71'
'0.004640 1 CEF801Cx Rx D 3 3F 00 3B'
'0.005130 1 14EF131Cx Rx D 6 DB 00 FF FF 00 71'
'0.005630 1 CEF801Cx Rx D 3 3F 00 C3'
'0.010015 1 18EFFA59x Rx D 8 AD 07 01 00 00 00 00 30'
'0.014145 1 CF004F0x Rx D 8 F0 FF 7D 00 00 FF FF FF'
'0.015060 1 18EFFA59x Rx D 8 AD 07 02 00 00 00 00 30'
'0.018235 1 18EF1CF0x Rx D 8 F2 1E 05 FF FF 00 71 FF'
'0.018845 1 18EA5941x Rx D 3 09 FF 00'};
test = strjoin(test', '\n');
C = textscan(test, '%8.6f %2u %11s %4s %2s %2u %33s', 'delimiter', '','whitespace','');
col1 = C{1};
col2 = C{2};
col3 = strtrim(C{3});
col3 = cellfun(#(x)hex2dec(x(1:end-1)), col3); % for instance.
col4 = strtrim(C{4});
col5 = strtrim(C{5});
col6 = C{6};
col7 = strtrim(C{7});
In the real world, you'd substitute the text string for a file id. For the last variable-length field, just read the whole thing in, making sure you specify the maximum possible length. MATLAB will read a field until it gets to the end or reaches a newline character (in fact, I made the last field width 1 larger, just to make sure). Each field is then aggregated into a cell. I also took the liberty of converting the third field from hex to decimal to show how you might post-process the numbers further.
As a further note, if you really do have gigantic files and need maximum speed, you could skip the strtrim step on the character fields by specifying %*ns where n is the desired field width, for any known gaps such as the 2 character gap between columns 3 and 4. The star says to ignore that field. I find this way of doing things a bit more readable and intuitive, however, and leaves a small margin of error in case one of the fields, such as the 4th, occasionally has a 3 character entry.
With a smart card Gemalto (IAS ECC), I would to calculate a signature by using private key stored on smart card. For this, I use APDU commands:
// Verify PIN
00 20 00 01 04 31 32 33 34
-> 90 00
// Create a context for security operation
00 22 41 B6 06 84 01 84 80 01 12
-> 90 00
// Set the hash of the document
00 2A 90 A0 14 HASH OF DOCUMENT
-> 69 85
// Calculating the signature
00 2A 9E 9A 80
-> 69 85
My problem is the following: the las two commands return the error code "69 85", meaning "Conditions of use not satisfied".
I have already tried several solutions, but I obtain always the same error. How to resolve it? What does this code can mean?
After some tests, I discovered something interesting. When I replace cla "00" by "10", smart card returns a different response:
// Create a context for security operation
00 22 41 B6 06 84 01 84 80 01 12
// Verify PIN
00 20 00 01 04 31 32 33 34
// Calculating the signature (I replace "00" by "10")
10 2A 9E 9A 23 30 21 30 09 06 05 2B 0E 03 02 1A 05 00 04 14 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 12 13 14 15
I don't know if it's the good solution because smart card returns "90 00". But, it would return the content of my signature!
Thank you for your help!
Best regards
You are getting SW 6985 for
// Set the hash of the document
00 2A 90 A0 14 HASH OF DOCUMENT
-> 69 85
Since you have not set the correct context in current security environment.
Let me explain this below
First you performed VERIFY PIN command which was successful
// Verify PIN
00 20 00 01 04 31 32 33 34
-> 90 00
Then you performed MSE SET command,Where you set the security context.For this you have to understood how SE works(Please refer to section 3.5 fron IAS ECC v1.01).
At the time of personalisation, the Personaliser agent create SDO(Secure Data Object) inside the card.The reference to this SDO are mentioned in SE(Security Environment) in form of CRT(Control reference template).
// Create a context for security operation
00 22 41 B6 06 84 01 84 80 01 12
-> 90 00
Generally speaking, MSE SET command will always return SW 900 even if the SDO reference is wrong. Since it only return SW 6A80 when the template is wrong not when the reference is wrong.(The SDO reference is passed in tag 84)
After that you performed PSO HASH command
// Set the hash of the document
00 2A 90 A0 14 HASH OF DOCUMENT
-> 69 85
where the card return SW 6985(Condition of use not satisfied), This indicate the algorithm and SDO reference used for calculating Hash may wrong. Which is probably happening since the SDO reference which was sent during the time of MSE SET command is not available
Detecting error coming from MSE SET could be tricky since it return SW 9000.
For these type of situation you have to check the personalisation file carefully and need to match the MSE SET command with regard to SDO reference and supported ALGOs.
It may be useful to put the default context (e.g., cryptographic algorithms or
security operations) into the current SE in order to have few exchanges of MSE set commands.