src.dualinventive.com/mtinfo/tcpserver/legacy/lib/include/di-util/util.h

149 lines
3.9 KiB
C

/*
************************************************************************
**
** Copyright (c) 2009..2010 by
** Core|Vision B.V.
** Cereslaan 10b
** 5384 VT Heesch
** The Netherlands
**
** All Rights Reserved
**
************************************************************************
*/
/*
************************************************************************
**
** Project name: Dual Inventive: Utility Library
** Filename: util.h
** Author: Jack Weeland
** Date: July 3, 2009
** File version: 1.00 of July 3, 2009
**
************************************************************************
*/
/*
************************************************************************
**
** Utility functions - definications
**
************************************************************************
*/
#ifndef _DI_UTIL_H
#define _DI_UTIL_H
#include <stddef.h>
#include <time.h>
/*
** Data types
*/
// GPS position
// definitions for the 'flags' field
#define GPS_HAS_FIX 0x0001
#define GPS_HAS_POSITION 0x0002
#define GPS_HAS_ALTITUDE 0x0004
#define GPS_HAS_SPEED 0x0008 // and direction
#define GPS_HAS_TIME 0x0010
//
typedef struct GPS_POSITION
{
unsigned int flags;
// position
double latitude, longitude;
double hdop;
// altitude
double altitude;
// motion (speed direction)
double speed;
double direction;
// time
time_t time;
} gps_position_t;
/*
** Functions - Phone numbers
*/
// Suggested maximum length for a telephone number (ITU E.164 indicates 15, plus EOS char)
#define PHONENR_MAXLEN 16
// IMEI, IMSI and EF-CCID/ICCID a.k.a. SIM card number (ITU E.212, E.118 and GSM phase 4,
// w/o EOS char)
#define IMEI_MAXLEN 16
#define IMSI_MAXLEN 15
#define CCID_MAXLEN 20
// Convert a human readable phone number to a machine readable number
// (i.e. remove all spaces, brackets, hyphens, periods...)
int convert_phonenr(const char *human_readable, char *phonenr, size_t phonenr_sz);
/*
** Functions - GSM 03.38 charset translations
*/
// Character encodings
#define CHARSET_CP437 0
#define CHARSET_GSM0338 1
#define CHARSET_UTF8 2
#define CHARSET_UTF16 3
#define CHARSET_WINDOWS_1252 4
#define CHARSET_ISO8859_1 5
#define CHARSET_ISO8859_15 6
#define CHARSET(in,out) ((in) | ((out) << 8))
#define CHARSET_IN(charset) ((charset) & 0xFF)
#define CHARSET_OUT(charset) (((charset) >> 8) & 0xFF)
// Translate string to GSM 03.38 or v.v.
// Returns number of bytes in 'gsm0338', or -1 on error
// The output 'gsm0338' is _not_ NUL-terminated, because the NUL byte is
// an actual character in the GSM 03.38 character set.
int str2gsm0338(const void *str, unsigned char *gsm0338, size_t gsm0338_sz, int encoding);
int gsm03382str(const unsigned char *gsm0338, size_t gsm0338_sz, void *str, size_t str_sz, int encoding);
/*
** Functions - date/time and position
*/
// Convert string (formatted as "YYYY-MM-DD hh:mm:ss") to time_t
time_t str2time(const char*);
// Convert time_t to date/time string; 's' may be NULL in which case
// a static buffer will be used
const char *time2str(time_t, char *s);
const char *time2iso8601(time_t, char *s);
// Convert SMS time (string) to time_t
time_t smstime(const char*);
// Convert generalized time format (string, as used in the ASN.1 notation)
// to time_t
time_t gentime2time(const char*);
// Convert GPS week and seconds_of_week to UTC time
time_t gps2utc(unsigned int weeks, unsigned int seconds_of_week);
// Convert degrees to radians and v.v.
double gps_pos2rad(double pos);
double gps_rad2pos(double rad);
// Calculate the distance between two points over the surface of the earth
double gps_distance(const gps_position_t *p, const gps_position_t *q);
/*
** Functions - Luhn algorithm
*/
// Luhn algorithms to calculate the check digit for a sequence of
// digits, e.g. an IMEI number
// Return '-1' on error (invalid input; note that hyphens are allowed
// but whitespace is not)
int calculate_luhn(const char *s, int len);
int check_luhn(const char *s, int len);
#endif /* _DI_UTIL_H */