[1947] | 1 | //roardmx.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
| 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 |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include "libroarlight.h" |
---|
| 26 | |
---|
[1950] | 27 | // base(ic) check |
---|
| 28 | #define BCHK(x) if ( (x) == NULL ) return -1 |
---|
| 29 | |
---|
| 30 | int roar_roardmx_message_new (struct roar_roardmx_message * mes) { |
---|
| 31 | BCHK(mes); |
---|
| 32 | |
---|
| 33 | memset(mes, 0, sizeof(struct roar_roardmx_message)); |
---|
| 34 | |
---|
| 35 | mes->version = ROAR_ROARDMX_VERSION; |
---|
| 36 | |
---|
| 37 | return 0; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | // low level: |
---|
| 41 | //int roar_roardmx_message_set_flag(struct roar_roardmx_message * mes, unsigned char flag); |
---|
| 42 | //int roar_roardmx_message_set_len (struct roar_roardmx_message * mes, size_t type); |
---|
| 43 | //int roar_roardmx_message_get_data(struct roar_roardmx_message * mes, unsigned char ** data); |
---|
| 44 | |
---|
| 45 | // mdium level: |
---|
| 46 | int roar_roardmx_message_set_type(struct roar_roardmx_message * mes, unsigned char type) { |
---|
| 47 | BCHK(mes); |
---|
| 48 | |
---|
| 49 | if ( (type | ROAR_ROARDMX_MASK_TYPE) - ROAR_ROARDMX_MASK_TYPE ) |
---|
| 50 | return -1; |
---|
| 51 | |
---|
| 52 | mes->type = type; |
---|
| 53 | |
---|
| 54 | return 0; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | int roar_roardmx_message_get_flag(struct roar_roardmx_message * mes, unsigned char * flag) { |
---|
| 58 | BCHK(mes); |
---|
| 59 | |
---|
| 60 | *flag = mes->flags; |
---|
| 61 | |
---|
| 62 | return 0; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | int roar_roardmx_message_get_type(struct roar_roardmx_message * mes, unsigned char * type) { |
---|
| 66 | BCHK(mes); |
---|
| 67 | |
---|
| 68 | *type = mes->type; |
---|
| 69 | |
---|
| 70 | return 0; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | int roar_roardmx_message_get_len (struct roar_roardmx_message * mes, size_t * length) { |
---|
| 74 | BCHK(mes); |
---|
| 75 | |
---|
| 76 | *length = mes->length; |
---|
| 77 | |
---|
| 78 | return 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | // IO: |
---|
[1955] | 83 | int roar_roardmx_message_send(struct roar_roardmx_message * mes, struct roar_vio_calls * vio) { |
---|
| 84 | BCHK(mes); |
---|
| 85 | BCHK(vio); |
---|
| 86 | |
---|
| 87 | if ( mes->length > ROAR_ROARDMX_DATA_LENGTH ) // this is very fatal! |
---|
| 88 | return -1; |
---|
| 89 | |
---|
| 90 | mes->data[0] = mes->version; |
---|
| 91 | mes->data[1] = (mes->flags & ROAR_ROARDMX_MASK_FLAGS) | |
---|
| 92 | (mes->type & ROAR_ROARDMX_MASK_TYPE ) ; |
---|
| 93 | |
---|
| 94 | mes->data[2] = mes->length; |
---|
| 95 | |
---|
[1957] | 96 | return roar_vio_write(vio, mes->data, mes->length + 3) == (mes->length + 3) ? 0 : -1; |
---|
[1955] | 97 | } |
---|
| 98 | |
---|
[1957] | 99 | int roar_roardmx_message_recv(struct roar_roardmx_message * mes, struct roar_vio_calls * vio) { |
---|
| 100 | BCHK(mes); |
---|
| 101 | BCHK(vio); |
---|
| 102 | |
---|
[1960] | 103 | if ( roar_roardmx_message_new(mes) == -1 ) |
---|
| 104 | return -1; |
---|
| 105 | |
---|
[1957] | 106 | if ( roar_vio_read(vio, mes->data, 3) != 3 ) |
---|
| 107 | return -1; |
---|
| 108 | |
---|
| 109 | mes->version = mes->data[0]; |
---|
| 110 | |
---|
| 111 | if ( mes->version != 0 ) |
---|
| 112 | return -1; |
---|
| 113 | |
---|
| 114 | mes->flags = mes->data[1] & ROAR_ROARDMX_MASK_FLAGS; |
---|
| 115 | mes->type = mes->data[1] & ROAR_ROARDMX_MASK_TYPE; |
---|
| 116 | |
---|
[1960] | 117 | mes->length = mes->data[2]; |
---|
[1957] | 118 | |
---|
| 119 | if ( roar_vio_read(vio, &(mes->data[3]), mes->length) != mes->length ) |
---|
| 120 | return -1; |
---|
| 121 | |
---|
| 122 | return 0; |
---|
| 123 | } |
---|
[1950] | 124 | |
---|
| 125 | // Data/high level: |
---|
| 126 | // * SSET: |
---|
| 127 | int roar_roardmx_message_new_sset (struct roar_roardmx_message * mes) { |
---|
| 128 | if ( roar_roardmx_message_new(mes) == -1 ) |
---|
| 129 | return -1; |
---|
| 130 | |
---|
| 131 | mes->type = ROAR_ROARDMX_TYPE_SSET; |
---|
| 132 | |
---|
| 133 | return 0; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | int roar_roardmx_message_add_chanval(struct roar_roardmx_message * mes, uint16_t channel, unsigned char val) { |
---|
[1959] | 137 | register uint16_t * chan; |
---|
[1950] | 138 | |
---|
| 139 | BCHK(mes); |
---|
| 140 | |
---|
| 141 | if ( (mes->length + 3) > ROAR_ROARDMX_DATA_LENGTH ) // message would be to long |
---|
| 142 | return -1; |
---|
| 143 | |
---|
[1959] | 144 | chan = (uint16_t *) &(mes->data[mes->length + 3]); |
---|
[1950] | 145 | |
---|
| 146 | *chan = ROAR_HOST2NET16(channel); |
---|
| 147 | |
---|
[1956] | 148 | mes->data[mes->length + 2 + 3] = val; |
---|
[1950] | 149 | |
---|
| 150 | mes->length += 3; |
---|
| 151 | |
---|
| 152 | return 0; |
---|
| 153 | } |
---|
| 154 | |
---|
[1959] | 155 | int roar_roardmx_message_get_chanval(struct roar_roardmx_message * mes, uint16_t * channel, unsigned char * val, int index) { |
---|
| 156 | register uint16_t * chan; |
---|
| 157 | |
---|
| 158 | BCHK(mes); |
---|
| 159 | |
---|
| 160 | if ( index < 0 ) |
---|
| 161 | return -1; |
---|
| 162 | |
---|
| 163 | if ( mes->version != 0 ) |
---|
| 164 | return -1; |
---|
| 165 | |
---|
| 166 | switch (mes->type) { |
---|
| 167 | case ROAR_ROARDMX_TYPE_SSET: |
---|
| 168 | case ROAR_ROARDMX_TYPE_INC8S: |
---|
| 169 | if ( index >= (ROAR_ROARDMX_DATA_LENGTH/3) ) |
---|
| 170 | return -1; |
---|
| 171 | |
---|
| 172 | *val = mes->data[3 * index + 2 + 3]; |
---|
| 173 | chan = (uint16_t *) &(mes->data[3 + 3 * index]); |
---|
| 174 | *channel = ROAR_NET2HOST16(*chan); |
---|
[1960] | 175 | return 0; |
---|
[1959] | 176 | break; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | return -1; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | int roar_roardmx_message_numchannels(struct roar_roardmx_message * mes) { |
---|
| 183 | BCHK(mes); |
---|
| 184 | |
---|
| 185 | if ( mes->version != 0 ) |
---|
| 186 | return -1; |
---|
| 187 | |
---|
| 188 | switch (mes->type) { |
---|
| 189 | case ROAR_ROARDMX_TYPE_SSET: |
---|
| 190 | case ROAR_ROARDMX_TYPE_INC8S: |
---|
| 191 | return mes->length / 3; |
---|
| 192 | break; |
---|
| 193 | case ROAR_ROARDMX_TYPE_IPO1: |
---|
| 194 | return mes->length / 6; |
---|
| 195 | break; |
---|
| 196 | case ROAR_ROARDMX_TYPE_IPO4: |
---|
| 197 | return mes->length / 12; |
---|
| 198 | break; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | return -1; |
---|
| 202 | } |
---|
[1950] | 203 | |
---|
[1947] | 204 | //ll |
---|