[0] | 1 | //ctl.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of libroar 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 | * libroar 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 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
[0] | 35 | #include "libroar.h" |
---|
| 36 | |
---|
| 37 | int roar_server_oinfo (struct roar_connection * con, struct roar_stream * sa) { |
---|
| 38 | struct roar_message mes; |
---|
| 39 | |
---|
[133] | 40 | memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 41 | |
---|
[0] | 42 | mes.cmd = ROAR_CMD_SERVER_OINFO; |
---|
| 43 | mes.datalen = 0; |
---|
| 44 | |
---|
| 45 | if ( roar_req(con, &mes, NULL) == -1 ) |
---|
| 46 | return -1; |
---|
| 47 | |
---|
| 48 | if ( mes.cmd != ROAR_CMD_OK ) |
---|
| 49 | return -1; |
---|
| 50 | |
---|
| 51 | if ( roar_stream_m2s(sa, &mes) == -1 ) |
---|
| 52 | return -1; |
---|
| 53 | |
---|
| 54 | return 0; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | int roar_get_standby (struct roar_connection * con) { |
---|
| 58 | struct roar_message mes; |
---|
| 59 | |
---|
[133] | 60 | memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 61 | |
---|
[0] | 62 | mes.cmd = ROAR_CMD_GET_STANDBY; |
---|
| 63 | mes.datalen = 0; |
---|
| 64 | |
---|
| 65 | if ( roar_req(con, &mes, NULL) == -1 ) |
---|
| 66 | return -1; |
---|
| 67 | |
---|
| 68 | if ( mes.cmd != ROAR_CMD_OK ) |
---|
| 69 | return -1; |
---|
| 70 | |
---|
| 71 | return ROAR_NET2HOST16(*((uint16_t*)mes.data)); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | int roar_set_standby (struct roar_connection * con, int state) { |
---|
| 75 | struct roar_message mes; |
---|
| 76 | |
---|
[133] | 77 | memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 78 | |
---|
[0] | 79 | mes.cmd = ROAR_CMD_SET_STANDBY; |
---|
| 80 | mes.datalen = 2; |
---|
| 81 | |
---|
| 82 | *((uint16_t*)mes.data) = ROAR_HOST2NET16((unsigned) state); |
---|
| 83 | |
---|
| 84 | if ( roar_req(con, &mes, NULL) == -1 ) |
---|
| 85 | return -1; |
---|
| 86 | |
---|
| 87 | if ( mes.cmd != ROAR_CMD_OK ) |
---|
| 88 | return -1; |
---|
| 89 | |
---|
| 90 | return 0; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | int roar_exit (struct roar_connection * con) { |
---|
[578] | 94 | return roar_terminate(con, 0); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | int roar_terminate (struct roar_connection * con, int terminate) { |
---|
[0] | 98 | struct roar_message mes; |
---|
| 99 | |
---|
[133] | 100 | memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 101 | |
---|
[578] | 102 | mes.cmd = ROAR_CMD_EXIT; |
---|
| 103 | mes.datalen = 1; |
---|
| 104 | mes.data[0] = terminate; |
---|
| 105 | |
---|
[0] | 106 | if ( roar_req(con, &mes, NULL) == -1 ) |
---|
| 107 | return -1; |
---|
| 108 | |
---|
| 109 | if ( mes.cmd != ROAR_CMD_OK ) |
---|
| 110 | return -1; |
---|
| 111 | |
---|
| 112 | return 0; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | int roar_list (struct roar_connection * con, int * items, int max, int cmd) { |
---|
| 116 | struct roar_message m; |
---|
| 117 | |
---|
[133] | 118 | memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 119 | |
---|
[0] | 120 | roar_ctl_f2m_any(&m); |
---|
| 121 | m.cmd = cmd; |
---|
| 122 | |
---|
| 123 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
| 126 | return roar_ctl_m2ia(&m, items, max); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | int roar_get_client (struct roar_connection * con, struct roar_client * client, int id) { |
---|
| 130 | struct roar_message m; |
---|
| 131 | |
---|
[133] | 132 | memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 133 | |
---|
[0] | 134 | m.cmd = ROAR_CMD_GET_CLIENT; |
---|
| 135 | m.datalen = 1; |
---|
| 136 | m.data[0] = id; |
---|
| 137 | |
---|
| 138 | ROAR_DBG("roar_get_client(*): id = %i", id); |
---|
| 139 | |
---|
| 140 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 141 | return -1; |
---|
| 142 | |
---|
| 143 | if ( m.cmd != ROAR_CMD_OK ) |
---|
| 144 | return -1; |
---|
| 145 | |
---|
| 146 | ROAR_DBG("roar_get_client(*): got ok"); |
---|
| 147 | |
---|
| 148 | return roar_ctl_m2c(&m, client); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | int roar_get_stream (struct roar_connection * con, struct roar_stream * stream, int id) { |
---|
| 152 | struct roar_message m; |
---|
| 153 | |
---|
[133] | 154 | memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 155 | |
---|
[0] | 156 | m.cmd = ROAR_CMD_GET_STREAM; |
---|
| 157 | m.datalen = 1; |
---|
| 158 | m.data[0] = id; |
---|
| 159 | |
---|
[798] | 160 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 161 | |
---|
| 162 | if ( roar_req(con, &m, NULL) == -1 ) { |
---|
| 163 | roar_errno = ROAR_ERROR_PROTO; |
---|
[0] | 164 | return -1; |
---|
[798] | 165 | } |
---|
[0] | 166 | |
---|
| 167 | if ( m.cmd != ROAR_CMD_OK ) |
---|
| 168 | return -1; |
---|
| 169 | |
---|
| 170 | return roar_stream_m2s(stream, &m); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | int roar_kick (struct roar_connection * con, int type, int id) { |
---|
| 174 | struct roar_message m; |
---|
[17] | 175 | uint16_t * info = (uint16_t *) m.data; |
---|
[0] | 176 | |
---|
[133] | 177 | memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 178 | |
---|
[0] | 179 | m.cmd = ROAR_CMD_KICK; |
---|
| 180 | m.datalen = 4; |
---|
[299] | 181 | info[0] = ROAR_HOST2NET16(type); |
---|
| 182 | info[1] = ROAR_HOST2NET16(id); |
---|
[0] | 183 | |
---|
| 184 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 185 | return -1; |
---|
| 186 | |
---|
| 187 | if ( m.cmd != ROAR_CMD_OK ) |
---|
| 188 | return -1; |
---|
| 189 | |
---|
| 190 | return 0; |
---|
| 191 | } |
---|
| 192 | |
---|
[17] | 193 | int roar_set_vol (struct roar_connection * con, int id, struct roar_mixer_settings * mixer, int channels) { |
---|
| 194 | struct roar_message m; |
---|
| 195 | uint16_t * info = (uint16_t *) m.data; |
---|
| 196 | int i; |
---|
| 197 | |
---|
[133] | 198 | memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 199 | |
---|
[17] | 200 | m.cmd = ROAR_CMD_SET_VOL; |
---|
| 201 | m.datalen = (3 + channels) * 2; |
---|
| 202 | info[0] = 0; |
---|
[300] | 203 | info[1] = ROAR_HOST2NET16(id); |
---|
| 204 | info[2] = ROAR_HOST2NET16(ROAR_SET_VOL_ALL); |
---|
[17] | 205 | |
---|
| 206 | for (i = 0; i < channels; i++) |
---|
[300] | 207 | info[i+3] = ROAR_HOST2NET16(mixer->mixer[i]); |
---|
[17] | 208 | |
---|
| 209 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 210 | return -1; |
---|
| 211 | |
---|
| 212 | if ( m.cmd != ROAR_CMD_OK ) |
---|
| 213 | return -1; |
---|
| 214 | |
---|
| 215 | return 0; |
---|
| 216 | } |
---|
| 217 | |
---|
[24] | 218 | int roar_get_vol (struct roar_connection * con, int id, struct roar_mixer_settings * mixer, int * channels) { |
---|
| 219 | struct roar_message m; |
---|
| 220 | uint16_t * info = (uint16_t *) m.data; |
---|
| 221 | int i; |
---|
| 222 | |
---|
[133] | 223 | memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy! |
---|
| 224 | |
---|
[24] | 225 | m.cmd = ROAR_CMD_GET_VOL; |
---|
| 226 | m.datalen = 2*2; |
---|
| 227 | info[0] = 0; |
---|
[300] | 228 | info[1] = ROAR_HOST2NET16(id); |
---|
[24] | 229 | |
---|
| 230 | if ( roar_req(con, &m, NULL) == -1 ) |
---|
| 231 | return -1; |
---|
| 232 | |
---|
| 233 | if ( m.cmd != ROAR_CMD_OK ) |
---|
| 234 | return -1; |
---|
| 235 | |
---|
| 236 | if ( info[0] != 0 ) |
---|
| 237 | return -1; |
---|
| 238 | |
---|
[300] | 239 | info[1] = ROAR_NET2HOST16(info[1]); |
---|
| 240 | |
---|
[24] | 241 | if ( channels != NULL ) |
---|
| 242 | *channels = info[1]; |
---|
| 243 | |
---|
| 244 | if ( info[1] > ROAR_MAX_CHANNELS ) |
---|
| 245 | return -1; |
---|
| 246 | |
---|
| 247 | for (i = 0; i < info[1]; i++) |
---|
[300] | 248 | mixer->mixer[i] = ROAR_NET2HOST16(info[i+2]); |
---|
[24] | 249 | |
---|
| 250 | return 0; |
---|
| 251 | } |
---|
[0] | 252 | |
---|
| 253 | // converts: *_m2*, *_*2m |
---|
| 254 | |
---|
| 255 | int roar_ctl_f2m (struct roar_message * m, unsigned char filter, unsigned char cmp, uint32_t id) { |
---|
| 256 | |
---|
| 257 | m->datalen = 7; |
---|
| 258 | |
---|
| 259 | m->data[0] = 0; |
---|
| 260 | m->data[1] = filter; |
---|
| 261 | m->data[2] = cmp; |
---|
| 262 | *((uint32_t*)&(m->data[3])) = ROAR_HOST2NET32(id); |
---|
| 263 | |
---|
| 264 | return 0; |
---|
| 265 | } |
---|
| 266 | int roar_ctl_m2f (struct roar_message * m, unsigned char * filter, unsigned char * cmp, uint32_t * id) { |
---|
| 267 | |
---|
| 268 | if ( m->datalen != 7 ) |
---|
| 269 | return -1; |
---|
| 270 | |
---|
| 271 | if ( m->data[0] != 0 ) { |
---|
| 272 | ROAR_ERR("roar_ctl_m2f(*): version %i not supported!", m->data[0]); |
---|
| 273 | return -1; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | *filter = m->data[1]; |
---|
| 277 | *cmp = m->data[2]; |
---|
| 278 | |
---|
| 279 | *id = ROAR_NET2HOST32(*((uint32_t*)&(m->data[3]))); |
---|
| 280 | |
---|
| 281 | return 0; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | int roar_ctl_ia2m (struct roar_message * m, int * data, int len) { |
---|
| 285 | int i; |
---|
| 286 | |
---|
| 287 | if ( len > LIBROAR_BUFFER_MSGDATA ) |
---|
| 288 | return -1; |
---|
| 289 | |
---|
| 290 | m->datalen = len; |
---|
| 291 | |
---|
| 292 | for (i = 0; i < len; i++) |
---|
| 293 | m->data[i] = data[i]; |
---|
| 294 | |
---|
| 295 | return 0; |
---|
| 296 | } |
---|
| 297 | int roar_ctl_m2ia (struct roar_message * m, int * data, int len) { |
---|
| 298 | int i; |
---|
| 299 | |
---|
| 300 | if ( m->datalen > len ) |
---|
| 301 | return -1; |
---|
| 302 | |
---|
| 303 | for (i = 0; i < m->datalen; i++) |
---|
| 304 | data[i] = m->data[i]; |
---|
| 305 | |
---|
| 306 | return m->datalen; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | int roar_ctl_c2m (struct roar_message * m, struct roar_client * c) { |
---|
| 310 | int cur = 0; |
---|
| 311 | int h; |
---|
| 312 | int i; |
---|
| 313 | int max_len; |
---|
[434] | 314 | uint32_t pid; |
---|
[0] | 315 | |
---|
| 316 | if ( c == NULL ) |
---|
| 317 | return -1; |
---|
| 318 | |
---|
| 319 | m->data[cur++] = 0; // 0: Version |
---|
| 320 | m->data[cur++] = c->execed; // 1: execed |
---|
| 321 | |
---|
| 322 | h = 0; |
---|
| 323 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 324 | if ( c->streams[i] != -1 ) |
---|
| 325 | m->data[cur+1+h++] = c->streams[i]; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | m->data[cur++] = h; // 2: num of streams |
---|
| 329 | cur += h; |
---|
| 330 | |
---|
| 331 | max_len = strlen(c->name); |
---|
| 332 | |
---|
| 333 | // TODO: add some code to check if this fits in the pkg |
---|
| 334 | // NOTE: add this code after we are sure how long this pkg will be |
---|
| 335 | // and fully decieded about this function. |
---|
| 336 | |
---|
| 337 | m->data[cur++] = max_len; |
---|
| 338 | |
---|
| 339 | strncpy((m->data)+cur, c->name, max_len); |
---|
| 340 | |
---|
| 341 | cur += max_len; |
---|
| 342 | |
---|
[434] | 343 | pid = ROAR_HOST2NET32(c->pid); |
---|
[438] | 344 | memcpy(&(m->data[cur]), &pid, 4); |
---|
| 345 | cur += 4; |
---|
[434] | 346 | |
---|
[438] | 347 | pid = ROAR_HOST2NET32(c->uid); |
---|
| 348 | memcpy(&(m->data[cur]), &pid, 4); |
---|
| 349 | cur += 4; |
---|
| 350 | |
---|
| 351 | pid = ROAR_HOST2NET32(c->gid); |
---|
[434] | 352 | memcpy(&(m->data[cur]), &pid, 4); |
---|
| 353 | cur += 4; |
---|
| 354 | |
---|
[0] | 355 | m->datalen = cur; |
---|
| 356 | |
---|
| 357 | return 0; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | int roar_ctl_m2c (struct roar_message * m, struct roar_client * c) { |
---|
| 361 | int i; |
---|
| 362 | int cur; |
---|
[434] | 363 | uint32_t pid; |
---|
[0] | 364 | |
---|
| 365 | if ( m == NULL || c == NULL ) |
---|
| 366 | return -1; |
---|
| 367 | |
---|
| 368 | if ( m->datalen == 0 ) |
---|
| 369 | return -1; |
---|
| 370 | |
---|
| 371 | ROAR_DBG("roar_ctl_m2c(*): got data!, len = %i", m->datalen); |
---|
| 372 | |
---|
| 373 | if ( m->data[0] != 0 ) { |
---|
| 374 | ROAR_DBG("roar_ctl_m2c(*): wrong version!"); |
---|
| 375 | return -1; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | if ( m->datalen < 3 ) |
---|
| 379 | return -1; |
---|
| 380 | |
---|
| 381 | ROAR_DBG("roar_ctl_m2c(*): have usable data!"); |
---|
| 382 | |
---|
| 383 | c->execed = m->data[1]; |
---|
| 384 | |
---|
| 385 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) |
---|
| 386 | c->streams[i] = -1; |
---|
| 387 | |
---|
| 388 | for (i = 0; i < m->data[2]; i++) |
---|
| 389 | c->streams[i] = m->data[3+i]; |
---|
| 390 | |
---|
| 391 | cur = 3 + m->data[2]; |
---|
| 392 | |
---|
| 393 | strncpy(c->name, (m->data)+cur+1, m->data[cur]); |
---|
[17] | 394 | c->name[(int)m->data[cur]] = 0; |
---|
[0] | 395 | |
---|
[434] | 396 | cur += m->data[cur] + 1; |
---|
| 397 | |
---|
| 398 | memcpy(&pid, &(m->data[cur]), 4); |
---|
| 399 | c->pid = ROAR_NET2HOST32(pid); |
---|
| 400 | cur += 4; |
---|
| 401 | |
---|
[438] | 402 | memcpy(&pid, &(m->data[cur]), 4); |
---|
| 403 | c->uid = ROAR_NET2HOST32(pid); |
---|
| 404 | cur += 4; |
---|
| 405 | |
---|
| 406 | memcpy(&pid, &(m->data[cur]), 4); |
---|
| 407 | c->gid = ROAR_NET2HOST32(pid); |
---|
| 408 | cur += 4; |
---|
| 409 | |
---|
[0] | 410 | return 0; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | //ll |
---|