Skip to content

Commit

Permalink
Cleanup in configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
johandc committed Sep 12, 2021
1 parent b5355e4 commit bd15417
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 97 deletions.
23 changes: 1 addition & 22 deletions include/csp/csp.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,13 @@ enum csp_dedup_types {
@see csp_init()
*/
typedef struct csp_conf_s {

uint8_t version; /**< Protocol version to use (either 1 or 2) */
uint16_t address; /**< CSP address of the system */

const char *hostname; /**< Host name, returned by the #CSP_CMP_IDENT request */
const char *model; /**< Model, returned by the #CSP_CMP_IDENT request */
const char *revision; /**< Revision, returned by the #CSP_CMP_IDENT request */

uint8_t conn_max; /**< Max number of connections. A fixed connection array is allocated by csp_init() */
uint8_t conn_queue_length; /**< Max queue length (max queued Rx messages). */

uint8_t fifo_length; /**< DEPRECATED: This is now compile time define */

uint8_t port_max_bind; /**< Max/highest port for use with csp_bind() */
char * port_buffer; /**< Must be sizeof(csp_port_t) * (port_max_bind + 2) or NULL if dynamic */

uint8_t rdp_max_window; /**< Max RDP window size */
uint16_t buffers; /**< Number of CSP buffers */
uint16_t buffer_data_size; /**< Data size of a CSP buffer. Total size will be sizeof(#csp_packet_t) + data_size. */
uint32_t conn_dfl_so; /**< Default connection options. Options will always be or'ed onto new connections, see csp_connect() */
uint8_t dedup; /**< Enable CSP deduplication. 0 = off, 1 = always on, 2 = only on forwarded packets, */

uint8_t version; /**< Protocol version to use (either 1 or 2) */
} csp_conf_t;

/**
Expand All @@ -89,12 +74,6 @@ static inline void csp_conf_get_defaults(csp_conf_t * conf) {
conf->hostname = "hostname";
conf->model = "model";
conf->revision = "revision";
conf->conn_max = 10;
conf->conn_queue_length = 10;
conf->port_max_bind = 24;
conf->rdp_max_window = 20;
conf->buffers = 10;
conf->buffer_data_size = 256;
conf->conn_dfl_so = CSP_O_NONE;
conf->dedup = CSP_DEDUP_OFF;
}
Expand Down
2 changes: 2 additions & 0 deletions include/csp/csp_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ size_t csp_buffer_size(void);
*/
size_t csp_buffer_data_size(void);

void csp_buffer_init(void);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ conf.set('CSP_QFIFO_LEN', get_option('qfifo_len'))
conf.set('CSP_PORT_MAX_BIND', get_option('port_max_bind'))
conf.set('CSP_CONN_RXQUEUE_LEN', get_option('conn_rxqueue_len'))
conf.set('CSP_CONN_MAX', get_option('conn_max'))
conf.set('CSP_BUFFER_SIZE', get_option('buffer_size'))
conf.set('CSP_BUFFER_COUNT', get_option('buffer_count'))
conf.set('CSP_RDP_MAX_WINDOW', get_option('rdp_max_window'))


zmq_dep = dependency('libzmq', version: '>4.0', required: false)
Expand Down
4 changes: 3 additions & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ option('qfifo_len', type: 'integer', value: 15, description: 'Length of incoming
option('port_max_bind', type: 'integer', value: 16, description: 'Length of incoming queue for router task')
option('conn_rxqueue_len', type: 'integer', value: 15, description: 'Number of packets in connection queue')
option('conn_max', type: 'integer', value: 10, description: 'Number of new connections on socket queue')

option('buffer_size', type: 'integer', value: 256, description: 'Bytes in each packet buffer')
option('buffer_count', type: 'integer', value: 15, description: 'Number of total packet buffers')
option('rdp_max_window', type: 'integer', value: 20, description: 'Max window size for RDP')
52 changes: 16 additions & 36 deletions src/csp_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,40 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#define CSP_BUFFER_ALIGN (sizeof(int *))
#endif


/** Internal buffer header */
typedef struct csp_skbf_s {
unsigned int refcount;
void * skbf_addr;
char skbf_data[]; // -> csp_packet_t
} csp_skbf_t;

#define SKBUF_SIZE CSP_BUFFER_ALIGN * ((sizeof(csp_skbf_t) + CSP_BUFFER_SIZE + (CSP_BUFFER_ALIGN - 1)) / CSP_BUFFER_ALIGN)

// Queue of free CSP buffers
static csp_queue_handle_t csp_buffers;
// Chunk of memory allocated for CSP buffers
static char * csp_buffer_pool;

int csp_buffer_init(void) {

// calculate total size and ensure correct alignment (int *) for buffers
const unsigned int skbfsize = CSP_BUFFER_ALIGN * ((sizeof(csp_skbf_t) + csp_buffer_size() + (CSP_BUFFER_ALIGN - 1)) / CSP_BUFFER_ALIGN);
void csp_buffer_init(void) {

csp_buffer_pool = csp_malloc(csp_conf.buffers * skbfsize);
if (csp_buffer_pool == NULL)
goto fail_malloc;
// Chunk of memory allocated for CSP buffers
static char csp_buffer_pool[SKBUF_SIZE * CSP_BUFFER_COUNT];

csp_buffers = csp_queue_create(csp_conf.buffers, sizeof(void *));
if (!csp_buffers)
goto fail_queue;
static csp_static_queue_t csp_buffers_queue;
static char csp_buffer_queue_data[CSP_BUFFER_COUNT * sizeof(csp_skbf_t *)];
csp_buffers = csp_queue_create_static(CSP_BUFFER_COUNT, sizeof(csp_skbf_t *), csp_buffer_queue_data, &csp_buffers_queue);

for (unsigned int i = 0; i < csp_conf.buffers; i++) {
csp_skbf_t * buf = (void *) &csp_buffer_pool[i * skbfsize];
for (unsigned int i = 0; i < CSP_BUFFER_COUNT; i++) {
csp_skbf_t * buf = (void *) &csp_buffer_pool[i * SKBUF_SIZE];
buf->skbf_addr = buf;
csp_queue_enqueue(csp_buffers, &buf, 0);
}

return CSP_ERR_NONE;

fail_queue:
csp_buffer_free_resources();
fail_malloc:
return CSP_ERR_NOMEM;

}

void csp_buffer_free_resources(void) {

if (csp_buffers) {
csp_queue_remove(csp_buffers);
csp_buffers = NULL;
}
csp_free(csp_buffer_pool);
csp_buffer_pool = NULL;

}

void *csp_buffer_get_isr(size_t _data_size) {

if (_data_size > csp_conf.buffer_data_size)
if (_data_size > CSP_BUFFER_SIZE)
return NULL;

csp_skbf_t * buffer = NULL;
Expand All @@ -101,8 +81,8 @@ void *csp_buffer_get_isr(size_t _data_size) {

void *csp_buffer_get(size_t _data_size) {

if (_data_size > csp_conf.buffer_data_size) {
csp_log_error("GET: Attempt to allocate too large data size %u > max %u", (unsigned int) _data_size, (unsigned int) csp_conf.buffer_data_size);
if (_data_size > CSP_BUFFER_SIZE) {
csp_log_error("GET: Too large data size %u > max %u", (unsigned int) _data_size, (unsigned int) CSP_BUFFER_SIZE);
return NULL;
}

Expand Down Expand Up @@ -209,9 +189,9 @@ int csp_buffer_remaining(void) {
}

size_t csp_buffer_size(void) {
return (csp_conf.buffer_data_size + CSP_BUFFER_PACKET_OVERHEAD);
return (CSP_BUFFER_SIZE + CSP_BUFFER_PACKET_OVERHEAD);
}

size_t csp_buffer_data_size(void) {
return csp_conf.buffer_data_size;
return CSP_BUFFER_SIZE;
}
14 changes: 4 additions & 10 deletions src/csp_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <csp/csp.h>
#include <csp/arch/csp_queue.h>
#include <csp/arch/csp_semaphore.h>
#include <csp/arch/csp_malloc.h>
#include <csp/arch/csp_time.h>
#include <csp/csp_id.h>
#include <csp_autoconfig.h>
Expand Down Expand Up @@ -73,7 +72,7 @@ int csp_conn_enqueue_packet(csp_conn_t * conn, csp_packet_t * packet) {
return CSP_ERR_NONE;
}

int csp_conn_init(void) {
void csp_conn_init(void) {

csp_bin_sem_create_static(&conn_lock, &conn_lock_buf);
csp_bin_sem_create_static(&sport_lock, &sport_lock_buf);
Expand All @@ -89,16 +88,11 @@ int csp_conn_init(void) {

conn->rx_queue = csp_queue_create_static(CSP_CONN_RXQUEUE_LEN, sizeof(csp_packet_t *), conn->rx_queue_static_data, &conn->rx_queue_static);


#if (CSP_USE_RDP)
if (csp_rdp_init(conn) != CSP_ERR_NONE) {
csp_log_error("csp_rdp_allocate(conn) failed");
return CSP_ERR_NOMEM;
}
csp_rdp_init(conn);
#endif
}

return CSP_ERR_NONE;
}

}

Expand Down Expand Up @@ -376,7 +370,7 @@ csp_conn_t * csp_connect(uint8_t prio, uint16_t dest, uint8_t dport, uint32_t ti
const uint8_t start = sport;
while (++sport != start) {
if (sport > csp_id_get_max_port())
sport = csp_conf.port_max_bind + 1;
sport = CSP_PORT_MAX_BIND + 1;

/* Search for ephemeral outgoing port */
if (csp_conn_find_dport(sport) == NULL) {
Expand Down
3 changes: 1 addition & 2 deletions src/csp_conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct csp_conn_s {
};

int csp_conn_enqueue_packet(csp_conn_t * conn, csp_packet_t * packet);
int csp_conn_init(void);
void csp_conn_init(void);
csp_conn_t * csp_conn_allocate(csp_conn_type_t type);

csp_conn_t * csp_conn_find_existing(csp_id_t * id);
Expand All @@ -118,7 +118,6 @@ int csp_conn_get_rxq(int prio);
int csp_conn_close(csp_conn_t * conn, uint8_t closed_by);

const csp_conn_t * csp_conn_get_array(size_t * size); // for test purposes only!
void csp_conn_free_resources(void);

#ifdef __cplusplus
}
Expand Down
15 changes: 2 additions & 13 deletions src/csp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,8 @@ int csp_init(const csp_conf_t * conf) {
* unless specific get/set functions are made */
memcpy(&csp_conf, conf, sizeof(csp_conf));

int ret = csp_buffer_init();
if (ret != CSP_ERR_NONE) {
return ret;
}

ret = csp_conn_init();
if (ret != CSP_ERR_NONE) {
return ret;
}

csp_buffer_init();
csp_conn_init();
csp_qfifo_init();

/* Loopback */
Expand All @@ -73,9 +65,6 @@ int csp_init(const csp_conf_t * conf) {
void csp_free_resources(void) {

csp_rtable_free();
csp_conn_free_resources();
csp_buffer_free_resources();
memset(&csp_conf, 0, sizeof(csp_conf));

}

Expand Down
3 changes: 0 additions & 3 deletions src/csp_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ extern "C" {

extern csp_conf_t csp_conf;

int csp_buffer_init(void);
void csp_buffer_free_resources(void);

#ifdef __cplusplus
}
#endif
Expand Down
14 changes: 7 additions & 7 deletions src/csp_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static csp_port_t ports[CSP_PORT_MAX_BIND + 2];

csp_socket_t * csp_port_get_socket(unsigned int port) {

if (port > csp_conf.port_max_bind) {
if (port > CSP_PORT_MAX_BIND) {
return NULL;
}

Expand All @@ -55,8 +55,8 @@ csp_socket_t * csp_port_get_socket(unsigned int port) {
return ports[port].socket;
}

if (ports[csp_conf.port_max_bind + 1].state == PORT_OPEN) {
return ports[csp_conf.port_max_bind + 1].socket;
if (ports[CSP_PORT_MAX_BIND + 1].state == PORT_OPEN) {
return ports[CSP_PORT_MAX_BIND + 1].socket;
}

return NULL;
Expand All @@ -73,14 +73,14 @@ int csp_bind(csp_socket_t * socket, uint8_t port) {
return CSP_ERR_INVAL;

if (port == CSP_ANY) {
port = csp_conf.port_max_bind + 1;
} else if (port > csp_conf.port_max_bind) {
csp_log_error("csp_bind: invalid port %u, only ports from 0-%u (+ CSP_ANY for default) are available for incoming ports", port, csp_conf.port_max_bind);
port = CSP_PORT_MAX_BIND + 1;
} else if (port > CSP_PORT_MAX_BIND) {
csp_log_error("csp_bind: invalid port %u", port);
return CSP_ERR_INVAL;
}

if (ports[port].state != PORT_CLOSED) {
csp_log_error("Port %d is already in use, state %u", port, ports[port].state);
csp_log_error("Port %d is already in use", port);
return CSP_ERR_USED;
}

Expand Down
6 changes: 3 additions & 3 deletions src/transport/csp_rdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ void csp_rdp_flush_all(csp_conn_t * conn) {
int csp_rdp_check_ack(csp_conn_t * conn) {

/* Check RX queue for spare capacity */
if (csp_conf.conn_queue_length - csp_queue_size(conn->rx_queue) <= 2 * (int32_t)conn->rdp.window_size) {
if (CSP_CONN_RXQUEUE_LEN - csp_queue_size(conn->rx_queue) <= 2 * (int32_t)conn->rdp.window_size) {
return CSP_ERR_NONE;
}

Expand Down Expand Up @@ -1017,15 +1017,15 @@ int csp_rdp_init(csp_conn_t * conn) {
csp_bin_sem_create_static(&conn->rdp.tx_wait, &conn->rdp.tx_wait_buf);

/* Create TX queue */
conn->rdp.tx_queue = csp_queue_create(csp_conf.rdp_max_window, sizeof(csp_packet_t *));
conn->rdp.tx_queue = csp_queue_create(CSP_RDP_MAX_WINDOW, sizeof(csp_packet_t *));
if (conn->rdp.tx_queue == NULL) {
csp_log_error("RDP %p: Failed to create TX queue for conn", conn);
csp_bin_sem_remove(&conn->rdp.tx_wait);
return CSP_ERR_NOMEM;
}

/* Create RX queue */
conn->rdp.rx_queue = csp_queue_create(csp_conf.rdp_max_window * 2, sizeof(csp_packet_t *));
conn->rdp.rx_queue = csp_queue_create(CSP_RDP_MAX_WINDOW * 2, sizeof(csp_packet_t *));
if (conn->rdp.rx_queue == NULL) {
csp_log_error("RDP %p: Failed to create RX queue for conn", conn);
csp_bin_sem_remove(&conn->rdp.tx_wait);
Expand Down

0 comments on commit bd15417

Please sign in to comment.