├── .github └── workflows │ ├── ci.yml │ └── release.yml ├── .gitignore ├── .gitlab-ci.yml ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── central_dispatch.py ├── dispatch.py ├── dispatches ├── __init__.py ├── flash_dispatch.py └── monitor_dispatch.py ├── main.py ├── main.spec ├── message ├── __init__.py └── message_protocol.py ├── requirements.txt ├── singleton.py ├── test ├── __ init __.py ├── test_flash.py ├── test_monitor.py └── test_monitor_dispatch.py └── tools └── monitor.py /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | esp-iwidc: 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - name: Clone Repository 17 | uses: actions/checkout@v2 18 | with: 19 | submodules: "recursive" 20 | 21 | - name: Setup Python ${{ matrix.python-version }} 22 | uses: actions/setup-python@v1 23 | with: 24 | python-version: 3.8 25 | 26 | - name: Install Python Dependencies 27 | run: | 28 | python -m pip install --upgrade pip 29 | pip install -r requirements.txt 30 | 31 | - name: Run tests 32 | run: | 33 | python -m unittest discover -v test "*test*.py" 34 | 35 | - name: Package Application 36 | uses: JackMcKew/pyinstaller-action-windows@main 37 | with: 38 | path: . 39 | 40 | - name: Upload Windows executable 41 | uses: actions/upload-artifact@v1 42 | with: 43 | name: esp-iwidc 44 | path: dist/windows -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | tags: 6 | - "v*" 7 | 8 | jobs: 9 | build: 10 | name: Upload release 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Checkout code 14 | uses: actions/checkout@v2 15 | with: 16 | submodules: "recursive" 17 | 18 | - name: Setup Python ${{ matrix.python-version }} 19 | uses: actions/setup-python@v1 20 | with: 21 | python-version: 3.8 22 | 23 | - name: Install Python Dependencies 24 | run: | 25 | python -m pip install --upgrade pip 26 | pip install -r requirements.txt 27 | 28 | - name: Run tests 29 | run: | 30 | python -m unittest discover -v test "*test*.py" 31 | 32 | - name: Package Application 33 | uses: JackMcKew/pyinstaller-action-windows@main 34 | with: 35 | path: . 36 | 37 | - name: Create Release 38 | id: create_release 39 | uses: actions/create-release@v1.0.0 40 | env: 41 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 42 | with: 43 | tag_name: ${{ github.ref }} 44 | release_name: ESP IDF Web IDE Desktop Companion Release ${{ github.ref }} 45 | draft: true 46 | body: | 47 | ### Release Highlights 48 | 49 | ### Features & Enhancements 50 | 51 | 52 | 53 | ### Bug Fixes 54 | 55 | 56 | 57 | - name: Determine version 58 | id: version 59 | run: "echo ::set-output name=version::${GITHUB_REF:11}" 60 | 61 | - name: Upload release asset 62 | id: upload-release-asset 63 | uses: actions/upload-release-asset@v1.0.1 64 | env: 65 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 66 | with: 67 | upload_url: ${{ steps.create_release.outputs.upload_url }} 68 | asset_path: ./dist/windows/main.exe 69 | asset_name: esp-iwidc-${{ steps.version.outputs.version }} 70 | asset_content_type: application/zip -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | esp-iwidc 2 | *.pyc 3 | *.bin 4 | *.log 5 | bins/ 6 | certs/ 7 | __pycache__ 8 | build 9 | dist 10 | venv -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | stages: 2 | - test 3 | 4 | test: 5 | image: python:3.9-slim-buster 6 | stage: test 7 | tags: 8 | - internet 9 | - amd64 10 | - build 11 | script: 12 | - python -m pip install -r requirements.txt 13 | - python -m unittest discover -v test "*test*.py" 14 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.testing.unittestArgs": [ 3 | "-v", 4 | "-s", 5 | "./test", 6 | "-p", 7 | "*test*.py" 8 | ], 9 | "python.testing.pytestEnabled": false, 10 | "python.testing.unittestEnabled": true 11 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![GitHub release](https://img.shields.io/github/release/espressif/iwidc.svg?style=flat-square&label=Latest%20release)](https://github.com/espressif/iwidc/releases/latest) 2 | [![Release workflow](https://img.shields.io/github/workflow/status/espressif/iwidc/Release?label=Release%20Status)](https://github.com/espressif/iwidc/actions?workflow=Release) 3 | 4 | # ESP IDF Web IDE Desktop Companion (ESP-IWIDC) 5 | 6 | ESP IWI-DC is a remote flasher, monitor and band of tools for the client side application for bridging the web-ide based flash and monitor. 7 | 8 | For the best results use IWIDC with Chrome Web Browser. 9 | 10 | ## Getting started 11 | 12 | - `git clone --recursive https://github.com/espressif/iwidc.git` 13 | - `cd esp-iwidc` 14 | - Use python 3.x or create a new virtual environment. 15 | - `pip3 install -r requirements.txt` 16 | - Run `python3 main.py` to see available serial ports. 17 | - Run `python3 main.py --port [SERIAL_PORT_OF_ESP_32]` 18 | 19 | Use python 3.x: 20 | - `pip3 install -r requirements.txt` 21 | 22 | Run: 23 | - `python3 main.py --port [SERIAL_PORT_OF_ESP_32]` 24 | 25 | 26 | ### With Pipenv (to isolate environment of the package / easy install) 27 | 28 | Clone repo: 29 | - `git clone --recursive https://github.com/espressif/iwidc.git` 30 | - `cd esp-iwidc` 31 | 32 | Use python 3.x: 33 | - `python3 -m pip install pipenv` 34 | - `python3 -m pipenv lock` 35 | - `python3 -m pipenv install --ignore-pipfile` 36 | - `python3 -m pipenv shell` 37 | 38 | Run: 39 | - `python3 main.py` to see available serial ports. 40 | - `python3 main.py --port [SERIAL_PORT_OF_ESP_32]` to start desktop companion. 41 | 42 | 43 | ## Windows users - How to find your port number 44 | - connect device 45 | - open command line and type `mode` 46 | 47 | Other option: Open Device manager and expand Ports (COM & LPT). 48 | 49 | If device is not visible, check Espressif docs article [Establish Serial Connection with ESP32](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/establish-serial-connection.html) 50 | 51 | ### Testing 52 | 53 | In a terminal run `python3 -m unittest discover -v test "*test*.py"` or from Visual Studio Code with `ms-python.python` Python extension, you can run test and see the output in the `Python Test Log` output. 54 | 55 | ## Build executable with PyInstaller 56 | 57 | Run (either using system python or the virtual environment from before): 58 | 59 | - `pip install pyinstaller` 60 | - `pyinstaller --onefile main.py` 61 | 62 | and find the executable in `dist/main.exe`. 63 | 64 | 65 | ### Windows driver installation 66 | 67 | - download and unzip [IDF-ENV](https://github.com/espressif/idf-env) 68 | - open a PowerShell under Administrator 69 | - run `idf-env driver install --espressif --ftdi --silabs` 70 | 71 | - unplug & plug device to let the system apply the driver 72 | -------------------------------------------------------------------------------- /central_dispatch.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from dispatch import DispatchHandler, Dispatch 3 | from dispatches import flash_dispatch, monitor_dispatch 4 | 5 | 6 | class UnhandledDispatch(DispatchHandler): 7 | def set_next(self, handler: Dispatch) -> Dispatch: 8 | # throw exception 9 | pass 10 | 11 | async def handle(self, ws, path: str, message: dict) -> bool: 12 | logging.error( 13 | "[Unhandled]: No dispatch handler could handle {}".format(path)) 14 | 15 | async def handle_internal(self, ws, path: str, message: dict) -> bool: 16 | pass 17 | 18 | 19 | class InitialDispatchHandler(DispatchHandler): 20 | async def handle(self, ws, path: str, message: dict) -> bool: 21 | result = await self.handle_internal(ws, path, message) 22 | if result == True: 23 | return await super().handle(ws, path, message) 24 | else: 25 | logging.error( 26 | "[Error]: Message protocol mismatch, canceling and exiting...") 27 | exit(1) 28 | 29 | async def handle_internal(self, ws, path, message: dict) -> bool: 30 | logging.info("[Internal]: Got Request to be parsed {}".format(path)) 31 | return True 32 | # check the validity of message protocol and other protocol specification 33 | 34 | 35 | def get_dispatch_handler() -> Dispatch: 36 | initial_handler = InitialDispatchHandler() 37 | flash = flash_dispatch.FlashDispatchHandler() 38 | monitor = monitor_dispatch.MonitorDispatchHandler() 39 | unhandled_handler = UnhandledDispatch() 40 | 41 | initial_handler.set_next(flash).set_next( 42 | monitor).set_next(unhandled_handler) 43 | 44 | return initial_handler 45 | -------------------------------------------------------------------------------- /dispatch.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | from abc import ABC, abstractmethod 3 | from typing import Any, Optional 4 | 5 | 6 | class Dispatch(ABC): 7 | @abstractmethod 8 | def set_next(self, handler: Dispatch) -> Dispatch: 9 | pass 10 | 11 | @abstractmethod 12 | async def handle(self, ws, path: str, message: dict) -> Optional[bool]: 13 | pass 14 | 15 | @abstractmethod 16 | async def handle_internal(self, ws, path: str, message: dict) -> Optional[bool]: 17 | pass 18 | 19 | 20 | class DispatchHandler(Dispatch): 21 | _next_handler: Dispatch = None 22 | _path: str = None 23 | _websocket = None 24 | 25 | def set_next(self, handler: Dispatch) -> Dispatch: 26 | self._next_handler = handler 27 | return handler 28 | 29 | async def handle(self, ws, path: str, message: dict) -> bool: 30 | if self._path == path: 31 | return await self.handle_internal(ws, path, message) 32 | elif self._next_handler: 33 | return await self._next_handler.handle(ws, path, message) 34 | return None 35 | -------------------------------------------------------------------------------- /dispatches/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/espressif/iwidc/ae2781b4eb1b4b07cd1b5116057c1904d1fd21e0/dispatches/__init__.py -------------------------------------------------------------------------------- /dispatches/flash_dispatch.py: -------------------------------------------------------------------------------- 1 | import os 2 | import logging 3 | import esptool 4 | from dispatch import DispatchHandler 5 | from message.message_protocol import MessageProtocol 6 | 7 | 8 | class FlashDispatchHandler(DispatchHandler): 9 | _TMP_DIR_NAME = "bins" 10 | 11 | def __init__(self): 12 | self._path = "/flash" 13 | 14 | def get_esptool_args(self, serial_port: str): 15 | esptool_params = [ 16 | "-p", serial_port, 17 | "-b", "115200", 18 | "--after", "hard_reset", 19 | "write_flash", 20 | "--flash_mode", "dio", 21 | "--flash_size", "detect", 22 | "--flash_freq", "40m" 23 | ] 24 | return esptool_params 25 | 26 | async def handle_internal(self, ws, path: str, message: dict) -> bool: 27 | if message["messageType"] == "flash": 28 | self.create_temp_dir(self._TMP_DIR_NAME) 29 | 30 | logging.info("[Flash ⚡️]: New request received") 31 | 32 | esptool_params = self.get_esptool_args(message["serial_port"]) 33 | sections = message["sections"] 34 | 35 | for section in sections: 36 | bin_path = os.path.join(self._TMP_DIR_NAME, section["name"]) 37 | with open(bin_path, 'wb') as fp: 38 | fp.write(bytes(section["bin"]["data"])) 39 | esptool_params.append(section["offset"]) 40 | esptool_params.append(bin_path) 41 | 42 | msg = None 43 | 44 | try: 45 | esptool.main(esptool_params) 46 | except Exception as e: 47 | msg = MessageProtocol("flash_error") 48 | msg.add("error", str(e)) 49 | logging.error("❌ [Flash ⚡️]: Failed!") 50 | else: 51 | msg = MessageProtocol("flash_done") 52 | logging.info("[Flash ⚡️]: Done!") 53 | finally: 54 | encoded_msg = msg.encode() 55 | await ws.send(encoded_msg) 56 | else: 57 | logging.info( 58 | "[Flash ⚠️]: Can't be processed by the Flash Dispatch") 59 | 60 | def create_temp_dir(self, folder_name: str) -> bool: 61 | try: 62 | os.mkdir(folder_name) 63 | return True 64 | except: 65 | return False 66 | -------------------------------------------------------------------------------- /dispatches/monitor_dispatch.py: -------------------------------------------------------------------------------- 1 | import os 2 | import logging 3 | from dispatch import DispatchHandler 4 | from message.message_protocol import MessageProtocol 5 | from tools.monitor import Monitor 6 | 7 | 8 | class MonitorDispatchHandler(DispatchHandler): 9 | m: Monitor = None 10 | 11 | def __init__(self): 12 | self._path = "/monitor" 13 | 14 | async def sendMessage(self, ws, msg: MessageProtocol): 15 | await ws.send(msg.encode()) 16 | 17 | async def handle_internal(self, ws, path: str, message: dict) -> bool: 18 | async def on_message_callback(msg): 19 | m = MessageProtocol("monitor") 20 | m.add("monitor-type", "message-from-chip") 21 | m.add("message", msg) 22 | 23 | await self.sendMessage(ws, m) 24 | 25 | if message["messageType"] == "monitor": 26 | logging.info("[Monitor 👀]: New request received") 27 | if message["monitor-type"] == "start": 28 | if not self.m: 29 | self.m = Monitor( 30 | message["serial_port"], on_message_callback) 31 | await self.m.start() 32 | else: 33 | await self.m.start() 34 | elif message["monitor-type"] == "stop": 35 | if self.m and self.m.is_monitoring() == True: 36 | self.m.stop() 37 | self.m = None 38 | elif message["monitor-type"] == "message-to-chip": 39 | if self.m and self.m.is_monitoring() == True: 40 | self.m.send_message_to_chip(message["message"]) 41 | else: 42 | self.m = Monitor( 43 | message["serial_port"], on_message_callback) 44 | self.m.send_message_to_chip(message["message"]) 45 | else: 46 | logging.info( 47 | "[Monitor 👀]: Error, unrecognized message type, skipping!") 48 | logging.debug(message) 49 | else: 50 | logging.warning( 51 | "[Monitor ⚠️]: Can't be processed by the Monitor Dispatch") 52 | errMsg = MessageProtocol("monitor") 53 | errMsg.add("message", "messageType is not supported") 54 | await self.sendMessage(ws, errMsg) 55 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import asyncio 3 | import sys 4 | import os 5 | import bson 6 | import websockets 7 | import argparse 8 | import logging 9 | import serial.tools.list_ports 10 | from central_dispatch import get_dispatch_handler 11 | 12 | 13 | config = {} 14 | 15 | 16 | async def central_dispatch(websocket, path): 17 | while True: 18 | data = await websocket.recv() 19 | logging.info("[Message 📦]: Received of length: {} and type: {}".format( 20 | len(data), type(data))) 21 | try: 22 | data = bson.BSON.decode(data) 23 | except: 24 | logging.error( 25 | "[Error]: Error while parsing bson to dict, only bson encoded") 26 | else: 27 | logging.info( 28 | "Finally received the message from the websocket client") 29 | data["serial_port"] = config["serial_port"] 30 | dispatch_handler = get_dispatch_handler() 31 | await dispatch_handler.handle(websocket, path, data) 32 | 33 | 34 | def main(host: str, port: int, serial_port: str): 35 | config["serial_port"] = serial_port 36 | if sys.platform == "win32" and sys.version_info >= (3, 8, 0): 37 | asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) 38 | 39 | event_loop = asyncio.get_event_loop() 40 | 41 | start_server = websockets.serve( 42 | central_dispatch, host=host, port=port, max_size=2**32) 43 | 44 | logging.info('[Start]: Connection started!') 45 | 46 | event_loop.run_until_complete(start_server) 47 | event_loop.run_forever() 48 | 49 | 50 | if __name__ == "__main__": 51 | 52 | logging.basicConfig( 53 | level=logging.INFO, 54 | format="%(asctime)s : %(levelname)s : %(filename)s : %(funcName)s : %(process)d : %(thread)d : %(message)s", 55 | handlers=[logging.FileHandler("iwidc.log"), logging.StreamHandler()] 56 | ) 57 | 58 | comports = [comport.device for comport in serial.tools.list_ports.comports()] 59 | comports.append('test') 60 | # ask user to enter the serial port 61 | parser = argparse.ArgumentParser( 62 | description='Desktop bridge for idf-web to flash and monitor esp-32 chip') 63 | parser.add_argument("--port", 64 | choices=comports, 65 | required=True, type=str, help="Comport where you want to flash or monitor") 66 | 67 | args = parser.parse_args() 68 | try: 69 | main("127.0.0.1", 3362, args.port) 70 | except websockets.exceptions.WebSocketException: 71 | logging.warning("[idf-web-ide]: Connection Closed OK 🖐🏼") 72 | except KeyboardInterrupt: 73 | logging.info('[Exit]: Bye!') 74 | try: 75 | sys.exit(0) 76 | except SystemExit: 77 | os._exit(0) 78 | except OSError as err: 79 | logging.error("OS error: {0}".format(err)) 80 | except Exception as err: 81 | logging.error("Unhandled Exception: {0}".format(err)) 82 | 83 | -------------------------------------------------------------------------------- /main.spec: -------------------------------------------------------------------------------- 1 | # -*- mode: python ; coding: utf-8 -*- 2 | 3 | 4 | block_cipher = None 5 | 6 | 7 | a = Analysis( 8 | ['main.py'], 9 | pathex=[], 10 | binaries=[], 11 | datas=[], 12 | hiddenimports=[], 13 | hookspath=[], 14 | runtime_hooks=[], 15 | excludes=[], 16 | win_no_prefer_redirects=False, 17 | win_private_assemblies=False, 18 | cipher=block_cipher, 19 | noarchive=False, 20 | ) 21 | pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) 22 | 23 | exe = EXE( 24 | pyz, 25 | a.scripts, 26 | a.binaries, 27 | a.zipfiles, 28 | a.datas, 29 | [], 30 | name='main', 31 | debug=False, 32 | bootloader_ignore_signals=False, 33 | strip=False, 34 | upx=True, 35 | upx_exclude=[], 36 | runtime_tmpdir=None, 37 | console=True, 38 | disable_windowed_traceback=False, 39 | argv_emulation=False, 40 | target_arch=None, 41 | codesign_identity=None, 42 | entitlements_file=None, 43 | ) 44 | -------------------------------------------------------------------------------- /message/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/espressif/iwidc/ae2781b4eb1b4b07cd1b5116057c1904d1fd21e0/message/__init__.py -------------------------------------------------------------------------------- /message/message_protocol.py: -------------------------------------------------------------------------------- 1 | import bson 2 | import uuid 3 | 4 | class MessageProtocol: 5 | _message = None 6 | def __init__(self, messageType: str): 7 | self._message = { 8 | "messageType": messageType, 9 | "version": "0.0.1", 10 | "uuid": str(uuid.uuid4()) 11 | } 12 | def add(self, key: str, val): 13 | self._message[key] = val 14 | def encode(self): 15 | return bson.BSON.encode(self._message) 16 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | astroid==2.3.3 2 | autopep8==1.4.4 3 | esptool==3.0 4 | isort==4.3.21 5 | lazy-object-proxy==1.4.3 6 | mccabe==0.6.1 7 | pycodestyle==2.5.0 8 | pylint==2.4.3 9 | pymongo==3.9.0 10 | pyserial==3.4 11 | python-dateutil==2.8.1 12 | six==1.13.0 13 | typed-ast==1.4.3 14 | websockets==8.1 15 | wrapt==1.11.2 16 | -------------------------------------------------------------------------------- /singleton.py: -------------------------------------------------------------------------------- 1 | class Singleton(type): 2 | def __init__(cls, name, bases, attrs, **kwargs): 3 | super().__init__(name, bases, attrs) 4 | cls._instance = None 5 | 6 | def __call__(cls, *args, **kwargs): 7 | if cls._instance is None: 8 | cls._instance = super().__call__(*args, **kwargs) 9 | return cls._instance 10 | -------------------------------------------------------------------------------- /test/__ init __.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/espressif/iwidc/ae2781b4eb1b4b07cd1b5116057c1904d1fd21e0/test/__ init __.py -------------------------------------------------------------------------------- /test/test_flash.py: -------------------------------------------------------------------------------- 1 | from dispatches import flash_dispatch 2 | import unittest 3 | 4 | 5 | class TestFlash(unittest.TestCase): 6 | def test_esptool_params(self): 7 | flash = flash_dispatch.FlashDispatchHandler() 8 | params = flash.get_esptool_args("test") 9 | expected = [ 10 | "-p", "test", 11 | "-b", "115200", 12 | "--after", "hard_reset", 13 | "write_flash", 14 | "--flash_mode", "dio", 15 | "--flash_size", "detect", 16 | "--flash_freq", "40m" 17 | ] 18 | self.assertListEqual(params, expected) 19 | -------------------------------------------------------------------------------- /test/test_monitor.py: -------------------------------------------------------------------------------- 1 | 2 | import asyncio 3 | import unittest 4 | import tools.monitor 5 | from unittest.mock import patch 6 | 7 | 8 | class MockSerial: 9 | port = "init_port" 10 | timeout = 1 11 | baudrate = 9600 12 | is_open = False 13 | curr_line = "" 14 | 15 | def open(self): 16 | self.is_open = True 17 | return True 18 | 19 | def close(): 20 | return True 21 | 22 | def readline(): 23 | return "message from mock serial" 24 | 25 | def write(self, msg: str): 26 | self.curr_line = msg 27 | return "written message: {}".format(msg) 28 | 29 | def flush(self): 30 | return None 31 | 32 | 33 | class TestMonitor(unittest.TestCase): 34 | msg = None 35 | 36 | async def sendMessage(self, msg: str): 37 | print(msg) 38 | self.msg = msg 39 | 40 | def test_monitor_send_message(self): 41 | async def async_test(): 42 | with patch("tools.monitor.serial.Serial") as mock_serial: 43 | mock_serial.return_value = MockSerial 44 | mon = tools.monitor.Monitor("test", self.sendMessage) 45 | mon._serial = MockSerial() 46 | mon._serial.port = "test" 47 | mon._serial.timeout = 1 48 | mon._serial.baudrate = 115200 49 | mon._serial.open() 50 | mon.send_message_to_chip("test-message") 51 | self.assertEqual(mon._serial.curr_line, "test-message".encode("utf8")) 52 | return asyncio.get_event_loop().run_until_complete(async_test()) 53 | -------------------------------------------------------------------------------- /test/test_monitor_dispatch.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import asyncio 3 | from message.message_protocol import MessageProtocol 4 | from dispatches import monitor_dispatch 5 | from unittest.mock import patch 6 | 7 | 8 | class MockMonitor: 9 | is_monitor_started = False 10 | 11 | def __init__(self, port: str, on_message_callback): 12 | self._port = port 13 | self._on_message_callback = on_message_callback 14 | 15 | async def start(self): 16 | self.is_monitor_started = True 17 | if callable(self._on_message_callback): 18 | await self._on_message_callback("monitor has started") 19 | return True 20 | 21 | def is_monitoring(self): 22 | return self.is_monitor_started 23 | 24 | def send_message_to_chip(self, msg: str): 25 | return msg 26 | 27 | 28 | class TestMonitorDispatch(unittest.TestCase): 29 | msg = None 30 | 31 | async def sendMessage(self, msg: MessageProtocol): 32 | self.msg = msg 33 | 34 | def test_monitor_dispatch(self): 35 | async def async_test(): 36 | with patch.object(monitor_dispatch, "Monitor") as mock_monitor: 37 | mock_monitor.return_value = MockMonitor( 38 | "test", self.sendMessage) 39 | msg = MessageProtocol("monitor") 40 | msg.add("monitor-type", "start") 41 | msg.add("serial_port", "test") 42 | dispatcher = monitor_dispatch.MonitorDispatchHandler() 43 | await dispatcher.handle_internal(None, "/monitor", msg._message) 44 | self.assertEqual(self.msg, "monitor has started") 45 | return asyncio.get_event_loop().run_until_complete(async_test()) 46 | -------------------------------------------------------------------------------- /tools/monitor.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import serial 3 | import threading 4 | import logging 5 | from time import sleep 6 | from singleton import Singleton 7 | 8 | 9 | class Monitor(metaclass=Singleton): 10 | _port = None 11 | _serial = None 12 | _on_message_callback = None 13 | _thread = None 14 | 15 | def __init__(self, port: str, on_message_callback): 16 | self._port = port 17 | self._on_message_callback = on_message_callback 18 | 19 | def is_monitoring(self): 20 | if self._serial: 21 | return self._serial.is_open 22 | return False 23 | 24 | def stop(self): 25 | if self.is_monitoring: 26 | logging.debug("[{}]: Stoping the Serial Monitoring at port {}".format( 27 | __file__, self._port)) 28 | pass 29 | 30 | async def start(self): 31 | if self.is_monitoring() is False: 32 | logging.debug("[{}]: Starting serial monitor".format(__file__)) 33 | self._serial = serial.Serial() 34 | self._serial.port = self._port 35 | self._serial.timeout = 1 36 | self._serial.baudrate = 115200 37 | try: 38 | self._serial.open() 39 | except Exception: 40 | logging.error("[{}]: Failed to open the serial port {}".format( 41 | __file__, self._port)) 42 | else: 43 | logging.info( 44 | "[{}]: Established connection with the Chip".format(__file__)) 45 | self._thread = threading.Thread( 46 | target=self.send_message_from_chip) 47 | self._thread.start() 48 | return self._thread 49 | 50 | def send_message_from_chip(self): 51 | while True: 52 | data = self._serial.readline() 53 | logging.debug("[{}]: From_Chip: {}".format(__file__, data)) 54 | if callable(self._on_message_callback): 55 | asyncio.run(self._on_message_callback(data)) 56 | 57 | def send_message_to_chip(self, message: str): 58 | if self.is_monitoring: 59 | logging.debug("[{}]: To_Chip: {}".format(__file__, message)) 60 | self._serial.write(message.encode('utf8')) 61 | self._serial.flush() 62 | 63 | 64 | def on_message(message: str): 65 | print(message) 66 | 67 | 68 | if __name__ == "__main__": 69 | m = Monitor("/dev/cu.usbserial-00101414B", on_message) 70 | if m.is_monitoring() is False: 71 | m.start() 72 | sleep(20) 73 | m.stop() 74 | --------------------------------------------------------------------------------