📄 daemonpart.c
字号:
/**************************************************************$Log: **************************************************************///----------------------------------------// Head files//----------------------------------------#include "3GDaemon.h"//----------------------------------------// Global variates//----------------------------------------char gBufferIn[BUF_MAX_NUM][BUF_MAX_SIZE];char gBufferOut[BUF_MAX_NUM][BUF_MAX_SIZE ];int inIndex = ZERO_LEN;int outIndex = ZERO_LEN;GMainLoop *mainLoop; pthread_mutex_t inMutex=PTHREAD_MUTEX_INITIALIZER;pthread_mutex_t outMutex=PTHREAD_MUTEX_INITIALIZER;FILE *logfile;//----------------------------------------// Functions declare//----------------------------------------static DBusHandlerResult ParseSignal (DBusConnection *connection, DBusMessage *message, void *userData);int SendSignal(DBusConnection* conn, char* sigvalue);void signal_handler(int sig){ switch(sig) { case SIGHUP: log_message(logfile, "hangup signal catched"); break; case SIGTERM: log_message(logfile, "terminate signal catched"); fclose(logfile); exit(0); break; }}void daemonize(){ int i,lfp; char str[10]; if(getppid()==1) return; /* already a daemon */ i=fork(); if (i<0) exit(1); /* fork error */ if (i>0) exit(0); /* parent exits */ /* child (daemon) continues */ setsid(); /* obtain a new process group */ for (i=getdtablesize();i>=0;--i) close(i); /* close all descriptors */ i=open("/dev/null",O_RDWR); dup(i); dup(i); /* handle standart I/O */ umask(027); /* set newly created file permissions */ //chdir(RUNNING_DIR); /* change running directory */ lfp=open(LOCK_FILE,O_RDWR|O_CREAT,0640); if (lfp<0) exit(1); /* can not open */ if (lockf(lfp,F_TLOCK,0)<0) exit(0); /* can not lock */ /* first instance continues */ sprintf(str,"%d\n",getpid()); write(lfp,str,strlen(str)); /* record pid to lockfile *///Debug close(lfp); signal(SIGCHLD,SIG_IGN); /* ignore child */ signal(SIGTSTP,SIG_IGN); /* ignore tty signals */ signal(SIGTTOU,SIG_IGN); signal(SIGTTIN,SIG_IGN); signal(SIGHUP,signal_handler); /* catch hangup signal */ signal(SIGTERM,signal_handler); /* catch kill signal */ signal(10, signal_handler);}/*****************************************Function: ListenFromDBusDescription: Parameter:Return:Remark:******************************************/int ListenFromDBus(void){ log_message(logfile, "ListenFromDBus thread start!\n"); DBusConnection *bus; DBusError error; char messageBuf[BUF_MAX_SIZE]; dbus_error_init(&error); bus = dbus_bus_get(DBUS_BUS_SESSION, &error); if (!bus) { sprintf(messageBuf, "listen Failed to connect to the D-BUS daemon: %s", error.message); log_message(logfile, messageBuf); dbus_error_free(&error); return; } dbus_connection_setup_with_g_main(bus, NULL); // listening to messages from all objects as no path is specified dbus_bus_add_match(bus, "type='signal',interface='com.compal.AppTo3G.Signal'", NULL); dbus_connection_add_filter(bus, ParseSignal, mainLoop, NULL);}static DBusHandlerResult ParseSignal(DBusConnection *connection, DBusMessage *message, void *userData){ // A signal on the dbus if (dbus_message_is_signal (message, "com.compal.AppTo3G.Signal", "Command")) { DBusError error; char messageBuf[BUF_MAX_SIZE]; char *rec; dbus_error_init (&error); if (dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &rec, DBUS_TYPE_INVALID)) { if (inIndex < BUF_MAX_NUM ) { pthread_mutex_lock(&inMutex); strcpy(gBufferIn[inIndex],rec); //g_print(gBufferIn[inIndex]); inIndex++; pthread_mutex_unlock(&inMutex); } else log_message(logfile, "gBufferIn overflowed!\n"); } else { sprintf(messageBuf,"Signal received, but error getting message: %s\n", error.message); log_message(logfile, messageBuf); dbus_error_free (&error); } return DBUS_HANDLER_RESULT_HANDLED; } return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;}int SendSignal(DBusConnection* conn, char* sigvalue){ DBusMessage* msg; DBusMessageIter args; int ret; dbus_uint32_t serial = ZERO_LEN; // create a signal & check for errors msg = dbus_message_new_signal("/com/compal/3G/Response", // object name of the signal "com.compal.ThreeGtoApp.Signal", // interface name of the signal "Response"); // name of the signal if (NULL == msg) { log_message(logfile, "SendSignal Error: Message is Null\n"); return 1; } // append arguments onto signal dbus_message_iter_init_append(msg, &args); if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) { log_message(logfile, "SendSignal Error: dbus_message_iter_append_basic() Out Of Memory!\n"); return 1; } // send the message and flush the connection if (!dbus_connection_send(conn, msg, &serial)) { log_message(logfile, "SendSignal Error: dbus_connection_send() Out Of Memory!\n"); return 1; } // free the message dbus_message_unref(msg);}/*****************************************Function: SendToDBusDescription: Parameter:Return:Remark:******************************************/void *SendToDBus(void* data){ log_message(logfile, "SendToDBus thread start!\n"); DBusConnection* conn; DBusError err; char messageBuf[BUF_MAX_SIZE]; // initialise the error value dbus_error_init(&err); // connect to the DBUS system bus, and check for errors >> join in the pipe conn = dbus_bus_get(DBUS_BUS_SESSION, &err); if (dbus_error_is_set(&err)) { sprintf(messageBuf, "Connection Error (%s)\n", err.message); log_message(logfile, messageBuf); dbus_error_free(&err); } if (NULL == conn) { return; } while(1) { if(outIndex > ZERO_LEN) { pthread_mutex_lock(&outMutex); outIndex--; SendSignal(conn, gBufferOut[outIndex]); pthread_mutex_unlock(&outMutex); } usleep(DELAY_FOR_OTHER_TIME); }}/*****************************************Function: ReadFromDriverDescription: Parameter:Return:Remark:******************************************/void *ReadWriteDriver(void* data){ log_message(logfile, "ReadWriteDriver thread start!\n"); while (1) { if (inIndex > ZERO_LEN) { if (outIndex < BUF_MAX_NUM) { //printf("ReadWriteDriver = %s\n",commandQueue[gBufferIn[inIndex]]); pthread_mutex_lock(&inMutex); inIndex--; //send at command to 3G module SendAtCommand(gBufferIn[inIndex]); pthread_mutex_unlock(&inMutex); usleep(DELAY_FOR_READ_TIME); pthread_mutex_lock(&outMutex); //read response from 3G module //strcpy(gBufferOut[outIndex],"It is testing!"); //strcpy(gBufferOut[outIndex],gBufferIn[inIndex]); if (ReceiveResponse(gBufferOut[outIndex]) == SUCCEED) { outIndex++; } pthread_mutex_unlock(&outMutex); } else log_message(logfile, "gBufferOut overflowed!\n"); } QueryCallMesg(); usleep(DELAY_FOR_OTHER_TIME); } return;}/*****************************************Function: mainDescription: main function of 3G daemonParameter: argc[input]: number of parameter argv[input]: pointer of parameterReturn:Remark:******************************************/int main (int argc, char **argv){ pthread_t send; pthread_t ctrl; daemonize(); logfile = fopen(LOG_FILE,"w+"); log_message(logfile,"3G Daemon start"); if (OpenCom(DEVICE_NAME) != SUCCEED) // open 3G device { log_message(logfile, "Open device failed! Daemon stop!\n"); return FAIL; } mainLoop = g_main_loop_new (NULL, FALSE); // Dbus send thread pthread_create(&send, NULL, SendToDBus, NULL); // Driver control thread pthread_create(&ctrl, NULL, ReadWriteDriver, NULL); // Listen thread sleep(1); ListenFromDBus(); g_main_loop_run(mainLoop); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -