# Copyright: (c) 2026, TeTeHacko # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) DOCUMENTATION = r""" module: meshcore_companion_acl short_description: Remote-CLI ACL of a MeshCore companion (contact flag 0x10), over its BLE link author: TeTeHacko (@TeTeHacko) description: - A companion (fw 1.18+, Solo 2.0+) runs a CLI command that arrives as TXT_TYPE_CLI_COMMAND only from a contact flagged "remote CLI" (ContactInfo flags bit 0x10). That flag IS its ACL, and the remote CLI can do everything (radio, name, PIN, reboot), so the set is converged exactly like a repeater's ACL - the listed keys get it, every other contact loses it. - Flags live on the companion and can only be changed over its own link, so this runs on a host with BLE in range (bleak, NUS, one write = one frame) and needs a bond with the node. A listed key the companion does not know is added as a chat contact; when its table is full, the contact heard longest ago that is neither a favourite nor in the list is removed to make room (what the firmware's own auto-add does) and reported in C(evicted). - Each contact is read, only its flags byte changed, and written back (CMD_ADD_UPDATE_CONTACT takes the CONTACT frame layout), so path, name and the other flag bits (favourite, telemetry, Solo's kept route) are kept. options: mac: {description: BLE MAC of the companion., type: str, required: true} admins: {description: Pubkeys (64 hex) that get remote CLI., type: list, elements: str, required: true} names: {description: pubkey -> name for contacts that have to be added., type: dict, default: {}} timeout: {description: Seconds for the whole BLE session., type: int, default: 120} """ EXAMPLES = r""" - name: Remote CLI only for these keys tth.meshcore.meshcore_companion_acl: mac: "AA:BB:CC:DD:EE:FF" admins: - "{{ my_phone_pubkey }}" names: "{{ my_phone_pubkey }}": my-phone """ import asyncio import re import struct from ansible.module_utils.basic import AnsibleModule try: from bleak import BleakClient HAS_BLEAK = True except ImportError: HAS_BLEAK = False RX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e" TX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e" CMD_APP_START = 1 CMD_GET_CONTACTS = 4 CMD_ADD_UPDATE_CONTACT = 9 CMD_REMOVE_CONTACT = 15 ERR_TABLE_FULL = 3 FLAG_FAV = 0x01 RESP_OK = 0 RESP_ERR = 1 RESP_CONTACTS_START = 2 RESP_CONTACT = 3 RESP_END = 4 RESP_SELF_INFO = 5 FLAG_REMOTE_CLI = 0x10 HEX64 = re.compile(r"^[0-9a-f]{64}$") # CONTACT frame: code pub[32] type flags out_path_len out_path[64] name[32] # last_advert[4] lat[4] lon[4] lastmod[4] OFF_FLAGS = 1 + 32 + 1 OFF_NAME = 1 + 32 + 3 + 64 OFF_LAST_ADVERT = OFF_NAME + 32 CONTACT_LEN = 1 + 32 + 3 + 64 + 32 + 16 class Ble: def __init__(self, client): self.c = client self.q = asyncio.Queue() async def start(self): await self.c.start_notify(TX, lambda _, d: self.q.put_nowait(bytes(d))) async def send(self, frame, want, timeout=10): await self.c.write_gatt_char(RX, frame, response=True) while True: f = await asyncio.wait_for(self.q.get(), timeout) if f and f[0] in want: return f def new_contact_frame(pubkey, name, flags): nm = name.encode()[:31].ljust(32, b"\0") return ( bytes([CMD_ADD_UPDATE_CONTACT]) + bytes.fromhex(pubkey) + bytes([1, flags, 0xFF]) + bytes(64) + nm + struct.pack("= CONTACT_LEN - 4: contacts[f[1:33].hex()] = f plan = [] # (pubkey, name, old_flags or None, new_flags) for pk, f in contacts.items(): has = bool(f[OFF_FLAGS] & FLAG_REMOTE_CLI) if has != (pk in want): plan.append( ( pk, f[OFF_NAME : OFF_NAME + 32].split(b"\0")[0].decode(errors="replace") or pk[:8], f[OFF_FLAGS], f[OFF_FLAGS] ^ FLAG_REMOTE_CLI, ) ) for pk, name in want.items(): if pk not in contacts and pk != self_pub: plan.append((pk, name, None, FLAG_REMOTE_CLI)) evicted = [] def victim(): cands = [ (struct.unpack_from("