summaryrefslogtreecommitdiff
path: root/smsd/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'smsd/core.c')
-rw-r--r--smsd/core.c69
1 files changed, 39 insertions, 30 deletions
diff --git a/smsd/core.c b/smsd/core.c
index 7c29640..80bbe3c 100644
--- a/smsd/core.c
+++ b/smsd/core.c
@@ -59,7 +59,7 @@
#include "log-event.h"
#endif
-#include "../helper/string.h"
+#include "../libgammu/misc/string.h"
#ifndef PATH_MAX
#ifdef MAX_PATH
@@ -106,6 +106,21 @@ GSM_Error SMSD_Shutdown(GSM_SMSDConfig *Config)
}
/**
+ * Interruptuptible sleep allowing to terminate SMSD.
+ */
+void SMSD_InterruptibleSleep(GSM_SMSDConfig *Config, int seconds)
+{
+ int i, loops;
+ loops = seconds * 2;
+ for (i = 0; i < loops; i++) {
+ if (Config->shutdown) {
+ break;
+ }
+ usleep(500000);
+ }
+}
+
+/**
* Callback from libGammu on sending message.
*/
void SMSD_SendSMSStatusCallback (GSM_StateMachine *sm, int status, int mr, void *user_data)
@@ -391,6 +406,7 @@ GSM_SMSDConfig *SMSD_NewConfig(const char *name)
Config->ServiceName = NULL;
Config->Service = NULL;
Config->IgnoredMessages = 0;
+ Config->PhoneID = NULL;
#if defined(HAVE_MYSQL_MYSQL_H)
Config->conn.my = NULL;
@@ -804,7 +820,7 @@ GSM_Error SMSD_ReadConfig(const char *filename, GSM_SMSDConfig *Config, gboolean
Config->deliveryreportdelay = INI_GetInt(Config->smsdcfgfile, "smsd", "deliveryreportdelay", 600);
Config->sendtimeout = INI_GetInt(Config->smsdcfgfile, "smsd", "sendtimeout", 30);
Config->receivefrequency = INI_GetInt(Config->smsdcfgfile, "smsd", "receivefrequency", 15);
- Config->statusfrequency = INI_GetInt(Config->smsdcfgfile, "smsd", "statusfrequency", 15);
+ Config->statusfrequency = INI_GetInt(Config->smsdcfgfile, "smsd", "statusfrequency", 60);
Config->loopsleep = INI_GetInt(Config->smsdcfgfile, "smsd", "loopsleep", 1);
Config->checksecurity = INI_GetBool(Config->smsdcfgfile, "smsd", "checksecurity", TRUE);
Config->hangupcalls = INI_GetBool(Config->smsdcfgfile, "smsd", "hangupcalls", FALSE);
@@ -1047,7 +1063,7 @@ void SMSD_RunOnReceiveEnvironment(GSM_MultiSMSMessage *sms, GSM_SMSDConfig *Conf
setenv(name, buffer, 1);
sprintf(name, "SMS_%d_NUMBER", i + 1);
setenv(name, DecodeUnicodeConsole(sms->SMS[i].Number), 1);
- if (sms->SMS[i].Coding != SMS_Coding_8bit) {
+ if (sms->SMS[i].Coding != SMS_Coding_8bit && sms->SMS[i].UDH.Type != UDH_UserUDH) {
sprintf(name, "SMS_%d_TEXT", i + 1);
setenv(name, DecodeUnicodeConsole(sms->SMS[i].Text), 1);
}
@@ -1065,7 +1081,7 @@ void SMSD_RunOnReceiveEnvironment(GSM_MultiSMSMessage *sms, GSM_SMSDConfig *Conf
case SMS_ConcatenatedAutoTextLong16bit:
case SMS_NokiaVCARD21Long:
case SMS_NokiaVCALENDAR10Long:
- sprintf(name, "DECODED_%d_TEXT", i);
+ sprintf(name, "DECODED_%d_TEXT", i + 1);
setenv(name, DecodeUnicodeConsole(SMSInfo.Entries[i].Buffer), 1);
break;
case SMS_MMSIndicatorLong:
@@ -1783,7 +1799,7 @@ failure_unsent:
Config->Service->UpdateRetries(Config, Config->SMSID);
- sleep(60);
+ SMSD_InterruptibleSleep(Config, 60);
return ERR_UNKNOWN;
failure_sent:
@@ -1933,6 +1949,7 @@ void SMSD_IncomingCallCallback(GSM_StateMachine *s, GSM_Call *call, void *user_d
SMSD_Log(DEBUG_INFO, Config, "Call callback: Unknown status %d\n", call->Status);
}
}
+
/**
* Main loop which takes care of connection to phone and processing of
* messages.
@@ -1943,8 +1960,7 @@ GSM_Error SMSD_MainLoop(GSM_SMSDConfig *Config, gboolean exit_on_failure, int ma
int errors = -1, initerrors=0;
double lastsleep;
time_t lastreceive = 0, lastreset = time(NULL), lasthardreset = time(NULL), lastnothingsent = 0, laststatus = 0;
- time_t lastloop = 0, current_time;
- int i;
+ time_t lastloop = 0;
gboolean first_start = TRUE, force_reset = FALSE, force_hard_reset = FALSE;
Config->failure = ERR_NONE;
@@ -1988,11 +2004,7 @@ GSM_Error SMSD_MainLoop(GSM_SMSDConfig *Config, gboolean exit_on_failure, int ma
if (initerrors++ > 3) {
SMSD_Log(DEBUG_INFO, Config, "Going to 30 seconds sleep because of too many connection errors");
- for (i = 0; i < 60; i++) {
- if (Config->shutdown)
- break;
- usleep(500000);
- }
+ SMSD_InterruptibleSleep(Config, 30);
}
SMSD_Log(DEBUG_INFO, Config, "Starting phone communication...");
error = GSM_InitConnection_Log(Config->gsm, 2, SMSD_Log_Function, Config);
@@ -2043,14 +2055,14 @@ GSM_Error SMSD_MainLoop(GSM_SMSDConfig *Config, gboolean exit_on_failure, int ma
error = GSM_Reset(Config->gsm, FALSE); /* soft reset */
SMSD_LogError(DEBUG_INFO, Config, "Soft reset return code", error);
lastreset = time(NULL);
- sleep(5);
+ SMSD_InterruptibleSleep(Config, 5);
force_reset = FALSE;
}
if (force_hard_reset) {
error = GSM_Reset(Config->gsm, TRUE); /* hard reset */
SMSD_LogError(DEBUG_INFO, Config, "Hard reset return code", error);
lasthardreset = time(NULL);
- sleep(5);
+ SMSD_InterruptibleSleep(Config, 5);
force_hard_reset = FALSE;
}
break;
@@ -2067,7 +2079,7 @@ GSM_Error SMSD_MainLoop(GSM_SMSDConfig *Config, gboolean exit_on_failure, int ma
}
/* Should we receive? */
- if (Config->enable_receive && ((difftime(time(NULL), lastreceive) >= Config->receivefrequency) || (Config->SendingSMSStatus != ERR_NONE))) {
+ if (Config->enable_receive && ((difftime(lastloop, lastreceive) >= Config->receivefrequency) || (Config->SendingSMSStatus != ERR_NONE))) {
lastreceive = time(NULL);
/* Do we need to check security? */
@@ -2095,12 +2107,11 @@ GSM_Error SMSD_MainLoop(GSM_SMSDConfig *Config, gboolean exit_on_failure, int ma
/* time for preventive reset */
- current_time = time(NULL);
- if (Config->resetfrequency > 0 && difftime(current_time, lastreset) >= Config->resetfrequency) {
+ if (Config->resetfrequency > 0 && difftime(lastloop, lastreset) >= Config->resetfrequency) {
force_reset = TRUE;
continue;
}
- if (Config->hardresetfrequency > 0 && difftime(current_time, lasthardreset) >= Config->hardresetfrequency) {
+ if (Config->hardresetfrequency > 0 && difftime(lastloop, lasthardreset) >= Config->hardresetfrequency) {
force_hard_reset = TRUE;
continue;
}
@@ -2109,11 +2120,10 @@ GSM_Error SMSD_MainLoop(GSM_SMSDConfig *Config, gboolean exit_on_failure, int ma
}
/* Send any queued messages */
- current_time = time(NULL);
- if (Config->enable_send && (difftime(current_time, lastnothingsent) >= Config->commtimeout)) {
+ if (Config->enable_send && (difftime(lastloop, lastnothingsent) >= Config->commtimeout)) {
error = SMSD_SendSMS(Config);
if (error == ERR_EMPTY) {
- lastnothingsent = current_time;
+ lastnothingsent = lastloop;
}
/* We don't care about other errors here, they are handled in SMSD_SendSMS */
}
@@ -2122,23 +2132,22 @@ GSM_Error SMSD_MainLoop(GSM_SMSDConfig *Config, gboolean exit_on_failure, int ma
}
/* Refresh phone status in shared memory and in service */
- current_time = time(NULL);
- if ((Config->statusfrequency > 0) && (difftime(current_time, laststatus) >= Config->statusfrequency)) {
+ if ((Config->statusfrequency > 0) && (difftime(lastloop, laststatus) >= Config->statusfrequency)) {
SMSD_PhoneStatus(Config);
- laststatus = current_time;
+ laststatus = lastloop;
Config->Service->RefreshPhoneStatus(Config);
}
if (Config->shutdown) {
break;
}
+
/* Sleep some time before another loop */
- current_time = time(NULL);
- lastsleep = difftime(current_time, lastloop);
- if (Config->loopsleep == 1) {
- sleep(1);
- } else if (lastsleep < Config->loopsleep) {
- sleep(Config->loopsleep - lastsleep);
+ /* Duration of last loop cycle */
+ lastsleep = difftime(time(NULL), lastloop);
+ if (Config->loopsleep > 0 && lastsleep < Config->loopsleep) {
+ /* Sleep LoopSleep - time of the loop */
+ SMSD_InterruptibleSleep(Config, Config->loopsleep - lastsleep);
}
}
Config->Service->Free(Config);