diff --git a/SBC/rpi-4/device_driver/firmware/bias.c b/SBC/rpi-4/device_driver/firmware/bias.c index 1b01c9b..e333994 100644 --- a/SBC/rpi-4/device_driver/firmware/bias.c +++ b/SBC/rpi-4/device_driver/firmware/bias.c @@ -1,18 +1,19 @@ #include "bias.h" +#include -int fd_i2c_bias; -int i2c_bias_handler; +static int fd_i2c_bias; +static int i2c_bias_handler; -void init_I2C_bias() { +void init_I2C_bias(void) { fd_i2c_bias = open("/dev/i2c-1", O_RDWR); if (fd_i2c_bias < 0 ) { fprintf(stderr, "Your SBC device is missing the following driver: '/dev/i2c-1' \n"); fprintf(stderr, "Change of Bias Setting is not possible\n"); - return fd_i2c_bias; + return ;// fd_i2c_bias; } i2c_bias_handler = ioctl(fd_i2c_bias, I2C_SLAVE, ADDR_BIAS); @@ -31,7 +32,7 @@ void write_I2C_bias(uint8_t control, uint8_t data) { else fprintf(stderr, "Write I2C Bias command failed \n"); } -void close_I2C_bias() { - if (fd_i2c_bias != NULL) close(fd_i2c_bias); +void close_I2C_bias(void) { + if (fd_i2c_bias >= 0) close(fd_i2c_bias); } -//end of source \ No newline at end of file +//end of source diff --git a/SBC/rpi-4/device_driver/firmware/bias.h b/SBC/rpi-4/device_driver/firmware/bias.h index 060bfcf..9668a63 100644 --- a/SBC/rpi-4/device_driver/firmware/bias.h +++ b/SBC/rpi-4/device_driver/firmware/bias.h @@ -29,11 +29,9 @@ #define ADDR_BIAS 0x2C -extern int fd_i2c_bias; -extern int i2c_bias_handler; - void openI2C_bias(void); void write_I2C_bias(uint8_t control, uint8_t data); void close_I2C_bias(void); +void init_I2C_bias(void); -#endif \ No newline at end of file +#endif diff --git a/SBC/rpi-4/device_driver/firmware/filters.h b/SBC/rpi-4/device_driver/firmware/filters.h index bd3f6fa..f6e48a4 100644 --- a/SBC/rpi-4/device_driver/firmware/filters.h +++ b/SBC/rpi-4/device_driver/firmware/filters.h @@ -12,6 +12,8 @@ #include +#include +#include int fd_i2c_alex; int fd_i2c_filter; @@ -44,7 +46,7 @@ int previousCW = -1; //**************************************** // Initializing Alex Interface //**************************************** -void initALEX() { +static inline void initALEX(void) { int result = 0; unsigned char data[3]; @@ -69,7 +71,7 @@ void initALEX() { //********************************************* // Initializing Generic filters Interface //********************************************* -void initGenericFilters() { +static inline void initGenericFilters(void) { int result = 0; unsigned char data[3]; @@ -95,7 +97,7 @@ void initGenericFilters() { //**************************************** // Initializing Alex Interface //**************************************** -void initN2ADR() { +static inline void initN2ADR(void) { int result = 0; unsigned char data[2]; @@ -123,7 +125,7 @@ void initN2ADR() { // // Look for Filter Selection description. //**************************************************************** -void handleN2ADRFilterBoard(char* buffer) +static inline void handleN2ADRFilterBoard(char* buffer) { if (i2c_n2adr & (buffer[523] & 0xFE) == 0x00) { i2c_n2adr_data = ((buffer[526] & 0x20) << 1) | ((buffer[525] & 0xFE) >> 1); @@ -141,7 +143,8 @@ void handleN2ADRFilterBoard(char* buffer) ldata[1] = i2c_n2adr_data & 0xFF; fprintf(stderr, "Set N2ADR data = %x \n", ldata[1]); - write(fd_i2c_n2adr, ldata, 2); + if(write(fd_i2c_n2adr, ldata, 2) < 0) + fprintf(stderr, "Error %d setting N2ADR data\n", errno); } } } @@ -149,7 +152,7 @@ void handleN2ADRFilterBoard(char* buffer) //******************************************* // Handle data to Alex Interface Board //******************************************* -void handleALEX(char* buffer) +static inline void handleALEX(char* buffer) { if (i2c_alex & ((buffer[523] & 0xFE) == 0x12) || ((buffer[11] & 0xFE) == 0x12)) { @@ -194,7 +197,8 @@ void handleALEX(char* buffer) fprintf(stderr, "Set Alex data 0 = %x \n", ldata[0]); fprintf(stderr, "Set Alex data 1 = %x \n", ldata[1]); fprintf(stderr, "Set Alex data 2 = %x \n", ldata[2]); - write(fd_i2c_alex, ldata, 3); + if (write(fd_i2c_alex, ldata, 3) < 0) + fprintf(stderr, "Error %d writing Alex data\n", errno); } } } @@ -204,7 +208,7 @@ void handleALEX(char* buffer) // This allow easier integration for different lpf, bpf filter interface that didn't match Alex interface filters groups. // This also allow easier integration for different countries band plan because the band plan is defined in the arduino firmware. //************************************************************************************************************************************ -void handleFiltersBoard(char* buffer, int cw) +static inline void handleFiltersBoard(char* buffer, int cw) { //*********************************************** // Send Band Selected Alex board Style @@ -245,7 +249,8 @@ void handleFiltersBoard(char* buffer, int cw) ldata[0] = 0x02; ldata[1] = ((i2c_alex_data >> 8) & 0xFF); ldata[2] = (i2c_alex_data & 0xFF); - write(fd_i2c_filter, ldata, 3); + if (write(fd_i2c_filter, ldata, 3) < 0) + fprintf(stderr, "Error %d writing Alex data\n", errno); fprintf(stderr, "Set Alex data 0 = %x \n", ldata[0]); fprintf(stderr, "Set Alex data 1 = %x \n", ldata[1]); fprintf(stderr, "Set Alex data 2 = %x \n", ldata[2]); @@ -267,7 +272,8 @@ void handleFiltersBoard(char* buffer, int cw) ldata[0] = 0x03; ldata[1] = ((buffer[11] & 0x01) == 0x01) ? 1 : 0; ldata[2] = currentCW; - write(fd_i2c_filter, ldata, 3); + if (write(fd_i2c_filter, ldata, 3) < 0) + fprintf(stderr, "Error %d writing Alex data\n", errno); fprintf(stderr, "PTT data 0 = %x \n", ldata[0]); fprintf(stderr, "PTT data 1 = %x \n", ldata[1]); fprintf(stderr, "PTT data 2 = %x \n", ldata[2]); @@ -293,8 +299,10 @@ void handleFiltersBoard(char* buffer, int cw) ldata[7] = (tempFreq / 10U) % 10; ldata[8] = (tempFreq / 1U) % 10; - write(fd_i2c_filter, ldata, 9); - fprintf(stderr, "Set Filters frequency to = %d \n", currentfreq); + if (write(fd_i2c_filter, ldata, 9) < 0) + fprintf(stderr, "Error %d setting frequency \n", errno); + else + fprintf(stderr, "Set Filters frequency to = %d \n", currentfreq); } } @@ -303,13 +311,13 @@ void handleFiltersBoard(char* buffer, int cw) //******************************************* // Convert frequency value to integer //******************************************* -int determine_freq(int base_index, char* buffer) { +static inline int determine_freq(int base_index, char* buffer) { return (((buffer[base_index + 1] & 0xFF) << 24) + ((buffer[base_index + 2] & 0xFF) << 16) + ((buffer[base_index + 3] & 0xFF) << 8) + (buffer[base_index + 4] & 0xFF)); } //********************************************************** // Determine which board to forward data - Alex or Generic //********************************************************** -void handleFilters(char* buffer, int cw) { +static inline void handleFilters(char* buffer, int cw) { if ((buffer[11] & 0xFE) == 0x04) { currentfreq = determine_freq(11, buffer); @@ -332,7 +340,7 @@ void handleFilters(char* buffer, int cw) { //******************************************************************** // Determine which filters interface is connected to the radioberry //******************************************************************** -void initFilters() { +static inline void initFilters(void) { fd_i2c_alex = open("/dev/i2c-1", O_RDWR); fd_i2c_filter = open("/dev/i2c-1", O_RDWR); diff --git a/SBC/rpi-4/device_driver/firmware/measure.c b/SBC/rpi-4/device_driver/firmware/measure.c index 260d2fc..ecdfc3c 100644 --- a/SBC/rpi-4/device_driver/firmware/measure.c +++ b/SBC/rpi-4/device_driver/firmware/measure.c @@ -1,11 +1,12 @@ #include "measure.h" +#include int i2c_measure_module_active; -int fd_i2c_measure; -int i2c_measure_handler; +static int fd_i2c_measure; +static int i2c_measure_handler; -int config_I2C_measure(){ +int config_I2C_measure(void){ uint8_t measure_config[1]; measure_config[0] = 0x07; @@ -18,7 +19,7 @@ int config_I2C_measure(){ return result; }; -void openI2C_measure() { +void openI2C_measure(void) { i2c_measure_module_active = 0; @@ -27,7 +28,7 @@ void openI2C_measure() { if (fd_i2c_measure < 0 ) { fprintf(stderr, "Your SBC device is missing the following driver: '/dev/i2c-1' \n"); fprintf(stderr, "Measurement is not possible\n"); - return fd_i2c_measure; + return; } i2c_measure_handler = ioctl(fd_i2c_measure, I2C_SLAVE, ADDR_MEAS); @@ -44,7 +45,7 @@ void read_I2C_measure(int *current, int *temperature){ *current = (int)(((measure_data[4] & 0x0F) <<8) | measure_data[5]); }; -void close_I2C_measure() { - if (fd_i2c_measure != NULL) close(fd_i2c_measure); +void close_I2C_measure(void) { + if (fd_i2c_measure >= 0) close(fd_i2c_measure); }; -//end of source \ No newline at end of file +//end of source diff --git a/SBC/rpi-4/device_driver/firmware/measure.h b/SBC/rpi-4/device_driver/firmware/measure.h index e8b5be6..e2e0f57 100644 --- a/SBC/rpi-4/device_driver/firmware/measure.h +++ b/SBC/rpi-4/device_driver/firmware/measure.h @@ -29,11 +29,8 @@ extern int i2c_measure_module_active; -extern int fd_i2c_measure; -extern int i2c_measure_handler; - void openI2C_measure(void); void read_I2C_measure(int *current, int *temperature); void close_I2C_measure(void); -#endif \ No newline at end of file +#endif diff --git a/SBC/rpi-4/device_driver/firmware/radioberry.c b/SBC/rpi-4/device_driver/firmware/radioberry.c index 8ade806..5dd318a 100644 --- a/SBC/rpi-4/device_driver/firmware/radioberry.c +++ b/SBC/rpi-4/device_driver/firmware/radioberry.c @@ -58,6 +58,9 @@ For more information, please refer to #include "register.h" #include "bias.h" #include "measure.h" +#include +#include +#include int main(int argc, char **argv) { @@ -71,7 +74,12 @@ int main(int argc, char **argv) closeRadioberry(); } -int initRadioberry() { +static void start_rb_control_thread(void); +static void start_rb_measure_thread(void); +static void start_rb_register_thread(void); +static void start_timer_thread(void); + +static int initRadioberry(void) { sem_init(&mutex, 0, 1); sem_init(&tx_empty, 0, TX_MAX); sem_init(&tx_full, 0, 0); @@ -184,18 +192,18 @@ int initRadioberry() { fcntl(sock_TCP_Server, F_SETFL, flags | O_NONBLOCK); start_rb_register_thread(); + return 0; } -int closeRadioberry() { +static void closeRadioberry(void) { if (fd_rb != 0) close(fd_rb); if (sock_TCP_Client >= 0) close(sock_TCP_Client); if (sock_TCP_Server >= 0) close(sock_TCP_Server); close_I2C_bias(); close_I2C_measure(); - return 0; } -void runRadioberry() { +static void runRadioberry(void) { fprintf(stderr, "Radioberry, Starting packet control part. \n"); start_timer_thread(); fprintf(stderr, "Radioberry, Starting packet tx part. \n"); @@ -207,7 +215,7 @@ void runRadioberry() { } } -void *packetreader(void *arg) { +static void *packetreader(void *arg) { int size, bytes_read, bytes_left; unsigned char buffer[2048]; @@ -245,7 +253,7 @@ void *packetreader(void *arg) { } } -void handlePacket(char* buffer){ +static void handlePacket(char* buffer){ uint32_t code; memcpy(&code, buffer, 4); switch (code) @@ -311,7 +319,7 @@ void handlePacket(char* buffer){ #define assign_change(a,b,c) if ((a) != b) { b = (a); fprintf(stderr, "%20s= %08lx (%10ld)\n", c, (long) b, (long) b ); } -void handleCommand(int base_index, char* buffer) { +static void handleCommand(int base_index, char* buffer) { command = buffer[base_index]; command_data=((buffer[base_index+1]&0xFF)<<24)+((buffer[base_index+2]&0xFF)<<16)+((buffer[base_index+3]&0xFF)<<8)+(buffer[base_index+4]&0xFF); @@ -330,12 +338,12 @@ void handleCommand(int base_index, char* buffer) { } } -void handleCommands(char* buffer) { +static void handleCommands(char* buffer) { handleCommand(11, buffer); handleCommand(523, buffer); } -void processPacket(char* buffer) +static void processPacket(char* buffer) { MOX = ((buffer[11] & 0x01)==0x01) ? 0x01:0x00; @@ -374,7 +382,7 @@ void processPacket(char* buffer) //************************************************** } -void sendPacket() { +static void sendPacket(void) { fillPacketToSend(); if (sock_TCP_Client >= 0) { if (sendto(sock_TCP_Client, hpsdrdata, sizeof(hpsdrdata), 0, NULL, 0) != 1032) fprintf(stderr, "TCP send error"); @@ -383,19 +391,20 @@ void sendPacket() { } } -void read_temperature_raspberryPi() { +static void read_temperature_raspberryPi(void) { FILE *thermal; thermal = fopen("/sys/class/thermal/thermal_zone0/temp","r"); float systemp, millideg; - fscanf(thermal,"%f",&millideg); - systemp = millideg / 1000; - //fprintf(stderr, "CPU temperature is %f degrees C\n",systemp); - sys_temp = (int) (4096/3.26) * ((systemp/ 100) + 0.5); - //fprintf(stderr, "CPU temperature in protocol has value %x\n",sys_temp); + if (fscanf(thermal,"%f",&millideg) == 1) { + systemp = millideg / 1000; + //fprintf(stderr, "CPU temperature is %f degrees C\n",systemp); + sys_temp = (int) (4096/3.26) * ((systemp/ 100) + 0.5); + //fprintf(stderr, "CPU temperature in protocol has value %x\n",sys_temp); + } fclose(thermal); } -void fillPacketToSend() { +static void fillPacketToSend(void) { memset(hpsdrdata,0,1032); memcpy(hpsdrdata, header_hpsdrdata, 4); hpsdrdata[4] = ((last_sequence_number >> 24) & 0xFF); @@ -416,8 +425,10 @@ void fillPacketToSend() { int coarse_pointer = frame * 512; // 512 bytes total in each frame int nr_samples = (nrx == 1)? 63 : (nrx == 2)? 72: (nrx ==3)? 75: 76; - read(fd_rb , rx_buffer , nr_samples); - + if (read(fd_rb , rx_buffer , nr_samples) < 0) { + fprintf(stderr, "Error %d reading frame from radioberry device\n", errno); + break; + } rb_sample = 0; for (int i=0; i< (504 / (8 + factor)); i++) { int index = 16 + coarse_pointer + (i * (8 + factor)); @@ -450,7 +461,7 @@ void fillPacketToSend() { } } -void send_control(unsigned char command) { +static void send_control(unsigned char command) { unsigned char data[6]; uint32_t command_data = commands[command]; @@ -482,7 +493,7 @@ static void *rb_control_thread(void *arg) { return NULL; } -void start_rb_control_thread() { +static void start_rb_control_thread(void) { pthread_t pid1; pthread_create(&pid1, NULL, rb_control_thread, NULL); } @@ -509,7 +520,7 @@ static void *rb_measure_thread(void *arg) { return NULL; } -void start_rb_measure_thread() { +static void start_rb_measure_thread(void) { pthread_t pid1; pthread_create(&pid1, NULL, rb_measure_thread, NULL); } @@ -524,7 +535,7 @@ static void *rb_register_thread(void *arg) { return NULL; } -void start_rb_register_thread() { +static void start_rb_register_thread(void) { pthread_t pid1; pthread_create(&pid1, NULL, rb_register_thread, NULL); } @@ -539,12 +550,12 @@ static void *timer_thread(void *arg) { return NULL; } -void start_timer_thread() { +static void start_timer_thread(void ) { pthread_t pid1; pthread_create(&pid1, NULL, timer_thread, NULL); } -void *txWriter(void *arg) { +static void *txWriter(void *arg) { gettimeofday(&t20, 0); @@ -563,7 +574,8 @@ void *txWriter(void *arg) { //first setup without EER if (MOX || CWX) { //fprintf(stderr, "I = %2X - %2X Q= %2X - %2X \n", tx_iqdata[0], tx_iqdata[1], tx_iqdata[2], tx_iqdata[3]); - write(fd_rb , tx_iqdata , sizeof(tx_iqdata)); + if (write(fd_rb , tx_iqdata , sizeof(tx_iqdata))<0) + fprintf(stderr, "Error %d writing to radioberry device\n", errno); } sem_post(&tx_empty); @@ -580,15 +592,15 @@ void *txWriter(void *arg) { } } -void put_tx_buffer(unsigned char value) { +static void put_tx_buffer(unsigned char value) { tx_buffer[fill_tx] = value; fill_tx = (fill_tx + 1) % TX_MAX_BUFFER; } -unsigned char get_tx_buffer() { +static unsigned char get_tx_buffer() { unsigned char tmp = tx_buffer[use_tx]; use_tx = (use_tx + 1) % TX_MAX_BUFFER; return tmp; } -//end of source. \ No newline at end of file +//end of source. diff --git a/SBC/rpi-4/device_driver/firmware/radioberry.h b/SBC/rpi-4/device_driver/firmware/radioberry.h index ca41d47..a9982ba 100644 --- a/SBC/rpi-4/device_driver/firmware/radioberry.h +++ b/SBC/rpi-4/device_driver/firmware/radioberry.h @@ -54,12 +54,12 @@ int pa_temp_ok = 1; uint32_t spi_commands[CAPACITY] = {0}; uint32_t p_read = 0; uint32_t p_write = 0; -mask(val) { return val & (CAPACITY - 1); } -push(val) { assert(!full()); spi_commands[mask(p_write++)] = val; } -pop() { assert(!empty()); return spi_commands[mask(p_read++)]; } -empty() { return p_read == p_write; } -full() { return size() == CAPACITY; } -size() { return p_write - p_read; } +static inline int size(void) { return p_write - p_read; } +static inline int empty(void) { return p_read == p_write; } +static inline int full(void) { return size() == CAPACITY; } +static inline int mask(int val) { return val & (CAPACITY - 1); } +static inline int push(int val) { assert(!full()); spi_commands[mask(p_write++)] = val; } +static inline int pop(void) { assert(!empty()); return spi_commands[mask(p_read++)]; } char rb_control = 0x00; @@ -75,23 +75,23 @@ int closerb = 0; int rb_sleep = 100; -int initRadioberry(); -void runRadioberry(void); -int closeRadioberry(); +static int initRadioberry(void); +static void runRadioberry(void); +static void closeRadioberry(void); -void sendPacket(void); -void handlePacket(char* buffer); -void processPacket(char* buffer); -void fillPacketToSend(void); +static void sendPacket(void); +static void handlePacket(char* buffer); +static void processPacket(char* buffer); +static void fillPacketToSend(void); -void *packetreader(void *arg); -void *txWriter(void *arg); +static void *packetreader(void *arg); +static void *txWriter(void *arg); -void send_control(unsigned char command); -float timedifference_msec(struct timeval t0, struct timeval t1); +static void send_control(unsigned char command); +static float timedifference_msec(struct timeval t0, struct timeval t1); -void put_tx_buffer(unsigned char value); -unsigned char get_tx_buffer(void); +static void put_tx_buffer(unsigned char value); +static unsigned char get_tx_buffer(void); int sock_TCP_Server = -1; int sock_TCP_Client = -1; @@ -166,4 +166,4 @@ void handle_sigint(int sig) if (running) fprintf(stderr, " SDR program is still running; please stop SDR first.\n"); closerb = 1; } -#endif \ No newline at end of file +#endif diff --git a/SBC/rpi-4/device_driver/firmware/register.c b/SBC/rpi-4/device_driver/firmware/register.c index 8634145..a610445 100644 --- a/SBC/rpi-4/device_driver/firmware/register.c +++ b/SBC/rpi-4/device_driver/firmware/register.c @@ -1,8 +1,12 @@ #include "register.h" +#include +#include +#include +#include -char macaddress[24]; -char* radiocall; -char* radiolocator; +static char macaddress[24]; +static char* radiocall; +static char* radiolocator; char gatewareversion[16]; char driverversion[16]; char firmwareversion[16]; @@ -95,14 +99,15 @@ ssize_t process_http(int sockfd, char *host, char *page, char *poststr) { char sendline[MAXLINE + 1], recvline[MAXLINE + 1]; ssize_t n; - snprintf(sendline, MAXSUB, + n = snprintf(sendline, MAXSUB, "POST %s HTTP/1.0\r\n" "Host: %s\r\n" "Content-type: application/x-www-form-urlencoded\r\n" - "Content-length: %d\r\n\r\n" + "Content-length: %zu\r\n\r\n" "%s", page, host, strlen(poststr), poststr); - write(sockfd, sendline, strlen(sendline)); + if(write(sockfd, sendline, n) < 0) + return -errno; //get response while ((n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = '\0'; @@ -149,4 +154,4 @@ void registerRadioberry() { if (postRadioberryConfiguration()) fprintf(stderr, "Registering your Radioberry not registered."); else fprintf(stderr, "Your Radioberry is registered: http://www.pa3gsb.nl/radioberry/api/read.php\n"); } -//end of source \ No newline at end of file +//end of source diff --git a/SBC/rpi-4/device_driver/firmware/register.h b/SBC/rpi-4/device_driver/firmware/register.h index 4ad35db..708b3b0 100644 --- a/SBC/rpi-4/device_driver/firmware/register.h +++ b/SBC/rpi-4/device_driver/firmware/register.h @@ -21,9 +21,6 @@ struct _PROPERTY { PROPERTY* next_property; }; -extern char macaddress[24]; -extern char* radiocall; -extern char* radiolocator; extern char gatewareversion[16]; extern char driverversion[16]; extern char firmwareversion[16]; @@ -39,4 +36,4 @@ int postRadioberryConfiguration(void); extern void registerRadioberry(void); -#endif \ No newline at end of file +#endif