Logo Search packages:      
Sourcecode: s51dude version File versions  Download package

int Read_IHexRecord ( IHexRecord ihexRecord,
FILE *  in 
)

Reads an Intel HEX record from an opened file.

Parameters:
ihexRecord A pointer to the Intel HEX record structure that will store the read record.
in A file pointer to an opened file that can be read.
Returns:
IHEX_OK on success, otherwise one of the IHEX_ERROR_ error codes.
Return values:
IHEX_OK on success.
IHEX_ERROR_INVALID_ARGUMENTS if ihexRecord does not point to a valid IHexRecord structure or the file pointer "in" is invalid.
IHEX_ERROR_EOF if end-of-file has been reached.
IHEX_ERROR_FILE if a file reading error has occured.
IHEX_INVALID_RECORD if the record read is invalid (record did not match specifications or record checksum was invalid).

Definition at line 33 of file ihex.c.

References _IHexRecord::address, _IHexRecord::checksum, _IHexRecord::data, _IHexRecord::dataLen, IHEX_ERROR_EOF, IHEX_ERROR_FILE, IHEX_ERROR_INVALID_ARGUMENTS, IHEX_ERROR_INVALID_RECORD, IHEX_OK, and _IHexRecord::type.

                                                      {
      char recordBuff[IHEX_RECORD_BUFF_SIZE];
      /* A temporary buffer to hold ascii hex encoded data, set to the maximum length we would ever need */
      char hexBuff[IHEX_ADDRESS_LEN+1];
      int dataCount, i;
            
      /* Check our file pointer and the IHexRecord struct */
      if (in == NULL || ihexRecord == NULL)
            return IHEX_ERROR_INVALID_ARGUMENTS;
            
      if (fgets(recordBuff, IHEX_RECORD_BUFF_SIZE, in) == NULL) {
                  /* In case we hit EOF, don't report a file error */
                  if (feof(in) != 0)
                        return IHEX_ERROR_EOF;
                  else
                        return IHEX_ERROR_FILE;
      }
      /* Get rid of that \n */
      recordBuff[strlen(recordBuff)-1] = 0;
      
      /* Size check for start code, count, addess, and type fields */
      if (strlen(recordBuff) < 1+IHEX_COUNT_LEN+IHEX_ADDRESS_LEN+IHEX_TYPE_LEN)
            return IHEX_ERROR_INVALID_RECORD;
            
      /* Check the for colon start code */
      if (recordBuff[IHEX_START_CODE_OFFSET] != IHEX_START_CODE)
            return IHEX_ERROR_INVALID_RECORD;
      
      /* Copy the ascii hex encoding of the count field into hexBuff, convert it to a usable integer */
      strncpy(hexBuff, recordBuff+IHEX_COUNT_OFFSET, IHEX_COUNT_LEN);
      hexBuff[IHEX_COUNT_LEN] = 0;
      dataCount = strtol(hexBuff, (char **)NULL, 16);
      
      /* Copy the ascii hex encoding of the address field into hexBuff, convert it to a usable integer */
      strncpy(hexBuff, recordBuff+IHEX_ADDRESS_OFFSET, IHEX_ADDRESS_LEN);
      hexBuff[IHEX_ADDRESS_LEN] = 0;
      ihexRecord->address = strtol(hexBuff, (char **)NULL, 16);
      
      /* Copy the ascii hex encoding of the address field into hexBuff, convert it to a usable integer */
      strncpy(hexBuff, recordBuff+IHEX_TYPE_OFFSET, IHEX_TYPE_LEN);
      hexBuff[IHEX_TYPE_LEN] = 0;
      ihexRecord->type = strtol(hexBuff, (char **)NULL, 16);
      
      /* Size check for start code, count, address, type, data and checksum fields */
      if (strlen(recordBuff) < 1+IHEX_COUNT_LEN+IHEX_ADDRESS_LEN+IHEX_TYPE_LEN+dataCount*2+IHEX_CHECKSUM_LEN)
            return IHEX_ERROR_INVALID_RECORD;
      
      /* Loop through each ascii hex byte of the data field, pull it out into hexBuff,
       * convert it and store the result in the data buffer of the Intel Hex record */
      for (i = 0; i < dataCount; i++) {
            /* Times two i because every byte is represented by two ascii hex characters */
            strncpy(hexBuff, recordBuff+IHEX_DATA_OFFSET+2*i, IHEX_ASCII_HEX_BYTE_LEN);
            hexBuff[IHEX_ASCII_HEX_BYTE_LEN] = 0;
            ihexRecord->data[i] = strtol(hexBuff, (char **)NULL, 16);
      }
      ihexRecord->dataLen = dataCount;    
      
      /* Copy the ascii hex encoding of the checksum field into hexBuff, convert it to a usable integer */
      strncpy(hexBuff, recordBuff+IHEX_DATA_OFFSET+dataCount*2, IHEX_CHECKSUM_LEN);
      hexBuff[IHEX_CHECKSUM_LEN] = 0;
      ihexRecord->checksum = strtol(hexBuff, (char **)NULL, 16);

      if (ihexRecord->checksum != Checksum_IHexRecord(*ihexRecord))
            return IHEX_ERROR_INVALID_RECORD;
      
      return IHEX_OK;
}


Generated by  Doxygen 1.6.0   Back to index