[1947] | 1 | //roardmx.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
[1947] | 5 | * |
---|
| 6 | * This file is part of libroardsp a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroardsp is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[1947] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "libroarlight.h" |
---|
| 27 | |
---|
[1950] | 28 | // base(ic) check |
---|
| 29 | #define BCHK(x) if ( (x) == NULL ) return -1 |
---|
| 30 | |
---|
| 31 | int roar_roardmx_message_new (struct roar_roardmx_message * mes) { |
---|
| 32 | BCHK(mes); |
---|
| 33 | |
---|
| 34 | memset(mes, 0, sizeof(struct roar_roardmx_message)); |
---|
| 35 | |
---|
| 36 | mes->version = ROAR_ROARDMX_VERSION; |
---|
| 37 | |
---|
| 38 | return 0; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | // low level: |
---|
| 42 | //int roar_roardmx_message_set_flag(struct roar_roardmx_message * mes, unsigned char flag); |
---|
| 43 | //int roar_roardmx_message_set_len (struct roar_roardmx_message * mes, size_t type); |
---|
| 44 | //int roar_roardmx_message_get_data(struct roar_roardmx_message * mes, unsigned char ** data); |
---|
| 45 | |
---|
| 46 | // mdium level: |
---|
| 47 | int roar_roardmx_message_set_type(struct roar_roardmx_message * mes, unsigned char type) { |
---|
| 48 | BCHK(mes); |
---|
| 49 | |
---|
| 50 | if ( (type | ROAR_ROARDMX_MASK_TYPE) - ROAR_ROARDMX_MASK_TYPE ) |
---|
| 51 | return -1; |
---|
| 52 | |
---|
| 53 | mes->type = type; |
---|
| 54 | |
---|
| 55 | return 0; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | int roar_roardmx_message_get_flag(struct roar_roardmx_message * mes, unsigned char * flag) { |
---|
| 59 | BCHK(mes); |
---|
| 60 | |
---|
| 61 | *flag = mes->flags; |
---|
| 62 | |
---|
| 63 | return 0; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | int roar_roardmx_message_get_type(struct roar_roardmx_message * mes, unsigned char * type) { |
---|
| 67 | BCHK(mes); |
---|
| 68 | |
---|
| 69 | *type = mes->type; |
---|
| 70 | |
---|
| 71 | return 0; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | int roar_roardmx_message_get_len (struct roar_roardmx_message * mes, size_t * length) { |
---|
| 75 | BCHK(mes); |
---|
| 76 | |
---|
| 77 | *length = mes->length; |
---|
| 78 | |
---|
| 79 | return 0; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | // IO: |
---|
[1955] | 84 | int roar_roardmx_message_send(struct roar_roardmx_message * mes, struct roar_vio_calls * vio) { |
---|
| 85 | BCHK(mes); |
---|
| 86 | BCHK(vio); |
---|
| 87 | |
---|
| 88 | if ( mes->length > ROAR_ROARDMX_DATA_LENGTH ) // this is very fatal! |
---|
| 89 | return -1; |
---|
| 90 | |
---|
| 91 | mes->data[0] = mes->version; |
---|
| 92 | mes->data[1] = (mes->flags & ROAR_ROARDMX_MASK_FLAGS) | |
---|
| 93 | (mes->type & ROAR_ROARDMX_MASK_TYPE ) ; |
---|
| 94 | |
---|
| 95 | mes->data[2] = mes->length; |
---|
| 96 | |
---|
[3636] | 97 | return roar_vio_write(vio, mes->data, mes->length + 3) == (ssize_t)(mes->length + 3) ? 0 : -1; |
---|
[1955] | 98 | } |
---|
| 99 | |
---|
[1957] | 100 | int roar_roardmx_message_recv(struct roar_roardmx_message * mes, struct roar_vio_calls * vio) { |
---|
| 101 | BCHK(mes); |
---|
| 102 | BCHK(vio); |
---|
| 103 | |
---|
[1960] | 104 | if ( roar_roardmx_message_new(mes) == -1 ) |
---|
| 105 | return -1; |
---|
| 106 | |
---|
[1957] | 107 | if ( roar_vio_read(vio, mes->data, 3) != 3 ) |
---|
| 108 | return -1; |
---|
| 109 | |
---|
| 110 | mes->version = mes->data[0]; |
---|
| 111 | |
---|
| 112 | if ( mes->version != 0 ) |
---|
| 113 | return -1; |
---|
| 114 | |
---|
| 115 | mes->flags = mes->data[1] & ROAR_ROARDMX_MASK_FLAGS; |
---|
| 116 | mes->type = mes->data[1] & ROAR_ROARDMX_MASK_TYPE; |
---|
| 117 | |
---|
[1960] | 118 | mes->length = mes->data[2]; |
---|
[1957] | 119 | |
---|
[3636] | 120 | if ( roar_vio_read(vio, &(mes->data[3]), mes->length) != (ssize_t)mes->length ) |
---|
[1957] | 121 | return -1; |
---|
| 122 | |
---|
| 123 | return 0; |
---|
| 124 | } |
---|
[1950] | 125 | |
---|
| 126 | // Data/high level: |
---|
| 127 | // * SSET: |
---|
| 128 | int roar_roardmx_message_new_sset (struct roar_roardmx_message * mes) { |
---|
| 129 | if ( roar_roardmx_message_new(mes) == -1 ) |
---|
| 130 | return -1; |
---|
| 131 | |
---|
| 132 | mes->type = ROAR_ROARDMX_TYPE_SSET; |
---|
| 133 | |
---|
| 134 | return 0; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | int roar_roardmx_message_add_chanval(struct roar_roardmx_message * mes, uint16_t channel, unsigned char val) { |
---|
[1959] | 138 | register uint16_t * chan; |
---|
[1950] | 139 | |
---|
| 140 | BCHK(mes); |
---|
| 141 | |
---|
| 142 | if ( (mes->length + 3) > ROAR_ROARDMX_DATA_LENGTH ) // message would be to long |
---|
| 143 | return -1; |
---|
| 144 | |
---|
[1959] | 145 | chan = (uint16_t *) &(mes->data[mes->length + 3]); |
---|
[1950] | 146 | |
---|
| 147 | *chan = ROAR_HOST2NET16(channel); |
---|
| 148 | |
---|
[1956] | 149 | mes->data[mes->length + 2 + 3] = val; |
---|
[1950] | 150 | |
---|
| 151 | mes->length += 3; |
---|
| 152 | |
---|
| 153 | return 0; |
---|
| 154 | } |
---|
| 155 | |
---|
[1959] | 156 | int roar_roardmx_message_get_chanval(struct roar_roardmx_message * mes, uint16_t * channel, unsigned char * val, int index) { |
---|
| 157 | register uint16_t * chan; |
---|
| 158 | |
---|
| 159 | BCHK(mes); |
---|
| 160 | |
---|
| 161 | if ( index < 0 ) |
---|
| 162 | return -1; |
---|
| 163 | |
---|
| 164 | if ( mes->version != 0 ) |
---|
| 165 | return -1; |
---|
| 166 | |
---|
| 167 | switch (mes->type) { |
---|
| 168 | case ROAR_ROARDMX_TYPE_SSET: |
---|
| 169 | case ROAR_ROARDMX_TYPE_INC8S: |
---|
| 170 | if ( index >= (ROAR_ROARDMX_DATA_LENGTH/3) ) |
---|
| 171 | return -1; |
---|
| 172 | |
---|
| 173 | *val = mes->data[3 * index + 2 + 3]; |
---|
| 174 | chan = (uint16_t *) &(mes->data[3 + 3 * index]); |
---|
| 175 | *channel = ROAR_NET2HOST16(*chan); |
---|
[1960] | 176 | return 0; |
---|
[1959] | 177 | break; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | return -1; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | int roar_roardmx_message_numchannels(struct roar_roardmx_message * mes) { |
---|
| 184 | BCHK(mes); |
---|
| 185 | |
---|
| 186 | if ( mes->version != 0 ) |
---|
| 187 | return -1; |
---|
| 188 | |
---|
| 189 | switch (mes->type) { |
---|
| 190 | case ROAR_ROARDMX_TYPE_SSET: |
---|
| 191 | case ROAR_ROARDMX_TYPE_INC8S: |
---|
| 192 | return mes->length / 3; |
---|
| 193 | break; |
---|
| 194 | case ROAR_ROARDMX_TYPE_IPO1: |
---|
| 195 | return mes->length / 6; |
---|
| 196 | break; |
---|
| 197 | case ROAR_ROARDMX_TYPE_IPO4: |
---|
| 198 | return mes->length / 12; |
---|
| 199 | break; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | return -1; |
---|
| 203 | } |
---|
[1950] | 204 | |
---|
[1947] | 205 | //ll |
---|