FIT_UINT16 Fit_GetMesgDefSize(const FIT_MESG_DEF *mesg_def) { if (mesg_def == FIT_NULL) return 0; return FIT_STRUCT_OFFSET(fields, FIT_MESG_DEF) + (FIT_UINT16)mesg_def->num_fields * FIT_FIELD_DEF_SIZE; }
void write_header(FILE *fp) { FIT_FILE_HDR file_header; file_header.header_size = FIT_FILE_HDR_SIZE; file_header.profile_version = FIT_PROFILE_VERSION; file_header.protocol_version = FIT_PROTOCOL_VERSION; memcpy((FIT_UINT8 *)&file_header.data_type, ".FIT", 4); fseek (fp , 0 , SEEK_END); file_header.data_size = ftell(fp) - FIT_FILE_HDR_SIZE - sizeof(FIT_UINT16); file_header.crc = FitCRC_Calc16(&file_header, FIT_STRUCT_OFFSET(crc, FIT_FILE_HDR)); fseek (fp , 0 , SEEK_SET); fwrite((void *)&file_header, 1, FIT_FILE_HDR_SIZE, fp); }
FIT_UINT8 Fit_LookupMessage(FIT_UINT16 global_mesg_num, FIT_UINT16 message_index, FIT_UINT32 *offset, FIT_READ_BYTES_FUNC read_bytes_func, FIT_BOOL read_header) { FIT_UINT16 global_mesg_nums[FIT_MAX_LOCAL_MESGS]; FIT_UINT8 sizes[FIT_MAX_LOCAL_MESGS]; FIT_UINT16 current_message_index = FIT_UINT16_INVALID; #if defined(FIT_MESSAGE_INDEX) FIT_UINT16 message_index_offset = FIT_UINT16_INVALID; #endif FIT_UINT8 i; *offset = 0; if (read_header) { if(read_bytes_func(offset, FIT_STRUCT_OFFSET(header_size, FIT_FILE_HDR), sizeof(FIT_UINT8)) != sizeof(FIT_UINT8)) return FIT_UINT8_INVALID; } for (i = 0; i < FIT_MAX_LOCAL_MESGS; i++) global_mesg_nums[i] = FIT_UINT16_INVALID; while (1) { FIT_UINT8 header; FIT_UINT8 local_mesg_num; if (read_bytes_func(&header, *offset, sizeof(header)) != sizeof(header)) return FIT_UINT8_INVALID; *offset += sizeof(header); if ((header & (FIT_HDR_TIME_REC_BIT | FIT_HDR_TYPE_DEF_BIT)) == FIT_HDR_TYPE_DEF_BIT) { FIT_MESG_DEF mesg_def_header; FIT_FIELD_DEF field_def; #if defined(FIT_MESSAGE_INDEX) FIT_UINT16 current_message_index_offset = FIT_UINT16_INVALID; // Initialize to invalid. If not found, it will remain invalid. #endif FIT_UINT8 current_size; FIT_UINT8 current_field_def; local_mesg_num = header & FIT_HDR_TYPE_MASK; if (read_bytes_func(&mesg_def_header, *offset, FIT_MESG_DEF_HEADER_SIZE) != FIT_MESG_DEF_HEADER_SIZE) return FIT_UINT8_INVALID; *offset += FIT_MESG_DEF_HEADER_SIZE; global_mesg_nums[local_mesg_num] = mesg_def_header.global_mesg_num; current_size = 0; for (current_field_def = 0; current_field_def < mesg_def_header.num_fields; current_field_def++) { if (read_bytes_func(&field_def, *offset, FIT_FIELD_DEF_SIZE) != FIT_FIELD_DEF_SIZE) return FIT_UINT8_INVALID; #if defined(FIT_MESSAGE_INDEX) if (field_def.field_def_num == FIT_FIELD_NUM_MESSAGE_INDEX) current_message_index_offset = current_size; #endif current_size += field_def.size; *offset += FIT_FIELD_DEF_SIZE; } sizes[local_mesg_num] = current_size; #if defined(FIT_MESSAGE_INDEX) if (global_mesg_nums[local_mesg_num] == global_mesg_num) message_index_offset = current_message_index_offset; #endif } else { if (header & FIT_HDR_TIME_REC_BIT) local_mesg_num = (header & FIT_HDR_TIME_TYPE_MASK) >> FIT_HDR_TIME_TYPE_SHIFT; else local_mesg_num = header & FIT_HDR_TYPE_MASK; if (global_mesg_nums[local_mesg_num] == global_mesg_num) { // If the requested message index is invalid, we've found a match. if (message_index == FIT_UINT16_INVALID) return local_mesg_num; #if defined(FIT_MESSAGE_INDEX) if (message_index_offset != FIT_UINT16_INVALID) { // Read the message index. if (read_bytes_func(¤t_message_index, *offset + message_index_offset, sizeof(current_message_index)) != sizeof(current_message_index)) return FIT_UINT8_INVALID; } else #endif { current_message_index++; } #if defined(FIT_MESSAGE_INDEX) if ((message_index & FIT_MESSAGE_INDEX_MASK) == (current_message_index & FIT_MESSAGE_INDEX_MASK)) #else if (message_index == current_message_index) #endif { return local_mesg_num; } } else if (global_mesg_nums[local_mesg_num] == FIT_UINT16_INVALID) { return FIT_UINT8_INVALID; } *offset += sizes[local_mesg_num]; } }