diff --git a/yasnd-gammu.c b/yasnd-gammu.c index 149876e..bd55fa5 100644 --- a/yasnd-gammu.c +++ b/yasnd-gammu.c @@ -18,6 +18,67 @@ bool check_gammu_error(GSM_Error err) return false; } +bool gammu_read_sms() +{ + GSM_MultiSMSMessage sms; + + // Variable for SMS sender number + char sender_number[20]; + memset(&sender_number, 0, sizeof(sender_number)); + + /* Connect to phone */ + /* 1 means number of replies you want to wait for */ + error = GSM_InitConnection(state_machine, 1); + if (!check_gammu_error(error)) + return false; + + /* Prepare for reading message */ + error = ERR_NONE; + sms.Number = 0; + sms.SMS[0].Location = 0; + sms.SMS[0].Folder = 0; + + log_debug("Trying to read SMS...",DEBUG_ALL); + + error = GSM_GetNextSMS(state_machine, &sms, TRUE); + if (error == ERR_EMPTY) return true; + if (!check_gammu_error(error)) + return false; + + // Now we can do something with the message + for (int i = 0; i < sms.Number; i++) + { + strncpy(sender_number,DecodeUnicodeConsole(sms.SMS[i].Number),19); + char dbg_tmp[1000]; + sprintf(dbg_tmp,"Number: '%s'", sender_number); + log_debug(dbg_tmp,DEBUG_ALL); + + // special hack for Motorola L7 + sms.SMS[i].Location-=100000; + + if (sms.SMS[i].Coding == SMS_Coding_8bit) { + log_event("gammu sms error: 8-bit message, can not read"); + // remove SMS + GSM_DeleteSMS(state_machine,&sms.SMS[i]); + return false; + } else { + sprintf(dbg_tmp,"Text: \"%s\"\n", DecodeUnicodeConsole(sms.SMS[i].Text)); + log_debug(dbg_tmp,DEBUG_ALL); + } + + // remove SMS after reading + error = GSM_DeleteSMS(state_machine,&sms.SMS[i]); + if (!check_gammu_error(error)) + return false; + } + + /* Terminate connection */ + error = GSM_TerminateConnection(state_machine); + if (!check_gammu_error(error)) + return false; + return true; +} + bool gammu_init() { GSM_InitLocales(NULL); @@ -115,64 +176,3 @@ bool gammu_send_sms(const char* message) } return true; } - -bool gammu_read_sms() -{ - GSM_MultiSMSMessage sms; - - // Variable for SMS sender number - char sender_number[20]; - memset(&sender_number, 0, sizeof(sender_number)); - - /* Connect to phone */ - /* 1 means number of replies you want to wait for */ - error = GSM_InitConnection(state_machine, 1); - if (!check_gammu_error(error)) - return false; - - /* Prepare for reading message */ - error = ERR_NONE; - sms.Number = 0; - sms.SMS[0].Location = 0; - sms.SMS[0].Folder = 0; - - log_debug("Trying to read SMS...",DEBUG_ALL); - - error = GSM_GetNextSMS(state_machine, &sms, TRUE); - if (error == ERR_EMPTY) return true; - if (!check_gammu_error(error)) - return false; - - // Now we can do something with the message - for (int i = 0; i < sms.Number; i++) - { - strncpy(sender_number,DecodeUnicodeConsole(sms.SMS[i].Number),19); - char dbg_tmp[100]; - sprintf(dbg_tmp,"Number: '%s'", sender_number); - log_debug(dbg_tmp,DEBUG_ALL); - - // special hack for Motorola L7 - sms.SMS[i].Location-=100000; - - if (sms.SMS[i].Coding == SMS_Coding_8bit) { - log_event("gammu sms error: 8-bit message, can not read"); - // remove SMS - GSM_DeleteSMS(state_machine,&sms.SMS[i]); - return false; - } else { - sprintf(dbg_tmp,"Text: \"%s\"\n", DecodeUnicodeConsole(sms.SMS[i].Text)); - log_debug(dbg_tmp,DEBUG_ALL); - } - - // remove SMS after reading - error = GSM_DeleteSMS(state_machine,&sms.SMS[i]); - if (!check_gammu_error(error)) - return false; - } - - /* Terminate connection */ - error = GSM_TerminateConnection(state_machine); - if (!check_gammu_error(error)) - return false; - return true; -} diff --git a/yasnd-ipc.c b/yasnd-ipc.c index afbc291..beee83c 100644 --- a/yasnd-ipc.c +++ b/yasnd-ipc.c @@ -4,6 +4,7 @@ #include "yasnd.h" pid_t ipc_clone_pid; +pid_t ipc_sms_read_clone_pid=-1; // pid of thread, that checks for new incoming SMS int qid=-1; // IPC queue id int open_queue() @@ -61,7 +62,7 @@ int ipc_queue_loop() mymsgbuf qbuf; while(1) { - int result=read_message(qid, &qbuf, 1); + int result=read_message(qid, &qbuf, 0); if (result==-1) { log_event("Error: finishing IPC loop process..."); @@ -70,6 +71,26 @@ int ipc_queue_loop() char dbg_txt[150]; sprintf(dbg_txt,"Read IPC message - Type: %ld Text: %s", qbuf.mtype, qbuf.mtext); log_debug(dbg_txt,DEBUG_ALL); + if (qbuf.mtype==SMS_SEND_TYPE && sms_send_enable) + { + log_debug(qbuf.mtext,DEBUG_BASE); + gammu_send_sms(qbuf.mtext); + } + sleep(6); + } +} + + +int ipc_sms_reading_loop() +{ + // set default signal handlers + signal(SIGTERM,SIG_DFL); + signal(SIGHUP,SIG_DFL); + // + while(1) + { + sleep(30); /* wait 30 seconds */ + //gammu_read_sms(); } } @@ -78,11 +99,17 @@ void ipc_init() qid = open_queue(); unsigned char ipc_child_stack[16384]; ipc_clone_pid=clone(ipc_queue_loop,ipc_child_stack+8192,CLONE_VM,NULL); + // start loop, that handles new incoming SMS +// unsigned char sms_child_stack[16384]; +// ipc_sms_read_clone_pid=clone(ipc_sms_reading_loop,sms_child_stack+8192,CLONE_VM,NULL); + // } void ipc_free() { kill(ipc_clone_pid,SIGTERM); waitpid(ipc_clone_pid,NULL,__WCLONE); +// kill(ipc_sms_read_clone_pid,SIGTERM); +// waitpid(ipc_sms_read_clone_pid,NULL,__WCLONE); remove_queue(qid); } diff --git a/yasnd.c b/yasnd.c index 7168a2c..fd92f78 100644 --- a/yasnd.c +++ b/yasnd.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include @@ -20,8 +19,8 @@ char* recipient_number=NULL; // recipient of sms alerts bool lpt_enable=false; // control usage of LPT port to reset target devices int lpt_port=0x378; // LPT port in hex(0x378 is usually LPT1) bool loop_locked=false; // flag for locking main loop while config re-reading -long failures_first=3; // count of failures while hosts checking, that triggers SMS sending -long failures_second=6; // count of failures while hosts checking, that triggers host's reset +long failures_first=0; // count of failures while hosts checking, that triggers SMS sending +long failures_second=1; // count of failures while hosts checking, that triggers host's reset long failures_third=9; // count of failures while hosts checking, that clean failure statistics pid_t mainloop_clone_pid; // pid of clone process, that containt main loop @@ -254,30 +253,29 @@ int loop_function() } for (int i=0;ifailures_second && hosts[i].alert_sent && !hosts[i].reaction_obtained) { if (lpt_enable) { - char message[150]; - sprintf(message,"Host %s does not answer and no reaction on this. Trying to reset it(LPT pin %d)",hosts[i].hostname,hosts[i].lpt_pin); - log_debug(message,DEBUG_BASE); - if (sms_send_enable) - gammu_send_sms(message); + qbuf.mtype=SMS_SEND_TYPE; + sprintf(qbuf.mtext,"Host %s does not answer and no reaction on this. Trying to reset it(LPT pin %d)",hosts[i].hostname,hosts[i].lpt_pin); reset_pin(hosts[i].lpt_pin); hosts[i].reaction_obtained=true; + // send message with SMS text to IPC queue + send_message(qid,&qbuf); } continue; } if (hosts[i].fail_count>failures_first && !hosts[i].alert_sent) { - char message[100]; - sprintf(message,"Host %s does not answer(LPT pin %d)",hosts[i].hostname,hosts[i].lpt_pin); - log_debug(message,DEBUG_BASE); - if (sms_send_enable) - gammu_send_sms(message); + qbuf.mtype=SMS_SEND_TYPE; + sprintf(qbuf.mtext,"Host %s does not answer(LPT pin %d)",hosts[i].hostname,hosts[i].lpt_pin); // set alert flag to prevent sending more than 1 message // for unreachable host hosts[i].alert_sent=true; + // send message with SMS text to IPC queue + send_message(qid,&qbuf); continue; } } diff --git a/yasnd.h b/yasnd.h index 75ba511..cf696de 100644 --- a/yasnd.h +++ b/yasnd.h @@ -5,6 +5,11 @@ #define DEBUG_BASE 1 #define DEBUG_ALL 2 +// IPC message types +#define SMS_SEND_TYPE 1 +#define SMS_RECEIVE_TYPE 2 + +#include #include #include #include @@ -49,5 +54,7 @@ extern char* recipient_number; extern char* phone_device; // phone device for gammu, for example: "/dev/ttyACM0" extern char* phone_model; // gammu phone model, for example: "at" extern char* phone_connection; // gammu phone connection type, for example: "at" +extern bool sms_send_enable; +extern int qid; #endif