Garmin Fleet Management Controller  2.19.0
util.cpp File Reference
#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <zlib.h>
#include "util.h"

Go to the source code of this file.

Functions

CString unzip (char *original)
 This function will uncompress a file into a new file. More...
 
BOOL unzip (char *original, LPCTSTR destination)
 This function will uncompress a file into a specified file. More...
 
void UTIL_calc_2d_direction (float32 aNorthVelocity, float32 aEastVelocity, char *aCardinalDirection, int aCardinalDirectionSize)
 Determine the nearest cardinal aCardinalDirection. More...
 
double UTIL_calc_2d_speed (float32 aNorthVelocity, float32 aEastVelocity)
 Determine the two-dimensional velocity. More...
 
boolean UTIL_convert_date_time_to_seconds (const date_time_data_type *aDateTime, time_type *aSeconds)
 Converts a date from from a structure to a Garmin date. More...
 
double UTIL_convert_degrees_to_radians (double aDegrees)
 Converts a latitude/longitude from degrees to radians. More...
 
sint32 UTIL_convert_degrees_to_semicircles (double aDegrees)
 Converts a latitude/longitude from degrees to semicircles. More...
 
void UTIL_convert_gps_time_to_seconds (const gps_time_type *aGpsTime, time_type *aSeconds)
 Converts from gps_time to Garmin time. More...
 
void UTIL_convert_local_to_UTC (const time_type *aLocalTime, time_type *aUtcTime)
 Converts a time_type from local to UTC time. More...
 
double UTIL_convert_radians_to_degrees (double aRadians)
 Converts a latitude/longitude from radians to degrees. More...
 
void UTIL_convert_seconds_to_date_type (const time_type *aSeconds, date_time_data_type *aDateTime)
 Converts a Garmin date to a structure containing year, month, and day. More...
 
void UTIL_convert_seconds_to_gps_time (const time_type *aSeconds, gps_time_type *aGpsTime)
 Converts Garmin time to a structure containing GPS time. More...
 
void UTIL_convert_seconds_to_time_type (const time_type *aSeconds, date_time_data_type *aDateTime)
 Converts from a Garmin time to a structure containing separate members for hour, minute, and second (time_type). More...
 
double UTIL_convert_semicircles_to_degrees (sint32 aSemicircles)
 Converts a latitude/longitude from semicircles to degrees. More...
 
void UTIL_convert_time_type_to_seconds (const date_time_data_type *aDateTime, time_type *aSeconds)
 Converts a time_type to seconds since midnight. More...
 
void UTIL_convert_UTC_to_local (const time_type *aUtcTime, time_type *aLocalTime)
 Converts a time_type from UTC to local time. More...
 
bool UTIL_data_is_printable (const char *aData, int aLength)
 Determine whether an array of characters consists only of printable ASCII. More...
 
bool UTIL_data_is_uint32 (const char *aData)
 Determine whether an array of characters consists only of numeric, and that the value when converted from a string is in the valid range for a uint32. More...
 
void UTIL_format_date_string (const date_time_data_type *aDateTime, char *aResultString, int aResultStringSize)
 Format a date as a string. More...
 
void UTIL_format_time_string (const date_time_data_type *aDateTime, char *aResultString, int aResultStringSize)
 Converts a time structure (date_time_data_type) to a time string representation. More...
 
time_type UTIL_get_current_garmin_time ()
 Get the current server time in Garmin format. More...
 
time_type UTIL_get_garmin_time (tm aTimeTm)
 Get the current server time in Garmin format. More...
 
uint8 UTIL_hex_to_uint16 (const char *aHexString, uint16 *aBinaryData, uint8 aMaxWords)
 Converts a null-terminated string in hexadecimal format to an array of uint16 numbers, assuming natural byte ordering in the hex. No prefix should be present in the hex string. More...
 
uint16 UTIL_hex_to_uint8 (const char *aHexString, uint8 *aBinaryData, uint16 aMaxBytes)
 Convert a hexadecimal ASCII string to an array of uint8. More...
 
void UTIL_uint8_to_hex (const uint8 *aData, char *aOutput, uint8 aNumBytes)
 Convert from binary to a hexadecimal string. More...
 
void write (CFile *catalog, char *text)
 This function is a helper for writing text to a file. More...
 
void write (CFile *catalog, LPCTSTR wText)
 This function is a helper for writing Unicode text to a file. More...
 
void write (CFile *catalog, LPCTSTR wText, int maxLength)
 This function is a helper for writing Unicode text to a file. More...
 

Variables

static uint8 const sMonthDays [12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
 Number of days in each month in a non-leap year. More...
 
static uint32 const sYearDays [12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }
 Cumulative number of days in prior months in a non-leap year. More...
 

Function Documentation

CString unzip ( char *  original)

This function will uncompress a file into a new file.

If the original file was not gz compressed then the result file will be an exact copy of the original

Definition at line 741 of file util.cpp.

Referenced by CIftaDlg::OnEventIftaFileTransferReceived(), and CCustomFormsDlg::OnFormSubmitReceived().

BOOL unzip ( char *  original,
LPCTSTR  destination 
)

This function will uncompress a file into a specified file.

If the original file was not gz compressed then the result file will be an exact copy of the original

Definition at line 771 of file util.cpp.

References FALSE, and TRUE.

void UTIL_calc_2d_direction ( float32  aNorthVelocity,
float32  aEastVelocity,
char *  aCardinalDirection,
int  aCardinalDirectionSize 
)

Determine the nearest cardinal aCardinalDirection.

Parameters
aNorthVelocityThe north velocity (negative is south)
aEastVelocityThe aEastVelocity velocity (negative is west)
aCardinalDirectionA buffer of at least 3 bytes to contain the cardinal direction, which is one of "N", "E", "S", "W", "NE", "NW", "SE", "SW", or ""

Definition at line 692 of file util.cpp.

References PI.

Referenced by FmiApplicationLayer::calculate2DVelocity(), and UTIL_calc_2d_speed().

double UTIL_calc_2d_speed ( float32  aNorthVelocity,
float32  aEastVelocity 
)

Determine the two-dimensional velocity.

Parameters
aNorthVelocityThe north velocity (negative is south)
aEastVelocityThe east velocity (negative is west)
Returns
The 2d velocity, which is sqrt( aNorthVelocity^2 + aEastVelocity^2 ).

Definition at line 674 of file util.cpp.

References UTIL_calc_2d_direction().

Referenced by FmiApplicationLayer::calculate2DVelocity(), and UTIL_format_date_string().

boolean UTIL_convert_date_time_to_seconds ( const date_time_data_type aDateTime,
time_type aSeconds 
)

Converts a date from from a structure to a Garmin date.

Parameters
aDateTimeStructure containing year, month, and day
aSecondsNumber of seconds since Dec 31, 1989
Returns
TRUE if the conversion was successful, FALSE if the date_type does not represent a valid date between Dec 31, 1989 and Feb 5, 2126.

Definition at line 114 of file util.cpp.

References BASE_YEAR, date_time_data_type::date, date_time_data_type::_date::day, DAYS_IN_1_YEAR, DAYS_IN_4_YEARS, FALSE, FEBRUARY, date_time_data_type::_date::month, returnif_v, SECONDS_PER_DAY, sYearDays, TRUE, UTIL_convert_degrees_to_radians(), and date_time_data_type::_date::year.

Referenced by convertDateTime(), convertToTimeType(), parseDateTime(), and UTIL_convert_seconds_to_date_type().

double UTIL_convert_degrees_to_radians ( double  aDegrees)

Converts a latitude/longitude from degrees to radians.

Parameters
aDegreesThe latitude/longitude in degrees
Returns
The latitude/longitude in radians.

Definition at line 187 of file util.cpp.

References PI, and UTIL_convert_degrees_to_semicircles().

Referenced by UTIL_convert_date_time_to_seconds().

sint32 UTIL_convert_degrees_to_semicircles ( double  aDegrees)

Converts a latitude/longitude from degrees to semicircles.

2^31 semicircles equal 180 degrees. The conversion uses the equivalent 2^30 semicircles per 90 degrees to avoid overflow.

Parameters
aDegreesThe latitude/longitude in degrees
Returns
The latitude/longitude in semicircles.

Definition at line 204 of file util.cpp.

References UTIL_convert_seconds_to_gps_time().

Referenced by decodeDegreeFields(), CStopNewDlg::OnBnClickedOk(), FmiApplicationLayer::sendA602Stop(), FmiApplicationLayer::sendEnablePvtCommand(), FmiApplicationLayer::sendWaypoint(), and UTIL_convert_degrees_to_radians().

void UTIL_convert_gps_time_to_seconds ( const gps_time_type aGpsTime,
time_type aSeconds 
)

Converts from gps_time to Garmin time.

Parameters
aGpsTimeStructure containing week number and time of week
aSecondsThe Garmin time (seconds since Dec. 31, 1989 UTC)

Definition at line 233 of file util.cpp.

References SECONDS_PER_DAY, gps_time_type::time_of_week, and gps_time_type::week_number_days.

Referenced by FmiApplicationLayer::rx(), and UTIL_convert_seconds_to_gps_time().

void UTIL_convert_local_to_UTC ( const time_type aLocalTime,
time_type aUtcTime 
)

Converts a time_type from local to UTC time.

Parameters
[in]aUtcTimeThe UTC time as a time_type.
[out]aLocalTimeThe local time as a time_type.

Definition at line 549 of file util.cpp.

References SECONDS_PER_MINUTE, and UTIL_format_time_string().

Referenced by convertDateTime(), convertToTimeType(), parseDateTime(), and UTIL_convert_UTC_to_local().

double UTIL_convert_radians_to_degrees ( double  aRadians)

Converts a latitude/longitude from radians to degrees.

Parameters
aRadiansThe latitude/longitude in radians
Returns
The latitude/longitude in degrees

Definition at line 247 of file util.cpp.

References PI, and UTIL_convert_semicircles_to_degrees().

Referenced by FmiLogParser::getPacketDetail(), and FmiApplicationLayer::rx().

void UTIL_convert_seconds_to_date_type ( const time_type aSeconds,
date_time_data_type aDateTime 
)

Converts a Garmin date to a structure containing year, month, and day.

Parameters
aSecondsNumber of seconds since Dec 31, 1989
aDateTimeStructure containing year, month, and day

Definition at line 39 of file util.cpp.

References BASE_YEAR, date_time_data_type::date, date_time_data_type::_date::day, DAYS_IN_1_YEAR, DAYS_IN_4_YEARS, DECEMBER, FALSE, FEBRUARY, MARCH, date_time_data_type::_date::month, SECONDS_PER_DAY, sMonthDays, sYearDays, TRUE, UTIL_convert_date_time_to_seconds(), and date_time_data_type::_date::year.

Referenced by convertFromTimeType(), convertTimeTypeToString(), FmiLogParser::formatTime(), and FmiApplicationLayer::rx().

void UTIL_convert_seconds_to_gps_time ( const time_type aSeconds,
gps_time_type aGpsTime 
)

Converts Garmin time to a structure containing GPS time.

Parameters
aSecondsThe Garmin time (seconds since Dec. 31, 1989 UTC)
aGpsTimeStructure containing week number and time of week
Returns
TRUE, always.

Definition at line 218 of file util.cpp.

References SECONDS_PER_DAY, gps_time_type::time_of_week, UTIL_convert_gps_time_to_seconds(), and gps_time_type::week_number_days.

Referenced by UTIL_convert_degrees_to_semicircles().

void UTIL_convert_seconds_to_time_type ( const time_type aSeconds,
date_time_data_type aDateTime 
)

Converts from a Garmin time to a structure containing separate members for hour, minute, and second (time_type).

Parameters
aSecondsThe Garmin time to convert
aDateTimeThe output structure

Definition at line 274 of file util.cpp.

References date_time_data_type::_time::hour, date_time_data_type::_time::minute, date_time_data_type::_time::second, SECONDS_PER_DAY, SECONDS_PER_HOUR, SECONDS_PER_MINUTE, date_time_data_type::time, and UTIL_convert_time_type_to_seconds().

Referenced by convertFromTimeType(), convertTimeTypeToString(), FmiLogParser::formatTime(), CDashcamNtfctnFromClient::init(), CTxtMsgFromClient::OnInitDialog(), FmiApplicationLayer::rx(), and UTIL_convert_semicircles_to_degrees().

double UTIL_convert_semicircles_to_degrees ( sint32  aSemicircles)

Converts a latitude/longitude from semicircles to degrees.

Parameters
aSemicirclesThe latitude/longitude in semicircles
Returns
The latitude/longitude in degrees

Definition at line 260 of file util.cpp.

References UTIL_convert_seconds_to_time_type().

Referenced by encodeDegreeFields(), FmiLogParser::formatFmiPacket(), FmiLogParser::formatLatitude(), FmiLogParser::formatLongitude(), FmiLogParser::getPacketDetail(), CDashcamNtfctnFromClient::init(), CTxtMsgFromClient::OnInitDialog(), FmiApplicationLayer::rx(), and UTIL_convert_radians_to_degrees().

void UTIL_convert_time_type_to_seconds ( const date_time_data_type aDateTime,
time_type aSeconds 
)

Converts a time_type to seconds since midnight.

Parameters
aDateTimeThe structure containing hours, minutes, seconds
aSecondsThe number of seconds since midnight.

Definition at line 295 of file util.cpp.

References date_time_data_type::_time::hour, date_time_data_type::_time::minute, date_time_data_type::_time::second, SECONDS_PER_HOUR, SECONDS_PER_MINUTE, date_time_data_type::time, and UTIL_hex_to_uint8().

Referenced by convertDateTime(), convertToTimeType(), parseDateTime(), and UTIL_convert_seconds_to_time_type().

void UTIL_convert_UTC_to_local ( const time_type aUtcTime,
time_type aLocalTime 
)

Converts a time_type from UTC to local time.

Parameters
[in]aUtcTimeThe UTC time as a time_type.
[out]aLocalTimeThe local time as a time_type.

Definition at line 526 of file util.cpp.

References SECONDS_PER_MINUTE, and UTIL_convert_local_to_UTC().

Referenced by convertFromTimeType(), convertTimeTypeToString(), FmiLogParser::formatTime(), CDashcamNtfctnFromClient::init(), CTxtMsgFromClient::OnInitDialog(), FmiApplicationLayer::rx(), and UTIL_data_is_uint32().

bool UTIL_data_is_printable ( const char *  aData,
int  aLength 
)

Determine whether an array of characters consists only of printable ASCII.

Uses isprint().

Parameters
[in]aDataThe character array to test.
[in]aLengthThe number of characters to test.
Returns
TRUE if all characters are printable, FALSE otherwise.

Definition at line 471 of file util.cpp.

References UTIL_data_is_uint32().

Referenced by CGpiQueryDlg::OnGpiInfoReceived(), MessageId::toCString(), and UTIL_hex_to_uint16().

bool UTIL_data_is_uint32 ( const char *  aData)

Determine whether an array of characters consists only of numeric, and that the value when converted from a string is in the valid range for a uint32.

Parameters
[in]aDataThe character array to test.
Returns
true if the resulting string is a valid uint32, false otherwise.

Definition at line 495 of file util.cpp.

References max_uint_val, and UTIL_convert_UTC_to_local().

Referenced by CDriverIdAndStatusDlg::OnEnChangeEditSet(), CStopNewDlg::OnFormChanged(), and UTIL_data_is_printable().

void UTIL_format_date_string ( const date_time_data_type aDateTime,
char *  aResultString,
int  aResultStringSize 
)

Format a date as a string.

Formats the date portion of aDateTime as a string, in MM/DD/YYYY format

Parameters
aDateTimeThe date to format.
aResultStringPointer to a char[11] or longer to contain the formatted date.

Definition at line 657 of file util.cpp.

References date_time_data_type::date, date_time_data_type::_date::day, date_time_data_type::_date::month, UTIL_calc_2d_speed(), and date_time_data_type::_date::year.

Referenced by FmiLogParser::formatTime(), FmiApplicationLayer::rx(), and UTIL_get_garmin_time().

void UTIL_format_time_string ( const date_time_data_type aDateTime,
char *  aResultString,
int  aResultStringSize 
)

Converts a time structure (date_time_data_type) to a time string representation.

Parameters
[in]aDateTimeThe structure containing the time.
[out]aResultStringA null-terminated ASCII string of the form "HH:MM:SS AA" (hours, minutes, seconds, AM/PM, respectively).

Definition at line 575 of file util.cpp.

References FALSE, date_time_data_type::_time::hour, date_time_data_type::_time::minute, date_time_data_type::_time::second, date_time_data_type::time, and TRUE.

Referenced by FmiLogParser::formatTime(), CDashcamNtfctnFromClient::init(), CTxtMsgFromClient::OnInitDialog(), FmiApplicationLayer::rx(), and UTIL_convert_local_to_UTC().

time_type UTIL_get_garmin_time ( tm  aTimeTm)

Get the current server time in Garmin format.

Returns
The current time as a time_type

Definition at line 619 of file util.cpp.

References DAYS_IN_1_YEAR, SECONDS_PER_DAY, SECONDS_PER_HOUR, SECONDS_PER_MINUTE, sYearDays, and UTIL_format_date_string().

Referenced by UTIL_get_current_garmin_time().

uint8 UTIL_hex_to_uint16 ( const char *  aHexString,
uint16 aBinaryData,
uint8  aMaxWords 
)

Converts a null-terminated string in hexadecimal format to an array of uint16 numbers, assuming natural byte ordering in the hex. No prefix should be present in the hex string.

For example, given a hex string "01020304" and aMaxWords 2, the output will be the equivalent of { 258, 772 } decimal.

Note
Conversion will stop when an invalid hexadecimal digit is encountered in the input or when the null terminator is found, whichever comes first.
Parameters
[in]aHexStringThe ASCII null-terminated string containing the hex to convert.
[out]aBinaryDataPointer to an array of uint16 to contain the output.
[in]aMaxWordsThe maximum number of words to convert.
Returns
The number of complete words converted, or 0 if either aHexString contains a character that is not a hex digit or the length of aHexString is not a multiple of 4.

Definition at line 411 of file util.cpp.

References UTIL_data_is_printable().

Referenced by CFmiPcAppDlg::OnBnClickedSend(), and UTIL_uint8_to_hex().

uint16 UTIL_hex_to_uint8 ( const char *  aHexString,
uint8 aBinaryData,
uint16  aMaxBytes 
)

Convert a hexadecimal ASCII string to an array of uint8.

Parameters
[in]aHexStringThe hexadecimal string to convert. May be null-terminated. aHexString should not contain any prefix; only the digits 0-9 and a-f may be present.
[out]aBinaryDataAn array of uint8 to receive the output. This array's size must be at least max_bytes.
[in]aMaxBytesThe maximum number of bytes to convert. If aHexString is not null-terminated its length must be at least 2 * max_bytes.
Returns
The number of bytes actually converted. This may be less than max_bytes if a null terminator was found, and will be 0 if a character that is not a hexadecimal digit was encountered or if the length of aHexString is not even.
Note
aBinaryData may be modified even if 0 is returned.

Definition at line 323 of file util.cpp.

References UTIL_uint8_to_hex().

Referenced by FmiLogParser::getPacketDetail(), FmiLogParser::getPacketTitle(), MessageId::MessageId(), CFileTransferDlg::OnBnClickedOk(), CFmiPcAppDlg::OnBnClickedSend(), FmiLogParser::resendPacket(), FmiApplicationLayer::rx(), and UTIL_convert_time_type_to_seconds().

void UTIL_uint8_to_hex ( const uint8 aData,
char *  aOutput,
uint8  aNumBytes 
)

Convert from binary to a hexadecimal string.

Converts an array of uint8 values to a displayable string, one octet (uint8) at a time. No prefix is included in the output. For example, given a null-terminated ASCII string "ABC" and aNumBytes = 4, the output would be the string "41424300".

Parameters
[in]aDataThe binary data.
[out]aOutputThe null-terminated output string. This must be a buffer of at least (2 * aNumBytes + 1) characters.
[in]aNumBytesThe number of bytes to convert.

Definition at line 376 of file util.cpp.

References UTIL_hex_to_uint16().

Referenced by CGpiQueryDlg::OnGpiInfoReceived(), MessageId::toCString(), and UTIL_hex_to_uint8().

void write ( CFile *  catalog,
char *  text 
)

This function is a helper for writing text to a file.

Definition at line 799 of file util.cpp.

void write ( CFile *  catalog,
LPCTSTR  wText 
)

This function is a helper for writing Unicode text to a file.

Definition at line 807 of file util.cpp.

void write ( CFile *  catalog,
LPCTSTR  wText,
int  maxLength 
)

This function is a helper for writing Unicode text to a file.

Definition at line 820 of file util.cpp.

Variable Documentation

uint8 const sMonthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
static

Number of days in each month in a non-leap year.

Definition at line 25 of file util.cpp.

Referenced by UTIL_convert_seconds_to_date_type().

uint32 const sYearDays[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }
static

Cumulative number of days in prior months in a non-leap year.

Definition at line 30 of file util.cpp.

Referenced by UTIL_convert_date_time_to_seconds(), UTIL_convert_seconds_to_date_type(), and UTIL_get_garmin_time().