Vergil Cola
/
MQTTGatewayK64
Fork of my MQTTGateway
XbeeMonitor/XBeeLib/FrameBuffer/FrameBuffer.cpp@0:f1d3878b8dd9, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:45:51 2017 +0000
- Revision:
- 0:f1d3878b8dd9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:f1d3878b8dd9 | 1 | /** |
vpcola | 0:f1d3878b8dd9 | 2 | * Copyright (c) 2015 Digi International Inc., |
vpcola | 0:f1d3878b8dd9 | 3 | * All rights not expressly granted are reserved. |
vpcola | 0:f1d3878b8dd9 | 4 | * |
vpcola | 0:f1d3878b8dd9 | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
vpcola | 0:f1d3878b8dd9 | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
vpcola | 0:f1d3878b8dd9 | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
vpcola | 0:f1d3878b8dd9 | 8 | * |
vpcola | 0:f1d3878b8dd9 | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
vpcola | 0:f1d3878b8dd9 | 10 | * ======================================================================= |
vpcola | 0:f1d3878b8dd9 | 11 | */ |
vpcola | 0:f1d3878b8dd9 | 12 | |
vpcola | 0:f1d3878b8dd9 | 13 | #include "FrameBuffer.h" |
vpcola | 0:f1d3878b8dd9 | 14 | #include "Utils/Debug.h" |
vpcola | 0:f1d3878b8dd9 | 15 | |
vpcola | 0:f1d3878b8dd9 | 16 | #if !(defined AVOID_DISABLE_IRQS) |
vpcola | 0:f1d3878b8dd9 | 17 | #define disable_irq() __disable_irq() |
vpcola | 0:f1d3878b8dd9 | 18 | #define enable_irq() __enable_irq() |
vpcola | 0:f1d3878b8dd9 | 19 | #else |
vpcola | 0:f1d3878b8dd9 | 20 | #define disable_irq() |
vpcola | 0:f1d3878b8dd9 | 21 | #define enable_irq() |
vpcola | 0:f1d3878b8dd9 | 22 | #endif |
vpcola | 0:f1d3878b8dd9 | 23 | |
vpcola | 0:f1d3878b8dd9 | 24 | FrameBuffer::FrameBuffer(uint8_t size, uint16_t max_payload_len) : _size(size), _head(0), _tail(0), _dropped_frames(0) |
vpcola | 0:f1d3878b8dd9 | 25 | { |
vpcola | 0:f1d3878b8dd9 | 26 | _frm_buf = new buf_element_t[_size]; |
vpcola | 0:f1d3878b8dd9 | 27 | |
vpcola | 0:f1d3878b8dd9 | 28 | assert(_frm_buf != NULL); |
vpcola | 0:f1d3878b8dd9 | 29 | |
vpcola | 0:f1d3878b8dd9 | 30 | for (int i = 0; i < _size; i++) { |
vpcola | 0:f1d3878b8dd9 | 31 | _frm_buf[i].frame = new ApiFrame(max_payload_len - 1); |
vpcola | 0:f1d3878b8dd9 | 32 | _frm_buf[i].status = FrameStatusFree; |
vpcola | 0:f1d3878b8dd9 | 33 | } |
vpcola | 0:f1d3878b8dd9 | 34 | } |
vpcola | 0:f1d3878b8dd9 | 35 | |
vpcola | 0:f1d3878b8dd9 | 36 | FrameBuffer::~FrameBuffer() |
vpcola | 0:f1d3878b8dd9 | 37 | { |
vpcola | 0:f1d3878b8dd9 | 38 | for (int i = 0; i < _size; i++) { |
vpcola | 0:f1d3878b8dd9 | 39 | delete _frm_buf[i].frame; |
vpcola | 0:f1d3878b8dd9 | 40 | } |
vpcola | 0:f1d3878b8dd9 | 41 | |
vpcola | 0:f1d3878b8dd9 | 42 | delete _frm_buf; |
vpcola | 0:f1d3878b8dd9 | 43 | } |
vpcola | 0:f1d3878b8dd9 | 44 | |
vpcola | 0:f1d3878b8dd9 | 45 | ApiFrame *FrameBuffer::get_next_free_frame(void) |
vpcola | 0:f1d3878b8dd9 | 46 | { |
vpcola | 0:f1d3878b8dd9 | 47 | uint8_t i = _head; |
vpcola | 0:f1d3878b8dd9 | 48 | ApiFrame *ret = NULL; |
vpcola | 0:f1d3878b8dd9 | 49 | |
vpcola | 0:f1d3878b8dd9 | 50 | do { |
vpcola | 0:f1d3878b8dd9 | 51 | if (_frm_buf[i].status == FrameStatusFree || _frm_buf[i].status == FrameStatusComplete) { |
vpcola | 0:f1d3878b8dd9 | 52 | if (_frm_buf[i].status == FrameStatusComplete) { |
vpcola | 0:f1d3878b8dd9 | 53 | _dropped_frames++; |
vpcola | 0:f1d3878b8dd9 | 54 | } |
vpcola | 0:f1d3878b8dd9 | 55 | _frm_buf[i].status = FrameStatusAssigned; |
vpcola | 0:f1d3878b8dd9 | 56 | ret = _frm_buf[i].frame; |
vpcola | 0:f1d3878b8dd9 | 57 | _head = ++i % _size; |
vpcola | 0:f1d3878b8dd9 | 58 | break; |
vpcola | 0:f1d3878b8dd9 | 59 | } |
vpcola | 0:f1d3878b8dd9 | 60 | i++; |
vpcola | 0:f1d3878b8dd9 | 61 | i = i % _size; |
vpcola | 0:f1d3878b8dd9 | 62 | } while (i != _head); |
vpcola | 0:f1d3878b8dd9 | 63 | |
vpcola | 0:f1d3878b8dd9 | 64 | return ret; |
vpcola | 0:f1d3878b8dd9 | 65 | } |
vpcola | 0:f1d3878b8dd9 | 66 | |
vpcola | 0:f1d3878b8dd9 | 67 | bool FrameBuffer::complete_frame(ApiFrame *frame) |
vpcola | 0:f1d3878b8dd9 | 68 | { |
vpcola | 0:f1d3878b8dd9 | 69 | bool ret = false; |
vpcola | 0:f1d3878b8dd9 | 70 | |
vpcola | 0:f1d3878b8dd9 | 71 | for (int i = 0; i < _size; i++) { |
vpcola | 0:f1d3878b8dd9 | 72 | if (_frm_buf[i].frame == frame) { |
vpcola | 0:f1d3878b8dd9 | 73 | _frm_buf[i].status = FrameStatusComplete; |
vpcola | 0:f1d3878b8dd9 | 74 | ret = true; |
vpcola | 0:f1d3878b8dd9 | 75 | break; |
vpcola | 0:f1d3878b8dd9 | 76 | } |
vpcola | 0:f1d3878b8dd9 | 77 | } |
vpcola | 0:f1d3878b8dd9 | 78 | |
vpcola | 0:f1d3878b8dd9 | 79 | return ret; |
vpcola | 0:f1d3878b8dd9 | 80 | } |
vpcola | 0:f1d3878b8dd9 | 81 | |
vpcola | 0:f1d3878b8dd9 | 82 | ApiFrame *FrameBuffer::get_next_complete_frame(void) |
vpcola | 0:f1d3878b8dd9 | 83 | { |
vpcola | 0:f1d3878b8dd9 | 84 | uint8_t i = _tail; |
vpcola | 0:f1d3878b8dd9 | 85 | ApiFrame *ret = NULL; |
vpcola | 0:f1d3878b8dd9 | 86 | |
vpcola | 0:f1d3878b8dd9 | 87 | do { |
vpcola | 0:f1d3878b8dd9 | 88 | disable_irq(); |
vpcola | 0:f1d3878b8dd9 | 89 | if (_frm_buf[i].status == FrameStatusComplete) { |
vpcola | 0:f1d3878b8dd9 | 90 | _frm_buf[i].status = FrameStatusAssigned; |
vpcola | 0:f1d3878b8dd9 | 91 | enable_irq(); |
vpcola | 0:f1d3878b8dd9 | 92 | ret = _frm_buf[i].frame; |
vpcola | 0:f1d3878b8dd9 | 93 | _tail = ++i % _size; |
vpcola | 0:f1d3878b8dd9 | 94 | break; |
vpcola | 0:f1d3878b8dd9 | 95 | } |
vpcola | 0:f1d3878b8dd9 | 96 | enable_irq(); |
vpcola | 0:f1d3878b8dd9 | 97 | i++; |
vpcola | 0:f1d3878b8dd9 | 98 | i = i % _size; |
vpcola | 0:f1d3878b8dd9 | 99 | } while (i != _tail); |
vpcola | 0:f1d3878b8dd9 | 100 | |
vpcola | 0:f1d3878b8dd9 | 101 | return ret; |
vpcola | 0:f1d3878b8dd9 | 102 | } |
vpcola | 0:f1d3878b8dd9 | 103 | |
vpcola | 0:f1d3878b8dd9 | 104 | bool FrameBuffer::free_frame(ApiFrame *frame) |
vpcola | 0:f1d3878b8dd9 | 105 | { |
vpcola | 0:f1d3878b8dd9 | 106 | bool ret = false; |
vpcola | 0:f1d3878b8dd9 | 107 | |
vpcola | 0:f1d3878b8dd9 | 108 | for (int i = 0; i < _size; i++) { |
vpcola | 0:f1d3878b8dd9 | 109 | if (_frm_buf[i].frame == frame) { |
vpcola | 0:f1d3878b8dd9 | 110 | _frm_buf[i].status = FrameStatusFree; |
vpcola | 0:f1d3878b8dd9 | 111 | ret = true; |
vpcola | 0:f1d3878b8dd9 | 112 | break; |
vpcola | 0:f1d3878b8dd9 | 113 | } |
vpcola | 0:f1d3878b8dd9 | 114 | } |
vpcola | 0:f1d3878b8dd9 | 115 | |
vpcola | 0:f1d3878b8dd9 | 116 | return ret; |
vpcola | 0:f1d3878b8dd9 | 117 | } |
vpcola | 0:f1d3878b8dd9 | 118 | |
vpcola | 0:f1d3878b8dd9 | 119 | uint32_t FrameBuffer::get_dropped_frames_count(void) |
vpcola | 0:f1d3878b8dd9 | 120 | { |
vpcola | 0:f1d3878b8dd9 | 121 | const uint32_t dropped_frames = _dropped_frames; |
vpcola | 0:f1d3878b8dd9 | 122 | |
vpcola | 0:f1d3878b8dd9 | 123 | _dropped_frames = 0; |
vpcola | 0:f1d3878b8dd9 | 124 | |
vpcola | 0:f1d3878b8dd9 | 125 | return dropped_frames; |
vpcola | 0:f1d3878b8dd9 | 126 | } |