2019-05-26 23:10:00 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Demonstrates the use of otatool.py, a tool for performing ota partition level
|
|
|
|
# operations.
|
|
|
|
#
|
|
|
|
# Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http:#www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-01-25 21:49:01 -05:00
|
|
|
import argparse
|
2019-05-26 23:10:00 -04:00
|
|
|
import os
|
|
|
|
import re
|
2021-01-25 21:49:01 -05:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2019-05-26 23:10:00 -04:00
|
|
|
from subprocess import CalledProcessError
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
import serial
|
|
|
|
|
2019-05-26 23:10:00 -04:00
|
|
|
|
|
|
|
def get_running_partition(port=None):
|
|
|
|
# Monitor the serial output of target device. The firmware outputs the currently
|
|
|
|
# running partition
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
IDF_PATH = os.path.expandvars('$IDF_PATH')
|
2019-05-26 23:10:00 -04:00
|
|
|
sys.path.append(os.path.join(IDF_PATH, 'components', 'esptool_py', 'esptool'))
|
|
|
|
import esptool
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
ESPTOOL_PY = os.path.join(IDF_PATH, 'components', 'esptool_py', 'esptool', 'esptool.py')
|
2019-05-26 23:10:00 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
baud = os.environ.get('ESPTOOL_BAUD', esptool.ESPLoader.ESP_ROM_BAUD)
|
2019-05-26 23:10:00 -04:00
|
|
|
|
|
|
|
if not port:
|
2021-01-25 21:49:01 -05:00
|
|
|
error_message = 'Unable to obtain default target device port.\nSerial log:\n\n'
|
2019-05-26 23:10:00 -04:00
|
|
|
try:
|
|
|
|
# Check what esptool.py finds on what port the device is connected to
|
2021-01-25 21:49:01 -05:00
|
|
|
output = subprocess.check_output([sys.executable, ESPTOOL_PY, 'chip_id']) # may raise CalledProcessError
|
|
|
|
pattern = r'Serial port ([\S]+)'
|
2019-05-26 23:10:00 -04:00
|
|
|
pattern = re.compile(pattern.encode())
|
|
|
|
|
|
|
|
port = re.search(pattern, output).group(1) # may raise AttributeError
|
|
|
|
except CalledProcessError as e:
|
|
|
|
raise Exception(error_message + e.output)
|
|
|
|
except AttributeError:
|
|
|
|
raise Exception(error_message + output)
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
serial_instance = serial.serial_for_url(port.decode('utf-8'), baud, do_not_open=True)
|
2019-05-26 23:10:00 -04:00
|
|
|
|
|
|
|
serial_instance.dtr = False
|
|
|
|
serial_instance.rts = False
|
|
|
|
|
|
|
|
serial_instance.rts = True
|
|
|
|
serial_instance.open()
|
|
|
|
serial_instance.rts = False
|
|
|
|
|
|
|
|
# Read until example end and find the currently running partition string
|
2021-01-25 21:49:01 -05:00
|
|
|
content = serial_instance.read_until(b'Example end')
|
|
|
|
pattern = re.compile(b'Running partition: ([a-z0-9_]+)')
|
2019-05-26 23:10:00 -04:00
|
|
|
running = re.search(pattern, content).group(1)
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
return running.decode('utf-8')
|
2019-05-26 23:10:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
2021-01-25 21:49:01 -05:00
|
|
|
parser.add_argument('--port', default=None)
|
2019-05-26 23:10:00 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
try:
|
|
|
|
res = get_running_partition(args.port)
|
|
|
|
except Exception as e:
|
|
|
|
print(e.message)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
print(res)
|
|
|
|
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
if __name__ == '__main__':
|
2019-05-26 23:10:00 -04:00
|
|
|
main()
|