Reaction Wheel Actuated Satellite Dynamics Test Platform

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RESIZE.cpp Source File

RESIZE.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #include "MODSERIAL.h"
00024 #include "MACROS.h"
00025 
00026 namespace AjK {
00027 
00028 int
00029 MODSERIAL::resizeBuffer(int size, IrqType type, bool memory_check)
00030 {
00031     int rval = Ok ;
00032     
00033     // If the requested size is the same as the current size there's nothing to do,
00034     // just continue to use the same buffer as it's fine as it is.
00035     if (buffer_size[type] == size) return rval;
00036     
00037     // Make sure the ISR cannot use the buffers while we are manipulating them.
00038     disableIrq();
00039     
00040     // If the requested buffer size is larger than the current size, 
00041     // attempt to create a new buffer and use it.
00042     if (buffer_size[type] < size) {
00043         rval = upSizeBuffer(size, type, memory_check);
00044     }
00045     else if (buffer_size[type] > size) {
00046         rval = downSizeBuffer(size, type, memory_check);
00047     }
00048     
00049     // Start the ISR system again with the new buffers.
00050     enableIrq();
00051     
00052     return rval;
00053 }
00054 
00055 int 
00056 MODSERIAL::downSizeBuffer(int size, IrqType type, bool memory_check)
00057 {
00058     if (size >= buffer_count[type]) {
00059         return BufferOversize ;
00060     }
00061     
00062     char *s = (char *)malloc(size);
00063     
00064     if (s == (char *)NULL) {
00065         if (memory_check) {
00066             error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
00067         }
00068         return NoMemory ;
00069     }
00070     
00071     int c, new_in = 0;
00072     
00073     do {
00074         c = __getc(false);
00075         if (c != -1) s[new_in++] = (char)c;
00076         if (new_in >= size) new_in = 0;
00077     }
00078     while (c != -1);
00079     
00080     free((char *)buffer[type]);
00081     buffer[type]      = s;
00082     buffer_in[type]   = new_in;
00083     buffer_out[type]  = 0;
00084     return Ok ;        
00085 }
00086 
00087 int 
00088 MODSERIAL::upSizeBuffer(int size, IrqType type, bool memory_check)
00089 {
00090     char *s = (char *)malloc(size);
00091     
00092     if (s == (char *)NULL) {
00093         if (memory_check) {
00094             error("Failed to allocate memory for %s buffer", type == TxIrq ? "TX" : "RX");
00095         }
00096         return NoMemory ;
00097     }
00098     
00099     if (buffer_count[type] == 0) { // Current buffer empty?
00100         free((char *)buffer[type]);
00101         buffer[type]      = s;
00102         buffer_in[type]   = 0;
00103         buffer_out[type]  = 0;
00104     }        
00105     else { // Copy the current contents into the new buffer.
00106         int c, new_in = 0;
00107         do {
00108             c = __getc(false);
00109             if (c != -1) s[new_in++] = (char)c;
00110             if (new_in >= size) new_in = 0; // Shouldn't happen, but be sure.
00111         }
00112         while (c != -1);
00113         free((char *)buffer[type]);
00114         buffer[type]      = s;
00115         buffer_in[type]   = new_in;
00116         buffer_out[type]  = 0;
00117     }
00118     
00119     buffer_size[type] = size;
00120     return Ok ;
00121 }
00122 
00123 }; // namespace AjK ends