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.

1137 lines
45 KiB

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