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.

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