mirror of
https://github.com/radio95-rnt/rds95.git
synced 2026-02-26 20:33:53 +01:00
refactor
This commit is contained in:
@@ -317,7 +317,7 @@ static void handle_level(char *arg, RDSModulator* mod, char* output) {
|
||||
|
||||
static void handle_reset(char *arg, RDSModulator* mod, char* output) {
|
||||
(void)arg;
|
||||
loadFromFile(mod->enc);
|
||||
encoder_loadFromFile(mod->enc);
|
||||
for(int i = 0; i < PROGRAMS; i++) reset_rds_state(mod->enc, i);
|
||||
Modulator_loadFromFile(&mod->params);
|
||||
strcpy(output, "+");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "common.h"
|
||||
#include "rds.h"
|
||||
#include "rds_fs.h"
|
||||
#include "fs.h"
|
||||
#include "modulator.h"
|
||||
#include "lib.h"
|
||||
#define CMD_BUFFER_SIZE 255
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "rds_fs.h"
|
||||
#include "fs.h"
|
||||
|
||||
void saveToFile(RDSEncoder *enc) {
|
||||
void encoder_saveToFile(RDSEncoder *enc) {
|
||||
char encoderPath[128];
|
||||
snprintf(encoderPath, sizeof(encoderPath), "%s/.rdsEncoder", getenv("HOME"));
|
||||
|
||||
@@ -32,7 +32,7 @@ void saveToFile(RDSEncoder *enc) {
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void loadFromFile(RDSEncoder *enc) {
|
||||
void encoder_loadFromFile(RDSEncoder *enc) {
|
||||
char encoderPath[128];
|
||||
snprintf(encoderPath, sizeof(encoderPath), "%s/.rdsEncoder", getenv("HOME"));
|
||||
|
||||
@@ -63,11 +63,84 @@ void loadFromFile(RDSEncoder *enc) {
|
||||
enc->program = rdsEncoderfile.program;
|
||||
}
|
||||
|
||||
int isFileSaved() {
|
||||
int encoder_saved() {
|
||||
char encoderPath[128];
|
||||
snprintf(encoderPath, sizeof(encoderPath), "%s/.rdsEncoder", getenv("HOME"));
|
||||
FILE *file = fopen(encoderPath, "rb");
|
||||
if(!file) return 0;
|
||||
fclose(file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Modulator_saveToFile(RDSModulatorParameters *emp) {
|
||||
char modulatorPath[128];
|
||||
snprintf(modulatorPath, sizeof(modulatorPath), "%s/.rdsModulator", getenv("HOME"));
|
||||
FILE *file;
|
||||
|
||||
RDSModulatorParameters tempMod;
|
||||
RDSModulatorParametersFile tempFile;
|
||||
memset(&tempFile, 0, sizeof(tempFile));
|
||||
file = fopen(modulatorPath, "rb");
|
||||
if (file != NULL) {
|
||||
fread(&tempFile, sizeof(RDSModulatorParametersFile), 1, file);
|
||||
fclose(file);
|
||||
} else {
|
||||
memset(&tempFile, 0, sizeof(RDSModulatorParametersFile));
|
||||
tempFile.check = 160;
|
||||
memcpy(&tempFile.params, emp, sizeof(RDSModulatorParameters));
|
||||
tempFile.crc = crc16_ccitt((char*)&tempFile, offsetof(RDSModulatorParametersFile, crc));
|
||||
}
|
||||
memcpy(&tempMod, &tempFile.params, sizeof(RDSModulatorParameters));
|
||||
|
||||
tempMod.level = emp->level;
|
||||
tempMod.rdsgen = emp->rdsgen;
|
||||
|
||||
memcpy(&tempFile.params, &tempMod, sizeof(RDSModulatorParameters));
|
||||
tempFile.check = 160;
|
||||
tempFile.crc = crc16_ccitt((char*)&tempFile, offsetof(RDSModulatorParametersFile, crc));
|
||||
|
||||
file = fopen(modulatorPath, "wb");
|
||||
if (file == NULL) {
|
||||
perror("Error opening file");
|
||||
return;
|
||||
}
|
||||
fwrite(&tempFile, sizeof(RDSModulatorParametersFile), 1, file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void Modulator_loadFromFile(RDSModulatorParameters *emp) {
|
||||
char modulatorPath[128];
|
||||
snprintf(modulatorPath, sizeof(modulatorPath), "%s/.rdsModulator", getenv("HOME"));
|
||||
FILE *file = fopen(modulatorPath, "rb");
|
||||
if (file == NULL) {
|
||||
perror("Error opening file");
|
||||
return;
|
||||
}
|
||||
RDSModulatorParametersFile tempFile;
|
||||
memset(&tempFile, 0, sizeof(tempFile));
|
||||
fread(&tempFile, sizeof(RDSModulatorParametersFile), 1, file);
|
||||
if (tempFile.check != 160) {
|
||||
fprintf(stderr, "[RDSMODULATOR-FILE] Invalid file format\n");
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
uint16_t calculated_crc = crc16_ccitt((char*)&tempFile, offsetof(RDSModulatorParametersFile, crc));
|
||||
if (calculated_crc != tempFile.crc) {
|
||||
fprintf(stderr, "[RDSMODULATOR-FILE] CRC mismatch! Data may be corrupted\n");
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
memcpy(emp, &tempFile.params, sizeof(RDSModulatorParameters));
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
int modulatorsaved() {
|
||||
char encoderPath[128];
|
||||
snprintf(encoderPath, sizeof(encoderPath), "%s/.rdsModulator", getenv("HOME"));
|
||||
FILE *file = fopen(encoderPath, "rb");
|
||||
if (file) {
|
||||
fclose(file);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
12
src/fs.h
Normal file
12
src/fs.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "common.h"
|
||||
#include "rds.h"
|
||||
#include "modulator.h"
|
||||
#include "lib.h"
|
||||
|
||||
void encoder_saveToFile(RDSEncoder *emp);
|
||||
void encoder_loadFromFile(RDSEncoder *emp);
|
||||
int encoder_saved();
|
||||
|
||||
void Modulator_saveToFile(RDSModulatorParameters *emp);
|
||||
void Modulator_loadFromFile(RDSModulatorParameters *emp);
|
||||
int modulatorsaved();
|
||||
@@ -1,77 +1,5 @@
|
||||
#include "modulator.h"
|
||||
|
||||
void Modulator_saveToFile(RDSModulatorParameters *emp) {
|
||||
char modulatorPath[128];
|
||||
snprintf(modulatorPath, sizeof(modulatorPath), "%s/.rdsModulator", getenv("HOME"));
|
||||
FILE *file;
|
||||
|
||||
RDSModulatorParameters tempMod;
|
||||
RDSModulatorParametersFile tempFile;
|
||||
memset(&tempFile, 0, sizeof(tempFile));
|
||||
file = fopen(modulatorPath, "rb");
|
||||
if (file != NULL) {
|
||||
fread(&tempFile, sizeof(RDSModulatorParametersFile), 1, file);
|
||||
fclose(file);
|
||||
} else {
|
||||
memset(&tempFile, 0, sizeof(RDSModulatorParametersFile));
|
||||
tempFile.check = 160;
|
||||
memcpy(&tempFile.params, emp, sizeof(RDSModulatorParameters));
|
||||
tempFile.crc = crc16_ccitt((char*)&tempFile, offsetof(RDSModulatorParametersFile, crc));
|
||||
}
|
||||
memcpy(&tempMod, &tempFile.params, sizeof(RDSModulatorParameters));
|
||||
|
||||
tempMod.level = emp->level;
|
||||
tempMod.rdsgen = emp->rdsgen;
|
||||
|
||||
memcpy(&tempFile.params, &tempMod, sizeof(RDSModulatorParameters));
|
||||
tempFile.check = 160;
|
||||
tempFile.crc = crc16_ccitt((char*)&tempFile, offsetof(RDSModulatorParametersFile, crc));
|
||||
|
||||
file = fopen(modulatorPath, "wb");
|
||||
if (file == NULL) {
|
||||
perror("Error opening file");
|
||||
return;
|
||||
}
|
||||
fwrite(&tempFile, sizeof(RDSModulatorParametersFile), 1, file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
void Modulator_loadFromFile(RDSModulatorParameters *emp) {
|
||||
char modulatorPath[128];
|
||||
snprintf(modulatorPath, sizeof(modulatorPath), "%s/.rdsModulator", getenv("HOME"));
|
||||
FILE *file = fopen(modulatorPath, "rb");
|
||||
if (file == NULL) {
|
||||
perror("Error opening file");
|
||||
return;
|
||||
}
|
||||
RDSModulatorParametersFile tempFile;
|
||||
memset(&tempFile, 0, sizeof(tempFile));
|
||||
fread(&tempFile, sizeof(RDSModulatorParametersFile), 1, file);
|
||||
if (tempFile.check != 160) {
|
||||
fprintf(stderr, "[RDSMODULATOR-FILE] Invalid file format\n");
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
uint16_t calculated_crc = crc16_ccitt((char*)&tempFile, offsetof(RDSModulatorParametersFile, crc));
|
||||
if (calculated_crc != tempFile.crc) {
|
||||
fprintf(stderr, "[RDSMODULATOR-FILE] CRC mismatch! Data may be corrupted\n");
|
||||
fclose(file);
|
||||
return;
|
||||
}
|
||||
memcpy(emp, &tempFile.params, sizeof(RDSModulatorParameters));
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
int modulatorsaved() {
|
||||
char encoderPath[128];
|
||||
snprintf(encoderPath, sizeof(encoderPath), "%s/.rdsModulator", getenv("HOME"));
|
||||
FILE *file = fopen(encoderPath, "rb");
|
||||
if (file) {
|
||||
fclose(file);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#include "fs.h"
|
||||
|
||||
void init_rds_modulator(RDSModulator* rdsMod, RDSEncoder* enc, uint8_t num_streams) {
|
||||
memset(rdsMod, 0, sizeof(*rdsMod));
|
||||
@@ -87,9 +15,7 @@ void init_rds_modulator(RDSModulator* rdsMod, RDSEncoder* enc, uint8_t num_strea
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < num_streams; i++) {
|
||||
rdsMod->data[i].symbol_shift = i * M_PI / 2.0f;
|
||||
}
|
||||
for (uint8_t i = 0; i < num_streams; i++) rdsMod->data[i].symbol_shift = i * M_PI / 2.0f;
|
||||
|
||||
if(modulatorsaved()) Modulator_loadFromFile(&rdsMod->params);
|
||||
else Modulator_saveToFile(&rdsMod->params);
|
||||
|
||||
@@ -34,9 +34,6 @@ typedef struct {
|
||||
uint8_t num_streams;
|
||||
} RDSModulator;
|
||||
|
||||
void Modulator_saveToFile(RDSModulatorParameters *emp);
|
||||
void Modulator_loadFromFile(RDSModulatorParameters *emp);
|
||||
int modulatorsaved();
|
||||
void init_rds_modulator(RDSModulator* rdsMod, RDSEncoder* enc, uint8_t num_streams);
|
||||
void cleanup_rds_modulator(RDSModulator* rdsMod);
|
||||
float get_rds_sample(RDSModulator* rdsMod, uint8_t stream);
|
||||
18
src/rds.c
18
src/rds.c
@@ -1,6 +1,6 @@
|
||||
#include "common.h"
|
||||
#include "rds.h"
|
||||
#include "rds_fs.h"
|
||||
#include "fs.h"
|
||||
#include "modulator.h"
|
||||
#include "lib.h"
|
||||
#include <time.h>
|
||||
@@ -138,26 +138,26 @@ static void get_rds_rt_group(RDSEncoder* enc, RDSGroup *group) {
|
||||
enc->state[enc->program].rt_state++;
|
||||
if (enc->state[enc->program].rt_state == segments) enc->state[enc->program].rt_state = 0;
|
||||
}
|
||||
static void get_rdsp_rtp_oda_group(RDSGroup *group) {
|
||||
inline static void get_rdsp_rtp_oda_group(RDSGroup *group) {
|
||||
group->b |= 3 << 12;
|
||||
group->b |= 11 << 1;
|
||||
group->d = ODA_AID_RTPLUS;
|
||||
}
|
||||
|
||||
static void get_rdsp_ertp_oda_group(RDSGroup *group) {
|
||||
inline static void get_rdsp_ertp_oda_group(RDSGroup *group) {
|
||||
group->b |= 3 << 12;
|
||||
group->b |= 13 << 1;
|
||||
group->d = ODA_AID_ERTPLUS;
|
||||
}
|
||||
|
||||
static void get_rdsp_ert_oda_group(RDSGroup *group) {
|
||||
inline static void get_rdsp_ert_oda_group(RDSGroup *group) {
|
||||
group->b |= 3 << 12;
|
||||
group->b |= 12 << 1;
|
||||
group->c = 1; // UTF-8
|
||||
group->d = ODA_AID_ERT;
|
||||
}
|
||||
|
||||
static void get_rdsp_oda_af_oda_group(RDSGroup *group) {
|
||||
inline static void get_rdsp_oda_af_oda_group(RDSGroup *group) {
|
||||
group->b |= 3 << 12;
|
||||
group->b |= 7 << 1;
|
||||
group->d = ODA_AID_ODAAF;
|
||||
@@ -234,13 +234,13 @@ static void get_rds_lps_group(RDSEncoder* enc, RDSGroup *group) {
|
||||
if (enc->state[enc->program].lps_state == enc->state[enc->program].lps_segments) enc->state[enc->program].lps_state = 0;
|
||||
}
|
||||
|
||||
static void get_rds_ecc_group(RDSEncoder* enc, RDSGroup *group) {
|
||||
inline static void get_rds_ecc_group(RDSEncoder* enc, RDSGroup *group) {
|
||||
group->b |= 1 << 12;
|
||||
group->c = enc->state[enc->program].eon_linkage << 15;
|
||||
group->c |= enc->data[enc->program].ecc;
|
||||
}
|
||||
|
||||
static void get_rds_slcdata_group(RDSEncoder* enc, RDSGroup *group) {
|
||||
inline static void get_rds_slcdata_group(RDSEncoder* enc, RDSGroup *group) {
|
||||
group->b |= 1 << 12;
|
||||
group->c = enc->state[enc->program].eon_linkage << 15;
|
||||
group->c |= 0x6000;
|
||||
@@ -702,8 +702,8 @@ void set_rds_defaults(RDSEncoder* enc, uint8_t program) {
|
||||
void init_rds_encoder(RDSEncoder* enc) {
|
||||
for(int i = 0; i < PROGRAMS; i++) set_rds_defaults(enc, i);
|
||||
|
||||
if (isFileSaved()) loadFromFile(enc);
|
||||
else saveToFile(enc);
|
||||
if (encoder_saved()) encoder_loadFromFile(enc);
|
||||
else encoder_saveToFile(enc);
|
||||
|
||||
for(int i = 0; i < PROGRAMS; i++) reset_rds_state(enc, i);
|
||||
}
|
||||
|
||||
10
src/rds95.c
10
src/rds95.c
@@ -7,7 +7,7 @@
|
||||
#include "../inih/ini.h"
|
||||
|
||||
#include "rds.h"
|
||||
#include "rds_fs.h"
|
||||
#include "fs.h"
|
||||
#include "modulator.h"
|
||||
#include "udp_server.h"
|
||||
#include "lib.h"
|
||||
@@ -133,12 +133,11 @@ int main(int argc, char **argv) {
|
||||
signal(SIGTERM, stop);
|
||||
|
||||
format.format = PA_SAMPLE_FLOAT32NE;
|
||||
format.channels = config.num_streams; // Use dynamic stream count
|
||||
format.channels = config.num_streams;
|
||||
format.rate = RDS_SAMPLE_RATE;
|
||||
|
||||
buffer.prebuf = 0;
|
||||
buffer.tlength = NUM_MPX_FRAMES * config.num_streams;
|
||||
buffer.maxlength = NUM_MPX_FRAMES * config.num_streams;
|
||||
buffer.tlength = buffer.maxlength = NUM_MPX_FRAMES * config.num_streams;
|
||||
|
||||
rds_device = pa_simple_new(
|
||||
NULL,
|
||||
@@ -178,7 +177,6 @@ int main(int argc, char **argv) {
|
||||
|
||||
int pulse_error;
|
||||
|
||||
// Dynamically allocate buffer based on stream count
|
||||
float *rds_buffer = (float*)malloc(NUM_MPX_FRAMES * config.num_streams * sizeof(float));
|
||||
if (rds_buffer == NULL) {
|
||||
fprintf(stderr, "Error: Could not allocate memory for RDS buffer\n");
|
||||
@@ -202,7 +200,7 @@ exit:
|
||||
pthread_join(udp_server_thread, NULL);
|
||||
}
|
||||
|
||||
saveToFile(&rdsEncoder);
|
||||
encoder_saveToFile(&rdsEncoder);
|
||||
Modulator_saveToFile(&rdsModulator.params);
|
||||
|
||||
cleanup_rds_modulator(&rdsModulator);
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
#include "common.h"
|
||||
#include "rds.h"
|
||||
#include "lib.h"
|
||||
|
||||
void saveToFile(RDSEncoder *emp);
|
||||
void loadFromFile(RDSEncoder *emp);
|
||||
int isFileSaved();
|
||||
@@ -30,7 +30,7 @@ int open_udp_server(int port, RDSModulator* rds_mod) {
|
||||
poller.fd = sockfd;
|
||||
poller.events = POLLIN;
|
||||
|
||||
mod = rds_mod; // Save mod pointer
|
||||
mod = rds_mod;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -38,7 +38,7 @@ int open_udp_server(int port, RDSModulator* rds_mod) {
|
||||
void poll_udp_server() {
|
||||
static char buf[BUF_SIZE];
|
||||
static char cmd_buf[BUF_SIZE];
|
||||
static char cmd_output[BUF_SIZE]; // Buffer to collect output from process_ascii_cmd
|
||||
static char cmd_output[BUF_SIZE];
|
||||
ssize_t bytes_read;
|
||||
|
||||
if (poll(&poller, 1, UDP_READ_TIMEOUT_MS) <= 0) return;
|
||||
@@ -46,8 +46,7 @@ void poll_udp_server() {
|
||||
|
||||
memset(buf, 0, BUF_SIZE);
|
||||
client_len = sizeof(client_addr);
|
||||
bytes_read = recvfrom(sockfd, buf, BUF_SIZE - 1, 0,
|
||||
(struct sockaddr *)&client_addr, &client_len);
|
||||
bytes_read = recvfrom(sockfd, buf, BUF_SIZE - 1, 0, (struct sockaddr *)&client_addr, &client_len);
|
||||
if (bytes_read <= 0) return;
|
||||
|
||||
buf[bytes_read] = '\0';
|
||||
@@ -60,17 +59,12 @@ void poll_udp_server() {
|
||||
strncpy(cmd_buf, token, BUF_SIZE - 1);
|
||||
|
||||
memset(cmd_output, 0, BUF_SIZE);
|
||||
// Pass cmd_output buffer to collect output from the command processor
|
||||
process_ascii_cmd(mod, cmd_buf, cmd_output);
|
||||
|
||||
// Send cmd_output back to client (even if empty, send it)
|
||||
size_t out_len = strlen(cmd_output);
|
||||
if (out_len > 0) {
|
||||
ssize_t sent = sendto(sockfd, cmd_output, out_len, 0,
|
||||
(struct sockaddr *)&client_addr, client_len);
|
||||
if (sent == -1) {
|
||||
perror("sendto");
|
||||
}
|
||||
ssize_t sent = sendto(sockfd, cmd_output, out_len, 0, (struct sockaddr *)&client_addr, client_len);
|
||||
if (sent == -1) perror("sendto");
|
||||
}
|
||||
}
|
||||
token = strtok(NULL, "\r\n");
|
||||
|
||||
@@ -13,7 +13,5 @@
|
||||
#include "ascii_cmd.h"
|
||||
|
||||
int open_udp_server(int port, RDSModulator *rds_mod);
|
||||
|
||||
void poll_udp_server();
|
||||
|
||||
void close_udp_server();
|
||||
|
||||
Reference in New Issue
Block a user