dll.c 11.7 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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
/*
 * 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       CAN Data Link Layer module
 *
 * This module contains the DLL interfaces for upper layer (raw_can_*)
 * and devices (can_dll_*).
 * It manages the connection between an device number and its candev thread.
 *
 *
 * @author      Toon Stegen <toon.stegen@altran.com>
 * @author      Vincent Dupont <vincent@otakeys.com>
 * @author      Aurelien Gonce <aurelien.gonce@altran.com>
 * @}
 */

#include <errno.h>
#include <string.h>

#include "thread.h"
#include "can/dll.h"
#include "can/raw.h"
#include "can/device.h"
#include "can/pkt.h"
#include "can/common.h"
#include "can/router.h"
#include "utlist.h"

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

static candev_dev_t *candev_list[CAN_DLL_NUMOF];
static int candev_nb = 0;
static can_reg_entry_t *tx_list[CAN_DLL_NUMOF];
static mutex_t tx_lock = MUTEX_INIT;

static int _get_ifnum(kernel_pid_t pid)
{
    for (int i = 0; i < candev_nb; i++) {
        if (candev_list[i]->pid == pid) {
            return i;
        }
    }

    return -ENODEV;
}

int _send_pkt(can_pkt_t *pkt)
{
    if (!pkt) {
        return -ENOMEM;
    }

    msg_t msg;
    int handle = pkt->handle;

    mutex_lock(&tx_lock);
    LL_APPEND(tx_list[pkt->entry.ifnum], &pkt->entry);
    mutex_unlock(&tx_lock);

    msg.type = CAN_MSG_SEND_FRAME;
    msg.content.ptr = (void*) pkt;

    if (msg_send(&msg, candev_list[pkt->entry.ifnum]->pid) <= 0) {
        return -EOVERFLOW;
    }

    return handle;
}

int raw_can_send(int ifnum, const struct can_frame *frame, kernel_pid_t pid)
{
    can_pkt_t *pkt;

    assert(frame);
    assert(ifnum < candev_nb);

    pkt = can_pkt_alloc_tx(ifnum, frame, pid);

    DEBUG("raw_can_send: ifnum=%d, id=0x%" PRIx32 " from pid=%" PRIkernel_pid ", handle=%d\n",
          ifnum, frame->can_id, pid, pkt->handle);

    return _send_pkt(pkt);
}

#ifdef MODULE_CAN_MBOX
int raw_can_send_mbox(int ifnum, const struct can_frame *frame, mbox_t *mbox)
{
    can_pkt_t *pkt;

    assert(frame);
    assert(ifnum < candev_nb);

    pkt = can_pkt_alloc_mbox_tx(ifnum, frame, mbox);

    DEBUG("raw_can_send: ifnum=%d, id=0x%" PRIx32 ", handle=%d\n", ifnum, frame->can_id, pkt->handle);

    return _send_pkt(pkt);
}
#endif

int raw_can_abort(int ifnum, int handle)
{
    msg_t msg, reply;
    can_pkt_t *pkt = NULL;
    can_reg_entry_t *entry;

    assert(ifnum < candev_nb);

    DEBUG("raw_can_abort: ifnum=%u, handle=%d\n", ifnum, handle);

    mutex_lock(&tx_lock);
    LL_FOREACH(tx_list[ifnum], entry) {
        pkt = container_of(entry, can_pkt_t, entry);
        if (pkt->handle == handle) {
            break;
        }
    }

    LL_DELETE(tx_list[ifnum], entry);
    mutex_unlock(&tx_lock);

    if (pkt == NULL) {
        DEBUG("raw_can_abort: no pkt\n");
        return -ENODEV;
    }

    msg.type = CAN_MSG_ABORT_FRAME;
    msg.content.ptr = pkt;

    msg_send_receive(&msg, &reply, candev_list[ifnum]->pid);

    can_pkt_free(pkt);

    return 0;
}

static int register_filter_entry(can_reg_entry_t *entry, struct can_filter *filter, void *param)
{
    msg_t msg, reply;
    int ret;

    DEBUG("register_filter_entry: ifnum=%d, filter=0x%" PRIx32 ", mask=0x%" PRIx32 ", param=%p\n",
          entry->ifnum, filter->can_id, filter->can_mask, param);

    ret = can_router_register(entry, filter->can_id, filter->can_mask, param);
    if (ret < 0) {
        return -ENOMEM;
    }
    else if (ret == 1) {
        DEBUG("raw_can_subscribe_rx: filter=0x%" PRIx32 " already in use\n", filter->can_id);
        return 0;
    }

    msg.type = CAN_MSG_SET_FILTER;
    msg.content.ptr = filter;
    msg_send_receive(&msg, &reply, candev_list[entry->ifnum]->pid);

    if ((int) reply.content.value < 0) {
        can_router_unregister(entry, filter->can_id, filter->can_mask, param);
        return -ENOMEM;
    }

    return 0;
}

static int unregister_filter_entry(can_reg_entry_t *entry, struct can_filter *filter, void *param)
{
    msg_t msg, reply;
    int ret;

    DEBUG("unregister_filter_entry: ifnum=%d, filter=0x%" PRIx32 ", mask=0x%" PRIx32 ", param=%p\n",
          entry->ifnum, filter->can_id, filter->can_mask, param);

    ret = can_router_unregister(entry, filter->can_id, filter->can_mask, param);
    if (ret < 0) {
        return -ENOMEM;
    }
    else if (ret == 1) {
        DEBUG("raw_can_unsubscribe_rx: filter=0x%" PRIx32 " still in use\n", filter->can_id);
        return 0;
    }

    msg.type = CAN_MSG_REMOVE_FILTER;
    msg.content.ptr = filter;
    msg_send_receive(&msg, &reply, candev_list[entry->ifnum]->pid);

    if ((int) reply.content.value < 0) {
        return -ENOMEM;
    }

    return 0;
}

int raw_can_subscribe_rx(int ifnum, struct can_filter *filter, kernel_pid_t pid, void *param)
{
    assert(ifnum < candev_nb);
    assert(filter);

    can_reg_entry_t entry;
    entry.ifnum = ifnum;
    entry.target.pid = pid;
#ifdef MODULE_CAN_MBOX
    entry.type = CAN_TYPE_DEFAULT;
#endif

    return register_filter_entry(&entry, filter, param);
}

#ifdef MODULE_CAN_MBOX
int raw_can_subscribe_rx_mbox(int ifnum, struct can_filter *filter, mbox_t *mbox, void *param)
{
    assert(ifnum < candev_nb);
    assert(filter);

    can_reg_entry_t entry;
    entry.ifnum = ifnum;
    entry.target.mbox = mbox;
    entry.type = CAN_TYPE_MBOX;

    return register_filter_entry(&entry, filter, param);
}
#endif

int raw_can_unsubscribe_rx(int ifnum, struct can_filter *filter, kernel_pid_t pid, void *param)
{
    assert(ifnum < candev_nb);
    assert(filter);

    can_reg_entry_t entry;
    entry.ifnum = ifnum;
    entry.target.pid = pid;
#ifdef MODULE_CAN_MBOX
    entry.type = CAN_TYPE_DEFAULT;
#endif

    return unregister_filter_entry(&entry, filter, param);
}

#ifdef MODULE_CAN_MBOX
int raw_can_unsubscribe_rx_mbox(int ifnum, struct can_filter *filter, mbox_t *mbox, void *param)
{
    assert(ifnum < candev_nb);
    assert(filter);

    can_reg_entry_t entry;
    entry.ifnum = ifnum;
    entry.target.mbox = mbox;
    entry.type = CAN_TYPE_MBOX;

    return unregister_filter_entry(&entry, filter, param);
}
#endif

int raw_can_free_frame(can_rx_data_t *frame)
{
    int ret = can_router_free_frame((struct can_frame *)frame->data.iov_base);

    can_pkt_free_rx_data(frame);

    return ret;
}

int raw_can_get_can_opt(int ifnum, can_opt_t *opt)
{
    msg_t msg, reply;

    assert(ifnum < CAN_DLL_NUMOF);

    if (!opt) {
        return -ENOMEM;
    }

    opt->context = (uint16_t)candev_list[ifnum]->pid;

    msg.type = CAN_MSG_GET;
    msg.content.ptr = (void *)opt;
    if (msg_send_receive(&msg, &reply, opt->context) != 1) {
        return -EBUSY;
    }

    return (int) reply.content.value;
}

int raw_can_set_can_opt(int ifnum, can_opt_t *opt)
{
    msg_t msg, reply;

    assert(ifnum < CAN_DLL_NUMOF);

    if (!opt) {
        return -ENOMEM;
    }

    opt->context = (uint16_t)candev_list[ifnum]->pid;

    msg.type = CAN_MSG_SET;
    msg.content.ptr = (void *)opt;
    if (msg_send_receive(&msg, &reply, opt->context) != 1) {
        return -EBUSY;
    }

    return (int) reply.content.value;
}

int can_dll_register_candev(candev_dev_t *candev)
{
    if (candev_nb >= CAN_DLL_NUMOF) {
        return -ENODEV;
    }

    DEBUG("can_dll_register_candev: candev=%p, ifnum=%d, pid=%" PRIkernel_pid "\n",
          (void *)candev, candev_nb, candev->pid);

    candev_list[candev_nb] = candev;

    return candev_nb++;
}

int can_dll_dispatch_rx_frame(struct can_frame *frame, kernel_pid_t pid)
{
    can_pkt_t *pkt = can_pkt_alloc_rx(_get_ifnum(pid), frame);

    return can_router_dispatch_rx_indic(pkt);
}

int can_dll_dispatch_tx_conf(can_pkt_t *pkt)
{
    DEBUG("can_dll_dispatch_tx_conf: pkt=0x%p\n", (void*)pkt);

    can_router_dispatch_tx_conf(pkt);

    mutex_lock(&tx_lock);
    LL_DELETE(tx_list[pkt->entry.ifnum], &pkt->entry);
    mutex_unlock(&tx_lock);

    can_pkt_free(pkt);

    return 0;
}

int can_dll_dispatch_tx_error(can_pkt_t *pkt)
{
    DEBUG("can_dll_dispatch_tx_error: pkt=0x%p\n", (void*)pkt);

    can_router_dispatch_tx_error(pkt);

    mutex_lock(&tx_lock);
    LL_DELETE(tx_list[pkt->entry.ifnum], &pkt->entry);
    mutex_unlock(&tx_lock);

    can_pkt_free(pkt);

    return 0;

}

int can_dll_dispatch_bus_off(kernel_pid_t pid)
{
    int ifnum = _get_ifnum(pid);
    can_reg_entry_t *entry = tx_list[ifnum];

    DEBUG("can_dll_dispatch_bus_off: ifnum=%d, pid=%" PRIkernel_pid "\n", ifnum, pid);

    mutex_lock(&tx_lock);
    while (entry) {
        can_pkt_t *pkt = container_of(entry, can_pkt_t, entry);
        can_router_dispatch_tx_error(pkt);
        LL_DELETE(tx_list[ifnum], entry);
        entry = tx_list[ifnum];
    }
    mutex_unlock(&tx_lock);

    return 0;
}

int can_dll_init(void)
{
    can_pkt_init();

    return 0;
}

int raw_can_power_down(int ifnum)
{
    msg_t msg, reply;

    assert(ifnum < candev_nb);

    msg.type = CAN_MSG_POWER_DOWN;
    if (msg_send_receive(&msg, &reply, candev_list[ifnum]->pid) != 1) {
        return -EBUSY;
    }

    return (int) reply.content.value;
}

int raw_can_power_up(int ifnum)
{
    msg_t msg, reply;

    assert(ifnum < candev_nb);

    msg.type = CAN_MSG_POWER_UP;
    if (msg_send_receive(&msg, &reply, candev_list[ifnum]->pid) != 1) {
        return -EBUSY;
    }

    return (int) reply.content.value;
}

int raw_can_set_bitrate(int ifnum, uint32_t bitrate, uint32_t sample_point)
{
    assert(ifnum < candev_nb);

    int res = 0;
    int ret;
    uint32_t clock;
    struct can_bittiming_const btc;
    struct can_bittiming bittiming;
    bittiming.bitrate = bitrate;
    bittiming.sample_point = sample_point;

    can_opt_t opt;
    opt.opt = CANOPT_CLOCK;
    opt.data = &clock;
    opt.data_len = sizeof(clock);
    ret = raw_can_get_can_opt(ifnum, &opt);
    if (ret < 0) {
        DEBUG("raw_can_set_bitrate: error when getting clock (%d)\n", ret);
        return -1;
    }
    DEBUG("raw_can_set_bitrate: clock=%" PRIu32 " Hz\n", clock);

    opt.opt = CANOPT_BITTIMING_CONST;
    opt.data = &btc;
    opt.data_len = sizeof(btc);
    ret = raw_can_get_can_opt(ifnum, &opt);
    if (ret < 0) {
        DEBUG("raw_can_set_bitrate: error when getting const (%d)\n", ret);
        return -1;
    }

    ret = can_device_calc_bittiming(clock, &btc, &bittiming);
    if (ret < 0) {
        DEBUG("raw_can_set_bitrate: bittiming might be wrong, ret=%d\n", ret);
        res = 1;
    }

    opt.data = &bittiming;
    opt.data_len = sizeof(bittiming);
    opt.opt = CANOPT_BITTIMING;

    ret = raw_can_set_can_opt(ifnum, &opt);
    if (ret < 0) {
        DEBUG("raw_can_set_bitrate: error when setting bitrate (%d)\n", ret);
        return -1;
    }

    DEBUG("raw_can_set_bitrate: success bitrate=%" PRIu32 ", spt=%" PRIu32 "\n",
          bittiming.bitrate, bittiming.sample_point);

    return res;
}

#ifdef MODULE_CAN_TRX
int raw_can_set_trx(int ifnum, can_trx_t *trx)
{
    msg_t msg, reply;

    assert(ifnum < candev_nb);

    msg.type = CAN_MSG_SET_TRX;
    msg.content.ptr = trx;
    if (msg_send_receive(&msg, &reply, candev_list[ifnum]->pid) != 1) {
        return -EBUSY;
    }

    return (int) reply.content.value;
}
#endif

int raw_can_get_ifnum_by_name(const char *name)
{
    for (int i = 0; i < candev_nb; i++) {
        if ((strcmp(name, candev_list[i]->name) == 0) &&
                (strlen(name) == strlen(candev_list[i]->name))) {
            return i;
        }
    }

    return RAW_CAN_DEV_UNDEF;
}

const char *raw_can_get_name_by_ifnum(int ifnum)
{
    assert(ifnum >= 0);

    if (ifnum >= candev_nb) {
        return NULL;
    }

    return candev_list[ifnum]->name;
}

candev_dev_t *raw_can_get_dev_by_ifnum(int ifnum)
{
    assert(ifnum >= 0);

    if (ifnum >= candev_nb) {
        return NULL;
    }

    return candev_list[ifnum];
}