Beispiel #1
0
def _build_reflash_script_action(target, source, env):
    """Create a TRUB script containing tile and controller reflashes and/or sensorgraph

    If the app_info is provided, then the final source file will be a sensorgraph.
    All subsequent files in source must be in intel hex format. This is guaranteed
    by the ensure_image_is_hex call in build_update_script.
    """

    out_path = str(target[0])
    source = [str(x) for x in source]
    records = []

    #Update application firmwares
    if env['SLOTS'] is not None:
        for (controller, slot_id), image_path in zip(env['SLOTS'], source):
            hex_data = IntelHex(image_path)
            hex_data.padding = 0xFF

            offset = hex_data.minaddr()
            bin_data = bytearray(
                hex_data.tobinarray(offset, hex_data.maxaddr()))

            if controller:
                record = ReflashControllerRecord(bin_data, offset)
            else:
                record = ReflashTileRecord(slot_id, bin_data, offset)

            records.append(record)

    #Update sensorgraph
    if env['UPDATE_SENSORGRAPH']:
        sensor_graph_file = source[-1]
        sensor_graph = compile_sgf(sensor_graph_file)
        output = format_script(sensor_graph)
        records += UpdateScript.FromBinary(output).records

    #Update App and OS Tag
    os_info = env['OS_INFO']
    app_info = env['APP_INFO']
    if os_info is not None:
        os_tag, os_version = os_info
        records.append(SetDeviceTagRecord(os_tag=os_tag,
                                          os_version=os_version))
    if app_info is not None:
        app_tag, app_version = app_info
        records.append(
            SetDeviceTagRecord(app_tag=app_tag, app_version=app_version))

    script = UpdateScript(records)

    with open(out_path, "wb") as outfile:
        outfile.write(script.encode())
Beispiel #2
0
def format_script(sensor_graph):
    """Create a binary script containing this sensor graph.

    This function produces a repeatable script by applying a known sorting
    order to all constants and config variables when iterating over those
    dictionaries.

    Args:
        sensor_graph (SensorGraph): the sensor graph that we want to format

    Returns:
        bytearray: The binary script data.

    """

    records = []

    records.append(SetGraphOnlineRecord(False, address=8))
    records.append(ClearDataRecord(address=8))
    records.append(ResetGraphRecord(address=8))

    for node in sensor_graph.nodes:
        records.append(AddNodeRecord(str(node), address=8))

    for streamer in sensor_graph.streamers:
        records.append(AddStreamerRecord(streamer, address=8))

    for stream, value in sorted(sensor_graph.constant_database.items(),
                                key=lambda x: x[0].encode()):
        records.append(SetConstantRecord(stream, value, address=8))

    records.append(PersistGraphRecord(address=8))

    records.append(ClearConfigVariablesRecord())
    for slot in sorted(sensor_graph.config_database, key=lambda x: x.encode()):
        for config_id in sorted(sensor_graph.config_database[slot]):
            config_type, value = sensor_graph.config_database[slot][config_id]
            byte_value = _convert_to_bytes(config_type, value)

            records.append(SetConfigRecord(slot, config_id, byte_value))

    # If we have an app tag and version set program them in
    app_tag = sensor_graph.metadata_database.get('app_tag')
    app_version = sensor_graph.metadata_database.get('app_version')

    if app_tag is not None:
        records.append(
            SetDeviceTagRecord(app_tag=app_tag, app_version=app_version))

    script = UpdateScript(records)
    return script.encode()
Beispiel #3
0
    def __init__(self, args):
        if 'file' not in args:
            raise ArgumentError(
                "SendOTAScriptStep required parameters missing",
                required=["file"],
                args=args)

        self._file = args['file']
        self._no_reboot = args.get('no_reboot', False)

        with open(self._file, "rb") as infile:
            data = infile.read()
            self._script = UpdateScript.FromBinary(data)
Beispiel #4
0
    def trigger_script(self):
        """Actually process a script."""

        if self.bridge_status not in (BRIDGE_STATUS.RECEIVED,):
            return [1] #FIXME: State change

        # This is asynchronous in real life so just cache the error
        try:
            self.parsed_script = UpdateScript.FromBinary(self._device.script)
            #self._run_script()

            self.bridge_status = BRIDGE_STATUS.IDLE
        except Exception as exc:
            self._logger.exception("Error parsing script streamed to device")
            self.script_error = exc
            self.bridge_error = 1 # FIXME: Error code

        return [0]
Beispiel #5
0
    def _apply_ota_update(self, blob):
        """"Attempt to apply script to device using the device_updater app"""

        updater = self._hw.app(name='device_updater')
        update_script = UpdateScript.FromBinary(blob)
        updater.run_script(update_script)
Beispiel #6
0
    def _restore_script(cls, b64encoded):
        if b64encoded is None:
            return None

        encoded = base64.b64decode(b64encoded)
        return UpdateScript.FromBinary(encoded)