#if defined(_MSC_VER)||defined(__MINGW32__) # define _CRT_SECURE_NO_WARNINGS 1 # include #endif #if defined(__linux__) # include # include # include # define INVALID_HANDLE_VALUE -1 #endif #include #include "pipeproto.h" #include "mypipe.h" /* Data per DLL load */ /* The following structure is completely specific for your DLL. In this example several things are stored: */ typedef struct localData { /* Handle to pipe to which data is written */ mypipe_t handle; /* Range of ChannelIDs, which should be processed */ unsigned int from,to; /* Buffer for data which should be written to DLL */ unsigned int h; unsigned char wbuf[8100]; } localDataT,*localDataP; enum { DECITM_D8 = 0x1, DECITM_D16 = 0x2, DECITM_D32 = 0x3, }; #define DECITM_TYPE(msg0) ((msg0)&0x7) static const char *MsgTypes[] = { " DUMMY", " D8", " D16", " D32" }; static void process(SPipeProtoInfo *info,localDataP data,unsigned char *buffer,int len) { uint32_t chn,d; int t,h; unsigned char *b=buffer; char *outb,*outs; if (buffer==NULL) { /* If the processing function is called with an empty buffer, it is an indication, that the data should be flushed */ if (data->handle != INVALID_HANDLE_VALUE) mypipe_write(data->handle,data->wbuf,data->h); data->h=0; return; } b=buffer; /* b[0..7] is timestamp */ /* get type of message from b[8] */ t=DECITM_TYPE(b[8]); /* get channel from b[9] */ chn=b[9]; /* filter channels */ if (chnfrom || chn>data->to) return; h=data->h; outb=(char *)(data->wbuf+h); if (t>= sizeof(MsgTypes)/sizeof(const char *)) { outb+=sprintf(outb,"Illegal message type 0x%x\n",t); } else { SPipeProtoCycleInfo cycle; outs=outb; outb+=sprintf(outb,"%s chn 0x%02x data ",MsgTypes[t],chn); switch(t) { case DECITM_D8: outb+=sprintf(outb,"0x%02x",b[10]); cycle.type = PIPE_LOG_TYPE_WRITE|PIPE_LOG_TYPE_WITHADDRESS|PIPE_LOG_TYPE_WITHDATA; cycle.address = 0xE0000000+(chn<<2); cycle.datasize = 1; cycle.data[0] = b[10]; PIPE_LogCycle(info,b,&cycle); break; case DECITM_D16: d=b[11];d<<=8; d|=b[10]; outb+=sprintf(outb,"0x%04x",d); cycle.type = PIPE_LOG_TYPE_WRITE|PIPE_LOG_TYPE_WITHADDRESS|PIPE_LOG_TYPE_WITHDATA; cycle.address = 0xE0000000+(chn<<2); cycle.datasize = 2; cycle.data[0] = b[10]; cycle.data[1] = b[11]; PIPE_LogCycle(info,b,&cycle); break; case DECITM_D32: d=b[13];d<<=8; d|=b[12];d<<=8; d|=b[11];d<<=8; d|=b[10]; outb+=sprintf(outb,"0x%08x",d); cycle.type = PIPE_LOG_TYPE_WRITE|PIPE_LOG_TYPE_WITHADDRESS|PIPE_LOG_TYPE_WITHDATA; cycle.address = 0xE0000000+(chn<<2); cycle.datasize = 4; cycle.data[0] = b[10]; cycle.data[1] = b[11]; cycle.data[2] = b[12]; cycle.data[3] = b[13]; PIPE_LogCycle(info,b,&cycle); break; default: ; } PIPE_LogCustom(info,b,PIPE_LOG_TYPE_STRING,(uint8_t *)outs,(int)(outb - outs)); outb+=sprintf(outb,"\n"); } h = (int)(outb - ((char *)data->wbuf)); if (h >= 7168) { if (data->handle != INVALID_HANDLE_VALUE) mypipe_write(data->handle,data->wbuf,h); h=0; } data->h=h; } /* called when DLL is unloaded * Parameters * info : Structure for calling functions inside T32 * data : Pointer to structure which holds local data for this DLL. */ static void exitDll(SPipeProtoInfo *info,localDataP data) { mypipe_t handle; handle = data->handle; /* close pipe */ if (handle!=INVALID_HANDLE_VALUE) mypipe_close(handle); /* Free data, which was allocated int "PIPE_Interface" */ free(data); } /* * Parameters * info : Structure for calling functions inside T32 * argc : numbers of parameters stored in "argv" * argv : array of pointers to "NUL" terminated command line parameters * * This function should allocate space to store local configuration data * for one DLL load. * It additionally should do all initialization which is necessary. */ int PIPE_Init(SPipeProtoInfo *info,int argc, char **argv) { mypipe_t handle; char pipeName[256]; /* char command[1000]; */ localDataP localdata; uint32_t from,to; handle=INVALID_HANDLE_VALUE; if (argc!=2 && argc!=4) { PIPE_Puts(info,"usage: pproto [ ]"); return 1; } #if defined(_MSC_VER)||defined(__MINGW32__) /* extract pipe name from command line parameters */ sprintf(pipeName,"//./pipe/%s",argv[1]); #endif #if defined(__linux__) /* extract pipe name from command line parameters */ sprintf(pipeName,"/tmp/%s",argv[1]); #endif /* extract range from command line parameters, if a range was given */ from=0; to=0xFFFFFFFF; if (argc==4) { from=strtoul(argv[2],NULL,0); to=strtoul(argv[3],NULL,0); } /* Start application which opens and reads from pipe */ /* sprintf(command,"os ./pread.exe //./pipe/%s",argv[1]); PIPE_Command(info, command); Sleep(500); */ /* Open PIPE for writing data to it */ if (mypipe_open_write(&handle,pipeName)) { PIPE_Printf(info,"Can not open %s for writing",pipeName); // return 2; } else PIPE_Printf(info,"Opened pipe %s",pipeName); /* create data set for this invocation of the DLL and store configuration data into it. */ localdata=(localDataP)malloc(sizeof(localDataT)); localdata->handle=handle; localdata->from=from; localdata->to=to; localdata->h=0; /* Register callbacks, use "data" as localdata for the callbacks */ /* * Parameters for PIPE_RegisterCallback: * info : Pointer to "SPipeProtoInfo" structure * type : Type of function to register * callback : Pointer to function which will be called. * flags : optional flags (currently always 0). */ PIPE_RegisterCallback(info, PIPE_PROCESS_CALLBACK, process, localdata, 0); PIPE_RegisterCallback(info, PIPE_EXIT_CALLBACK , exitDll, localdata, 0); return 0; }