ChibiOS/RT Architecture - Reference Manual - Guides |
00001 /* 00002 ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio. 00003 00004 This file is part of ChibiOS/RT. 00005 00006 ChibiOS/RT is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 ChibiOS/RT is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 00019 --- 00020 00021 A special exception to the GPL can be applied should you wish to distribute 00022 a combined work that includes ChibiOS/RT, without being obliged to provide 00023 the source code for any proprietary components. See the file exception.txt 00024 for full details of how and when the exception can be applied. 00025 */ 00026 00027 /** 00028 * @file memstreams.c 00029 * @brief Memory streams code. 00030 * 00031 * @addtogroup memory_streams 00032 * @{ 00033 */ 00034 00035 #include <string.h> 00036 00037 #include "ch.h" 00038 #include "memstreams.h" 00039 00040 /* 00041 * @brief Write virtual method implementation. 00042 * 00043 * @param[in] ip pointer to a @p MemoryStream object 00044 * @param[in] bp pointer to the data buffer 00045 * @param[in] n the maximum amount of data to be transferred 00046 * @return The number of bytes transferred. The return value can 00047 * be less than the specified number of bytes if the 00048 * stream reaches a physical end of file and cannot be 00049 * extended. 00050 */ 00051 static size_t writes(void *ip, const uint8_t *bp, size_t n) { 00052 MemoryStream *msp = ip; 00053 00054 if (msp->size - msp->eos < n) 00055 n = msp->size - msp->eos; 00056 memcpy(msp->buffer + msp->eos, bp, n); 00057 msp->eos += n; 00058 return n; 00059 } 00060 00061 /* 00062 * @brief Read virtual method implementation. 00063 * 00064 * @param[in] ip pointer to a @p MemoryStream object 00065 * @param[out] bp pointer to the data buffer 00066 * @param[in] n the maximum amount of data to be transferred 00067 * @return The number of bytes transferred. The return value can 00068 * be less than the specified number of bytes if the 00069 * stream reaches the end of the available data. 00070 */ 00071 static size_t reads(void *ip, uint8_t *bp, size_t n) { 00072 MemoryStream *msp = ip; 00073 00074 if (msp->eos - msp->offset < n) 00075 n = msp->eos - msp->offset; 00076 memcpy(bp, msp->buffer + msp->offset, n); 00077 msp->offset += n; 00078 return n; 00079 } 00080 00081 static const struct MemStreamVMT vmt = {writes, reads}; 00082 00083 /** 00084 * @brief Memory stream object initialization. 00085 * 00086 * @param[out] msp pointer to the @p MemoryStream object to be initialized 00087 * @param[in] buffer pointer to the memory buffer for the memory stream 00088 * @param[in] size total size of the memory stream buffer 00089 * @param[in] eos initial End Of Stream offset. Normally you need to 00090 * put this to zero for RAM buffers or equal to @p size 00091 * for ROM streams. 00092 */ 00093 void msObjectInit(MemoryStream *msp, uint8_t *buffer, size_t size, size_t eos) { 00094 00095 msp->vmt = &vmt; 00096 msp->buffer = buffer; 00097 msp->size = size; 00098 msp->eos = eos; 00099 msp->offset = 0; 00100 } 00101 00102 /** @} */