isotp.c 10.2 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
/*
 * 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_conn
 * @{
 * @file
 * @brief       Implementation of isotp CAN connection
 *
 * @author      Vincent Dupont <vincent@otakeys.com>
 * @}
 */

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

#include "can/conn/isotp.h"
#include "can/isotp.h"
#include "can/device.h"

#ifdef MODULE_CONN_CAN_ISOTP_MULTI
#include "utlist.h"
#endif

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

#include "xtimer.h"

#define _TIMEOUT_TX_MSG_TYPE    (0x8000)
#define _TIMEOUT_RX_MSG_TYPE    (0x8001)
#define _CLOSE_CONN_MSG_TYPE    (0x8002)
#define _TIMEOUT_MSG_VALUE      (0xABCDEFAB)

#ifndef CONN_CAN_ISOTP_TIMEOUT_TX_CONF
#define CONN_CAN_ISOTP_TIMEOUT_TX_CONF (10 * US_PER_SEC)
#endif

static inline void put_msg(conn_can_isotp_t *conn, msg_t *msg)
{
#ifdef MODULE_CONN_CAN_ISOTP_MULTI
    mbox_put(&conn->master->mbox, msg);
#else
    mbox_put(&conn->mbox, msg);
#endif
}

static inline void get_msg(conn_can_isotp_t *conn, msg_t *msg)
{
#ifdef MODULE_CONN_CAN_ISOTP_MULTI
    mbox_get(&conn->master->mbox, msg);
#else
    mbox_get(&conn->mbox, msg);
#endif
}

int conn_can_isotp_create(conn_can_isotp_t *conn, struct isotp_options *options, int ifnum)
{
    assert(conn != NULL);
    assert(options != NULL);
    assert(ifnum < CAN_DLL_NUMOF);

#ifdef MODULE_CONN_CAN_ISOTP_MULTI
    DEBUG("conn_can_isotp_create: conn=%p, conn->master=%p, ifnum=%d\n",
          (void *)conn, (void *)conn->master, ifnum);

    if (conn->master == conn || conn->master == NULL) {
        conn->master = conn;
        conn->master->next = NULL;
        mutex_init(&conn->master->lock);
        mutex_lock(&conn->master->lock);
        DEBUG("conn_can_isotp_create: init master conn\n");
        mbox_init(&conn->master->mbox, conn->master->mbox_queue, CONN_CAN_ISOTP_MBOX_SIZE);
        mutex_unlock(&conn->master->lock);
    }
#else
    mbox_init(&conn->mbox, conn->mbox_queue, CONN_CAN_ISOTP_MBOX_SIZE);
#endif

    conn->ifnum = ifnum;

    memset(&conn->isotp, 0, sizeof(struct isotp));
    conn->isotp.opt = *options;

    return 0;
}

int conn_can_isotp_bind(conn_can_isotp_t *conn)
{
    assert(conn != NULL);
    assert(conn->isotp.opt.tx_id != 0 || conn->isotp.opt.rx_id != 0);

    DEBUG("conn_can_isotp_bind: conn=%p, ifnum=%d\n",
          (void *)conn, conn->ifnum);

    if (conn->bound) {
        return -EALREADY;
    }
    msg_t msg;
    int ret;
    can_reg_entry_t entry;
    entry.ifnum = conn->ifnum;
    entry.type = CAN_TYPE_MBOX;
#ifdef MODULE_CONN_CAN_ISOTP_MULTI
    assert(conn->master != NULL);

    entry.target.mbox = &(conn->master->mbox);
    if (conn != conn->master) {
        mutex_lock(&conn->master->lock);
        LL_APPEND(conn->master->next, (conn_can_isotp_slave_t *)conn);
        mutex_unlock(&conn->master->lock);
    }
    ret = mbox_try_get(&conn->master->mbox, &msg);
#else
    entry.target.mbox = &conn->mbox;
    ret = mbox_try_get(&conn->mbox, &msg);
#endif
    if ((ret == 1) && (msg.type != _CLOSE_CONN_MSG_TYPE)) {
        DEBUG("conn_can_isotp_bind: msg in queue type=%x\n", msg.type);
        put_msg(conn, &msg);
    }

    ret = isotp_bind(&conn->isotp, &entry, conn);
    if (!ret) {
        conn->bound = 1;
    }
    return ret;
}

static void _tx_conf_timeout(void *arg)
{
    conn_can_isotp_t *conn = arg;
    msg_t msg;

    msg.type = _TIMEOUT_TX_MSG_TYPE;
    msg.content.value = _TIMEOUT_MSG_VALUE;

    put_msg(conn, &msg);
}

int conn_can_isotp_send(conn_can_isotp_t *conn, const void *buf, size_t size, int flags)
{
    assert(conn != NULL);
    assert(buf != NULL || size == 0);

    int ret = 0;

    if (!conn->bound) {
        return -ENOTCONN;
    }

    if (flags & CAN_ISOTP_TX_DONT_WAIT) {
        return isotp_send(&conn->isotp, buf, size, flags);
    }
    else {
        xtimer_t timer;
        timer.callback = _tx_conf_timeout;
        timer.arg = conn;
        xtimer_set(&timer, CONN_CAN_ISOTP_TIMEOUT_TX_CONF);

        ret = isotp_send(&conn->isotp, buf, size, flags);

        msg_t msg;
        while (1) {
            get_msg(conn, &msg);
            switch (msg.type) {
            case CAN_MSG_TX_ERROR:
                if (msg.content.ptr == conn) {
                    ret = -EIO;
                }
                /* No break */
            case CAN_MSG_TX_CONFIRMATION:
#ifdef MODULE_CONN_CAN_ISOTP_MULTI
                if (msg.content.ptr != conn) {
                    mbox_put(&conn->master->mbox, &msg);
                    break;
                }
#endif
                xtimer_remove(&timer);
                return ret;
            case _TIMEOUT_TX_MSG_TYPE:
                return -ETIMEDOUT;
            default:
                DEBUG("conn_can_isotp_send: unexpected msg %x, requeing\n", msg.type);
                put_msg(conn, &msg);
                break;
            }
        }
    }

    return ret;
}

static void _rx_timeout(void *arg)
{
    conn_can_isotp_t *conn = arg;
    msg_t msg;

    msg.type = _TIMEOUT_RX_MSG_TYPE;
    msg.content.value = _TIMEOUT_MSG_VALUE;

    put_msg(conn, &msg);
}

int conn_can_isotp_recv(conn_can_isotp_t *conn, void *buf, size_t size, uint32_t timeout)
{
    assert(conn != NULL);
    assert(buf != NULL);

    int ret = 0;
    gnrc_pktsnip_t *snip;

    if (!conn->bound) {
        return -ENOTCONN;
    }

#ifdef MODULE_CONN_CAN_ISOTP_MULTI
    if (conn->rx) {
        snip = conn->rx->data.iov_base;
        if (snip->size <= size) {
            memcpy(buf, snip->data, snip->size);
            ret = snip->size;
        }
        else {
            ret = -EOVERFLOW;
        }
        isotp_free_rx(conn->rx);
        conn->rx = NULL;
        return ret;
    }
#endif

    xtimer_t timer;
    if (timeout != 0) {
        timer.callback = _rx_timeout;
        timer.arg = conn;
        xtimer_set(&timer, timeout);
    }

    msg_t msg;
    can_rx_data_t *rx;

    while (1) {
        get_msg(conn, &msg);
        switch (msg.type) {
        case CAN_MSG_RX_INDICATION:
            DEBUG("conn_can_isotp_recv: CAN_MSG_RX_INDICATION\n");
            rx = msg.content.ptr;
            snip = rx->data.iov_base;
#ifdef MODULE_CONN_CAN_ISOTP_MULTI
            if (rx->arg != conn) {
                mbox_put(&conn->master->mbox, &msg);
                break;
            }
#endif
            if (timeout != 0) {
                xtimer_remove(&timer);
            }
            if (snip->size <= size) {
                memcpy(buf, snip->data, snip->size);
                ret = snip->size;
            }
            else {
                ret = -EOVERFLOW;
            }
            isotp_free_rx(rx);
            return ret;
        case _TIMEOUT_RX_MSG_TYPE:
            DEBUG("conn_can_isotp_recv: _TIMEOUT_RX_MSG_TYPE\n");
            if (msg.content.value == _TIMEOUT_MSG_VALUE) {
                ret = -ETIMEDOUT;
            }
            else {
                ret = -EINTR;
            }
            return ret;
        case _CLOSE_CONN_MSG_TYPE:
            DEBUG("conn_can_isotp_recv: _CLOSE_CONN_MSG_TYPE\n");
#ifdef MODULE_CONN_CAN_ISOTP_MULTI
            if ((msg.content.ptr == conn) || (msg.content.ptr == conn->master)) {
#endif
                if (timeout != 0) {
                    xtimer_remove(&timer);
                }
                return -ECONNABORTED;
#ifdef MODULE_CONN_CAN_ISOTP_MULTI
            }
#endif
            break;
        default:
            DEBUG("conn_can_isotp_recv: unexpected msg %x\n", msg.type);
            if (timeout != 0) {
                xtimer_remove(&timer);
            }
            ret = -EINTR;
            return ret;
        }
    }

    return ret;
}

int conn_can_isotp_close(conn_can_isotp_t *conn)
{
    assert(conn != NULL);
    msg_t msg;

    DEBUG("conn_can_isotp_close: conn=%p, ifnum=%d\n",
          (void *)conn, conn->ifnum);

    if (!conn->bound) {
        return -EALREADY;
    }

#ifdef MODULE_CONN_CAN_ISOTP_MULTI
    assert(conn->master != NULL);

    if (conn->master != conn) {
        mutex_lock(&conn->master->lock);
        LL_DELETE(conn->master->next, (conn_can_isotp_slave_t *)conn);
        mutex_unlock(&conn->master->lock);
    }
    else {
        if (conn->master->next) {
            return -EBUSY;
        }
    }
#endif

    isotp_release(&conn->isotp);

#ifdef MODULE_CONN_CAN_ISOTP_MULTI
    if (conn->rx) {
        isotp_free_rx(conn->rx);
    }
    if (conn->master == conn) {
        while (mbox_try_get(&conn->master->mbox, &msg)) {
            if (msg.type == CAN_MSG_RX_INDICATION) {
                DEBUG("conn_can_isotp_close: freeing %p\n", msg.content.ptr);
                isotp_free_rx(msg.content.ptr);
            }
        }
    }
#else
    while (mbox_try_get(&conn->mbox, &msg)) {
        if (msg.type == CAN_MSG_RX_INDICATION) {
            DEBUG("conn_can_isotp_close: freeing %p\n", msg.content.ptr);
            isotp_free_rx(msg.content.ptr);
        }
    }
#endif

    msg.type = _CLOSE_CONN_MSG_TYPE;
    msg.content.ptr = conn;
    put_msg(conn, &msg);

    conn->bound = 0;

    return 0;
}

#ifdef MODULE_CONN_CAN_ISOTP_MULTI
int conn_can_isotp_select(conn_can_isotp_slave_t **conn, conn_can_isotp_t *master, uint32_t timeout)
{
    assert(master != NULL);
    assert(conn != NULL);

    int ret;

    xtimer_t timer;
    if (timeout != 0) {
        timer.callback = _rx_timeout;
        timer.arg = master;
        xtimer_set(&timer, timeout);
    }

    msg_t msg;
    can_rx_data_t *rx;

    mbox_get(&master->mbox, &msg);

    if (timeout != 0) {
        xtimer_remove(&timer);
    }
    switch (msg.type) {
    case CAN_MSG_RX_INDICATION:
        DEBUG("conn_can_isotp_select: CAN_MSG_RX_INDICATION\n");
        rx = msg.content.ptr;
        *conn = rx->arg;
        (*conn)->rx = rx;
        ret = 0;
        break;
    case _TIMEOUT_RX_MSG_TYPE:
        DEBUG("conn_can_isotp_select: _TIMEOUT_MSG_VALUE\n");
        if (msg.content.value == _TIMEOUT_MSG_VALUE) {
            ret = -ETIMEDOUT;
        }
        else {
            ret = -EINTR;
        }
        *conn = NULL;
        break;
    default:
        DEBUG("conn_can_isotp_select: %d\n", msg.type);
        *conn = NULL;
        ret = -EINTR;
        break;
    }

    return ret;
}
#endif /* MODULE_CONN_CAN_ISOTP_MULTI */

#else
typedef int dont_be_pedantic;
#endif /* MODULE_CAN_ISOTP */