⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flowhdl-msg.c

📁 通过消息队列让几个进程间支持通信
💻 C
字号:
#include <unistd.h>#include <fcntl.h>		// for O_RDWR,O_RDONLY,o_WRONLY#include <sys/types.h>#include <sys/msg.h>#define JDEBUG#include "jdebug.h"#include "msghdr.h"#define DSPFILE "/dev/dspHPI"/* define buffer length */#define	NLEN	2048#define GLEN	2048//#define DLEN (NLEN > GLEN?NLEN:GLEN)#define DLEN	NLEN //extern int state;int openfifo(char *file,mode_t mode){	int retv;	retv = open(file, mode);	if (retv < 0)	{		printf("cant open fifo:%s\n",file);		exit(0);	}	return retv;}int creatqueue(key_t key){	int retv;	retv = msgget(key, IPC_CREAT|0666);	if (retv < 0)	{		printf("can not create queue:%d\n",key);		exit(0);	}	return retv;}/* do packeting operation for arm<->dsp interface 	if the packet format changes, here should be modified too.*/void packet(char *buf, int *length, int type){/*  0   2    4     5	|len|type|octet|...data...|pad|	at least should add 5 octets	if data_len is odd, add 5 octets, then become even	if data_len is even, add 5 octets, then odd, should pad 1 octet	consideration that dsp can just handle 16bit-based*/	int len = *length;	if (len%2)	/* odd */	{		len += 1;		buf[4] = 1;		/* do padding at the end */		buf[len-1] = 0; 	}	else	{		buf[4] = 0;		/* no padding needed*/	}		buf[0] = len & 0xff;	buf[1] = (len>>8) & 0xff;	buf[2] = type & 0xff;	buf[3] = (type>>8) & 0xff;		*length = len;	return;}int main(){	int qdni,qdno;	int qdgi,qdgo;	int fddsp;	msg_t nimsg,gimsg;	msg_t omsg;	char dspbuf[DLEN];		int nilen,nolen,gilen,golen;	int dsplen, tmp, datalen,datatype;	int retv;/*		fdni = openfifo(NICINCOME,O_RDONLY); 	fdno = openfifo(NICOUTPUT,O_WRONLY);	fdgi = openfifo(GUIINCOME,O_RDONLY);	fdgo = openfifo(GUIOUTPUT,O_WRONLY);*/	qdni = creatqueue(NICIKEY);		qdno = creatqueue(NICOKEY);		qdgi = creatqueue(GUIIKEY);		qdgo = creatqueue(GUIOKEY);		fddsp = open(DSPFILE,O_RDWR);	if (fddsp < 0)	{		printf("cant open %s,pls check it. stop\n",DSPFILE);		exit(0);	}	Jdebug("flowhdl:creat 4 msg queues and dsp finished!\n");		/* Xibuf[0-1] reserved for length */	/* Xibuf[2-3] reserved for types */	/* Xibuf[4]   indicates pad or not */	/* type=0x1111 for nic data(web service)	   type=0x2222 for gui data(qt interface)	   type=0x3333 for reconfig data(dsp reconfig)	*/	if (fork() == 0)	// child process, read dsp and write Xoutput fifo	{		Jdebug("flowhdl chd1: r dsp and w OUT msg queues\n");		while(1)		{			dsplen = read(fddsp, dspbuf, DLEN);			//Jdebug("flowhdl chd1: read dsp return %d\n",dsplen);			if (dsplen)	// read dsp valid			{				datalen = dspbuf[0] + (dspbuf[1] << 8);				if (dsplen != datalen)					{							printf("dsplen != tmp. oddy!still continue,!!!dsplen-datalen=!!!%d\n",dsplen-datalen);						continue;					}				datatype = dspbuf[2] + (dspbuf[3] << 8);				if (dspbuf[4])	/* dspbuf[4]==1 pading */					dsplen -= 1;				memcpy(omsg.data, dspbuf, dsplen);				//printf("!!!!!!state=%d!!!!!!!\n",state);				switch(datatype)				{					case 0x1111: /* to nic */					//	tmp = write(fdno, dspbuf+5, dsplen);						omsg.type = 0x1111;						retv = msgsnd(qdno, &omsg,dsplen+LONGSIZE,IPC_NOWAIT);						if (!retv)						Jdebug("flowhdl chd1:write NicOut Queue %d\n",dsplen+LONGSIZE);						else						Jdebug("####flowhdl chd1: write NicOUT queue failed\n");						break;					case 0x2222: /* to gui */						omsg.type = 0x2222;						msgsnd(qdgo, &omsg, dsplen+LONGSIZE,0);						break;					case 0x3333: /* reconfig return,to gui? */						omsg.type = 0x3333;						msgsnd(qdgo, &omsg, dsplen+LONGSIZE,0);						break;					default:						printf("data type undefined!drop data\n");						break;				}							}		}	}	else	{ 	/* creat process or thread to read each incoming fifo */		if (fork() == 0)	//another child process,read nic incoming data		{				Jdebug("flowhdl child2: r nic and w dsp\n");			while(1)			{				nilen = msgrcv(qdni, &nimsg, NLEN, 0,0);				Jdebug("flowhdl child2: read NicInQueue %d\n",nilen);				if (nilen > 0)		/* read valid */				{					nilen = nilen - LONGSIZE;					/* nimsg len is including length+type+flag */					packet(nimsg.data, &nilen, 0x1111);					tmp = write(fddsp, nimsg.data, nilen);					Jdebug("flowhdl child2:write dsp return %d\n",tmp);				}					}		}		else		{			Jdebug("flowhdl parent: r gui and w dsp\n");			while(1)		//parent read gui incoming data			{				gilen = msgrcv(qdgi, &gimsg, GLEN,0,0);				Jdebug("flowhdl parent: read GuiIncome %d\n",gilen);				if (gilen > 0)				{					gilen -= LONGSIZE;					packet(gimsg.data, &gilen, 0x2222);					tmp = write(fddsp, gimsg.data, gilen);					Jdebug("flowhdl parent:write dsp return %d\n",tmp);				}			}		}	}		msgctl(qdni,IPC_RMID, 0);	msgctl(qdno,IPC_RMID, 0);	msgctl(qdgi,IPC_RMID, 0);	msgctl(qdgo,IPC_RMID, 0);	close(fddsp);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -