from test.helpers.syscmd import SysCommand from test.helpers.globalVariables import globalVar import binascii import uuid import subprocess from PIL import Image, ImageDraw, ImageFont import qrcode import PIL class Finisher(object): __muhb = None __final_to_burn_to_eeprom = None __qr_image = None def __init__(self, muhb = None): self.__muhb = muhb self.__final_to_burn_to_eeprom = bytearray() self.__qr_image = None pass def eeprom_burn(self): ## Create binary file str_cmd2 = "find /sys/ -iname 'eeprom'" eeprom_location = SysCommand("eeprom_location", str_cmd2) if eeprom_location.execute() == 0: raw_out = eeprom_location.getOutput() if raw_out == "": self.fail("Unable to get EEPROM location. IS EEPROM CONNECTED?") eeprom = raw_out.decode('ascii') eeprom = eeprom.strip('\n') ## push binary data to eeprom like if working with files file2 = open(eeprom, "w+b") file2.write(self.__final_to_burn_to_eeprom) else: self.fail("failed: could not complete find eeprom command") def generate_qr_stamp(self): # Generate QR to put in a stamp qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) # Use board_uuid to generate the QR code qr.add_data(globalVar.g_uuid) qr.make(fit=True) # Save QR as image stamp.png img = qr.make_image() # Store QR as a class atrib self.__qr_image = img img.save('/home/root/stamp.png') def print_stamp(self): # Print stamp by sending a cmd to the printer str_cmd3 = "lpr -o scaling=4 stamp.png".format(self.__display) printstamp = SysCommand("printstamp", str_cmd3) if printstamp.execute() != 0: self.fail("failed: could not print stamp") def generate_screen(self): # Generate green image with board uuid and QR maybe pasted on top of green image # Define image size W = 1250 H = 703 # Create blank rectangle to write on image = Image.new('RGB', (W, H), (46, 204, 113, 0)) draw = ImageDraw.Draw(image) message = "TEST OK\n\n" + "Board UUID: "+ globalVar.g_uuid font = ImageFont.truetype('/usr/share/fonts/truetype/dejavuu/DejaVuSans.ttf', 40) # Calculate the width and height of the text to be drawn, given font size w, h = draw.textsize(message, font=font) # Write the text to the image, where (x,y) is the top left corner of the text draw.text(((W-w)/2,(H-h)/2), message, align='center', font=font) #draw.image(self.__qr_image) image.save('/home/root/test/files/test_ok.png') def show_result_screen(self): # If test OK show test_OK.png located in /home/root, If test fail show test_fail.png, located in /home/root/test/files/test_KO.png if globalVar.fstatus: str_cmd4 = "fbi -T 1 --noverbose -d /dev/{} test/files/test_ok.png".format('fb0') else: str_cmd4 = "fbi -T 1 --noverbose -d /dev/{} test/files/test_ko.png".format('fb0') display_image = SysCommand("display_image", str_cmd4) #print(display_image.execute()) if display_image.execute() != -1: self.fail("failed: could not display the image") def end_ok(self): # Burn retrieved igep eeprom struct new = self.__muhb[0][0] # Convert from string to hex hnew = new.encode() # Magic_id and default crc32 0x6d, 0x6a, 0x6d, 0xe4 with endianess changed so that u-boot loads it correctly # IF magic ever changes this magic_id should be changed. At the end always magic_id of test and u-boot should # be the same magic_id = bytes([0xe4, 0x6d, 0x6a, 0x6d]) default_igep_crc32 = bytes([0x00, 0x00, 0x00, 0x00]) # Create bytearray to_calculate = bytearray() # Build the final hex binary for crc32 operation to_calculate.extend(magic_id) to_calculate.extend(default_igep_crc32) to_calculate.extend(hnew) # Calculate crc32! new_crc32 = binascii.crc32(to_calculate) hnew_crc32 = new_crc32.to_bytes(4, byteorder="little") # Recreate final eeprom struct in bytearray self.__final_to_burn_to_eeprom = bytearray() self.__final_to_burn_to_eeprom.extend(magic_id) self.__final_to_burn_to_eeprom.extend(hnew_crc32) self.__final_to_burn_to_eeprom.extend(hnew) self.eeprom_burn() # Generate QR stamp self.generate_qr_stamp() # Send print stamp command #self.print_stamp() # Generate green image with board uuid and QR maybe pasted on top of green image self.generate_screen() # Show test_ok.png image in screen self.show_result_screen() def end_fail(self): # Burn igep eeprom bug struct hnew = self.__muhb.encode() default_fail = bytes([0xD0, 0xBA, 0xD0, 0xBA]) self.__final_burn_to_eeprom = bytearray() self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(hnew) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.__final_to_burn_to_eeprom.extend(default_fail) self.eeprom_burn() # Show test_ko.png image in screen self.show_result_screen()