LCOV - code coverage report
Current view: top level - py - ringbuf.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-325-gd11ca092f.info Lines: 26 52 50.0 %
Date: 2024-04-17 08:48:33 Functions: 3 5 60.0 %
Branches: 18 30 60.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2019 Jim Mussared
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include <string.h>
      28                 :            : 
      29                 :            : #include "ringbuf.h"
      30                 :            : 
      31                 :         12 : int ringbuf_get16(ringbuf_t *r) {
      32                 :         12 :     int v = ringbuf_peek16(r);
      33         [ +  + ]:         12 :     if (v == -1) {
      34                 :            :         return v;
      35                 :            :     }
      36                 :          8 :     r->iget += 2;
      37         [ +  + ]:          8 :     if (r->iget >= r->size) {
      38                 :          4 :         r->iget -= r->size;
      39                 :            :     }
      40                 :            :     return v;
      41                 :            : }
      42                 :            : 
      43                 :         12 : int ringbuf_peek16(ringbuf_t *r) {
      44         [ +  + ]:         12 :     if (r->iget == r->iput) {
      45                 :            :         return -1;
      46                 :            :     }
      47                 :         10 :     uint32_t iget_a = r->iget + 1;
      48         [ +  + ]:         10 :     if (iget_a == r->size) {
      49                 :          2 :         iget_a = 0;
      50                 :            :     }
      51         [ +  + ]:         10 :     if (iget_a == r->iput) {
      52                 :            :         return -1;
      53                 :            :     }
      54                 :          8 :     return (r->buf[r->iget] << 8) | (r->buf[iget_a]);
      55                 :            : }
      56                 :            : 
      57                 :         12 : int ringbuf_put16(ringbuf_t *r, uint16_t v) {
      58                 :         12 :     uint32_t iput_a = r->iput + 1;
      59         [ +  + ]:         12 :     if (iput_a == r->size) {
      60                 :          2 :         iput_a = 0;
      61                 :            :     }
      62         [ +  + ]:         12 :     if (iput_a == r->iget) {
      63                 :            :         return -1;
      64                 :            :     }
      65                 :         10 :     uint32_t iput_b = iput_a + 1;
      66         [ +  + ]:         10 :     if (iput_b == r->size) {
      67                 :          2 :         iput_b = 0;
      68                 :            :     }
      69         [ +  + ]:         10 :     if (iput_b == r->iget) {
      70                 :            :         return -1;
      71                 :            :     }
      72                 :          8 :     r->buf[r->iput] = (v >> 8) & 0xff;
      73                 :          8 :     r->buf[iput_a] = v & 0xff;
      74                 :          8 :     r->iput = iput_b;
      75                 :          8 :     return 0;
      76                 :            : }
      77                 :            : 
      78                 :            : // Returns:
      79                 :            : //    0: Success
      80                 :            : //   -1: Not enough data available to complete read (try again later)
      81                 :            : //   -2: Requested read is larger than buffer - will never succeed
      82                 :          0 : int ringbuf_get_bytes(ringbuf_t *r, uint8_t *data, size_t data_len) {
      83         [ #  # ]:          0 :     if (ringbuf_avail(r) < data_len) {
      84         [ #  # ]:          0 :         return (r->size <= data_len) ? -2 : -1;
      85                 :            :     }
      86                 :          0 :     uint32_t iget = r->iget;
      87                 :          0 :     uint32_t iget_a = (iget + data_len) % r->size;
      88                 :          0 :     uint8_t *datap = data;
      89         [ #  # ]:          0 :     if (iget_a < iget) {
      90                 :            :         // Copy part of the data from the space left at the end of the buffer
      91                 :          0 :         memcpy(datap, r->buf + iget, r->size - iget);
      92                 :          0 :         datap += (r->size - iget);
      93                 :          0 :         iget = 0;
      94                 :            :     }
      95                 :          0 :     memcpy(datap, r->buf + iget, iget_a - iget);
      96                 :          0 :     r->iget = iget_a;
      97                 :          0 :     return 0;
      98                 :            : }
      99                 :            : 
     100                 :            : // Returns:
     101                 :            : //    0: Success
     102                 :            : //   -1: Not enough free space available to complete write (try again later)
     103                 :            : //   -2: Requested write is larger than buffer - will never succeed
     104                 :          0 : int ringbuf_put_bytes(ringbuf_t *r, const uint8_t *data, size_t data_len) {
     105         [ #  # ]:          0 :     if (ringbuf_free(r) < data_len) {
     106         [ #  # ]:          0 :         return (r->size <= data_len) ? -2 : -1;
     107                 :            :     }
     108                 :          0 :     uint32_t iput = r->iput;
     109                 :          0 :     uint32_t iput_a = (iput + data_len) % r->size;
     110                 :          0 :     const uint8_t *datap = data;
     111         [ #  # ]:          0 :     if (iput_a < iput) {
     112                 :            :         // Copy part of the data to the end of the buffer
     113                 :          0 :         memcpy(r->buf + iput, datap, r->size - iput);
     114                 :          0 :         datap += (r->size - iput);
     115                 :          0 :         iput = 0;
     116                 :            :     }
     117                 :          0 :     memcpy(r->buf + iput, datap, iput_a - iput);
     118                 :          0 :     r->iput = iput_a;
     119                 :          0 :     return 0;
     120                 :            : }

Generated by: LCOV version 1.15-5-g462f71d