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