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.

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