| 1 | # EAFDSS - Electronic Fiscal Signature Devices Library | ||
| 2 | # Ειδική Ασφαλής Φορολογική Διάταξη Σήμανσης (ΕΑΦΔΣΣ) | ||
| 3 | # | ||
| 4 | # Copyright (C) 2008 Hasiotis Nikos | ||
| 5 | # | ||
| 6 | # ID: $Id: SDNP.pm 105 2009-05-18 10:52:03Z hasiotis $ | ||
| 7 | |||
| 8 | package EAFDSS::SDNP; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | EAFDSS::SDNP - EAFDSS Driver for Micrelec SDNP 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 ethernet 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 | use Socket; | ||
| 29 | use IO::Socket::INET; | ||
| 30 | |||
| 31 | use base qw (EAFDSS::Micrelec ); | ||
| 32 | |||
| 33 | our($VERSION) = '0.80'; | ||
| 34 | |||
| 35 | my($clock_ticks); | ||
| 36 | if ( $^O =~ /MSWin32/ ) { | ||
| 37 | $clock_ticks = 1000; | ||
| 38 | } else { | ||
| 39 | $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK); | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | =head2 init | ||
| 44 | |||
| 45 | The constructor | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub init { | ||
| 50 | my($class) = shift @_; | ||
| 51 | my($config) = @_; | ||
| 52 | my($self) = $class->SUPER::init(@_); | ||
| 53 | |||
| 54 | $self->debug("Initializing"); | ||
| 55 | |||
| 56 | if (! exists $config->{PARAMS}) { | ||
| 57 | return $self->error("No parameters have been given!"); | ||
| 58 | } else { | ||
| 59 | $self->{IP} = $config->{PARAMS}; | ||
| 60 | $self->{PORT} = 24222; | ||
| 61 | } | ||
| 62 | |||
| 63 | $self->debug(" Socket Initialization to IP/hostname [%s]", $self->{IP}); | ||
| 64 | $self->{_SOCKET} = IO::Socket::INET->new(PeerPort => $self->{PORT}, Proto => 'udp', PeerAddr => $self->{IP}); | ||
| 65 | if (! defined $self->{_SOCKET}) { | ||
| 66 | return undef; | ||
| 67 | } | ||
| 68 | |||
| 69 | $self->debug(" Setting timers"); | ||
| 70 | $self->_setTimer('_TSYNC', 0); | ||
| 71 | $self->_setTimer('_T0', 0); | ||
| 72 | $self->_setTimer('_T1', 0); | ||
| 73 | |||
| 74 | $self->debug(" Setting frame counter to 1"); | ||
| 75 | $self->{_FSN} = 1; | ||
| 76 | |||
| 77 | |||
| 78 | my($reply, $deviceID) = $self->PROTO_ReadDeviceID(); | ||
| 79 | $self->debug(" Read device ID"); | ||
| 80 | if ( ($reply == 0) && ($deviceID ne $self->{SN}) ) { | ||
| 81 | return $self->error("Serial Number not matching"); | ||
| 82 | } | ||
| 83 | |||
| 84 | $self->debug(" Init OK"); | ||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 SendRequest | ||
| 89 | |||
| 90 | The ethernet version of SendRequest command. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub SendRequest { | ||
| 95 | my($self) = shift @_; | ||
| 96 | my($opcode) = shift @_; | ||
| 97 | my($opdata) = shift @_; | ||
| 98 | my($data) = shift @_; | ||
| 99 | |||
| 100 | my(%reply) = (); | ||
| 101 | |||
| 102 | # For at least 6 times do: | ||
| 103 | my($busy_try, $state, $try); | ||
| 104 | BUSY: for ($busy_try = 1; $busy_try <= 3; $busy_try++) { | ||
| 105 | for ($try = 1; $try < 6; $try++) { | ||
| 106 | my(%reply) = (); | ||
| 107 | $self->debug(" Send Request try #%d", $try); | ||
| 108 | SYNC: | ||
| 109 | # If state is UNSYNCHRONIZED or connection SYNC timer expired then: | ||
| 110 | if ($self->_getTimer('_TSYNC') >= 0) { | ||
| 111 | $self->debug(" Syncing with device"); | ||
| 112 | if ( $self->_sdnpSync() == 0) { | ||
| 113 | $self->debug(" Sync Failed"); | ||
| 114 | $self->error(64+3); | ||
| 115 | return %reply; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | SEND: | ||
| 120 | # Send REQUEST(Connection's NextFSN) using 'RequestDataPacket'; | ||
| 121 | my($msg) = $self->_sdnpPacket($opcode, $opdata, $data); | ||
| 122 | $self->_sdnpPrintFrame(" ----> [%s]", $msg); | ||
| 123 | $self->{_SOCKET}->send($msg); | ||
| 124 | |||
| 125 | # Set T0 timer to 800 milliseconds; | ||
| 126 | $self->_setTimer('_T0', 0.800); | ||
| 127 | |||
| 128 | # Do until T0 expires: | ||
| 129 | while ($self->_getTimer('_T0') <= 0) { | ||
| 130 | my($frame) = undef; | ||
| 131 | |||
| 132 | $self->{_SOCKET}->recv($frame, 512); | ||
| 133 | if ($frame) { | ||
| 134 | %reply = $self->_sdnpAnalyzeFrame($frame); | ||
| 135 | $self->_sdnpPrintFrame(" <---- [%s]", $msg); | ||
| 136 | $reply{'HOST'} = $self->{_SOCKET}->peerhost(); | ||
| 137 | } else { | ||
| 138 | $reply{HOST} = -1; | ||
| 139 | } | ||
| 140 | |||
| 141 | # If a valid SDNP frame received then do | ||
| 142 | if ($self->_sdnpFrameCheck(\%reply)) { | ||
| 143 | # If received frame's FSN <> Request frame's FSN | ||
| 144 | if ($self->{_FSN} != $reply{SN}) { | ||
| 145 | $self->debug(" Bad FSN, Discarding\n"); | ||
| 146 | next; | ||
| 147 | } else { | ||
| 148 | # Test received frame's opcode; | ||
| 149 | # Case RST: | ||
| 150 | if ($reply{OPCODE} == 0x10) { | ||
| 151 | # Set connection's state to UNSYNCHRONIZED; | ||
| 152 | $self->_setTimer('_TSYNC', 0); | ||
| 153 | goto SYNC; | ||
| 154 | } | ||
| 155 | # Case NAK: | ||
| 156 | if ($reply{OPCODE} == 0x13) { | ||
| 157 | goto SEND; | ||
| 158 | } | ||
| 159 | # Case REPLY: | ||
| 160 | if ($reply{OPCODE} == 0x22) { | ||
| 161 | # If received frame's data packet does not validate okay then: | ||
| 162 | my($i, $checksum) = (0, 0xAA55); | ||
| 163 | for ($i=0; $i < length($reply{DATA}); $i++) { | ||
| 164 | $checksum += ord substr($reply{DATA}, $i, 1); | ||
| 165 | } | ||
| 166 | #$self->debug( " Checking Data checksum [%04X]", $checksum); | ||
| 167 | if ($checksum != $reply{CHECKSUM}) { | ||
| 168 | # Create and send NAK frame with FSN set to received FSN; | ||
| 169 | my($msg) = $self->sdnpPacket(0x13, 0x00); | ||
| 170 | $self->_sdnpPrintFrame(" ----> [%s]\n", $msg); | ||
| 171 | $self->{_SOCKET}->send($msg); | ||
| 172 | next; | ||
| 173 | } else { | ||
| 174 | if ( $reply{DATA} =~ /^0E/ ) { | ||
| 175 | $self->debug(" Will retry because of busyness $busy_try"); | ||
| 176 | $state = "BUSY"; | ||
| 177 | sleep 2; | ||
| 178 | next BUSY; | ||
| 179 | } else { | ||
| 180 | $self->debug(" Done Getting reply"); | ||
| 181 | |||
| 182 | # Renew connection's SYNC timer; | ||
| 183 | $self->_setTimer('_TSYNC', 4); | ||
| 184 | |||
| 185 | # Advance connection's NextFSN by one; | ||
| 186 | $self->{_FSN}++; | ||
| 187 | |||
| 188 | # Return request transmittion success; | ||
| 189 | return %reply; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | $self->debug( " Bad Frame, Discarding"); | ||
| 194 | next; | ||
| 195 | } | ||
| 196 | } else { | ||
| 197 | $self->debug( " Bad Frame, Discarding"); | ||
| 198 | } | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | if ($state eq "BUSY") { | ||
| 204 | $self->debug(" Too busy device... aborting"); | ||
| 205 | $reply{DATA} = "0E/0/"; | ||
| 206 | } | ||
| 207 | |||
| 208 | # Return request transmittion failure; | ||
| 209 | return %reply; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub _sdnpQuery { | ||
| 213 | my($self) = shift @_; | ||
| 214 | my($devices); | ||
| 215 | |||
| 216 | $self->_sdnpSendQuery(); | ||
| 217 | |||
| 218 | # Set timer T0 to 500 milliseconds; | ||
| 219 | $self->_setTimer('_T0', 0.500); | ||
| 220 | |||
| 221 | # Do until T0 expires: | ||
| 222 | while ($self->_getTimer('_T0') <= 0) { | ||
| 223 | my(%reply) = (); | ||
| 224 | my($frame) = undef; | ||
| 225 | |||
| 226 | my($query_socket) = IO::Socket::INET->new( | ||
| 227 | LocalPort => $self->{PORT} + 1, | ||
| 228 | Proto => 'udp', | ||
| 229 | ); | ||
| 230 | |||
| 231 | if (! defined $query_socket) { | ||
| 232 | return undef; | ||
| 233 | } | ||
| 234 | |||
| 235 | $query_socket->recv($frame, 512); | ||
| 236 | if ($frame) { | ||
| 237 | %reply = $self->_sdnpAnalyzeFrame($frame); | ||
| 238 | $self->_sdnpPrintFrame(" <---- [%s]", $frame); | ||
| 239 | $reply{'HOST'} = $query_socket->peerhost(); | ||
| 240 | |||
| 241 | # If a valid frame received then: | ||
| 242 | if ($self->_sdnpQueryFrameCheck(\%reply)) { | ||
| 243 | #If frame type is ACTIVE then: | ||
| 244 | if ($reply{OPCODE} == 0x01) { | ||
| 245 | #If ACTIVE(FSN) = QUERY(FSN) then: | ||
| 246 | if ($self->{_FSN} == $reply{SN}) { | ||
| 247 | #Add IP address of sender to device list; | ||
| 248 | $self->debug(" Found host on IP %s", $reply{'HOST'}); | ||
| 249 | $devices->{$reply{'HOST'}} = 1; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | } | ||
| 254 | close($query_socket); | ||
| 255 | |||
| 256 | $self->_setTimer('_T1', 0.500); | ||
| 257 | if ($self->_getTimer('_T1') >= 0) { | ||
| 258 | $self->_sdnpSendQuery(); | ||
| 259 | $self->_setTimer('_T1', 0.500); | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | $self->_setTimer('_TSYNC', 0); | ||
| 264 | |||
| 265 | return (0, $devices); | ||
| 266 | } | ||
| 267 | |||
| 268 | sub _sdnpSendQuery { | ||
| 269 | my($self) = shift @_; | ||
| 270 | |||
| 271 | $self->{_SOCKET} = IO::Socket::INET->new( | ||
| 272 | LocalPort => $self->{PORT} + 1, | ||
| 273 | PeerAddr => inet_ntoa(INADDR_BROADCAST), | ||
| 274 | PeerPort => $self->{PORT}, | ||
| 275 | Proto => 'udp', | ||
| 276 | Type => SOCK_DGRAM, | ||
| 277 | Broadcast => 1, | ||
| 278 | ); | ||
| 279 | |||
| 280 | if (! defined $self->{_SOCKET}) { | ||
| 281 | return undef; | ||
| 282 | } | ||
| 283 | |||
| 284 | $self->{_FSN} = int(rand(32768) + 1); | ||
| 285 | |||
| 286 | $self->debug( " Send Query Request" ); | ||
| 287 | my($msg) = $self->_sdnpPacket(0x00, 0x00); | ||
| 288 | $self->_sdnpPrintFrame(" ----> [%s]", $msg); | ||
| 289 | $self->{_SOCKET}->send($msg); | ||
| 290 | |||
| 291 | close($self->{_SOCKET}); | ||
| 292 | } | ||
| 293 | |||
| 294 | sub _sdnpSync { | ||
| 295 | my($self) = shift @_; | ||
| 296 | |||
| 297 | # Set connection state to UNSYNCHRONIZED; | ||
| 298 | $self->_setTimer('_TSYNC', 0); | ||
| 299 | |||
| 300 | # For at least 6 times do: | ||
| 301 | my($try); | ||
| 302 | for ($try = 1; $try < 6; $try++) { | ||
| 303 | $self->debug( " Send Sync Request try #%d", $try); | ||
| 304 | |||
| 305 | # Set timer T0 to 500 milliseconds; | ||
| 306 | $self->_setTimer('_T0', 0.500); | ||
| 307 | |||
| 308 | # Select a random initial FSN (IFSN); | ||
| 309 | $self->{_FSN} = int(rand(32768) + 1); | ||
| 310 | |||
| 311 | # Send SYNC(IFSN) frame to connection IP address; | ||
| 312 | my($msg) = $self->_sdnpPacket(0x11, 0x00); | ||
| 313 | $self->_sdnpPrintFrame(" ----> [%s]", $msg); | ||
| 314 | $self->{_SOCKET}->send($msg); | ||
| 315 | |||
| 316 | # Do until T0 expires: | ||
| 317 | while ($self->_getTimer('_T0') <= 0) { | ||
| 318 | my(%reply) = (); | ||
| 319 | my($frame) = undef; | ||
| 320 | |||
| 321 | $self->{_SOCKET}->recv($frame, 512); | ||
| 322 | if ($frame) { | ||
| 323 | %reply = $self->_sdnpAnalyzeFrame($frame); | ||
| 324 | $self->_sdnpPrintFrame(" <---- [%s]", $frame); | ||
| 325 | $reply{'HOST'} = $self->{_SOCKET}->peerhost(); | ||
| 326 | } else { | ||
| 327 | $reply{HOST} = -1; | ||
| 328 | } | ||
| 329 | |||
| 330 | # If a valid frame received then: | ||
| 331 | if ($self->_sdnpFrameCheck(\%reply)) { | ||
| 332 | # If frame type is ACK | ||
| 333 | if ($reply{OPCODE} == 0x12) { | ||
| 334 | # If ACK(FSN) = IFSN then: | ||
| 335 | if ($self->{_FSN} == $reply{SN}) { | ||
| 336 | # Set connection NextFSN = IFSN + 1; | ||
| 337 | $self->{_FSN}++; | ||
| 338 | |||
| 339 | # Set connection SYNC timer to 4 seconds; | ||
| 340 | $self->_setTimer('_TSYNC', 4); | ||
| 341 | |||
| 342 | # Reset T0 timer | ||
| 343 | $self->_setTimer('_T0', 0); | ||
| 344 | |||
| 345 | # Return sync success; | ||
| 346 | return 1; | ||
| 347 | } | ||
| 348 | } else { | ||
| 349 | $self->debug( " SYNC NOT ACKed!"); | ||
| 350 | } | ||
| 351 | } | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | return 0; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub _sdnpQueryFrameCheck { | ||
| 359 | my($self) = shift @_; | ||
| 360 | my($frame) = shift @_; | ||
| 361 | |||
| 362 | $self->debug( " Checking Query Frame"); | ||
| 363 | |||
| 364 | # Check if size of UDP frame < size of SDNP header then: | ||
| 365 | $self->debug( " Checking frame size [%d]", length($frame->{RAW})); | ||
| 366 | if (length($frame->{RAW}) < 12) { | ||
| 367 | return 0; | ||
| 368 | } | ||
| 369 | |||
| 370 | # Check if size of UDP frame > 512 then: | ||
| 371 | if (length($frame) > 512) { | ||
| 372 | return 0; | ||
| 373 | } | ||
| 374 | |||
| 375 | # Check if SDNP header checksum does not validate okay then: | ||
| 376 | my($i, $checksum) = (0, 0xAA55); | ||
| 377 | for ($i=0; $i < 10 ; $i++) { | ||
| 378 | $checksum += ord substr($frame->{RAW}, $i, 1); | ||
| 379 | } | ||
| 380 | $self->debug( " Checking frame header checksum [%04X]", $checksum); | ||
| 381 | if ($checksum != $frame->{HEADER_CHECKSUM}) { | ||
| 382 | return 0; | ||
| 383 | } | ||
| 384 | |||
| 385 | # Check if UDP frame size <> SDNP header data length + SDNP header size then: | ||
| 386 | $self->debug( " Checking UDP frame size [%d]", length($frame->{RAW})); | ||
| 387 | if (length($frame->{RAW}) != 12 + $frame->{LENGTH}) { | ||
| 388 | return 0; | ||
| 389 | } | ||
| 390 | |||
| 391 | # Check if frame id in SDNP header <> SDNP device protocol id then: | ||
| 392 | $self->debug( " Checking frame id [%04X]", $frame->{ID}); | ||
| 393 | if ($frame->{ID} != 0x7A2D) { | ||
| 394 | return 0; | ||
| 395 | } | ||
| 396 | |||
| 397 | # Return success; | ||
| 398 | return 1; | ||
| 399 | } | ||
| 400 | |||
| 401 | sub _sdnpFrameCheck { | ||
| 402 | my($self) = shift @_; | ||
| 403 | my($frame) = shift @_; | ||
| 404 | |||
| 405 | #$self->debug( " Checking Frame"); | ||
| 406 | |||
| 407 | # Check sender ip | ||
| 408 | my($ip) = inet_ntoa(inet_aton($self->{IP})); | ||
| 409 | #$self->debug( " Comparing [%s][%s]", $frame->{HOST}, $ip); | ||
| 410 | if ($frame->{HOST} ne $ip) { | ||
| 411 | return 0; | ||
| 412 | } | ||
| 413 | |||
| 414 | # Check if size of UDP frame < size of SDNP header then: | ||
| 415 | #$self->debug( " Checking frame size [%d]", length($frame->{RAW})); | ||
| 416 | if (length($frame->{RAW}) < 12) { | ||
| 417 | return 0; | ||
| 418 | } | ||
| 419 | |||
| 420 | # Check if size of UDP frame > 512 then: | ||
| 421 | if (length($frame) > 512) { | ||
| 422 | return 0; | ||
| 423 | } | ||
| 424 | |||
| 425 | # Check if SDNP header checksum does not validate okay then: | ||
| 426 | my($i, $checksum) = (0, 0xAA55); | ||
| 427 | for ($i=0; $i < 10 ; $i++) { | ||
| 428 | $checksum += ord substr($frame->{RAW}, $i, 1); | ||
| 429 | } | ||
| 430 | #$self->debug( " Checking frame header checksum [%04X]", $checksum); | ||
| 431 | if ($checksum != $frame->{HEADER_CHECKSUM}) { | ||
| 432 | return 0; | ||
| 433 | } | ||
| 434 | |||
| 435 | # Check if UDP frame size <> SDNP header data length + SDNP header size then: | ||
| 436 | #$self->debug( " Checking UDP frame size [%d]", length($frame->{RAW})); | ||
| 437 | if (length($frame->{RAW}) != 12 + $frame->{LENGTH}) { | ||
| 438 | return 0; | ||
| 439 | } | ||
| 440 | |||
| 441 | # Check if frame id in SDNP header <> SDNP device protocol id then: | ||
| 442 | #$self->debug( " Checking frame id [%04X]", $frame->{ID}); | ||
| 443 | if ($frame->{ID} != 0x7A2D) { | ||
| 444 | return 0; | ||
| 445 | } | ||
| 446 | |||
| 447 | # Return success; | ||
| 448 | return 1; | ||
| 449 | } | ||
| 450 | |||
| 451 | sub _sdnpPacket { | ||
| 452 | my($self) = shift @_; | ||
| 453 | |||
| 454 | my($i); | ||
| 455 | |||
| 456 | my($frame_id) = 0xE18F; | ||
| 457 | my($frame_sn) = $self->{_FSN}; | ||
| 458 | my($opcode) = shift @_; | ||
| 459 | my($opdata) = shift @_; | ||
| 460 | my($data) = shift @_; | ||
| 461 | my($length) = 0x0000; | ||
| 462 | my($checksum) = 0xAA55; | ||
| 463 | my($header) = 0xAA55; | ||
| 464 | |||
| 465 | if ($data) { | ||
| 466 | $length = length($data); | ||
| 467 | for ($i=0; $i < length($data); $i++) { | ||
| 468 | $checksum += ord substr($data, $i, 1); | ||
| 469 | } | ||
| 470 | } else { | ||
| 471 | $data = ""; | ||
| 472 | } | ||
| 473 | |||
| 474 | my($retValue) = pack("SSCCSS", $frame_id, $frame_sn, $opcode, $opdata, $length, $checksum); | ||
| 475 | for ($i=0; $i < length($retValue); $i++) { | ||
| 476 | $header += ord substr($retValue, $i, 1); | ||
| 477 | } | ||
| 478 | |||
| 479 | return pack("SSCCSSS", $frame_id, $frame_sn, $opcode, $opdata, $length, $checksum, $header) . $data; | ||
| 480 | } | ||
| 481 | |||
| 482 | sub _sdnpPrintFrame { | ||
| 483 | my($self) = shift @_; | ||
| 484 | my($format) = shift @_; | ||
| 485 | my($msg) = shift @_; | ||
| 486 | |||
| 487 | my($i, $tmpString); | ||
| 488 | for ($i=0; $i < 11; $i++) { | ||
| 489 | $tmpString .= sprintf("%02X::", ord substr($msg, $i, 1)); | ||
| 490 | } | ||
| 491 | $tmpString .= sprintf("%02X", ord substr($msg, length($msg) - 1, 1)); | ||
| 492 | $self->debug($format, $tmpString); | ||
| 493 | |||
| 494 | my(%frame) = $self->_sdnpAnalyzeFrame($msg); | ||
| 495 | #$self->debug("\t\t ID..................[%04X]", $frame{ID}); | ||
| 496 | #$self->debug("\t\t SN..................[%04X]", $frame{SN}); | ||
| 497 | #$self->debug("\t\t OPCODE..............[ %02X]", $frame{OPCODE}); | ||
| 498 | #$self->debug("\t\t OPDATA..............[ %02X]", $frame{OPDATA}); | ||
| 499 | #$self->debug("\t\t LENGTH..............[%04X]", $frame{LENGTH}); | ||
| 500 | #$self->debug("\t\t CHECKSUM............[%04X]", $frame{CHECKSUM}); | ||
| 501 | #$self->debug("\t\t HEADER_CHECKSUM.....[%04X]", $frame{HEADER_CHECKSUM}); | ||
| 502 | #$self->debug("\t\t DATA................[%s]", $frame{DATA}); | ||
| 503 | |||
| 504 | return; | ||
| 505 | } | ||
| 506 | |||
| 507 | sub _sdnpAnalyzeFrame { | ||
| 508 | my($self) = shift @_; | ||
| 509 | my($msg) = shift @_; | ||
| 510 | |||
| 511 | my(%retValue) = (); | ||
| 512 | |||
| 513 | $retValue{RAW} = $msg; | ||
| 514 | $retValue{ID} = unpack("S", substr($msg, 0, 2)); | ||
| 515 | $retValue{SN} = unpack("S", substr($msg, 2, 2)); | ||
| 516 | $retValue{OPCODE} = unpack("C", substr($msg, 4, 1)); | ||
| 517 | $retValue{OPDATA} = unpack("C", substr($msg, 5, 1)); | ||
| 518 | $retValue{LENGTH} = unpack("S", substr($msg, 6, 2)); | ||
| 519 | $retValue{CHECKSUM} = unpack("S", substr($msg, 8, 2)); | ||
| 520 | $retValue{HEADER_CHECKSUM} = unpack("S", substr($msg, 10, 2)); | ||
| 521 | $retValue{DATA} = substr($msg, 12); | ||
| 522 | |||
| 523 | return %retValue; | ||
| 524 | } | ||
| 525 | |||
| 526 | sub _setTimer { | ||
| 527 | my($self) = shift @_; | ||
| 528 | my($t) = shift @_; | ||
| 529 | my($msec) = shift @_; | ||
| 530 | |||
| 531 | my($realtime, $user, $system, $cuser, $csystem) = POSIX::times(); | ||
| 532 | |||
| 533 | $self->{$t}->{START} = $realtime/$clock_ticks; | ||
| 534 | $self->{$t}->{DURATION} = $msec; | ||
| 535 | } | ||
| 536 | |||
| 537 | sub _getTimer { | ||
| 538 | my($self) = shift @_; | ||
| 539 | my($t) = shift @_; | ||
| 540 | |||
| 541 | my($realtime, $user, $system, $cuser, $csystem) = POSIX::times(); | ||
| 542 | |||
| 543 | #$self->debug(" TIMER[%s]: %4.4f - %4.4f", $t, $realtime/$clock_ticks - $self->{$t}->{START}, $self->{$t}->{DURATION}); | ||
| 544 | |||
| 545 | return $realtime/$clock_ticks - $self->{$t}->{START} - $self->{$t}->{DURATION}; | ||
| 546 | } | ||
| 547 | |||
| 548 | # Preloaded methods go here. | ||
| 549 | |||
| 550 | 1; | ||
| 551 | __END__ |