From 8538d6c661a083774e5b54163eaf5cdbcadcda6d Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 5 Sep 2019 15:14:45 +0200 Subject: [PATCH] Better use of enum dev_class_t. --- src/meshlink.c | 8 ++++---- src/meshlink.h | 2 +- src/meshlink_internal.h | 2 +- src/net.c | 4 ++-- src/net_setup.c | 4 ++-- src/node.c | 2 +- src/node.h | 2 +- src/protocol_auth.c | 4 ++-- src/protocol_edge.c | 4 ++-- .../test_cases_get_all_nodes_by_dev_class.c | 2 +- 10 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/meshlink.c b/src/meshlink.c index 42573c8f..a7e94e3a 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -575,7 +575,7 @@ static bool finalize_join(meshlink_handle_t *mesh, const void *buf, uint16_t len char *name = packmsg_get_str_dup(&in); packmsg_skip_element(&in); /* submesh */ - int32_t devclass = packmsg_get_int32(&in); + dev_class_t devclass = packmsg_get_int32(&in); uint32_t count = packmsg_get_array(&in); if(!name) { @@ -1012,7 +1012,7 @@ meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const ch } } - if((int)devclass < 0 || devclass > _DEV_CLASS_MAX) { + if(devclass < 0 || devclass >= DEV_CLASS_COUNT) { logger(NULL, MESHLINK_ERROR, "Invalid devclass given!\n"); meshlink_errno = MESHLINK_EINVAL; return NULL; @@ -1237,7 +1237,7 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { } } - if((int)params->devclass < 0 || params->devclass > _DEV_CLASS_MAX) { + if(params->devclass < 0 || params->devclass >= DEV_CLASS_COUNT) { logger(NULL, MESHLINK_ERROR, "Invalid devclass given!\n"); meshlink_errno = MESHLINK_EINVAL; return NULL; @@ -1919,7 +1919,7 @@ static bool search_node_by_submesh(const node_t *node, const void *condition) { } meshlink_node_t **meshlink_get_all_nodes_by_dev_class(meshlink_handle_t *mesh, dev_class_t devclass, meshlink_node_t **nodes, size_t *nmemb) { - if(!mesh || ((int)devclass < 0) || (devclass > _DEV_CLASS_MAX) || !nmemb) { + if(!mesh || devclass < 0 || devclass >= DEV_CLASS_COUNT || !nmemb) { meshlink_errno = MESHLINK_EINVAL; return NULL; } diff --git a/src/meshlink.h b/src/meshlink.h index 06f87913..28f36273 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -79,7 +79,7 @@ typedef enum { DEV_CLASS_STATIONARY = 1, DEV_CLASS_PORTABLE = 2, DEV_CLASS_UNKNOWN = 3, - _DEV_CLASS_MAX = 3 + DEV_CLASS_COUNT } dev_class_t; /// Invitation flags diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 569628cc..3d6ac782 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -138,7 +138,7 @@ struct meshlink_handle { struct ecdsa *private_key; struct ecdsa *invitation_key; - int32_t devclass; + dev_class_t devclass; int invitation_timeout; int pinginterval; /* seconds between pings */ diff --git a/src/net.c b/src/net.c index 1df0be00..7ced1d15 100644 --- a/src/net.c +++ b/src/net.c @@ -424,7 +424,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { if(!connect_to && min_connects <= cur_connects && cur_connects < max_connects) { unsigned int connects = 0; - for(int32_t devclass = 0; devclass <= mesh->devclass; ++devclass) { + for(dev_class_t devclass = 0; devclass <= mesh->devclass; ++devclass) { for list_each(connection_t, c, mesh->connections) { if(c->status.active && c->node && c->node->devclass == devclass) { connects += 1; @@ -519,7 +519,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { if(min_connects < cur_connects /*&& cur_connects <= max_connects*/) { unsigned int connects = 0; - for(int32_t devclass = 0; devclass <= mesh->devclass; ++devclass) { + for(dev_class_t devclass = 0; devclass <= mesh->devclass; ++devclass) { for list_each(connection_t, c, mesh->connections) { if(c->status.active && c->node && c->node->devclass == devclass) { connects += 1; diff --git a/src/net_setup.c b/src/net_setup.c index ccfaacce..0045cd6f 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -85,11 +85,11 @@ bool node_read_partial(meshlink_handle_t *mesh, node_t *n) { } } - int32_t devclass = packmsg_get_int32(&in); + dev_class_t devclass = packmsg_get_int32(&in); bool blacklisted = packmsg_get_bool(&in); config_free(&config); - if(!packmsg_input_ok(&in) || devclass < 0 || devclass > _DEV_CLASS_MAX) { + if(!packmsg_input_ok(&in) || devclass < 0 || devclass >= DEV_CLASS_COUNT) { return false; } diff --git a/src/node.c b/src/node.c index 690599c5..5f5d235c 100644 --- a/src/node.c +++ b/src/node.c @@ -57,7 +57,7 @@ node_t *new_node(void) { n->edge_tree = new_edge_tree(); n->mtu = MTU; n->maxmtu = MTU; - n->devclass = _DEV_CLASS_MAX; + n->devclass = DEV_CLASS_UNKNOWN; return n; } diff --git a/src/node.h b/src/node.h index dc15e3f2..e42aac4a 100644 --- a/src/node.h +++ b/src/node.h @@ -47,7 +47,7 @@ typedef struct node_t { // Private member variables node_status_t status; uint16_t minmtu; /* Probed minimum MTU */ - int32_t devclass; + dev_class_t devclass; // Used for packet I/O sptps_t sptps; diff --git a/src/protocol_auth.c b/src/protocol_auth.c index c7d68829..a6144c5f 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -395,7 +395,7 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { return false; } - if(devclass < 0 || devclass > _DEV_CLASS_MAX) { + if(devclass < 0 || devclass >= DEV_CLASS_COUNT) { logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid"); return false; } @@ -450,7 +450,7 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { /* Create an edge_t for this connection */ - assert(devclass >= 0 && devclass <= _DEV_CLASS_MAX); + assert(devclass >= 0 && devclass < DEV_CLASS_COUNT); c->edge = new_edge(); c->edge->from = mesh->self; diff --git a/src/protocol_edge.c b/src/protocol_edge.c index 4efb1ece..74ace0cc 100644 --- a/src/protocol_edge.c +++ b/src/protocol_edge.c @@ -108,12 +108,12 @@ bool add_edge_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { // Check if devclasses are valid - if(from_devclass < 0 || from_devclass > _DEV_CLASS_MAX) { + if(from_devclass < 0 || from_devclass >= DEV_CLASS_COUNT) { logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "from devclass invalid"); return false; } - if(to_devclass < 0 || to_devclass > _DEV_CLASS_MAX) { + if(to_devclass < 0 || to_devclass >= DEV_CLASS_COUNT) { logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ADD_EDGE", c->name, "to devclass invalid"); return false; } diff --git a/test/blackbox/run_blackbox_tests/test_cases_get_all_nodes_by_dev_class.c b/test/blackbox/run_blackbox_tests/test_cases_get_all_nodes_by_dev_class.c index 6f4badf6..82f0e17f 100644 --- a/test/blackbox/run_blackbox_tests/test_cases_get_all_nodes_by_dev_class.c +++ b/test/blackbox/run_blackbox_tests/test_cases_get_all_nodes_by_dev_class.c @@ -216,7 +216,7 @@ static bool test_steps_mesh_get_node_by_dev_class_02(void) { assert(mesh_nut); meshlink_set_log_cb(mesh_nut, TEST_MESHLINK_LOG_LEVEL, log_message); - nodes = meshlink_get_all_nodes_by_dev_class(mesh_nut, _DEV_CLASS_MAX + 10, NULL, &nnodes); + nodes = meshlink_get_all_nodes_by_dev_class(mesh_nut, DEV_CLASS_COUNT + 10, NULL, &nnodes); assert_int_equal(nodes, NULL); assert_int_not_equal(meshlink_errno, 0); -- 2.39.5