2011-12-14 17:52:13 +04:00
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/msg.h>
|
2012-04-06 13:12:42 +04:00
|
|
|
#include <pthread.h>
|
2011-12-14 17:52:13 +04:00
|
|
|
|
|
|
|
#include "yasnd.h"
|
2012-03-22 17:37:01 +04:00
|
|
|
#include "yasnd-gammu.h"
|
2012-03-22 17:27:26 +04:00
|
|
|
#include "yasnd-log.h"
|
2011-12-14 17:52:13 +04:00
|
|
|
|
2012-04-06 13:12:42 +04:00
|
|
|
pthread_t ipc_main_handle;
|
2012-04-06 13:25:22 +04:00
|
|
|
pthread_t ipc_sms_read_handle; // handle of thread, that checks for new incoming SMS
|
2011-12-19 11:08:19 +04:00
|
|
|
int qid=-1; // IPC queue id
|
2011-12-14 17:52:13 +04:00
|
|
|
|
|
|
|
int open_queue()
|
|
|
|
{
|
|
|
|
key_t keyval=ftok(".",'m');
|
|
|
|
int qid;
|
|
|
|
if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
|
|
|
|
{
|
|
|
|
log_event("Error opening/creating IPC message queue");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return qid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int send_message(int qid, mymsgbuf* qbuf)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
/* The length is essentially the size of the structure minus sizeof(mtype) */
|
|
|
|
int length = sizeof(mymsgbuf) - sizeof(long);
|
|
|
|
|
|
|
|
if((result = msgsnd( qid, qbuf, length, 0)) == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_message(int qid, mymsgbuf* qbuf, long type)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
int length = sizeof(mymsgbuf) - sizeof(long);
|
|
|
|
if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int remove_queue( int qid )
|
|
|
|
{
|
|
|
|
if( msgctl(qid, IPC_RMID, 0) == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-06 13:12:42 +04:00
|
|
|
void* ipc_queue_loop(void* param)
|
2011-12-14 17:52:13 +04:00
|
|
|
{
|
|
|
|
mymsgbuf qbuf;
|
|
|
|
while(1)
|
|
|
|
{
|
2011-12-19 18:35:14 +04:00
|
|
|
int result=read_message(qid, &qbuf, 0);
|
2011-12-14 17:52:13 +04:00
|
|
|
if (result==-1)
|
|
|
|
{
|
|
|
|
log_event("Error: finishing IPC loop process...");
|
2012-04-06 13:12:42 +04:00
|
|
|
return (void*)1;
|
2011-12-14 17:52:13 +04:00
|
|
|
}
|
|
|
|
char dbg_txt[150];
|
|
|
|
sprintf(dbg_txt,"Read IPC message - Type: %ld Text: %s", qbuf.mtype, qbuf.mtext);
|
|
|
|
log_debug(dbg_txt,DEBUG_ALL);
|
2011-12-20 17:05:53 +04:00
|
|
|
if (qbuf.mtype==SMS_RECEIVE_TYPE && sms_enable)
|
2011-12-20 17:02:34 +04:00
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
log_event("Error: can not fork child :(");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// If pid == 0, then we are in the child
|
|
|
|
if (pid == 0) {
|
|
|
|
gammu_read_sms();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
// wait for SMS reading thread
|
|
|
|
// STUB: need to react on status, returned by thread
|
|
|
|
waitpid(pid,NULL,0);
|
|
|
|
}
|
2011-12-20 17:05:53 +04:00
|
|
|
if (qbuf.mtype==SMS_SEND_TYPE && sms_enable)
|
2011-12-19 18:35:14 +04:00
|
|
|
{
|
|
|
|
log_debug(qbuf.mtext,DEBUG_BASE);
|
2011-12-20 13:23:13 +04:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
log_event("Error: can not fork child :(");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// If pid == 0, then we are in the child
|
|
|
|
if (pid == 0) {
|
|
|
|
gammu_send_sms(qbuf.mtext);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
// wait for SMS sending thread
|
|
|
|
// STUB: need to react on status, returned by thread
|
|
|
|
waitpid(pid,NULL,0);
|
2011-12-19 18:35:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-06 13:25:22 +04:00
|
|
|
void* ipc_sms_reading_loop(void* param)
|
2011-12-19 18:35:14 +04:00
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
sleep(30); /* wait 30 seconds */
|
2011-12-20 17:02:34 +04:00
|
|
|
mymsgbuf qbuf;
|
|
|
|
qbuf.mtype=SMS_RECEIVE_TYPE;
|
|
|
|
qbuf.mtext[0]=(char)0;
|
|
|
|
send_message(qid,&qbuf);
|
2011-12-14 17:52:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipc_init()
|
|
|
|
{
|
|
|
|
qid = open_queue();
|
2012-04-06 13:12:42 +04:00
|
|
|
if(pthread_create(&ipc_main_handle,NULL,ipc_queue_loop, NULL) != 0)
|
|
|
|
{
|
|
|
|
log_event("Main IPC event thread creation failed");
|
|
|
|
}
|
2011-12-19 18:35:14 +04:00
|
|
|
// start loop, that handles new incoming SMS
|
2012-04-06 13:25:22 +04:00
|
|
|
if(pthread_create(&ipc_sms_read_handle,NULL,ipc_sms_reading_loop, NULL) != 0)
|
|
|
|
{
|
|
|
|
log_event("Failed to create thread for incoming SMS parsing");
|
|
|
|
}
|
2011-12-19 18:35:14 +04:00
|
|
|
//
|
2011-12-14 17:52:13 +04:00
|
|
|
}
|
2011-12-14 18:13:42 +04:00
|
|
|
|
|
|
|
void ipc_free()
|
|
|
|
{
|
2012-04-06 13:12:42 +04:00
|
|
|
pthread_cancel(ipc_main_handle);
|
2012-04-06 13:25:22 +04:00
|
|
|
pthread_cancel(ipc_sms_read_handle);
|
2011-12-14 18:13:42 +04:00
|
|
|
remove_queue(qid);
|
|
|
|
}
|