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.

517 lines
15 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
  1. #include "basicFunctionality.h"
  2. //#ifdef DEBUG
  3. #include "basicLogCollection.h"
  4. #define URGANT_LOG(X) basicLogCollection::getInstance()->Log("basic Functionality", NULL, X )
  5. //#endif
  6. bool fileExist(const char *strFilename) {
  7. struct stat stFileInfo;
  8. int intStat;
  9. intStat = stat( strFilename ,&stFileInfo);
  10. if( intStat == 0 ){
  11. return true;
  12. };
  13. return false;
  14. };
  15. #ifndef __linux__
  16. #else
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. #include <sys/syscall.h>
  20. #include <sys/types.h>
  21. pid_t gettid( void ){
  22. return syscall(SYS_gettid);
  23. };
  24. #endif
  25. CSTR trim( CSTR val ){
  26. int i = 0;
  27. int j = strlen( val );
  28. if ( j ){
  29. while(( i < j ) && (((unsigned char)val[i])< 0x21 )) i++;
  30. while(( j > 0 ) && (((unsigned char)val[j])< 0x21 )) j--;
  31. if( ( i > 0 ) && ((j-i-1) > 0 ) ) memcpy( val, val+i, j-i-1 );
  32. val[j-i+1]=(char)0;
  33. };
  34. return val;
  35. };
  36. uint8_t checksum8( uint8_t *Buffer, size_t BufferSize ){
  37. uint16_t TempVal;
  38. TempVal = 0;
  39. for( int index = 0; index < BufferSize; index++ ) {
  40. TempVal+=Buffer[index];
  41. };
  42. return (uint8_t)(TempVal & 0x00FF);
  43. };
  44. uint16_t checksum16( uint8_t *Buffer, size_t BufferSize ){
  45. uint32_t TempVal;
  46. TempVal = 0;
  47. for( int index = 0; index < BufferSize; index++ ) {
  48. TempVal+=Buffer[index];
  49. };
  50. return (uint16_t)(TempVal & 0x0000FFFF);
  51. };
  52. uint8_t checksumLGS( uint8_t *Buffer, size_t BufferSize ){
  53. uint16_t TempVal;
  54. TempVal = 0;
  55. for( int index = 0; index < BufferSize; index++ ) {
  56. TempVal+=Buffer[index];
  57. };
  58. return (uint8_t)(TempVal & 0x00FF);
  59. };
  60. CArray *explode( char Delim, CSTR S_Str ){
  61. CArray *RetVal;
  62. CSTR b_ptr = S_Str;
  63. U16 s_ptr = 0;
  64. RetVal = NULL;
  65. if( S_Str ){
  66. RetVal = new CArray;
  67. while( ( U8 ) S_Str[s_ptr] ){
  68. if( (( U8 ) Delim ) == (( U8 )S_Str[s_ptr]) ){
  69. S_Str[s_ptr] = ( char ) 0;
  70. RetVal->push_back( b_ptr );
  71. s_ptr++;
  72. b_ptr = &S_Str[s_ptr];
  73. } else {
  74. s_ptr++;
  75. };
  76. };
  77. RetVal->push_back( b_ptr );
  78. };
  79. return RetVal;
  80. };
  81. CSTR implode( CArray *Data, CSTR S_Str, U8 Start, U8 End ){
  82. CSTR RetVal;
  83. U8 RStart = Start;
  84. U8 REnd = End;
  85. if( Start > Data->size() ) return NULL;
  86. if( End < Start ) return NULL;
  87. if( End > Data->size() ) REnd = Data->size()-1;
  88. if( strlen( (*Data).at( RStart )) ){
  89. RetVal = (CSTR) malloc( strlen( (*Data).at( RStart )) + 1 );
  90. sprintf( RetVal,"%s",(*Data).at( RStart ));
  91. } else {
  92. RetVal = (CSTR) malloc( 1 );
  93. *RetVal=(char)0;
  94. };
  95. for( int i = RStart+1; i < REnd; i++){
  96. RetVal = (CSTR) realloc ( RetVal, strlen( RetVal ) + strlen( (*Data).at( i )) + 1 + strlen( S_Str ) );
  97. sprintf( RetVal, "%s%s%s", RetVal, S_Str, (*Data).at( i ) );
  98. };
  99. return RetVal;
  100. };
  101. int getIP4( if_info **arrParam, int max_ifs ){
  102. int index = 0;
  103. #ifdef __linux__
  104. struct ifaddrs *addrs, *tmp;
  105. getifaddrs(&addrs);
  106. tmp = addrs;
  107. //arrParam= malloc( 10 * sezeof(if_info) );
  108. while (tmp){
  109. if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET){
  110. if( strncmp( tmp->ifa_name, "lo", 2 ) != 0 ){
  111. (*arrParam)[index].if_name = (CSTR)malloc(strlen(tmp->ifa_name)+1);
  112. snprintf( (*arrParam)[index].if_name, strlen(tmp->ifa_name)+1,"%s", tmp->ifa_name );
  113. (*arrParam)[index].if_addr = (CSTR)malloc(16);
  114. struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr;
  115. snprintf( (*arrParam)[index].if_addr, 16, "%s", inet_ntoa(pAddr->sin_addr) );
  116. index++;
  117. if( index == max_ifs ) break;
  118. }
  119. };
  120. tmp = tmp->ifa_next;
  121. };
  122. freeifaddrs(addrs);
  123. #endif
  124. return index;
  125. };
  126. #ifdef __linux__
  127. long long getFreeSpace( CSTR DirName ){
  128. struct statvfs fiData;
  129. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  130. return -1;
  131. };
  132. return fiData.f_bsize * fiData.f_bfree;
  133. };
  134. long long getTotalSpace( CSTR DirName ){
  135. struct statvfs fiData;
  136. if( ( statvfs( DirName, &fiData ) ) < 0 ) {
  137. return -1;
  138. };
  139. return fiData.f_bsize * fiData.f_blocks;
  140. };
  141. #else
  142. __int64 getFreeSpace( CSTR DirName ){
  143. return -1;
  144. };
  145. __int64 getTotalSpace( CSTR DirName ){
  146. return -1;
  147. };
  148. #endif
  149. CSTR getTStr( CSTR Fmt ){
  150. //CSTR RetVal = NULL;
  151. char DateStr[200];
  152. time_t t;
  153. struct tm *tmp;
  154. t = time(NULL);
  155. tmp = gmtime(&t);
  156. if( tmp == NULL) {
  157. fprintf(stderr, "localtime!");
  158. return NULL;
  159. };
  160. if( Fmt == NULL ) {
  161. if( strftime( DateStr, 200, "%F %T GMT", tmp )==0 ){
  162. fprintf( stderr, "strftime returned 0" );
  163. return NULL;
  164. };
  165. } else {
  166. if( strftime( DateStr, 200, Fmt, tmp )==0 ){
  167. fprintf( stderr, "strftime returned 0" );
  168. return NULL;
  169. };
  170. }
  171. //RetVal =(CSTR) malloc( strlen(DateStr) +1 );
  172. //if( RetVal != NULL ) strcpy( RetVal, DateStr );
  173. return strdup(DateStr);
  174. };
  175. #ifdef __linux__
  176. long long getMilliseconds( void ){
  177. long long tMilliseconds;
  178. struct timeval tData;
  179. struct timezone tTZ;
  180. tMilliseconds = (long long) 0;
  181. tTZ.tz_minuteswest = 0;
  182. tTZ.tz_dsttime = 0;
  183. if( gettimeofday( &tData, &tTZ ) == -1 ){
  184. tMilliseconds = (long long) 0;
  185. URGANT_LOG( "EbioMat!");
  186. } else {
  187. tMilliseconds = (long long) tData.tv_sec;
  188. tMilliseconds = ( tMilliseconds * 1000 );
  189. tMilliseconds += (long long)( tData.tv_usec / 1000 );
  190. //URGANT_LOG("MilSecCollected!");
  191. };
  192. #else
  193. #include<ctime>
  194. unsigned __int64 getMilliseconds( void ){
  195. unsigned __int64 tMilliseconds;
  196. SYSTEMTIME time;
  197. GetSystemTime(&time);
  198. tMilliseconds = (uint32_t)std::time(NULL);
  199. tMilliseconds *= 1000;
  200. tMilliseconds += time.wMilliseconds;
  201. #endif
  202. return tMilliseconds;
  203. };
  204. #ifdef __linux__
  205. bool enumerateDevices( enumeratorInfo *mlist, const char *dTemplate){
  206. DIR *d;
  207. struct dirent *dir;
  208. DeviceInfo *tDevInfo;
  209. if( mlist == NULL )return false;
  210. if( dTemplate == NULL )return false;
  211. for( int i = 0; i < mlist->size(); i++ ) {
  212. delete( mlist->at(i) );
  213. };
  214. mlist->clear();
  215. d = opendir("/dev");
  216. if( d ){
  217. while((dir = readdir(d)) != NULL){
  218. if( dir->d_type == DT_CHR || dir->d_type == DT_BLK ){
  219. if( strncmp( dir->d_name, dTemplate, 5 ) == 0 ) {
  220. tDevInfo = (DeviceInfo *) malloc( sizeof(DeviceInfo) );
  221. sprintf( tDevInfo->DevicePath, "/dev/%s", dir->d_name );
  222. tDevInfo->FriendlyName[0] =(char)0;
  223. if( strncmp( dTemplate, "video", 5 ) == 0 ){
  224. FILE *fd;
  225. char CMD[256];
  226. sprintf( CMD, "v4l2-ctl -d %s -D", tDevInfo->DevicePath );
  227. fd = popen( CMD, "r");
  228. if( fd != NULL ) {
  229. char * line = NULL;
  230. size_t len = 0;
  231. ssize_t rd;
  232. while ((rd = getline(&line, &len, fd)) != -1) {
  233. char *tS1, *tS2;
  234. // Future use strcasestr() or make both stings same case
  235. if( ( tS1 = strstr( line, "Bus info" )) != NULL ) {
  236. tS2 = trim( strstr( tS1, ":" ) + 2 );
  237. sprintf( tDevInfo->FriendlyName, "%s", tS2 );
  238. };
  239. };
  240. fclose( fd );
  241. if( line ) free( line );
  242. };
  243. };
  244. mlist->push_back( tDevInfo );
  245. //printf("%s\n", dir->d_name );
  246. };
  247. //if( strncmp( dir->d_name, "ttyS", 4 ) == 0 ){
  248. // tBuffer = ( char * ) malloc( strlen(dir->d_name)+1 );
  249. // strncpy( tBuffer, dir->d_name, strlen(dir->d_name)+1 );
  250. // mlist->pushBack( tBuffer );
  251. // //printf("%s\n", dir->d_name );
  252. //};
  253. };
  254. };
  255. closedir(d);
  256. } else {
  257. return false;
  258. };
  259. return true;
  260. };
  261. #else
  262. #ifndef IID_PPV_ARGS
  263. extern "C++" {
  264. template<typename T> void** IID_PPV_ARGS_Helper(T** pp)
  265. {
  266. // make sure everyone derives from IUnknown
  267. static_cast<IUnknown*>(*pp);
  268. return reinterpret_cast<void**>(pp);
  269. }
  270. }
  271. #define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
  272. #endif
  273. #pragma comment(lib,"Strmiids.lib")
  274. bool enumerateDevices(enumeratorInfo *mlist, REFGUID category){
  275. HRESULT hr;
  276. bool initOk = true;
  277. hr = CoInitializeEx( NULL, COINIT_SPEED_OVER_MEMORY | COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE );
  278. if( hr == E_INVALIDARG ){
  279. printf("Couldn't Init! hr=0x%x", hr);
  280. return false;
  281. } else if ( hr == E_OUTOFMEMORY ){
  282. printf("Couldn't Init! hr=0x%x", hr);
  283. return false;
  284. } else if ( hr == E_UNEXPECTED ){
  285. printf("Couldn't Init! hr=0x%x", hr);
  286. return false;
  287. } else if( hr == RPC_E_CHANGED_MODE ){
  288. initOk = false;
  289. };
  290. if( hr == S_OK || hr == RPC_E_CHANGED_MODE ){
  291. // Create the system device enumerator
  292. ICreateDevEnum *pDevEnum;
  293. hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum) );
  294. if( hr != S_OK ){
  295. printf("Couldn't create system enumerator! hr=0x%x", hr);
  296. CoUninitialize();
  297. return false;
  298. };
  299. if( pDevEnum != NULL ){
  300. IEnumMoniker *pClsEnum = NULL;
  301. hr = pDevEnum->CreateClassEnumerator(category, &pClsEnum, 0);
  302. if( hr == S_FALSE ){
  303. // The category is empty. Treat as an error.
  304. pDevEnum->Release();
  305. CoUninitialize();
  306. return false;
  307. };
  308. if( pClsEnum != NULL ){
  309. IMoniker *pMoniker = NULL;
  310. while( pClsEnum->Next(1, &pMoniker, NULL) == S_OK ){
  311. IPropertyBag *pPropBag = NULL;
  312. hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
  313. if( FAILED(hr) ){
  314. pMoniker->Release();
  315. continue;
  316. };
  317. VARIANT var;
  318. VariantInit(&var);
  319. DeviceInfo *tRec;
  320. tRec = (DeviceInfo *)malloc(sizeof(DeviceInfo));
  321. // Get description or friendly name.
  322. hr = pPropBag->Read(L"Description", &var, 0);
  323. if( FAILED(hr) ){
  324. hr = pPropBag->Read(L"FriendlyName", &var, 0);
  325. };
  326. if (SUCCEEDED(hr)){
  327. if (tRec != NULL) {
  328. sprintf(tRec->FriendlyName, "%S", var.bstrVal);
  329. mlist->push_back(tRec);
  330. };
  331. VariantClear(&var);
  332. };
  333. hr = pPropBag->Write(L"FriendlyName", &var);
  334. // WaveInID applies only to audio capture devices.
  335. hr = pPropBag->Read(L"WaveInID", &var, 0);
  336. if (SUCCEEDED(hr)){
  337. //printf("WaveIn ID: %d\n", var.lVal);
  338. VariantClear(&var);
  339. };
  340. hr = pPropBag->Read(L"DevicePath", &var, 0);
  341. if (SUCCEEDED(hr)){
  342. // The device path is not intended for display.
  343. if( tRec != NULL) {
  344. sprintf(tRec->DevicePath, "%S", var.bstrVal);
  345. };
  346. VariantClear(&var);
  347. };
  348. pPropBag->Release();
  349. pMoniker->Release();
  350. };
  351. pClsEnum->Release();
  352. };
  353. pDevEnum->Release();
  354. };
  355. if( initOk ) CoUninitialize();
  356. };
  357. return true;
  358. };
  359. #endif
  360. localIpAddresses g_vLocalIpAdressess;
  361. bool g_bAdressesAreDiscovered = false;
  362. #ifdef __linux__
  363. #include <linux/if.h>
  364. localIpAddresses *getLocalIP(){
  365. if( !g_bAdressesAreDiscovered ) {
  366. struct ifaddrs *ifaddr, *ifa;
  367. localIp4Info *tIpAddress = NULL;
  368. if (getifaddrs(&ifaddr) != -1) {
  369. /* Walk through linked list, maintaining head pointer so we can free list later */
  370. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  371. char protocol[IFNAMSIZ] = {0};
  372. if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) continue;
  373. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  374. if(tIpAddress!=NULL){
  375. sprintf( tIpAddress->address , "%d.%d.%d.%d",
  376. (uint8_t)ifa->ifa_addr->sa_data[2],
  377. (uint8_t)ifa->ifa_addr->sa_data[3],
  378. (uint8_t)ifa->ifa_addr->sa_data[4],
  379. (uint8_t)ifa->ifa_addr->sa_data[5]
  380. );
  381. sprintf( tIpAddress->netmask , "%d.%d.%d.%d",
  382. (uint8_t)ifa->ifa_netmask->sa_data[2],
  383. (uint8_t)ifa->ifa_netmask->sa_data[3],
  384. (uint8_t)ifa->ifa_netmask->sa_data[4],
  385. (uint8_t)ifa->ifa_netmask->sa_data[5]
  386. );
  387. sprintf( tIpAddress->broadcast , "%d.%d.%d.%d",
  388. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  389. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  390. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  391. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  392. );
  393. g_vLocalIpAdressess.push_back(tIpAddress);
  394. };
  395. };
  396. freeifaddrs(ifaddr);
  397. };
  398. g_bAdressesAreDiscovered = true;
  399. };
  400. return &g_vLocalIpAdressess;
  401. };
  402. #else
  403. //https://stackoverflow.com/questions/5213629/how-to-enumerate-all-available-network-interfaces
  404. #include <iphlpapi.h>
  405. #pragma comment(lib, "IPHLPAPI.lib")
  406. localIpAddresses* getLocalIP() {
  407. char psBuffer[128];
  408. FILE* fd;
  409. BYTE mB;
  410. localIp4Info* tIpAddress;
  411. if (!g_bAdressesAreDiscovered) {
  412. ULONG outBufLen = 0;
  413. DWORD dwRetVal = 0;
  414. IP_ADAPTER_INFO* pAdapterInfos = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
  415. // retry up to 5 times, to get the adapter infos needed
  416. for (int i = 0; i < 5 && (dwRetVal == ERROR_BUFFER_OVERFLOW || dwRetVal == NO_ERROR); ++i) {
  417. dwRetVal = GetAdaptersInfo(pAdapterInfos, &outBufLen);
  418. if (dwRetVal == NO_ERROR) {
  419. break;
  420. } else {
  421. if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
  422. free(pAdapterInfos);
  423. pAdapterInfos = (IP_ADAPTER_INFO*)malloc(outBufLen);
  424. } else {
  425. //pAdapterInfos = 0;
  426. break;
  427. };
  428. };
  429. };
  430. if (dwRetVal == NO_ERROR) {
  431. IP_ADAPTER_INFO* pAdapterInfo = pAdapterInfos;
  432. while (pAdapterInfo) {
  433. IP_ADDR_STRING* pIpAddress = &(pAdapterInfo->IpAddressList);
  434. while (pIpAddress != 0) {
  435. uint8_t A, B, C, D;
  436. uint8_t M, O, S, K, G, U, T, E;
  437. uint32_t Z1, Z2, Z3, Z4;
  438. int RetVal;
  439. RetVal = sscanf(pIpAddress->IpAddress.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  440. A = Z1 & 0xFF;
  441. B = Z2 & 0xFF;
  442. C = Z3 & 0xFF;
  443. D = Z4 & 0xFF;
  444. RetVal = sscanf(pIpAddress->IpMask.String, "%d.%d.%d.%d", &Z1, &Z2, &Z3, &Z4);
  445. M = Z1 & 0xFF;
  446. O = Z2 & 0xFF;
  447. S = Z3 & 0xFF;
  448. K = Z4 & 0xFF;
  449. //
  450. // <<<<
  451. // here pAdapterInfo->Address should contain the MAC address
  452. // >>>>
  453. //
  454. if (A > 0) {
  455. tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info));
  456. if (tIpAddress != NULL) {
  457. sprintf(tIpAddress->address, "%d.%d.%d.%d", A, B, C, D );
  458. sprintf(tIpAddress->netmask, "%d.%d.%d.%d", M, O, S, K );
  459. G = (A & M) | (~M);
  460. U = (B & O) | (~O);
  461. T = (C & S) | (~S);
  462. E = (D & K) | (~K);
  463. sprintf(tIpAddress->broadcast, "%d.%d.%d.%d", G, U, T, E );
  464. g_vLocalIpAdressess.push_back(tIpAddress);
  465. };
  466. };
  467. pIpAddress = pIpAddress->Next;
  468. };
  469. pAdapterInfo = pAdapterInfo->Next;
  470. };
  471. };
  472. free(pAdapterInfos);
  473. };
  474. return &g_vLocalIpAdressess;
  475. };
  476. #endif
  477. //#ifdef DEBUG
  478. #undef URGANT_LOG
  479. //#endif