Split out GAMMU-related code from library into daemon.

Add proper optional support for GAMMU via build-time options.
This commit fixes #4, but some additional checks maybe needed.
This commit is contained in:
2013-09-02 22:54:14 +04:00
parent badad6874c
commit a1c83a3df2
8 changed files with 23 additions and 8 deletions

View File

@ -1,10 +1,11 @@
include_HEADERS = $(top_srcdir)/yasnd.h yasnd-sock.h
include_HEADERS = $(top_srcdir)/yasnd.h yasnd-gammu.h yasnd-sock.h
yasnd_LDADD = ${libconfuse_LIBS}
yasnd_LDADD += $(top_srcdir)/lib/libyasnd.la
yasnd_LDADD += @GAMMU_LIBS@
sbin_PROGRAMS = yasnd
yasnd_SOURCES = yasnd.c yasnd-ipc.c yasnd-sock.c
yasnd_CPPFLAGS = -I$(top_srcdir)
yasnd_SOURCES = yasnd.c yasnd-gammu.c yasnd-ipc.c yasnd-sock.c
yasnd_CPPFLAGS = -I$(top_srcdir) @GAMMU_CFLAGS@
distclean-local:
rm -f Makefile.in &>/dev/null

188
daemon/yasnd-gammu.c Normal file
View File

@ -0,0 +1,188 @@
#include "config.h"
#include "yasnd.h"
#include "yasnd-log.h"
#ifdef HAVE_LIBGAMMU
#include <gammu.h>
#endif
char* phone_device=NULL; // phone device for gammu, for example: "/dev/ttyACM0"
char* phone_model=NULL; // gammu phone model, for example: "at"
char* phone_connection=NULL; // gammu phone connection type, for example: "at"
#ifdef HAVE_LIBGAMMU
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) {
// 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_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);
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(phone_device);
free(gammu_cfg->Connection);
gammu_cfg->Connection = strdup(phone_connection);
strcpy(gammu_cfg->Model, phone_model);
/* We care only about first configuration */
GSM_SetConfigNum(state_machine, 1);
return true;
}
void gammu_free()
{
GSM_FreeStateMachine(state_machine);
}
/* 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;
log_debug(tmp,DEBUG_BASE);
} 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;
/* 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;
}
#endif

15
daemon/yasnd-gammu.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef YASND_GAMMU_H
#define YASND_GAMMU_H
// External variables
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"
// External functions
extern bool gammu_init();
extern bool gammu_send_sms(const char* message);
extern bool gammu_read_sms();
extern void gammu_free();
#endif

View File

@ -1,3 +1,4 @@
#include "config.h"
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>
@ -70,6 +71,7 @@ void* ipc_queue_loop(void* param)
char dbg_txt[150];
sprintf(dbg_txt,"Read IPC message - Type: %ld Text: %s", qbuf.mtype, qbuf.mtext);
log_debug(dbg_txt,DEBUG_ALL);
#ifdef HAVE_LIBGAMMU
if (qbuf.mtype==SMS_RECEIVE_TYPE && sms_enable)
{
pid_t pid = fork();
@ -103,6 +105,7 @@ void* ipc_queue_loop(void* param)
// STUB: need to react on status, returned by thread
waitpid(pid,NULL,0);
}
#endif
}
}

View File

@ -1,3 +1,4 @@
#include "config.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
@ -152,9 +153,11 @@ void init()
hosts[i].alert_sent=false;
hosts[i].reaction_obtained=false;
}
#ifdef HAVE_LIBGAMMU
// initialize gammu structures
if (sms_enable)
gammu_init();
#endif
// initialize LPT control circuit
lpt_init();
// initialize server socket
@ -281,8 +284,10 @@ void signal_handler(int signum)
ipc_free();
// free config structure
cfg_free(cfg);
#ifdef HAVE_LIBGAMMU
// free gammu structure
gammu_free();
#endif
// free hosts structures memory
if (hosts!=NULL)
free(hosts);