You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
529 lines
14 KiB
529 lines
14 KiB
#include "basicFunctionality.h"
|
|
//#ifdef DEBUG
|
|
#include "basicLogCollection.h"
|
|
#define URGANT_LOG(X) basicLogCollection::getInstance()->Log("basic Functionality", NULL, X )
|
|
//#endif
|
|
|
|
bool fileExist(const char *strFilename) {
|
|
struct stat stFileInfo;
|
|
int intStat;
|
|
intStat = stat( strFilename ,&stFileInfo);
|
|
if( intStat == 0 ){
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
CSTR trim( CSTR val ){
|
|
int i = 0;
|
|
int j = strlen( val );
|
|
if ( j ){
|
|
while(( i < j ) && (((unsigned char)val[i])< 0x21 )) i++;
|
|
while(( j > 0 ) && (((unsigned char)val[j])< 0x21 )) j--;
|
|
if( ( i > 0 ) && ((j-i-1) > 0 ) ) memcpy( val, val+i, j-i-1 );
|
|
val[j-i+1]=(char)0;
|
|
};
|
|
return val;
|
|
};
|
|
|
|
uint8_t checksum8( uint8_t *Buffer, size_t BufferSize ){
|
|
uint16_t TempVal;
|
|
TempVal = 0;
|
|
for( int index = 0; index < BufferSize; index++ ) {
|
|
TempVal+=Buffer[index];
|
|
};
|
|
return (uint8_t)(TempVal & 0x00FF);
|
|
};
|
|
|
|
uint16_t checksum16( uint8_t *Buffer, size_t BufferSize ){
|
|
uint32_t TempVal;
|
|
TempVal = 0;
|
|
for( int index = 0; index < BufferSize; index++ ) {
|
|
TempVal+=Buffer[index];
|
|
};
|
|
return (uint16_t)(TempVal & 0x0000FFFF);
|
|
};
|
|
|
|
uint8_t checksumLGS( uint8_t *Buffer, size_t BufferSize ){
|
|
uint16_t TempVal;
|
|
TempVal = 0;
|
|
for( int index = 0; index < BufferSize; index++ ) {
|
|
TempVal+=Buffer[index];
|
|
};
|
|
return (uint8_t)(TempVal & 0x00FF);
|
|
};
|
|
|
|
CArray *explode( char Delim, CSTR S_Str ){
|
|
CArray *RetVal;
|
|
CSTR b_ptr = S_Str;
|
|
U16 s_ptr = 0;
|
|
RetVal = NULL;
|
|
if( S_Str ){
|
|
RetVal = new CArray;
|
|
while( ( U8 ) S_Str[s_ptr] ){
|
|
if( (( U8 ) Delim ) == (( U8 )S_Str[s_ptr]) ){
|
|
S_Str[s_ptr] = ( char ) 0;
|
|
RetVal->push_back( b_ptr );
|
|
s_ptr++;
|
|
b_ptr = &S_Str[s_ptr];
|
|
} else {
|
|
s_ptr++;
|
|
};
|
|
};
|
|
RetVal->push_back( b_ptr );
|
|
};
|
|
return RetVal;
|
|
};
|
|
|
|
CSTR implode( CArray *Data, CSTR S_Str, U8 Start, U8 End ){
|
|
CSTR RetVal;
|
|
U8 RStart = Start;
|
|
U8 REnd = End;
|
|
if( Start > Data->size() ) return NULL;
|
|
if( End < Start ) return NULL;
|
|
if( End > Data->size() ) REnd = Data->size()-1;
|
|
if( strlen( (*Data).at( RStart )) ){
|
|
RetVal = (CSTR) malloc( strlen( (*Data).at( RStart )) + 1 );
|
|
sprintf( RetVal,"%s",(*Data).at( RStart ));
|
|
} else {
|
|
RetVal = (CSTR) malloc( 1 );
|
|
*RetVal=(char)0;
|
|
};
|
|
for( int i = RStart+1; i < REnd; i++){
|
|
RetVal = (CSTR) realloc ( RetVal, strlen( RetVal ) + strlen( (*Data).at( i )) + 1 + strlen( S_Str ) );
|
|
sprintf( RetVal, "%s%s%s", RetVal, S_Str, (*Data).at( i ) );
|
|
};
|
|
return RetVal;
|
|
};
|
|
|
|
int getIP4( if_info **arrParam, int max_ifs ){
|
|
int index = 0;
|
|
#ifdef __linux__
|
|
struct ifaddrs *addrs, *tmp;
|
|
getifaddrs(&addrs);
|
|
tmp = addrs;
|
|
//arrParam= malloc( 10 * sezeof(if_info) );
|
|
while (tmp){
|
|
if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET){
|
|
if( strncmp( tmp->ifa_name, "lo", 2 ) != 0 ){
|
|
(*arrParam)[index].if_name = (CSTR)malloc(strlen(tmp->ifa_name)+1);
|
|
snprintf( (*arrParam)[index].if_name, strlen(tmp->ifa_name)+1,"%s", tmp->ifa_name );
|
|
(*arrParam)[index].if_addr = (CSTR)malloc(16);
|
|
struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
|
|
snprintf( (*arrParam)[index].if_addr, 16, "%s", inet_ntoa(pAddr->sin_addr) );
|
|
index++;
|
|
if( index == max_ifs ) break;
|
|
}
|
|
};
|
|
tmp = tmp->ifa_next;
|
|
};
|
|
freeifaddrs(addrs);
|
|
#endif
|
|
return index;
|
|
};
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
long long getFreeSpace( CSTR DirName ){
|
|
struct statvfs fiData;
|
|
if( ( statvfs( DirName, &fiData ) ) < 0 ) {
|
|
return -1;
|
|
};
|
|
return fiData.f_bsize * fiData.f_bfree;
|
|
};
|
|
|
|
long long getTotalSpace( CSTR DirName ){
|
|
struct statvfs fiData;
|
|
if( ( statvfs( DirName, &fiData ) ) < 0 ) {
|
|
return -1;
|
|
};
|
|
return fiData.f_bsize * fiData.f_blocks;
|
|
};
|
|
#else
|
|
__int64 getFreeSpace( CSTR DirName ){
|
|
return -1;
|
|
};
|
|
__int64 getTotalSpace( CSTR DirName ){
|
|
return -1;
|
|
};
|
|
#endif
|
|
|
|
|
|
CSTR getTStr( CSTR Fmt ){
|
|
CSTR RetVal = NULL;
|
|
char DateStr[200];
|
|
time_t t;
|
|
struct tm *tmp;
|
|
|
|
t = time(NULL);
|
|
tmp = gmtime(&t);
|
|
if( tmp == NULL) {
|
|
fprintf(stderr, "localtime!");
|
|
return NULL;
|
|
};
|
|
if( Fmt == NULL ) {
|
|
if( strftime( DateStr, 200, "%F %T GMT", tmp )==0 ){
|
|
fprintf( stderr, "strftime returned 0" );
|
|
return NULL;
|
|
};
|
|
} else {
|
|
if( strftime( DateStr, 200, Fmt, tmp )==0 ){
|
|
fprintf( stderr, "strftime returned 0" );
|
|
return NULL;
|
|
};
|
|
}
|
|
RetVal =(CSTR) malloc( strlen(DateStr) +1 );
|
|
if( RetVal != NULL ) strcpy( RetVal, DateStr );
|
|
return RetVal;
|
|
};
|
|
|
|
#ifdef __linux__
|
|
long long getMilliseconds( void ){
|
|
long long tMilliseconds;
|
|
struct timeval tData;
|
|
struct timezone tTZ;
|
|
tMilliseconds = (long long) 0;
|
|
tTZ.tz_minuteswest = 0;
|
|
tTZ.tz_dsttime = 0;
|
|
if( gettimeofday( &tData, &tTZ ) == -1 ){
|
|
tMilliseconds = (long long) 0;
|
|
URGANT_LOG( "EbioMat!");
|
|
} else {
|
|
tMilliseconds = (long long) tData.tv_sec;
|
|
tMilliseconds = ( tMilliseconds * 1000 );
|
|
tMilliseconds += (long long)( tData.tv_usec / 1000 );
|
|
//URGANT_LOG("MilSecCollected!");
|
|
};
|
|
#else
|
|
unsigned __int64 getMilliseconds( void ){
|
|
unsigned __int64 tMilliseconds;
|
|
SYSTEMTIME time;
|
|
GetSystemTime(&time);
|
|
tMilliseconds = (time.wSecond * 1000) + time.wMilliseconds;
|
|
#endif
|
|
return tMilliseconds;
|
|
};
|
|
|
|
|
|
#ifdef __linux__
|
|
bool enumerateDevices( enumeratorInfo *mlist, const char *dTemplate){
|
|
DIR *d;
|
|
struct dirent *dir;
|
|
DeviceInfo *tDevInfo;
|
|
if( mlist == NULL )return false;
|
|
if( dTemplate == NULL )return false;
|
|
for( int i = 0; i < mlist->size(); i++ ) {
|
|
delete( mlist->at(i) );
|
|
};
|
|
mlist->clear();
|
|
d = opendir("/dev");
|
|
if( d ){
|
|
while((dir = readdir(d)) != NULL){
|
|
if( dir->d_type == DT_CHR || dir->d_type == DT_BLK ){
|
|
if( strncmp( dir->d_name, dTemplate, 5 ) == 0 ) {
|
|
tDevInfo = (DeviceInfo *) malloc( sizeof(DeviceInfo) );
|
|
sprintf( tDevInfo->DevicePath, "/dev/%s", dir->d_name );
|
|
tDevInfo->FriendlyName[0] =(char)0;
|
|
if( strncmp( dTemplate, "video", 5 ) == 0 ){
|
|
FILE *fd;
|
|
char CMD[256];
|
|
sprintf( CMD, "v4l2-ctl -d %s -D", tDevInfo->DevicePath );
|
|
fd = popen( CMD, "r");
|
|
if( fd != NULL ) {
|
|
char * line = NULL;
|
|
size_t len = 0;
|
|
ssize_t rd;
|
|
while ((rd = getline(&line, &len, fd)) != -1) {
|
|
char *tS1, *tS2;
|
|
// Future use strcasestr() or make both stings same case
|
|
if( ( tS1 = strstr( line, "Bus info" )) != NULL ) {
|
|
tS2 = trim( strstr( tS1, ":" ) + 2 );
|
|
sprintf( tDevInfo->FriendlyName, "%s", tS2 );
|
|
};
|
|
};
|
|
fclose( fd );
|
|
if( line ) free( line );
|
|
};
|
|
};
|
|
mlist->push_back( tDevInfo );
|
|
//printf("%s\n", dir->d_name );
|
|
};
|
|
//if( strncmp( dir->d_name, "ttyS", 4 ) == 0 ){
|
|
// tBuffer = ( char * ) malloc( strlen(dir->d_name)+1 );
|
|
// strncpy( tBuffer, dir->d_name, strlen(dir->d_name)+1 );
|
|
// mlist->pushBack( tBuffer );
|
|
// //printf("%s\n", dir->d_name );
|
|
//};
|
|
};
|
|
};
|
|
closedir(d);
|
|
} else {
|
|
return false;
|
|
};
|
|
return true;
|
|
};
|
|
#else
|
|
#ifndef IID_PPV_ARGS
|
|
extern "C++" {
|
|
|
|
template<typename T> void** IID_PPV_ARGS_Helper(T** pp)
|
|
|
|
{
|
|
|
|
// make sure everyone derives from IUnknown
|
|
|
|
static_cast<IUnknown*>(*pp);
|
|
|
|
|
|
|
|
return reinterpret_cast<void**>(pp);
|
|
|
|
}
|
|
|
|
}
|
|
#define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
|
|
#endif
|
|
#pragma comment(lib,"Strmiids.lib")
|
|
bool enumerateDevices(enumeratorInfo *mlist, REFGUID category){
|
|
HRESULT hr;
|
|
bool initOk = true;
|
|
hr = CoInitializeEx( NULL, COINIT_SPEED_OVER_MEMORY | COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE );
|
|
if( hr == E_INVALIDARG ){
|
|
printf("Couldn't Init! hr=0x%x", hr);
|
|
return false;
|
|
} else if ( hr == E_OUTOFMEMORY ){
|
|
printf("Couldn't Init! hr=0x%x", hr);
|
|
return false;
|
|
} else if ( hr == E_UNEXPECTED ){
|
|
printf("Couldn't Init! hr=0x%x", hr);
|
|
return false;
|
|
} else if( hr == RPC_E_CHANGED_MODE ){
|
|
initOk = false;
|
|
};
|
|
if( hr == S_OK || hr == RPC_E_CHANGED_MODE ){
|
|
// Create the system device enumerator
|
|
ICreateDevEnum *pDevEnum;
|
|
hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum) );
|
|
if( hr != S_OK ){
|
|
printf("Couldn't create system enumerator! hr=0x%x", hr);
|
|
CoUninitialize();
|
|
return false;
|
|
};
|
|
if( pDevEnum != NULL ){
|
|
IEnumMoniker *pClsEnum = NULL;
|
|
hr = pDevEnum->CreateClassEnumerator(category, &pClsEnum, 0);
|
|
if( hr == S_FALSE ){
|
|
// The category is empty. Treat as an error.
|
|
pDevEnum->Release();
|
|
CoUninitialize();
|
|
return false;
|
|
};
|
|
if( pClsEnum != NULL ){
|
|
IMoniker *pMoniker = NULL;
|
|
while( pClsEnum->Next(1, &pMoniker, NULL) == S_OK ){
|
|
IPropertyBag *pPropBag = NULL;
|
|
hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
|
|
if( FAILED(hr) ){
|
|
pMoniker->Release();
|
|
continue;
|
|
};
|
|
VARIANT var;
|
|
VariantInit(&var);
|
|
DeviceInfo *tRec;
|
|
tRec = (DeviceInfo *)malloc(sizeof(DeviceInfo));
|
|
// Get description or friendly name.
|
|
hr = pPropBag->Read(L"Description", &var, 0);
|
|
if( FAILED(hr) ){
|
|
hr = pPropBag->Read(L"FriendlyName", &var, 0);
|
|
};
|
|
if (SUCCEEDED(hr)){
|
|
if (tRec != NULL) {
|
|
sprintf(tRec->FriendlyName, "%S", var.bstrVal);
|
|
mlist->push_back(tRec);
|
|
};
|
|
VariantClear(&var);
|
|
};
|
|
|
|
hr = pPropBag->Write(L"FriendlyName", &var);
|
|
|
|
// WaveInID applies only to audio capture devices.
|
|
hr = pPropBag->Read(L"WaveInID", &var, 0);
|
|
if (SUCCEEDED(hr)){
|
|
//printf("WaveIn ID: %d\n", var.lVal);
|
|
VariantClear(&var);
|
|
};
|
|
|
|
hr = pPropBag->Read(L"DevicePath", &var, 0);
|
|
if (SUCCEEDED(hr)){
|
|
// The device path is not intended for display.
|
|
if( tRec != NULL) {
|
|
sprintf(tRec->DevicePath, "%S", var.bstrVal);
|
|
};
|
|
VariantClear(&var);
|
|
};
|
|
pPropBag->Release();
|
|
pMoniker->Release();
|
|
};
|
|
pClsEnum->Release();
|
|
};
|
|
pDevEnum->Release();
|
|
};
|
|
if( initOk ) CoUninitialize();
|
|
};
|
|
return true;
|
|
};
|
|
#endif
|
|
|
|
|
|
localIpAddresses g_vLocalIpAdressess;
|
|
bool g_bAdressesAreDiscovered = false;
|
|
|
|
localIpAddresses *getLocalIP(){
|
|
char psBuffer[128];
|
|
FILE *fd;
|
|
BYTE mB;
|
|
localIp4Info *tIpAddress;
|
|
|
|
if( g_bAdressesAreDiscovered ) return &g_vLocalIpAdressess;
|
|
|
|
#ifdef __linux__
|
|
fd = popen("ifconfig", "r");
|
|
#else
|
|
fd = _popen("ipconfig.exe","r");
|
|
#endif
|
|
mB = 0;
|
|
tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
|
|
if (tIpAddress == NULL){
|
|
#ifdef __linux__
|
|
pclose(fd);
|
|
#else
|
|
_pclose(fd);
|
|
#endif
|
|
g_bAdressesAreDiscovered = true;
|
|
return &g_vLocalIpAdressess;
|
|
};
|
|
char tBuff[64];
|
|
//#if defined( WIN_XP ) || defined( __linux__ )
|
|
#ifdef __linux__
|
|
sprintf(tBuff, "inet addr");
|
|
#else
|
|
if (IsWindows7OrGreater()){
|
|
sprintf(tBuff, "IPv4 Address" );
|
|
} else {
|
|
sprintf(tBuff, "IP Address");
|
|
};
|
|
#endif
|
|
while (!feof(fd)){
|
|
if( fgets( psBuffer, 128, fd ) != NULL ){
|
|
if( strstr( psBuffer, tBuff ) != NULL ){
|
|
|
|
CSTR tVal = strstr(psBuffer, ":");
|
|
int tLen;
|
|
tLen = strlen(psBuffer) - (tVal - psBuffer) - 3;
|
|
if( tLen > 4){
|
|
#ifdef __linux__
|
|
sscanf( tVal+1,"%s ", &tIpAddress->address );
|
|
#else
|
|
strncpy(tIpAddress->address, tVal + 2, tLen);
|
|
#endif
|
|
tIpAddress->address[tLen] = (char)0;
|
|
} else {
|
|
tIpAddress->address[0] = (char)0;
|
|
}
|
|
mB |= 1;
|
|
};
|
|
#ifdef __linux__
|
|
if (strstr(psBuffer, "Mask") != NULL){
|
|
CSTR tVal = strstr(psBuffer, "Mask:");
|
|
int tLen;
|
|
tLen = strlen(tVal) - 6;
|
|
#else
|
|
if (strstr(psBuffer, "Subnet Mask") != NULL){
|
|
CSTR tVal = strstr(psBuffer, ":");
|
|
int tLen;
|
|
tLen = strlen(psBuffer) - (tVal - psBuffer) - 3;
|
|
#endif
|
|
|
|
if (tLen > 7){
|
|
#ifdef __linux__
|
|
strncpy(tIpAddress->netmask, tVal + 5, tLen);
|
|
#else
|
|
strncpy(tIpAddress->netmask, tVal + 2, tLen);
|
|
#endif
|
|
tIpAddress->netmask[tLen] = (char)0;
|
|
char A[4], B[4], C[4], D[4];
|
|
strncpy(A, tIpAddress->address, 3);
|
|
A[3] = (char)0;
|
|
strncpy(B, strstr(tIpAddress->address,".")+1, 3);
|
|
B[3] = (char)0;
|
|
strncpy(C, strstr(strstr(tIpAddress->address, ".")+1, ".") + 1, 3);
|
|
C[3] = (char)0;
|
|
strncpy(D, strstr(strstr(strstr(tIpAddress->address, ".") + 1, ".")+1,".") + 1, 3);
|
|
D[3] = (char)0;
|
|
BYTE a, b, c, d;
|
|
a = atoi(A); b = atoi(B); c = atoi(C); d = atoi(D);
|
|
char E[4], F[4], G[4], H[4];
|
|
strncpy(E, tIpAddress->netmask, 3);
|
|
E[3] = (char)0;
|
|
strncpy(F, strstr(tIpAddress->netmask, ".") + 1, 3);
|
|
F[3] = (char)0;
|
|
strncpy(G, strstr(strstr(tIpAddress->netmask, ".") + 1, ".") + 1, 3);
|
|
G[3] = (char)0;
|
|
strncpy(H, strstr(strstr(strstr(tIpAddress->netmask, ".") + 1, ".") + 1, ".") + 1, 3);
|
|
H[3] = (char)0;
|
|
BYTE e, f, g, h;
|
|
e = atoi(E); f = atoi(F); g = atoi(G); h = atoi(H);
|
|
a |= ~e; b |= ~f; c |= ~g; d |= ~h;
|
|
sprintf(tIpAddress->broadcast, "%d.%d.%d.%d", a, b, c, d);
|
|
} else {
|
|
tIpAddress->netmask[0] = (char)0;
|
|
tIpAddress->broadcast[0] = (char)0;
|
|
}
|
|
mB |= 2;
|
|
};
|
|
if (strstr(psBuffer, "Default Gateway") != NULL){
|
|
CSTR tVal = strstr(psBuffer, ":");
|
|
int tLen;
|
|
tLen = strlen(psBuffer) - (tVal - psBuffer) - 3;
|
|
if (tLen > 7){
|
|
strncpy(tIpAddress->gateway, tVal + 2, tLen);
|
|
tIpAddress->gateway[tLen] = (char)0;
|
|
} else {
|
|
tIpAddress->gateway[0] = (char)0;
|
|
}
|
|
mB |= 4;
|
|
};
|
|
#ifdef __linux__
|
|
if ((mB & 3) == 3){
|
|
#else
|
|
if ((mB & 7) == 7){
|
|
#endif
|
|
g_vLocalIpAdressess.push_back(tIpAddress);
|
|
tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
|
|
if (tIpAddress == NULL){
|
|
#ifdef __linux__
|
|
pclose(fd);
|
|
#else
|
|
_pclose(fd);
|
|
#endif
|
|
g_bAdressesAreDiscovered = true;
|
|
return &g_vLocalIpAdressess;
|
|
};
|
|
mB = 0;
|
|
};
|
|
};
|
|
};
|
|
#ifdef __linux__
|
|
pclose(fd);
|
|
#else
|
|
_pclose(fd);
|
|
#endif
|
|
g_bAdressesAreDiscovered = true;
|
|
return &g_vLocalIpAdressess;
|
|
};
|
|
|
|
//#ifdef DEBUG
|
|
#undef URGANT_LOG
|
|
//#endif
|
|
|