ymodem.cpp

Go to the documentation of this file.
00001 /*
00002 This program is distributed under the terms of the 'MIT license'. The text
00003 of this licence follows...
00004 
00005 Copyright (c) 2006 J.D.Medhurst (a.k.a. Tixy)
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00020 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023 THE SOFTWARE.
00024 */
00025 
00032 #include "../common/common.h"
00033 #include "ymodem.h"
00034 
00035 
00036 uint16_t YModem::UpdateCRC16(uint16_t crcIn, uint8_t byte)
00037     {
00038     uint32_t crc = crcIn;
00039     uint32_t in = byte|0x100;
00040     do
00041         {
00042         crc <<= 1;
00043         in <<= 1;
00044         if(in&0x100)
00045             ++crc;
00046         if(crc&0x10000)
00047             crc ^= 0x1021;
00048         }
00049     while(!(in&0x10000));
00050     return crc&0xffffu;
00051     }
00052 
00053 
00054 uint16_t YModem::CRC16(const uint8_t* data, size_t size)
00055     {
00056     uint32_t crc = 0;
00057     const uint8_t* dataEnd = data+size;
00058     while(data<dataEnd)
00059         crc = UpdateCRC16(crc,*data++);
00060     crc = UpdateCRC16(crc,0);
00061     crc = UpdateCRC16(crc,0);
00062     return crc&0xffffu;
00063     }
00064 
00065 
00066 uint8_t YModem::Checksum(const uint8_t* data, size_t size)
00067     {
00068     int sum = 0;
00069     const uint8_t* dataEnd = data+size;
00070     while(data<dataEnd)
00071         sum += *data++;
00072     return sum&0xffu;
00073     }
00074 
00075 
00076 int YModem::InChar(unsigned timeout)
00077     {
00078     uint8_t c = 0;
00079     int result = Port.In(&c,1,timeout);
00080     if(result==1)
00081         return c;
00082     if(result==0)
00083         return ErrorTimeout;
00084     return result;
00085     }
00086 
00087 
00088 void YModem::Cancel()
00089     {
00090     const uint8_t CancelString[] = { CAN,CAN,CAN,CAN,CAN };
00091     Port.Out(CancelString,sizeof(CancelString),1000);
00092     }

Generated by  doxygen 1.6.1