📄 source.c
字号:
/*************************************************************************** source.c - RTLinux kernel module for a source ------------------- begin : 2002 authors : Linus Gasser emails : linus.gasser@epfl.ch ***************************************************************************//*************************************************************************** Changes ------- date - name - description 02-09-20 - ineiti- begin 03/05/26 - ineiti - added a complex-output 04/03/06 - ineiti - adjusted description **************************************************************************//*************************************************************************** * * * 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. * * * ***************************************************************************//** * This module has two outputs: one for sending a pre-defined string (that * can be adjusted), the other for sending SYMBOL_COMPLEX signals. Depending * on config->complex_type, it outputs either a stream of zeroes, a given * data, or config->complex_type * ( 1 + I ). */#include "spc.h"#define DBG_LVL 0typedef struct { // The string outputted char str[128]; // Ne vous conformez pas a l'Esprit de ce monde // The kind of complex: // 0 - all zeroes, 1 - data, 2.. this amplitude int complex_type; // 0 // The data to send void *data; // NULL // The length of the data to send int len; // 0}config_t;typedef struct { // The total number of bytes/symbols sent int tot_out;}stats_t;typedef struct {}private_t;/** * The initialisation part that is only called the first time this module * is instantiated. */int spc_init( swr_sdb_t *context ) { stats_t *stats; config_t *config; MOD_INC_USE_COUNT; swr_sdb_get_stats_struct( context->id, (void**)&stats ); swr_sdb_get_config_struct( context->id, (void**)&config ); stats->tot_out = 0; strcpy( config->str, "Ne vous conformez pas a l'Esprit de ce monde!" ); config->complex_type = 0; config->data = 0; config->len = 0; size_out(0) = 0; PR_DBG( 1, "Initialised source\n" ); swr_sdb_free_config_struct( context->id, (void**)&config ); swr_sdb_free_stats_struct( context->id, (void**)&stats ); return 0;}/** * Every time somebody from the outside changes a configuration value, * this function is called just before the working-function */int spc_reconfig( swr_sdb_t *context ) { config_t *config; int ret = 0; swr_sdb_get_config_struct( context->id, (void**)&config ); PR_DBG( 4, "Reconfig from config, str = %s\n", config->str ); swr_sdb_free_config_struct( context->id, (void**)&config ); return ret;}/** * The actual working function */int spc_pdata( swr_sdb_t *context ) { config_t *config; stats_t *stats; U8 *out; SYMBOL_COMPLEX *out_complex, *data; int i, sent = 0; swr_sdb_get_config_struct( context->id, (void**)&config ); if ( size_out(0) ) { // Have some easy definitions out = buffer_out( 0 ); sent += size_out(0); // Output our string strncpy( out, config->str, min( size_out(0), 128 ) ); PR_DBG( 2, "Sent %i bytes: %s\n", size_out(0), out ); } // Output the complex-data if ( size_out(1) ) { PR_DBG( 2, "Outputting %i complex-symbols\n", size_out(1) ); out_complex = buffer_out(1); data = config->data; for ( i=0; i<size_out(1); i++ ) { switch( config->complex_type ) { case 1: out_complex[i] = data[ i % config->len ]; break; default: out_complex[i].imag = out_complex[i].real = config->complex_type; break; } } sent += size_out(1); } swr_sdb_free_config_struct( context->id, (void**)&config ); // Update the stats swr_sdb_get_stats_struct( context->id, (void**)&stats ); stats->tot_out += sent; swr_sdb_free_stats_struct( context->id, (void**)&stats ); return 0;}int spc_custom_msg(swr_sdb_t *context, swr_usr_msg_t* msg_data, swr_msgq ret ) { spc_pdata( context ); return 0;}int spc_finalize( swr_sdb_t *context ) { MOD_DEC_USE_COUNT; return 0;}swr_spc_id_t spc_id;/** * This function is called upon "insmod" and is used to register the * different parts of the module to the SPM. */int spc_module_init(void) { swr_spc_desc_t *desc; // Get a description-part from SPM // Nbr_input_ports, Nbr_output_ports, Nbr_config_values, Nbr_stats_values desc = swr_spc_get_new_desc( 0, 2, 4, 1 ); if ( !desc ) { PR_DBG( 0, "Can't initialise the module. This is bad!\n" ); return -1; } // Define the different parts of config, stats and io-ports UM_CONFIG_STRING128( "source_str" ); UM_CONFIG_INT( "complex_type" ); UM_CONFIG_POINTER( "data" ); UM_CONFIG_INT( "len" ); UM_STATS_INT( "counter" ); UM_OUTPUT( SIG_U8, 0 ); UM_OUTPUT( SIG_SYMBOL_COMPLEX, 0 ); // Initialise the callback-functions. NULL for not-used functions desc->fn_init = spc_init; desc->fn_reconfigure = spc_reconfig; desc->fn_process_data = spc_pdata; desc->fn_custom_msg = spc_custom_msg; desc->fn_finalize = spc_finalize; // And register the module in the SPM spc_id = swr_cdb_register_spc( &desc, "source" ); if ( spc_id == SWR_SPM_INVALID_ID ) { swr_spc_free_desc( desc ); PR_DBG( 0, "Couldn't register the module!\n" ); return 1; } PR_DBG( 1, "\"source\" is ready\n" ); return 0;}void spc_module_exit( void ) { PR_DBG( 2, "Source is going\n" ); if ( swr_cdb_unregister_spc( spc_id ) < 0 ) { PR_DBG( 0, "Still in use somewhere\n" ); }}module_init( spc_module_init );module_exit( spc_module_exit );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -