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.

528 lines
16 KiB

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