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

//----------------------------------------------------------------------------
// Dr.Frucht Systems Ltd.
// Copyright © 2000-2017. All Rights Reserved.
// Version 2.0 7/20/2017
// Last modification: 7/20/2017
// PROJECT:
// SUBSYSTEM:
// FILE: mojoHardwareManager.cpp
// AUTHOR: gagua
// OVERVIEW
// ~~~~~~~~
// Header file of mojoHardwareManager.cpp
//----------------------------------------------------------------------------
//
#include "mojoHardwareManager.h"
#include "../hardware/hid/hid_dc.h"
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <hidapi/hidapi.h>
#include <ifaddrs.h>
#include <linux/serial.h>
#include <linux/videodev2.h>
#define LOG( X ) m_pLog->Log( "MHWM", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG2w34[256];\
sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
m_pLog->Log( "MHWM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
}
#else
#define LOG_ERROR( X ) m_pLog->Log( "MHWM", this, _LOG_ERROR, X );
#endif
mojoHardwareManager *mojoHardwareManager::_instance = NULL;
/**
* Returns the single instance of the object.
*/
mojoHardwareManager *mojoHardwareManager::getInstance( config_setting_t * Settings ) {
// check if the instance has been created yet
if( 0 == _instance ){
// if not, then create it
_instance = new mojoHardwareManager( Settings );
};
// return the single instance
return _instance;
};
// Constructor implementation
mojoHardwareManager::mojoHardwareManager( config_setting_t *SNode ){
m_pLog = basicLogCollection::getInstance();
m_pSettingsProvider = basicSettingsProvider::getInstance();
m_nMemoryLimitHeapSize = 0;
if( m_pSettingsProvider ) {
m_pSettings = m_pSettingsProvider->getSubNode( SNode, "HARDWARE_MANAGER" );
if( m_pSettingsProvider->isCompromised() ) {
m_pSettingsProvider->validate();
};
} else {
m_pSettings = NULL;
};
m_pDBC = mojoDBC::getInstance();
char tB[128];
struct rlimit rl={0};
getrlimit( RLIMIT_STACK, &rl );
sprintf( tB, "Stack Size: %d", rl.rlim_cur ); //RLIM_INFINITY
LOG( tB );
sprintf( tB, "MAX Stack Size: %d", rl.rlim_max );
LOG( tB );
m_bInitOK = false;
};
mojoHardwareManager::~mojoHardwareManager(){
_instance = NULL;
};
bool mojoHardwareManager::validate(){
bool RetVal = true;
//Mem Validator
if( m_nMemoryLimitHeapSize ) {
size_t memStat = getCurrentRSS();
#ifdef DEBUG_EXTRA
char MEM_MSG[128];
sprintf( MEM_MSG, "MEM: %d TARG: %d", memStat, m_nMemoryLimitHeapSize );
LOG( MEM_MSG );
#endif
RetVal = ( memStat < m_nMemoryLimitHeapSize );
if ( !RetVal ) {
LOG_ERROR("Memory Consumption too BIG!");
};
};
return RetVal;
};
int xioctl(int fh, int request, void *arg){
int r;
do {
r = ioctl( fh, request, arg );
} while (-1 == r && EINTR == errno);
return r;
};
#define CLEAR(x) memset(&(x), 0, sizeof(x))
void mojoHardwareManager::getMAC( CSTR if_name, CSTR buf ){
char tB[128];
FILE * fd=NULL;
if ( buf ) {
sprintf(tB, "/sys/class/net/%s/address", if_name);
#ifdef DEBUG
LOG( tB );
#endif
fd=fopen(tB,"r");
if ( fd ) {
fread( buf, 1, INET6_ADDRSTRLEN, fd );
fclose( fd );
buf = trim( buf );
} else {
memset( buf, 0, INET6_ADDRSTRLEN );
};
#ifdef DEBUG
LOG( buf );
#endif
};
};
void mojoHardwareManager::getGW( CSTR if_name, CSTR buf ){
FILE * fd = popen( "route -n", "r" );
char tB[1024];
//char expBuf[1024];
memset( buf, 0, INET6_ADDRSTRLEN );
if ( fd ) {
while (!feof(fd)) {
fgets((CSTR)tB, 1024, fd);
trim((CSTR)tB);
if( strlen(tB) ) {
if( strstr( tB, if_name ) != NULL ){
//strncpy( expBuf, tB, 1024 );
CArray *CA = explode( ' ', tB );
if ( CA ) {
//for (int i=0; i< CA->size(); i++ ) {
// char tB[128];
// trim( CA->at(i) );
// sprintf( tB, "GW# %d: %s", i, CA->at(i) );
// LOG( tB );
//};
trim( CA->at(1) );
if( strstr(CA->at(1), "0.0.0.0") == NULL ) {
strncpy( buf, CA->at(1), INET6_ADDRSTRLEN );
};
delete(CA);
};
};
};
};
fclose( fd );
};
if( strlen( buf ) == 0 ) strncpy( buf, "0.0.0.0", INET6_ADDRSTRLEN );
};
void mojoHardwareManager::getTTyDesc( CSTR dev_name, CSTR buf ){
FILE * fd = popen( "dmesg | grep tty", "r" );
char tB[1024];
//char expBuf[1024];
memset( buf, 0, DBC_RESOURCE_DESC_SIZE );
if ( fd ) {
while (!feof(fd)) {
fgets((CSTR)tB, 1024, fd);
trim((CSTR)tB);
if( strlen(tB) ) {
if( strstr( tB, dev_name ) != NULL ){
//strncpy( expBuf, tB, 1024 );
CArray *CA = explode( ':', tB );
if ( CA ) {
//for (int i=0; i< CA->size(); i++ ) {
// char tB[128];
// trim( CA->at(i) );
// sprintf( tB, "GW# %d: %s", i, CA->at(i) );
// LOG( tB );
//};
trim( CA->at(1) );
strncpy( buf, CA->at(1), DBC_RESOURCE_DESC_SIZE );
buf[DBC_RESOURCE_DESC_SIZE-1]=(char)0;
delete(CA);
};
};
};
};
fclose( fd );
};
if( strlen( buf ) == 0 ){
strncpy( buf, "--Unknown--", DBC_RESOURCE_DESC_SIZE );
buf[DBC_RESOURCE_DESC_SIZE-1]=(char)0;
};
};
PARSE_RESULT_NET_INTERFACES *mojoHardwareManager::parse_neconfig( CSTR if_name ){
PARSE_RESULT_NET_INTERFACES *tVal = NULL;
FILE *fd;
bool cConfig = false;
fd = fopen("/etc/network/interfaces","r");
if ( fd ) {
char tB[1024];
char expBuf[1024];
tVal = (PARSE_RESULT_NET_INTERFACES *) malloc( sizeof(PARSE_RESULT_NET_INTERFACES));
if ( tVal ) {
memset(tVal, 0 , sizeof(PARSE_RESULT_NET_INTERFACES));
while (!feof(fd)) {
fgets((CSTR)tB, 1024, fd);
trim((CSTR)tB);
if( strlen(tB) ) {
strncpy( expBuf, tB, 1024 );
CArray *CA = explode( ' ', expBuf );
if( CA ) {
for( CArray::iterator idx = CA->begin(); idx < CA->end(); idx++ ) {
if((*idx)[0]=='#') break;
if(strstr((*idx), "auto")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->enabled = true;
if(strstr((*idx), "allow-hotplug")!= NULL ) if( strstr( tB, if_name )!= NULL ) tVal->hot_plug = true;
if(strstr((*idx), "iface")!= NULL ) if( strstr( tB, if_name )!= NULL ){
if (strstr( tB, "static") != NULL) tVal->if_static = true;
cConfig = true;
} else cConfig = false;
if(strstr((*idx), "address")!= NULL ) if( cConfig ){
idx++;
if(strstr((*idx),"/")!= NULL ){
int8_t sh = strstr((*idx),"/") - (*idx);
strncpy( tVal->address, (*idx), sh );
tVal->address[sh]=(char)0;
int8_t bm = atoi( (*idx) + sh + 1 );
union{
uint32_t mm;
uint8_t nm[4];
} nmu = {0};
for ( sh = 0; sh < bm; sh++ ) nmu.mm |= (1 << sh);
sprintf(tVal->netmask, "%d.%d.%d.%d", nmu.nm[0], nmu.nm[1], nmu.nm[2], nmu.nm[3]);
} else {
strncpy( tVal->address, (*idx), INET6_ADDRSTRLEN );
};
};
if(strstr((*idx), "netmask")!= NULL ) if( cConfig ){
LOG("netMSK");
idx++;
LOG(*idx);
strncpy(tVal->netmask, (*idx), INET6_ADDRSTRLEN);
};
//if(strstr((*idx), "bridge_ports")!= NULL ) cMode = 6;
//if(strstr((*idx), "bridge_stp")!= NULL ) cMode = 7;
//if(strstr((*idx), "bridge_waitport")!= NULL ) cMode = 8;
//if(strstr((*idx), "bridge_fd")!= NULL ) cMode = 9;
};
delete( CA );
};
};
};
};
fclose( fd );
};
#ifdef DEBUG
if ( tVal ) {
if( tVal->enabled ) LOG("NIF Enabled"); else LOG("NIF Disabled");
if (tVal->hot_plug) LOG("NIF Hot Plugable"); else LOG("NIF not Hot Plugable");
if (tVal->if_static) LOG("Address is static"); else LOG("Address is Dynamic");
if(strlen(tVal->address))LOG(tVal->address);
if(strlen(tVal->netmask))LOG(tVal->netmask);
};
#endif
return tVal;
};
PARSE_RESULT_WIFI_SETTINGS *mojoHardwareManager::parse_wifi( CSTR if_name ){
PARSE_RESULT_WIFI_SETTINGS *tVal = NULL;
FILE *fd;
bool cConfig = false;
tVal = (PARSE_RESULT_WIFI_SETTINGS *) malloc( sizeof(PARSE_RESULT_WIFI_SETTINGS));
if ( tVal ) {
memset(tVal, 0 , sizeof(PARSE_RESULT_WIFI_SETTINGS));
fd = fopen("/etc/dnsmasq.conf","r");
if ( fd ) {
char tB[1024];
char expBuf[1024];
while (!feof(fd)) {
fgets((CSTR)tB, 1024, fd);
trim((CSTR)tB);
if( strlen(tB) ) {
strncpy( expBuf, tB, 1024 );
CArray *CA = explode( '=', expBuf );
if( CA ) {
if( CA->size() ){
if( CA->at(0)[0] == '#' ) continue;
if( strstr(CA->at(0), "interface")!= NULL ) {
if( strstr( tB, if_name)!= NULL ) {
cConfig = true;
tVal->dhcp_enabled = true;
};
};
if( cConfig ){
if ( strstr(CA->at(0), "dhcp-range")!= NULL ) {
char tT[1024];
strncpy( tT, CA->at(1), 1024 );
CArray *CB = explode(',', tT );
if( CB ){
if( CB->size() > 3 ){
strncpy( tVal->range_start, CB->at(0), INET6_ADDRSTRLEN);
strncpy( tVal->range_stop, CB->at(1), INET6_ADDRSTRLEN);
strncpy( tVal->netmask, CB->at(2), INET6_ADDRSTRLEN);
strncpy( tVal->lease_period, CB->at(3), INET6_ADDRSTRLEN);
};
delete( CB );
};
};
};
};
delete( CA );
};
};
};
fclose( fd );
};
//Check WiFi
if( strstr(if_name,"w")!= NULL ) {
char tName[128];
sprintf( tName, "iwconfig %s", if_name );
fd = popen( tName, "r" );
if ( fd ) {
char tB[1024];
while (!feof(fd)) {
fgets((CSTR)tB, 1024, fd );
trim( tB );
if (strstr(tB, "Mode") != NULL) if (strstr(tB, "Master") != NULL) tVal->access_point = true;
};
fclose( fd );
};
};
if ( tVal->access_point ) {
fd = fopen("/etc/hostapd/hostapd.conf", "r");
if ( fd ) {
char tB[1024];
char expBuf[1024];
while (!feof(fd)) {
fgets((CSTR)tB, 1024, fd);
trim( tB );
if( strlen(tB) ) {
if( tB[0]=='#' ) continue;
strncpy(expBuf, tB, 1024);
CArray *CA = explode( '=', expBuf );
if( CA ) {
if( CA->size() ){
if (strstr(CA->at(0), "ssid") != NULL) if (strstr(CA->at(0), "ignore_broadcast_ssid") == NULL){
strncpy( tVal->SSID, CA->at(1), 64 );
} else tVal->hiden_ssid = (bool) atoi(CA->at(1));
if (strstr(CA->at(0), "wpa_passphrase") != NULL) strncpy( tVal->password, CA->at(1), 64 );
if (strstr(CA->at(0), "country_code") != NULL) strncpy(tVal->country_code, CA->at(1), 3);
if (strstr(CA->at(0), "channel") != NULL) tVal->channel = atoi(CA->at(1));
};
delete( CA );
};
};
};
fclose( fd );
};
} else {
fd = fopen("/etc/wpa_supplicant/wpa_supplicant.conf", "r");
if ( fd ) {
fclose( fd );
};
};
fd = fopen("/etc/rc.local","r");
if ( fd ) {
char tB[1024];
char expBuf[1024];
while (!feof(fd)) {
fgets((CSTR)tB, 1024, fd);
trim((CSTR)tB);
if( strlen(tB) ) {
strncpy( expBuf, tB, 1024 );
CArray *CA = explode( ' ', expBuf );
if( CA ) {
if ( CA->size() ) {
if (CA->at(0)[0] == '#') continue;
if( strstr(CA->at(0), "iptables-restore")!= NULL ) {
char zB[1024];
FILE *afd = fopen( CA->at(2), "r" );
if ( afd ) {
while( !feof( afd ) ){
fgets((CSTR)zB, 1024, afd );
if( strstr( zB, "POSTROUTING" )!= NULL ) if( strstr( zB, "MASQUERADE" )!= NULL ) if( strstr( zB, if_name )== NULL ){
CArray *CB = explode( ' ', zB );
if ( CB ) {
if ( CB->size() > 3 ) {
strncpy(tVal->router_outbond, CB->at(3), INET6_ADDRSTRLEN);
};
delete( CB );
};
tVal->router_enabled = true;
};
};
fclose(afd);
} else LOG("Error Opening Firewall Rules!");
};
};
delete( CA );
};
};
};
fclose(fd);
} else LOG_ERROR("Can not open rc.local!");
};
#ifdef DEBUG
if ( tVal ) {
if (tVal->access_point) LOG("AP Mode"); else LOG("WiFi Mode");
LOG(tVal->SSID);
if (tVal->hiden_ssid) LOG("SSID is hidden");
LOG(tVal->password);
if (tVal->dhcp_enabled){
LOG("DHCP Service");
LOG(tVal->range_start);
LOG(tVal->range_stop);
LOG(tVal->lease_period);
} else LOG("DHCP Disabled");
if (tVal->router_enabled){
LOG("Routing Enabled");
LOG( tVal->router_outbond );
} else LOG("Routing Disabled");
};
#endif
return tVal;
};
void mojoHardwareManager::enumerateNIFS( void ){
struct ifaddrs * ifaddr = NULL;
char tBuffer[128];
//if( m_pKernel == NULL ) return;
if( m_pDBC == NULL ) return;
if( -1 == getifaddrs(&ifaddr)) {
LOG_ERROR("No NET INTERFACES?!");
} else {
#ifdef DEBUG
LOG("Enumerating Network Interfaces");
#endif
struct ifaddrs * ifa;
for( ifa = ifaddr; ifa; ifa = ifa->ifa_next ){
char addr[INET6_ADDRSTRLEN];
char mask[INET6_ADDRSTRLEN];
char gw[INET6_ADDRSTRLEN];
char bc[INET6_ADDRSTRLEN];
char mac[INET6_ADDRSTRLEN];
char device[INET6_ADDRSTRLEN];
uint32_t nif_id = 0;
uint32_t nif_aux_id = 0;
uint32_t router_id = 0;
ARGUS_NIF_TYPE nif_type = NIF_LOOPBACK;
ARGUS_NETWORK_TYPE net_type = NETWORK_IPV4;
ARGUS_RESOURCE_TYPE res_type = ARGUS_HARDWARE_NIF_LOOPBACK;
ARGUS_NIF_MODE nif_mode = ADDRESS_DISABLED;
#ifdef DEBUG_EXTRA
LOG("NIF ***");
#endif
strncpy( device, ifa->ifa_name, INET6_ADDRSTRLEN );
strncpy( mac,"--no-mac--", INET6_ADDRSTRLEN );
// Filter Loopback if
if( strstr( device, "lo" ) != NULL ){
nif_type = NIF_LOOPBACK;
res_type = ARGUS_HARDWARE_NIF_LOOPBACK;
//Not Implemented
continue;
};
// Filter vpn TUN if
if( strstr( device, "tun" ) != NULL ){
nif_type = NIF_VPN_TUNEL;
res_type = ARGUS_HARDWARE_NIF_TUNEL;
//Not Implemented Yet
continue;
};
if( strstr( device, "eth" ) != NULL ){
LOG(device);
nif_type = NIF_ETHERNET;
res_type = ARGUS_HARDWARE_NIF_ETHERNET;
};
if( strstr( device, "wlan" ) != NULL ){
LOG(device);
nif_type = NIF_WIFI;
res_type = ARGUS_HARDWARE_NIF_WIFI;
};
if( strstr( device, "wwan" ) != NULL ){
LOG(device);
nif_type = NIF_WAN;
res_type = ARGUS_HARDWARE_NIF_WAN;
};
if( ifa->ifa_addr != NULL ){
if (ifa->ifa_addr->sa_family != AF_INET && ifa->ifa_addr->sa_family != AF_INET6) {
LOG("CONT");
continue;
}
} else {
LOG("ifa->ifa_addr is NULL");
continue;
};
LOG("GETMAC");
getMAC( device, mac );
getGW( device, gw );
LOG( mac );
LOG( gw );
PARSE_RESULT_NET_INTERFACES * tt = NULL;
PARSE_RESULT_WIFI_SETTINGS * tw = NULL;
tt = parse_neconfig( device );
tw = parse_wifi( device );
if( tt ) {
if ( tt->enabled ) {
if( tt->if_static ){
nif_mode = ADDRESS_STATIC;
} else {
nif_mode = ADDRESS_DYNAMIC;
};
} else {
nif_mode = ADDRESS_DISABLED;
};
} else {
LOG_ERROR("Error Parsing System NIF Settings!");
continue;
};
if ( tw ){
if( nif_mode == ADDRESS_STATIC ) {
if (tw->dhcp_enabled) {
nif_mode = ADDRESS_STATIC_RAP;
};
};
if ( nif_type == NIF_WIFI ){
nif_id = m_pDBC->addLocalNIF( device, mac, nif_type, nif_mode );
if( nif_id == 0 ) continue;
ARGUS_WIFI_MODE w_mode;
if( !tt->enabled ) {
w_mode = WIFI_DISABLED;
} else {
if (tt->if_static) {
if( tw->access_point ) {
w_mode = WIFI_IS_AP;
uint32_t outbond_id = 0;
if( tw->router_enabled ){
outbond_id = m_pDBC->getNIF_IDbyDevice( tw->router_outbond );
};
if( tw->dhcp_enabled ) {
router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id, true, tw->range_start, tw->range_stop, tw->netmask, tw->lease_period );
} else {
router_id = m_pDBC->addLocalNetworkInfrastructure(nif_id, outbond_id );
};
} else {
w_mode = WIFI_CONNECTED_TO_AP;
};
};
};
nif_aux_id = m_pDBC->addLocalWiFi(nif_id, w_mode, tw->SSID, tw->password, ENCRIPTION_WPA_PSK, router_id );
};
if( nif_type == NIF_ETHERNET || nif_type == NIF_ETHERNET_VIRTUAL ){
};
free(tt);
free(tw);
} else {
LOG_ERROR("Error Parsing System Router/AP Settings!");
free(tt);
continue;
};
nif_id = m_pDBC->addLocalNIF( device, mac, nif_type, nif_mode, nif_aux_id );
if( nif_id == 0 ) continue;
LOG("NIF added!");
m_pDBC->addResource( 1, res_type, device, nif_id );
if(ifa->ifa_addr->sa_family == AF_INET){
net_type = NETWORK_IPV4;
// create IPv4 string
struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
sprintf( mask, "%d.%d.%d.%d",
(uint8_t)ifa->ifa_netmask->sa_data[2],
(uint8_t)ifa->ifa_netmask->sa_data[3],
(uint8_t)ifa->ifa_netmask->sa_data[4],
(uint8_t)ifa->ifa_netmask->sa_data[5]
);
sprintf( bc, "%d.%d.%d.%d",
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5]
);
} else { // AF_INET6
net_type = NETWORK_IPV6;
// create IPv6 string
struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
sprintf( mask, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
(uint8_t)ifa->ifa_netmask->sa_data[2],
(uint8_t)ifa->ifa_netmask->sa_data[3],
(uint8_t)ifa->ifa_netmask->sa_data[4],
(uint8_t)ifa->ifa_netmask->sa_data[5],
(uint8_t)ifa->ifa_netmask->sa_data[6],
(uint8_t)ifa->ifa_netmask->sa_data[7],
(uint8_t)ifa->ifa_netmask->sa_data[8],
(uint8_t)ifa->ifa_netmask->sa_data[9],
(uint8_t)ifa->ifa_netmask->sa_data[10],
(uint8_t)ifa->ifa_netmask->sa_data[11],
(uint8_t)ifa->ifa_netmask->sa_data[12],
(uint8_t)ifa->ifa_netmask->sa_data[13],
(uint8_t)ifa->ifa_netmask->sa_data[14],
(uint8_t)ifa->ifa_netmask->sa_data[15],
(uint8_t)ifa->ifa_netmask->sa_data[16],
(uint8_t)ifa->ifa_netmask->sa_data[17]
);
if( ifa->ifa_ifu.ifu_broadaddr != NULL ){
sprintf( bc, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[6],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[7],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[8],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[9],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[10],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[11],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[12],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[13],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[14],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[15],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[16],
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[17]
);
} else {
sprintf( bc, "" );
};
};
m_pDBC->addLocalNetwork( nif_id, net_type, addr, mask, gw, bc );
#ifdef DEBUG
sprintf( tBuffer, "Detected Network Interface: %s [%s]", ifa->ifa_name, addr );
LOG(tBuffer);
#endif
};
freeifaddrs( ifaddr );
};
};
void mojoHardwareManager::enumerateHardware( void ){
char tBuffer[CTEXT];
DIR * dFD = NULL;
struct dirent * pDR = NULL;
//ToDo: Check Ability to work without Kernel and DBC
//if( m_pKernel == NULL ) return;
if( m_pDBC == NULL ) return;
#ifdef DEBUG
LOG("HW Enumeration Started!");
#endif
// Moved to a constructor!!
//m_nServerNodeID = m_pKernel->getServerNodeId();
//m_nServerID = m_pKernel->getServerId();
//m_nSiteID = m_pKernel->getSiteId();
/* Add Network Interfaces */
m_pDBC->cleanResources( 1, true );
enumerateNIFS();
/* Check HID Devices */
struct hid_device_info * pDevs;
uint8_t DCBCount;
//Enumerate USB devices with the given IDs
pDevs = hid_enumerate( 0x16c0, 0x05df );
if( pDevs!=NULL ){
struct hid_device_info * pCDev = pDevs;
//Count the number of returned devices
for( DCBCount = 0; pCDev!=NULL; DCBCount++ ) pCDev = pCDev->next;
//Allocate a buffer for the relays
if( DCBCount > 0 ){
DC_BOARD tDCB;
//Fill the relay structs
pCDev = pDevs;
for( int idx = 0; idx < DCBCount; idx++ ){
//Save the path to this device
memset( tDCB.path, 0, MAX_PATH_LEN );
strncpy( tDCB.path, pCDev->path, MAX_PATH_LEN );
// The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
tDCB.relay_count = atoi((const char *)&pCDev->product_string[8]);
//Open it to get more details
hid_device * handle;
handle = hid_open_path( tDCB.path );
if( handle ){
unsigned char buf[9];
//Get the features of the device
buf[0] = 0x01;
if( hid_get_feature_report( handle, buf, sizeof(buf)) == -1 ){
LOG_ERROR("hid_get_feature_report");
pCDev = pCDev->next;
hid_close( handle );
continue;
};
//Set the serial number (0x0 for termination)
memset( tDCB.serial, 0x0, (HID_DCR_SERIIAL_LENGTH+1) );
memcpy( tDCB.serial, buf, HID_DCR_SERIIAL_LENGTH );
//Byte 7 in the response contains the target_state of the relays
tDCB.state = buf[7];
hid_close( handle );
#ifdef DEBUG
//Output the device enumeration details if verbose is on
LOG("HID DCB Found");
sprintf(tBuffer, " Type: %04hx::%04hx", pCDev->vendor_id, pCDev->product_id);
LOG(tBuffer);
sprintf(tBuffer, " Path: %s", pCDev->path );
LOG(tBuffer);
sprintf(tBuffer, " Serial_number: %ls", pCDev->serial_number );
LOG(tBuffer);
sprintf(tBuffer, " Manufacturer: %ls", pCDev->manufacturer_string);
LOG(tBuffer);
sprintf(tBuffer, " Product: %ls", pCDev->product_string);
LOG(tBuffer);
sprintf(tBuffer, " Release: %hx", pCDev->release_number);
LOG(tBuffer);
sprintf(tBuffer, " Interface: %d", pCDev->interface_number);
LOG(tBuffer);
sprintf(tBuffer, " Number of Relays = %d", tDCB.relay_count);
LOG(tBuffer);
//If debug are on, output individual relay details
for( int k = 0; k < tDCB.relay_count; k++ ){
if( tDCB.state & 1 << k ){
sprintf(tBuffer, " %s_%d=1", tDCB.serial, k + 1);
} else {
sprintf(tBuffer, " %s_%d=0", tDCB.serial, k + 1);
};
LOG(tBuffer);
};
#endif
m_pDBC->addLocalDCB( 1, pCDev->path, tDCB.serial, tDCB.relay_count, tDCB.state );
} else {
LOG_ERROR("unable to open HID device");
};
pCDev = pCDev->next;
};
};
hid_free_enumeration( pDevs );
};
/* Add devices from DEV */
dFD = opendir("/dev");
if( dFD != NULL ) {
while( ( pDR = readdir( dFD ))!= NULL ){
if( strcasestr( pDR->d_name, "video" ) == pDR->d_name ) {
createResourceVIDEO( pDR->d_name );
continue;
};
if( strcasestr( pDR->d_name, "ttyS" ) == pDR->d_name ){
createResourceUART( pDR->d_name );
continue;
};
// RaspberyPI Hardware
if( strcasestr( pDR->d_name, "ttyAMA" ) == pDR->d_name ){
createResourceUART( pDR->d_name );
continue;
};
if( strcasestr( pDR->d_name, "ttyUSB" ) == pDR->d_name ){
createResourceUART( pDR->d_name );
continue;
};
};
closedir( dFD );
};
//m_bHardwareEnumerated = true;
};
uint32_t mojoHardwareManager::createResourceUART( CSTR devName ){
uint32_t RetVal = 0;
char tBuffer[256];
/* Enumerate BuiltIn Serial */
#ifdef DEBUG_EXTRA
sprintf( tBuffer, "Serial Device: %s", devName );
LOG( tBuffer );
#endif
char dev_name[CTEXT];
int fd = -1;
struct serial_struct ss;
sprintf( dev_name, "/dev/%s", devName );
fd = open( dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK );
if (-1 == fd) {
sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
LOG_ERROR( tBuffer );
} else {
if ( 0 && ( -1 == xioctl(fd, TIOCGSERIAL, &ss) )){
LOG_ERROR("TIOCGSERIAL");
} else {
#ifdef DEBUG_EXTRA
sprintf( tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]",
ss.port,
ss.irq,
ss.baud_base,
ss.line,
ss.type,
ss.flags
);
LOG( tBuffer );
#endif
getTTyDesc( devName, tBuffer );
uint32_t tID = m_pDBC->addLocalUART( 1, 1, 19200, 8, 'N', 1.0f, dev_name );
if ( tID ) {
m_pDBC->addResource( 1, ARGUS_HARDWARE_UART, tBuffer, tID );
};
};
close(fd);
};
return RetVal;
};
uint32_t mojoHardwareManager::createResourceVIDEO( CSTR devName ){
uint32_t RetVal = 0;
char tBuffer[256];
/* Enumerate Video Capture */
#ifdef DEBUG_EXTRA
sprintf( tBuffer, "Video Source: %s", devName );
LOG( tBuffer );
#endif
struct v4l2_capability cap;
//struct v4l2_cropcap cropcap;
//struct v4l2_crop crop;
struct v4l2_format fmt;
char dev_name[CTEXT];
int fd = -1;
struct stat st;
sprintf( dev_name, "/dev/%s", devName );
if (-1 == stat( dev_name, &st )) {
sprintf( tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno));
LOG_ERROR( tBuffer );
return 0;
};
if (!S_ISCHR(st.st_mode)) {
sprintf( tBuffer, "%s is no devicen", dev_name);
LOG_ERROR( tBuffer );
return 0;
};
fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
if (-1 == fd) {
sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno));
LOG_ERROR( tBuffer );
} else {
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
if (EINVAL == errno) {
sprintf( tBuffer, "%s is no V4L2 device", dev_name );
LOG_ERROR( tBuffer );
} else {
LOG_ERROR( "VIDIOC_QUERYCAP" );
};
close(fd);
return 0;
};
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
#ifdef DEBUG_EXTRA
sprintf(tBuffer, "%s is no video capture device", dev_name);
LOG_ERROR( tBuffer );
#endif
close(fd);
return 0;
};
CLEAR(fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
/* Preserve original settings as set by v4l2-ctl for example */
if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt)){
//LOG_ERROR("VIDIOC_G_FMT");
} else {
#ifdef DEBUG_EXTRA
sprintf( tBuffer, "Grabber[%dx%d]",fmt.fmt.pix.width,fmt.fmt.pix.height);
LOG( tBuffer );
#endif
uint64_t mid = m_pDBC->addLocalMediaSource( 1, dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height );
//m_pDBC->addNodeSocket( m_nServerNodeID, mid, ARGUS_NODE_SOCK_TYPE_ANALOG_VIDEO_GRABBER, ARGUS_NODE_SOCK_MODE_OUT, false, dev_name, tBuffer );
RetVal = m_pDBC->addResource( 1, ARGUS_HARDWARE_ANALOG_VIDEO_IN, dev_name, mid );
};
close(fd);
};
return RetVal;
};
uint32_t mojoHardwareManager::createResourceUDCB( CSTR devName ){
uint32_t RetVal = 0;
char tBuffer[256];
return RetVal;
};
/*
killWifi(){
ifconfig wlan0 down
ifconfig wlan0 up
iw dev wlan0 station dump
}
*/
#undef LOG
#undef LOG_ERROR