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.
 
 
 
 
 
 

327 lines
10 KiB

#include "kernelModuleModemManager.h"
#include "../../components/base/basicFunctionality.h"
#define LOG( X ) kernelModule::m_pLog->Log( "MOD_MODEM", 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 );\
kernelModule::m_pLog->Log( "MOD_MODEM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
}
#else
#define LOG_ERROR( X ) kernelModule::m_pLog->Log( "MOD_MODEM", this, _LOG_ERROR, X )
#endif
kernelModuleModemManager::kernelModuleModemManager( uint32_t moduleID ): kernelModule( moduleID, KERNEL_MODULE_MODEM_MANAGER ) /*, basicThread() */{
m_cMyWanIP[0]= (char)0;
if( checkCmdExists("mmcli") > 0 ){
LOG("ModemManager Present!");
} else {
LOG_ERROR("No ModemManager installed in the System!");
};
};
kernelModuleModemManager::~kernelModuleModemManager(){
//if ( m_vUserData.size() ) {
//for ( VECTOR_MDM_USER_DATA::iterator idx = m_vUserData.begin(); idx < m_vUserData.end(); idx++ ) {
// if ( !(*idx)->finished ) pthread_join( (*idx)->handler, NULL);
//};
//};
};
bool kernelModuleModemManager::reloadSettings(){
LOG("Reload");
//Reload Phonebook
return true;
};
bool kernelModuleModemManager::startService(){
LOG("Start Service");
//Load PhoneBook
setWM( MDM_IDLE_WM );
if ( m_sMMData.sms_enabled ) {
// Get current WAN IP Address
if( updateWanIP() ) {
LOG( "WAN IP OK!" );
} else LOG( "WAN IP NOK!" );
} else LOG( "SMS DISABLED!" );
return moduleActive = init();
};
bool kernelModuleModemManager::stopService( bool force ){
LOG("Stop Service");
setWM( MDM_STOP_WM );
return moduleActive = !uninit();
};
void kernelModuleModemManager::m_fThreadProc( void ){
uint32_t nCurrentPhoneBookId = 0;
uint32_t nCurrentCallId = 0;
uint64_t lNextWindowAction = 0;
m_bThreadStatus == TH_WORKING;
while ( m_bThreadStatus < TH_ENDED ) {
uint8_t bCWM = getWM();
switch ( bCWM ) {
case MDM_IDLE_WM:{
break;
};
case MDM_INIT_CALL_WM:{
if ( nCurrentPhoneBookId < m_vPhoneBook.size() ) {
DBC_MODEM_PHONEBOOK_RECORD *tRECORD = m_vPhoneBook.at( nCurrentPhoneBookId );
if ( tRECORD ) {
if( tRECORD->mode == 2 ){
LOG("Start Call");
nCurrentCallId = startCall( tRECORD->number );
lNextWindowAction = getMilliseconds() + m_sMMData.call_hangup_wait * 1000;
// Need to wait till calling
setWM( MDM_MODEM_IS_BUSY_WM );
} else {
LOG("Start SMS");
nCurrentCallId = 0;
sendSMS( tRECORD->number, tRECORD->custom_msg );
//No need to wait SMS
setWM( MDM_FETCH_NEXT_WM );
};
} else {
LOG_ERROR("No Phone Book Record!");
sessionStarted = false;
setWM( MDM_IDLE_WM );
};
} else {
LOG_ERROR("Phone Book is Empty!");
sessionStarted = false;
setWM( MDM_IDLE_WM );
};
break;
};
case MDM_MODEM_IS_BUSY_WM:{
if ( lNextWindowAction < getMilliseconds() ){
LOG("Hangup Call");
hangupCall( nCurrentCallId );
setWM( MDM_FETCH_NEXT_WM );
};
break;
};
case MDM_FETCH_NEXT_WM:{
if ( nCurrentPhoneBookId < m_vPhoneBook.size() ) {
nCurrentPhoneBookId++;
setWM( MDM_INIT_CALL_WM );
} else {
nCurrentPhoneBookId = 0;
lNextWindowAction = getMilliseconds() + m_sMMData.time_window;
setWM( MDM_WAIT_WINDOW_WM );
};
break;
};
case MDM_WAIT_WINDOW_WM:{
if ( lNextWindowAction < getMilliseconds() ){
sessionStarted = false;
setWM( MDM_IDLE_WM );
};
break;
};
case MDM_STOP_WM:{
m_bThreadStatus = TH_ENDED;
break;
}
};
SLEEP(DEFAULT_THREAD_SLEEP_TIME);
};
};
bool kernelModuleModemManager::dispatchEvent( EVENT_CONTAINER *Evt ){
if ( moduleActive ) {
if( Evt == NULL ) return false;
if( Evt->m_eClass == EVT_CLASS_SYSTEM ){
if( Evt->m_eType == EVT_LRF_START_SESSION ){
//sessionStarted = true;
//setWM( MDM_INIT_CALL_WM );
};
if ( Evt->m_eType == EVT_LRF_END_SESSION_TO || Evt->m_eType == EVT_LRF_END_SESSION_MA ) {
};
};
switch( Evt->m_eType ) {
case EVT_TARGET_1D:
case EVT_TARGET_3D_WHITE:
case EVT_TARGET_3D_BLACK:
case EVT_TARGET_2D_WHITE:
case EVT_TARGET_2D_BLACK:{
sessionStarted = true;
setWM( MDM_INIT_CALL_WM );
break;
};
case EVT_LRF_END_SESSION_TO:
case EVT_LRF_END_SESSION_MA:{
sessionStarted = false;
break;
};
};
};
return true;
};
bool kernelModuleModemManager::validate(){
//LOG("Validate");
if ( moduleActive ) {
if ( m_sMMData.sms_enabled ) {
// Get current WAN IP Address
return updateWanIP();
};
};
return true;
};
#define TLOG_ERROR( X ) DATA->parent->m_pLog->Log( "MOD_MODEM_T", DATA->parent, _LOG_ERROR, X )
void *kernelModuleModemManager::callProc( void *ptr ){
MDM_CALL_DATA *DATA = (MDM_CALL_DATA *)ptr;
if ( DATA ) {
char CMD[128];
uint32_t callID = 0;
sprintf( CMD, "mmcli -m %d --voice-create-call='number=%s'", DATA->modem_id, DATA->target );
/*
Successfully created new call: /org/freedesktop/ModemManager1/Call/2
*/
FILE *fd = popen( CMD, "r");
if ( fd ) {
char tB[1024];
if ( fread( tB, 1, 1023, fd ) > 0 ) {
CSTR tS = strstr( tB, "Call/" );
if ( tS ) {
callID = atoi( tS+5 );
};
};
pclose( fd );
};
if ( callID ) {
// Wait X seconds then hang call
SLEEP( DATA->parent->m_sMMData.call_hangup_wait * 1000 );
sprintf( CMD, "mmcli -m %d --close-call %d", DATA->modem_id, callID );
fd = popen( CMD, "r");
if ( fd ) {
pclose( fd );
} else {
TLOG_ERROR("Cannot Execute Call Hang Command!");
};
} else TLOG_ERROR("Call is not registered at Modem!");
};
pthread_exit( NULL );
return NULL;
};
void *kernelModuleModemManager::smsProc( void *ptr ){
MDM_SMS_DATA *DATA = (MDM_SMS_DATA *)ptr;
if ( DATA ) {
char CMD[128];
uint32_t smsID = 0;
sprintf( CMD, "mmcli -m %d --messaging-create-sms=\"text='%s',number='%s'\"", DATA->modem_id, DATA->text, DATA->target );
FILE *fd = popen( CMD, "r");
if ( fd ) {
char tB[1024];
if ( fread( tB, 1, 1023, fd ) > 0 ) {
CSTR tS = strstr( tB, "SMS/" );
if ( tS ) {
smsID = atoi( tS+4 );
};
};
pclose( fd );
};
if ( smsID ) {
// Wait X seconds then hang call
//SLEEP( DATA->parent->m_sMMData.call_hangup_wait * 1000 );
sprintf( CMD, "mmcli -s %d --send", smsID );
fd = popen( CMD, "r");
if ( fd ) {
pclose( fd );
} else {
TLOG_ERROR("Cannot Execute Call Hang Command!");
};
} else TLOG_ERROR("Call is not registered at Modem!");
};
pthread_exit( NULL );
return NULL;
};
uint32_t kernelModuleModemManager::startCall( CSTR number ){
MDM_CALL_DATA *DATA = ( MDM_CALL_DATA *) malloc( sizeof( MDM_CALL_DATA ));
if( DATA ) {
memset( DATA, 0 , sizeof( MDM_CALL_DATA ));
DATA->parent = this;
//strncpy( DATA->target, m_sMMData.call_target, 64 );
int RetVal = pthread_create( &DATA->handler, NULL, callProc, ( void * ) DATA );
if( RetVal == -1 ) {
LOG_ERROR("Cannot start Call Procedure.");
free( DATA );
return 0;
};
//Push Data into vector
m_vUserData.push_back( (MDM_USER_DATA*) DATA );
} else {
LOG_ERROR("Cannot allocate Call data.");
};
return 0;
};
void kernelModuleModemManager::sendSMS( CSTR number, CSTR text ){
};
void kernelModuleModemManager::hangupCall( uint32_t call_id ){
};
bool kernelModuleModemManager::updateWanIP(){
char CMD[128];
uint32_t smsID = 0;
bool bDone = false;
sprintf( CMD, "ifconfig" );
FILE *fd = popen( CMD, "r");
if ( fd ) {
LOG( "CMD OK" );
CSTR tB = NULL;
size_t bSize = 0;
bool bIfMeet = false;
while ( !feof( fd ) ) {
ssize_t rv = getline( &tB, &bSize, fd );
if( rv ) {
if( !bDone ) {
if (bIfMeet) {
CSTR ssp = strstr( tB, "inet ");
if ( ssp ){
LOG( "IF IP OK" );
CSTR nsp = strstr( tB, "netmask");
int msl = nsp - ssp - 6;
strncpy( m_cMyWanIP, ssp+5, msl );
m_cMyWanIP[msl]=(char)0;
bDone = true;
LOG( "WWAN IP" );
LOG( m_cMyWanIP );
};
} else {
LOG( "IF MEET OK" );
if ( strstr( tB, "wwan")) bIfMeet = true;
};
};
} else LOG( "READ NOK" );
if ( tB ) {
free( tB );
tB = NULL;
};
};
pclose( fd );
};
return bDone;
};
#undef LOG
#undef LOG_ERROR