LingYun IoT Studio NB-IoT research project
guowenxue
2018-11-19 ec8c799d8bb2ee69b5e6f56201231e8c905edeb1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    Based on ringbuffer.c by Patrick Prasse (patrick.prasse@gmx.net). Code
    has been modified by Telenor and Gemalto.
 
 */
 
 
#include <string.h>
#include <assert.h>
#include "cp_ringbuf.h"
 
void rb_init (struct ring_buffer *ring, u_char* buff, int size) {
    memset (ring, 0, sizeof (struct ring_buffer));
    ring->rd_pointer = 0;
    ring->wr_pointer = 0;
    ring->buffer= buff;
    ring->size = size;
}
 
int
rb_write (struct ring_buffer *rb, u_char * buf, int len)
{
    int total;
    int i;
 
    /* total = len = min(space, len) */
    total = rb_free_size(rb);
    if(len > total)
        len = total;
    else
        total = len;
 
    i = rb->wr_pointer;
    if(i + len > rb->size)
    {
        memcpy(rb->buffer + i, buf, rb->size - i);
        buf += rb->size - i;
        len -= rb->size - i;
        i = 0;
    }
    memcpy(rb->buffer + i, buf, len);
    rb->wr_pointer = i + len;
    return total;
}
 
int rb_free_size (struct ring_buffer *rb){
    return (rb->size - 1 - rb_data_size(rb));
}
 
int rb_read (struct ring_buffer *rb, u_char * buf, int max) {
    int total;
    int i;
    /* total = len = min(used, len) */
    total = rb_data_size(rb);
 
    if(max > total)
        max = total;
    else
        total = max;
 
    i = rb->rd_pointer;
    if(i + max > rb->size)
    {
        memcpy(buf, rb->buffer + i, rb->size - i);
        buf += rb->size - i;
        max -= rb->size - i;
        i = 0;
    }
    memcpy(buf, rb->buffer + i, max);
    rb->rd_pointer = i + max;
 
    return total;
}
 
int rb_data_size (struct ring_buffer *rb) {
    return ((rb->wr_pointer - rb->rd_pointer) & (rb->size-1));
}
 
void rb_clear (struct ring_buffer *rb) {
    memset(rb->buffer,0,rb->size);
    rb->rd_pointer=0;
    rb->wr_pointer=0;
}
 
u_char rb_peek(struct ring_buffer* rb, int index) {
    assert(index < rb_data_size(rb));
 
    return rb->buffer[((rb->rd_pointer + index) % rb->size)];
}