void printDriverDataDispatcherList(OutputStream* outputStream, DriverDataDispatcherList* dispatcherList) {
    int i;
    for (i = 0; i < dispatcherList->size; i++) {
        DriverDataDispatcher* dispatcher = getDriverDataDispatcherByIndex(i);    
        printDriverDataDispatcher(outputStream, dispatcher);
    }
}
DriverDataDispatcher* addDriverDataDispatcher(
                            TransmitMode transmitMode,
                            const char* name,
                            const char* addressString,
                            int address,
                            InputStream* inputStream,
                            OutputStream* outputStream,
                            driverDataDispatcherTransmitDataFunction* driverDataDispatcherTransmitData) {
    if (dispatcherList.dispatchers == NULL) {
        writeError(DISPATCHERS_LIST_NOT_INITIALIZED);
        return NULL;
    }
    unsigned char size = dispatcherList.size;
    if (size < dispatcherList.maxSize) {
        DriverDataDispatcher* result = getDriverDataDispatcherByIndex(size);
        result->transmitMode = transmitMode;
        result->name = name;
        result->addressString = addressString;
        result->address = address;
        result->inputStream = inputStream;
        result->outputStream = outputStream;
        result->driverDataDispatcherTransmitData = driverDataDispatcherTransmitData;
        dispatcherList.size++;
        return result;
    } else {
        writeError(TOO_MUCH_DRIVER_DATA_DISPATCHER);
        return NULL;
    }
}
DriverDataDispatcher* getDriverDataDispatcherByTransmitMode(TransmitMode transmitMode, int address) {
    int size = dispatcherList.size;
    int i;
    for (i = 0; i < size; i++) {
        DriverDataDispatcher* dispatcher = getDriverDataDispatcherByIndex(i);
        if (dispatcher->transmitMode == transmitMode && dispatcher->address == address) {
            return dispatcher;
        }
    }
    return NULL;
}
bool handleNotificationFromDispatcherList(TransmitMode transmitMode, int address) {
    int size = dispatcherList.size;
    int i;
    for (i = 0; i < size; i++) {
        DriverDataDispatcher* dispatcher = getDriverDataDispatcherByIndex(i);
        if (dispatcher->transmitMode != transmitMode) {
            continue;
        }
        if (dispatcher->address != address) {
            continue;
        }
        if (handleNotificationFromDispatcher(dispatcher)) {
            return true;
        }
    }
    return false;
}
void test_initDriverDataDispatcherList_add_too_much_dispatchers(void) {
    init_driverDataDispatcherList();
    TEST_ASSERT_FALSE(isThereAnyError());
    TEST_ASSERT_EQUAL(0, getDriverDataDispatcherCount());

    // Add a first dispatcher
    DriverDataDispatcher* dispacher = addLocalDriverDataDispatcher();
    TEST_ASSERT_FALSE(isThereAnyError());
    TEST_ASSERT_EQUAL(1, getDriverDataDispatcherCount());
    // TEST_ASSERT_EQUAL(dispacher, getDriverDataDispatcherByIndex(0));
    
    // Add a second dispatcher
    dispacher = addLocalDriverDataDispatcher();
    TEST_ASSERT_FALSE(isThereAnyError());
    TEST_ASSERT_EQUAL(2, getDriverDataDispatcherCount());
    TEST_ASSERT_EQUAL(dispacher, getDriverDataDispatcherByIndex(1));

    dispacher = addLocalDriverDataDispatcher();
    TEST_ASSERT_EQUAL(TOO_MUCH_DRIVER_DATA_DISPATCHER, getLastError());
}
bool handleStreamInstruction(Buffer* inputBuffer,
                            Buffer* outputBuffer,
                            OutputStream* outputStream,
                            filterCharFunction* inputFilterChar,
                            filterCharFunction* outputFilterChar) {

    if (inputBuffer == NULL) {
        writeError(DRIVER_STREAM_LISTENER_INPUT_BUFFER_NULL);
        return false;
    }
    if (outputBuffer == NULL) {
        writeError(DRIVER_STREAM_LISTENER_OUTPUT_BUFFER_NULL);
        return false;
    }

    // Try to clear the buffer if needed ('z' char)
    if (clearBufferIfNeeded(inputBuffer)) {
        return false;
    }

    // We received data
    int inputBufferCount = getBufferElementsCount(inputBuffer);

    if (inputBufferCount > 0) {

        if (filterFirstNextChar(inputBuffer, inputFilterChar)) {
            return false;
        }

        // As there is clear of char filtering, we must reload the size of the buffer
        int bufferSize = getBufferElementsCount(inputBuffer);

        if (bufferSize < DEVICE_HEADER_LENGTH) {
            return false;
        }

        // Get the header
        unsigned char deviceHeader = bufferGetCharAtIndex(inputBuffer, DEVICE_HEADER_INDEX);

        // Manage the dispatcher specifier (3 chars : Ex j01 before real command ...)
        unsigned char specifyDispatcherLength = 0;
        if (deviceHeader == DATA_DISPATCHER_DEVICE_HEADER) {
            specifyDispatcherLength += DISPATCHER_COMMAND_AND_INDEX_HEADER_LENGTH;
        }

        // Check if enough data
        if (bufferSize < specifyDispatcherLength + DEVICE_AND_COMMAND_HEADER_LENGTH) {
            return false;
        }

        // Reload the deviceHeader to take the dispatcher specifier if any
        deviceHeader = bufferGetCharAtIndex(inputBuffer, specifyDispatcherLength + DEVICE_HEADER_INDEX);
        unsigned char commandHeader = bufferGetCharAtIndex(inputBuffer, specifyDispatcherLength + COMMAND_HEADER_INDEX);

        // find the device corresponding to this header. It must at least be declared in local or in remote !
        unsigned char dataSize = bufferSize - specifyDispatcherLength;
        const Device* device = deviceDataDispatcherFindDevice(deviceHeader, commandHeader, dataSize, DEVICE_MODE_INPUT);

        // if the device was not found
        if (device == NULL) {
            return false;
        }

        // At this moment, device Interface is found
        DeviceInterface* deviceInterface = device->deviceInterface;

        // We must send device specifyDispatcherLength + Header + commandHeader + data => + 2
        int dataToTransferCount = deviceInterface->deviceGetInterface(commandHeader, DEVICE_MODE_INPUT, false) + specifyDispatcherLength + DEVICE_AND_COMMAND_HEADER_LENGTH;

        if (bufferSize < dataToTransferCount) {
            return false;
        }

        // We must receive ack + device header + command header + data => + 3
        int dataToReceiveCount = deviceInterface->deviceGetInterface(commandHeader, DEVICE_MODE_OUTPUT, false) + ACK_LENGTH + DEVICE_AND_COMMAND_HEADER_LENGTH;

        InputStream* bufferedInputStream = getInputStream(inputBuffer);
        OutputStream* bufferedOutputStream = getOutputStream(outputBuffer);

        TransmitMode transmitMode = device->transmitMode;

        // we handle locally the request
        if (specifyDispatcherLength == 0 && transmitMode == TRANSMIT_LOCAL) {

            // We need the implementation for local mode
            DeviceDescriptor* deviceDescriptor = device->descriptor;
            if (deviceDescriptor == NULL) {
                writeError(NO_DEVICE_DESC_FOUND_FOR);
                append(getErrorOutputStreamLogger(), deviceHeader);
                append(getErrorOutputStreamLogger(), commandHeader);
                return false;
            }

            // remove the first chars corresponding to the device header and command Header
            bufferClearLastChars(inputBuffer, DEVICE_AND_COMMAND_HEADER_LENGTH);

            // Call to the device
            deviceDescriptor->deviceHandleRawData(commandHeader, bufferedInputStream, bufferedOutputStream);

        } // we forward the request through Remote Operation with Dispatcher
        else if (specifyDispatcherLength > 0 || transmitMode == TRANSMIT_I2C || transmitMode == TRANSMIT_UART || transmitMode == TRANSMIT_ZIGBEE) {

            // Find dispatcher
            DriverDataDispatcher* dispatcher = NULL;

            if (specifyDispatcherLength > 0) {
                bufferClearLastChars(inputBuffer, DEVICE_HEADER_LENGTH);
                unsigned char dispatcherIndex = readHex2(bufferedInputStream);

                dispatcher = getDriverDataDispatcherByIndex(dispatcherIndex);
                if (dispatcher == NULL) {
                    writeError(NO_DISPATCHER_FOUND);
                    OutputStream* errorOutputStream = getErrorOutputStreamLogger();
                    appendStringAndDec(errorOutputStream, ", dispatcherIndex=", dispatcherIndex);
                    return false;
                }
            }
            else {
                TransmitMode transmitMode = device->transmitMode;
                int address = device->address;

                dispatcher = getDriverDataDispatcherByTransmitMode(transmitMode, address);
                
                if (dispatcher == NULL) {
                    writeError(NO_DISPATCHER_FOUND);
                    OutputStream* errorOutputStream = getErrorOutputStreamLogger();
                    appendStringAndDec(errorOutputStream, ", transmitMode=", transmitMode);
                    append(errorOutputStream, '(');
                    appendString(errorOutputStream, getTransmitModeAsString(transmitMode));
                    append(errorOutputStream, ')');
                    appendStringAndDec(errorOutputStream, ", addr=", address);
                    return false;
                }
            }

            // copy Driver buffer with remote Call
            dispatcher->driverDataDispatcherTransmitData(dispatcher,
                    inputBuffer,
                    outputBuffer,
                    dataToTransferCount,
                    dataToReceiveCount
                    );
        }
        
        // In All Cases (Local / I2C / UART / Zigbee ...)

        // Copy the data from bufferOutputStream to the outputStream
        if (outputStream != NULL) {
            copyInputToOutputStream(&(outputBuffer->inputStream), outputStream, outputFilterChar, dataToReceiveCount);
        }
        return true;
    }
    return false;
}