LCOV - code coverage report
Current view: top level - py - objringio.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-148-g136058496.info Lines: 39 39 100.0 %
Date: 2024-12-20 19:05:39 Functions: 5 5 100.0 %
Branches: 17 19 89.5 %

           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) 2024 Andrew Leech
       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 "ringbuf.h"
      28                 :            : #include "py/mpconfig.h"
      29                 :            : 
      30                 :            : #if MICROPY_PY_MICROPYTHON_RINGIO
      31                 :            : 
      32                 :            : #include "py/runtime.h"
      33                 :            : #include "py/stream.h"
      34                 :            : 
      35                 :            : typedef struct _micropython_ringio_obj_t {
      36                 :            :     mp_obj_base_t base;
      37                 :            :     ringbuf_t ringbuffer;
      38                 :            : } micropython_ringio_obj_t;
      39                 :            : 
      40                 :         16 : static mp_obj_t micropython_ringio_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
      41                 :         16 :     mp_arg_check_num(n_args, n_kw, 1, 1, false);
      42                 :         16 :     mp_int_t buff_size = -1;
      43                 :         16 :     mp_buffer_info_t bufinfo = {NULL, 0, 0};
      44                 :            : 
      45         [ +  + ]:         16 :     if (!mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
      46                 :         12 :         buff_size = mp_obj_get_int(args[0]);
      47                 :            :     }
      48                 :         12 :     micropython_ringio_obj_t *self = mp_obj_malloc(micropython_ringio_obj_t, type);
      49         [ +  + ]:         12 :     if (bufinfo.buf != NULL) {
      50                 :            :         // buffer passed in, use it directly for ringbuffer.
      51                 :          4 :         self->ringbuffer.buf = bufinfo.buf;
      52                 :          4 :         self->ringbuffer.size = bufinfo.len;
      53                 :          4 :         self->ringbuffer.iget = self->ringbuffer.iput = 0;
      54                 :            :     } else {
      55                 :            :         // Allocate new buffer, add one extra to buff_size as ringbuf consumes one byte for tracking.
      56                 :          8 :         ringbuf_alloc(&(self->ringbuffer), buff_size + 1);
      57                 :            :     }
      58                 :         12 :     return MP_OBJ_FROM_PTR(self);
      59                 :            : }
      60                 :            : 
      61                 :         92 : static mp_uint_t micropython_ringio_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
      62                 :         92 :     micropython_ringio_obj_t *self = MP_OBJ_TO_PTR(self_in);
      63         [ +  - ]:         92 :     size = MIN(size, ringbuf_avail(&self->ringbuffer));
      64                 :         92 :     ringbuf_memcpy_get_internal(&(self->ringbuffer), buf_in, size);
      65                 :         92 :     *errcode = 0;
      66                 :         92 :     return size;
      67                 :            : }
      68                 :            : 
      69                 :         96 : static mp_uint_t micropython_ringio_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
      70                 :         96 :     micropython_ringio_obj_t *self = MP_OBJ_TO_PTR(self_in);
      71         [ +  + ]:         96 :     size = MIN(size, ringbuf_free(&self->ringbuffer));
      72                 :         96 :     ringbuf_memcpy_put_internal(&(self->ringbuffer), buf_in, size);
      73                 :         96 :     *errcode = 0;
      74                 :         96 :     return size;
      75                 :            : }
      76                 :            : 
      77                 :        105 : static mp_uint_t micropython_ringio_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
      78                 :        105 :     micropython_ringio_obj_t *self = MP_OBJ_TO_PTR(self_in);
      79      [ +  +  - ]:        105 :     switch (request) {
      80                 :        101 :         case MP_STREAM_POLL: {
      81                 :        101 :             mp_uint_t ret = 0;
      82   [ +  +  +  + ]:        101 :             if ((arg & MP_STREAM_POLL_RD) && ringbuf_avail(&self->ringbuffer) > 0) {
      83                 :         32 :                 ret |= MP_STREAM_POLL_RD;
      84                 :            :             }
      85   [ +  +  +  + ]:        101 :             if ((arg & MP_STREAM_POLL_WR) && ringbuf_free(&self->ringbuffer) > 0) {
      86                 :         28 :                 ret |= MP_STREAM_POLL_WR;
      87                 :            :             }
      88                 :            :             return ret;
      89                 :            :         }
      90                 :            :         case MP_STREAM_CLOSE:
      91                 :            :             return 0;
      92                 :            :     }
      93                 :          4 :     *errcode = MP_EINVAL;
      94                 :          4 :     return MP_STREAM_ERROR;
      95                 :            : }
      96                 :            : 
      97                 :         16 : static mp_obj_t micropython_ringio_any(mp_obj_t self_in) {
      98                 :         16 :     micropython_ringio_obj_t *self = MP_OBJ_TO_PTR(self_in);
      99                 :         16 :     return MP_OBJ_NEW_SMALL_INT(ringbuf_avail(&self->ringbuffer));
     100                 :            : }
     101                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(micropython_ringio_any_obj, micropython_ringio_any);
     102                 :            : 
     103                 :            : static const mp_rom_map_elem_t micropython_ringio_locals_dict_table[] = {
     104                 :            :     { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&micropython_ringio_any_obj) },
     105                 :            :     { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
     106                 :            :     { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
     107                 :            :     { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
     108                 :            :     { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
     109                 :            :     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
     110                 :            : 
     111                 :            : };
     112                 :            : static MP_DEFINE_CONST_DICT(micropython_ringio_locals_dict, micropython_ringio_locals_dict_table);
     113                 :            : 
     114                 :            : static const mp_stream_p_t ringio_stream_p = {
     115                 :            :     .read = micropython_ringio_read,
     116                 :            :     .write = micropython_ringio_write,
     117                 :            :     .ioctl = micropython_ringio_ioctl,
     118                 :            :     .is_text = false,
     119                 :            : };
     120                 :            : 
     121                 :            : MP_DEFINE_CONST_OBJ_TYPE(
     122                 :            :     mp_type_ringio,
     123                 :            :     MP_QSTR_RingIO,
     124                 :            :     MP_TYPE_FLAG_NONE,
     125                 :            :     make_new, micropython_ringio_make_new,
     126                 :            :     protocol, &ringio_stream_p,
     127                 :            :     locals_dict, &micropython_ringio_locals_dict
     128                 :            :     );
     129                 :            : 
     130                 :            : #endif // MICROPY_PY_MICROPYTHON_RINGIO

Generated by: LCOV version 1.15-5-g462f71d