Skip to content
Merged
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (NOT NO_STATIC_ANALYSIS)
set(cppcheck_options
--enable=warning,style,performance,portability
--error-exitcode=2
-UCY_CONFIG_HEADER
--suppress=missingIncludeSystem
--suppress=badBitmaskCheck
--suppress=constParameterCallback
Expand Down
455 changes: 229 additions & 226 deletions cy/cy.c

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions cy/cy.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@

#pragma once

// Define this macro to include build configuration header.
// Usage example with CMake: "-DCY_CONFIG_HEADER=\"${CMAKE_CURRENT_SOURCE_DIR}/my_cy_config.h\""
#ifdef CY_CONFIG_HEADER
#include CY_CONFIG_HEADER
#endif

// By default, this macro resolves to the standard assert().
// To disable assertion checks completely, make it expand into `(void)(0)`.
#ifndef CY_ASSERT
#include <assert.h>
#define CY_ASSERT(x) assert(x)
#endif

#define WKV_ASSERT(x) CY_ASSERT(x)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#define WKV_ASSERT(x) CY_ASSERT(x)
#ifndef WKV_ASSERT
#define WKV_ASSERT(x) CY_ASSERT(x)
#endif

#include <wild_key_value.h>

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Expand Down
50 changes: 25 additions & 25 deletions cy_can/cy_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ static bool v_canard_tx(canard_t* const self,
cy_can_t* const owner = (cy_can_t*)self->user_context;
const uint_least8_t len = (uint_least8_t)can_data.size;
(void)user_context;
assert(iface_index < owner->iface_count);
CY_ASSERT(iface_index < owner->iface_count);
if (fd && (owner->vtable->tx_fd != NULL)) {
return owner->vtable->tx_fd(owner->user, deadline, iface_index, extended_can_id, can_data.data, len);
}
Expand All @@ -323,7 +323,7 @@ static bool v_canard_tx(canard_t* const self,
static bool v_canard_filter(canard_t* const self, const size_t filter_count, const canard_filter_t* const filters)
{
cy_can_t* const owner = (cy_can_t*)self->user_context;
assert((owner != NULL) && (owner->vtable != NULL) && (owner->vtable->filter != NULL));
CY_ASSERT((owner != NULL) && (owner->vtable != NULL) && (owner->vtable->filter != NULL));
return owner->vtable->filter(owner->user, filter_count, filters);
}

Expand Down Expand Up @@ -353,11 +353,11 @@ static void v_on_msg_16b(canard_subscription_t* const self,
{
(void)transfer_id;
subject_reader_t* const reader = (subject_reader_t*)self->user_context;
assert(reader != NULL);
CY_ASSERT(reader != NULL);
cy_can_t* const owner = reader->owner;
assert(owner != NULL);
CY_ASSERT(owner != NULL);
pending_v1_t* const pending = owner->pending_v1;
assert((pending != NULL) && (pending->message.content == NULL));
CY_ASSERT((pending != NULL) && (pending->message.content == NULL));
const bool multiframe = (payload.origin.data != NULL);

can_message_t* const msg = make_message(owner, multiframe ? 0 : payload.view.size);
Expand Down Expand Up @@ -397,12 +397,12 @@ static void v_on_msg_13b(canard_subscription_t* const self,
{
(void)transfer_id;
subject_reader_t* const reader = (subject_reader_t*)self->user_context;
assert(reader != NULL);
CY_ASSERT(reader != NULL);
cy_can_t* const owner = reader->owner;
assert(owner != NULL);
CY_ASSERT(owner != NULL);
pending_v1_t* const pending = owner->pending_v1;
subject_reader_pinned_t* const pinned = as_pinned(reader);
assert((pinned != NULL) && (pending != NULL) && (pending->message.content == NULL));
CY_ASSERT((pinned != NULL) && (pending != NULL) && (pending->message.content == NULL));
const bool multiframe = (payload.origin.data != NULL);

const size_t inline_size = HEADER_BYTES + (multiframe ? 0 : payload.view.size);
Expand Down Expand Up @@ -451,9 +451,9 @@ static void v_on_msg_unicast(canard_subscription_t* const self,
{
(void)transfer_id;
cy_can_t* const owner = (cy_can_t*)self->user_context;
assert(owner != NULL);
CY_ASSERT(owner != NULL);
pending_v1_t* const pending = owner->pending_v1;
assert((pending != NULL) && (pending->message.content == NULL));
CY_ASSERT((pending != NULL) && (pending->message.content == NULL));
const bool multiframe = (payload.origin.data != NULL);

can_message_t* const msg = make_message(owner, multiframe ? 0 : payload.view.size);
Expand Down Expand Up @@ -530,7 +530,7 @@ static cy_err_t v_subject_writer_send(cy_platform_t* const platform,
const uint32_t sid = base->subject_id;
const uint_least8_t ibm = (uint_least8_t)((1U << owner->iface_count) - 1U);

assert((message.data != NULL) && (message.size >= HEADER_BYTES));
CY_ASSERT((message.data != NULL) && (message.size >= HEADER_BYTES));
const bool pinned = (sid <= CY_SUBJECT_ID_PINNED_MAX);
const bool best_effort = (((const uint_least8_t*)message.data)[0] == 0); // header_msg_be
const bool use_13b = pinned && best_effort && topic_is_compat_named(sid, message.data);
Expand Down Expand Up @@ -599,7 +599,7 @@ static void reader_set_extent(subject_reader_t* const self, const size_t extent)

static void tombstone_remove(cy_can_t* const owner, subject_reader_t* const self)
{
assert((owner != NULL) && (self != NULL));
CY_ASSERT((owner != NULL) && (self != NULL));
if (self->prev_tombstone != NULL) {
self->prev_tombstone->next_tombstone = self->next_tombstone;
} else {
Expand All @@ -616,7 +616,7 @@ static void tombstone_remove(cy_can_t* const owner, subject_reader_t* const self

static void tombstone_enqueue(cy_can_t* const owner, subject_reader_t* const self)
{
assert((owner != NULL) && (self != NULL));
CY_ASSERT((owner != NULL) && (self != NULL));
self->prev_tombstone = owner->tombstone_tail;
self->next_tombstone = NULL;
if (owner->tombstone_tail != NULL) {
Expand Down Expand Up @@ -644,11 +644,11 @@ static subject_reader_t* reader_try_revive(cy_can_t* const owner, const uint32_t
return NULL;
}
subject_reader_t* const self = (subject_reader_t*)incumbent->user_context;
assert((self != NULL) && (self->owner == owner) && (self->base.subject_id == subject_id));
CY_ASSERT((self != NULL) && (self->owner == owner) && (self->base.subject_id == subject_id));
if (as_pinned(self) != NULL) {
canard_subscription_t* const incumbent_13b =
canard_find_subscription(&owner->canard, canard_kind_message_13b, (uint16_t)subject_id);
assert((incumbent_13b != NULL) && (((subject_reader_t*)incumbent_13b->user_context) == self));
CY_ASSERT((incumbent_13b != NULL) && (((subject_reader_t*)incumbent_13b->user_context) == self));
if ((incumbent_13b == NULL) || (((subject_reader_t*)incumbent_13b->user_context) != self)) {
return NULL;
}
Expand All @@ -663,7 +663,7 @@ static subject_reader_t* reader_try_revive(cy_can_t* const owner, const uint32_t
/// Finalize a reader: unsubscribe from canard and free memory. Does NOT unlink from any list.
static void reader_finalize(cy_can_t* const owner, subject_reader_t* const self)
{
assert((owner != NULL) && (self != NULL));
CY_ASSERT((owner != NULL) && (self != NULL));
if (owner->base.cy != NULL) {
CY_TRACE(owner->base.cy, "S%08jx ptr=%p", (uintmax_t)self->base.subject_id, (void*)self);
}
Expand Down Expand Up @@ -704,7 +704,7 @@ static cy_subject_reader_t* v_subject_reader_new(cy_platform_t* const base,
extent,
CANARD_DEFAULT_TRANSFER_ID_TIMEOUT_us,
&sub_vtable_16b);
assert(sub_16b == &self->sub_16b);
CY_ASSERT(sub_16b == &self->sub_16b);
if (sub_16b != &self->sub_16b) {
owner->vtable->realloc(owner->user, self, 0);
return NULL;
Expand All @@ -715,14 +715,14 @@ static cy_subject_reader_t* v_subject_reader_new(cy_platform_t* const base,
subject_reader_pinned_t* const p = (subject_reader_pinned_t*)self;
// 13-bit payload does not include the Cy header; we prepend it ourselves.
const size_t extent_13b = (extent > HEADER_BYTES) ? (extent - HEADER_BYTES) : 0;
assert(canard_find_subscription(&owner->canard, canard_kind_message_13b, (uint16_t)subject_id) == NULL);
CY_ASSERT(canard_find_subscription(&owner->canard, canard_kind_message_13b, (uint16_t)subject_id) == NULL);
canard_subscription_t* const sub_13b = canard_subscribe_13b(&owner->canard,
&p->sub_13b,
(uint16_t)subject_id,
extent_13b,
CANARD_DEFAULT_TRANSFER_ID_TIMEOUT_us,
&sub_vtable_13b);
assert(sub_13b == &p->sub_13b);
CY_ASSERT(sub_13b == &p->sub_13b);
if (sub_13b != &p->sub_13b) {
canard_unsubscribe(&owner->canard, &self->sub_16b);
owner->vtable->realloc(owner->user, self, 0);
Expand Down Expand Up @@ -818,7 +818,7 @@ static void ingest_frame(cy_can_t* const owner, const cy_can_rx_t* const frame)
const canard_bytes_t can_data = { .size = frame->len, .data = frame->data };
pending_v1_t pending_v1 = { 0 };
pending_v0_t pending_v0 = { 0 };
assert((owner->pending_v1 == NULL) && (owner->pending_v0 == NULL));
CY_ASSERT((owner->pending_v1 == NULL) && (owner->pending_v0 == NULL));
owner->pending_v1 = &pending_v1;
owner->pending_v0 = &pending_v0;
(void)canard_ingest_frame(&owner->canard, frame->timestamp, frame->iface_index, frame->can_id, can_data);
Expand Down Expand Up @@ -1002,10 +1002,10 @@ void cy_can_destroy(cy_platform_t* const base)
if (owner == NULL) {
return;
}
assert((owner->pending_v1 == NULL) && (owner->pending_v0 == NULL));
CY_ASSERT((owner->pending_v1 == NULL) && (owner->pending_v0 == NULL));
while (owner->tombstone_head != NULL) {
subject_reader_t* const rd = tombstone_pop(owner);
assert(rd != NULL);
CY_ASSERT(rd != NULL);
reader_finalize(owner, rd);
}
canard_unsubscribe(&owner->canard, &owner->unicast_sub);
Expand Down Expand Up @@ -1033,18 +1033,18 @@ static void v_on_msg_v0(canard_subscription_t* const self,
const canard_payload_t payload)
{
cy_can_v0_subscription_t* const sub = (cy_can_v0_subscription_t*)self->user_context;
assert((sub != NULL) && (sub->owner != NULL));
CY_ASSERT((sub != NULL) && (sub->owner != NULL));
cy_can_t* const owner = sub->owner;
pending_v0_t* const pending = owner->pending_v0;
assert((pending != NULL) && (pending->payload.view.data == NULL));
CY_ASSERT((pending != NULL) && (pending->payload.view.data == NULL));
pending->subscription = sub;
pending->timestamp = timestamp;
pending->priority = priority;
pending->source_node_id = source_node_id;
pending->transfer_id = transfer_id;
pending->payload = payload;
if (payload.origin.data == NULL) {
assert(payload.view.size <= sizeof(pending->single_frame_data));
CY_ASSERT(payload.view.size <= sizeof(pending->single_frame_data));
(void)memcpy(pending->single_frame_data, payload.view.data, payload.view.size);
pending->payload.view.data = pending->single_frame_data;
}
Expand Down
11 changes: 5 additions & 6 deletions cy_can/cy_can_socketcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#define RAPIDHASH_COMPACT
#include <rapidhash.h>

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Expand Down Expand Up @@ -63,7 +62,7 @@ static bool v_tx_classic(void* const user,
{
const socketcan_t* const self = (const socketcan_t*)user;
(void)deadline;
assert(iface_index < self->iface_count);
CY_ASSERT(iface_index < self->iface_count);
struct can_frame frame = { .can_id = can_id | CAN_EFF_FLAG, .can_dlc = len };
if ((data != NULL) && (len > 0)) {
(void)memcpy(frame.data, data, (len <= 8) ? len : 8);
Expand All @@ -81,7 +80,7 @@ static bool v_tx_fd(void* const user,
{
const socketcan_t* const self = (const socketcan_t*)user;
(void)deadline;
assert(iface_index < self->iface_count);
CY_ASSERT(iface_index < self->iface_count);
struct canfd_frame frame = { .can_id = can_id | CAN_EFF_FLAG, .len = len, .flags = CANFD_FDF };
if ((data != NULL) && (len > 0)) {
(void)memcpy(frame.data, data, (len <= 64) ? len : 64);
Expand Down Expand Up @@ -214,8 +213,8 @@ static const cy_can_vtable_t socketcan_vtable_classic = { .tx_classic = v_tx_cla

static bool socketcan_iface_is_fd_capable(const int sock, const char* const iface_name)
{
assert(sock >= 0);
assert(iface_name != NULL);
CY_ASSERT(sock >= 0);
CY_ASSERT(iface_name != NULL);
struct ifreq ifr;
(void)memset(&ifr, 0, sizeof(ifr));
(void)strncpy(ifr.ifr_name, iface_name, sizeof(ifr.ifr_name) - 1);
Expand All @@ -227,7 +226,7 @@ static bool socketcan_iface_is_fd_capable(const int sock, const char* const ifac

static bool socketcan_set_fd_frames(const socketcan_t* const self, const bool enable)
{
assert(self != NULL);
CY_ASSERT(self != NULL);
const int value = enable ? 1 : 0;
for (uint_least8_t i = 0; i < self->iface_count; i++) {
if (setsockopt(self->sock_fd[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &value, sizeof(value)) < 0) {
Expand Down
Loading