1/*
2 * Copyright (c) 2013, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#ifndef BULK_TRANSFER_HELPERS_H
11#define BULK_TRANSFER_HELPERS_H
12
13#include <bulk_transfer/bulk_transfer.h>
14
15static inline enum bulk_channel_role bulk_role_other(
16        enum bulk_channel_role role)
17{
18    if (role == BULK_ROLE_MASTER) {
19        return BULK_ROLE_SLAVE;
20    } else if (role == BULK_ROLE_SLAVE){
21        return BULK_ROLE_MASTER;
22    } else {
23        /* XXX: What do we do with that? */
24        return BULK_ROLE_GENERIC;
25    }
26}
27
28static inline enum bulk_channel_direction bulk_direction_other(
29        enum bulk_channel_direction direction)
30{
31    if (direction == BULK_DIRECTION_TX) {
32        return BULK_DIRECTION_RX;
33    } else {
34        return BULK_DIRECTION_TX;
35    }
36}
37
38// Can we trust auto-assigned values to enums? better make this explicit here
39static inline int bulk_trust_value(enum bulk_trust_level t)
40{
41    switch(t) {
42        case BULK_TRUST_NONE: return 0;
43        case BULK_TRUST_HALF: return 1;
44        case BULK_TRUST_FULL: return 2;
45        default: assert(!"Badness");
46                 // BULK_TRUST_UNINITIALIZED and other surprises
47    }
48    return -1;
49}
50
51/**
52 * \brief Compare trust levels.
53 *
54 * @return: -1 lhs lower trust than rhs
55 *           0 trust levels match
56 *          +1 lhs higher trust than rhs
57 */
58static inline int bulk_trust_compare(enum bulk_trust_level lhs,
59                                     enum bulk_trust_level rhs)
60{
61    int l_n = bulk_trust_value(lhs);
62    int r_n = bulk_trust_value(rhs);
63
64    if (l_n < r_n) {
65        return -1;
66    }
67    if (l_n == r_n) {
68        return  0;
69    }
70    return  1;
71}
72
73#endif // BULK_TRANSFER_HELPERS_H
74