Source code for nat20.link

"""
Contains the messy details of communicating with a pixels die.
"""
import asyncio
import collections
import inspect
import logging
from typing import Callable, TypeVar

import bleak

from .constants import CHARI_NOTIFY, CHARI_WRITE
from .msglib import Message, pack, unpack

LOG = logging.getLogger(__name__)


def _call_or_task(func, *pargs, **kwargs):
    """
    Calls the given function. Async functions are wrapped in a Task.
    """
    rv = func(*pargs, **kwargs)
    if inspect.isawaitable(rv):
        asyncio.ensure_future(rv)


async def _get_real_mtu(client):
    # https://github.com/hbldh/bleak/blob/master/examples/mtu_size.py
    if client._backend.__class__.__name__ == "BleakClientBlueZDBus":
        await client._backend._acquire_mtu()

    return client.mtu_size

ReplyKind = TypeVar('R', bound=Message)