summaryrefslogtreecommitdiffabout
path: root/gammu/emb/gammu/smsd/smsdcore.c
Side-by-side diff
Diffstat (limited to 'gammu/emb/gammu/smsd/smsdcore.c') (more/less context) (ignore whitespace changes)
-rw-r--r--gammu/emb/gammu/smsd/smsdcore.c180
1 files changed, 128 insertions, 52 deletions
diff --git a/gammu/emb/gammu/smsd/smsdcore.c b/gammu/emb/gammu/smsd/smsdcore.c
index fbc55d3..cc9accc 100644
--- a/gammu/emb/gammu/smsd/smsdcore.c
+++ b/gammu/emb/gammu/smsd/smsdcore.c
@@ -1,4 +1,4 @@
-/* (c) 2002-2003 by Marcin Wiacek and Joergen Thomsen */
+/* (c) 2002-2004 by Marcin Wiacek and Joergen Thomsen */
#include <string.h>
#include <signal.h>
@@ -14,10 +14,13 @@
#endif
FILE *smsd_log_file = NULL;
+static int TPMR;
static GSM_Error SendingSMSStatus;
+
static void SMSSendingSMSStatus (char *Device, int status, int mr)
{
dbgprintf("Incoming SMS device: \"%s\" status=%d, reference=%d\n",Device, status, mr);
+ TPMR = mr;
if (status==0) {
SendingSMSStatus = ERR_NONE;
} else {
@@ -73,7 +76,7 @@ void WriteSMSDLog(char *format, ...)
}
}
-void SMSD_ReadConfig(char *filename, GSM_SMSDConfig *Config, bool log)
+void SMSD_ReadConfig(char *filename, GSM_SMSDConfig *Config, bool log, char *service)
{
INI_Section *smsdcfgfile = NULL;
GSM_Config smsdcfg;
@@ -102,6 +105,7 @@ void SMSD_ReadConfig(char *filename, GSM_SMSDConfig *Config, bool log)
if (Config->IncludeNumbers) {
GSM_ReadConfig(smsdcfgfile, &smsdcfg, 0);
memcpy(&s.Config,&smsdcfg,sizeof(GSM_Config));
+ error=GSM_SetDebugFile(s.Config[0].DebugFile, &di);
}
Config->PINCode=INI_GetValue(smsdcfgfile, "smsd", "PIN", false);
@@ -112,22 +116,16 @@ void SMSD_ReadConfig(char *filename, GSM_SMSDConfig *Config, bool log)
}
if (log) WriteSMSDLog("PIN code is \"%s\"",Config->PINCode);
- Config->user = INI_GetValue(smsdcfgfile, "smsd", "user", false);
- if (Config->user == NULL) Config->user="root";
- Config->password = INI_GetValue(smsdcfgfile, "smsd", "password", false);
- if (Config->password == NULL) Config->password="";
- Config->PC = INI_GetValue(smsdcfgfile, "smsd", "pc", false);
- if (Config->PC == NULL) Config->PC="localhost";
- Config->database = INI_GetValue(smsdcfgfile, "smsd", "database", false);
- if (Config->database == NULL) Config->database="sms";
-
str = INI_GetValue(smsdcfgfile, "smsd", "commtimeout", false);
if (str) Config->commtimeout=atoi(str); else Config->commtimeout = 1;
str = INI_GetValue(smsdcfgfile, "smsd", "sendtimeout", false);
if (str) Config->sendtimeout=atoi(str); else Config->sendtimeout = 10;
str = INI_GetValue(smsdcfgfile, "smsd", "receivefrequency", false);
if (str) Config->receivefrequency=atoi(str); else Config->receivefrequency = 0;
- if (log) WriteSMSDLog("commtimeout=%i, sendtimeout=%i, receivefrequency=%i", Config->commtimeout, Config->sendtimeout, Config->receivefrequency);
+ str = INI_GetValue(smsdcfgfile, "smsd", "resetfrequency", false);
+ if (str) Config->resetfrequency=atoi(str); else Config->resetfrequency = 0;
+ if (log) WriteSMSDLog("commtimeout=%i, sendtimeout=%i, receivefrequency=%i, resetfrequency=%i",
+ Config->commtimeout, Config->sendtimeout, Config->receivefrequency, Config->resetfrequency);
Config->deliveryreport = INI_GetValue(smsdcfgfile, "smsd", "deliveryreport", false);
if (Config->deliveryreport == NULL || (!mystrncasecmp(Config->deliveryreport, "log", 3) && !mystrncasecmp(Config->deliveryreport, "sms", 3))) {
@@ -135,31 +133,52 @@ void SMSD_ReadConfig(char *filename, GSM_SMSDConfig *Config, bool log)
}
if (log) WriteSMSDLog("deliveryreport = %s", Config->deliveryreport);
- Config->inboxpath=INI_GetValue(smsdcfgfile, "smsd", "inboxpath", false);
- if (Config->inboxpath == NULL) Config->inboxpath = emptyPath;
+ Config->PhoneID = INI_GetValue(smsdcfgfile, "smsd", "phoneid", false);
+ if (Config->PhoneID == NULL) Config->PhoneID = "";
+ if (log) WriteSMSDLog("phoneid = %s", Config->PhoneID);
- Config->inboxformat=INI_GetValue(smsdcfgfile, "smsd", "inboxformat", false);
- if (Config->inboxformat == NULL || (!mystrncasecmp(Config->inboxformat, "detail", 6) && !mystrncasecmp(Config->inboxformat, "unicode", 7))) {
- Config->inboxformat = "standard";
- }
- if (log) WriteSMSDLog("Inbox is \"%s\" with format \"%s\"", Config->inboxpath, Config->inboxformat);
+ if (!strcmp(service,"FILES")) {
+ Config->inboxpath=INI_GetValue(smsdcfgfile, "smsd", "inboxpath", false);
+ if (Config->inboxpath == NULL) Config->inboxpath = emptyPath;
- Config->outboxpath=INI_GetValue(smsdcfgfile, "smsd", "outboxpath", false);
- if (Config->outboxpath == NULL) Config->outboxpath = emptyPath;
+ Config->inboxformat=INI_GetValue(smsdcfgfile, "smsd", "inboxformat", false);
+ if (Config->inboxformat == NULL || (!mystrncasecmp(Config->inboxformat, "detail", 6) && !mystrncasecmp(Config->inboxformat, "unicode", 7))) {
+ Config->inboxformat = "standard";
+ }
+ if (log) WriteSMSDLog("Inbox is \"%s\" with format \"%s\"", Config->inboxpath, Config->inboxformat);
- Config->transmitformat=INI_GetValue(smsdcfgfile, "smsd", "transmitformat", false);
- if (Config->transmitformat == NULL || (!mystrncasecmp(Config->transmitformat, "auto", 4) && !mystrncasecmp(Config->transmitformat, "unicode", 7))) {
- Config->transmitformat = "7bit";
- }
- if (log) WriteSMSDLog("Outbox is \"%s\" with transmission format \"%s\"", Config->outboxpath, Config->transmitformat);
+ Config->outboxpath=INI_GetValue(smsdcfgfile, "smsd", "outboxpath", false);
+ if (Config->outboxpath == NULL) Config->outboxpath = emptyPath;
- Config->sentsmspath=INI_GetValue(smsdcfgfile, "smsd", "sentsmspath", false);
- if (Config->sentsmspath == NULL) Config->sentsmspath = Config->outboxpath;
- if (log) WriteSMSDLog("Sent SMS moved to \"%s\"",Config->sentsmspath);
+ Config->transmitformat=INI_GetValue(smsdcfgfile, "smsd", "transmitformat", false);
+ if (Config->transmitformat == NULL || (!mystrncasecmp(Config->transmitformat, "auto", 4) && !mystrncasecmp(Config->transmitformat, "unicode", 7))) {
+ Config->transmitformat = "7bit";
+ }
+ if (log) WriteSMSDLog("Outbox is \"%s\" with transmission format \"%s\"", Config->outboxpath, Config->transmitformat);
- Config->errorsmspath=INI_GetValue(smsdcfgfile, "smsd", "errorsmspath", false);
- if (Config->errorsmspath == NULL) Config->errorsmspath = Config->sentsmspath;
- if (log) WriteSMSDLog("SMS with errors moved to \"%s\"",Config->errorsmspath);
+ Config->sentsmspath=INI_GetValue(smsdcfgfile, "smsd", "sentsmspath", false);
+ if (Config->sentsmspath == NULL) Config->sentsmspath = Config->outboxpath;
+ if (log) WriteSMSDLog("Sent SMS moved to \"%s\"",Config->sentsmspath);
+
+ Config->errorsmspath=INI_GetValue(smsdcfgfile, "smsd", "errorsmspath", false);
+ if (Config->errorsmspath == NULL) Config->errorsmspath = Config->sentsmspath;
+ if (log) WriteSMSDLog("SMS with errors moved to \"%s\"",Config->errorsmspath);
+ }
+
+#ifdef HAVE_MYSQL_MYSQL_H
+ if (!strcmp(service,"MYSQL")) {
+ Config->skipsmscnumber = INI_GetValue(smsdcfgfile, "smsd", "skipsmscnumber", false);
+ if (Config->skipsmscnumber == NULL) Config->skipsmscnumber="";
+ Config->user = INI_GetValue(smsdcfgfile, "smsd", "user", false);
+ if (Config->user == NULL) Config->user="root";
+ Config->password = INI_GetValue(smsdcfgfile, "smsd", "password", false);
+ if (Config->password == NULL) Config->password="";
+ Config->PC = INI_GetValue(smsdcfgfile, "smsd", "pc", false);
+ if (Config->PC == NULL) Config->PC="localhost";
+ Config->database = INI_GetValue(smsdcfgfile, "smsd", "database", false);
+ if (Config->database == NULL) Config->database="sms";
+ }
+#endif
Config->IncludeNumbers=INI_FindLastSectionEntry(smsdcfgfile, "include_numbers", false);
Config->ExcludeNumbers=INI_FindLastSectionEntry(smsdcfgfile, "exclude_numbers", false);
@@ -174,8 +193,10 @@ void SMSD_ReadConfig(char *filename, GSM_SMSDConfig *Config, bool log)
}
}
- Config->retries = 0;
- Config->prevSMSID[0] = 0;
+ Config->retries = 0;
+ Config->prevSMSID[0] = 0;
+ Config->SMSC.Location = 0;
+ Config->relativevalidity = -1;
}
bool SMSD_CheckSecurity(GSM_SMSDConfig *Config)
@@ -192,6 +213,7 @@ bool SMSD_CheckSecurity(GSM_SMSDConfig *Config)
}
/* No supported - do not check more */
if (error == ERR_NOTSUPPORTED) return true;
+
/* If PIN, try to enter */
switch (SecurityCode.Type) {
case SEC_Pin:
@@ -326,6 +348,7 @@ bool SMSD_SendSMS(GSM_SMSDConfig *Config,GSM_SMSDService *Service)
my_sleep(10);
GSM_GetCurrentDateTime(&Date);
}
+ Service->RefreshPhoneStatus(Config);
}
return true;
}
@@ -333,11 +356,12 @@ bool SMSD_SendSMS(GSM_SMSDConfig *Config,GSM_SMSDService *Service)
/* Unknown error - escape */
WriteSMSDLog("Error in outbox on %s", Config->SMSID);
for (i=0;i<sms.Number;i++) {
- Service->AddSentSMSInfo(&sms, Config, Config->SMSID, i+1, false);
+ Service->AddSentSMSInfo(&sms, Config, Config->SMSID, i+1, SMSD_SEND_ERROR, -1);
}
Service->MoveSMS(&sms,Config, Config->SMSID, true,false);
return false;
}
+
if (!gshutdown) {
if (strcmp(Config->prevSMSID, Config->SMSID) == 0) {
Config->retries++;
@@ -346,7 +370,7 @@ bool SMSD_SendSMS(GSM_SMSDConfig *Config,GSM_SMSDService *Service)
strcpy(Config->prevSMSID, "");
WriteSMSDLog("Moved to errorbox: %s", Config->SMSID);
for (i=0;i<sms.Number;i++) {
- Service->AddSentSMSInfo(&sms, Config, Config->SMSID, i+1, false);
+ Service->AddSentSMSInfo(&sms, Config, Config->SMSID, i+1, SMSD_SEND_ERROR, -1);
}
Service->MoveSMS(&sms,Config, Config->SMSID, true,false);
return false;
@@ -355,14 +379,40 @@ bool SMSD_SendSMS(GSM_SMSDConfig *Config,GSM_SMSDService *Service)
Config->retries = 0;
strcpy(Config->prevSMSID, Config->SMSID);
}
- for (i=0;i<sms.Number;i++) {
- if (strcmp(Config->deliveryreport, "no") != 0) sms.SMS[i].PDU = SMS_Status_Report;
+ for (i=0;i<sms.Number;i++) {
+ if (sms.SMS[i].SMSC.Location == 1) {
+ if (Config->SMSC.Location == 0) {
+ Config->SMSC.Location = 1;
+ error = Phone->GetSMSC(&s,&Config->SMSC);
+ if (error!=ERR_NONE) {
+ WriteSMSDLog("Error getting SMSC from phone");
+ return false;
+ }
+
+ }
+ memcpy(&sms.SMS[i].SMSC,&Config->SMSC,sizeof(GSM_SMSC));
+ sms.SMS[i].SMSC.Location = 0;
+ if (Config->relativevalidity != -1) {
+ sms.SMS[i].SMSC.Validity.Format = SMS_Validity_RelativeFormat;
+ sms.SMS[i].SMSC.Validity.Relative = Config->relativevalidity;
+ }
+ }
+
+ if (Config->currdeliveryreport == 1) {
+ sms.SMS[i].PDU = SMS_Status_Report;
+ } else {
+ if ((strcmp(Config->deliveryreport, "no") != 0 && (Config->currdeliveryreport == -1))) sms.SMS[i].PDU = SMS_Status_Report;
+ }
+
error=Phone->SendSMS(&s, &sms.SMS[i]);
if (error!=ERR_NONE) {
+ Service->AddSentSMSInfo(&sms, Config, Config->SMSID, i+1, SMSD_SEND_SENDING_ERROR, -1);
WriteSMSDLog("Error sending SMS %s (%i): %s", Config->SMSID, error,print_error(error,s.di.df,s.msg));
return false;
}
- j=0;
+ Service->RefreshPhoneStatus(Config);
+ j = 0;
+ TPMR = -1;
SendingSMSStatus = ERR_TIMEOUT;
while (!gshutdown) {
GSM_GetCurrentDateTime (&Date);
@@ -373,23 +423,22 @@ bool SMSD_SendSMS(GSM_SMSDConfig *Config,GSM_SMSDService *Service)
GSM_ReadDevice(&s,true);
if (SendingSMSStatus != ERR_TIMEOUT) break;
}
+ Service->RefreshSendStatus(Config, Config->SMSID);
+ Service->RefreshPhoneStatus(Config);
if (SendingSMSStatus != ERR_TIMEOUT) break;
j++;
if (j>Config->sendtimeout) break;
}
if (SendingSMSStatus != ERR_NONE) {
+ Service->AddSentSMSInfo(&sms, Config, Config->SMSID, i+1, SMSD_SEND_SENDING_ERROR, TPMR);
WriteSMSDLog("Error getting send status of %s (%i): %s", Config->SMSID, SendingSMSStatus,print_error(SendingSMSStatus,s.di.df,s.msg));
return false;
}
- error = Service->AddSentSMSInfo(&sms, Config, Config->SMSID, i+1, true);
+ error = Service->AddSentSMSInfo(&sms, Config, Config->SMSID, i+1, SMSD_SEND_OK, TPMR);
if (error!=ERR_NONE) {
return false;
}
}
- while ((int)i<sms.Number-1) {
- Service->AddSentSMSInfo(&sms, Config, Config->SMSID, i+1, false);
- i++;
- }
strcpy(Config->prevSMSID, "");
if (Service->MoveSMS(&sms,Config, Config->SMSID, false, true) != ERR_NONE) {
Service->MoveSMS(&sms,Config, Config->SMSID, true, false);
@@ -403,7 +452,7 @@ void SMSDaemon(int argc, char *argv[])
int errors = 255, initerrors=0;
GSM_SMSDService *Service;
GSM_Error error;
- time_t time1;
+ time_t lastreceive, lastreset = 0;
GSM_SMSDConfig Config;
if (!strcmp(argv[2],"FILES")) {
@@ -417,7 +466,7 @@ void SMSDaemon(int argc, char *argv[])
exit(-1);
}
- SMSD_ReadConfig(argv[3], &Config, true);
+ SMSD_ReadConfig(argv[3], &Config, true, argv[2]);
error = Service->Init(&Config);
if (error!=ERR_NONE) {
@@ -428,7 +477,8 @@ void SMSDaemon(int argc, char *argv[])
signal(SIGTERM, interrupt);
fprintf(stderr,"Press Ctrl+C to stop the program ...\n");
- time1 = time(NULL);
+ lastreceive = time(NULL);
+ lastreset = time(NULL);
SendingSMSStatus = ERR_UNKNOWN;
while (!gshutdown) {
@@ -445,7 +495,27 @@ void SMSDaemon(int argc, char *argv[])
case ERR_NONE:
s.User.SendSMSStatus = SMSSendingSMSStatus;
Phone = s.Phone.Functions;
- errors = 0;
+ if (errors == 255) {
+ errors = 0;
+ s.Phone.Data.IMEI[0] = 0;
+ if (!(Phone->GetIMEI(&s))) {
+ errors++;
+ } else {
+ error = Service->InitAfterConnect(&Config);
+ if (error!=ERR_NONE) {
+ GSM_Terminate_SMSD("Stop GAMMU smsd (%i)", error, true, -1);
+ }
+ Phone->SetFastSMSSending(&s,true);
+ }
+ } else {
+ errors = 0;
+ }
+ if (initerrors > 3 || initerrors < 0) {
+ error=Phone->Reset(&s, false); /* soft reset */
+ WriteSMSDLog("Reset return code: %s (%i) ", error == ERR_NONE? "OK":"ERROR", error);
+ lastreset = time(NULL);
+ my_sleep(5000);
+ }
/* Marcin Wiacek: FIXME. To check */
// di = s.di;
break;
@@ -457,8 +527,8 @@ void SMSDaemon(int argc, char *argv[])
}
continue;
}
- if ((difftime(time(NULL), time1) >= Config.receivefrequency) || (SendingSMSStatus != ERR_NONE)) {
- time1 = time(NULL);
+ if ((difftime(time(NULL), lastreceive) >= Config.receivefrequency) || (SendingSMSStatus != ERR_NONE)) {
+ lastreceive = time(NULL);
if (!SMSD_CheckSecurity(&Config)) {
errors++;
@@ -468,13 +538,19 @@ void SMSDaemon(int argc, char *argv[])
initerrors = 0;
- if (!SMSD_CheckSMSStatus(&Config,Service)) {
+ if (!SMSD_CheckSMSStatus(&Config,Service)) { /* read all incoming SMS */
errors++;
continue;
} else errors=0;
+
+ if (Config.resetfrequency > 0 && difftime(time(NULL), lastreset) >= Config.resetfrequency) { /* time for preventive reset */
+ errors = 254; initerrors = -2;
+ continue;
+ }
}
if (!SMSD_SendSMS(&Config,Service)) continue;
}
+ Phone->SetFastSMSSending(&s,false);
GSM_Terminate_SMSD("Stop GAMMU smsd", 0, false, 0);
}
@@ -494,7 +570,7 @@ GSM_Error SMSDaemonSendSMS(char *service, char *filename, GSM_MultiSMSMessage *s
exit(-1);
}
- SMSD_ReadConfig(filename, &Config, false);
+ SMSD_ReadConfig(filename, &Config, false, service);
error = Service->Init(&Config);
if (error!=ERR_NONE) return ERR_UNKNOWN;