📄 dbg_user.c
字号:
/*************************************************************************** dbg_user.c - User setup-routines for dbg ------------------- begin : 2003 authors : Linus Gasser emails : linus.gasser@epfl.ch ***************************************************************************//*************************************************************************** Changes ------- date - name - description 03-08-07 - ineiti - start **************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#ifdef USER_SPACE/** * sets up two fifos: * 0) command * 1) data kernel->user */#define DBG_LVL 0#include <stdio.h>#include <stdlib.h>#include <dirent.h>#include <sys/types.h>#include <sys/stat.h>#include "std.h"#include "system.h"#include "debugging.h"#include "sdb.h"#include "cdb.h"#include "dbg.h"#include "dbg_common.h"struct thread thr_cmd, thr_reply;pthread_mutex_t fifo_mutex = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t fifo_cond = PTHREAD_COND_INITIALIZER;void *fifo_data;uint fifo_len;int fifo_quit = 0;void fifo_data_reply( FILE *f ) { char size[16]; int pos, i, read; PR_DBG( 4, "Data-reply got called for buffer at %p with len %i\n", fifo_data, fifo_len ); i = snprintf( size, 16, "%10i\n", fifo_len ); fwrite( size, 1, i, f ); // fflush( f ); if ( fifo_len > 0 ) { for ( pos = 0; pos < fifo_len; pos += read ) { read = fwrite( fifo_data + pos, 1, min( fifo_len - pos, FIFO_SIZE ), f ); PR_DBG( 4, "Pos: %i, Read: %i\n", pos, read ); } fflush( f ); swr_free( fifo_data ); fifo_data = NULL; }}void *fifo_command( void *arg ) { FILE *f_cmd, *f_reply; char *base = arg, *path; char *buf; int size, size_cmd; // Setup the two fifos path = swr_malloc( 256 ); snprintf( path, 256, "%s/fifo_cmd", base ); PR_DBG( 4, "Initialising fifo_command at %s\n", path ); f_cmd = fopen( path, "r" ); snprintf( path, 256, "%s/fifo_reply", base ); PR_DBG( 4, "Initialising fifo_reply at %s\n", path ); f_reply = fopen( path, "w" ); swr_free( path ); buf = swr_malloc( FIFO_SIZE ); while ( !fifo_quit ) { // First read the 2-digit decimal size + LF size = fread( buf, 1, 3, f_cmd ); if ( size > 0 ) { buf[ size ] = 0; sscanf( buf, "%i", &size_cmd ); PR_DBG( 4, "Going to read a command of size %i\n", size_cmd ); size = fread( buf, 1, size_cmd, f_cmd ); buf[ size ] = 0; if ( size != size_cmd ) { PR_DBG( 0, "Strange, read bytes (%i) don't match cmd-size (%i)\n", size, size_cmd ); continue; } PR_DBG( 4, "Got cmd (%i bytes): %s\n", size, buf ); parse_command( buf ); fifo_data_reply( f_reply ); PR_DBG( 4, "Sent reply\n" ); } else { usleep( 100000 ); } } PR_DBG( 4, "fifo_cmd end\n" ); return NULL;}int setup_fifos() { char *path, *base = NULL, user[32]; int i; DIR *d; strcpy( user, getenv( "USER" ) ); // Search for an empty directory path = swr_malloc( 256 ); // Check for /tmp/$(USER) snprintf( path, 256, "/tmp/%s", user ); if ( ( d = opendir( path ) ) ) { closedir( d ); } else { mkdir( path, 0700 ); } // And search for first free entry for ( i=0; !base; i++ ) { snprintf( path, 256, "/tmp/%s/sradio.%i", user, i ); if ( ( d = opendir( path ) ) ) { closedir( d ); } else { base = path; mkdir( base, 0700 ); } } // Make a command-fifo path = swr_malloc( 256 ); snprintf( path, 256, "%s/fifo_cmd", base ); mkfifo( path, 0777 ); // Make a reply-fifo snprintf( path, 256, "%s/fifo_reply", base ); mkfifo( path, 0777 ); swr_free( path ); // fifo_command is responsible for cleaning up base swr_thread_init( &thr_cmd, fifo_command, base ); PR_DBG( 1, "Thread initialised\n" ); return 0;}int destroy_fifos() { PR_DBG( 4, "Going to delete the FIFOs\n" ); pthread_cancel( thr_cmd.thread ); pthread_cancel( thr_reply.thread ); swr_thread_free( &thr_cmd, NULL ); swr_thread_free( &thr_reply, NULL ); return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -