implement SMS sending via IPC queue loop
This commit is contained in:
parent
94e7bc7478
commit
d8829305fc
122
yasnd-gammu.c
122
yasnd-gammu.c
@ -18,6 +18,67 @@ bool check_gammu_error(GSM_Error err)
|
|||||||
return false;
|
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()
|
bool gammu_init()
|
||||||
{
|
{
|
||||||
GSM_InitLocales(NULL);
|
GSM_InitLocales(NULL);
|
||||||
@ -115,64 +176,3 @@ bool gammu_send_sms(const char* message)
|
|||||||
}
|
}
|
||||||
return true;
|
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;
|
|
||||||
}
|
|
||||||
|
29
yasnd-ipc.c
29
yasnd-ipc.c
@ -4,6 +4,7 @@
|
|||||||
#include "yasnd.h"
|
#include "yasnd.h"
|
||||||
|
|
||||||
pid_t ipc_clone_pid;
|
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 qid=-1; // IPC queue id
|
||||||
|
|
||||||
int open_queue()
|
int open_queue()
|
||||||
@ -61,7 +62,7 @@ int ipc_queue_loop()
|
|||||||
mymsgbuf qbuf;
|
mymsgbuf qbuf;
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
int result=read_message(qid, &qbuf, 1);
|
int result=read_message(qid, &qbuf, 0);
|
||||||
if (result==-1)
|
if (result==-1)
|
||||||
{
|
{
|
||||||
log_event("Error: finishing IPC loop process...");
|
log_event("Error: finishing IPC loop process...");
|
||||||
@ -70,6 +71,26 @@ int ipc_queue_loop()
|
|||||||
char dbg_txt[150];
|
char dbg_txt[150];
|
||||||
sprintf(dbg_txt,"Read IPC message - Type: %ld Text: %s", qbuf.mtype, qbuf.mtext);
|
sprintf(dbg_txt,"Read IPC message - Type: %ld Text: %s", qbuf.mtype, qbuf.mtext);
|
||||||
log_debug(dbg_txt,DEBUG_ALL);
|
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();
|
qid = open_queue();
|
||||||
unsigned char ipc_child_stack[16384];
|
unsigned char ipc_child_stack[16384];
|
||||||
ipc_clone_pid=clone(ipc_queue_loop,ipc_child_stack+8192,CLONE_VM,NULL);
|
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()
|
void ipc_free()
|
||||||
{
|
{
|
||||||
kill(ipc_clone_pid,SIGTERM);
|
kill(ipc_clone_pid,SIGTERM);
|
||||||
waitpid(ipc_clone_pid,NULL,__WCLONE);
|
waitpid(ipc_clone_pid,NULL,__WCLONE);
|
||||||
|
// kill(ipc_sms_read_clone_pid,SIGTERM);
|
||||||
|
// waitpid(ipc_sms_read_clone_pid,NULL,__WCLONE);
|
||||||
remove_queue(qid);
|
remove_queue(qid);
|
||||||
}
|
}
|
||||||
|
24
yasnd.c
24
yasnd.c
@ -2,7 +2,6 @@
|
|||||||
#include <sys/io.h>
|
#include <sys/io.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <confuse.h>
|
#include <confuse.h>
|
||||||
|
|
||||||
@ -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
|
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)
|
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
|
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_first=0; // 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_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
|
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
|
pid_t mainloop_clone_pid; // pid of clone process, that containt main loop
|
||||||
|
|
||||||
@ -254,30 +253,29 @@ int loop_function()
|
|||||||
}
|
}
|
||||||
for (int i=0;i<hosts_count;i++)
|
for (int i=0;i<hosts_count;i++)
|
||||||
{
|
{
|
||||||
|
mymsgbuf qbuf;
|
||||||
if (hosts[i].fail_count>failures_second && hosts[i].alert_sent && !hosts[i].reaction_obtained)
|
if (hosts[i].fail_count>failures_second && hosts[i].alert_sent && !hosts[i].reaction_obtained)
|
||||||
{
|
{
|
||||||
if (lpt_enable)
|
if (lpt_enable)
|
||||||
{
|
{
|
||||||
char message[150];
|
qbuf.mtype=SMS_SEND_TYPE;
|
||||||
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);
|
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);
|
||||||
log_debug(message,DEBUG_BASE);
|
|
||||||
if (sms_send_enable)
|
|
||||||
gammu_send_sms(message);
|
|
||||||
reset_pin(hosts[i].lpt_pin);
|
reset_pin(hosts[i].lpt_pin);
|
||||||
hosts[i].reaction_obtained=true;
|
hosts[i].reaction_obtained=true;
|
||||||
|
// send message with SMS text to IPC queue
|
||||||
|
send_message(qid,&qbuf);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (hosts[i].fail_count>failures_first && !hosts[i].alert_sent)
|
if (hosts[i].fail_count>failures_first && !hosts[i].alert_sent)
|
||||||
{
|
{
|
||||||
char message[100];
|
qbuf.mtype=SMS_SEND_TYPE;
|
||||||
sprintf(message,"Host %s does not answer(LPT pin %d)",hosts[i].hostname,hosts[i].lpt_pin);
|
sprintf(qbuf.mtext,"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);
|
|
||||||
// set alert flag to prevent sending more than 1 message
|
// set alert flag to prevent sending more than 1 message
|
||||||
// for unreachable host
|
// for unreachable host
|
||||||
hosts[i].alert_sent=true;
|
hosts[i].alert_sent=true;
|
||||||
|
// send message with SMS text to IPC queue
|
||||||
|
send_message(qid,&qbuf);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
yasnd.h
7
yasnd.h
@ -5,6 +5,11 @@
|
|||||||
#define DEBUG_BASE 1
|
#define DEBUG_BASE 1
|
||||||
#define DEBUG_ALL 2
|
#define DEBUG_ALL 2
|
||||||
|
|
||||||
|
// IPC message types
|
||||||
|
#define SMS_SEND_TYPE 1
|
||||||
|
#define SMS_RECEIVE_TYPE 2
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -49,5 +54,7 @@ extern char* recipient_number;
|
|||||||
extern char* phone_device; // phone device for gammu, for example: "/dev/ttyACM0"
|
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_model; // gammu phone model, for example: "at"
|
||||||
extern char* phone_connection; // gammu phone connection type, for example: "at"
|
extern char* phone_connection; // gammu phone connection type, for example: "at"
|
||||||
|
extern bool sms_send_enable;
|
||||||
|
extern int qid;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user