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.

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