1 | //emul_rplay.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
5 | * |
---|
6 | * This file is part of roard 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 | * RoarAudio 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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | struct emul_rplay_command emul_rplay_commands[] = { |
---|
29 | {"access", NULL, -1, -1, NULL}, |
---|
30 | {"application", NULL, 1, -1, NULL}, |
---|
31 | {"continue", NULL, 1, -1, NULL}, |
---|
32 | {"die", NULL, 1, -1, NULL}, |
---|
33 | {"done", NULL, 1, -1, NULL}, // #ifdef DEBUG |
---|
34 | {"find", NULL, 1, 1, NULL}, |
---|
35 | {"get", NULL, 1, 1, NULL}, |
---|
36 | {"help", NULL, -1, -1, NULL}, |
---|
37 | {"info", NULL, 1, 1, NULL}, |
---|
38 | {"list", NULL, 0, 1, NULL}, |
---|
39 | {"modify", NULL, 2, -1, NULL}, |
---|
40 | {"monitor", NULL, 1, -1, NULL}, |
---|
41 | {"pause", NULL, 1, -1, NULL}, |
---|
42 | {"play", NULL, 1, -1, NULL}, |
---|
43 | {"put", NULL, 2, -1, NULL}, |
---|
44 | {"quit", NULL, 0, 0, NULL}, |
---|
45 | {"reset", NULL, 0, 0, NULL}, |
---|
46 | {"set", NULL, 1, -1, NULL}, |
---|
47 | {"skip", NULL, 1, 1, NULL}, |
---|
48 | {"status", NULL, 0, 0, NULL}, |
---|
49 | {"stop", NULL, 1, -1, NULL}, |
---|
50 | {"version", NULL, 0, 0, NULL}, |
---|
51 | {"volume", NULL, 0, 1, NULL}, |
---|
52 | {"wait", NULL, -1, -1, NULL}, |
---|
53 | {NULL, NULL, -1, -1, NULL} |
---|
54 | }; |
---|
55 | |
---|
56 | static inline int is_true(const char * str) { |
---|
57 | const char * ts[] = {"true", "t", "1", "yes", "y", "on"}; |
---|
58 | int i; |
---|
59 | |
---|
60 | for (i = 0; i < sizeof(ts)/sizeof(*ts); i++) |
---|
61 | if ( !strcasecmp(str, ts[i]) ) |
---|
62 | return 1; |
---|
63 | |
---|
64 | return 0; |
---|
65 | } |
---|
66 | |
---|
67 | static inline int is_false(const char * str) { |
---|
68 | return !is_true(str); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | int emul_rplay_check_client (int client, struct roar_vio_calls * vio) { |
---|
73 | struct roar_vio_calls calls; |
---|
74 | char buf[1024]; |
---|
75 | ssize_t len; |
---|
76 | |
---|
77 | if ( client == -1 ) |
---|
78 | return -1; |
---|
79 | |
---|
80 | if ( vio == NULL ) { |
---|
81 | vio = &calls; |
---|
82 | if ( roar_vio_open_fh_socket(vio, clients_get_fh(client)) == -1 ) |
---|
83 | return -1; |
---|
84 | } |
---|
85 | |
---|
86 | if ( (len = roar_vio_read(vio, buf, sizeof(buf)-1)) <= 0 ) { |
---|
87 | // really bad protocol error |
---|
88 | clients_delete(client); |
---|
89 | return -1; |
---|
90 | } |
---|
91 | |
---|
92 | for (; buf[len-1] == '\r' || buf[len-1] == '\n'; len--); |
---|
93 | |
---|
94 | buf[len] = 0; |
---|
95 | |
---|
96 | return emul_rplay_exec_command(client, vio, buf); |
---|
97 | } |
---|
98 | |
---|
99 | int emul_rplay_exec_command (int client, struct roar_vio_calls * vio, char * command) { |
---|
100 | struct emul_rplay_command * cmd; |
---|
101 | struct roar_keyval * kv; |
---|
102 | ssize_t kvlen; |
---|
103 | char * para = NULL; |
---|
104 | char * c; |
---|
105 | int last_was_space = 0; |
---|
106 | |
---|
107 | for (c = command; *c != 0; c++) { |
---|
108 | if ( *c == ' ' || *c == '\t' ) { |
---|
109 | last_was_space = 1; |
---|
110 | *c = 0; |
---|
111 | } else { |
---|
112 | if ( last_was_space ) { |
---|
113 | para = c; |
---|
114 | break; |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | if ( para == NULL ) { |
---|
120 | kv = NULL; |
---|
121 | kvlen = 0; |
---|
122 | } else { |
---|
123 | kvlen = roar_keyval_split(&kv, para, " \t", "=", 0); |
---|
124 | if ( kvlen == -1 ) |
---|
125 | return emul_rplay_send_error(client, NULL, vio, NULL, 0, "Can not parse parameter list"); |
---|
126 | } |
---|
127 | |
---|
128 | for (cmd = emul_rplay_commands; cmd->name != NULL; cmd++) { |
---|
129 | if ( !strcasecmp(cmd->name, command) ) |
---|
130 | break; |
---|
131 | } |
---|
132 | |
---|
133 | if ( cmd->name == NULL ) |
---|
134 | return emul_rplay_send_error(client, NULL, vio, kv, kvlen, "unknown command"); |
---|
135 | |
---|
136 | if ( cmd->handler == NULL ) |
---|
137 | return emul_rplay_send_error(client, cmd, vio, kv, kvlen, "unsupported command"); |
---|
138 | |
---|
139 | return cmd->handler(client, cmd, vio, kv, kvlen); |
---|
140 | } |
---|
141 | |
---|
142 | int emul_rplay_send_error (int client, struct emul_rplay_command * cmd, struct roar_vio_calls * vio, struct roar_keyval * kv, size_t kvlen, const char * msg) { |
---|
143 | struct roar_keyval * kvr; |
---|
144 | const char * command; |
---|
145 | const char * cd = NULL; |
---|
146 | |
---|
147 | if ( cmd != NULL ) { |
---|
148 | command = cmd->name; |
---|
149 | } else { |
---|
150 | command = "(unknown)"; |
---|
151 | } |
---|
152 | |
---|
153 | if ( kv != NULL ) { |
---|
154 | kvr = roar_keyval_lookup(kv, "client-data", kvlen, 0); |
---|
155 | if ( kvr != NULL ) |
---|
156 | cd = kvr->value; |
---|
157 | } |
---|
158 | |
---|
159 | if ( cd == NULL ) |
---|
160 | cd = ""; |
---|
161 | |
---|
162 | return roar_vio_printf("-error=\"%s\" command=\"%s\" client-data=\"%s\"\n", msg, command, cd) <= 0 ? -1 : 0; |
---|
163 | } |
---|
164 | |
---|
165 | //ll |
---|