现有一个需求要求ui和app之间完成一次通信,采用消息的方式,代码如下:
1. APP层
appmsg.h
#ifndef _APPMSG_H_ #define _APPMSG_H_ int Notify_UI_CloseHandle(char * devpath); #endifappmsg.c
#include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <errPublic.h> #include "appmsg.h" #define MSG_ERROR -1 #define MSG_CL_KEY 0x100 #define MSG_SV_KEY 0x200 #define BUF_SIZE_MAX 64 #define TEK_MSG_TYPE 110 typedef struct _TEK_MSG_DATA_ { long mtype; char mtext[BUF_SIZE_MAX]; } TEK_MSG_DATA; static int sndmsg_to_ui(char *devpath); static int rcvmsg_from_ui(); /********************************************************************* **Function: Notify_UI_CloseHandle **Param: devpath: device path **Return:: 0:success, -1:fail **********************************************************************/ int Notify_UI_CloseHandle(char * devpath) { int ret = 0; if (NULL == devpath) { errLogPrintf(ERR_FILE_SYSTEM, ERR_ERROR, "Notify_UI_CloseHandle=>param is invalid\n"); return -1; } //Notify ui to close handler ret = sndmsg_to_ui(devpath); if (MSG_ERROR == ret) { return -1; } //Wait for ui completed close handler ret = rcvmsg_from_ui(); if (MSG_ERROR == ret) { return -1; } return 0; } /********************************************************************* **Function: sndmsg_to_app **Param: NONE **Return:: 0:success, -1:fail **********************************************************************/ int sndmsg_to_ui(char *devpath) { int ret = 0; int msgid = 0; long msgtype = 0; TEK_MSG_DATA data; //send message to notify app close handler completed msgid = msgget((key_t)MSG_SV_KEY, 0666 | IPC_CREAT); if (MSG_ERROR == msgid) { errLogPrintf(ERR_FILE_SYSTEM, ERR_ERROR, "sndmsg_to_ui=>Failed to create msgid\n"); return -1; } data.mtype = TEK_MSG_TYPE; memset(data.mtext, 0x00, sizeof(data.mtext)); memcpy(data.mtext, devpath, strlen(devpath)); ret = msgsnd(msgid, (void *)&data, strlen(devpath), IPC_NOWAIT); if (MSG_ERROR == ret) { errLogPrintf(ERR_FILE_SYSTEM, ERR_ERROR, "sndmsg_to_ui fail\n"); return -1; } return 0; } /********************************************************************* **Function: rcvmsg_from_app **Detail: rcv message from app, purpose to close all handler **Param: NONE **Return:: 0:success, -1:fail **********************************************************************/ int rcvmsg_from_ui() { int ret = 0; int msgid = 0; long msgtype = 0; TEK_MSG_DATA data; memset(&data, 0x00, sizeof(TEK_MSG_DATA)); msgid = msgget((key_t)MSG_CL_KEY, 0666 | IPC_CREAT); if (MSG_ERROR == msgid) { errLogPrintf(ERR_FILE_SYSTEM, ERR_ERROR, "rcvmsg_from_ui=>Failed to create msgid.\n"); return -1; } msgtype = TEK_MSG_TYPE; ret = msgrcv(msgid, (void *)&data, BUF_SIZE_MAX, msgtype, 0/*IPC_NOWAIT*/); if (MSG_ERROR == ret) { errLogPrintf(ERR_FILE_SYSTEM, ERR_ERROR,"rcvmsg_from_ui fail\n"); return -1; } //clear message msgctl(msgid, IPC_RMID, 0); debugLogPrintf(ERR_FILE_SYSTEM, ERR_ERROR, "rcvmsg_from_ui=>msgrcv: mtype=%d, mtext=%s\n", data.mtype, data.mtext); return 0; }2. ui层
umntoptPublic.cpp
#include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <pthread.h> #include <QDebug> #include <umntoptPublic.h> #define MSG_ERROR -1 #define MSG_CL_KEY 0x100 #define MSG_SV_KEY 0x200 #define BUF_SIZE_MAX 64 #define TEK_MSG_TYPE 110 //#ifdef __cplusplus //extern "C" { //#endif typedef struct _TEK_MSG_DATA_ { long mtype; char mtext[BUF_SIZE_MAX]; } TEK_MSG_DATA; static int rcvmsg_from_app(char *); static int sndmsg_to_app(); static pthread_t tid_close_file=0; void* thread_close_file(void * ); //static char dirBuf[50]={"\0"}; /********************************************************************* **Function: rcvmsg_from_app **Detail: rcv message from app, purpose to close all handler **Param: NONE **Return:: 0:success, -1:fail **********************************************************************/ int rcvmsg_from_app(char * dirBuf) { int ret = 0; int msgid = 0; long msgtype = 0; TEK_MSG_DATA data; memset(&data, 0x00, sizeof(TEK_MSG_DATA)); msgid = msgget((key_t)MSG_SV_KEY, 0666 | IPC_CREAT); if (MSG_ERROR == msgid) { qDebug()<<"rcvmsg_from_app=>Failed to create msgid"; return -1; } msgtype = TEK_MSG_TYPE; ret = msgrcv(msgid, (void *)&data, BUF_SIZE_MAX, msgtype, 0/*IPC_NOWAIT*/); if (MSG_ERROR == ret) { qDebug()<<"rcvmsg_from_app=>Failed to msgrcv"; return -1; } //clear message msgctl(msgid, IPC_RMID, 0); qDebug()<<"rcvmsg_from_app=>msgrcv: mtype="<<data.mtype<<" mtext="<<data.mtext; strncpy(dirBuf,data.mtext,BUF_SIZE_MAX); return 0; } /********************************************************************* **Function: sndmsg_to_app **Param: NONE **Return:: 0:success, -1:fail **********************************************************************/ int sndmsg_to_app() { int ret = 0; int msgid = 0; //long msgtype = 0; TEK_MSG_DATA data; //send message to notify app close handler completed msgid = msgget((key_t)MSG_CL_KEY, 0666 | IPC_CREAT); if (MSG_ERROR == msgid) { // printf("sndmsg_to_app=>Failed to create msgid.\n"); return -1; } data.mtype = TEK_MSG_TYPE; memcpy(data.mtext, "close handle completed", 22); ret = msgsnd(msgid, (void *)&data, 22, IPC_NOWAIT); if (MSG_ERROR == ret) { // printf("sndmsg_to_app=>Failed to msgsnd\n"); return -1; } return 0; } int init_close_file() { int ret; //tid_recv ret =pthread_create(&tid_close_file, NULL, thread_close_file, NULL); if (!ret) { qDebug()<<"closeFile thread start successfully. \n"; } else { qDebug()<<"ret is "<<ret; qDebug()<<"can't create thread: "<<strerror(ret); } return 0; } void* thread_close_file(void * ) { int ret = 0; int brun = 1; char buffer[BUF_SIZE_MAX]={"\0"}; while(brun) { qDebug()<<"enter thread_close_file"; //Wait for notify from app ret = rcvmsg_from_app(buffer); if(MSG_ERROR == ret) { qDebug()<<"receive msg error"; } //Do close handler //T.D.B //load .so qDebug()<<"close directory file is "<<buffer; umntCloseFile(buffer); //Notify app to close handler completed ret = sndmsg_to_app(); if (MSG_ERROR == ret) { qDebug()<<"send msg error"; } qDebug()<<"has closed"<<buffer <<"and sent msg to ulpp"; } return NULL; }