initial implementation of sms sending
This commit is contained in:
parent
3e183be533
commit
e6d0c3610e
101
yasnd-gammu.c
101
yasnd-gammu.c
@ -1,14 +1,113 @@
|
||||
#include "yasnd.h"
|
||||
|
||||
GSM_Error error; // structure to store possible gammu errors
|
||||
GSM_StateMachine *state_machine=NULL; // structure to interact with mobile phones
|
||||
volatile gboolean gammu_shutdown=FALSE; // variable that indicates gammu shutdown
|
||||
volatile GSM_Error sms_send_status;
|
||||
|
||||
bool check_gammu_error(GSM_Error err)
|
||||
{
|
||||
if (err == ERR_NONE) {
|
||||
return false;
|
||||
// No error, return OK('true')
|
||||
return true;
|
||||
}
|
||||
char tmp[150];
|
||||
sprintf(tmp,"gammu failure: %s\n", GSM_ErrorString(err));
|
||||
log_event(tmp);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool gammu_init()
|
||||
{
|
||||
GSM_InitLocales(NULL);
|
||||
state_machine = GSM_AllocStateMachine();
|
||||
/* Set gammu configuration */
|
||||
GSM_Config *gammu_cfg = GSM_GetConfig(state_machine, 0);
|
||||
// first, free old values and set new one
|
||||
free(gammu_cfg->Device);
|
||||
gammu_cfg->Device = strdup("/dev/ttyACM0");
|
||||
free(gammu_cfg->Connection);
|
||||
gammu_cfg->Connection = strdup("at");
|
||||
strcpy(gammu_cfg->Model, "at");
|
||||
/* We care only about first configuration */
|
||||
GSM_SetConfigNum(state_machine, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Handler for SMS send reply */
|
||||
void send_sms_callback (GSM_StateMachine *sm, int status, int MessageReference, void * user_data)
|
||||
{
|
||||
char tmp[100];
|
||||
if (status==0) {
|
||||
sprintf(tmp,"Sent SMS on device '%s' - OK\n", GSM_GetConfig(sm, -1)->Device);
|
||||
sms_send_status = ERR_NONE;
|
||||
} else {
|
||||
sprintf(tmp,"Sent SMS on device '%s' - FAILURE\n", GSM_GetConfig(sm, -1)->Device);
|
||||
sms_send_status = ERR_UNKNOWN;
|
||||
}
|
||||
log_event(tmp);
|
||||
}
|
||||
|
||||
bool gammu_send_sms(const char* message)
|
||||
{
|
||||
GSM_SMSMessage sms;
|
||||
GSM_SMSC PhoneSMSC;
|
||||
int return_value = 0;
|
||||
// STUB: recipient number must be in config file
|
||||
const char recipient_number[]="+79515019051";
|
||||
// END STUB
|
||||
|
||||
/* Prepare message */
|
||||
// Cleanup the structure
|
||||
memset(&sms, 0, sizeof(sms));
|
||||
// Encode message text
|
||||
EncodeUnicode(sms.Text, message, strlen(message));
|
||||
// Encode recipient numbe
|
||||
EncodeUnicode(sms.Number, recipient_number, strlen(recipient_number));
|
||||
/* We want to submit message */
|
||||
sms.PDU = SMS_Submit;
|
||||
/* No UDH, just a plain message */
|
||||
sms.UDH.Type = UDH_NoUDH;
|
||||
/* We used default coding for text */
|
||||
sms.Coding = SMS_Coding_Default_No_Compression;
|
||||
/* Class 1 message (normal) */
|
||||
sms.Class = 1;
|
||||
|
||||
/* 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;
|
||||
|
||||
/* Set callback for message sending */
|
||||
/* This needs to be done after initiating connection */
|
||||
GSM_SetSendSMSStatusCallback(state_machine, send_sms_callback, NULL);
|
||||
|
||||
/* We need to know SMSC number */
|
||||
PhoneSMSC.Location = 1;
|
||||
error = GSM_GetSMSC(state_machine, &PhoneSMSC);
|
||||
if (!check_gammu_error(error))
|
||||
return false;
|
||||
|
||||
/* Set SMSC number in message */
|
||||
CopyUnicodeString(sms.SMSC.Number, PhoneSMSC.Number);
|
||||
|
||||
log_debug("Prepare to send SMS...",DEBUG_ALL);
|
||||
/* Send message */
|
||||
error = GSM_SendSMS(state_machine, &sms);
|
||||
if (!check_gammu_error(error))
|
||||
return false;
|
||||
|
||||
while (!gammu_shutdown) {
|
||||
GSM_ReadDevice(state_machine, TRUE);
|
||||
if (sms_send_status == ERR_NONE) {
|
||||
/* Message sent OK */
|
||||
return true;
|
||||
}
|
||||
if (sms_send_status != ERR_TIMEOUT) {
|
||||
/* Message sending failed */
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
31
yasnd.c
31
yasnd.c
@ -8,7 +8,6 @@
|
||||
#include <unistd.h>
|
||||
#include <syslog.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <confuse.h>
|
||||
|
||||
#include "yasnd.h"
|
||||
@ -17,7 +16,6 @@ cfg_t *cfg; // pointer to configuration structure
|
||||
host_decl* hosts=NULL; // structure with hosts' definitions
|
||||
int hosts_count=0; // count of hosts
|
||||
int debug_flag=0;
|
||||
GSM_StateMachine *state_machine=NULL; // structure to interact with mobile phones
|
||||
|
||||
void log_debug(const char *message,int verbosity)
|
||||
{
|
||||
@ -71,18 +69,8 @@ void init()
|
||||
hosts[i].fail_count=0;
|
||||
}
|
||||
// initialize gammu structures
|
||||
GSM_InitLocales(NULL);
|
||||
state_machine = GSM_AllocStateMachine();
|
||||
/* Set gammu configuration */
|
||||
GSM_Config *gammu_cfg = GSM_GetConfig(state_machine, 0);
|
||||
// first, free old values and set new one
|
||||
free(gammu_cfg->Device);
|
||||
gammu_cfg->Device = strdup("/dev/ttyACM0");
|
||||
free(gammu_cfg->Connection);
|
||||
gammu_cfg->Connection = strdup("at");
|
||||
strcpy(gammu_cfg->Model, "at");
|
||||
/* We care only about first configuration */
|
||||
GSM_SetConfigNum(state_machine, 1);
|
||||
gammu_init();
|
||||
|
||||
}
|
||||
|
||||
void check_host(int num,host_decl* host)
|
||||
@ -97,7 +85,7 @@ void check_host(int num,host_decl* host)
|
||||
char tmp[50];
|
||||
sprintf(tmp, "Pinging host %s", host->hostname);
|
||||
log_debug(tmp,DEBUG_BASE);
|
||||
execl("/bin/ping","/bin/ping","-c 4","-n",host->hostname,(char*) 0);
|
||||
execl("/bin/ping","/bin/ping","-c 1","-n",host->hostname,(char*) 0);
|
||||
// STUB: check result of exec call
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
@ -131,6 +119,18 @@ void loop_function()
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i=0;i<hosts_count;i++)
|
||||
{
|
||||
if (hosts[i].fail_count>0)
|
||||
{
|
||||
char log_message[100];
|
||||
char sms_message[100];
|
||||
sprintf(log_message,"Host %s does not answer, sending sms",hosts[i].hostname);
|
||||
log_debug(log_message,DEBUG_BASE);
|
||||
sprintf(sms_message,"Host %s does not answer",hosts[i].hostname);
|
||||
gammu_send_sms(sms_message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void termination_handler(int signum)
|
||||
@ -195,6 +195,7 @@ int main(void) {
|
||||
while (1) {
|
||||
loop_function();
|
||||
// sleep(60); /* wait 60 seconds */
|
||||
log_event("Exiting");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
|
6
yasnd.h
6
yasnd.h
@ -6,10 +6,14 @@
|
||||
#define DEBUG_ALL 2
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <gammu.h>
|
||||
|
||||
// External functions
|
||||
extern void log_event(const char *message);
|
||||
extern bool check_gammu_error(GSM_Error err);
|
||||
extern bool gammu_init();
|
||||
extern bool gammu_send_sms(const char* message);
|
||||
|
||||
typedef struct {
|
||||
char* hostname; // address of host
|
||||
@ -17,8 +21,10 @@ typedef struct {
|
||||
int fail_count; // how many times in a row host was unreachable
|
||||
} host_decl;
|
||||
|
||||
// External variables
|
||||
extern int hosts_count; // count of hosts
|
||||
extern int debug_flag;
|
||||
extern GSM_Error error; // structure to store possible gammu errors
|
||||
extern GSM_StateMachine *state_machine; // structure to interact with mobile phones
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user