void close_interfaces(void *ctx) { int i; moddebug("nof_input=%d, nof_output=%d\n",nof_input_itf,nof_output_itf); if (ctrl_in) { if (oesr_itf_close(ctrl_in)) { oesr_perror("oesr_itf_close"); } } for (i=0;i<nof_input_itf;i++) { moddebug("input_%d=0x%x\n",i,inputs[i]); if (inputs[i]) { if (oesr_itf_close(inputs[i])) { oesr_perror("oesr_itf_close"); } } } for (i=0;i<nof_output_itf;i++) { moddebug("output_%d=0x%x\n",i,outputs[i]); if (outputs[i]) { if (oesr_itf_close(outputs[i])) { oesr_perror("oesr_itf_close"); } } } }
int init_interfaces(void *ctx) { int i; int has_ctrl=0; if (oesr_itf_nofinputs(ctx) > nof_input_itf) { /* try to create control interface */ ctrl_in = oesr_itf_create(ctx, 0, ITF_READ, CTRL_IN_BUFFER); if (ctrl_in == NULL) { if (oesr_error_code(ctx) == OESR_ERROR_NOTREADY) { return 0; } } else { modinfo("Created control port\n"); has_ctrl=1; } } moddebug("configuring %d inputs and %d outputs %d %d %d\n",nof_input_itf,nof_output_itf,inputs[0],input_max_samples,input_sample_sz); for (i=0;i<nof_input_itf;i++) { if (inputs[i] == NULL) { inputs[i] = oesr_itf_create(ctx, has_ctrl+i, ITF_READ, input_max_samples*input_sample_sz); if (inputs[i] == NULL) { if (oesr_error_code(ctx) == OESR_ERROR_NOTREADY) { return 0; } else { oesr_perror("creating input interface\n"); return -1; } } else { moddebug("input_%d=0x%x\n",i,inputs[i]); } } } for (i=0;i<nof_output_itf;i++) { if (outputs[i] == NULL) { outputs[i] = oesr_itf_create(ctx, i, ITF_WRITE, output_max_samples*output_sample_sz); if (outputs[i] == NULL) { if (oesr_error_code(ctx) == OESR_ERROR_NOTFOUND) { modinfo_msg("Caution output port %d not connected,\n",i); } else { moderror_msg("Error creating output port %d\n",i); oesr_perror("oesr_itf_create\n"); return -1; } } else { moddebug("output_%d=0x%x\n",i,outputs[i]); } } } return 1; }
int process_ctrl_packets(void *ctx) { int n, nof_packets; int i; do { moddebug("receiving control\n",0); n = oesr_itf_read(ctrl_in, ctrl_in_buffer, sizeof(struct ctrl_in_pkt)*MAX_INPUT_PACKETS,oesr_tstamp(ctx)); if (n == -1) { oesr_perror("oesr_itf_read"); return -1; } else if (n>0) { if (n % sizeof(struct ctrl_in_pkt)) { moderror_msg("Received %d bytes but packet size is %d\n", n, sizeof(struct ctrl_in_pkt)); } nof_packets = n / sizeof(struct ctrl_in_pkt); for (i=0;i<nof_packets;i++) { if (process_ctrl_packet(ctx, &ctrl_in_buffer[i])) { moderror_msg("Error processing control packet %d/%d\n",i,nof_packets); return -1; } } } } while(n>0); return 0; }
void close_counter(void *ctx) { moddebug("counter=0x%x\n",counter); if (!counter) return; if (oesr_counter_close(counter)) { oesr_perror("oesr_counter_close\n"); } }
void close_log(void *ctx) { moddebug("log=0x%x\n",mlog); if (!mlog) return; if (oesr_log_close(mlog)) { oesr_perror("oesr_counter_close\n"); } }
int close_remote_itf(void *ctx, int nof_itf) { int i; for (i=0;i<nof_itf;i++) { if (oesr_itf_close(outputs[i].itf)) { oesr_perror("oesr_itf_close"); } } return 0; }
int init_counter(void *ctx) { counter = oesr_counter_create(ctx, "work"); moddebug("counter=0x%x\n",counter); if (!counter) { oesr_perror("oesr_counter_create\n"); return -1; } return 0; }
int init_log(void *ctx) { mlog = oesr_log_create(ctx, "default"); moddebug("log=0x%x\n",mlog); if (!mlog) { oesr_perror("oesr_log_create\n"); return -1; } return 0; }
int process_ctrl_packet(void *ctx, struct ctrl_in_pkt *packet) { if (oesr_var_param_set_value_idx(ctx,packet->pm_idx,packet->value, packet->size) == -1) { moderror_msg("pm_idx=%d, value=0x%x, size=%d\n",packet->pm_idx,packet->value,packet->size); oesr_perror("Error setting control parameter\n"); return -1; } return 0; }
int process_ctrl_packet(void) { if (oesr_var_param_set_value(ctx,parameters[ctrl_in_buffer.pm_idx],ctrl_in_buffer.value, ctrl_in_buffer.size)) { oesr_perror("Error setting control parameter\n"); return -1; } return 0; }
int close_local_variables(void *ctx, int nof_vars) { int i; for (i=0;i<nof_vars;i++) { if (local_variables[i]) { if (oesr_var_close(ctx, local_variables[i])) { oesr_perror("oesr_var_close\n"); } } } return 0; }
void close_variables(void *ctx) { int i; moddebug("nof_vars=%d\n",nof_vars); for (i=0;i<nof_vars;i++) { if (vars[i]) { if (oesr_var_close(ctx, vars[i])) { oesr_perror("oesr_var_close\n"); } } } }
int local_parameters_update(void *ctx) { int i; for (i=0;i<nof_local_variables;i++) { if (oesr_var_param_get_value(ctx, (var_t) local_variables[i], local_params_db[i].value, local_params_db[i].size) == -1) { oesr_perror("oesr_var_param_value\n"); return -1; } } return 0; }
int param_get(pmid_t id, void *ptr, int max_size, param_type_t *type) { if (type) { *type = (param_type_t) oesr_var_param_type(ctx,(var_t) id); } int n = oesr_var_param_get_value(ctx, (var_t) id, ptr, max_size); if (n == -1) { /* keep quiet in this case */ if (oesr_error_code(ctx) != OESR_ERROR_INVAL) { oesr_perror("oesr_var_param_value\n"); } } return n; }
int init_variables(void *ctx) { int i; moddebug("nof_vars=%d\n",nof_vars); for (i=0;nof_vars;i++) { moddebug("var %d\n",i); vars[i]=oesr_var_create(ctx, user_vars[i].name, user_vars[i].value, user_vars[i].size); if (!vars[i]) { oesr_perror("oesr_var_create\n"); moderror_msg("variable name=%s size=%d\n",user_vars[i].name, user_vars[i].size); return -1; } } nof_parameters = oesr_var_param_list(ctx,parameters,MAX_PARAMETERS); if (nof_parameters == -1) { oesr_perror("oesr_var_param_list"); return -1; } return 0; }
int init_remote_itf(void *ctx, int nof_itf) { int i,j; int port,delay; char tmp[64]; for (i=0;i<nof_itf;i++) { port = outputs[i].module_idx-oesr_module_id(ctx)+nof_output_data_itf; if (port < 0) { moderror_msg("Can't sent to a module back in the chain (module_idx=%d)\n",outputs[i].module_idx); return -1; } /* check if a parameter sets a different delay */ for (j=0;j<nof_remote_variables;j++) { if (remote_variables[j].module_idx == outputs[i].module_idx) { break; } } if (j < nof_remote_variables) { snprintf(tmp,64,"delay_%s",remote_params_db[j].module_name); if (!param_get_int_name(tmp,&delay)) { moddebug("Setting a delay of %d slots to port %d, module %s\n",delay,port, remote_params_db[j].module_name); if (oesr_itf_delay_set(ctx,port,ITF_WRITE,delay)) { moderror_msg("Setting delay to port %d\n",port); return -1; } } } /* now create the variable */ outputs[i].itf = oesr_itf_create(ctx, port, ITF_WRITE, sizeof(struct ctrl_in_pkt)); if (outputs[i].itf == NULL) { if (oesr_error_code(ctx) == OESR_ERROR_NOTFOUND) { modinfo_msg("Caution output port %d not connected,\n",i); } else { moderror_msg("Error creating output port %d\n",i); oesr_perror("oesr_itf_create\n"); return -1; } } } return 0; }
int Run(void *_ctx) { ctx = _ctx; int tstamp = oesr_tstamp(ctx); moddebug("enter ts=%d\n",oesr_tstamp(ctx)); int i; int n; if (ctrl_in) { do { n = oesr_itf_read(ctrl_in, &ctrl_in_buffer, CTRL_IN_BUFFER); if (n == -1) { oesr_perror("oesr_itf_read"); return -1; } else if (n>0) { if (process_ctrl_packet()) { moderror("Error processing control packet\n"); return -1; } } } while(n>0); } for (i=0;i<nof_input_itf;i++) { if (!inputs[i]) { input_ptr[i] = NULL; rcv_len[i] = 0; } else { n = oesr_itf_ptr_get(inputs[i], &input_ptr[i], &rcv_len[i], tstamp); if (n == 0) { itfdebug("[ts=%d] received no input from %d\n",rtdal_time_slot(),i); } else if (n == -1) { oesr_perror("oesr_itf_get"); return -1; } else { itfdebug("[ts=%d] received %d bytes\n",rtdal_time_slot(),rcv_len[i]); rcv_len[i] /= input_sample_sz; } } } for (i=0;i<nof_output_itf;i++) { if (!outputs[i]) { output_ptr[i] = NULL; } else { n = oesr_itf_ptr_request(outputs[i], &output_ptr[i]); if (n == 0) { /* moderror_msg("[ts=%d] no packets available in output interface %d\n",rtdal_time_slot(),i); */ } else if (n == -1) { oesr_perror("oesr_itf_request"); return -1; } } } memset(snd_len,0,sizeof(int)*nof_output_itf); #if MOD_DEBUG==1 oesr_counter_start(counter); #endif n = work(input_ptr,output_ptr); #if MOD_DEBUG==1 oesr_counter_stop(counter); moddebug("work exec time: %d us\n",oesr_counter_usec(counter)); #endif if (n<0) { return -1; } memset(rcv_len,0,sizeof(int)*nof_input_itf); for (i=0;i<nof_output_itf;i++) { if (!snd_len[i] && output_ptr[i]) { snd_len[i] = n*output_sample_sz; } } for (i=0;i<nof_input_itf;i++) { if (input_ptr[i]) { n = oesr_itf_ptr_release(inputs[i]); if (n == 0) { itfdebug("[ts=%d] packet from interface %d not released\n",rtdal_time_slot(),i); } else if (n == -1) { oesr_perror("oesr_itf_ptr_release\n"); return -1; } } } for (i=0;i<nof_output_itf;i++) { if (output_ptr[i] && snd_len[i]) { n = oesr_itf_ptr_put(outputs[i],snd_len[i],tstamp); if (n == 0) { itfdebug("[ts=%d] no space left in output interface %d\n",rtdal_time_slot(),i); } else if (n == -1) { oesr_perror("oesr_itf_ptr_put\n"); return -1; } } } moddebug("exit ts=%d\n",oesr_tstamp(ctx)); return 0; }