Skip to content

New commands for myevic #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,33 @@ Use ``--no-verify`` to disable verification for APROM or data flash. To disable
::

$ evic-usb upload --no-verify aprom --no-verify dataflash firmware.bin

Reset the device:

::

$ evic-usb reset

Dump any part of the flash memory:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add that fmc-read, time and screenshot are specific to your FW. reset is good.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.


::

$ evic-usb fmc-read -o out.bin -s startaddr -l length

Example to read the parameters flash memory:

::

$ evic-usb fmc-read -o out.bin -s 122880 -l 4096

Setup date and time of the device to the current time:

::

$ evic-usb time

Take a screenshot of the device display:

::

$ evic-usb screenshot -o outfile.[png|jpg|...]
164 changes: 132 additions & 32 deletions evic/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
import struct
from time import sleep
from contextlib import contextmanager
from datetime import datetime
from PIL import Image

import click

import evic

from .device import DeviceInfo

@contextmanager
def handle_exceptions(*exceptions):
Expand Down Expand Up @@ -108,17 +109,39 @@ def read_dataflash(dev, verify):
return dataflash


def print_device_info(device_info, dataflash):
def fmc_read(dev, start, len):
"""Reads the device data flash.

Args:
dev: evic.HIDTransfer object.

Returns:
evic.DataFlash object containing the device data flash.
"""

# Read the data flash
with handle_exceptions(IOError):
click.echo("Reading data flash...", nl=False)
fmemory = dev.fmc_read(start, len)

return fmemory


def print_device_info(dev, dataflash):
"""Prints the device information found from data flash.

Args:
device_info: device.DeviceInfo tuple.
dev: evic.HIDTransfer object.
dataflash: evic.DataFlash object.
"""

# Find the product name
product_name = dev.product_names.get(dataflash.product_id,
"Unknown device")

# Print out the information
click.echo("\tDevice name: ", nl=False)
click.secho(device_info.name, bold=True)
click.secho(product_name, bold=True)
click.echo("\tFirmware version: ", nl=False)
click.secho("{0:.2f}".format(dataflash.fw_version / 100.0), bold=True)
click.echo("\tHardware version: ", nl=False)
Expand Down Expand Up @@ -167,12 +190,8 @@ def upload(inputfile, encrypted, dataflashfile, noverify):
dataflash = read_dataflash(dev, verify)
dataflash_original = copy.deepcopy(dataflash)

# Get the device info
device_info = dev.devices.get(dataflash.product_id,
DeviceInfo("Unknown device", None, None))

# Print the device information
print_device_info(device_info, dataflash)
print_device_info(dev, dataflash)

# Read the APROM image
aprom = evic.APROM(inputfile.read())
Expand All @@ -183,12 +202,9 @@ def upload(inputfile, encrypted, dataflashfile, noverify):
if 'aprom' not in noverify:
with handle_exceptions(evic.APROMError):
click.echo("Verifying APROM...", nl=False)

supported_product_ids = [dataflash.product_id]
if device_info.supported_product_ids:
supported_product_ids.extend(device_info.supported_product_ids)

aprom.verify(supported_product_ids, dataflash.hw_version)
aprom.verify(
dev.supported_product_ids[dataflash.product_id],
dataflash.hw_version)

# Are we using a data flash file?
if dataflashfile:
Expand Down Expand Up @@ -237,6 +253,53 @@ def upload(inputfile, encrypted, dataflashfile, noverify):
dev.write_aprom(aprom)


@usb.command('reset')
def reset():
"""Resets the device."""

dev = evic.HIDTransfer()

# Connect the device
connect(dev)

# Restart
click.echo("Restarting the device...", nl=False)
dev.reset()
sleep(2)
click.secho("OK", fg='green', nl=False, bold=True)


@usb.command('time')
def time():
"""Sets the device date/time to now."""

dev = evic.HIDTransfer()

# Connect the device
connect(dev)

# Read the data flash
dataflash = read_dataflash(dev, 1)

# Print the device information
print_device_info(dev, dataflash)

dt = datetime.now()
dataflash.df_year = dt.year
dataflash.df_month = dt.month
dataflash.df_day = dt.day
dataflash.df_hour = dt.hour
dataflash.df_minute = dt.minute
dataflash.df_second = dt.second

# Write data flash to the device
with handle_exceptions(IOError):
click.echo("Writing data flash...", nl=False)
sleep(0.1)
dev.write_dataflash(dataflash)
click.secho("OK", fg='green', bold=True)


@usb.command('upload-logo')
@click.argument('inputfile', type=click.File('rb'))
@click.option('--invert', '-i', is_flag=True,
Expand All @@ -258,27 +321,22 @@ def uploadlogo(inputfile, invert, noverify):
dataflash = read_dataflash(dev, noverify)
dataflash_original = copy.deepcopy(dataflash)

# Get the device info
device_info = dev.devices.get(dataflash.product_id,
DeviceInfo("Unknown device", None, None))

# Print the device information
print_device_info(device_info, dataflash)
print_device_info(dev, dataflash)

# Convert the image
with handle_exceptions(evic.LogoConversionError):
click.echo("Converting logo...", nl=False)

# Check supported logo dimensions
logo_dimensions = device_info.logo_dimensions
if not logo_dimensions:
# Check supported logo size
try:
logosize = dev.supported_logo_size[dataflash.product_id]
except KeyError:
raise evic.LogoConversionError("Device doesn't support logos.")

# Perform the actual conversion
logo = evic.logo.fromimage(inputfile, invert)
if (logo.width, logo.height) != logo_dimensions:
if (logo.width, logo.height) != logosize:
raise evic.LogoConversionError("Device only supports {}x{} logos."
.format(*logo_dimensions))
.format(*logosize))

# We want to boot to LDROM on restart
if not dev.ldrom:
Expand Down Expand Up @@ -325,19 +383,61 @@ def dumpdataflash(output, noverify):
# Read the data flash
dataflash = read_dataflash(dev, noverify)

# Get the device info
device_info = dev.devices.get(dataflash.product_id,
DeviceInfo("Unknown device", None, None))

# Print the device information
print_device_info(device_info, dataflash)
print_device_info(dev, dataflash)

# Write the data flash to the file
with handle_exceptions(IOError):
click.echo("Writing data flash to the file...", nl=False)
output.write(dataflash.array)


@usb.command('fmcread')
@click.option('--output', '-o', type=click.File('wb'), required=True)
@click.option('--start', '-s', type=click.INT, required=True)
@click.option('--length', '-l', type=click.INT, required=True)
def fmcread(output, start, length):
"""Write device flash memory to a file."""

dev = evic.HIDTransfer()

# Connect the device
connect(dev)

# Print the USB info of the device
print_usb_info(dev)

# Read the data flash
fmemory = fmc_read(dev, start, length)

# Write the data flash to the file
with handle_exceptions(IOError):
click.echo("Writing flash memory to the file...", nl=False)
output.write(fmemory)


@usb.command('screenshot')
@click.option('--output', '-o', type=click.File('wb'), required=True)
def screenshot(output):
"""Take a screenshot."""

dev = evic.HIDTransfer()

# Connect the device
connect(dev)

# Read the screen data
data = dev.read_screen()

# create the image from screen data
im = Image.fromstring("1",(64,128),bytes(data))

# Write the image to the file
with handle_exceptions(IOError):
click.echo("Writing image to the file...", nl=False)
im.save(output,"PNG")


@usb.command('reset-dataflash')
def resetdataflash():
"""Reset device data flash."""
Expand Down
7 changes: 7 additions & 0 deletions evic/dataflash.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class DataFlash(binstruct.StructTemplate):
fw_version = binstruct.Int32Field(256)
ldrom_version = binstruct.Int32Field(260)

df_year = binstruct.Int16Field(320)
df_month = binstruct.Int8Field(322)
df_day = binstruct.Int8Field(323)
df_hour = binstruct.Int8Field(324)
df_minute = binstruct.Int8Field(325)
df_second = binstruct.Int8Field(326)

def verify(self, checksum):
"""Verifies the data flash against given checksum.

Expand Down
Loading