rbstream.c (7049B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2016 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #include <stdlib.h> 23 #include <string.h> 24 25 #include "rbstream.h" 26 #include "context-private.h" 27 #include "device-private.h" 28 29 struct dc_rbstream_t { 30 dc_device_t *device; 31 dc_rbstream_direction_t direction; 32 unsigned int pagesize; 33 unsigned int packetsize; 34 unsigned int begin; 35 unsigned int end; 36 unsigned int address; 37 unsigned int offset; 38 unsigned int available; 39 unsigned int skip; 40 unsigned char cache[]; 41 }; 42 43 static unsigned int 44 ifloor (unsigned int x, unsigned int n) 45 { 46 // Round down to next lower multiple. 47 return (x / n) * n; 48 } 49 50 static unsigned int 51 iceil (unsigned int x, unsigned int n) 52 { 53 // Round up to next higher multiple. 54 return ((x + n - 1) / n) * n; 55 } 56 57 dc_status_t 58 dc_rbstream_new (dc_rbstream_t **out, dc_device_t *device, unsigned int pagesize, unsigned int packetsize, unsigned int begin, unsigned int end, unsigned int address, dc_rbstream_direction_t direction) 59 { 60 dc_rbstream_t *rbstream = NULL; 61 62 if (out == NULL || device == NULL) 63 return DC_STATUS_INVALIDARGS; 64 65 // Page and packet size should be non-zero. 66 if (pagesize == 0 || packetsize == 0) { 67 ERROR (device->context, "Zero length page or packet size!"); 68 return DC_STATUS_INVALIDARGS; 69 } 70 71 // Packet size should be a multiple of the page size. 72 if (packetsize % pagesize != 0) { 73 ERROR (device->context, "Packet size not a multiple of the page size!"); 74 return DC_STATUS_INVALIDARGS; 75 } 76 77 // Ringbuffer boundaries should be aligned to the page size. 78 if (begin % pagesize != 0 || end % pagesize != 0) { 79 ERROR (device->context, "Ringbuffer not aligned to the page size!"); 80 return DC_STATUS_INVALIDARGS; 81 } 82 83 // Ringbuffer boundaries should not be reversed. 84 if (begin > end) { 85 ERROR (device->context, "Ringbuffer boundaries reversed!"); 86 return DC_STATUS_INVALIDARGS; 87 } 88 89 // Packet size should be smaller than the ringbuffer size. 90 if (packetsize > (end - begin)) { 91 ERROR (device->context, "Packet size larger than the ringbuffer size!"); 92 return DC_STATUS_INVALIDARGS; 93 } 94 95 // Address should be inside the ringbuffer. 96 if (address < begin || address > end) { 97 ERROR (device->context, "Address outside the ringbuffer!"); 98 return DC_STATUS_INVALIDARGS; 99 } 100 101 // Allocate memory. 102 rbstream = (dc_rbstream_t *) malloc (sizeof(*rbstream) + packetsize); 103 if (rbstream == NULL) { 104 ERROR (device->context, "Failed to allocate memory."); 105 return DC_STATUS_NOMEMORY; 106 } 107 108 rbstream->device = device; 109 rbstream->direction = direction; 110 rbstream->pagesize = pagesize; 111 rbstream->packetsize = packetsize; 112 rbstream->begin = begin; 113 rbstream->end = end; 114 if (direction == DC_RBSTREAM_FORWARD) { 115 rbstream->address = ifloor(address, pagesize); 116 rbstream->skip = address - rbstream->address; 117 } else { 118 rbstream->address = iceil(address, pagesize); 119 rbstream->skip = rbstream->address - address; 120 } 121 rbstream->offset = 0; 122 rbstream->available = 0; 123 124 *out = rbstream; 125 126 return DC_STATUS_SUCCESS; 127 } 128 129 static dc_status_t 130 dc_rbstream_read_backward (dc_rbstream_t *rbstream, dc_event_progress_t *progress, unsigned char data[], unsigned int size) 131 { 132 dc_status_t rc = DC_STATUS_SUCCESS; 133 134 unsigned int nbytes = 0; 135 unsigned int offset = size; 136 while (nbytes < size) { 137 if (rbstream->available == 0) { 138 // Handle the ringbuffer wrap point. 139 if (rbstream->address == rbstream->begin) 140 rbstream->address = rbstream->end; 141 142 // Calculate the packet size. 143 unsigned int len = rbstream->packetsize; 144 if (rbstream->begin + len > rbstream->address) 145 len = rbstream->address - rbstream->begin; 146 147 // Read the packet into the cache. 148 rc = dc_device_read (rbstream->device, rbstream->address - len, rbstream->cache, rbstream->packetsize); 149 if (rc != DC_STATUS_SUCCESS) 150 return rc; 151 152 // Move to the end of the next packet. 153 rbstream->address -= len; 154 155 rbstream->available = len - rbstream->skip; 156 rbstream->skip = 0; 157 } 158 159 unsigned int length = rbstream->available; 160 if (nbytes + length > size) 161 length = size - nbytes; 162 163 offset -= length; 164 rbstream->available -= length; 165 166 memcpy (data + offset, rbstream->cache + rbstream->available, length); 167 168 // Update and emit a progress event. 169 if (progress) { 170 progress->current += length; 171 device_event_emit (rbstream->device, DC_EVENT_PROGRESS, progress); 172 } 173 174 nbytes += length; 175 } 176 177 return rc; 178 } 179 180 static dc_status_t 181 dc_rbstream_read_forward (dc_rbstream_t *rbstream, dc_event_progress_t *progress, unsigned char data[], unsigned int size) 182 { 183 dc_status_t rc = DC_STATUS_SUCCESS; 184 185 unsigned int nbytes = 0; 186 while (nbytes < size) { 187 if (rbstream->available == 0) { 188 // Handle the ringbuffer wrap point. 189 if (rbstream->address == rbstream->end) 190 rbstream->address = rbstream->begin; 191 192 // Calculate the packet size. 193 unsigned int len = rbstream->packetsize; 194 if (rbstream->address + len > rbstream->end) 195 len = rbstream->end - rbstream->address; 196 197 // Calculate the excess number of bytes. 198 unsigned int extra = rbstream->packetsize - len; 199 200 // Read the packet into the cache. 201 rc = dc_device_read (rbstream->device, rbstream->address - extra, rbstream->cache, rbstream->packetsize); 202 if (rc != DC_STATUS_SUCCESS) 203 return rc; 204 205 // Move to the begin of the next packet. 206 rbstream->address += len; 207 208 rbstream->offset = extra + rbstream->skip; 209 rbstream->available = len - rbstream->skip; 210 rbstream->skip = 0; 211 } 212 213 unsigned int length = rbstream->available; 214 if (nbytes + length > size) 215 length = size - nbytes; 216 217 memcpy (data + nbytes, rbstream->cache + rbstream->offset, length); 218 219 rbstream->offset += length; 220 rbstream->available -= length; 221 222 // Update and emit a progress event. 223 if (progress) { 224 progress->current += length; 225 device_event_emit (rbstream->device, DC_EVENT_PROGRESS, progress); 226 } 227 228 nbytes += length; 229 } 230 231 return rc; 232 } 233 234 dc_status_t 235 dc_rbstream_read (dc_rbstream_t *rbstream, dc_event_progress_t *progress, unsigned char data[], unsigned int size) 236 { 237 if (rbstream == NULL) 238 return DC_STATUS_INVALIDARGS; 239 240 if (rbstream->direction == DC_RBSTREAM_FORWARD) { 241 return dc_rbstream_read_forward (rbstream, progress, data, size); 242 } else { 243 return dc_rbstream_read_backward (rbstream, progress, data, size); 244 } 245 } 246 247 dc_status_t 248 dc_rbstream_free (dc_rbstream_t *rbstream) 249 { 250 free (rbstream); 251 252 return DC_STATUS_SUCCESS; 253 }