EAFDSS/SDSP.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 248 CC 20 SCAR 44.7
CC
20
Cov
0%
SendRequest
182.0
init
56.0
_checksum
6.0
CRAP 87.2
1 # EAFDSS - Electronic Fiscal Signature Devices Library
2 # Ειδική Ασφαλής Φορολογική Διάταξη Σήμανσης (ΕΑΦΔΣΣ)
3 #
4 # Copyright (C) 2008 Hasiotis Nikos
5 #
6 # ID: $Id: SDSP.pm 105 2009-05-18 10:52:03Z hasiotis $
7  
8 package EAFDSS::SDSP;
9  
10 =head1 NAME
11  
12 EAFDSS::SDSP - EAFDSS Driver for Micrelec SDSP Devices
13  
14 =head1 DESCRIPTION
15  
16 Read EAFDSS on how to use the module. This module implements the part of the micrelec protocol
17 specific to the serial model.
18  
19 =cut
20  
21 use 5.006_000;
22 use strict;
23 use warnings;
24 use POSIX;
25  
26 use Carp;
27 use Class::Base;
28  
29 use base qw (EAFDSS::Micrelec );
30  
31 our($VERSION) = '0.80';
32  
33 my($control) = {
34 'ACK' => chr(0x06),
35 'NAK' => chr(0x15),
36 'STX' => chr(0x02),
37 'ETX' => chr(0x03),
38 'CAN' => chr(0x18),
39 'ENQ' => chr(0x05)
40 };
41  
42 =head2 init
43  
44 The constructor
45  
46 =cut
47  
48 sub init {
49 my($class) = shift @_;
50 my($config) = @_;
51 my($self) = $class->SUPER::init(@_);
52  
53 $self->debug("Initializing");
54  
55 if (! exists $config->{PARAMS}) {
56 return $self->error("No parameters have been given!");
57 } else {
58 $self->{SERIAL} = $config->{PARAMS};
59 $self->{BAUD} = 115200;
60 }
61  
62 $self->debug(" Serial Device Initialization [%s]", $self->{SERIAL});
63 if ($^O eq 'MSWin32') {
64 require Win32::SerialPort;
65 $self->{_SERIAL} = Win32::SerialPort->new($self->{SERIAL});
66  
67 } else {
68 require Device::SerialPort;
69 $self->{_SERIAL} = Device::SerialPort->new($self->{SERIAL});
70 }
71  
72  
73 if (! defined $self->{_SERIAL}) {
74 return undef;
75 }
76  
77 $self->{_SERIAL}->baudrate($self->{BAUD});
78 $self->{_SERIAL}->parity("none");
79 $self->{_SERIAL}->databits(8);
80 $self->{_SERIAL}->stopbits(1);
81  
82 $self->{_SERIAL}->read_const_time(1);
83  
84 my($reply, $deviceID) = $self->PROTO_ReadDeviceID();
85 if ( ($reply == 0) && ($deviceID ne $self->{SN}) ) {
86 return $self->error("Serial Number not matching");
87 }
88
89 if ( $reply ==0x0E ) {
90 return $self->error("Device is too busy!");
91 }
92  
93 if ( $reply != 0 ) {
94 return $self->error("Error initializing device");
95 }
96  
97 return $self;
98 }
99  
100 =head2 SendRequest
101  
102 The serial version of SendRequest command.
103  
104 =cut
105  
106 sub SendRequest {
107 my($self) = shift @_;
108 my($opcode) = shift @_;
109 my($opdata) = shift @_;
110 my($data) = shift @_;
111  
112 my(%reply) = ();
113  
114 my($busy_try, $try, $state);
115  
116 BUSY: for ($busy_try = 1; $busy_try <= 3; $busy_try++) {
117 $self->{_SERIAL}->read_const_time(3000);
118 $self->{_SERIAL}->read_char_time(0);
119 $state = "ENQ";
120 ENQ: for ($try = 1; $try <= 3; $try++) {
121 my($count_out);
122 $self->debug(" Sending ENQ [try %d]", $try);
123 $count_out = $self->{_SERIAL}->write($control->{'CAN'});
124 $count_out = $self->{_SERIAL}->write($control->{'CAN'});
125 $count_out = $self->{_SERIAL}->write($control->{'CAN'});
126 $count_out = $self->{_SERIAL}->write($control->{'ENQ'});
127 if ($count_out) {
128 my($count_in, $ack) = $self->{_SERIAL}->read(1);
129 if ($count_in && ($ack eq $control->{'ACK'} ) ) {
130 $self->debug(" Got ACK to ENQ");
131 $state = "PACKET";
132 last ENQ;
133 }
134 }
135 }
136 if ($state ne "PACKET") {
137 $reply{DATA} = "02/0/";
138 return %reply;
139 }
140  
141 my($packet) = sprintf("%s%s/%02d%s", $control->{'STX'}, $data, $self->_checksum($data . "/"), $control->{'ETX'});
142 my($ppacket) = sprintf("%s.../%02d", substr($data, 0, 10), $self->_checksum($data . "/"));
143 $self->debug(" Build packet for data[%s]", $ppacket);
144 PACKET: for ($try = 1; $try <= 3; $try++) {
145 my($count_out);
146 $self->debug(" Sending PACKET [try %d]", $try);
147 $count_out = $self->{_SERIAL}->write($packet);
148 if ($count_out == length($packet) ) {
149 my($count_in, $ack) = $self->{_SERIAL}->read(1);
150 if ($count_in && ($ack eq $control->{'ACK'} ) ) {
151 $self->debug(" Got ACK to PACKET send");
152 $state = "REPLY";
153 last PACKET;
154 }
155 if ($count_in && ($ack eq $control->{'NAK'} ) ) {
156 $reply{OPCODE} = 0x13;
157 $self->debug(" Got NAK to PACKET send");
158 }
159 }
160 }
161 if ($state ne "REPLY") {
162 $reply{DATA} = "02/0/";
163 return %reply;
164 }
165  
166 my($full_reply, $reply, $checksum);
167 REPLY: for ($try = 1; $try <= 3; $try++) {
168 $full_reply = "";
169 $self->debug(" Receiving REPLY [try %d]", $try);
170 my($count_in, $ack) = $self->{_SERIAL}->read(1);
171 if ($count_in && ($ack eq $control->{'STX'}) ) {
172 my($count_in, $c);
173 do {
174 ($count_in, $c) = $self->{_SERIAL}->read(1);
175 if ($count_in && ($c ne $control->{'ETX'} ) ) {
176 $full_reply .= $c;
177 }
178 } until ($c eq $control->{'ETX'});
179 $full_reply =~ /(.*)\/(\d\d)$/;
180 ($reply, $checksum) = ($1, $2);
181 if ( $checksum == $self->_checksum($reply . "/")) {
182 $self->debug(" Got Reply [%s]", $reply);
183 $self->{_SERIAL}->write($control->{'ACK'});
184 if ( $reply =~ /^0E/ ) {
185 $self->debug(" Will retry because of busyness");
186 sleep 2;
187 next BUSY;
188 } else {
189 $self->debug(" Done Getting reply");
190 $state = "DONE";
191 last REPLY;
192 }
193 } else {
194 $self->{_SERIAL}->write($control->{'NAK'});
195 $self->debug(" Discarding because of bad checksum");
196 }
197 }
198 }
199 if ($state ne "DONE") {
200 $reply{DATA} = "02/0/";
201 return %reply;
202 } else {
203 $reply{DATA} = $reply;
204 return %reply;
205 }
206 }
207  
208 $self->debug(" Too busy device... aborting");
209 $reply{DATA} = "0E/0/";
210 return %reply;
211 }
212  
213 sub _sdnpQuery {
214 my($self) = shift @_;
215 return (0, undef);
216 }
217  
218 sub _checksum {
219 my($self) = shift @_;
220 my($data) = shift @_;
221  
222 $data = $data;
223 my($i, $checksum) = (0, 0);
224 for ($i=0; $i < length($data); $i++) {
225 if (ord substr($data, $i, 1) != 10) {
226 $checksum += ord substr($data, $i, 1);
227 }
228 $checksum = $checksum % 256;
229 #printf("[%s %3d :: %4d :: %3d]\n", substr($data, $i, 1), ord substr($data, $i, 1), $checksum, $checksum % 100);
230 }
231  
232 return $checksum % 100;
233 }
234  
235 # Preloaded methods go here.
236  
237 1;
238 __END__