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.

1390 lines
54 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "hardwareManager.h"
  2. #include <sys/types.h>
  3. #include <sys/sysinfo.h>
  4. #include <sys/statvfs.h>
  5. #include <time.h>
  6. #ifdef USE_MYSQL
  7. #include "MySQL_dataBinder.h"
  8. #else
  9. #include "SQLite_dataBinder.h"
  10. #endif
  11. //#include "rgooseKernel.h"
  12. //#include "rgooseDBConnector.h"
  13. #include "hardwareResourceSerialProvider.h"
  14. #include "hardwareResourceNetworkProvider.h"
  15. #include "hardwareResourceVideoProvider.h"
  16. #include "../hardware/hid/hid_dc.h"
  17. #include "../alg/generic/heapDebug.h"
  18. #include <sys/time.h>
  19. #include <sys/resource.h>
  20. #include <sys/types.h>
  21. #include <sys/ioctl.h>
  22. #include <hidapi/hidapi.h>
  23. #include <ifaddrs.h>
  24. #include <linux/serial.h>
  25. #include <linux/videodev2.h>
  26. #define LOG( X ) m_pLog->Log( "HWM", this, X )
  27. #ifdef DEBUG
  28. #define LOG_ERROR( X ) {\
  29. char _tBfrErrMSG2w34[256];\
  30. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  31. m_pLog->Log( "HWM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  32. }
  33. #else
  34. #define LOG_ERROR( X ) m_pLog->Log( "HWM", this, _LOG_ERROR, X );
  35. #endif
  36. hardwareManager *hardwareManager::_instance = NULL;
  37. /**
  38. * Returns the single instance of the object.
  39. */
  40. hardwareManager* hardwareManager::getInstance( bool cold_start ) {
  41. // check if the instance has been created yet
  42. if( 0 == _instance ){
  43. // if not, then create it
  44. _instance = new hardwareManager( cold_start );
  45. };
  46. // return the single instance
  47. return _instance;
  48. };
  49. hardwareManager::hardwareManager( bool cold_start ){
  50. m_pLog = basicLogCollection::getInstance();
  51. //m_bHardwareEnumerated = false;
  52. m_nServerNodeID = 0;
  53. m_nServerID = 0;
  54. m_nSiteID = 0;
  55. m_nMemoryLimitHeapSize = 724288000; //524288000; //500 MB ~ 1 Hour
  56. memset( &m_sSYS_INFO, 0, sizeof(DBC_SYS_INFO) );
  57. strncpy( m_sSYS_INFO.SW_INFO, _BUILD_STRING_, 128 );
  58. FILE *fd = popen("/bin/uname -s -r -v -m","r");
  59. if( fd!= NULL ) {
  60. fgets( m_sSYS_INFO.OS_INFO, 127, fd);
  61. m_sSYS_INFO.OS_INFO[127] = (char)0;
  62. trim( m_sSYS_INFO.OS_INFO );
  63. memset( m_sSYS_INFO.HW_INFO, 0, 128 );
  64. fclose( fd );
  65. fd = NULL;
  66. };
  67. fd = fopen("/proc/cpuinfo","r");
  68. if ( fd!= NULL ) {
  69. char tBB[1024];
  70. bool bARM = false;
  71. while( !feof( fd ) ) {
  72. fgets( tBB, 1024, fd );
  73. trim( tBB );
  74. if( strcasestr(tBB, "model") != NULL ) {
  75. if( strcasestr(tBB, "model name") != NULL ) {
  76. if ( !bARM ) {
  77. if( strcasestr( tBB, "ARM" ) != NULL){
  78. bARM = true;
  79. } else {
  80. CArray *tA = explode(':', tBB );
  81. if ( tA ) {
  82. strncpy( m_sSYS_INFO.HW_INFO, tA->at(1), 127 );
  83. delete tA;
  84. };
  85. break;
  86. };
  87. };
  88. } else {
  89. CArray *tA = explode(':', tBB );
  90. if ( tA ) {
  91. strncpy( m_sSYS_INFO.HW_INFO, tA->at(1), 127 );
  92. delete tA;
  93. };
  94. break;
  95. };
  96. };
  97. };
  98. m_sSYS_INFO.HW_INFO[127] = (char)0;
  99. trim( m_sSYS_INFO.HW_INFO );
  100. fclose( fd );
  101. fd = NULL;
  102. };
  103. #ifdef USE_MYSQL
  104. m_pDBC = mysqlDBC::getInstance();
  105. #else
  106. m_pDBC = sqliteDBC::getInstance();
  107. #endif
  108. if( m_pDBC == NULL ) {
  109. LOG_ERROR( "Failure to get DBC Instance -> Forced HW Enumeration!" );
  110. enumerateHardware();
  111. } else {
  112. char tB[128];
  113. struct rlimit rl={0};
  114. getrlimit( RLIMIT_STACK, &rl );
  115. sprintf( tB, "Stack Size: %d", rl.rlim_cur ); //RLIM_INFINITY
  116. LOG( tB );
  117. sprintf( tB, "MAX Stack Size: %d", rl.rlim_max );
  118. LOG( tB );
  119. //enumerateHardware( !cold_start );
  120. enumerateNIFS();
  121. #ifdef DEBUG
  122. LOG("Load Ressources...");
  123. #endif
  124. loadResources();
  125. #ifdef DEBUG
  126. LOG("Load Resources Done");
  127. #endif
  128. };
  129. };
  130. hardwareManager::~hardwareManager(){
  131. #ifdef DEBUG
  132. LOG("Destructor Begin");
  133. #endif
  134. unloadResources();
  135. #ifdef DEBUG
  136. LOG("Destructor End");
  137. #endif
  138. _instance = NULL;
  139. };
  140. void hardwareManager::unloadResources( void ){
  141. #ifdef DEBUG
  142. LOG("Unload Resources ...");
  143. #endif
  144. //m_mVectorProtector.lock();
  145. if( m_vResources.size() ) {
  146. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  147. switch( (*idx)->getHardwareType() ) {
  148. case HARDWARE_UART:
  149. case HARDWARE_IP_UART: delete( ( hardwareResourceSerialProvider*)(*idx) ); break;
  150. case HARDWARE_NIF_LOOPBACK:
  151. case HARDWARE_NIF_ETHERNET:
  152. case HARDWARE_NIF_WIFI:
  153. case HARDWARE_NIF_VIRTUAL:
  154. case HARDWARE_NIF_TUNEL: delete( ( hardwareResourceNetwork*)(*idx) ); break;
  155. };
  156. };
  157. m_vResources.clear();
  158. };
  159. //m_mVectorProtector.unlock();
  160. #ifdef DEBUG
  161. LOG("Unload Resources Done");
  162. #endif
  163. };
  164. void hardwareManager::loadResources( void ){
  165. if( m_pDBC == NULL ) return;
  166. DBC_INT_LIST *tVal;
  167. tVal = m_pDBC->listResources( );//load whole site resources
  168. //tVal = m_pDBC->listResources( m_nServerNodeID );//Load Only Server Node resources
  169. if( tVal!= NULL ) {
  170. if( tVal->size() ) {
  171. for (DBC_INT_LIST::iterator index = tVal->begin(); index < tVal->end(); index++) {
  172. DBC_RESOURCE_DATA *tData = m_pDBC->getResourceData( *index );
  173. if( tData != NULL ) {
  174. hardwareResource *tPtr = NULL;
  175. switch( tData->type ){
  176. case HARDWARE_UART:{
  177. #ifdef DEBUG
  178. LOG("Construct HW UART");
  179. #endif
  180. hardwareResourceSerialProvider *tRes = new hardwareResourceSerialProvider( tData->id );
  181. if( tRes != NULL ) {
  182. if( tRes->create() ) {
  183. tPtr = ( hardwareResource *) tRes;
  184. } else {
  185. LOG_ERROR("Error Creating HW UART!");
  186. delete( tRes );
  187. };
  188. };
  189. break;
  190. };
  191. case HARDWARE_NIF_LOOPBACK:
  192. case HARDWARE_NIF_ETHERNET:
  193. case HARDWARE_NIF_WIFI:
  194. case HARDWARE_NIF_VIRTUAL:
  195. case HARDWARE_NIF_TUNEL:{
  196. #ifdef DEBUG
  197. LOG("Construct NetIF");
  198. #endif
  199. hardwareResourceNetwork *tRes = new hardwareResourceNetwork( tData->id );
  200. if( tRes != NULL ) {
  201. if( tRes->create() ) {
  202. tPtr = ( hardwareResource *) tRes;
  203. } else {
  204. LOG_ERROR("Error Creating Network Ressource!");
  205. delete( tRes );
  206. };
  207. };
  208. break;
  209. };
  210. case HARDWARE_IP_UART:{
  211. #ifdef DEBUG
  212. LOG("Construct IP UART");
  213. #endif
  214. hardwareResourceSerialProvider *tRes = new hardwareResourceSerialProvider( tData->id );
  215. if( tRes != NULL ) {
  216. if( tRes->create() ) {
  217. tPtr = ( hardwareResource *) tRes;
  218. } else {
  219. LOG_ERROR("Error Creating IP UART!");
  220. delete( tRes );
  221. };
  222. };
  223. break;
  224. };
  225. case HARDWARE_ANALOG_VIDEO_IN:
  226. case HARDWARE_DIGITAL_VIDEO_IN:{
  227. #ifdef DEBUG
  228. LOG("Construct VIDEO IN");
  229. #endif
  230. hardwareResourceVideoProvider *tRes = new hardwareResourceVideoProvider( tData->id );
  231. if( tRes != NULL ) {
  232. if( tRes->create() ) {
  233. tPtr = ( hardwareResource *) tRes;
  234. } else {
  235. LOG_ERROR("Error Creating Video IN!");
  236. delete( tRes );
  237. };
  238. };
  239. break;
  240. };
  241. #ifdef DEBUG
  242. default:{
  243. char tB[128];
  244. sprintf( tB, "Unknown Ressource[%d] Type[%d]!", *index, tData->type );
  245. LOG_ERROR(tB);
  246. break;
  247. };
  248. #endif
  249. };
  250. if( tPtr!= NULL ) {
  251. #ifdef DEBUG
  252. LOG("Resource Sucessfully Created!");
  253. #endif
  254. m_mVectorProtector.lock();
  255. m_vResources.push_back( tPtr );
  256. m_mVectorProtector.unlock();
  257. m_pDBC->setRessourceActive( *index );
  258. } else {
  259. m_pDBC->setRessourceActive( *index, false );
  260. };
  261. } else {
  262. LOG_ERROR("Can not fetch Resource Data from Database!");
  263. };
  264. };
  265. #ifdef DEBUG
  266. } else {
  267. LOG("Fetched Empty List!");
  268. #endif
  269. };
  270. tVal->clear();
  271. delete( tVal );
  272. #ifdef DEBUG
  273. } else {
  274. LOG("Fetched NULL!");
  275. #endif
  276. };
  277. };
  278. bool hardwareManager::validate( void ){
  279. bool RetVal = true;
  280. m_mVectorProtector.lock();
  281. if( m_vResources.size() ) {
  282. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  283. if( !(*idx)->validate() ) {
  284. // recreate Resource
  285. LOG_ERROR( "HW Resource Invalid!" );
  286. m_pDBC->setRessourceActive( (*idx)->getDbId(), false );
  287. #ifdef DEBUG
  288. char tB[256];
  289. sprintf( tB, "Info: DBID[%d] R-Type: %d Provider: %d ", (*idx)->getDbId(), (*idx)->getHardwareType(), (*idx)->getProviderType() );
  290. LOG( tB );
  291. #endif
  292. if( !(*idx)->create()){
  293. LOG_ERROR( "HW Resource Creation Failure!" );
  294. } else {
  295. m_pDBC->setRessourceActive( (*idx)->getDbId() );
  296. #ifdef DEBUG
  297. LOG("Re-Created OK");
  298. #endif
  299. };
  300. #ifdef DEBUG_EXTRA
  301. } else {
  302. LOG("Resource OK!");
  303. #endif
  304. };
  305. };
  306. #ifdef DEBUG_EXTRA
  307. } else {
  308. LOG("No Resources!");
  309. #endif
  310. };
  311. m_mVectorProtector.unlock();
  312. //Mem Validator
  313. if( m_nMemoryLimitHeapSize ) {
  314. size_t memStat = getCurrentRSS();
  315. #ifdef DEBUG_EXTRA
  316. char MEM_MSG[128];
  317. sprintf( MEM_MSG, "MEM: %d TARG: %d", memStat, m_nMemoryLimitHeapSize );
  318. LOG( MEM_MSG );
  319. #endif
  320. //https://www.ebay.com/itm/274974088159?
  321. RetVal = ( memStat < m_nMemoryLimitHeapSize );
  322. if ( !RetVal ) {
  323. LOG_ERROR("Memory Consumption too BIG!");
  324. };
  325. };
  326. return RetVal;
  327. };
  328. void hardwareManager::getMAC( CSTR if_name, CSTR buf ){
  329. char tB[128];
  330. FILE * fd=NULL;
  331. if ( buf ) {
  332. sprintf(tB, "/sys/class/net/%s/address", if_name);
  333. #ifdef DEBUG_EXTRA
  334. LOG( tB );
  335. #endif
  336. fd=fopen(tB,"r");
  337. if ( fd ) {
  338. fread( buf, 1, INET6_ADDRSTRLEN, fd );
  339. fclose( fd );
  340. buf = trim( buf );
  341. } else {
  342. memset( buf, 0, INET6_ADDRSTRLEN );
  343. };
  344. #ifdef DEBUG_EXTRA
  345. LOG( buf );
  346. #endif
  347. };
  348. };
  349. void hardwareManager::getGW( CSTR if_name, CSTR buf ){
  350. FILE * fd = popen( "route -n", "r" );
  351. char tB[1024];
  352. //char expBuf[1024];
  353. memset( buf, 0, INET6_ADDRSTRLEN );
  354. if ( fd ) {
  355. while (!feof(fd)) {
  356. fgets((CSTR)tB, 1024, fd);
  357. trim((CSTR)tB);
  358. if( strlen(tB) ) {
  359. if( strstr( tB, if_name ) != NULL ){
  360. //strncpy( expBuf, tB, 1024 );
  361. CArray *CA = explode( ' ', tB );
  362. if ( CA ) {
  363. //for (int i=0; i< CA->size(); i++ ) {
  364. // char tB[128];
  365. // trim( CA->at(i) );
  366. // sprintf( tB, "GW# %d: %s", i, CA->at(i) );
  367. // LOG( tB );
  368. //};
  369. trim( CA->at(1) );
  370. if( strstr(CA->at(1), "0.0.0.0") == NULL ) {
  371. strncpy( buf, CA->at(1), INET6_ADDRSTRLEN );
  372. };
  373. delete(CA);
  374. };
  375. };
  376. };
  377. };
  378. fclose( fd );
  379. };
  380. if( strlen( buf ) == 0 ) strncpy( buf, "0.0.0.0", INET6_ADDRSTRLEN );
  381. };
  382. void hardwareManager::getTTyDesc( CSTR dev_name, CSTR buf ){
  383. FILE * fd = popen( "dmesg | grep tty", "r" );
  384. char tB[1024];
  385. //char expBuf[1024];
  386. memset( buf, 0, DBC_RESOURCE_DESC_SIZE );
  387. if ( fd ) {
  388. while (!feof(fd)) {
  389. fgets((CSTR)tB, 1024, fd);
  390. trim((CSTR)tB);
  391. if( strlen(tB) ) {
  392. if( strstr( tB, dev_name ) != NULL ){
  393. //strncpy( expBuf, tB, 1024 );
  394. CArray *CA = explode( ':', tB );
  395. if ( CA ) {
  396. //for (int i=0; i< CA->size(); i++ ) {
  397. // char tB[128];
  398. // trim( CA->at(i) );
  399. // sprintf( tB, "GW# %d: %s", i, CA->at(i) );
  400. // LOG( tB );
  401. //};
  402. trim( CA->at(1) );
  403. strncpy( buf, CA->at(1), DBC_RESOURCE_DESC_SIZE );
  404. buf[DBC_RESOURCE_DESC_SIZE-1]=(char)0;
  405. delete(CA);
  406. };
  407. };
  408. };
  409. };
  410. fclose( fd );
  411. };
  412. if( strlen( buf ) == 0 ){
  413. strncpy( buf, "--Unknown--", DBC_RESOURCE_DESC_SIZE );
  414. buf[DBC_RESOURCE_DESC_SIZE-1]=(char)0;
  415. };
  416. };
  417. PARSE_RESULT_NET_INTERFACES *hardwareManager::parse_neconfig( CSTR if_name ){
  418. PARSE_RESULT_NET_INTERFACES *tVal = NULL;
  419. FILE *fd;
  420. bool cConfig = false;
  421. fd = fopen("/etc/network/interfaces","r");
  422. if ( fd ) {
  423. char tB[1024];
  424. char expBuf[1024];
  425. tVal = (PARSE_RESULT_NET_INTERFACES *) malloc( sizeof(PARSE_RESULT_NET_INTERFACES));
  426. if ( tVal ) {
  427. memset(tVal, 0 , sizeof(PARSE_RESULT_NET_INTERFACES));
  428. while (!feof(fd)) {
  429. fgets((CSTR)tB, 1024, fd);
  430. trim((CSTR)tB);
  431. if( strlen(tB) ) {
  432. strncpy( expBuf, tB, 1024 );
  433. CArray *CA = explode( ' ', expBuf );
  434. if( CA ) {
  435. for( CArray::iterator idx = CA->begin(); idx < CA->end(); idx++ ) {
  436. if((*idx)[0]=='#') break;
  437. if(strstr((*idx), "auto")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->enabled = true;
  438. if(strstr((*idx), "allow-hotplug")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->hot_plug = true;
  439. if(strstr((*idx), "iface")!= NULL ) if( strstr( tB, if_name )!= NULL ){
  440. if (strstr( tB, "static") != NULL) tVal->if_static = true;
  441. cConfig = true;
  442. } else cConfig = false;
  443. if(strstr((*idx), "address")!= NULL ) if( cConfig ){
  444. idx++;
  445. if(strstr((*idx),"/")!= NULL ){
  446. int8_t sh = strstr((*idx),"/") - (*idx);
  447. strncpy( tVal->address, (*idx), sh );
  448. tVal->address[sh]=(char)0;
  449. int8_t bm = atoi( (*idx) + sh + 1 );
  450. union{
  451. uint32_t mm;
  452. uint8_t nm[4];
  453. } nmu = {0};
  454. for ( sh = 0; sh < bm; sh++ ) nmu.mm |= (1 << sh);
  455. sprintf(tVal->netmask, "%d.%d.%d.%d", nmu.nm[0], nmu.nm[1], nmu.nm[2], nmu.nm[3]);
  456. } else {
  457. strncpy( tVal->address, (*idx), INET6_ADDRSTRLEN );
  458. };
  459. };
  460. if(strstr((*idx), "netmask")!= NULL ) if( cConfig ){
  461. LOG("netMSK");
  462. idx++;
  463. LOG(*idx);
  464. strncpy(tVal->netmask, (*idx), INET6_ADDRSTRLEN);
  465. };
  466. //if(strstr((*idx), "bridge_ports")!= NULL ) cMode = 6;
  467. //if(strstr((*idx), "bridge_stp")!= NULL ) cMode = 7;
  468. //if(strstr((*idx), "bridge_waitport")!= NULL ) cMode = 8;
  469. //if(strstr((*idx), "bridge_fd")!= NULL ) cMode = 9;
  470. };
  471. delete( CA );
  472. };
  473. };
  474. };
  475. };
  476. fclose( fd );
  477. };
  478. #ifdef DEBUG_EXTRA
  479. if ( tVal ) {
  480. if( tVal->enabled ) LOG("NIF Enabled"); else LOG("NIF Disabled");
  481. if (tVal->hot_plug) LOG("NIF Hot Plugable"); else LOG("NIF not Hot Plugable");
  482. if (tVal->if_static) LOG("Address is static"); else LOG("Address is Dynamic");
  483. if(strlen(tVal->address))LOG(tVal->address);
  484. if(strlen(tVal->netmask))LOG(tVal->netmask);
  485. };
  486. #endif
  487. return tVal;
  488. };
  489. PARSE_RESULT_WIFI_SETTINGS *hardwareManager::parse_wifi( CSTR if_name ){
  490. PARSE_RESULT_WIFI_SETTINGS * tVal = NULL;
  491. FILE * fd;
  492. bool cConfig = false;
  493. bool is_wifi = false;
  494. tVal = (PARSE_RESULT_WIFI_SETTINGS *) malloc( sizeof(PARSE_RESULT_WIFI_SETTINGS));
  495. if ( tVal ) {
  496. memset(tVal, 0 , sizeof(PARSE_RESULT_WIFI_SETTINGS));
  497. fd = fopen("/etc/dnsmasq.conf","r");
  498. if ( fd ) {
  499. char tB[1024];
  500. char expBuf[1024];
  501. while (!feof(fd)) {
  502. fgets((CSTR)tB, 1024, fd);
  503. trim((CSTR)tB);
  504. if( strlen(tB) ) {
  505. strncpy( expBuf, tB, 1024 );
  506. CArray *CA = explode( '=', expBuf );
  507. if( CA ) {
  508. if( CA->size() ){
  509. if( CA->at(0)[0] == '#' ) continue;
  510. if( strstr(CA->at(0), "interface")!= NULL ) {
  511. if( strstr( tB, if_name)!= NULL ) {
  512. cConfig = true;
  513. tVal->dhcp_enabled = true;
  514. };
  515. };
  516. if( cConfig ){
  517. if ( strstr(CA->at(0), "dhcp-range")!= NULL ) {
  518. char tT[1024];
  519. strncpy( tT, CA->at(1), 1024 );
  520. CArray *CB = explode(',', tT );
  521. if( CB ){
  522. if( CB->size() > 3 ){
  523. strncpy( tVal->range_start, CB->at(0), INET6_ADDRSTRLEN);
  524. strncpy( tVal->range_stop, CB->at(1), INET6_ADDRSTRLEN);
  525. strncpy( tVal->netmask, CB->at(2), INET6_ADDRSTRLEN);
  526. strncpy( tVal->lease_period, CB->at(3), INET6_ADDRSTRLEN);
  527. };
  528. delete( CB );
  529. };
  530. };
  531. };
  532. };
  533. delete( CA );
  534. };
  535. };
  536. };
  537. fclose( fd );
  538. };
  539. //Check WiFi
  540. if( strstr(if_name,"w")!= NULL ) {
  541. char tName[128];
  542. is_wifi = true;
  543. sprintf( tName, "iwconfig %s", if_name );
  544. fd = popen( tName, "r" );
  545. if ( fd ) {
  546. char tB[1024];
  547. while (!feof(fd)) {
  548. fgets((CSTR)tB, 1024, fd );
  549. trim( tB );
  550. if (strstr(tB, "Mode") != NULL) if (strstr(tB, "Master") != NULL) tVal->access_point = true;
  551. };
  552. fclose( fd );
  553. };
  554. };
  555. if ( tVal->access_point ) {
  556. fd = fopen("/etc/hostapd/hostapd.conf", "r");
  557. if ( fd ) {
  558. char tB[1024];
  559. char expBuf[1024];
  560. while (!feof(fd)) {
  561. fgets((CSTR)tB, 1024, fd);
  562. trim( tB );
  563. if( strlen(tB) ) {
  564. if( tB[0]=='#' ) continue;
  565. strncpy(expBuf, tB, 1024);
  566. CArray *CA = explode( '=', expBuf );
  567. if( CA ) {
  568. if( CA->size() ){
  569. if (strstr(CA->at(0), "ssid") != NULL) if (strstr(CA->at(0), "ignore_broadcast_ssid") == NULL){
  570. strncpy( tVal->SSID, CA->at(1), 64 );
  571. } else tVal->hiden_ssid = (bool) atoi(CA->at(1));
  572. if (strstr(CA->at(0), "wpa_passphrase") != NULL) strncpy( tVal->password, CA->at(1), 64 );
  573. if (strstr(CA->at(0), "country_code") != NULL) strncpy(tVal->country_code, CA->at(1), 3);
  574. if (strstr(CA->at(0), "channel") != NULL) tVal->channel = atoi(CA->at(1));
  575. };
  576. delete( CA );
  577. };
  578. };
  579. };
  580. fclose( fd );
  581. };
  582. } else {
  583. fd = fopen("/etc/wpa_supplicant/wpa_supplicant.conf", "r");
  584. if ( fd ) {
  585. fclose( fd );
  586. };
  587. };
  588. fd = fopen("/etc/rc.local","r");
  589. if ( fd ) {
  590. char tB[1024];
  591. char expBuf[1024];
  592. while (!feof(fd)) {
  593. fgets((CSTR)tB, 1024, fd);
  594. trim((CSTR)tB);
  595. if( strlen(tB) ) {
  596. strncpy( expBuf, tB, 1024 );
  597. CArray *CA = explode( ' ', expBuf );
  598. if( CA ) {
  599. if ( CA->size() ) {
  600. if (CA->at(0)[0] == '#') continue;
  601. if( strstr(CA->at(0), "iptables-restore")!= NULL ) {
  602. char zB[1024];
  603. FILE *afd = fopen( CA->at(2), "r" );
  604. if ( afd ) {
  605. while( !feof( afd ) ){
  606. fgets((CSTR)zB, 1024, afd );
  607. if( strstr( zB, "POSTROUTING" )!= NULL ) if( strstr( zB, "MASQUERADE" )!= NULL ) if( strstr( zB, if_name )== NULL ){
  608. CArray *CB = explode( ' ', zB );
  609. if ( CB ) {
  610. if ( CB->size() > 3 ) {
  611. strncpy(tVal->router_outbond, CB->at(3), INET6_ADDRSTRLEN);
  612. };
  613. delete( CB );
  614. };
  615. tVal->router_enabled = true;
  616. };
  617. };
  618. fclose(afd);
  619. } else LOG("Error Opening Firewall Rules!");
  620. };
  621. };
  622. delete( CA );
  623. };
  624. };
  625. };
  626. fclose(fd);
  627. } else LOG_ERROR("Can not open rc.local!");
  628. #ifdef DEBUG_EXTRA
  629. char zz[128];
  630. if ( is_wifi ) {
  631. if (tVal->access_point){
  632. LOG("WiFi is in AP Mode");
  633. sprintf( zz, "\tSSID:%s PWD:%s", tVal->SSID, tVal->password );
  634. LOG( zz );
  635. if (tVal->hiden_ssid) LOG("SSID is hidden");
  636. } else LOG("WiFi is in Client Mode");
  637. };
  638. if (tVal->dhcp_enabled){
  639. LOG("DHCP Service - Enabled");
  640. sprintf( zz, "\tRange: %s - %s Lease Time: %s", tVal->range_start, tVal->range_stop, tVal->lease_period );
  641. LOG( zz );
  642. } else {
  643. LOG("DHCP Service - Disabled");
  644. };
  645. if (tVal->router_enabled){
  646. sprintf( zz, "Routing enabled to [%s] NIF", tVal->router_outbond );
  647. LOG( zz );
  648. } else {
  649. LOG("Routing Disabled");
  650. };
  651. #endif
  652. };
  653. return tVal;
  654. };
  655. void hardwareManager::enumerateNIFS( void ){
  656. struct ifaddrs * ifaddr = NULL;
  657. char tBuffer[128];
  658. //if( m_pKernel == NULL ) return;
  659. if( m_pDBC == NULL ) return;
  660. if( -1 == getifaddrs(&ifaddr)) {
  661. LOG_ERROR("No NET INTERFACES?!");
  662. } else {
  663. #ifdef DEBUG
  664. LOG("Enumerating Network Interfaces");
  665. #endif
  666. struct ifaddrs * ifa;
  667. for( ifa = ifaddr; ifa; ifa = ifa->ifa_next ){
  668. char addr[INET6_ADDRSTRLEN];
  669. char mask[INET6_ADDRSTRLEN];
  670. char gw[INET6_ADDRSTRLEN];
  671. char bc[INET6_ADDRSTRLEN];
  672. char mac[INET6_ADDRSTRLEN];
  673. char device[INET6_ADDRSTRLEN];
  674. uint32_t nif_id = 0;
  675. uint32_t nif_aux_id = 0;
  676. uint32_t router_id = 0;
  677. NIF_TYPE nif_type = NIF_LOOPBACK;
  678. NETWORK_TYPE net_type = NETWORK_IPV4;
  679. RESOURCE_TYPE res_type = HARDWARE_NIF_LOOPBACK;
  680. NIF_MODE nif_mode = ADDRESS_DISABLED;
  681. #ifdef DEBUG_EXTRA
  682. LOG("*** Network Interface ***");
  683. #endif
  684. strncpy( device, ifa->ifa_name, INET6_ADDRSTRLEN );
  685. strncpy( mac,"--no-mac--", INET6_ADDRSTRLEN );
  686. #ifdef DEBUG_EXTRA
  687. LOG(device);
  688. #endif
  689. // Filter Loopback if
  690. if( strstr( device, "lo" ) != NULL ){
  691. nif_type = NIF_LOOPBACK;
  692. res_type = HARDWARE_NIF_LOOPBACK;
  693. //Not Implemented
  694. #ifdef DEBUG_EXTRA
  695. LOG("Loopback Device -> SKIP");
  696. #endif
  697. continue;
  698. };
  699. // Filter vpn TUN if
  700. if( strstr( device, "tun" ) != NULL ){
  701. nif_type = NIF_VPN_TUNEL;
  702. res_type = HARDWARE_NIF_TUNEL;
  703. //Not Implemented Yet
  704. #ifdef DEBUG_EXTRA
  705. LOG("Tunel Device -> SKIP");
  706. #endif
  707. continue;
  708. };
  709. if( strstr( device, "eth" ) != NULL ){
  710. nif_type = NIF_ETHERNET;
  711. res_type = HARDWARE_NIF_ETHERNET;
  712. };
  713. if( strstr( device, "wlan" ) != NULL ){
  714. nif_type = NIF_WIFI;
  715. res_type = HARDWARE_NIF_WIFI;
  716. };
  717. if( strstr( device, "wwan" ) != NULL ){
  718. nif_type = NIF_WAN;
  719. res_type = HARDWARE_NIF_WAN;
  720. };
  721. if( ifa->ifa_addr != NULL ){
  722. if (ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) {
  723. #ifdef DEBUG_EXTRA
  724. LOG("No INET ADDRESSES -> SKIP");
  725. #endif
  726. continue;
  727. }
  728. } else {
  729. #ifdef DEBUG_EXTRA
  730. LOG("ifa->ifa_addr is NULL -> SKIP");
  731. #endif
  732. continue;
  733. };
  734. #ifdef DEBUG_EXTRA
  735. LOG("Getting Info....");
  736. #endif
  737. getMAC( device, mac );
  738. getGW( device, gw );
  739. #ifdef DEBUG_EXTRA
  740. LOG( mac );
  741. LOG( gw );
  742. #endif
  743. PARSE_RESULT_NET_INTERFACES * tt = NULL;
  744. PARSE_RESULT_WIFI_SETTINGS * tw = NULL;
  745. #ifdef DEBUG_EXTRA
  746. LOG("Parse NetConfig Info....");
  747. #endif
  748. tt = parse_neconfig( device );
  749. if( tt ) {
  750. if ( tt->enabled ) {
  751. if( tt->if_static ){
  752. nif_mode = ADDRESS_STATIC;
  753. } else {
  754. nif_mode = ADDRESS_DYNAMIC;
  755. };
  756. } else {
  757. nif_mode = ADDRESS_DISABLED;
  758. };
  759. } else {
  760. LOG_ERROR("Error Parsing System NIF Settings!");
  761. continue;
  762. };
  763. #ifdef DEBUG_EXTRA
  764. LOG("Parse Routing/DHCP/AP Info....");
  765. #endif
  766. tw = parse_wifi( device );
  767. if ( tw ){
  768. #ifdef DEBUG_EXTRA
  769. LOG("Routing/AP Info OK!");
  770. #endif
  771. if( nif_mode == ADDRESS_STATIC ) {
  772. if( tw->dhcp_enabled ){
  773. #ifdef DEBUG_EXTRA
  774. LOG("Address is Router");
  775. #endif
  776. nif_mode = ADDRESS_STATIC_RAP;
  777. };
  778. };
  779. nif_id = m_pDBC->addLocalNIF( device, mac, nif_type, nif_mode );
  780. #ifdef DEBUG_EXTRA
  781. sprintf( tBuffer, "NIF(%s) added as [%d]", device, nif_id );
  782. LOG( tBuffer );
  783. #endif
  784. if ( nif_type == NIF_WIFI ){
  785. if( nif_id == 0 ){
  786. LOG_ERROR("Error Adding NIF in DB!");
  787. continue;
  788. };
  789. WIFI_MODE w_mode;
  790. if( !tt->enabled ) {
  791. w_mode = WIFI_DISABLED;
  792. } else {
  793. if( tt->if_static ){
  794. if( tw->access_point ) {
  795. w_mode = WIFI_IS_AP;
  796. uint32_t outbond_id = 0;
  797. if( tw->router_enabled ){
  798. outbond_id = m_pDBC->getNIF_IDbyDevice( tw->router_outbond );
  799. };
  800. if( tw->dhcp_enabled ) {
  801. #ifdef DEBUG_EXTRA
  802. LOG("ADD Router With DHCP");
  803. #endif
  804. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period );
  805. } else {
  806. #ifdef DEBUG_EXTRA
  807. LOG("ADD Router ONLY");
  808. #endif
  809. router_id = m_pDBC->addLocalNetworkInfrastructure( nif_id, outbond_id );
  810. };
  811. #ifdef DEBUG_EXTRA
  812. sprintf( tBuffer, "Router added as [%d]", router_id );
  813. LOG( tBuffer );
  814. #endif
  815. } else {
  816. w_mode = WIFI_CONNECTED_TO_AP;
  817. };
  818. };
  819. };
  820. #ifdef DEBUG_EXTRA
  821. LOG("ADD WiFi Record");
  822. #endif
  823. nif_aux_id = m_pDBC->addLocalWiFi( nif_id, w_mode, tw->SSID, tw->password, ENCRIPTION_WPA_PSK, router_id );
  824. };
  825. if( nif_type == NIF_ETHERNET || nif_type == NIF_ETHERNET_VIRTUAL ){
  826. if( tt->if_static ){
  827. uint32_t outbond_id = 0;
  828. nif_mode = ADDRESS_STATIC;
  829. if( tw->router_enabled ){
  830. nif_mode = ADDRESS_STATIC_RAP;
  831. outbond_id = m_pDBC->getNIF_IDbyDevice( tw->router_outbond );
  832. };
  833. if( tw->dhcp_enabled ) {
  834. #ifdef DEBUG_EXTRA
  835. LOG("ADD Router With DHCP");
  836. #endif
  837. router_id = m_pDBC->addLocalNetworkInfrastructure( nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period );
  838. } else {
  839. #ifdef DEBUG_EXTRA
  840. LOG("ADD Router ONLY");
  841. #endif
  842. router_id = m_pDBC->addLocalNetworkInfrastructure( nif_id, outbond_id );
  843. };
  844. #ifdef DEBUG_EXTRA
  845. sprintf( tBuffer, "Router added as [%d]", router_id );
  846. LOG( tBuffer );
  847. #endif
  848. } else {
  849. nif_mode = ADDRESS_DYNAMIC;
  850. };
  851. };
  852. free(tt);
  853. free(tw);
  854. } else {
  855. LOG_ERROR("Error Parsing System Router/AP Settings!");
  856. free(tt);
  857. continue;
  858. };
  859. if( nif_aux_id != 0 ) {
  860. nif_id = m_pDBC->addLocalNIF(device, mac, nif_type, nif_mode, nif_aux_id );
  861. #ifdef DEBUG_EXTRA
  862. sprintf( tBuffer, "2 NIF(%s) Updated as [%d]", device, nif_id );
  863. LOG( tBuffer );
  864. #endif
  865. if( nif_id == 0 ) {
  866. LOG_ERROR("Error Adding NIF in DB!");
  867. continue;
  868. };
  869. #ifdef DEBUG_EXTRA
  870. LOG("NIF added!");
  871. } else {
  872. LOG("NIF Already added (regular Mode)!");
  873. #endif
  874. };
  875. m_pDBC->addResource( /*m_nServerNodeID,*/ res_type, device, nif_id );
  876. if( ifa->ifa_addr->sa_family == AF_INET ){
  877. net_type = NETWORK_IPV4;
  878. // create IPv4 string
  879. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  880. inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
  881. sprintf( mask, "%d.%d.%d.%d",
  882. (uint8_t)ifa->ifa_netmask->sa_data[2],
  883. (uint8_t)ifa->ifa_netmask->sa_data[3],
  884. (uint8_t)ifa->ifa_netmask->sa_data[4],
  885. (uint8_t)ifa->ifa_netmask->sa_data[5]
  886. );
  887. sprintf( bc, "%d.%d.%d.%d",
  888. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  889. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  890. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  891. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  892. );
  893. } else { // AF_INET6
  894. net_type = NETWORK_IPV6;
  895. // create IPv6 string
  896. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  897. inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
  898. sprintf( mask, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  899. (uint8_t)ifa->ifa_netmask->sa_data[2],
  900. (uint8_t)ifa->ifa_netmask->sa_data[3],
  901. (uint8_t)ifa->ifa_netmask->sa_data[4],
  902. (uint8_t)ifa->ifa_netmask->sa_data[5],
  903. (uint8_t)ifa->ifa_netmask->sa_data[6],
  904. (uint8_t)ifa->ifa_netmask->sa_data[7],
  905. (uint8_t)ifa->ifa_netmask->sa_data[8],
  906. (uint8_t)ifa->ifa_netmask->sa_data[9],
  907. (uint8_t)ifa->ifa_netmask->sa_data[10],
  908. (uint8_t)ifa->ifa_netmask->sa_data[11],
  909. (uint8_t)ifa->ifa_netmask->sa_data[12],
  910. (uint8_t)ifa->ifa_netmask->sa_data[13],
  911. (uint8_t)ifa->ifa_netmask->sa_data[14],
  912. (uint8_t)ifa->ifa_netmask->sa_data[15],
  913. (uint8_t)ifa->ifa_netmask->sa_data[16],
  914. (uint8_t)ifa->ifa_netmask->sa_data[17]
  915. );
  916. if( ifa->ifa_ifu.ifu_broadaddr != NULL ){
  917. sprintf( bc, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  918. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  919. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  920. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  921. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5],
  922. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[6],
  923. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[7],
  924. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[8],
  925. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[9],
  926. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[10],
  927. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[11],
  928. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[12],
  929. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[13],
  930. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[14],
  931. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[15],
  932. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[16],
  933. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[17]
  934. );
  935. } else {
  936. sprintf( bc, "" );
  937. };
  938. };
  939. m_pDBC->addLocalNetwork( nif_id, net_type, addr, mask, gw, bc );
  940. #ifdef DEBUG
  941. sprintf( tBuffer, "Detected Network Interface: %s [%s]", ifa->ifa_name, addr );
  942. LOG(tBuffer);
  943. #endif
  944. nif_id = 0;
  945. nif_aux_id = 0;
  946. router_id = 0;
  947. nif_type = NIF_LOOPBACK;
  948. net_type = NETWORK_IPV4;
  949. res_type = HARDWARE_NIF_LOOPBACK;
  950. nif_mode = ADDRESS_DISABLED;
  951. };
  952. freeifaddrs( ifaddr );
  953. };
  954. };
  955. void hardwareManager::enumerateHardware( void ){
  956. char tBuffer[CTEXT];
  957. DIR * dFD = NULL;
  958. struct dirent * pDR = NULL;
  959. //ToDo: Check Ability to work without Kernel and DBC
  960. //if( m_pKernel == NULL ) return;
  961. if( m_pDBC == NULL ) return;
  962. #ifdef DEBUG
  963. LOG("HW Enumeration Started!");
  964. #endif
  965. // Moved to a constructor!!
  966. //m_nServerNodeID = m_pKernel->getServerNodeId();
  967. //m_nServerID = m_pKernel->getServerId();
  968. //m_nSiteID = m_pKernel->getSiteId();
  969. /* Add Network Interfaces */
  970. m_pDBC->cleanResources( true );
  971. enumerateNIFS();
  972. /* Check HID Devices */
  973. struct hid_device_info * pDevs;
  974. uint8_t DCBCount;
  975. //Enumerate USB devices with the given IDs
  976. pDevs = hid_enumerate( 0x16c0, 0x05df );
  977. if( pDevs!=NULL ){
  978. struct hid_device_info * pCDev = pDevs;
  979. //Count the number of returned devices
  980. for( DCBCount = 0; pCDev!=NULL; DCBCount++ ) pCDev = pCDev->next;
  981. //Allocate a buffer for the relays
  982. if( DCBCount > 0 ){
  983. DC_BOARD tDCB;
  984. //Fill the relay structs
  985. pCDev = pDevs;
  986. for( int idx = 0; idx < DCBCount; idx++ ){
  987. //Save the path to this device
  988. memset( tDCB.path, 0, MAX_PATH_LEN );
  989. strncpy( tDCB.path, pCDev->path, MAX_PATH_LEN );
  990. // The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
  991. tDCB.relay_count = atoi((const char *)&pCDev->product_string[8]);
  992. //Open it to get more details
  993. hid_device * handle;
  994. handle = hid_open_path( tDCB.path );
  995. if( handle ){
  996. unsigned char buf[9];
  997. //Get the features of the device
  998. buf[0] = 0x01;
  999. if( hid_get_feature_report( handle, buf, sizeof(buf)) == -1 ){
  1000. LOG_ERROR("hid_get_feature_report");
  1001. pCDev = pCDev->next;
  1002. hid_close( handle );
  1003. continue;
  1004. };
  1005. //Set the serial number (0x0 for termination)
  1006. memset( tDCB.serial, 0x0, (HID_DCR_SERIIAL_LENGTH+1) );
  1007. memcpy( tDCB.serial, buf, HID_DCR_SERIIAL_LENGTH );
  1008. //Byte 7 in the response contains the target_state of the relays
  1009. tDCB.state = buf[7];
  1010. hid_close( handle );
  1011. #ifdef DEBUG
  1012. //Output the device enumeration details if verbose is on
  1013. LOG("HID DCB Found");
  1014. sprintf(tBuffer, " Type: %04hx::%04hx", pCDev->vendor_id, pCDev->product_id);
  1015. LOG(tBuffer);
  1016. sprintf(tBuffer, " Path: %s", pCDev->path );
  1017. LOG(tBuffer);
  1018. sprintf(tBuffer, " Serial_number: %ls", pCDev->serial_number );
  1019. LOG(tBuffer);
  1020. sprintf(tBuffer, " Manufacturer: %ls", pCDev->manufacturer_string);
  1021. LOG(tBuffer);
  1022. sprintf(tBuffer, " Product: %ls", pCDev->product_string);
  1023. LOG(tBuffer);
  1024. sprintf(tBuffer, " Release: %hx", pCDev->release_number);
  1025. LOG(tBuffer);
  1026. sprintf(tBuffer, " Interface: %d", pCDev->interface_number);
  1027. LOG(tBuffer);
  1028. sprintf(tBuffer, " Number of Relays = %d", tDCB.relay_count);
  1029. LOG(tBuffer);
  1030. //If debug are on, output individual relay details
  1031. for( int k = 0; k < tDCB.relay_count; k++ ){
  1032. if( tDCB.state & 1 << k ){
  1033. sprintf(tBuffer, " %s_%d=1", tDCB.serial, k + 1);
  1034. } else {
  1035. sprintf(tBuffer, " %s_%d=0", tDCB.serial, k + 1);
  1036. };
  1037. LOG(tBuffer);
  1038. };
  1039. #endif
  1040. m_pDBC->addLocalDCB( m_nSiteID, pCDev->path, tDCB.serial, tDCB.relay_count, tDCB.state );
  1041. } else {
  1042. LOG_ERROR("unable to open HID device");
  1043. };
  1044. pCDev = pCDev->next;
  1045. };
  1046. };
  1047. hid_free_enumeration( pDevs );
  1048. };
  1049. /* Add devices from DEV */
  1050. dFD = opendir("/dev");
  1051. if( dFD != NULL ) {
  1052. while( ( pDR = readdir( dFD ))!= NULL ){
  1053. if( strcasestr( pDR->d_name, "video" ) == pDR->d_name ) {
  1054. createResourceVIDEO( pDR->d_name );
  1055. continue;
  1056. };
  1057. if( strcasestr( pDR->d_name, "ttyS" ) == pDR->d_name ){
  1058. createResourceUART( pDR->d_name );
  1059. continue;
  1060. };
  1061. // RaspberyPI Hardware
  1062. if( strcasestr( pDR->d_name, "ttyAMA" ) == pDR->d_name ){
  1063. createResourceUART( pDR->d_name );
  1064. continue;
  1065. };
  1066. if( strcasestr( pDR->d_name, "ttyUSB" ) == pDR->d_name ){
  1067. createResourceUART( pDR->d_name );
  1068. continue;
  1069. };
  1070. };
  1071. closedir( dFD );
  1072. };
  1073. //m_bHardwareEnumerated = true;
  1074. m_pDBC->updateSystemInfo( &m_sSYS_INFO );
  1075. };
  1076. uint32_t hardwareManager::createResourceUART( CSTR devName ){
  1077. uint32_t RetVal = 0;
  1078. char tBuffer[256];
  1079. /* Enumerate BuiltIn Serial */
  1080. #ifdef DEBUG_EXTRA
  1081. sprintf( tBuffer, "Serial Device: %s", devName );
  1082. LOG( tBuffer );
  1083. #endif
  1084. char dev_name[CTEXT];
  1085. int fd = -1;
  1086. struct serial_struct ss;
  1087. sprintf( dev_name, "/dev/%s", devName );
  1088. fd = open( dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK );
  1089. if (-1 == fd) {
  1090. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1091. LOG_ERROR( tBuffer );
  1092. } else {
  1093. if ( 0 && ( -1 == xioctl(fd, TIOCGSERIAL, &ss) )){
  1094. LOG_ERROR("TIOCGSERIAL");
  1095. } else {
  1096. #ifdef DEBUG_EXTRA
  1097. sprintf( tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]",
  1098. ss.port,
  1099. ss.irq,
  1100. ss.baud_base,
  1101. ss.line,
  1102. ss.type,
  1103. ss.flags
  1104. );
  1105. LOG( tBuffer );
  1106. #endif
  1107. getTTyDesc( devName, tBuffer );
  1108. uint32_t tID = m_pDBC->addLocalUART( /*m_nServerNodeID,*/ 1, 19200, 8, 'N', 1.0f, dev_name );
  1109. if ( tID ) {
  1110. m_pDBC->addResource( /*m_nServerNodeID,*/ HARDWARE_UART, tBuffer, tID );
  1111. };
  1112. };
  1113. close(fd);
  1114. };
  1115. return RetVal;
  1116. };
  1117. uint32_t hardwareManager::createResourceVIDEO( CSTR devName ){
  1118. uint32_t RetVal = 0;
  1119. char tBuffer[256];
  1120. /* Enumerate Video Capture */
  1121. #ifdef DEBUG_EXTRA
  1122. sprintf( tBuffer, "Video Source: %s", devName );
  1123. LOG( tBuffer );
  1124. #endif
  1125. struct v4l2_capability cap;
  1126. //struct v4l2_cropcap cropcap;
  1127. //struct v4l2_crop crop;
  1128. struct v4l2_format fmt;
  1129. struct v4l2_frmivalenum sprm;
  1130. char dev_name[CTEXT];
  1131. int fd = -1;
  1132. struct stat st;
  1133. sprintf( dev_name, "/dev/%s", devName );
  1134. if (-1 == stat( dev_name, &st )) {
  1135. sprintf( tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1136. LOG_ERROR( tBuffer );
  1137. return 0;
  1138. };
  1139. if (!S_ISCHR(st.st_mode)) {
  1140. sprintf( tBuffer, "%s is no devicen", dev_name);
  1141. LOG_ERROR( tBuffer );
  1142. return 0;
  1143. };
  1144. #ifdef DEBUG
  1145. LOG( dev_name );
  1146. #endif
  1147. fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  1148. if (-1 == fd) {
  1149. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  1150. LOG_ERROR( tBuffer );
  1151. } else {
  1152. if( -1 == xioctl(fd, VIDIOC_QUERYCAP, &cap) ){
  1153. if (EINVAL == errno) {
  1154. sprintf( tBuffer, "%s is no V4L2 device", dev_name );
  1155. LOG_ERROR( tBuffer );
  1156. } else {
  1157. LOG_ERROR( "VIDIOC_QUERYCAP" );
  1158. };
  1159. close(fd);
  1160. return 0;
  1161. };
  1162. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  1163. #ifdef DEBUG
  1164. sprintf(tBuffer, "%s is no video capture device", dev_name);
  1165. LOG_ERROR( tBuffer );
  1166. #endif
  1167. close(fd);
  1168. return 0;
  1169. };
  1170. CLEAR(fmt);
  1171. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1172. /* Preserve original settings as set by v4l2-ctl for example */
  1173. if( -1 == xioctl(fd, VIDIOC_G_FMT, &fmt)){
  1174. LOG_ERROR("VIDIOC_G_FMT");
  1175. } else {
  1176. CLEAR( sprm );
  1177. if(-1 == xioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &sprm) && 0 ){
  1178. LOG_ERROR("VIDIOC_ENUM_FRAMEINTERVALS");
  1179. } else {
  1180. uint16_t fps = 15;
  1181. switch ( sprm.type ) {
  1182. case V4L2_FRMIVAL_TYPE_DISCRETE: LOG("1DISCRETE"); break;
  1183. case V4L2_FRMIVAL_TYPE_CONTINUOUS: LOG("2DISCRETE"); break;
  1184. case V4L2_FRMIVAL_TYPE_STEPWISE: LOG("3DISCRETE"); break;
  1185. };
  1186. #ifdef DEBUG
  1187. sprintf( tBuffer, "Grabber{%s}{%s}[%dx%d@%d]", cap.card, cap.driver, fmt.fmt.pix.width,fmt.fmt.pix.height, fps );
  1188. LOG( tBuffer );
  1189. #endif
  1190. uint32_t mid = m_pDBC->addLocalCam( (CSTR)cap.card, (CSTR)cap.driver, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height, fps );
  1191. //uint64_t mid = m_pDBC->addLocalMediaSource( m_nSiteID, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height );
  1192. //m_pDBC->addNodeSocket( m_nServerNodeID, mid, NODE_SOCK_TYPE_ANALOG_VIDEO_GRABBER, NODE_SOCK_MODE_OUT, false, dev_name, tBuffer );
  1193. RetVal = m_pDBC->addResource( HARDWARE_ANALOG_VIDEO_IN, dev_name, mid );
  1194. };
  1195. };
  1196. close(fd);
  1197. };
  1198. return RetVal;
  1199. };
  1200. uint32_t hardwareManager::createResourceUDCB( CSTR devName ){
  1201. uint32_t RetVal = 0;
  1202. char tBuffer[256];
  1203. return RetVal;
  1204. };
  1205. bool hardwareManager::assignResource( hardwareResource *ptr, bool protected_mode ){
  1206. LOG("ASSIGN HWR");
  1207. if ( ptr == NULL ) return false;
  1208. if( protected_mode ) m_mVectorProtector.lock();
  1209. LOG("LOCKED");
  1210. m_vResources.push_back( ptr );
  1211. LOG("ADDED");
  1212. if( protected_mode ) m_mVectorProtector.unlock();
  1213. return true;
  1214. };
  1215. bool hardwareManager::resignResource( hardwareResource *ptr, bool protected_mode ){
  1216. LOG("RESIGN HWR");
  1217. if( ptr == NULL ) return false;
  1218. if( protected_mode ) m_mVectorProtector.lock();
  1219. LOG("LOCKED");
  1220. if( m_vResources.size() ) {
  1221. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  1222. if( (*idx) == ptr ) {
  1223. LOG("FOUND");
  1224. m_vResources.erase( idx );
  1225. LOG("ERASED");
  1226. break;
  1227. };
  1228. };
  1229. } else {
  1230. LOG("NO RES!");
  1231. };
  1232. LOG("UNLOCKED");
  1233. if( protected_mode ) m_mVectorProtector.unlock();
  1234. return true;
  1235. };
  1236. hardwareResource *hardwareManager::requestResource( HARDWARE_PROVIDERS type, uint32_t id ){
  1237. hardwareResource *RetVal = NULL;
  1238. m_mVectorProtector.lock();
  1239. if( m_vResources.size() ) {
  1240. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  1241. #ifdef DEBUG_EXTRA
  1242. char tB[256];
  1243. sprintf( tB, "Check Resources RR[%d] RI[%d] ?? CR[%d] CI[%d]", type, id, (*idx)->getProviderType(), (*idx)->getDbId() );
  1244. LOG( tB );
  1245. #endif
  1246. if( ((*idx)->getProviderType() == type ) && ( (*idx)->getDbId() == id ) ) {
  1247. RetVal = (*idx);
  1248. break;
  1249. };
  1250. };
  1251. } else {
  1252. LOG("No Resources!!");
  1253. };
  1254. m_mVectorProtector.unlock();
  1255. return RetVal;
  1256. };
  1257. DBC_INT_LIST *hardwareManager::requestResourcesByType( HARDWARE_PROVIDERS type ){
  1258. DBC_INT_LIST *RetVal = NULL;
  1259. #ifdef DEBUG
  1260. char tBuffer[128];
  1261. sprintf( tBuffer, "Requested Resource type: %d", type );
  1262. LOG( tBuffer );
  1263. #endif
  1264. m_mVectorProtector.lock();
  1265. if( m_vResources.size() ) {
  1266. #ifdef DEBUG
  1267. LOG("Have Some Resources!");
  1268. #endif
  1269. RetVal = new DBC_INT_LIST();
  1270. if( RetVal ) {
  1271. #ifdef DEBUG
  1272. LOG("RetVal Created!");
  1273. #endif
  1274. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  1275. #ifdef DEBUG
  1276. sprintf( tBuffer, "Have Resource type: %d", (*idx)->getHardwareType() );
  1277. LOG( tBuffer );
  1278. #endif
  1279. if( (*idx)->getProviderType() == type ) {
  1280. #ifdef DEBUG
  1281. LOG("Matched!");
  1282. #endif
  1283. RetVal->push_back((*idx)->getDbId());
  1284. };
  1285. };
  1286. } else {
  1287. LOG_ERROR("Cannot Create DBC_INT_LIST");
  1288. };
  1289. };
  1290. m_mVectorProtector.unlock();
  1291. return RetVal;
  1292. };
  1293. bool hardwareManager::updateSysInfo( DBC_SYS_LOAD_INFO *ptr ){
  1294. if ( ptr == NULL) return false;
  1295. struct sysinfo SI;
  1296. struct statvfs DI;
  1297. ptr->CPU_CORES = get_nprocs();
  1298. if( sysinfo( &SI ) == 0 ){
  1299. float f_load = 100.f / (float)( ptr->CPU_CORES * ( 1 << SI_LOAD_SHIFT ) );
  1300. ptr->cpu1 = SI.loads[0] * f_load;
  1301. ptr->cpu5 = SI.loads[1] * f_load;
  1302. ptr->cpu15 = SI.loads[2] * f_load;
  1303. ptr->T_MEM = SI.totalram;
  1304. ptr->C_MEM = SI.totalram - SI.freeram;
  1305. ptr->mem = 100.0f * (float)ptr->C_MEM / (float)ptr->T_MEM;
  1306. } else {
  1307. LOG_ERROR("Error while getting System Info!");
  1308. };
  1309. //const uint32_t MB = 1 << 10;//(1024 * 1024);// * 1024;
  1310. if( statvfs("/", &DI) == 0 ){
  1311. ptr->T_DISK = ( DI.f_blocks * (unsigned long long) DI.f_frsize ) >> 10;// / MB;
  1312. //cDISK = ( DI.f_bavail * (unsigned long long) DI.f_bsize ) >> 10;// / MB;
  1313. ptr->C_DISK = ptr->T_DISK - ( ( DI.f_bfree * (unsigned long long) DI.f_bsize ) >> 10 );
  1314. ptr->disk = 100.0f * (float)ptr->C_DISK / (float)ptr->T_DISK;
  1315. } else {
  1316. LOG_ERROR("Error while getting Disk Info!");
  1317. };
  1318. //FILE *fd = popen("","r");
  1319. ptr->temperature = 46.7;
  1320. return m_pDBC->updateServerUtilisation( ptr );
  1321. };
  1322. #undef CLEAR
  1323. #undef LOG
  1324. #undef LOG_ERROR