Aller au contenu

Les Stream Buffers

Les stream buffers permettent de transmettre un flux d’octets d’une tâche (ou d’une routine de traitement d’interruption) à une autre tâche.

Contrairement aux queues les informations ne sont pas structurées en messages de taille fixe. C’est juste un flux d’information d’un point à un autre. Les stream buffers sont d’ailleurs optimisés pour des scénarios point à point, tels que le passage de données d’une routine de service d’interruption à une tâche.

Un stream buffer peut être créé avec la fonction xStreamBufferCreate()

StreamBufferHandle_t xStreamBufferCreate(size_t xBufferSizeBytes,
                                         size_t xTriggerLevelBytes);

Si on souhaite faire intervenir des call backs, on peut utiliser la fonction xStreamBufferCreateWithCallback :

StreamBufferHandle_t xStreamBufferCreateWithCallback(
    size_t xBufferSizeBytes,
    size_t xTriggerLevelBytes,
    StreamBufferCallbackFunction_t pxSendCompletedCallback,
    StreamBufferCallbackFunction_t pxReceiveCompletedCallback);

On peut envoyer des octets dans un stream buffer avec la fonction xStreamBufferSend()

size_t xStreamBufferSend(StreamBufferHandle_t xStreamBuffer,
                         const void* pvTxData,
                         size_t xDataLengthBytes,
                         TickType_t xTicksToWait);

Exemple :

void vAFunction(StreamBufferHandle_t xStreamBuffer) {
    size_t xBytesSent;
    uint8_t ucArrayToSend[] = {0, 1, 2, 3};
    char* pcStringToSend    = "String to send";
    const TickType_t x100ms = pdMS_TO_TICKS(100);

    // Send an array to the stream buffer, blocking for a maximum of 100ms to
    // wait for enough space to be available in the stream buffer.
    xBytesSent = xStreamBufferSend(
        xStreamBuffer, (void*)ucArrayToSend, sizeof(ucArrayToSend), x100ms);

    if (xBytesSent != sizeof(ucArrayToSend)) {
        // The call to xStreamBufferSend() times out before there was enough
        // space in the buffer for the data to be written, but it did
        // successfully write xBytesSent bytes.
    }

    // Send the string to the stream buffer.  Return immediately if there is not
    // enough space in the buffer. */
    xBytesSent = xStreamBufferSend(
        xStreamBuffer, (void*)pcStringToSend, strlen(pcStringToSend), 0);

    if (xBytesSent != strlen(pcStringToSend)) {
        // The entire string could not be added to the stream buffer because
        // there was not enough free space in the buffer, but xBytesSent bytes
        // were sent.  Could try again to send the remaining bytes.
    }
}

Pour recevoir des octets, nous utilisons la fonction xStreamBufferReceive().

size_t xStreamBufferReceive(StreamBufferHandle_t xStreamBuffer,
                            void* pvRxData,
                            size_t xBufferLengthBytes,
                            TickType_t xTicksToWait);

Exemple :

void vAFunction(StreamBuffer_t xStreamBuffer) {
    uint8_t ucRxData[20];
    size_t xReceivedBytes;
    const TickType_t xBlockTime = pdMS_TO_TICKS(20);

    // Receive up to another sizeof( ucRxData ) bytes from the stream buffer.
    // Wait in the Blocked state (so not using any CPU processing time) for a
    // maximum of 100ms for the full sizeof( ucRxData ) number of bytes to be
    // available.
    xReceivedBytes = xStreamBufferReceive(
        xStreamBuffer, (void*)ucRxData, sizeof(ucRxData), xBlockTime);

    if (xReceivedBytes > 0) {
        // A ucRxData contains another xRecievedBytes bytes of data, which can
        // be processed here....
    }
}