| 1 | # EAFDSS - Electronic Fiscal Signature Devices Library | ||
| 2 | # Ειδική Ασφαλής Φορολογική Διάταξη Σήμανσης (ΕΑΦΔΣΣ) | ||
| 3 | # | ||
| 4 | # Copyright (C) 2008 Hasiotis Nikos | ||
| 5 | # | ||
| 6 | # ID: $Id: Micrelec.pm 105 2009-05-18 10:52:03Z hasiotis $ | ||
| 7 | |||
| 8 | package EAFDSS::Micrelec; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | EAFDSS::Micrelec - EAFDSS Base Class Driver for Micrelec drivers (SDNP and SDSP) | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | You can't directly use that module. Read EAFDSS manual page on how to use the library. What | ||
| 17 | follows is a list of all the functions that the Micelec driver implements in order to be complete | ||
| 18 | EAFDSS driver. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | use 5.006_000; | ||
| 23 | use strict; | ||
| 24 | use warnings; | ||
| 25 | use Switch; | ||
| 26 | use Carp; | ||
| 27 | use Class::Base; | ||
| 28 | |||
| 29 | use base qw ( EAFDSS::Base ); | ||
| 30 | |||
| 31 | our($VERSION) = '0.80'; | ||
| 32 | |||
| 33 | =head1 Methods | ||
| 34 | |||
| 35 | =head2 PROTO_DetailSign | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub PROTO_DetailSign { | ||
| 40 | my($self) = shift @_; | ||
| 41 | my($fh) = shift @_; | ||
| 42 | |||
| 43 | my($reply, $totalSigns, $dailySigns, $date, $time, $sign) = $self->GetSign($fh); | ||
| 44 | |||
| 45 | if ($reply == 0) { | ||
| 46 | return (hex($reply), sprintf("%s %04d %08d %s%s %s", $sign, $dailySigns, $totalSigns, $self->date6ToHost($date), substr($time, 0, 4), $self->{SN})); | ||
| 47 | } else { | ||
| 48 | return (-1); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 PROTO_GetSign | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub PROTO_GetSign { | ||
| 57 | my($self) = shift @_; | ||
| 58 | my($invoice) = shift @_; | ||
| 59 | |||
| 60 | my($chunk, %reply); | ||
| 61 | $self->debug(" [PROTO] Get Sign"); | ||
| 62 | %reply = $self->SendRequest(0x21, 0x00, "{/0"); | ||
| 63 | if ($reply{DATA} =~ /^18/) { | ||
| 64 | $self->PROTO_IssueReport(); | ||
| 65 | %reply = $self->SendRequest(0x21, 0x00, "{/0"); | ||
| 66 | } | ||
| 67 | |||
| 68 | if (! exists $reply{OPCODE}) { | ||
| 69 | $reply{OPCODE} = 0x22; | ||
| 70 | } | ||
| 71 | if ( %reply && ($reply{OPCODE} == 0x22) ) { | ||
| 72 | foreach $chunk (unpack('(A400)*', $invoice)) { | ||
| 73 | my(%reply) = $self->SendRequest(0x21, 0x00, "@/$chunk"); | ||
| 74 | } | ||
| 75 | %reply = $self->SendRequest(0x21, 0x00, "}"); | ||
| 76 | } | ||
| 77 | |||
| 78 | if (%reply) { | ||
| 79 | my($replyCode, $status1, $status2, $totalSigns, $dailySigns, $date, $time, $sign, $sn, $nextZ) = split(/\//, $reply{DATA}); | ||
| 80 | return (hex($replyCode), $totalSigns, $dailySigns, $date, $time, $nextZ, $sign); | ||
| 81 | } else { | ||
| 82 | return (-1); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | =head2 PROTO_SetHeader | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub PROTO_SetHeader { | ||
| 91 | my($self) = shift @_; | ||
| 92 | my($headers) = shift @_; | ||
| 93 | |||
| 94 | $self->debug(" [PROTO] Set Headers"); | ||
| 95 | my(%reply) = $self->SendRequest(0x21, 0x00, "H/$headers/"); | ||
| 96 | |||
| 97 | if (%reply) { | ||
| 98 | my($replyCode, $status1, $status2) = split(/\//, $reply{DATA}); | ||
| 99 | return (hex($replyCode)); | ||
| 100 | } else { | ||
| 101 | return ($self->error()); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 PROTO_GetStatus | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub PROTO_GetStatus { | ||
| 110 | my($self) = shift @_; | ||
| 111 | |||
| 112 | $self->debug(" [PROTO] Get Status"); | ||
| 113 | my(%reply) = $self->SendRequest(0x21, 0x00, "?"); | ||
| 114 | |||
| 115 | if (%reply) { | ||
| 116 | my($replyCode, $status1, $status2) = split(/\//, $reply{DATA}); | ||
| 117 | return (hex($replyCode), hex($status1), hex($status2)); | ||
| 118 | } else { | ||
| 119 | return ($self->error()); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 PROTO_GetHeader | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub PROTO_GetHeader { | ||
| 128 | my($self) = shift @_; | ||
| 129 | |||
| 130 | $self->debug(" [PROTO] Get Headers"); | ||
| 131 | my(%reply) = $self->SendRequest(0x21, 0x00, "h"); | ||
| 132 | |||
| 133 | if (%reply) { | ||
| 134 | my($replyCode, $status1, $status2, @header) = split(/\//, $reply{DATA}); | ||
| 135 | if (hex($replyCode) == 0) { | ||
| 136 | my($i); | ||
| 137 | for ($i=0; $i < 12; $i+=2) { | ||
| 138 | $header[$i+1] =~ s/\s*$//; | ||
| 139 | } | ||
| 140 | return (hex($replyCode), @header); | ||
| 141 | } else { | ||
| 142 | return (hex($replyCode)); | ||
| 143 | } | ||
| 144 | } else { | ||
| 145 | return (-1); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | =head2 PROTO_ReadTime | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub PROTO_ReadTime { | ||
| 154 | my($self) = shift @_; | ||
| 155 | |||
| 156 | $self->debug(" [PROTO] Read Time"); | ||
| 157 | my(%reply) = $self->SendRequest(0x21, 0x00, "t"); | ||
| 158 | |||
| 159 | if (%reply) { | ||
| 160 | my($replyCode, $status1, $status2, $date, $time) = split(/\//, $reply{DATA}); | ||
| 161 | if (hex($replyCode) == 0) { | ||
| 162 | my($day) = substr($date, 0, 2); | ||
| 163 | my($month) = substr($date, 2, 2); | ||
| 164 | my($year) = substr($date, 4, 2); | ||
| 165 | my($hour) = substr($time, 0, 2); | ||
| 166 | my($min) = substr($time, 2, 2); | ||
| 167 | my($sec) = substr($time, 4, 2); | ||
| 168 | return (hex($replyCode), sprintf("%s/%s/%s %s:%s:%s", $day, $month, $year, $hour, $min, $sec )); | ||
| 169 | } else { | ||
| 170 | return (hex($replyCode)); | ||
| 171 | } | ||
| 172 | } else { | ||
| 173 | return (-1); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | =head2 PROTO_SetTime | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub PROTO_SetTime { | ||
| 182 | my($self) = shift @_; | ||
| 183 | my($time) = shift @_; | ||
| 184 | |||
| 185 | $self->debug(" [PROTO] Set Time"); | ||
| 186 | if (length($time) < 16) { | ||
| 187 | return hex(3); | ||
| 188 | } | ||
| 189 | |||
| 190 | my($pdate) = substr($time, 0, 2) . substr($time, 3, 2) . substr($time, 6, 2); | ||
| 191 | my($ptime) = substr($time, 9, 2) . substr($time, 12, 2) . substr($time, 15, 2); | ||
| 192 | my(%reply) = $self->SendRequest(0x21, 0x00, "T/$pdate/$ptime/"); | ||
| 193 | |||
| 194 | if (%reply) { | ||
| 195 | my($replyCode, $status1, $status2) = split(/\//, $reply{DATA}); | ||
| 196 | return (hex($replyCode)); | ||
| 197 | } else { | ||
| 198 | return ($self->error()); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | =head2 PROTO_ReadDeviceID | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | sub PROTO_ReadDeviceID { | ||
| 207 | my($self) = shift @_; | ||
| 208 | |||
| 209 | $self->debug(" [PROTO] Read Device ID"); | ||
| 210 | my(%reply) = $self->SendRequest(0x21, 0x00, "a"); | ||
| 211 | |||
| 212 | if (%reply) { | ||
| 213 | my($replyCode, $status1, $status2, $deviceId) = split(/\//, $reply{DATA}); | ||
| 214 | return (hex($replyCode), $deviceId); | ||
| 215 | } else { | ||
| 216 | return (-1); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | =head2 PROTO_VersionInfo | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub PROTO_VersionInfo { | ||
| 225 | my($self) = shift @_; | ||
| 226 | |||
| 227 | $self->debug(" [PROTO] Version Info"); | ||
| 228 | my(%reply) = $self->SendRequest(0x21, 0x00, "v"); | ||
| 229 | |||
| 230 | if (%reply) { | ||
| 231 | my($replyCode, $status1, $status2, $vendor, $model, $version) = split(/\//, $reply{DATA}); | ||
| 232 | if (hex($replyCode) == 0) { | ||
| 233 | return (hex($replyCode), sprintf("%s %s version %s", $vendor, $model, $version)); | ||
| 234 | } else { | ||
| 235 | return (hex($replyCode)); | ||
| 236 | } | ||
| 237 | } else { | ||
| 238 | return ($self->error()); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | =head2 PROTO_DisplayMessage | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub PROTO_DisplayMessage { | ||
| 247 | my($self) = shift @_; | ||
| 248 | my($msg) = shift @_; | ||
| 249 | |||
| 250 | $self->debug(" [PROTO] Display Message"); | ||
| 251 | my(%reply) = $self->SendRequest(0x21, 0x00, "7/1/$msg"); | ||
| 252 | |||
| 253 | if (%reply) { | ||
| 254 | my($replyCode, $status1, $status2) = split(/\//, $reply{DATA}); | ||
| 255 | return (hex($replyCode)); | ||
| 256 | } else { | ||
| 257 | return (-1); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | =head2 PROTO_ReadSignEntry | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub PROTO_ReadSignEntry { | ||
| 266 | my($self) = shift @_; | ||
| 267 | my($index) = shift @_; | ||
| 268 | |||
| 269 | $self->debug(" [PROTO] Read Sign Entry"); | ||
| 270 | my(%reply) = $self->SendRequest(0x21, 0x00, "\$/$index"); | ||
| 271 | |||
| 272 | if (%reply) { | ||
| 273 | my($replyCode, $status1, $status2, $totalSigns, $dailySigns, $date, $time, $sign, $sn, $closure) = split(/\//, $reply{DATA}); | ||
| 274 | return (hex($replyCode), $status1, $status2, $totalSigns, $dailySigns, $date, $time, $sign, $sn, $closure); | ||
| 275 | } else { | ||
| 276 | return (-1); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | =head2 PROTO_ReadClosure | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub PROTO_ReadClosure { | ||
| 285 | my($self) = shift @_; | ||
| 286 | my($index) = shift @_; | ||
| 287 | my(%reply, $replyCode, $status1, $status2, $totalSigns, $dailySigns, $date, $time, $z, $sn, $closure); | ||
| 288 | |||
| 289 | $self->debug(" [PROTO] Read Closure"); | ||
| 290 | do { | ||
| 291 | %reply = $self->SendRequest(0x21, 0x00, "R/$index"); | ||
| 292 | if (%reply) { | ||
| 293 | ($replyCode, $status1, $status2, $totalSigns, $dailySigns, $date, $time, $z, $sn, $closure) = split(/\//, $reply{DATA}); | ||
| 294 | } else { | ||
| 295 | return (-1); | ||
| 296 | } | ||
| 297 | if ($replyCode =~ /^0E$/) { | ||
| 298 | sleep 1; | ||
| 299 | } | ||
| 300 | } until ($replyCode !~ /^0E$/); | ||
| 301 | |||
| 302 | return (hex($replyCode), $status1, $status2, $totalSigns, $dailySigns, $self->UTIL_date6ToHost($date), $self->UTIL_time6toHost($time), $z, $sn, $closure); | ||
| 303 | } | ||
| 304 | |||
| 305 | =head2 PROTO_ReadSummary | ||
| 306 | |||
| 307 | =cut | ||
| 308 | |||
| 309 | sub PROTO_ReadSummary { | ||
| 310 | my($self) = shift @_; | ||
| 311 | my(%reply, $replyCode, $status1, $status2, $lastZ, $total, $daily, $signBlock, $remainDaily); | ||
| 312 | |||
| 313 | $self->debug(" [PROTO] Read Summary"); | ||
| 314 | do { | ||
| 315 | my(%reply) = $self->SendRequest(0x21, 0x00, "Z"); | ||
| 316 | if (%reply) { | ||
| 317 | ($replyCode, $status1, $status2, $lastZ, $total, $daily, $signBlock, $remainDaily) = split(/\//, $reply{DATA}); | ||
| 318 | } else { | ||
| 319 | return (-1); | ||
| 320 | } | ||
| 321 | if ($replyCode =~ /^0E$/) { | ||
| 322 | sleep 1; | ||
| 323 | } | ||
| 324 | } until ($replyCode !~ /^0E$/); | ||
| 325 | |||
| 326 | return (hex($replyCode), $status1, $status2, $lastZ, $total, $daily, $signBlock, $remainDaily); | ||
| 327 | } | ||
| 328 | |||
| 329 | =head2 PROTO_IssueReport | ||
| 330 | |||
| 331 | =cut | ||
| 332 | |||
| 333 | sub PROTO_IssueReport { | ||
| 334 | my($self) = shift @_; | ||
| 335 | my(%reply, $replyCode, $status1, $status2); | ||
| 336 | |||
| 337 | $self->debug(" [PROTO] Issue Report"); | ||
| 338 | %reply = $self->SendRequest(0x21, 0x00, "x/2/0"); | ||
| 339 | |||
| 340 | if (%reply) { | ||
| 341 | my($replyCode, $status1, $status2) = split(/\//, $reply{DATA}); | ||
| 342 | return (hex($replyCode)); | ||
| 343 | } else { | ||
| 344 | return (-1); | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | =head2 PROTO_Query | ||
| 349 | |||
| 350 | =cut | ||
| 351 | |||
| 352 | sub PROTO_Query{ | ||
| 353 | my($self) = shift @_; | ||
| 354 | |||
| 355 | $self->debug(" [PROTO] Query "); | ||
| 356 | my($replyCode, $devices) = $self->_sdnpQuery(); | ||
| 357 | |||
| 358 | if (! $replyCode) { | ||
| 359 | return (hex($replyCode), $devices); | ||
| 360 | } else { | ||
| 361 | return ($self->error()); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | =head2 errMessage | ||
| 366 | |||
| 367 | =cut | ||
| 368 | |||
| 369 | sub errMessage { | ||
| 370 | my($self) = shift @_; | ||
| 371 | my($errCode) = shift @_; | ||
| 372 | |||
| 373 | switch ($errCode) { | ||
| 374 | case 00+0x00 { return "No errors - success"} | ||
| 375 | |||
| 376 | case 00+0x01 { return "Wrong number of fields"} | ||
| 377 | case 00+0x02 { return "Field too long"} | ||
| 378 | case 00+0x03 { return "Field too small"} | ||
| 379 | case 00+0x04 { return "Field fixed size mismatch"} | ||
| 380 | case 00+0x05 { return "Field range or type check failed"} | ||
| 381 | case 00+0x06 { return "Bad request code"} | ||
| 382 | case 00+0x09 { return "Printing type bad"} | ||
| 383 | case 00+0x0A { return "Cannot execute with day open"} | ||
| 384 | case 00+0x0B { return "RTC programming requires jumper"} | ||
| 385 | case 00+0x0C { return "RTC date or time invalid"} | ||
| 386 | case 00+0x0D { return "No records in fiscal period"} | ||
| 387 | case 00+0x0E { return "Device is busy in another task"} | ||
| 388 | case 00+0x0F { return "No more header records allowed"} | ||
| 389 | case 00+0x10 { return "Cannot execute with block open"} | ||
| 390 | case 00+0x11 { return "Block not open"} | ||
| 391 | case 00+0x12 { return "Bad data stream"} | ||
| 392 | case 00+0x13 { return "Bad signature field"} | ||
| 393 | case 00+0x14 { return "Z closure time limit"} | ||
| 394 | case 00+0x15 { return "Z closure not found"} | ||
| 395 | case 00+0x16 { return "Z closure record bad"} | ||
| 396 | case 00+0x17 { return "User browsing in progress"} | ||
| 397 | case 00+0x18 { return "Signature daily limit reached"} | ||
| 398 | case 00+0x19 { return "Printer paper end detected"} | ||
| 399 | case 00+0x1A { return "Printer is offline"} | ||
| 400 | case 00+0x1B { return "Fiscal unit is offline"} | ||
| 401 | case 00+0x1C { return "Fatal hardware error"} | ||
| 402 | case 00+0x1D { return "Fiscal unit is full"} | ||
| 403 | case 00+0x1E { return "No data passed for signature"} | ||
| 404 | case 00+0x1F { return "Signature does not exist"} | ||
| 405 | case 00+0x20 { return "Battery fault detected"} | ||
| 406 | case 00+0x21 { return "Recovery in progress"} | ||
| 407 | case 00+0x22 { return "Recovery only after CMOS reset"} | ||
| 408 | case 00+0x23 { return "Real-Time Clock needs programming"} | ||
| 409 | case 00+0x24 { return "Z closure date warning"} | ||
| 410 | case 00+0x25 { return "Bad character in stream"} | ||
| 411 | case 00+0x26 { return ""} | ||
| 412 | case 00+0x01 { return "Device not accessible"} | ||
| 413 | |||
| 414 | case 64+0x01 { return "Device not accessible"} | ||
| 415 | case 64+0x02 { return "No such file"} | ||
| 416 | case 64+0x03 { return "Device Sync Failed"} | ||
| 417 | case 64+0x04 { return "Bad Serial Number"} | ||
| 418 | case 64+0x05 { return "Query found no devices"} | ||
| 419 | case 64+0x10 { return "File contains invalid characters"} | ||
| 420 | |||
| 421 | else { return undef} | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | =head2 UTIL_devStatus | ||
| 426 | |||
| 427 | =cut | ||
| 428 | |||
| 429 | sub UTIL_devStatus { | ||
| 430 | my($self) = shift @_; | ||
| 431 | my($status) = sprintf("%08b", shift); | ||
| 432 | my(@status) = split(//, $status); | ||
| 433 | |||
| 434 | my($busy, $fatal, $paper, $cmos, $printer, $user, $fiscal, $battery) = | ||
| 435 | ($status[7],$status[6],$status[5],$status[4],$status[3],$status[2],$status[1],$status[0]); | ||
| 436 | |||
| 437 | return ($busy, $fatal, $paper, $cmos, $printer, $user, $fiscal, $battery); | ||
| 438 | } | ||
| 439 | |||
| 440 | =head2 UTIL_appStatus | ||
| 441 | |||
| 442 | =cut | ||
| 443 | |||
| 444 | sub UTIL_appStatus { | ||
| 445 | my($self) = shift @_; | ||
| 446 | |||
| 447 | my($status) = sprintf("%08b", shift); | ||
| 448 | my(@status) = split(//, $status); | ||
| 449 | |||
| 450 | my($day, $signature, $recovery, $fiscalWarn, $dailyFull, $fiscalFull) = | ||
| 451 | ($status[6],$status[5],$status[4],$status[2],$status[1],$status[0]); | ||
| 452 | |||
| 453 | return ($day, $signature, $recovery, $fiscalWarn, $dailyFull, $fiscalFull); | ||
| 454 | } | ||
| 455 | |||
| 456 | =head2 UTIL_date6ToHost | ||
| 457 | |||
| 458 | =cut | ||
| 459 | |||
| 460 | sub UTIL_date6ToHost { | ||
| 461 | my($self) = shift @_; | ||
| 462 | my($var) = shift @_; | ||
| 463 | |||
| 464 | $var =~ s/(\d\d)(\d\d)(\d\d)/$3$2$1/; | ||
| 465 | |||
| 466 | return $var; | ||
| 467 | } | ||
| 468 | |||
| 469 | =head2 UTIL_time6toHost | ||
| 470 | |||
| 471 | =cut | ||
| 472 | |||
| 473 | sub UTIL_time6toHost { | ||
| 474 | my($self) = shift @_; | ||
| 475 | my($var) = shift @_; | ||
| 476 | |||
| 477 | $var =~ s/(\d\d)(\d\d)(\d\d)/$1$2/; | ||
| 478 | |||
| 479 | return $var; | ||
| 480 | } | ||
| 481 | |||
| 482 | # Preloaded methods go here. | ||
| 483 | |||
| 484 | 1; | ||
| 485 | |||
| 486 | =head1 VERSION | ||
| 487 | |||
| 488 | This is version 0.80. | ||
| 489 | |||
| 490 | =head1 AUTHOR | ||
| 491 | |||
| 492 | Hasiotis Nikos, E<lt>hasiotis@gmail.comE<gt> | ||
| 493 | |||
| 494 | =head1 COPYRIGHT AND LICENSE | ||
| 495 | |||
| 496 | Copyright (C) 2008 by Hasiotis Nikos | ||
| 497 | |||
| 498 | This library is free software; you can redistribute it and/or modify | ||
| 499 | it under the terms of the LGPL or the same terms as Perl itself, | ||
| 500 | either Perl version 5.8.8 or, at your option, any later version of | ||
| 501 | Perl 5 you may have available. | ||
| 502 | |||
| 503 | =cut |