# MangoLink Simulator 0.3.0 — Python desktop example

Mango Juice Tech | https://mangojuicetech.com/mangolink/example/

This source example acts as a virtual embedded device for MangoLink Android
0.3.0. It provides logs, telemetry and commands using MangoLink Protocol 1.
No ESP32 or other board is required. Sensor readings and LED state are simulated.

## What you need

- Python 3.10 or newer with Tk/Tcl support for the desktop window.
- A PC and phone on the same local network.
- MangoLink Android 0.3.0 to scan the pairing QR. Manual entry also works.

Extract the entire ZIP first; the Python files import each other. Open a terminal
in the extracted folder containing this README and mangolink_simulator.py.

## Windows PowerShell

Use a virtual environment. Calling its Python directly avoids PowerShell's
activation-script restrictions and leaves your system Python packages alone.

```powershell
py -3 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements-tls.txt -r requirements-qr.txt
.\.venv\Scripts\python.exe mangolink_simulator.py
```

For later launches, run only the last command. The included start-windows.bat
instead uses your system Python; install dependencies there if you use it.

## Linux and macOS

```bash
python3 -m venv .venv
.venv/bin/python -m pip install -r requirements-tls.txt -r requirements-qr.txt
.venv/bin/python mangolink_simulator.py
```

Install your operating system's Tk package if Python reports that tkinter is
missing. Linux may also require its Python venv package. Run the GUI on a desktop
with a display. The headless server below does not require Tk.

## Connect with TLS and one QR scan

1. Keep TLS selected, with port 8766. Click Create TLS certificate once, or use
   Browse to select a certificate/key pair you already created. Existing identity
   files are never automatically overwritten.
2. Press Start server. A random session token is shown. The default bind address
   0.0.0.0 listens on the PC's interfaces; it is not a phone destination.
3. Press Show pairing QR. Enter the PC's reachable LAN IP (find it using ipconfig
   on Windows) and press Generate / update QR code. Include session token is
   checked by default. The QR contains host, port, TLS certificate SHA-256 and
   token. The server's private key is never included.
4. On the phone, open Connection settings → Scan simulator QR code. Allow camera
   access, scan the trusted PC display, review the populated fields and Connect.
5. Try status, sensors, led on, led off, warn or error. Use the desktop controls
   to inject faults, drop clients or simulate reboot. These only affect simulated
   device state.

Only show the token-bearing QR to intended users. Closing the QR window does not
revoke its token. Restart the simulator to obtain a new random token if a QR is
exposed. Do not publish photos of your own QR or token.

Manual setup: enter the same mode, PC LAN IP, port, token and fingerprint on the
phone. Enter host first: changing it clears the fingerprint. Scanning fills fields
but never connects automatically. The phone still checks the exact certificate
before sending the token. A blank fingerprint needs a normally trusted server
certificate with a matching hostname/IP; this example's self-signed one needs a pin.

## Certificate storage

Windows default: %LOCALAPPDATA%\MangoJuiceTech\MangoLinkSimulator\certs
macOS: ~/Library/Application Support/MangoJuiceTech/MangoLinkSimulator/certs
Linux: $XDG_DATA_HOME/MangoJuiceTech/MangoLinkSimulator/certs, or
~/.local/share/MangoJuiceTech/MangoLinkSimulator/certs when unset.

The window shows full certificate/key paths. If creation is denied, use Choose
certificate folder to select a writable folder and try again. Do not run as
administrator to work around a bad path. Keep private keys on the PC. Certificates
are valid for one year; renew in a new folder and deliberately pair the new pin.

## Headless server

After creating a certificate in the default folder, run (Windows):

```powershell
.\.venv\Scripts\python.exe server.py --host 0.0.0.0 --port 8766
```

Or generate one without opening the GUI:

```powershell
.\.venv\Scripts\python.exe tls_support.py
.\.venv\Scripts\python.exe server.py --host 0.0.0.0
```

The CLI prints a token and public certificate fingerprint. Enter them manually on
the phone; Show pairing QR is a desktop GUI feature. Use --cert PATH --key PATH
to load a non-default pair. Use the corresponding .venv/bin/python on Linux/macOS.

For explicitly unencrypted lab testing:

```powershell
.\.venv\Scripts\python.exe server.py --mode tcp --host 0.0.0.0 --port 8765
```

Choose TCP on the phone too. No certificate is required in TCP mode, but tokens,
logs and commands travel in readable form. TLS failures never switch to TCP.

## Adapt the example

- server.py: authentication, protocol framing, periodic telemetry and commands.
- mangolink_simulator.py: native desktop controls and server lifecycle.
- tls_support.py: TLS configuration and local certificate creation.
- pairing.py: QR payload format and local QR display.
- docs/PROTOCOL.md and docs/PAIRING.md: wire and optical pairing specifications.

Start with Simulator.tick() for sample logs/telemetry and Simulator.command() for
commands. Retain authentication, frame limits and certificate validation when
adapting this to another device/server. An IP address alone does not make hardware
compatible; firmware must implement the documented protocol.

Dependencies are installed from PyPI using the requirements files. They are not
vendored in this ZIP; their respective licenses apply. Serving an existing TLS
identity or unencrypted TCP uses Python's standard library. Certificate creation
needs cryptography; displaying a QR needs qrcode.

## Checks

```powershell
.\.venv\Scripts\python.exe -m unittest -v test_server test_tls test_pairing
.\.venv\Scripts\python.exe -m unittest -v test_gui
```

The GUI checks require a desktop display. Source 0.3.0 passed 24 server/TLS/pairing
checks and 4 GUI checks on Windows and Linux. Physical-phone scanning still needs
testing. This is a developer example, not a production remote-access service.

## Troubleshooting and current limits

- Keep PC and phone on the same LAN. Allow Python on the private-network firewall
  if prompted. Guest Wi-Fi/client isolation, a VPN or a sleeping PC may block it.
- For Android's standard emulator use 10.0.2.2 instead of the PC LAN IP.
- Self-signed TLS requires the fingerprint; QR pairing transfers it without typing.
- Camera denial or no camera: use manual connection fields.
- Phone monitoring is foreground-only in 0.3.0; screen-off/background continuity
  is not implemented. Keep the app visible during this test.
- No cloud relay, automatic reconnect, durable log replay or ESP32 firmware yet.
- Keep this development listener on your trusted LAN, not the public internet.

source-manifest.json records the exact upstream commit and file checksums for
this download. The archive includes no certificates, private keys or real tokens.
Support: support@mangojuicetech.com
