1 # -*- coding: utf-8 -*-
3 # (c) Copyright 2010 Hewlett-Packard Development Company, L.P.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # Author: Suma Byrappa
35 from base.codes import *
36 from base import device, utils, pml, codes
42 from ctypes import cdll
44 import ctypes.util as cu
46 log.error("Marvell fax support requires python-ctypes module. Exiting!")
50 # **************************************************************************** #
51 # Marvell Message Types
59 REQUEST_FAX_STATUS = 12
65 FAX_DATA_BLOCK_SIZE = 4096
67 # Fax data variant header TTI header control
69 TTI_PREPENDED_TO_IMAGE = 1
70 TTI_OVERLAYED_ON_IMAGE = 2
72 # **************************************************************************** #
73 class MarvellFaxDevice(FaxDevice):
75 def __init__(self, device_uri=None, printer_name=None,
77 fax_type=FAX_TYPE_NONE,
80 FaxDevice.__init__(self, device_uri,
85 self.send_fax_thread = None
86 self.upload_log_thread = None
89 sendfax_path = utils.which('hp-sendfax')
90 sendfax_a_path = os.readlink(sendfax_path+"/hp-sendfax")
91 if not os.path.isabs(sendfax_a_path):
92 sendfax_f_path = os.path.join(sendfax_path, sendfax_a_path)
94 sendfax_f_path = sendfax_a_path
96 sendfax_abs_path = os.path.realpath(sendfax_f_path)
97 (head, tail) = os.path.split(sendfax_abs_path)
99 lib_name = head+"/fax/plugins/fax_marvell.so"
100 log.debug("Load the library %s\n" % lib_name)
101 if not os.path.exists(lib_name):
102 log.error("Loading %s failed. Try after installing plugin libraries\n" %lib_name);
103 log.info("Run \"hp-plugin\" to installa plugin libraries if you are not automatically prompted\n")
105 self.service.SendEvent(device_uri, printer_name, EVENT_FAX_FAILED_MISSING_PLUGIN, os.getenv('USER'), job_id, "Plugin is not installed")
108 self.libfax_marvell = cdll.LoadLibrary(lib_name)
110 log.error("Loading fax_marvell failed (%s)\n" % e.msg);
114 # Creates a message packet for message type given in argument, and sends it to device
116 # 1. Gets the message packet using fax_marvell.so
117 # 2. Writes the packets to device
118 # 3. Returns the result of send operation
119 def send_packet_for_message(self, msg_type, param1=0, param2=0, status=0, data_len=0):
120 int_array_8 = c_int * 8
121 i_buf = int_array_8(0, 0, 0, 0, 0, 0, 0, 0)
123 result = self.libfax_marvell.create_packet(msg_type, param1, param2, status, data_len, byref(i_buf))
125 log.log_data(buf, 32)
126 self.writeMarvellFax(buf)
127 # self.closeMarvellFax()
132 # Reads response message packet from the device for message type given in argument.
133 # Reads the response from device, and sends the data read to the caller of this method
134 # No Marvell specific code or info
135 def read_response_for_message(self, msg_type):
136 ret_buf = cStringIO.StringIO()
137 while self.readMarvellFax(32, ret_buf, timeout=10):
140 ret_buf = ret_buf.getvalue()
141 #self.closeMarvellFax()
143 log.debug("response_for_message (%d): response packet is\n" % msg_type)
144 log.log_data(ret_buf, 32)
149 def setPhoneNum(self, num):
150 log.debug("************************* setPhoneNum (%s) START **************************" % num)
152 set_buf = cStringIO.StringIO()
154 int_array = c_int * 8
155 i_buf = int_array(0, 0, 0, 0, 0, 0, 0, 0)
157 char_array = c_char * 308
160 date_array = c_char * 15
161 date_buf = date_array()
163 date_buf = "%4d%02d%02d%02d%02d%02d" % (t[0], t[1], t[2], t[3], t[4], t[5])
164 log.debug("Date and Time string is ==>")
167 result = self.libfax_marvell.create_packet(SET_FAX_SETTINGS, 0, 0, 0, 0, byref(i_buf))
168 result = self.libfax_marvell.create_fax_settings_packet(self.station_name, str(num), date_buf, byref(c_buf))
170 msg_buf = buffer(i_buf)
171 msg_c_buf = buffer(c_buf)
173 for i in range(0, 32):
174 set_buf.write(msg_buf[i])
175 for i in range(0, 308):
176 set_buf.write(msg_c_buf[i])
178 set_buf = set_buf.getvalue()
179 log.debug("setPhoneNum: send SET_FAX_SETTINGS message and data ===> ")
180 log.log_data(set_buf, 340)
182 self.writeMarvellFax(set_buf)
183 ret_buf = cStringIO.StringIO()
184 while self.readMarvellFax(32, ret_buf, timeout=10):
186 ret_buf = ret_buf.getvalue()
187 self.closeMarvellFax()
189 response = self.libfax_marvell.extract_response(ret_buf)
190 log.debug("setPhoneNum: response is %d" % response)
192 log.debug("************************* setPhoneNum END **************************")
196 def getPhoneNum(self):
197 int_array_8 = c_int * 8
198 i_buf = int_array_8(0, 0, 0, 0, 0, 0, 0, 0)
199 ph_buf = int_array_8(0, 0, 0, 0, 0, 0, 0, 0)
201 log.debug("******************** getPhoneNum START **********************")
203 result = self.libfax_marvell.create_packet(GET_FAX_SETTINGS, 0, 0, 0, 0, byref(i_buf))
206 self.writeMarvellFax(buf)
207 #self.closeMarvellFax()
208 ret_buf = cStringIO.StringIO()
209 while self.readMarvellFax(512, ret_buf, timeout=10):
211 ret_buf = ret_buf.getvalue()
212 self.closeMarvellFax()
214 response = self.libfax_marvell.extract_response(ret_buf)
215 log.debug("create_packet: response is %d" % response)
217 response = self.libfax_marvell.extract_phone_number(ret_buf, ph_buf)
218 ph_num_buf = cStringIO.StringIO()
219 for i in range(0, 7):
221 ph_num_buf.write(str(ph_buf[i]))
223 ph_num_buf = ph_num_buf.getvalue()
224 log.debug("getPhoneNum: ph_num_buf=%s " % (ph_num_buf))
226 log.debug("******************** getPhoneNum END **********************")
230 # Note down the fax (phone) number
231 phone_num = property(getPhoneNum, setPhoneNum)
234 # Set the station name in the device's settings
236 def setStationName(self, name):
237 log.debug("************************* setStationName(%s) START **************************" % name)
239 int_array = c_int * 8
240 i_buf = int_array(0, 0, 0, 0, 0, 0, 0, 0)
241 set_buf = cStringIO.StringIO()
243 char_array = c_char * 308
246 date_array = c_char * 15
247 date_buf = date_array()
249 date_buf = "%4d%02d%02d%02d%02d%02d" % (t[0], t[1], t[2], t[3], t[4], t[5])
250 log.debug("Date and Time string is ==>")
253 result = self.libfax_marvell.create_packet(SET_FAX_SETTINGS, 0, 0, 0, 0, byref(i_buf))
254 result = self.libfax_marvell.create_fax_settings_packet(str(name), self.phone_num, date_buf, byref(c_buf))
256 msg_buf = buffer(i_buf)
257 msg_c_buf = buffer(c_buf)
259 for i in range(0, 32):
260 set_buf.write(msg_buf[i])
261 for i in range(0, 308):
262 set_buf.write(msg_c_buf[i])
263 set_buf = set_buf.getvalue()
264 log.debug("setStationName: SET_FAX_SETTINGS message and data ===> ")
265 log.log_data(set_buf, 340)
267 self.writeMarvellFax(set_buf)
268 ret_buf = cStringIO.StringIO()
269 while self.readMarvellFax(32, ret_buf, timeout=10):
271 ret_buf = ret_buf.getvalue()
272 self.closeMarvellFax()
274 response = self.libfax_marvell.extract_response(ret_buf)
275 log.debug("setStationName: response is %d" % response)
277 log.debug("************************* setStationName END **************************")
281 def getStationName(self):
282 int_array = c_int * 8
283 i_buf = int_array(0, 0, 0, 0, 0, 0, 0, 0)
284 st_buf = create_string_buffer(128)
286 log.debug("************************* getStationName START **************************")
288 result = self.libfax_marvell.create_packet(GET_FAX_SETTINGS, 0, 0, 0, 0, byref(i_buf))
291 self.writeMarvellFax(buf)
292 #self.closeMarvellFax()
294 ret_buf = cStringIO.StringIO()
295 while self.readMarvellFax(512, ret_buf, timeout=10):
298 ret_buf = ret_buf.getvalue()
299 self.closeMarvellFax()
301 response = self.libfax_marvell.extract_response(ret_buf)
302 log.debug("getStationName: response is %d" % response)
304 result = self.libfax_marvell.extract_station_name(ret_buf, st_buf)
305 log.debug("getStationName: station_name=%s ; result is %d" % (st_buf.value, result))
307 log.debug("************************* getStationName END **************************")
311 # Note down the station-name
312 station_name = property(getStationName, setStationName)
315 # Set date and time in the device's settings
317 # 1. Gets the message packet and fax_settings packet using fax_marvell.so
318 # 2. Writes the packets to the device; Reads response from the device
319 # 3. Extracts the status from the device's response
320 def setDateAndTime(self):
321 int_array = c_int * 8
322 i_buf = int_array(0, 0, 0, 0, 0, 0, 0, 0)
324 log.debug("************************* setDateAndTime START **************************")
326 c_buf = create_string_buffer(308)
327 set_buf = cStringIO.StringIO()
328 ret_buf = cStringIO.StringIO()
329 date_array = c_char * 15
330 date_buf = date_array()
334 date_buf = "%4d%02d%02d%02d%02d%02d" % (t[0], t[1], t[2], t[3], t[4], t[5])
335 log.debug("Date and Time string is ==>")
338 result = self.libfax_marvell.create_packet(SET_FAX_SETTINGS, 0, 0, 0, 0, byref(i_buf))
339 result = create_marvell_faxsettings_pkt(self.phone_num, self.station_name, date_buf, c_buf)
341 msg_buf = buffer(i_buf)
342 for i in range(0, 31):
343 set_buf.write(msg_buf[i])
345 set_buf.write(c_buf.raw)
346 set_buf = set_buf.getvalue()
347 self.dev.writeMarvellFax(set_buf)
348 while self.dev.readMarvellFax(32, ret_buf, timeout=5):
350 ret_buf = ret_buf.getvalue()
351 self.closeMarvellFax()
353 response = self.libfax_marvell.extract_response(ret_buf)
354 log.debug("setDateAndTime: response is %d" % response)
359 # Get the state of the device
361 # 1. Gets the message packet using fax_marvell.so
362 # 2. Writes the packet to the device; Reads response from the device
363 # 3. Extracts the response status and device status from the device's response
364 def getFaxDeviceState(self):
365 log.debug("************************* getFaxDeviceState: START **************************")
367 int_array = c_int * 8
368 i_buf = int_array(0, 0, 0, 0, 0, 0, 0, 0)
371 result = self.libfax_marvell.create_packet(REQUEST_FAX_STATUS, 0, 0, 0, 0, byref(i_buf))
373 self.writeMarvellFax(buf)
375 ret_buf = cStringIO.StringIO()
376 while self.readMarvellFax(32, ret_buf, timeout=5):
378 ret_buf = ret_buf.getvalue()
379 self.closeMarvellFax()
381 response = self.libfax_marvell.extract_response(ret_buf)
382 log.debug("getFaxDeviceState: response is %d" % response)
387 # Creates a thread which does actual Fax submission the state of the device
389 def sendFaxes(self, phone_num_list, fax_file_list, cover_message='', cover_re='',
390 cover_func=None, preserve_formatting=False, printer_name='',
391 update_queue=None, event_queue=None):
393 if not self.isSendFaxActive():
395 self.send_fax_thread = MarvellFaxSendThread(self, self.service, phone_num_list, fax_file_list,
396 cover_message, cover_re, cover_func,
398 printer_name, update_queue,
401 self.send_fax_thread.start()
408 # **************************************************************************** #
409 # Does the actual Fax transmission
410 # **************************************************************************** #
411 class MarvellFaxSendThread(FaxSendThread):
412 def __init__(self, dev, service, phone_num_list, fax_file_list,
413 cover_message='', cover_re='', cover_func=None, preserve_formatting=False,
414 printer_name='', update_queue=None, event_queue=None):
416 FaxSendThread.__init__(self, dev, service, phone_num_list, fax_file_list,
417 cover_message, cover_re, cover_func, preserve_formatting,
418 printer_name, update_queue, event_queue)
427 STATE_READ_SENDER_INFO = 30
429 STATE_COUNT_PAGES = 50
430 STATE_NEXT_RECIPIENT = 60
431 STATE_COVER_PAGE = 70
432 STATE_SINGLE_FILE = 80
433 STATE_MERGE_FILES = 90
434 STATE_SINGLE_FILE = 100
439 next_recipient = self.next_recipient_gen()
444 state = STATE_READ_SENDER_INFO
445 self.rendered_file_list = []
447 while state != STATE_DONE: # --------------------------------- Fax state machine
448 if self.check_for_cancel():
449 log.debug("***** Job is Cancelled.")
450 state = STATE_ABORTED
452 log.debug("*************** STATE=(%d, 0, 0)" % state)
454 if state == STATE_ABORTED: # --------------------------------- Aborted
455 log.error("Aborted by user.")
456 self.write_queue((STATUS_IDLE, 0, ''))
457 state = STATE_CLEANUP
460 elif state == STATE_SUCCESS: # --------------------------------- Success
461 log.debug("Success.")
462 self.write_queue((STATUS_COMPLETED, 0, ''))
463 state = STATE_CLEANUP
466 elif state == STATE_ERROR: # --------------------------------- Error
467 log.error("Error, aborting.")
468 self.write_queue((STATUS_ERROR, 0, ''))
469 state = STATE_CLEANUP
472 elif state == STATE_BUSY: # --------------------------------- Busy
473 log.error("Device busy, aborting.")
474 self.write_queue((STATUS_BUSY, 0, ''))
475 state = STATE_CLEANUP
478 elif state == STATE_READ_SENDER_INFO: # --------------------------------- Get sender info
479 log.debug("%s State: Get sender info" % ("*"*20))
480 state = STATE_PRERENDER
485 log.error("Unable to open device (%s)." % e.msg)
489 self.sender_name = self.dev.station_name
490 self.sender_fax = self.dev.phone_num
492 log.error("Getting station-name and phone_num failed!")
499 elif state == STATE_PRERENDER: # --------------------------------- Pre-render non-G3 files
500 log.debug("%s State: Pre-render non-G3 files" % ("*"*20))
501 state = self.pre_render(STATE_COUNT_PAGES)
504 elif state == STATE_COUNT_PAGES: # --------------------------------- Get total page count
505 log.debug("%s State: Get total page count" % ("*"*20))
506 state = self.count_pages(STATE_NEXT_RECIPIENT)
509 elif state == STATE_NEXT_RECIPIENT: # --------------------------------- Loop for multiple recipients
510 log.debug("%s State: Next recipient" % ("*"*20))
511 state = STATE_COVER_PAGE
514 recipient = next_recipient.next()
516 self.write_queue((STATUS_SENDING_TO_RECIPIENT, 0, recipient['name']))
518 rec_name = recipient['name']
519 rec_num = recipient['fax'].encode('ascii')
520 log.debug("recipient is %s num is %s" % (rec_name, rec_num))
522 except StopIteration:
523 state = STATE_SUCCESS
524 log.debug("Last recipient.")
527 self.recipient_file_list = self.rendered_file_list[:]
530 elif state == STATE_COVER_PAGE: # --------------------------------- Create cover page
531 log.debug("%s State: Render cover page" % ("*"*20))
532 state = self.cover_page(recipient)
535 elif state == STATE_SINGLE_FILE: # --------------------------------- Special case for single file (no merge)
536 log.debug("%s State: Handle single file" % ("*"*20))
537 state = self.single_file(STATE_SEND_FAX)
539 elif state == STATE_MERGE_FILES: # --------------------------------- Merge multiple G3 files
540 log.debug("%s State: Merge multiple files" % ("*"*20))
541 log.debug("Not merging the files for Marvell support")
542 state = STATE_SEND_FAX
544 elif state == STATE_SEND_FAX: # --------------------------------- Send fax state machine
545 log.debug("%s State: Send fax" % ("*"*20))
546 state = STATE_NEXT_RECIPIENT
548 next_file = self.next_file_gen()
550 FAX_SEND_STATE_DONE = 0
551 FAX_SEND_STATE_SUCCESS = 10
552 FAX_SEND_STATE_ABORT = 21
553 FAX_SEND_STATE_ERROR = 22
554 FAX_SEND_STATE_BUSY = 25
555 FAX_SEND_STATE_DEVICE_OPEN = 30
556 FAX_SEND_STATE_NEXT_FILE = 35
557 FAX_SEND_STATE_CHECK_IDLE = 40
558 FAX_SEND_STATE_START_JOB_REQUEST = 50
559 FAX_SEND_STATE_SEND_JOB_REQUEST = 60
560 FAX_SEND_STATE_SET_PARAMS = 70
561 FAX_SEND_STATE_SEND_FAX_HEADER = 80
562 FAX_SEND_STATE_SEND_FILE_DATA = 90
563 FAX_SEND_STATE_END_FILE_DATA = 100
564 FAX_SEND_STATE_END_JOB_REQUEST = 110
565 FAX_SEND_STATE_GET_LOG_INFORMATION = 120
567 monitor_state = False
568 current_state = SUCCESS
569 fax_send_state = FAX_SEND_STATE_DEVICE_OPEN
571 while fax_send_state != FAX_SEND_STATE_DONE:
573 if self.check_for_cancel():
574 log.error("Fax send aborted.")
575 fax_send_state = FAX_SEND_STATE_ABORT
578 fax_state = self.getFaxDeviceState()
579 if fax_state != SUCCESS:
580 log.error("Device is in error state=%d" % fax_state)
581 fax_send_state = FAX_SEND_STATE_ERROR
585 log.debug("********* FAX_SEND_STATE=(%d, %d, %d)" % (STATE_SEND_FAX, fax_send_state, current_state))
587 if fax_send_state == FAX_SEND_STATE_ABORT: # -------------- Abort
588 monitor_state = False
589 fax_send_state = FAX_SEND_STATE_END_JOB_REQUEST
590 state = STATE_ABORTED
592 elif fax_send_state == FAX_SEND_STATE_ERROR: # -------------- Error
593 log.error("Fax send error.")
594 monitor_state = False
596 fax_send_state = FAX_SEND_STATE_END_JOB_REQUEST
599 elif fax_send_state == FAX_SEND_STATE_BUSY: # -------------- Busy
600 log.error("Fax device busy.")
601 monitor_state = False
602 fax_send_state = FAX_SEND_STATE_END_JOB_REQUEST
605 elif fax_send_state == FAX_SEND_STATE_SUCCESS: # -------------- Success
606 log.debug("Fax send success.")
607 monitor_state = False
608 fax_send_state = FAX_SEND_STATE_END_JOB_REQUEST
609 state = STATE_NEXT_RECIPIENT
611 elif fax_send_state == FAX_SEND_STATE_DEVICE_OPEN: # -------------- Device open
612 log.debug("%s State: Open device" % ("*"*20))
613 fax_send_state = FAX_SEND_STATE_NEXT_FILE
617 log.error("Unable to open device (%s)." % e.msg)
618 fax_send_state = FAX_SEND_STATE_ERROR
620 if self.dev.device_state == DEVICE_STATE_NOT_FOUND:
621 fax_send_state = FAX_SEND_STATE_ERROR
624 elif fax_send_state == FAX_SEND_STATE_NEXT_FILE: # -------------- Device open
625 log.debug("%s State: Open device" % ("*"*20))
626 fax_send_state = FAX_SEND_STATE_CHECK_IDLE
628 fax_file = next_file.next()
630 log.debug("***** file name is : %s..." % self.f)
631 except StopIteration:
632 log.debug("file(s) are sent to the device" )
633 fax_send_state = FAX_SEND_STATE_DONE
636 elif fax_send_state == FAX_SEND_STATE_CHECK_IDLE: # -------------- Check for initial idle
637 log.debug("%s State: Check idle" % ("*"*20))
638 fax_send_state = FAX_SEND_STATE_START_JOB_REQUEST
641 ff = file(self.f, 'r')
643 log.error("Unable to read fax file.")
644 fax_send_state = FAX_SEND_STATE_ERROR
648 header = ff.read(FILE_HEADER_SIZE)
650 log.error("Unable to read fax file.")
651 fax_send_state = FAX_SEND_STATE_ERROR
654 magic, version, total_pages, hort_dpi, vert_dpi, page_size, \
655 resolution, encoding, reserved1, reserved2 = self.decode_fax_header(header)
657 if magic != 'hplip_g3':
658 log.error("Invalid file header. Bad magic.")
659 fax_send_state = FAX_SEND_STATE_ERROR
661 log.debug("Magic=%s Version=%d Total Pages=%d hDPI=%d vDPI=%d Size=%d Resolution=%d Encoding=%d"
662 % (magic, version, total_pages, hort_dpi, vert_dpi, page_size, resolution, encoding))
664 dev_state = self.dev.getFaxDeviceState()
667 log.debug("State: device status is zero ")
669 log.debug("State: device status is non-zero ")
670 fax_send_state = FAX_SEND_STATE_BUSY
673 elif fax_send_state == FAX_SEND_STATE_START_JOB_REQUEST: # -------------- Request fax start
674 log.debug("%s State: Request start" % ("*"*20))
675 fax_send_state = FAX_SEND_STATE_SEND_JOB_REQUEST
677 file_len = os.stat(self.f)[ST_SIZE]
678 tx_data_len = file_len - FILE_HEADER_SIZE - (PAGE_HEADER_SIZE*total_pages)
679 log.debug("#### file_len = %d" % file_len)
680 log.debug("#### tx_data_len = %d" % tx_data_len)
681 ret_value = self.dev.send_packet_for_message(START_FAX_JOB, tx_data_len, 0, 0, 0)
683 log.debug("Sending start fax request failed with %d" % ret_value)
684 fax_send_state = FAX_SEND_STATE_ERROR
686 log.debug("Successfully sent start fax request")
687 ret_buf = self.dev.read_response_for_message(START_FAX_JOB)
688 dev_response = self.dev.libfax_marvell.extract_response(ret_buf)
690 log.debug("start-fax request failed with %d" % dev_response)
691 fax_send_state = FAX_SEND_STATE_ERROR
693 log.debug("start-fax request is successful")
695 elif fax_send_state == FAX_SEND_STATE_SEND_JOB_REQUEST: # -------------- Set data request
696 log.debug("%s State: Send data request" % ("*"*20))
697 fax_send_state = FAX_SEND_STATE_SET_PARAMS
699 ret_value = self.dev.send_packet_for_message(SEND_FAX_JOB)
701 log.debug("Sending send-data request failed with %d" % ret_value)
702 fax_send_state = FAX_SEND_STATE_ERROR
704 log.debug("Successfully sent send-fax request")
707 elif fax_send_state == FAX_SEND_STATE_SET_PARAMS: # -------------- Set fax send params
708 log.debug("%s State: Set params" % ("*"*20))
709 fax_send_state = FAX_SEND_STATE_SEND_FAX_HEADER
711 c_buf = create_string_buffer(68)
712 set_buf = cStringIO.StringIO()
715 ret_val = self.dev.libfax_marvell.create_job_settings_packet(no_data, rec_num, c_buf)
716 set_buf.write(c_buf.raw)
717 set_buf = set_buf.getvalue()
719 self.dev.writeMarvellFax(set_buf)
720 #self.dev.closeMarvellFax()
723 elif fax_send_state == FAX_SEND_STATE_SEND_FAX_HEADER: # -------------- Fax header
724 # Taken care by the device
725 fax_send_state = FAX_SEND_STATE_SEND_FILE_DATA
727 elif fax_send_state == FAX_SEND_STATE_SEND_FILE_DATA: # --------------------------------- Send fax pages state machine
728 log.debug("%s State: Send pages" % ("*"*20))
729 fax_send_state = FAX_SEND_STATE_END_FILE_DATA
730 current_state = SUCCESS
733 file_len = os.stat(self.f)[ST_SIZE]
734 bytes_to_read = file_len - FILE_HEADER_SIZE - (PAGE_HEADER_SIZE*total_pages)
736 for p in range(total_pages):
738 if self.check_for_cancel():
739 current_state = FAILURE
741 if current_state == FAILURE:
745 header = ff.read(PAGE_HEADER_SIZE)
747 log.error("Unable to read fax file.")
748 current_state = FAILURE
751 page_num, ppr, rpp, b_to_read, thumbnail_bytes, reserved2 = \
752 self.decode_page_header(header)
754 log.debug("Page=%d PPR=%d RPP=%d BPP=%d Thumb=%d" %
755 (page_num, ppr, rpp, b_to_read, thumbnail_bytes))
757 page.write(ff.read(b_to_read))
758 thumbnail = ff.read(thumbnail_bytes) # thrown away for now (should be 0 read)
760 bytes_to_write = b_to_read
762 while (bytes_to_write > 0):
764 data = page.read(FAX_DATA_BLOCK_SIZE)
766 log.error("Unable to read fax file.")
767 current_state = FAILURE
771 log.error("No data!")
772 current_state = FAILURE
775 if self.check_for_cancel():
776 current_state = FAILURE
777 log.error("Job is cancelled. Aborting...")
780 total_read += FAX_DATA_BLOCK_SIZE
783 ret_value = self.dev.send_packet_for_message(FAX_DATA_BLOCK, 0, 0, 0, len(data))
785 log.debug("Sending fax-data-block request failed with %d" % ret_value)
786 current_state = FAILURE
788 log.debug("Successfully sent fax-data-block request")
790 self.dev.writeMarvellFax(data)
791 #self.dev.closeMarvellFax()
793 log.error("Channel write error.")
794 current_state = FAILURE
797 bytes_to_write = bytes_to_write - FAX_DATA_BLOCK_SIZE
803 elif fax_send_state == FAX_SEND_STATE_END_FILE_DATA: # -------------- end-of-data
804 log.debug("%s State: Send end-of-file-data request" % ("*"*20))
805 fax_send_state = FAX_SEND_STATE_END_JOB_REQUEST
807 ret_value = self.dev.send_packet_for_message(FAX_DATA_BLOCK, 0, 0, current_state, 0)
809 log.debug("Sending fax-data-block packet failed with %d" % ret_value)
810 current_state = FAILURE
812 log.debug("Successfully sent fax-data-block request")
813 ret_buf = self.dev.read_response_for_message(SEND_FAX_JOB)
814 dev_response = self.dev.libfax_marvell.extract_response(ret_buf)
816 log.debug("send-fax request failed with %d" % dev_response)
817 current_state = FAILURE
819 log.debug("send-fax request is successful")
822 log.debug("Exiting...")
826 elif fax_send_state == FAX_SEND_STATE_END_JOB_REQUEST: # -------------- Wait for complete
827 log.debug("%s State: End the job" % ("*"*20))
828 fax_send_state = FAX_SEND_STATE_NEXT_FILE
830 ret_value = self.dev.send_packet_for_message(END_FAX_JOB, 0, 0, current_state, 0)
832 log.debug("Sending end-fax-job packet failed with %d" % ret_value)
833 current_state = FAILURE
835 log.debug("Successfully sent end-fax-job request")
836 ret_buf = self.dev.read_response_for_message(END_FAX_JOB)
837 dev_response = self.dev.libfax_marvell.extract_response(ret_buf)
839 log.debug("end-fax-job request failed with %d" % dev_response)
840 current_state = FAILURE
842 log.debug("end-fax-job request is successful")
844 if current_state != SUCCESS:
845 # There was an error during transmission...
846 log.error("An error occurred! setting fax_send_state to DONE")
847 fax_send_state = FAX_SEND_STATE_DONE
859 elif state == STATE_CLEANUP: # --------------------------------- Cleanup
860 log.debug("%s State: Cleanup" % ("*"*20))
862 if self.remove_temp_file:
863 log.debug("Removing merged file: %s" % self.f)
868 log.debug("Not found")