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.

1380 lines
53 KiB

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