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.

679 lines
26 KiB

6 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
​
​
​
​
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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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. #define LOG( X ) m_pLog->Log( "HWM", this, X )
  10. #ifdef DEBUG
  11. #define LOG_ERROR( X ) {\
  12. char _tBfrErrMSG2w34[256];\
  13. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  14. m_pLog->Log( "HWM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  15. }
  16. #else
  17. #define LOG_ERROR( X ) m_pLog->Log( "HWM", this, _LOG_ERROR, X );
  18. #endif
  19. argusHardwareManager *argusHardwareManager::_instance = NULL;
  20. /**
  21. * Returns the single instance of the object.
  22. */
  23. argusHardwareManager* argusHardwareManager::getInstance( bool cold_start ) {
  24. // check if the instance has been created yet
  25. if( 0 == _instance ){
  26. // if not, then create it
  27. _instance = new argusHardwareManager( cold_start );
  28. };
  29. // return the single instance
  30. return _instance;
  31. };
  32. argusHardwareManager::argusHardwareManager( bool cold_start ){
  33. m_pLog = basicLogCollection::getInstance();
  34. //m_bHardwareEnumerated = false;
  35. m_nServerNodeID = 0;
  36. m_nServerID = 0;
  37. m_nSiteID = 0;
  38. m_nMemoryLimitHeapSize = 524288000; // 50 MB //524288000; //500 MB ~ 1 Hour
  39. m_pKernel = argusKernel::getInstance();
  40. if( m_pKernel == NULL ){
  41. LOG_ERROR( "Failure to get Kernel Instance!" );
  42. } else {
  43. m_nServerNodeID = m_pKernel->getServerNodeId();
  44. m_nServerID = m_pKernel->getServerId();
  45. m_nSiteID = m_pKernel->getSiteId();
  46. };
  47. m_pDBC = argusDBC::getInstance();
  48. if( m_pDBC == NULL ) {
  49. LOG_ERROR( "Failure to get DBC Instance -> Forced HW Enumeration!" );
  50. enumerateHardware();
  51. } else {
  52. //enumerateHardware( !cold_start );
  53. enumerateNIFS();
  54. #ifdef DEBUG
  55. LOG("Loading Ressources...");
  56. #endif
  57. loadResources();
  58. };
  59. };
  60. argusHardwareManager::~argusHardwareManager(){
  61. unloadResources();
  62. };
  63. void argusHardwareManager::unloadResources( void ){
  64. if( m_vResources.size() ) {
  65. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  66. switch( (*idx)->getHardwareType() ) {
  67. case ARGUS_HARDWARE_UART:
  68. case ARGUS_HARDWARE_IP_UART: delete( (argusHardwareResourceSerialProvider*)(*idx) ); break;
  69. case ARGUS_HARDWARE_NIF_LOOPBACK:
  70. case ARGUS_HARDWARE_NIF_ETHERNET:
  71. case ARGUS_HARDWARE_NIF_WIFI:
  72. case ARGUS_HARDWARE_NIF_VIRTUAL:
  73. case ARGUS_HARDWARE_NIF_TUNEL: delete( (argusHardwareResourceNetwork*)(*idx) ); break;
  74. };
  75. };
  76. m_vResources.clear();
  77. };
  78. };
  79. void argusHardwareManager::loadResources( void ){
  80. if( m_pDBC == NULL ) return;
  81. DBC_INT_LIST *tVal;
  82. //tVal = m_pDBC->listResources( );//load whole site resources
  83. tVal = m_pDBC->listResources( m_nServerNodeID );//Load Only Server Node resources
  84. if( tVal!= NULL ) {
  85. for( DBC_INT_LIST::iterator index = tVal->begin(); index < tVal->end(); index++ ) {
  86. DBC_RESOURCE_DATA *tData = m_pDBC->getResourceData( *index );
  87. if( tData != NULL ) {
  88. argusHardwareResource *tPtr = NULL;
  89. switch( tData->type ){
  90. case ARGUS_HARDWARE_UART:{
  91. #ifdef DEBUG
  92. LOG("Construct HW UART");
  93. #endif
  94. argusHardwareResourceSerialProvider *tRes = new argusHardwareResourceSerialProvider( tData->id );
  95. if( tRes != NULL ) {
  96. if( tRes->create() ) {
  97. tPtr = ( argusHardwareResource *) tRes;
  98. } else {
  99. LOG_ERROR("Error Creating HW UART!");
  100. delete( tRes );
  101. };
  102. };
  103. break;
  104. };
  105. case ARGUS_HARDWARE_NIF_LOOPBACK:
  106. case ARGUS_HARDWARE_NIF_ETHERNET:
  107. case ARGUS_HARDWARE_NIF_WIFI:
  108. case ARGUS_HARDWARE_NIF_VIRTUAL:
  109. case ARGUS_HARDWARE_NIF_TUNEL:{
  110. #ifdef DEBUG
  111. LOG("Construct NetIF");
  112. #endif
  113. argusHardwareResourceNetwork *tRes = new argusHardwareResourceNetwork( tData->id );
  114. if( tRes != NULL ) {
  115. if( tRes->create() ) {
  116. tPtr = ( argusHardwareResource *) tRes;
  117. } else {
  118. LOG_ERROR("Error Creating Network Ressource!");
  119. delete( tRes );
  120. };
  121. };
  122. break;
  123. };
  124. case ARGUS_HARDWARE_IP_UART:{
  125. #ifdef DEBUG
  126. LOG("Construct IP UART");
  127. #endif
  128. argusHardwareResourceSerialProvider *tRes = new argusHardwareResourceSerialProvider( tData->id );
  129. if( tRes != NULL ) {
  130. if( tRes->create() ) {
  131. tPtr = ( argusHardwareResource *) tRes;
  132. } else {
  133. LOG_ERROR("Error Creating IP UART!");
  134. delete( tRes );
  135. };
  136. };
  137. break;
  138. };
  139. #ifdef DEBUG
  140. default:{
  141. char tB[128];
  142. sprintf( tB, "Unknown Ressource[%d] Type[%d]!", *index, tData->type );
  143. LOG_ERROR(tB);
  144. break;
  145. };
  146. #endif
  147. };
  148. if( tPtr!= NULL ) {
  149. #ifdef DEBUG
  150. LOG("Resource Sucessfully Created!");
  151. #endif
  152. m_vResources.push_back( tPtr );
  153. };
  154. } else {
  155. LOG_ERROR("Can not fetch Resource Data from Database!");
  156. };
  157. };
  158. tVal->clear();
  159. delete( tVal );
  160. };
  161. };
  162. bool argusHardwareManager::validate( void ){
  163. bool RetVal = true;
  164. #ifdef DEBUG_EXTRA
  165. LOG("Validator");
  166. #endif
  167. //if( !m_bHardwareEnumerated ) return;// RetVal;
  168. //enumerateHardware( m_bHardwareEnumerated );
  169. if( m_vResources.size() ) {
  170. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  171. if( !(*idx)->validate() ) {
  172. // recreate Resource
  173. LOG_ERROR( "HW Resource Invalid!" );
  174. if( !(*idx)->create()){
  175. LOG_ERROR( "HW Resource Creation Failure!" );
  176. } else LOG("Re-Created OK");
  177. }
  178. #ifdef DEBUG_EXTRA
  179. else LOG("Resource OK!")
  180. #endif
  181. ;
  182. };
  183. }
  184. #ifdef DEBUG_EXTRA
  185. else LOG("No Resources!")
  186. #endif
  187. ;
  188. //Mem Validator
  189. if( m_nMemoryLimitHeapSize ) {
  190. size_t memStat = getCurrentRSS();
  191. #ifdef DEBUG_EXTRA
  192. char MEM_MSG[128];
  193. sprintf( MEM_MSG, "MEM: %d TARG: %d", memStat, m_nMemoryLimitHeapSize );
  194. LOG( MEM_MSG );
  195. #endif
  196. //https://www.ebay.com/itm/274974088159?
  197. RetVal = ( memStat < m_nMemoryLimitHeapSize );
  198. };
  199. return RetVal;
  200. };
  201. int xioctl(int fh, int request, void *arg){
  202. int r;
  203. do {
  204. r = ioctl( fh, request, arg );
  205. } while (-1 == r && EINTR == errno);
  206. return r;
  207. };
  208. #define CLEAR(x) memset(&(x), 0, sizeof(x))
  209. #include <sys/types.h>
  210. #include <ifaddrs.h>
  211. void argusHardwareManager::getMAC( CSTR if_name, CSTR buf ){
  212. char tB[128];
  213. FILE * fd=NULL;
  214. if ( buf ) {
  215. sprintf(tB, "/sys/class/net/%s/address", if_name);
  216. fd=fopen(tB,"r");
  217. if ( fd ) {
  218. fread( buf, 1, INET6_ADDRSTRLEN, fd );
  219. fclose( fd );
  220. buf = trim( buf );
  221. } else {
  222. memset( buf, 0, INET6_ADDRSTRLEN );
  223. };
  224. };
  225. };
  226. void argusHardwareManager::enumerateNIFS( void ){
  227. struct ifaddrs * ifaddr = NULL;
  228. char tBuffer[128];
  229. if( m_pKernel == NULL ) return;
  230. if( m_pDBC == NULL ) return;
  231. if( -1 == getifaddrs(&ifaddr)) {
  232. LOG_ERROR("No NET INTERFACES?!");
  233. } else {
  234. #ifdef DEBUG
  235. LOG("Enumerating Network Interfaces");
  236. #endif
  237. struct ifaddrs * ifa;
  238. char addr[INET6_ADDRSTRLEN];
  239. char mask[INET6_ADDRSTRLEN];
  240. char gw[INET6_ADDRSTRLEN];
  241. char bc[INET6_ADDRSTRLEN];
  242. char mac[INET6_ADDRSTRLEN];
  243. char device[INET6_ADDRSTRLEN];
  244. uint32_t nif_id;
  245. ARGUS_NIF_TYPE nif_type;
  246. ARGUS_NETWORK_TYPE net_type;
  247. ARGUS_RESOURCE_TYPE res_type;
  248. for( ifa = ifaddr; ifa; ifa = ifa->ifa_next ){
  249. #ifdef DEBUG_EXTRA
  250. LOG("NIF ***");
  251. #endif
  252. strncpy( device, ifa->ifa_name, INET6_ADDRSTRLEN );
  253. strncpy( mac,"10928", INET6_ADDRSTRLEN );
  254. // Filter Loopback if
  255. if( strstr( device, "lo" ) != NULL ){
  256. nif_type = NIF_LOOPBACK;
  257. res_type = ARGUS_HARDWARE_NIF_LOOPBACK;
  258. //Not Implemented
  259. continue;
  260. };
  261. // Filter vpn TUN if
  262. if( strstr( device, "tun" ) != NULL ){
  263. nif_type = NIF_VPN_TUNEL;
  264. res_type = ARGUS_HARDWARE_NIF_TUNEL;
  265. //Not Implemented Yet
  266. continue;
  267. };
  268. if( strstr( device, "eth" ) != NULL ){
  269. LOG(device);
  270. nif_type = NIF_ETHERNET;
  271. res_type = ARGUS_HARDWARE_NIF_ETHERNET;
  272. };
  273. if( strstr( device, "wlan" ) != NULL ){
  274. LOG(device);
  275. nif_type = NIF_WIFI;
  276. res_type = ARGUS_HARDWARE_NIF_WIFI;
  277. };
  278. if( ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6 ) continue;
  279. getMAC( device, mac );
  280. LOG( mac );
  281. nif_id = m_pDBC->addLocalNIF( device, mac, nif_type );
  282. if( nif_id == 0 ) continue;
  283. LOG("NIF added!");
  284. m_pDBC->addResource( m_nServerNodeID, res_type, device, nif_id );
  285. if(ifa->ifa_addr->sa_family == AF_INET){
  286. net_type = NETWORK_IPV4;
  287. // create IPv4 string
  288. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  289. inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
  290. sprintf( mask, "%d.%d.%d.%d",
  291. (uint8_t)ifa->ifa_netmask->sa_data[2],
  292. (uint8_t)ifa->ifa_netmask->sa_data[3],
  293. (uint8_t)ifa->ifa_netmask->sa_data[4],
  294. (uint8_t)ifa->ifa_netmask->sa_data[5]
  295. );
  296. sprintf( bc, "%d.%d.%d.%d",
  297. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  298. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  299. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  300. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
  301. );
  302. sprintf( gw, "" );
  303. } else { // AF_INET6
  304. net_type = NETWORK_IPV6;
  305. // create IPv6 string
  306. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  307. inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
  308. sprintf( mask, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  309. (uint8_t)ifa->ifa_netmask->sa_data[2],
  310. (uint8_t)ifa->ifa_netmask->sa_data[3],
  311. (uint8_t)ifa->ifa_netmask->sa_data[4],
  312. (uint8_t)ifa->ifa_netmask->sa_data[5],
  313. (uint8_t)ifa->ifa_netmask->sa_data[6],
  314. (uint8_t)ifa->ifa_netmask->sa_data[7],
  315. (uint8_t)ifa->ifa_netmask->sa_data[8],
  316. (uint8_t)ifa->ifa_netmask->sa_data[9],
  317. (uint8_t)ifa->ifa_netmask->sa_data[10],
  318. (uint8_t)ifa->ifa_netmask->sa_data[11],
  319. (uint8_t)ifa->ifa_netmask->sa_data[12],
  320. (uint8_t)ifa->ifa_netmask->sa_data[13],
  321. (uint8_t)ifa->ifa_netmask->sa_data[14],
  322. (uint8_t)ifa->ifa_netmask->sa_data[15],
  323. (uint8_t)ifa->ifa_netmask->sa_data[16],
  324. (uint8_t)ifa->ifa_netmask->sa_data[17]
  325. );
  326. if( ifa->ifa_ifu.ifu_broadaddr != NULL ){
  327. sprintf( bc, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
  328. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
  329. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
  330. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
  331. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5],
  332. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[6],
  333. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[7],
  334. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[8],
  335. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[9],
  336. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[10],
  337. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[11],
  338. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[12],
  339. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[13],
  340. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[14],
  341. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[15],
  342. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[16],
  343. (uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[17]
  344. );
  345. } else {
  346. sprintf( bc, "" );
  347. };
  348. sprintf( gw, "" );
  349. };
  350. m_pDBC->addLocalNetwork( nif_id, net_type, addr, mask, gw, bc );
  351. #ifdef DEBUG
  352. sprintf( tBuffer, "Detected Network Interface: %s [%s]", ifa->ifa_name, addr );
  353. LOG(tBuffer);
  354. #endif
  355. };
  356. freeifaddrs( ifaddr );
  357. };
  358. };
  359. void argusHardwareManager::enumerateHardware( void ){
  360. char tBuffer[CTEXT];
  361. DIR * dFD = NULL;
  362. struct dirent * pDR = NULL;
  363. //ToDo: Check Ability to work without Kernel and DBC
  364. if( m_pKernel == NULL ) return;
  365. if( m_pDBC == NULL ) return;
  366. #ifdef DEBUG
  367. LOG("HW Enumeration Started!");
  368. #endif
  369. // Moved to a constructor!!
  370. //m_nServerNodeID = m_pKernel->getServerNodeId();
  371. //m_nServerID = m_pKernel->getServerId();
  372. //m_nSiteID = m_pKernel->getSiteId();
  373. /* Add Network Interfaces */
  374. m_pDBC->cleanResources( m_nServerNodeID, true );
  375. enumerateNIFS();
  376. /* Check HID Devices */
  377. struct hid_device_info * pDevs;
  378. uint8_t DCBCount;
  379. //Enumerate USB devices with the given IDs
  380. pDevs = hid_enumerate( 0x16c0, 0x05df );
  381. if( pDevs!=NULL ){
  382. struct hid_device_info * pCDev = pDevs;
  383. //Count the number of returned devices
  384. for( DCBCount = 0; pCDev!=NULL; DCBCount++ ) pCDev = pCDev->next;
  385. //Allocate a buffer for the relays
  386. if( DCBCount > 0 ){
  387. DC_BOARD tDCB;
  388. //Fill the relay structs
  389. pCDev = pDevs;
  390. for( int idx = 0; idx < DCBCount; idx++ ){
  391. //Save the path to this device
  392. memset( tDCB.path, 0, MAX_PATH_LEN );
  393. strncpy( tDCB.path, pCDev->path, MAX_PATH_LEN );
  394. // The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
  395. tDCB.relay_count = atoi((const char *)&pCDev->product_string[8]);
  396. //Open it to get more details
  397. hid_device * handle;
  398. handle = hid_open_path( tDCB.path );
  399. if( handle ){
  400. unsigned char buf[9];
  401. //Get the features of the device
  402. buf[0] = 0x01;
  403. if( hid_get_feature_report( handle, buf, sizeof(buf)) == -1 ){
  404. LOG_ERROR("hid_get_feature_report");
  405. pCDev = pCDev->next;
  406. hid_close( handle );
  407. continue;
  408. };
  409. //Set the serial number (0x0 for termination)
  410. memset( tDCB.serial, 0x0, (HID_DCR_SERIIAL_LENGTH+1) );
  411. memcpy( tDCB.serial, buf, HID_DCR_SERIIAL_LENGTH );
  412. //Byte 7 in the response contains the target_state of the relays
  413. tDCB.state = buf[7];
  414. hid_close( handle );
  415. #ifdef DEBUG
  416. //Output the device enumeration details if verbose is on
  417. LOG("HID DCB Found");
  418. sprintf(tBuffer, " Type: %04hx::%04hx", pCDev->vendor_id, pCDev->product_id);
  419. LOG(tBuffer);
  420. sprintf(tBuffer, " Path: %s", pCDev->path );
  421. LOG(tBuffer);
  422. sprintf(tBuffer, " Serial_number: %ls", pCDev->serial_number );
  423. LOG(tBuffer);
  424. sprintf(tBuffer, " Manufacturer: %ls", pCDev->manufacturer_string);
  425. LOG(tBuffer);
  426. sprintf(tBuffer, " Product: %ls", pCDev->product_string);
  427. LOG(tBuffer);
  428. sprintf(tBuffer, " Release: %hx", pCDev->release_number);
  429. LOG(tBuffer);
  430. sprintf(tBuffer, " Interface: %d", pCDev->interface_number);
  431. LOG(tBuffer);
  432. sprintf(tBuffer, " Number of Relays = %d", tDCB.relay_count);
  433. LOG(tBuffer);
  434. //If debug are on, output individual relay details
  435. for( int k = 0; k < tDCB.relay_count; k++ ){
  436. if( tDCB.state & 1 << k ){
  437. sprintf(tBuffer, " %s_%d=1", tDCB.serial, k + 1);
  438. } else {
  439. sprintf(tBuffer, " %s_%d=0", tDCB.serial, k + 1);
  440. };
  441. LOG(tBuffer);
  442. };
  443. #endif
  444. m_pDBC->addLocalDCB( m_nSiteID, pCDev->path, tDCB.serial, tDCB.relay_count, tDCB.state );
  445. } else {
  446. LOG_ERROR("unable to open HID device");
  447. };
  448. pCDev = pCDev->next;
  449. };
  450. };
  451. hid_free_enumeration( pDevs );
  452. };
  453. /* Add devices from DEV */
  454. dFD = opendir("/dev");
  455. if( dFD != NULL ) {
  456. while( ( pDR = readdir( dFD ))!= NULL ){
  457. if( strcasestr( pDR->d_name, "video" ) == pDR->d_name ) {
  458. createResourceVIDEO( pDR->d_name );
  459. continue;
  460. };
  461. if( strcasestr( pDR->d_name, "ttyS" ) == pDR->d_name ){
  462. createResourceUART( pDR->d_name );
  463. continue;
  464. };
  465. // RaspberyPI Hardware
  466. if( strcasestr( pDR->d_name, "ttyAMA" ) == pDR->d_name ){
  467. createResourceUART( pDR->d_name );
  468. continue;
  469. };
  470. if( strcasestr( pDR->d_name, "ttyUSB" ) == pDR->d_name ){
  471. createResourceUART( pDR->d_name );
  472. continue;
  473. };
  474. };
  475. closedir( dFD );
  476. };
  477. //m_bHardwareEnumerated = true;
  478. };
  479. uint32_t argusHardwareManager::createResourceUART( CSTR devName ){
  480. uint32_t RetVal = 0;
  481. char tBuffer[256];
  482. /* Enumerate BuiltIn Serial */
  483. #ifdef DEBUG_EXTRA
  484. sprintf( tBuffer, "Serial Device: %s", devName );
  485. LOG( tBuffer );
  486. #endif
  487. char dev_name[CTEXT];
  488. int fd = -1;
  489. struct serial_struct ss;
  490. sprintf( dev_name, "/dev/%s", devName );
  491. fd = open( dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK );
  492. if (-1 == fd) {
  493. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  494. LOG_ERROR( tBuffer );
  495. } else {
  496. if (-1 == xioctl(fd, TIOCGSERIAL, &ss)){
  497. LOG_ERROR("TIOCGSERIAL");
  498. } else {
  499. #ifdef DEBUG_EXTRA
  500. sprintf( tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]",
  501. ss.port,
  502. ss.irq,
  503. ss.baud_base,
  504. ss.line,
  505. ss.type,
  506. ss.flags
  507. );
  508. LOG( tBuffer );
  509. #endif
  510. char comName[16];
  511. int pn = atoi( devName+4 ) + 1;
  512. sprintf( comName,"COM%d", pn );
  513. //uint32_t dbid = m_pDBC->addNodeSocket( m_nServerNodeID, m_nServerID, ARGUS_NODE_SOCK_TYPE_SERIAL_PORT, ARGUS_NODE_SOCK_MODE_INOUT, true, dev_name, comName );
  514. //if( dbid == 0 ) {
  515. // Socket Allredy in DB - was just updated
  516. // dbid = m_pDBC->getNodeSocketID( m_nServerNodeID, m_nServerID, ARGUS_NODE_SOCK_TYPE_SERIAL_PORT, dev_name );
  517. //};
  518. RetVal = m_pDBC->addResource( m_nServerNodeID, ARGUS_HARDWARE_UART, dev_name );
  519. if( RetVal ) {
  520. m_pDBC->updateResourceAUX( RetVal, m_pDBC->addLocalUART( RetVal, 1, 115200, 8, 'N', 1.0f, dev_name ) );
  521. };
  522. };
  523. close(fd);
  524. };
  525. return RetVal;
  526. };
  527. uint32_t argusHardwareManager::createResourceVIDEO( CSTR devName ){
  528. uint32_t RetVal = 0;
  529. char tBuffer[256];
  530. /* Enumerate Video Capture */
  531. #ifdef DEBUG_EXTRA
  532. sprintf( tBuffer, "Video Source: %s", devName );
  533. LOG( tBuffer );
  534. #endif
  535. struct v4l2_capability cap;
  536. //struct v4l2_cropcap cropcap;
  537. //struct v4l2_crop crop;
  538. struct v4l2_format fmt;
  539. char dev_name[CTEXT];
  540. int fd = -1;
  541. struct stat st;
  542. sprintf( dev_name, "/dev/%s", devName );
  543. if (-1 == stat( dev_name, &st )) {
  544. sprintf( tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  545. LOG_ERROR( tBuffer );
  546. return 0;
  547. };
  548. if (!S_ISCHR(st.st_mode)) {
  549. sprintf( tBuffer, "%s is no devicen", dev_name);
  550. LOG_ERROR( tBuffer );
  551. return 0;
  552. };
  553. fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
  554. if (-1 == fd) {
  555. sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
  556. LOG_ERROR( tBuffer );
  557. } else {
  558. if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
  559. if (EINVAL == errno) {
  560. sprintf( tBuffer, "%s is no V4L2 device", dev_name );
  561. LOG_ERROR( tBuffer );
  562. } else {
  563. LOG_ERROR( "VIDIOC_QUERYCAP" );
  564. };
  565. close(fd);
  566. return 0;
  567. };
  568. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  569. #ifdef DEBUG_EXTRA
  570. sprintf(tBuffer, "%s is no video capture device", dev_name);
  571. LOG_ERROR( tBuffer );
  572. #endif
  573. close(fd);
  574. return 0;
  575. };
  576. CLEAR(fmt);
  577. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  578. /* Preserve original settings as set by v4l2-ctl for example */
  579. if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt)){
  580. //LOG_ERROR("VIDIOC_G_FMT");
  581. } else {
  582. #ifdef DEBUG_EXTRA
  583. sprintf( tBuffer, "Grabber[%dx%d]",fmt.fmt.pix.width,fmt.fmt.pix.height);
  584. LOG( tBuffer );
  585. #endif
  586. uint64_t mid = m_pDBC->addLocalMediaSource( m_nSiteID, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height );
  587. //m_pDBC->addNodeSocket( m_nServerNodeID, mid, ARGUS_NODE_SOCK_TYPE_ANALOG_VIDEO_GRABBER, ARGUS_NODE_SOCK_MODE_OUT, false, dev_name, tBuffer );
  588. RetVal = m_pDBC->addResource( m_nServerNodeID, ARGUS_HARDWARE_ANALOG_VIDEO_IN, dev_name, mid );
  589. };
  590. close(fd);
  591. };
  592. return RetVal;
  593. };
  594. uint32_t argusHardwareManager::createResourceUDCB( CSTR devName ){
  595. uint32_t RetVal = 0;
  596. char tBuffer[256];
  597. return RetVal;
  598. };
  599. argusHardwareResource *argusHardwareManager::requestResource( ARGUS_HARDWARE_PROVIDERS type, uint32_t id ){
  600. argusHardwareResource *RetVal = NULL;
  601. if( m_vResources.size() ) {
  602. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  603. if( ((*idx)->getProviderType() == type ) && ( (*idx)->getDbId() == id ) ) {
  604. RetVal = (*idx);
  605. break;
  606. };
  607. };
  608. };
  609. return RetVal;
  610. };
  611. DBC_INT_LIST *argusHardwareManager::requestResourcesByType( ARGUS_HARDWARE_PROVIDERS type ){
  612. DBC_INT_LIST *RetVal = NULL;
  613. #ifdef DEBUG_EXTRA
  614. char tBuffer[128];
  615. sprintf( tBuffer, "Requested Resource type: %d", type );
  616. LOG( tBuffer );
  617. #endif
  618. if( m_vResources.size() ) {
  619. #ifdef DEBUG_EXTRA
  620. LOG("Have Some Resources!");
  621. #endif
  622. RetVal = new DBC_INT_LIST();
  623. if( RetVal ) {
  624. #ifdef DEBUG_EXTRA
  625. LOG("RetVal Created!");
  626. #endif
  627. for( VECTOR_HW_RESOURCES::iterator idx = m_vResources.begin(); idx < m_vResources.end(); idx++ ) {
  628. #ifdef DEBUG_EXTRA
  629. sprintf( tBuffer, "Have Resource type: %d", (*idx)->getHardwareType() );
  630. LOG( tBuffer );
  631. #endif
  632. if( (*idx)->getProviderType() == type ) {
  633. #ifdef DEBUG_EXTRA
  634. LOG("Matched!");
  635. #endif
  636. RetVal->push_back((*idx)->getDbId());
  637. };
  638. };
  639. } else {
  640. LOG_ERROR("Cannot Create DBC_INT_LIST");
  641. };
  642. };
  643. return RetVal;
  644. };
  645. #undef CLEAR
  646. #undef LOG
  647. #undef LOG_ERROR