router.c 11.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
/*
 * Copyright (C) 2016 OTA keys S.A.
 *
 * This file is subject to the terms and conditions of the GNU Lesser General
 * Public License v2.1. See the file LICENSE in the top level directory for more
 * details.
 */

/**
 * @ingroup     sys_can_dll
 * @{
 * @file
 * @brief       Functions for routing RX can frames
 *
 * @author      Toon Stegen <toon.stegen@altran.com>
 * @author      Vincent Dupont <vincent@otakeys.com>
 * @}
 */

#include <stdint.h>
#include <errno.h>

#include "kernel_defines.h"

#include "net/gnrc/pktbuf.h"

#include "can/router.h"
#include "can/pkt.h"
#include "can/device.h"
#include "utlist.h"
#include "mutex.h"
#include "assert.h"

#ifdef MODULE_CAN_MBOX
#include "mbox.h"
#endif

#define ENABLE_DEBUG (0)
#include "debug.h"

#if ENABLE_DEBUG
#include <inttypes.h>
#endif

/**
 * This is a can_id element
 */
typedef struct filter_el {
    can_reg_entry_t entry;   /**< filter entry */
    canid_t can_id;          /**< CAN ID of the element */
    canid_t mask;            /**< Mask of the element */
    void *data;              /**< Private data */
    gnrc_pktsnip_t *snip;    /**< Pointer to the allocated snip */
} filter_el_t;

/**
 * This table contains @p CAN_ROUTER_APP_MAX lists of CAN IDs per interface
 */
static can_reg_entry_t *table[CAN_DLL_NUMOF];


static mutex_t lock = MUTEX_INIT;

static filter_el_t *_alloc_filter_el(canid_t can_id, canid_t mask, void *data);
static void _free_filter_el(filter_el_t *el);
static void _insert_to_list(can_reg_entry_t **list, filter_el_t *el);
static filter_el_t *_find_filter_el(can_reg_entry_t *list, can_reg_entry_t *entry, canid_t can_id, canid_t mask, void *data);
static int _filter_is_used(unsigned int ifnum, canid_t can_id, canid_t mask);

#if ENABLE_DEBUG
static void _print_filters(void)
{
    for (int i = 0; i < (int)CAN_DLL_NUMOF; i++) {
        DEBUG("--- Ifnum: %d ---\n", i);
        can_reg_entry_t *entry;
        LL_FOREACH(table[i], entry) {
            filter_el_t *el = container_of(entry, filter_el_t, entry);
            DEBUG("App pid=%" PRIkernel_pid ", el=%p, can_id=0x%" PRIx32 ", mask=0x%" PRIx32 ", data=%p\n",
                  el->entry.target.pid, (void*)el, el->can_id, el->mask, el->data);
        }
    }
}

#define PRINT_FILTERS() _print_filters()
#else
#define PRINT_FILTERS()
#endif

static filter_el_t *_alloc_filter_el(canid_t can_id, canid_t mask, void *data)
{
    filter_el_t *el;
    gnrc_pktsnip_t *snip = gnrc_pktbuf_add(NULL, NULL, sizeof(*el), GNRC_NETTYPE_UNDEF);
    if (!snip) {
        DEBUG("can_router: _alloc_canid_el: out of memory\n");
        return NULL;
    }

    el = snip->data;
    el->can_id = can_id;
    el->mask = mask;
    el->data = data;
    el->entry.next = NULL;
    el->snip = snip;
    DEBUG("_alloc_canid_el: el allocated with can_id=0x%" PRIx32 ", mask=0x%" PRIx32
          ", data=%p\n", can_id, mask, data);
    return el;
}

static void _free_filter_el(filter_el_t *el)
{
    DEBUG("_free_canid_el: el freed with can_id=0x%" PRIx32 ", mask=0x%" PRIx32
          ", data=%p\n", el->can_id, el->mask, el->data);

    gnrc_pktbuf_release(el->snip);
}

/* Insert to the list in a sorted way
 * Lower CAN IDs are inserted first */
static void _insert_to_list(can_reg_entry_t **list, filter_el_t *el)
{
    can_reg_entry_t *next_entry = *list;
    filter_el_t *next_el = container_of(next_entry, filter_el_t, entry);

    DEBUG("_insert_to_list: list=%p, el=%p\n", (void *)list, (void *)el);

    if (!(*list) || (next_el->can_id > el->can_id)) {
        LL_PREPEND(*list, &el->entry);
        DEBUG("_insert_to_list: inserting first el, list=%p\n", (void *)list);
    }
    else {
        do {
            if (el->can_id <= next_el->can_id) {
                DEBUG("_insert_to_list: found next_el can_id:0x%" PRIx32
                      "\n", next_el->can_id);
                LL_PREPEND_ELEM(*list, next_entry, &el->entry);
                return;
            }
            else if (next_el->entry.next == NULL) {
                DEBUG("_insert_to_list: insert at the end\n");
                LL_APPEND(next_entry, &el->entry);
                return;
            }
            else {
                next_entry = next_entry->next;
                next_el = container_of(next_entry, filter_el_t, entry);
                DEBUG("_insert_to_list: going to next el: %p\n", (void*) next_el);
            }
        } while (next_el);

    }
}

#ifdef MODULE_CAN_MBOX
#define ENTRY_MATCHES(e1, e2) (((e1)->type == (e2)->type) && \
    (((e1)->type == CAN_TYPE_DEFAULT && (e1)->target.pid == (e2)->target.pid) ||\
    ((e1)->type == CAN_TYPE_MBOX && (e1)->target.mbox == (e2)->target.mbox)))
#else
#define ENTRY_MATCHES(e1, e2)  ((e1)->target.pid == (e2)->target.pid)
#endif

static filter_el_t *_find_filter_el(can_reg_entry_t *list, can_reg_entry_t *entry, canid_t can_id, canid_t mask, void *data)
{
    filter_el_t *el = container_of(list, filter_el_t, entry);
    if (!el) {
        return el;
    }
    do {
        if ((el->can_id == can_id) && (el->mask == mask) && (el->data == data) &&
                ENTRY_MATCHES(&el->entry, entry)) {
            DEBUG("_find_filter_el: found el=%p, can_id=%" PRIx32 ", mask=%" PRIx32 ", data=%p\n",
                  (void *)el, el->can_id, el->mask, el->data);
            return el;
        }
        el = container_of(el->entry.next, filter_el_t, entry);
    }  while (el);

    return NULL;
}

static int _filter_is_used(unsigned int ifnum, canid_t can_id, canid_t mask)
{
    filter_el_t *el = container_of(table[ifnum], filter_el_t, entry);
    if (!el) {
        DEBUG("_filter_is_used: empty list\n");
        return 0;
    }
    do {
        if ((el->can_id == can_id) && (el->mask == mask)) {
            DEBUG("_filter_is_used: found el=%p, can_id=%" PRIx32 ", mask=%" PRIx32 ", data=%p\n",
                  (void *)el, el->can_id, el->mask, el->data);
            return 1;
        }
        el = container_of(el->entry.next, filter_el_t, entry);
    }  while (el);

    DEBUG("_filter_is_used: filter not found\n");

    return 0;
}

/* register interested users */
int can_router_register(can_reg_entry_t *entry, canid_t can_id, canid_t mask, void *param)
{
    filter_el_t *filter;
    int ret;

#if ENABLE_DEBUG
    if (entry->type == CAN_TYPE_DEFAULT) {
        DEBUG("can_router_register: ifnum=%d, pid=%" PRIkernel_pid ", can_id=0x%" PRIx32
              ", mask=0x%" PRIx32 ", data=%p\n", entry->ifnum, entry->target.pid, can_id, mask, param);
    } else if (entry->type == CAN_TYPE_MBOX) {
        DEBUG("can_router_register: ifnum=%d, mbox=%p, can_id=0x%" PRIx32
              ", mask=0x%" PRIx32 ", data=%p\n", entry->ifnum, (void *)entry->target.mbox, can_id, mask, param);
    }
#endif

    mutex_lock(&lock);
    ret = _filter_is_used(entry->ifnum, can_id, mask);

    filter = _alloc_filter_el(can_id, mask, param);
    if (!filter) {
        mutex_unlock(&lock);
        return -ENOMEM;
    }

#ifdef MODULE_CAN_MBOX
    filter->entry.type = entry->type;
    switch (entry->type) {
    case CAN_TYPE_DEFAULT:
        filter->entry.target.pid = entry->target.pid;
        break;
    case CAN_TYPE_MBOX:
        filter->entry.target.mbox = entry->target.mbox;
        break;
    }

#else
    filter->entry.target.pid = entry->target.pid;
#endif
    filter->entry.ifnum = entry->ifnum;
    _insert_to_list(&table[entry->ifnum], filter);
    mutex_unlock(&lock);

    PRINT_FILTERS();

    return ret;
}

/* unregister interested users */
int can_router_unregister(can_reg_entry_t *entry, canid_t can_id,
                          canid_t mask, void *param)
{
    filter_el_t *el;
    int ret;

#if ENABLE_DEBUG
    if (entry->type == CAN_TYPE_DEFAULT) {
        DEBUG("can_router_unregister: ifnum=%d, pid=%" PRIkernel_pid ", can_id=0x%" PRIx32
              ", mask=0x%" PRIx32 ", data=%p", entry->ifnum, entry->target.pid, can_id, mask, param);
    } else if (entry->type == CAN_TYPE_MBOX) {
        DEBUG("can_router_unregister: ifnum=%d, mbox=%p, can_id=0x%" PRIx32
              ", mask=0x%" PRIx32 ", data=%p\n", entry->ifnum, (void *)entry->target.mbox, can_id, mask, param);
    }
#endif

    mutex_lock(&lock);
    el = _find_filter_el(table[entry->ifnum], entry, can_id, mask, param);
    if (!el) {
        mutex_unlock(&lock);
        return -EINVAL;
    }
    LL_DELETE(table[entry->ifnum], &el->entry);
    _free_filter_el(el);
    ret = _filter_is_used(entry->ifnum, can_id, mask);
    mutex_unlock(&lock);

    PRINT_FILTERS();

    return ret;
}

static int _send_msg(msg_t *msg, can_reg_entry_t *entry)
{
#ifdef MODULE_CAN_MBOX
    switch (entry->type) {
    case CAN_TYPE_DEFAULT:
        return msg_try_send(msg, entry->target.pid);
    case CAN_TYPE_MBOX:
        DEBUG("_send_msg: sending msg=%p to mbox=%p\n", (void *)msg, (void *)entry->target.mbox);
        return mbox_try_put(entry->target.mbox, msg);
    default:
        return -ENOTSUP;
    }
#else
    return msg_try_send(msg, entry->target.pid);
#endif
}

/* send received pkt to all interested users */
int can_router_dispatch_rx_indic(can_pkt_t *pkt)
{
    if (!pkt) {
        DEBUG("can_router_dispatch_rx_indic: invalid pkt\n");
        return -EINVAL;
    }

    int res = 0;
    msg_t msg;
    msg.type = CAN_MSG_RX_INDICATION;
#if ENABLE_DEBUG
    int msg_cnt = 0;
#endif
    DEBUG("can_router_dispatch_rx_indic: pkt=%p, ifnum=%d, can_id=%" PRIx32 "\n",
          (void *)pkt, pkt->entry.ifnum, pkt->frame.can_id);

    mutex_lock(&lock);
    can_reg_entry_t *entry;
    filter_el_t *el;
    LL_FOREACH(table[pkt->entry.ifnum], entry) {
        el = container_of(entry, filter_el_t, entry);
        if ((pkt->frame.can_id & el->mask) == el->can_id) {
            DEBUG("can_router_dispatch_rx_indic: found el=%p, data=%p\n",
                  (void *)el, (void *)el->data);
            DEBUG("can_router_dispatch_rx_indic: rx_ind to pid: %"
                  PRIkernel_pid "\n", entry->target.pid);
            atomic_fetch_add(&pkt->ref_count, 1);
            msg.content.ptr = can_pkt_alloc_rx_data(&pkt->frame, sizeof(pkt->frame), el->data);
#if ENABLE_DEBUG
            msg_cnt++;
#endif
            if (!msg.content.ptr || (_send_msg(&msg, entry) <= 0)) {
                can_pkt_free_rx_data(msg.content.ptr);
                atomic_fetch_sub(&pkt->ref_count, 1);
                DEBUG("can_router_dispatch_rx_indic: failed to send msg to "
                      "pid=%" PRIkernel_pid "\n", entry->target.pid);
                res = -EBUSY;
                break;
            }
        }
    }
    mutex_unlock(&lock);
#if ENABLE_DEBUG
    DEBUG("can_router_dispatch_rx: msg send to %d threads\n", msg_cnt);
#endif
    if (atomic_load(&pkt->ref_count) == 0) {
        can_pkt_free(pkt);
    }

    return res;
}

int can_router_dispatch_tx_conf(can_pkt_t *pkt)
{
    msg_t msg;
    msg.type = CAN_MSG_TX_CONFIRMATION;
    msg.content.value = pkt->handle;

    DEBUG("can_router_dispatch_tx_conf: frame=%p, pid=%" PRIkernel_pid "\n",
          (void *)&pkt->frame, pkt->entry.target.pid);

    if (_send_msg(&msg, &pkt->entry) <= 0) {
        return -1;
    }

    return 0;
}

int can_router_dispatch_tx_error(can_pkt_t *pkt)
{
    msg_t msg;
    msg.type = CAN_MSG_TX_ERROR;
    msg.content.value = pkt->handle;

    DEBUG("can_router_dispatch_tx_error: frame=%p, pid=%" PRIkernel_pid "\n",
          (void *)&pkt->frame, pkt->entry.target.pid);

    if (_send_msg(&msg, &pkt->entry) <= 0) {
        return -1;
    }

    return 0;
}

int can_router_free_frame(struct can_frame *frame)
{
    can_pkt_t *pkt = NULL;

    pkt = container_of(frame, can_pkt_t, frame);

    DEBUG("can_router_free_frame: pkt=%p\n", (void*) pkt);

    if (!pkt || (atomic_load(&pkt->ref_count) <= 0)) {
        return -1;
    }

    atomic_fetch_sub(&pkt->ref_count, 1);

    if (atomic_load(&pkt->ref_count) == 0) {
        can_pkt_free(pkt);
    }
    return 0;
}