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.

916 lines
37 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2017. All Rights Reserved.
  4. // Version 2.0 7/20/2017
  5. // Last modification: 7/20/2017
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: mojoHardwareManager.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of mojoHardwareManager.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "mojoHardwareManager.h"
  16. #include "../hardware/hid/hid_dc.h"
  17. #include <sys/time.h>
  18. #include <sys/resource.h>
  19. #include <sys/types.h>
  20. #include <sys/ioctl.h>
  21. #include <hidapi/hidapi.h>
  22. #include <ifaddrs.h>
  23. #include <linux/serial.h>
  24. #include <linux/videodev2.h>
  25. #define LOG( X ) m_pLog->Log( "MHWM", this, X )
  26. #ifdef DEBUG
  27. #define LOG_ERROR( X ) {\
  28. char _tBfrErrMSG2w34[256];\
  29. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  30. m_pLog->Log( "MHWM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  31. }
  32. #else
  33. #define LOG_ERROR( X ) m_pLog->Log( "MHWM", this, _LOG_ERROR, X );
  34. #endif
  35. mojoHardwareManager *mojoHardwareManager::_instance = NULL;
  36. /**
  37. * Returns the single instance of the object.
  38. */
  39. mojoHardwareManager *mojoHardwareManager::getInstance( config_setting_t * Settings ) {
  40. // check if the instance has been created yet
  41. if( 0 == _instance ){
  42. // if not, then create it
  43. _instance = new mojoHardwareManager( Settings );
  44. };
  45. // return the single instance
  46. return _instance;
  47. };
  48. // Constructor implementation
  49. mojoHardwareManager::mojoHardwareManager( config_setting_t *SNode ){
  50. m_pLog = basicLogCollection::getInstance();
  51. m_pSettingsProvider = basicSettingsProvider::getInstance();
  52. m_nMemoryLimitHeapSize = 0;
  53. if( m_pSettingsProvider ) {
  54. m_pSettings = m_pSettingsProvider->getSubNode( SNode, "HARDWARE_MANAGER" );
  55. if( m_pSettingsProvider->isCompromised() ) {
  56. m_pSettingsProvider->validate();
  57. };
  58. } else {
  59. m_pSettings = NULL;
  60. };
  61. m_pDBC = mojoDBC::getInstance();
  62. char tB[128];
  63. struct rlimit rl={0};
  64. getrlimit( RLIMIT_STACK, &rl );
  65. sprintf( tB, "Stack Size: %d", rl.rlim_cur ); //RLIM_INFINITY
  66. LOG( tB );
  67. sprintf( tB, "MAX Stack Size: %d", rl.rlim_max );
  68. LOG( tB );
  69. m_bInitOK = false;
  70. };
  71. mojoHardwareManager::~mojoHardwareManager(){
  72. _instance = NULL;
  73. };
  74. bool mojoHardwareManager::validate(){
  75. bool RetVal = true;
  76. //Mem Validator
  77. if( m_nMemoryLimitHeapSize ) {
  78. size_t memStat = getCurrentRSS();
  79. #ifdef DEBUG_EXTRA
  80. char MEM_MSG[128];
  81. sprintf( MEM_MSG, "MEM: %d TARG: %d", memStat, m_nMemoryLimitHeapSize );
  82. LOG( MEM_MSG );
  83. #endif
  84. RetVal = ( memStat < m_nMemoryLimitHeapSize );
  85. if ( !RetVal ) {
  86. LOG_ERROR("Memory Consumption too BIG!");
  87. };
  88. };
  89. return RetVal;
  90. };
  91. int xioctl(int fh, int request, void *arg){
  92. int r;
  93. do {
  94. r = ioctl( fh, request, arg );
  95. } while (-1 == r && EINTR == errno);
  96. return r;
  97. };
  98. #define CLEAR(x) memset(&(x), 0, sizeof(x))
  99. void mojoHardwareManager::getMAC( CSTR if_name, CSTR buf ){
  100. char tB[128];
  101. FILE * fd=NULL;
  102. if ( buf ) {
  103. sprintf(tB, "/sys/class/net/%s/address", if_name);
  104. #ifdef DEBUG
  105. LOG( tB );
  106. #endif
  107. fd=fopen(tB,"r");
  108. if ( fd ) {
  109. fread( buf, 1, INET6_ADDRSTRLEN, fd );
  110. fclose( fd );
  111. buf = trim( buf );
  112. } else {
  113. memset( buf, 0, INET6_ADDRSTRLEN );
  114. };
  115. #ifdef DEBUG
  116. LOG( buf );
  117. #endif
  118. };
  119. };
  120. void mojoHardwareManager::getGW( CSTR if_name, CSTR buf ){
  121. FILE * fd = popen( "route -n", "r" );
  122. char tB[1024];
  123. //char expBuf[1024];
  124. memset( buf, 0, INET6_ADDRSTRLEN );
  125. if ( fd ) {
  126. while (!feof(fd)) {
  127. fgets((CSTR)tB, 1024, fd);
  128. trim((CSTR)tB);
  129. if( strlen(tB) ) {
  130. if( strstr( tB, if_name ) != NULL ){
  131. //strncpy( expBuf, tB, 1024 );
  132. CArray *CA = explode( ' ', tB );
  133. if ( CA ) {
  134. //for (int i=0; i< CA->size(); i++ ) {
  135. // char tB[128];
  136. // trim( CA->at(i) );
  137. // sprintf( tB, "GW# %d: %s", i, CA->at(i) );
  138. // LOG( tB );
  139. //};
  140. trim( CA->at(1) );
  141. if( strstr(CA->at(1), "0.0.0.0") == NULL ) {
  142. strncpy( buf, CA->at(1), INET6_ADDRSTRLEN );
  143. };
  144. delete(CA);
  145. };
  146. };
  147. };
  148. };
  149. fclose( fd );
  150. };
  151. if( strlen( buf ) == 0 ) strncpy( buf, "0.0.0.0", INET6_ADDRSTRLEN );
  152. };
  153. void mojoHardwareManager::getTTyDesc( CSTR dev_name, CSTR buf ){
  154. FILE * fd = popen( "dmesg | grep tty", "r" );
  155. char tB[1024];
  156. //char expBuf[1024];
  157. memset( buf, 0, DBC_RESOURCE_DESC_SIZE );
  158. if ( fd ) {
  159. while (!feof(fd)) {
  160. fgets((CSTR)tB, 1024, fd);
  161. trim((CSTR)tB);
  162. if( strlen(tB) ) {
  163. if( strstr( tB, dev_name ) != NULL ){
  164. //strncpy( expBuf, tB, 1024 );
  165. CArray *CA = explode( ':', tB );
  166. if ( CA ) {
  167. //for (int i=0; i< CA->size(); i++ ) {
  168. // char tB[128];
  169. // trim( CA->at(i) );
  170. // sprintf( tB, "GW# %d: %s", i, CA->at(i) );
  171. // LOG( tB );
  172. //};
  173. trim( CA->at(1) );
  174. strncpy( buf, CA->at(1), DBC_RESOURCE_DESC_SIZE );
  175. buf[DBC_RESOURCE_DESC_SIZE-1]=(char)0;
  176. delete(CA);
  177. };
  178. };
  179. };
  180. };
  181. fclose( fd );
  182. };
  183. if( strlen( buf ) == 0 ){
  184. strncpy( buf, "--Unknown--", DBC_RESOURCE_DESC_SIZE );
  185. buf[DBC_RESOURCE_DESC_SIZE-1]=(char)0;
  186. };
  187. };
  188. PARSE_RESULT_NET_INTERFACES *mojoHardwareManager::parse_neconfig( CSTR if_name ){
  189. PARSE_RESULT_NET_INTERFACES *tVal = NULL;
  190. FILE *fd;
  191. bool cConfig = false;
  192. fd = fopen("/etc/network/interfaces","r");
  193. if ( fd ) {
  194. char tB[1024];
  195. char expBuf[1024];
  196. tVal = (PARSE_RESULT_NET_INTERFACES *) malloc( sizeof(PARSE_RESULT_NET_INTERFACES));
  197. if ( tVal ) {
  198. memset(tVal, 0 , sizeof(PARSE_RESULT_NET_INTERFACES));
  199. while (!feof(fd)) {
  200. fgets((CSTR)tB, 1024, fd);
  201. trim((CSTR)tB);
  202. if( strlen(tB) ) {
  203. strncpy( expBuf, tB, 1024 );
  204. CArray *CA = explode( ' ', expBuf );
  205. if( CA ) {
  206. for( CArray::iterator idx = CA->begin(); idx < CA->end(); idx++ ) {
  207. if((*idx)[0]=='#') break;
  208. if(strstr((*idx), "auto")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->enabled = true;
  209. if(strstr((*idx), "allow-hotplug")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->hot_plug = true;
  210. if(strstr((*idx), "iface")!= NULL ) if( strstr( tB, if_name )!= NULL ){
  211. if (strstr( tB, "static") != NULL) tVal->if_static = true;
  212. cConfig = true;
  213. } else cConfig = false;
  214. if(strstr((*idx), "address")!= NULL ) if( cConfig ){
  215. idx++;
  216. if(strstr((*idx),"/")!= NULL ){
  217. int8_t sh = strstr((*idx),"/") - (*idx);
  218. strncpy( tVal->address, (*idx), sh );
  219. tVal->address[sh]=(char)0;
  220. int8_t bm = atoi( (*idx) + sh + 1 );
  221. union{
  222. uint32_t mm;
  223. uint8_t nm[4];
  224. } nmu = {0};
  225. for ( sh = 0; sh < bm; sh++ ) nmu.mm |= (1 << sh);
  226. sprintf(tVal->netmask, "%d.%d.%d.%d", nmu.nm[0], nmu.nm[1], nmu.nm[2], nmu.nm[3]);
  227. } else {
  228. strncpy( tVal->address, (*idx), INET6_ADDRSTRLEN );
  229. };
  230. };
  231. if(strstr((*idx), "netmask")!= NULL ) if( cConfig ){
  232. LOG("netMSK");
  233. idx++;
  234. LOG(*idx);
  235. strncpy(tVal->netmask, (*idx), INET6_ADDRSTRLEN);
  236. };
  237. //if(strstr((*idx), "bridge_ports")!= NULL ) cMode = 6;
  238. //if(strstr((*idx), "bridge_stp")!= NULL ) cMode = 7;
  239. //if(strstr((*idx), "bridge_waitport")!= NULL ) cMode = 8;
  240. //if(strstr((*idx), "bridge_fd")!= NULL ) cMode = 9;
  241. };
  242. delete( CA );
  243. };
  244. };
  245. };
  246. };
  247. fclose( fd );
  248. };
  249. #ifdef DEBUG
  250. if ( tVal ) {
  251. if( tVal->enabled ) LOG("NIF Enabled"); else LOG("NIF Disabled");
  252. if (tVal->hot_plug) LOG("NIF Hot Plugable"); else LOG("NIF not Hot Plugable");
  253. if (tVal->if_static) LOG("Address is static"); else LOG("Address is Dynamic");
  254. if(strlen(tVal->address))LOG(tVal->address);
  255. if(strlen(tVal->netmask))LOG(tVal->netmask);
  256. };
  257. #endif
  258. return tVal;
  259. };
  260. PARSE_RESULT_WIFI_SETTINGS *mojoHardwareManager::parse_wifi( CSTR if_name ){
  261. PARSE_RESULT_WIFI_SETTINGS *tVal = NULL;
  262. FILE *fd;
  263. bool cConfig = false;
  264. tVal = (PARSE_RESULT_WIFI_SETTINGS *) malloc( sizeof(PARSE_RESULT_WIFI_SETTINGS));
  265. if ( tVal ) {
  266. memset(tVal, 0 , sizeof(PARSE_RESULT_WIFI_SETTINGS));
  267. fd = fopen("/etc/dnsmasq.conf","r");
  268. if ( fd ) {
  269. char tB[1024];
  270. char expBuf[1024];
  271. while (!feof(fd)) {
  272. fgets((CSTR)tB, 1024, fd);
  273. trim((CSTR)tB);
  274. if( strlen(tB) ) {
  275. strncpy( expBuf, tB, 1024 );
  276. CArray *CA = explode( '=', expBuf );
  277. if( CA ) {
  278. if( CA->size() ){
  279. if( CA->at(0)[0] == '#' ) continue;
  280. if( strstr(CA->at(0), "interface")!= NULL ) {
  281. if( strstr( tB, if_name)!= NULL ) {
  282. cConfig = true;
  283. tVal->dhcp_enabled = true;
  284. };
  285. };
  286. if( cConfig ){
  287. if ( strstr(CA->at(0), "dhcp-range")!= NULL ) {
  288. char tT[1024];
  289. strncpy( tT, CA->at(1), 1024 );
  290. CArray *CB = explode(',', tT );
  291. if( CB ){
  292. if( CB->size() > 3 ){
  293. strncpy( tVal->range_start, CB->at(0), INET6_ADDRSTRLEN);
  294. strncpy( tVal->range_stop, CB->at(1), INET6_ADDRSTRLEN);
  295. strncpy( tVal->netmask, CB->at(2), INET6_ADDRSTRLEN);
  296. strncpy( tVal->lease_period, CB->at(3), INET6_ADDRSTRLEN);
  297. };
  298. delete( CB );
  299. };
  300. };
  301. };
  302. };
  303. delete( CA );
  304. };
  305. };
  306. };
  307. fclose( fd );
  308. };
  309. //Check WiFi
  310. if( strstr(if_name,"w")!= NULL ) {
  311. char tName[128];
  312. sprintf( tName, "iwconfig %s", if_name );
  313. fd = popen( tName, "r" );
  314. if ( fd ) {
  315. char tB[1024];
  316. while (!feof(fd)) {
  317. fgets((CSTR)tB, 1024, fd );
  318. trim( tB );
  319. if (strstr(tB, "Mode") != NULL) if (strstr(tB, "Master") != NULL) tVal->access_point = true;
  320. };
  321. fclose( fd );
  322. };
  323. };
  324. if ( tVal->access_point ) {
  325. fd = fopen("/etc/hostapd/hostapd.conf", "r");
  326. if ( fd ) {
  327. char tB[1024];
  328. char expBuf[1024];
  329. while (!feof(fd)) {
  330. fgets((CSTR)tB, 1024, fd);
  331. trim( tB );
  332. if( strlen(tB) ) {
  333. if( tB[0]=='#' ) continue;
  334. strncpy(expBuf, tB, 1024);
  335. CArray *CA = explode( '=', expBuf );
  336. if( CA ) {
  337. if( CA->size() ){
  338. if (strstr(CA->at(0), "ssid") != NULL) if (strstr(CA->at(0), "ignore_broadcast_ssid") == NULL){
  339. strncpy( tVal->SSID, CA->at(1), 64 );
  340. } else tVal->hiden_ssid = (bool) atoi(CA->at(1));
  341. if (strstr(CA->at(0), "wpa_passphrase") != NULL) strncpy( tVal->password, CA->at(1), 64 );
  342. if (strstr(CA->at(0), "country_code") != NULL) strncpy(tVal->country_code, CA->at(1), 3);
  343. if (strstr(CA->at(0), "channel") != NULL) tVal->channel = atoi(CA->at(1));
  344. };
  345. delete( CA );
  346. };
  347. };
  348. };
  349. fclose( fd );
  350. };
  351. } else {
  352. fd = fopen("/etc/wpa_supplicant/wpa_supplicant.conf", "r");
  353. if ( fd ) {
  354. fclose( fd );
  355. };
  356. };
  357. fd = fopen("/etc/rc.local","r");
  358. if ( fd ) {
  359. char tB[1024];
  360. char expBuf[1024];
  361. while (!feof(fd)) {
  362. fgets((CSTR)tB, 1024, fd);
  363. trim((CSTR)tB);
  364. if( strlen(tB) ) {
  365. strncpy( expBuf, tB, 1024 );
  366. CArray *CA = explode( ' ', expBuf );
  367. if( CA ) {
  368. if ( CA->size() ) {
  369. if (CA->at(0)[0] == '#') continue;
  370. if( strstr(CA->at(0), "iptables-restore")!= NULL ) {
  371. char zB[1024];
  372. FILE *afd = fopen( CA->at(2), "r" );
  373. if ( afd ) {
  374. while( !feof( afd ) ){
  375. fgets((CSTR)zB, 1024, afd );
  376. if( strstr( zB, "POSTROUTING" )!= NULL ) if( strstr( zB, "MASQUERADE" )!= NULL ) if( strstr( zB, if_name )== NULL ){
  377. CArray *CB = explode( ' ', zB );
  378. if ( CB ) {
  379. if ( CB->size() > 3 ) {
  380. strncpy(tVal->router_outbond, CB->at(3), INET6_ADDRSTRLEN);
  381. };
  382. delete( CB );
  383. };
  384. tVal->router_enabled = true;
  385. };
  386. };
  387. fclose(afd);
  388. } else LOG("Error Opening Firewall Rules!");
  389. };
  390. };
  391. delete( CA );
  392. };
  393. };
  394. };
  395. fclose(fd);
  396. } else LOG_ERROR("Can not open rc.local!");
  397. };
  398. #ifdef DEBUG
  399. if ( tVal ) {
  400. if (tVal->access_point) LOG("AP Mode"); else LOG("WiFi Mode");
  401. LOG(tVal->SSID);
  402. if (tVal->hiden_ssid) LOG("SSID is hidden");
  403. LOG(tVal->password);
  404. if (tVal->dhcp_enabled){
  405. LOG("DHCP Service");
  406. LOG(tVal->range_start);
  407. LOG(tVal->range_stop);
  408. LOG(tVal->lease_period);
  409. } else LOG("DHCP Disabled");
  410. if (tVal->router_enabled){
  411. LOG("Routing Enabled");
  412. LOG( tVal->router_outbond );
  413. } else LOG("Routing Disabled");
  414. };
  415. #endif
  416. return tVal;
  417. };
  418. void mojoHardwareManager::enumerateNIFS( void ){
  419. struct ifaddrs * ifaddr = NULL;
  420. char tBuffer[128];
  421. //if( m_pKernel == NULL ) return;
  422. if( m_pDBC == NULL ) return;
  423. if( -1 == getifaddrs(&ifaddr)) {
  424. LOG_ERROR("No NET INTERFACES?!");
  425. } else {
  426. #ifdef DEBUG
  427. LOG("Enumerating Network Interfaces");
  428. #endif
  429. struct ifaddrs * ifa;
  430. for( ifa = ifaddr; ifa; ifa = ifa->ifa_next ){
  431. char addr[INET6_ADDRSTRLEN];
  432. char mask[INET6_ADDRSTRLEN];
  433. char gw[INET6_ADDRSTRLEN];
  434. char bc[INET6_ADDRSTRLEN];
  435. char mac[INET6_ADDRSTRLEN];
  436. char device[INET6_ADDRSTRLEN];
  437. uint32_t nif_id = 0;
  438. uint32_t nif_aux_id = 0;
  439. uint32_t router_id = 0;
  440. ARGUS_NIF_TYPE nif_type = NIF_LOOPBACK;
  441. ARGUS_NETWORK_TYPE net_type = NETWORK_IPV4;
  442. ARGUS_RESOURCE_TYPE res_type = ARGUS_HARDWARE_NIF_LOOPBACK;
  443. ARGUS_NIF_MODE nif_mode = ADDRESS_DISABLED;
  444. #ifdef DEBUG_EXTRA
  445. LOG("NIF ***");
  446. #endif
  447. strncpy( device, ifa->ifa_name, INET6_ADDRSTRLEN );
  448. strncpy( mac,"--no-mac--", INET6_ADDRSTRLEN );
  449. // Filter Loopback if
  450. if( strstr( device, "lo" ) != NULL ){
  451. nif_type = NIF_LOOPBACK;
  452. res_type = ARGUS_HARDWARE_NIF_LOOPBACK;
  453. //Not Implemented
  454. continue;
  455. };
  456. // Filter vpn TUN if
  457. if( strstr( device, "tun" ) != NULL ){
  458. nif_type = NIF_VPN_TUNEL;
  459. res_type = ARGUS_HARDWARE_NIF_TUNEL;
  460. //Not Implemented Yet
  461. continue;
  462. };
  463. if( strstr( device, "eth" ) != NULL ){
  464. LOG(device);
  465. nif_type = NIF_ETHERNET;
  466. res_type = ARGUS_HARDWARE_NIF_ETHERNET;
  467. };
  468. if( strstr( device, "wlan" ) != NULL ){
  469. LOG(device);
  470. nif_type = NIF_WIFI;
  471. res_type = ARGUS_HARDWARE_NIF_WIFI;
  472. };
  473. if( strstr( device, "wwan" ) != NULL ){
  474. LOG(device);
  475. nif_type = NIF_WAN;
  476. res_type = ARGUS_HARDWARE_NIF_WAN;
  477. };
  478. if( ifa->ifa_addr != NULL ){
  479. if (ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) {
  480. LOG("CONT");
  481. continue;
  482. }
  483. } else {
  484. LOG("ifa->ifa_addr is NULL");
  485. continue;
  486. };
  487. LOG("GETMAC");
  488. getMAC( device, mac );
  489. getGW( device, gw );
  490. LOG( mac );
  491. LOG( gw );
  492. PARSE_RESULT_NET_INTERFACES * tt = NULL;
  493. PARSE_RESULT_WIFI_SETTINGS * tw = NULL;
  494. tt = parse_neconfig( device );
  495. tw = parse_wifi( device );
  496. if( tt ) {
  497. if ( tt->enabled ) {
  498. if( tt->if_static ){
  499. nif_mode = ADDRESS_STATIC;
  500. } else {
  501. nif_mode = ADDRESS_DYNAMIC;
  502. };
  503. } else {
  504. nif_mode = ADDRESS_DISABLED;
  505. };
  506. } else {
  507. LOG_ERROR("Error Parsing System NIF Settings!");
  508. continue;
  509. };
  510. if ( tw ){
  511. if( nif_mode == ADDRESS_STATIC ) {
  512. if (tw->dhcp_enabled) {
  513. nif_mode = ADDRESS_STATIC_RAP;
  514. };
  515. };
  516. if ( nif_type == NIF_WIFI ){
  517. nif_id = m_pDBC->addLocalNIF( device, mac, nif_type, nif_mode );
  518. if( nif_id == 0 ) continue;
  519. ARGUS_WIFI_MODE w_mode;
  520. if( !tt->enabled ) {
  521. w_mode = WIFI_DISABLED;
  522. } else {
  523. if (tt->if_static) {
  524. if( tw->access_point ) {
  525. w_mode = WIFI_IS_AP;
  526. uint32_t outbond_id = 0;
  527. if( tw->router_enabled ){
  528. outbond_id = m_pDBC->getNIF_IDbyDevice( tw->router_outbond );
  529. };
  530. if( tw->dhcp_enabled ) {
  531. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period );
  532. } else {
  533. router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id );
  534. };
  535. } else {
  536. w_mode = WIFI_CONNECTED_TO_AP;
  537. };
  538. };
  539. };
  540. nif_aux_id = m_pDBC->addLocalWiFi(nif_id, w_mode, tw->SSID, tw->password, ENCRIPTION_WPA_PSK, router_id );
  541. };
  542. if( nif_type == NIF_ETHERNET || nif_type == NIF_ETHERNET_VIRTUAL ){
  543. };
  544. free(tt);
  545. free(tw);
  546. } else {
  547. LOG_ERROR("Error Parsing System Router/AP Settings!");
  548. free(tt);
  549. continue;
  550. };
  551. nif_id = m_pDBC->addLocalNIF( device, mac, nif_type, nif_mode, nif_aux_id );
  552. if( nif_id == 0 ) continue;
  553. LOG("NIF added!");
  554. m_pDBC->addResource( 1, res_type, device, nif_id );
  555. if(ifa->ifa_addr->sa_family == AF_INET){
  556. net_type = NETWORK_IPV4;
  557. // create IPv4 string
  558. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  559. inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
  560. sprintf( mask, "%d.%d.%d.%d",
  561. (uint8_t)ifa->ifa_netmask->sa_data[2],
  562. (uint8_t)ifa->ifa_netmask->sa_data[3],
  563. (uint8_t)ifa->ifa_netmask->sa_data[4],
  564. (uint8_t)ifa->ifa_netmask->sa_data[5]
  565. );
  566. sprintf( bc, "%d.%d.%d.%d",
  567. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  568. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  569. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  570. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  571. );
  572. } else { // AF_INET6
  573. net_type = NETWORK_IPV6;
  574. // create IPv6 string
  575. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  576. inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
  577. sprintf( mask, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  578. (uint8_t)ifa->ifa_netmask->sa_data[2],
  579. (uint8_t)ifa->ifa_netmask->sa_data[3],
  580. (uint8_t)ifa->ifa_netmask->sa_data[4],
  581. (uint8_t)ifa->ifa_netmask->sa_data[5],
  582. (uint8_t)ifa->ifa_netmask->sa_data[6],
  583. (uint8_t)ifa->ifa_netmask->sa_data[7],
  584. (uint8_t)ifa->ifa_netmask->sa_data[8],
  585. (uint8_t)ifa->ifa_netmask->sa_data[9],
  586. (uint8_t)ifa->ifa_netmask->sa_data[10],
  587. (uint8_t)ifa->ifa_netmask->sa_data[11],
  588. (uint8_t)ifa->ifa_netmask->sa_data[12],
  589. (uint8_t)ifa->ifa_netmask->sa_data[13],
  590. (uint8_t)ifa->ifa_netmask->sa_data[14],
  591. (uint8_t)ifa->ifa_netmask->sa_data[15],
  592. (uint8_t)ifa->ifa_netmask->sa_data[16],
  593. (uint8_t)ifa->ifa_netmask->sa_data[17]
  594. );
  595. if( ifa->ifa_ifu.ifu_broadaddr != NULL ){
  596. sprintf( bc, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  597. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  598. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  599. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  600. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5],
  601. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[6],
  602. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[7],
  603. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[8],
  604. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[9],
  605. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[10],
  606. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[11],
  607. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[12],
  608. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[13],
  609. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[14],
  610. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[15],
  611. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[16],
  612. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[17]
  613. );
  614. } else {
  615. sprintf( bc, "" );
  616. };
  617. };
  618. m_pDBC->addLocalNetwork( nif_id, net_type, addr, mask, gw, bc );
  619. #ifdef DEBUG
  620. sprintf( tBuffer, "Detected Network Interface: %s [%s]", ifa->ifa_name, addr );
  621. LOG(tBuffer);
  622. #endif
  623. };
  624. freeifaddrs( ifaddr );
  625. };
  626. };
  627. void mojoHardwareManager::enumerateHardware( void ){
  628. char tBuffer[CTEXT];
  629. DIR * dFD = NULL;
  630. struct dirent * pDR = NULL;
  631. //ToDo: Check Ability to work without Kernel and DBC
  632. //if( m_pKernel == NULL ) return;
  633. if( m_pDBC == NULL ) return;
  634. #ifdef DEBUG
  635. LOG("HW Enumeration Started!");
  636. #endif
  637. // Moved to a constructor!!
  638. //m_nServerNodeID = m_pKernel->getServerNodeId();
  639. //m_nServerID = m_pKernel->getServerId();
  640. //m_nSiteID = m_pKernel->getSiteId();
  641. /* Add Network Interfaces */
  642. m_pDBC->cleanResources( 1, true );
  643. enumerateNIFS();
  644. /* Check HID Devices */
  645. struct hid_device_info * pDevs;
  646. uint8_t DCBCount;
  647. //Enumerate USB devices with the given IDs
  648. pDevs = hid_enumerate( 0x16c0, 0x05df );
  649. if( pDevs!=NULL ){
  650. struct hid_device_info * pCDev = pDevs;
  651. //Count the number of returned devices
  652. for( DCBCount = 0; pCDev!=NULL; DCBCount++ ) pCDev = pCDev->next;
  653. //Allocate a buffer for the relays
  654. if( DCBCount > 0 ){
  655. DC_BOARD tDCB;
  656. //Fill the relay structs
  657. pCDev = pDevs;
  658. for( int idx = 0; idx < DCBCount; idx++ ){
  659. //Save the path to this device
  660. memset( tDCB.path, 0, MAX_PATH_LEN );
  661. strncpy( tDCB.path, pCDev->path, MAX_PATH_LEN );
  662. // The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
  663. tDCB.relay_count = atoi((const char *)&pCDev->product_string[8]);
  664. //Open it to get more details
  665. hid_device * handle;
  666. handle = hid_open_path( tDCB.path );
  667. if( handle ){
  668. unsigned char buf[9];
  669. //Get the features of the device
  670. buf[0] = 0x01;
  671. if( hid_get_feature_report( handle, buf, sizeof(buf)) == -1 ){
  672. LOG_ERROR("hid_get_feature_report");
  673. pCDev = pCDev->next;
  674. hid_close( handle );
  675. continue;
  676. };
  677. //Set the serial number (0x0 for termination)
  678. memset( tDCB.serial, 0x0, (HID_DCR_SERIIAL_LENGTH+1) );
  679. memcpy( tDCB.serial, buf, HID_DCR_SERIIAL_LENGTH );
  680. //Byte 7 in the response contains the target_state of the relays
  681. tDCB.state = buf[7];
  682. hid_close( handle );
  683. #ifdef DEBUG
  684. //Output the device enumeration details if verbose is on
  685. LOG("HID DCB Found");
  686. sprintf(tBuffer, " Type: %04hx::%04hx", pCDev->vendor_id, pCDev->product_id);
  687. LOG(tBuffer);
  688. sprintf(tBuffer, " Path: %s", pCDev->path );
  689. LOG(tBuffer);
  690. sprintf(tBuffer, " Serial_number: %ls", pCDev->serial_number );
  691. LOG(tBuffer);
  692. sprintf(tBuffer, " Manufacturer: %ls", pCDev->manufacturer_string);
  693. LOG(tBuffer);
  694. sprintf(tBuffer, " Product: %ls", pCDev->product_string);
  695. LOG(tBuffer);
  696. sprintf(tBuffer, " Release: %hx", pCDev->release_number);
  697. LOG(tBuffer);
  698. sprintf(tBuffer, " Interface: %d", pCDev->interface_number);
  699. LOG(tBuffer);
  700. sprintf(tBuffer, " Number of Relays = %d", tDCB.relay_count);
  701. LOG(tBuffer);
  702. //If debug are on, output individual relay details
  703. for( int k = 0; k < tDCB.relay_count; k++ ){
  704. if( tDCB.state & 1 << k ){
  705. sprintf(tBuffer, " %s_%d=1", tDCB.serial, k + 1);
  706. } else {
  707. sprintf(tBuffer, " %s_%d=0", tDCB.serial, k + 1);
  708. };
  709. LOG(tBuffer);
  710. };
  711. #endif
  712. m_pDBC->addLocalDCB( 1, pCDev->path, tDCB.serial, tDCB.relay_count, tDCB.state );
  713. } else {
  714. LOG_ERROR("unable to open HID device");
  715. };
  716. pCDev = pCDev->next;
  717. };
  718. };
  719. hid_free_enumeration( pDevs );
  720. };
  721. /* Add devices from DEV */
  722. dFD = opendir("/dev");
  723. if( dFD != NULL ) {
  724. while( ( pDR = readdir( dFD ))!= NULL ){
  725. if( strcasestr( pDR->d_name, "video" ) == pDR->d_name ) {
  726. createResourceVIDEO( pDR->d_name );
  727. continue;
  728. };
  729. if( strcasestr( pDR->d_name, "ttyS" ) == pDR->d_name ){
  730. createResourceUART( pDR->d_name );
  731. continue;
  732. };
  733. // RaspberyPI Hardware
  734. if( strcasestr( pDR->d_name, "ttyAMA" ) == pDR->d_name ){
  735. createResourceUART( pDR->d_name );
  736. continue;
  737. };
  738. if( strcasestr( pDR->d_name, "ttyUSB" ) == pDR->d_name ){
  739. createResourceUART( pDR->d_name );
  740. continue;
  741. };
  742. };
  743. closedir( dFD );
  744. };
  745. //m_bHardwareEnumerated = true;
  746. };
  747. uint32_t mojoHardwareManager::createResourceUART( CSTR devName ){
  748. uint32_t RetVal = 0;
  749. char tBuffer[256];
  750. /* Enumerate BuiltIn Serial */
  751. #ifdef DEBUG_EXTRA
  752. sprintf( tBuffer, "Serial Device: %s", devName );
  753. LOG( tBuffer );
  754. #endif
  755. char dev_name[CTEXT];
  756. int fd = -1;
  757. struct serial_struct ss;
  758. sprintf( dev_name, "/dev/%s", devName );
  759. fd = open( dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK );
  760. if (-1 == fd) {
  761. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  762. LOG_ERROR( tBuffer );
  763. } else {
  764. if ( 0 && ( -1 == xioctl(fd, TIOCGSERIAL, &ss) )){
  765. LOG_ERROR("TIOCGSERIAL");
  766. } else {
  767. #ifdef DEBUG_EXTRA
  768. sprintf( tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]",
  769. ss.port,
  770. ss.irq,
  771. ss.baud_base,
  772. ss.line,
  773. ss.type,
  774. ss.flags
  775. );
  776. LOG( tBuffer );
  777. #endif
  778. getTTyDesc( devName, tBuffer );
  779. uint32_t tID = m_pDBC->addLocalUART( 1, 1, 19200, 8, 'N', 1.0f, dev_name );
  780. if ( tID ) {
  781. m_pDBC->addResource( 1, ARGUS_HARDWARE_UART, tBuffer, tID );
  782. };
  783. };
  784. close(fd);
  785. };
  786. return RetVal;
  787. };
  788. uint32_t mojoHardwareManager::createResourceVIDEO( CSTR devName ){
  789. uint32_t RetVal = 0;
  790. char tBuffer[256];
  791. /* Enumerate Video Capture */
  792. #ifdef DEBUG_EXTRA
  793. sprintf( tBuffer, "Video Source: %s", devName );
  794. LOG( tBuffer );
  795. #endif
  796. struct v4l2_capability cap;
  797. //struct v4l2_cropcap cropcap;
  798. //struct v4l2_crop crop;
  799. struct v4l2_format fmt;
  800. char dev_name[CTEXT];
  801. int fd = -1;
  802. struct stat st;
  803. sprintf( dev_name, "/dev/%s", devName );
  804. if (-1 == stat( dev_name, &st )) {
  805. sprintf( tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  806. LOG_ERROR( tBuffer );
  807. return 0;
  808. };
  809. if (!S_ISCHR(st.st_mode)) {
  810. sprintf( tBuffer, "%s is no devicen", dev_name);
  811. LOG_ERROR( tBuffer );
  812. return 0;
  813. };
  814. fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  815. if (-1 == fd) {
  816. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  817. LOG_ERROR( tBuffer );
  818. } else {
  819. if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
  820. if (EINVAL == errno) {
  821. sprintf( tBuffer, "%s is no V4L2 device", dev_name );
  822. LOG_ERROR( tBuffer );
  823. } else {
  824. LOG_ERROR( "VIDIOC_QUERYCAP" );
  825. };
  826. close(fd);
  827. return 0;
  828. };
  829. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  830. #ifdef DEBUG_EXTRA
  831. sprintf(tBuffer, "%s is no video capture device", dev_name);
  832. LOG_ERROR( tBuffer );
  833. #endif
  834. close(fd);
  835. return 0;
  836. };
  837. CLEAR(fmt);
  838. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  839. /* Preserve original settings as set by v4l2-ctl for example */
  840. if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt)){
  841. //LOG_ERROR("VIDIOC_G_FMT");
  842. } else {
  843. #ifdef DEBUG_EXTRA
  844. sprintf( tBuffer, "Grabber[%dx%d]",fmt.fmt.pix.width,fmt.fmt.pix.height);
  845. LOG( tBuffer );
  846. #endif
  847. uint64_t mid = m_pDBC->addLocalMediaSource( 1, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height );
  848. //m_pDBC->addNodeSocket( m_nServerNodeID, mid, ARGUS_NODE_SOCK_TYPE_ANALOG_VIDEO_GRABBER, ARGUS_NODE_SOCK_MODE_OUT, false, dev_name, tBuffer );
  849. RetVal = m_pDBC->addResource( 1, ARGUS_HARDWARE_ANALOG_VIDEO_IN, dev_name, mid );
  850. };
  851. close(fd);
  852. };
  853. return RetVal;
  854. };
  855. uint32_t mojoHardwareManager::createResourceUDCB( CSTR devName ){
  856. uint32_t RetVal = 0;
  857. char tBuffer[256];
  858. return RetVal;
  859. };
  860. /*
  861. killWifi(){
  862. ifconfig wlan0 down
  863. ifconfig wlan0 up
  864. iw dev wlan0 station dump
  865. }
  866. */
  867. #undef LOG
  868. #undef LOG_ERROR