Example #1
0
def main(block_name, input_device, output_device, block_size, sample_rate):

    # Initialise ConnectSDK
    sdk = ChirpConnect(block=block_name)
    print(str(sdk))
    print('Protocol: {protocol} [v{version}]'.format(
        protocol=sdk.protocol_name, version=sdk.protocol_version))
    print(sdk.audio.query_devices())

    # Configure audio
    sdk.audio.input_device = input_device
    sdk.audio.output_device = output_device
    sdk.audio.block_size = block_size
    sdk.input_sample_rate = sample_rate
    sdk.output_sample_rate = sample_rate

    # Set callback functions
    sdk.set_callbacks(Callbacks())

    # Generate random payload and send
    payload = sdk.random_payload()
    sdk.start(send=True, receive=True)
    sdk.send(payload)

    try:
        # Process audio streams
        while True:
            time.sleep(0.1)
            sys.stdout.write('.')
            sys.stdout.flush()
    except KeyboardInterrupt:
        print('Exiting')

    sdk.stop()
Example #2
0
def main(args):
    # ------------------------------------------------------------------------
    # Initialise the Connect SDK.
    # ------------------------------------------------------------------------
    sdk = ChirpConnect()
    print(str(sdk))
    if args.network_config:
        sdk.set_config_from_network()

    print('Protocol: {protocol} [v{version}]'.format(
        protocol=sdk.protocol_name,
        version=sdk.protocol_version))

    # ------------------------------------------------------------------------
    # Disable audio playback.
    # ------------------------------------------------------------------------
    sdk.audio = None
    sdk.set_callbacks(Callbacks(args))
    sdk.start(send=False, receive=True)

    w = wave.open(args.infile, 'r')
    data = w.readframes(w.getnframes())
    sdk.input_sample_rate = w.getframerate()

    for f in range(0, len(data), CHIRP_SDK_BUFFER_SIZE):
        if w.getsampwidth() == 2:
            sdk.process_shorts_input(data[f: f + CHIRP_SDK_BUFFER_SIZE])
        elif w.getsampwidth() == 4:
            sdk.process_input(data[f: f + CHIRP_SDK_BUFFER_SIZE])

    sdk.stop()
def main(input_device):

    # Initialise Connect SDK
    sdk = ChirpConnect()
    print(str(sdk))
    print(sdk.audio.query_devices())

    if sdk.protocol_name != 'standard':
        raise RuntimeError('Must use the standard protocol ' +
                           'to be compatible with other Chirp Messenger apps.')

    # Configure audio
    sdk.audio.frame_size = 4096
    sdk.audio.input_device = input_device

    # Set callbacks and start SDK
    sdk.set_callbacks(Callbacks())
    sdk.start(send=False, receive=True)

    try:
        # Process audio streams
        while True:
            time.sleep(1)

    except KeyboardInterrupt:
        print('Exiting')

    sdk.stop()
    sdk.close()
def main(args):
    # ------------------------------------------------------------------------
    # Initialise the Connect SDK.
    # ------------------------------------------------------------------------
    sdk = ChirpConnect()
    print(sdk.audio.query_devices())
    print(str(sdk))
    sdk.audio.output_device = args.o
    if args.network_config:
        sdk.set_config_from_network()

    if sdk.protocol_name != '16khz-mono':
        raise RuntimeError('Must use the 16khz-mono protocol ' +
                           'to be compatible with other Chirp Messenger apps.')

    # ------------------------------------------------------------------------
    # Parse unicode and send as a chirp payload
    # ------------------------------------------------------------------------
    message = args.message.encode('utf-8')
    payload = sdk.new_payload(message)

    sdk.volume = args.volume
    sdk.set_callbacks(Callbacks())
    sdk.start()
    sdk.send(payload)

    try:
        # Process audio streams
        while True:
            time.sleep(0.1)
    except KeyboardInterrupt:
        print('Exiting')

    sdk.stop()
    sdk.close()
Example #5
0
def main(args):
    # ------------------------------------------------------------------------
    # Initialise the Connect SDK.
    # ------------------------------------------------------------------------
    sdk = ChirpConnect()
    print(str(sdk))
    if args.network_config:
        sdk.set_config_from_network()

    print('Protocol: {protocol} [v{version}]'.format(
        protocol=sdk.protocol_name, version=sdk.protocol_version))

    # ------------------------------------------------------------------------
    # Disable audio playback.
    # ------------------------------------------------------------------------
    sdk.audio = None
    sdk.start(send=True, receive=False)

    # ------------------------------------------------------------------------
    # Encode payload
    # ------------------------------------------------------------------------
    if args.unicode:
        message = args.unicode.encode('utf-8')
        payload = sdk.new_payload(message)
    elif args.hex:
        message = bytearray.fromhex(args.hex)
        payload = sdk.new_payload(message)
    else:
        payload = sdk.random_payload()

    # ------------------------------------------------------------------------
    # Set transmission channel
    # ------------------------------------------------------------------------
    if args.channel:
        if args.channel >= sdk.channel_count:
            raise ValueError('Channel %d is not available' % args.channel)
        sdk.transmission_channel = args.channel

    # ------------------------------------------------------------------------
    # Process output
    # ------------------------------------------------------------------------
    output_file = args.outfile if args.outfile else '%s.wav' % payload
    w = wave.open(output_file, 'w')
    w.setnchannels(1)
    w.setsampwidth(2)
    w.setframerate(sdk.output_sample_rate)

    sdk.send(payload)

    while sdk.state == CHIRP_CONNECT_STATE_SENDING:
        data = ar.array('h', [0] * CHIRP_SDK_BUFFER_SIZE)
        byte_data = bytearray(data.tobytes() if sys.version[0] ==
                              '3' else data.tostring())
        sdk.process_shorts_output(byte_data)
        w.writeframes(byte_data)

    print('Wrote audio to output: %s' % output_file)
    w.close()

    sdk.stop()
Example #6
0
class Grillo:
    """
    Tool to send data to a different computer or receive it, just using audio and mic.
    """
    HEADER_SEPARATOR = b"|"
    FILE_NAME_SEPARATOR = b"<NAME>"

    def __init__(self, send=False, receive=False):
        """
        Return an instance of a chirp thingymagic ready to be used.
        """
        self.chirp = ChirpConnect(
            key=config.CHIRP_APP_KEY,
            secret=config.CHIRP_APP_SECRET,
            config=config.CHIRP_APP_CONFIG,
        )

        self.chirp.set_callbacks(ChirpCallbacks(self))
        self.listening = receive
        self.chirp.start(send=send, receive=receive)

    def send_message(self, kind, payload):
        """
        Build a serialized message to send over audio.
        """
        message = kind.value.encode(
            "utf-8") + Grillo.HEADER_SEPARATOR + payload

        if len(message) > 32:
            raise MessageTooLongException()

        self.chirp.send(message, blocking=True)

    def read_message(self, message):
        """
        Read a serialized message received over audio.
        """
        parts = message.split(Grillo.HEADER_SEPARATOR)

        kind = MessageKind(parts[0].decode("utf-8"))
        payload = Grillo.HEADER_SEPARATOR.join(parts[1:])

        return kind, payload

    def send_text(self, text):
        """
        Send text via audio.
        """
        self.send_message(MessageKind.TEXT, text.encode("utf-8"))

    def send_clipboard(self):
        """
        Send clipboard contents via audio.
        """
        self.send_message(MessageKind.CLIPBOARD,
                          pyperclip.paste().encode("utf-8"))

    def send_file(self, file_path):
        """
        Send file contents via audio.
        """
        if isinstance(file_path, str):
            file_path = Path(file_path)

        with file_path.open('rb') as file:
            file_contents = file.read()

        payload = (file_path.name.encode("utf-8") +
                   Grillo.FILE_NAME_SEPARATOR + file_contents)

        self.send_message(MessageKind.FILE, payload)

    def listen(self, forever=False):
        """
        Receive whatever data is being sent from the source computer.
        """
        while self.listening or forever:
            time.sleep(1)

    def receive_message(self, message):
        """
        Process an incoming message.
        """
        kind, payload = self.read_message(message)
        if kind == MessageKind.TEXT:
            self.receive_text(payload)
        elif kind == MessageKind.CLIPBOARD:
            self.receive_clipboard(payload)
        elif kind == MessageKind.FILE:
            self.receive_file(payload)

        self.listening = False

    def receive_text(self, payload):
        """
        Receive text via audio.
        """
        text = payload.decode("utf-8")
        print("Received text:")
        print(text)

    def receive_clipboard(self, payload):
        """
        Receive clipboard contents via audio.
        """
        clipboard_contents = payload.decode("utf-8")
        pyperclip.copy(clipboard_contents)
        print("Received clipboard contents, copied to your own clipboard :)")

    def receive_file(self, payload):
        """
        Receive file contents via audio.
        """
        parts = payload.split(Grillo.FILE_NAME_SEPARATOR)

        name = parts[0].decode("utf-8")
        file_contents = Grillo.FILE_NAME_SEPARATOR.join(parts[1:])

        file_path = Path(".") / name

        copy_counter = 0
        while file_path.exists():
            copy_counter += 1
            file_path = Path(".") / str(copy_counter) + "_" + name

        with file_path.open('wb') as file:
            file.write(file_contents)

        print("Received a file, saved to", str(file_path))
Example #7
0
class TestConnectSDK(unittest.TestCase):
    BUFFER_SIZE = 1024
    TEST_PAYLOAD_LENGTH = 3

    @classmethod
    def setUpClass(cls):
        config = configparser.ConfigParser()
        config.read(os.path.expanduser('~/.chirprc'))

        try:
            cls.app_key = config.get('test', 'app_key')
            cls.app_secret = config.get('test', 'app_secret')
            cls.app_config = config.get('test', 'app_config')
        except configparser.NoSectionError:
            raise Exception(
                "Couldn't find test credentials. Please add a [test] section to your ~/.chirprc."
            )

        cls.is2 = sys.version[0] == '2'

    def setUp(self):
        self.sdk = ChirpConnect(self.app_key, self.app_secret, self.app_config)
        self.sdk.audio = None

        self.async_request = patch(
            'requests_futures.sessions.FuturesSession.post')
        self.async_patch = self.async_request.start()
        self.addCleanup(self.async_request.stop)

        self.length = None
        self.channel = None

    def test_version(self):
        version = self.sdk.version
        for data in ['name', 'version', 'build']:
            self.assertIn(data, version)
            self.assertTrue(len(version[data]) > 0)

    def test_read_chirprc(self):
        self.sdk = ChirpConnect()
        self.sdk.read_chirprc('test')
        self.assertIsNotNone(self.sdk.config)

    # -- Getters & Setters

    def test_volume(self):
        self.assertEqual(self.sdk.volume, 1.0)

    def test_set_volume(self):
        self.sdk.volume = 0.33
        self.assertEqual(self.sdk.volume, 0.33)

    def test_input_sample_rate(self):
        self.assertEqual(self.sdk.input_sample_rate, 44100)

    def test_output_sample_rate(self):
        self.assertEqual(self.sdk.output_sample_rate, 44100)

    def test_set_input_sample_rate(self):
        self.sdk.input_sample_rate = 48000
        self.assertEqual(self.sdk.input_sample_rate, 48000)
        with self.assertRaises(ConnectError):
            self.sdk.input_sample_rate = 0

    def test_set_output_sample_rate(self):
        self.sdk.output_sample_rate = 48000
        self.assertEqual(self.sdk.output_sample_rate, 48000)
        with self.assertRaises(ConnectError):
            self.sdk.output_sample_rate = 0

    def test_default_state(self):
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_STOPPED)

    def test_get_auto_mute(self):
        self.assertTrue(self.sdk.auto_mute)

    def test_set_auto_mute(self):
        self.sdk.auto_mute = False
        self.assertFalse(self.sdk.auto_mute)

    def test_protocol_name(self):
        self.assertEqual(self.sdk.protocol_name, 'standard')

    def test_protocol_version(self):
        self.assertIsInstance(self.sdk.protocol_version, int)

    def test_protocol_duration(self):
        self.assertEqual(self.sdk.get_duration(10), 2.04)

    def test_expiry(self):
        self.assertIsInstance(self.sdk.expiry, datetime)

    def test_channel_count(self):
        self.assertEqual(self.sdk.channel_count, 1)

    def test_transmission_channel(self):
        self.assertEqual(self.sdk.transmission_channel, 0)

    def test_get_state_for_channel(self):
        self.sdk.start()
        self.assertEqual(self.sdk.get_state_for_channel(0),
                         CHIRP_CONNECT_STATE_RUNNING)
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.sdk.send(payload)
        self.assertEqual(self.sdk.get_state_for_channel(0),
                         CHIRP_CONNECT_STATE_SENDING)

    # -- States

    def test_not_created(self):
        with self.assertRaises(ConnectError):
            sdk = ChirpConnect(self.app_key, self.app_secret, '/not/real/path')
            self.assertEqual(sdk.state, CHIRP_CONNECT_STATE_NOT_CREATED)

    def test_start(self):
        self.sdk.start()
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_RUNNING)

    def test_already_started(self):
        self.sdk.start()
        with self.assertRaises(ConnectError):
            self.sdk.start()

    def test_pause(self):
        self.sdk.start()
        self.sdk.pause(True)
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_PAUSED)

    def test_unpause(self):
        self.sdk.start()
        self.sdk.pause(True)
        self.sdk.pause(False)
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_RUNNING)

    def test_already_paused(self):
        with self.assertRaises(ConnectError):
            self.sdk.pause(True)

    def test_stop(self):
        self.sdk.start()
        self.sdk.stop()
        self.assertEqual(self.sdk.state, CHIRP_CONNECT_STATE_STOPPED)

    def test_already_stopped(self):
        with self.assertRaises(ConnectError):
            self.sdk.stop()

    # -- Callbacks

    def stub_connect_state_callback(self, old, new):
        self.old = old
        self.new = new

    def stub_connect_callback(self, payload, channel):
        self.length = len(payload)
        self.channel = channel

    def stub_receiving_callback(self, channel):
        self.recv = True
        self.channel = channel

    def test_state_changed_callback(self):
        self.sdk.callbacks.on_state_changed = self.stub_connect_state_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertIsNotNone(self.old)
        self.assertIsNotNone(self.new)

    def test_sending_callback(self):
        self.sdk.callbacks.on_sending = self.stub_connect_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertIsNotNone(self.length)
        self.assertIsNotNone(self.channel)

    def test_sent_callback(self):
        self.sdk.callbacks.on_sent = self.stub_connect_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertIsNotNone(self.length)
        self.assertIsNotNone(self.channel)

    def test_receiving_callback(self):
        self.sdk.callbacks.on_receiving = self.stub_receiving_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertTrue(self.recv)
        self.assertIsNotNone(self.channel)

    def test_received_callback(self):
        self.sdk.callbacks.on_received = self.stub_connect_callback
        self.sdk.trigger_callbacks([0, 1, 2, 3, 4])
        self.assertIsNotNone(self.length)
        self.assertIsNotNone(self.channel)

    # -- Processing

    def test_process_input(self):
        indata = ar.array('f', [0.025] * self.BUFFER_SIZE)
        self.sdk.start()
        self.sdk.process_input(
            getattr(indata, 'tostring' if self.is2 else 'tobytes')())

    def test_process_input_not_started(self):
        indata = ar.array('f', [0.025] * self.BUFFER_SIZE)
        with self.assertRaises(ConnectError):
            self.sdk.process_input(
                getattr(indata, 'tostring' if self.is2 else 'tobytes')())

    def test_process_output(self):
        outdata = ar.array('f', [0.05] * self.BUFFER_SIZE)
        self.sdk.start()
        self.sdk.process_output(outdata)

    def test_process_output_not_started(self):
        outdata = ar.array('f', [0.05] * self.BUFFER_SIZE)
        with self.assertRaises(ConnectError):
            self.sdk.process_output(outdata)

    def test_process_shorts_input(self):
        indata = ar.array('h', [128] * self.BUFFER_SIZE)
        self.sdk.start()
        self.sdk.process_shorts_input(
            getattr(indata, 'tostring' if self.is2 else 'tobytes')())

    def test_process_shorts_output(self):
        outdata = ar.array('h', [-128] * self.BUFFER_SIZE)
        self.sdk.start()
        self.sdk.process_shorts_output(outdata)

    # -- Payload

    def test_get_max_payload_length(self):
        self.assertIsInstance(self.sdk.max_payload_length, int)
        self.assertTrue(self.sdk.max_payload_length > 0)

    def test_new_payload_string(self):
        payload = self.sdk.new_payload('test'.encode())
        self.assertIsInstance(payload, bytearray)

    def test_new_payload_array(self):
        payload = self.sdk.new_payload([64, 27, 33, 27])
        self.assertIsInstance(payload, bytearray)

    def test_random_payload(self):
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertIsInstance(payload, bytearray)
        for byte in range(0, len(payload)):
            self.assertIsInstance(payload[byte], int)

    def test_pseudo_random_payload(self):
        self.sdk._set_seed(0)
        self.assertEqual(
            self.sdk.random_payload(0),
            [47, 117, 192, 67, 251, 195, 103, 9, 211, 21, 242, 36, 87])

    def test_is_valid(self):
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertTrue(self.sdk.is_valid(payload))

    def test_payload_is_valid(self):
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertTrue(payload.isvalid())

    def test_as_string(self):
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertIsInstance(self.sdk.as_string(payload), str)

    def test_payload_as_string(self):
        payload = self.sdk.new_payload(b'hello')
        self.assertEqual(str(payload), '68656c6c6f')

    def test_send(self):
        self.sdk.start()
        payload = self.sdk.random_payload(self.TEST_PAYLOAD_LENGTH)
        self.assertIsNone(self.sdk.send(payload))

    def test_null_payload(self):
        with self.assertRaises(ValueError):
            self.sdk.new_payload([])

    def test_payload_too_long(self):
        payload = self.sdk.new_payload('hello'.encode('ascii'))
        with self.assertRaises(ValueError):
            payload.extend('this-is-wayyyyy-toooooo-long')
Example #8
0
#!/usr/bin/env python
import os
from playsound import playsound

from chirpsdk import ChirpConnect, CallbackSet

chirp = ChirpConnect()
chirp.start(send=True)
identifier = 'cop'
payload = bytearray([ord(ch) for ch in identifier])
chirp.send(payload, blocking=True)

playsound('beedoo.mp3')
playsound('beedoo.mp3')
Example #9
0
import sys
import time
import json

from chirpsdk import ChirpConnect, CallbackSet


class Callbacks(CallbackSet):
    def on_received(self, payload, channel):
        if payload is not None:
            hex = payload.decode('utf-8')
            print(json.dumps({"data": hex, "type": "hex"}))
        else:
            print(json.dumps({"data": "Decode Failed", "type": "error"}))

    def on_receiving(self, channel):
        print(json.dumps({"data": "Listening...", "type": "listening"}))
        return super().on_receiving(channel)


chirp = ChirpConnect(block='ultrasonic-multi-channel')
chirp.start(receive=True)
chirp.set_callbacks(Callbacks())

try:
    while True:
        time.sleep(0.1)
        sys.stdout.flush()
except KeyboardInterrupt:
    print('Exiting')
Example #10
0
from chirpsdk import ChirpConnect, CallbackSet
from resources import send_credential


class Callbacks(CallbackSet):
    def on_received(self, payload, channel):
        if payload is None:
            print('Decode failed!')
        else:
            payload = ''.join([chr(tmp) for tmp in payload])
            data = payload.split(':')
            send_audio_credential(data[0], data[1])


chirp = ChirpConnect()
chirp.set_callbacks(Callbacks())

chirp.start(send=False, receive=True)
try:
    while (True):
        pass
except KeyboardInterrupt:
    chirp.stop()
    print('Exiting...')
Example #11
0
    def on_received(self, payload, channel):
        print("algo")
        if payload is not None:
            global recebido
            identifier = payload.decode('utf-8')
            print('Received: ' + identifier)
            recebido = True
        else:
            print('Decode failed')

    def on_state_changed(self, old, new):
        print(old, new)


#chirp.input_sample_rate = 32000
chirp.set_callbacks(Callbacks())
chirp.start()
mensagem = 'hello'
dados = bytearray([ord(ch) for ch in mensagem])

termino = time.time() + 20
try:
    while not recebido and time.time() < termino:
        time.sleep(0.1)
        sys.stdout.write('.')
        sys.stdout.flush()
except KeyboardInterrupt:
    print('Exiting')

chirp.stop()
Example #12
0
from chirpsdk import ChirpConnect, CallbackSet

chirp = ChirpConnect()
chirp.start(send=True, receive=True)

identifier = 'Hello'
payload = bytearray([ord(ch) for ch in identifier])
chirp.send(payload, blocking=True)
Example #13
0
def main(block_name, input_device, output_device, block_size, sample_rate,
         string):

    # Initialise ConnectSDK
    sdk = ChirpConnect(block=block_name)
    print(str(sdk))
    print('Protocol: {protocol} [v{version}]'.format(
        protocol=sdk.protocol_name, version=sdk.protocol_version))
    print(sdk.audio.query_devices())

    # Configure audio
    sdk.audio.input_device = input_device
    sdk.audio.output_device = output_device
    sdk.audio.block_size = block_size
    sdk.input_sample_rate = sample_rate
    sdk.output_sample_rate = 44100

    # Set callback functions
    sdk.set_callbacks(Callbacks())

    # print("type your message")
    #msg = ""
    print(string)
    divideMsg8(string)
    #print(divMsg)

    # window = Tk()

    # messages = Text(window)
    # messages.pack()

    # # input_user = StringVar()
    # # input_field = Entry(window, text=input_user)
    # input_field.pack(side=BOTTOM, fill=X)

    # def Enter_pressed(event):
    #     input_get = input_field.get()
    #     global msg, divMsg
    #     msg = input_get
    #     divMsg = divideMsg8(string)
    #     #print(input_get)
    #     messages.insert(INSERT, '%s\n' % input_get)

    #     # label = Label(window, text=input_get)
    #     input_user.set('')
    #     # label.pack()
    #     return "break"

    # frame = Frame(window)  # , width=300, height=300)
    # input_field.bind("<Return>", Enter_pressed)
    # frame.pack()

    # window.mainloop()

    #initialize SDK to SEND ONLY

    sdk.start(send=True, receive=True)

    timeLapse = 0
    # numMsgSent = 0;

    try:
        # Process audio streams
        while True:

            if string != "xS$9!a6@":

                time.sleep(0.1)
                # sys.stdout.write('.')
                sys.stdout.flush()
                timeLapse += 1
                if timeLapse % 50 == 0:
                    timeLapse = 0
                    # if numMsgSent <= len(divMsg):
                    if len(divMsg) > 0:
                        # identifier = divMsg[numMsgSent]
                        identifier = divMsg[0]
                        payload = bytearray([ord(ch) for ch in identifier])
                        sdk.send(payload)
                        divMsg.pop(0)
                        # numMsgSent += 1
                    else:
                        identifier = "@6a!9$Sx"
                        payload = bytearray([ord(ch) for ch in identifier])
                        sdk.send(payload)
                        time.sleep(5)
                        break
            else:
                time.sleep(0.1)
                # sys.stdout.write('.')
                sys.stdout.flush()
                break

    except KeyboardInterrupt:
        print('Exiting')

    sdk.stop()
Example #14
0
class Modem:
    """
    An audio modem able to encode and decode data from/to audio. Internally uses chirp for
    the modulation/demodulation and error correction, but adding a layer that allows for messages
    longer than 32 bytes (sending multiple chirp messages for every grillo message).
    """
    DATA_LEN = 30
    PACKET_DURATION = timedelta(seconds=4.66)

    def __init__(self, with_confirmation=False):
        self.chirp = ChirpConnect(
            key=config.CHIRP_APP_KEY,
            secret=config.CHIRP_APP_SECRET,
            config=config.CHIRP_APP_CONFIG,
        )
        self.chirp.start(send=True, receive=True)

        self.with_confirmation = with_confirmation
        self.reset_chained_status()

    def reset_chained_status(self):
        """
        Reset the status of the chained message that is being received.
        """
        self.chained_total_parts = None
        self.chained_parts = {}

    def send_message(self, message):
        """
        Send a message as multiple packets.
        """
        chain_len = self._get_chain_len(len(message))
        if chain_len > 255:
            raise MessageTooLongException()

        packets_to_send = range(chain_len)
        while len(packets_to_send) > 0:
            self._send_packets(message, packets_to_send, chain_len)
            if self.with_confirmation:
                packets_to_send = self._get_packets_to_retry()
            else:
                break

    def _get_packets_to_retry(self):
        """
        Wait for the other end to inform which parts of a message it didn't receive.
        """
        packets_to_retry = []
        ack_msg = self.receive_packet(self.PACKET_DURATION * 2)
        if ack_msg is None:
            return []

        header = ack_msg[0]
        if header == 0:
            packets_to_retry = ack_msg[1:]
            return packets_to_retry
        else:
            raise MessageAckIsBroken()

    def _send_packets(self, message, packet_list, chain_len):
        """
        Send a message as multiple packets, one after the other.
        """
        for i in packet_list:
            packet = (bytes([chain_len, i]) +
                      message[self.DATA_LEN * i:self.DATA_LEN * (i + 1)])
            self.send_packet(packet)

    def send_packet(self, packet):
        """
        Send a single packet.
        """
        self.chirp.send(packet, blocking=True)

    def send_ack(self, missing_parts=None):
        """
        Send a packet informing the missing parts of a chained message.
        """
        if missing_parts is None:
            missing_parts = []

        self.send_packet(bytes([0] + missing_parts))

    def _get_chain_len(self, size):
        return size // self.DATA_LEN + 1

    def receive_packet(self, timeout=None):
        """
        Wait (blocking) for a single packet, and return it when received.
        """
        receiver = SinglePacketReceiver()
        self.chirp.set_callbacks(receiver)

        start = datetime.now()

        while receiver.packet is None:
            time.sleep(0.1)

            if timeout:
                now = datetime.now()
                if now - start > timeout:
                    break

        self.stop_listening()
        return receiver.packet

    def receive_message(self, timeout=300):
        """
        Wait (blocking) for a single message, and return it when received.
        """
        self.reset_chained_status()

        receiver = SinglePacketReceiver(callback=self.on_chained_part_received)
        self.chirp.set_callbacks(receiver)

        self.timeout_start = datetime.now()
        if timeout:
            self.timeout_delta = timedelta(seconds=timeout)

        chained_message = None
        last_expected_part = None

        while chained_message is None:
            time.sleep(0.1)

            if self.chained_total_parts is not None:
                if last_expected_part is None:
                    last_expected_part = self.chained_total_parts - 1

                if last_expected_part in self.chained_parts or self._timeout_expired(
                ):
                    # finished receiving all the parts or should have finished and we didn't
                    missing_parts = self.chained_missing_parts()

                    if missing_parts:
                        # we didn't get all the parts, ask for the missing ones
                        parts_to_resend = missing_parts[:self.DATA_LEN]
                        last_expected_part = parts_to_resend[-1]
                        self.send_ack(parts_to_resend)
                        self._reset_timeout()
                    else:
                        # stop the chained building loop, we got all the parts
                        chained_message = self.chained_combine()
                        self.send_ack()
                        break

            if timeout and self._timeout_expired():
                break

        self.stop_listening()
        self.reset_chained_status()

        return chained_message

    def _timeout_expired(self):
        now = datetime.now()
        return (now - self.timeout_start) > self.timeout_delta

    def on_chained_part_received(self, packet):
        """
        Executed when chirp receives data that is part of a chained message.
        """
        if packet is not None:
            total_parts = packet[0]
            part_number = packet[1]
            message_part = packet[2:]

            if self.chained_total_parts is None:
                # first part received!
                self.chained_total_parts = total_parts

            self.chained_parts[part_number] = message_part
            self._reset_timeout()

    def _reset_timeout(self):
        self.timeout_delta = self.PACKET_DURATION * 1.5
        self.timeout_start = datetime.now()

    def chained_missing_parts(self):
        """
        Which parts of the message are missing?
        """
        return [
            part_number for part_number in range(self.chained_total_parts)
            if part_number not in self.chained_parts
        ]

    def chained_combine(self):
        """
        Concatenate all the message parts.
        """
        return b''.join(self.chained_parts[part_number]
                        for part_number in range(self.chained_total_parts))

    def listen_for_packets(self, callback):
        """
        Start listening for packets, calling a callback whenever a packet is received.
        """
        receiver = SinglePacketReceiver(callback)
        self.chirp.set_callbacks(receiver)

    def listen_for_messages(self, callback):
        """
        Start listening for messages, calling a callback whenever a packet is received.
        """
        while True:
            message = self.receive_message()
            callback(message)
            self.reset_chained_status()

    def stop_listening(self):
        """
        Stop using chirp to listen for packets.
        """
        self.chirp.set_callbacks(NoCallbacks())