summaryrefslogtreecommitdiff
path: root/tools/mako/csd-daemon.c
blob: 61277ab886e90ada2bc7646e3bbc3d6a6a77028b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* csd-daemon.c
**
** Copyright 2013, Mozilla Corporation
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**     * Redistributions of source code must retain the above copyright
**       notice, this list of conditions and the following disclaimer.
**     * Redistributions in binary form must reproduce the above copyright
**       notice, this list of conditions and the following disclaimer in the
**       documentation and/or other materials provided with the distribution.
**     * Neither the name of The Android Open Source Project nor the names of
**       its contributors may be used to endorse or promote products derived
**       from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY Mozilla Corporation ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
** DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <dlfcn.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <asoundlib.h>

#define DEBUG 1
#include "common.h"

/* FIXME: we're always setting dmic + tty-off, is that always correct? */
#define ACDB_SETTINGS 0x12

struct state {
    int sockfd;
    int rx_dev, tx_dev;
    int start_voice;
    int disable_device;

    snd_pcm_t *play, *rec;
};

static struct state state;

/* Based on prototypes in hardware/qcom/audio/hal/audio_hw.h */

int  (*acdb_loader_init_ACDB)();
void (*acdb_loader_deallocate_ACDB)();
void (*acdb_loader_send_audio_cal)(int, int);
void (*acdb_loader_send_voice_cal)(int, int);

int (*csd_client_init)(void);
int (*csd_client_deinit)(void);
int (*csd_client_disable_device)(void);
int (*csd_client_enable_device)(int, int, uint32_t);
int (*csd_client_volume)(int);
int (*csd_client_mic_mute)(int);
int (*csd_client_start_voice)(void);
int (*csd_client_stop_voice)(void);

static int init_lib(void)
{
    void *handle;

    handle = dlopen("/system/lib/libacdbloader.so", RTLD_NOW);

    acdb_loader_init_ACDB = dlsym(handle, "acdb_loader_init_ACDB");
    acdb_loader_deallocate_ACDB = dlsym(handle, "acdb_loader_deallocate_ACDB");
    acdb_loader_send_audio_cal = dlsym(handle, "acdb_loader_send_audio_cal");
    acdb_loader_send_voice_cal = dlsym(handle, "acdb_loader_send_voice_cal");

    if (!acdb_loader_init_ACDB || !acdb_loader_deallocate_ACDB ||
        !acdb_loader_send_audio_cal || !acdb_loader_send_voice_cal) {
        ERR("Could not find all acdb_loader_* functions\n");
        return -1;
    }

    handle = dlopen("/system/lib/libcsd-client.so", RTLD_NOW);

    csd_client_init = dlsym(handle, "csd_client_init");
    csd_client_deinit = dlsym(handle, "csd_client_deinit");
    csd_client_disable_device = dlsym(handle, "csd_client_disable_device");
    csd_client_enable_device = dlsym(handle, "csd_client_enable_device");
    csd_client_volume = dlsym(handle, "csd_client_volume");
    csd_client_mic_mute = dlsym(handle, "csd_client_mic_mute");
    csd_client_start_voice = dlsym(handle, "csd_client_start_voice");
    csd_client_stop_voice = dlsym(handle, "csd_client_stop_voice");

    if (!csd_client_init || !csd_client_deinit || !csd_client_disable_device ||
        !csd_client_enable_device || !csd_client_volume ||
        !csd_client_mic_mute || !csd_client_start_voice ||
        !csd_client_stop_voice) {
        ERR("Could not find all csd_client_* functions\n");
        return -1;
    }

    return 0;
}

static int init_daemon(void)
{
    struct stat st;
    struct sockaddr_un sa;

    state.rx_dev = state.tx_dev = -1;

    if (stat(DEFAULT_RUNTIME_DIR, &st) < 0) {
        if (errno != -ENOENT) {
            /* First run, try to create the directory */
            if (mkdir(DEFAULT_RUNTIME_DIR, 0700) < 0) {
                ERR("Could not create runtime directory\n");
                return -1;
            }
        } else {
            ERR("Could not access runtime directory\n");
            return -1;
        }
    }

    if ((state.sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        ERR("Could not create socket\n");
        return -1;
    }

    sa.sun_family = AF_UNIX;
    snprintf(sa.sun_path, sizeof(sa.sun_path), DEFAULT_SOCKET_PATH);

    if (stat(DEFAULT_SOCKET_PATH, &st) == 0) {
        /* Either a daemon is already running or this is a stale socket */
        if (connect(state.sockfd, (struct sockaddr *) &sa, sizeof(sa)) < 0 &&
            errno == ECONNREFUSED) {
            DBG("Removing stale socket\n");
            unlink(DEFAULT_SOCKET_PATH);
        } else {
            ERR("Found existing socket, daemon already running?\n");
            close(state.sockfd);
            return -1;
        }
    }

    if (bind(state.sockfd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
        ERR("Could not bind address\n");
        return -1;
    }

    if (listen(state.sockfd, 1) < 0) {
        ERR("Could not start listening for connections\n");
        return -1;
    }

    return 0;
}

#define MODEM_PCM "hw:apq8064tablasnd,12"

static int open_pcms(void)
{
    if (snd_pcm_open(&state.play, MODEM_PCM, SND_PCM_STREAM_PLAYBACK, 0) < 0) {
        ERR("Could not open playback device\n");
        return -1;
    }

    if (snd_pcm_open(&state.rec, MODEM_PCM, SND_PCM_STREAM_CAPTURE, 0) < 0) {
        ERR("Could not open capture device\n");
        return -1;
    }

    if (snd_pcm_set_params(state.play,
                           SND_PCM_FORMAT_S16_LE,
                           SND_PCM_ACCESS_RW_INTERLEAVED,
                           1,
                           8000,
                           0,
                           0) < 0) {
        ERR("Could not set playback params\n");
        return -1;
    }

    if (snd_pcm_set_params(state.rec,
                           SND_PCM_FORMAT_S16_LE,
                           SND_PCM_ACCESS_RW_INTERLEAVED,
                           1,
                           8000,
                           0,
                           0) < 0) {
        ERR("Could not set capture params\n");
        return -1;
    }

    if (snd_pcm_start(state.play) < 0) {
        ERR("Could not start playback PCM\n");
        return -1;
    }

    if (snd_pcm_start(state.rec) < 0) {
        ERR("Could not start capture PCM\n");
        return -1;
    }

    return 0;
}

static void close_pcms(void)
{
    if (state.play) {
        snd_pcm_close(state.play);
        state.play = NULL;
    }

    if (state.rec) {
        snd_pcm_close(state.rec);
        state.rec = NULL;
    }
}

/* Return negative value to signal death (currently only returns 0 */
static int process(const char *command)
{
    int32_t ret = 0;

    if (strncmp(command, "enable-rx-device", 16) == 0) {
        if (sscanf(command, "%*s %d", &state.rx_dev) != 1) {
            ERR("Bad argument to enable-rx-device\n");
            goto out;
        }

        DBG("enable rx device: %d\n", state.rx_dev);

        /* Only enable device after we get both rx and tx device */
        if (state.rx_dev != -1 && state.tx_dev != -1) {
            ret = csd_client_enable_device(state.rx_dev, state.tx_dev, ACDB_SETTINGS);
            if (ret == 0 && state.start_voice) {
                state.start_voice = 0;
                ret = csd_client_start_voice();

                if (ret == 0) {
                    DBG("Opening PCMs\n");
                    ret = open_pcms();
                }
            }
        }

    } else if (strncmp(command, "enable-tx-device", 16) == 0) {
        if (sscanf(command, "%*s %d", &state.tx_dev) != 1) {
            ERR("Bad argument to enable-tx-device\n");
            goto out;
        }

        DBG("enable tx device: %d\n", state.tx_dev);

        /* Only enable device after we get both rx and tx device */
        if (state.rx_dev != -1 && state.tx_dev != -1) {
            if (state.disable_device) {
                state.disable_device = 0;
                ret = csd_client_disable_device();
            }

            if (ret == 0) {
                ret = csd_client_enable_device(state.rx_dev, state.tx_dev,
                                               ACDB_SETTINGS);
            }

            if (ret == 0 && state.start_voice) {
                state.start_voice = 0;
                ret = csd_client_start_voice();

                if (ret == 0) {
                    DBG("Opening PCMs\n");
                    ret = open_pcms();
                }
            }
        }

    } else if (strcmp(command, "disable-device") == 0) {
        /* We need to disable the device before an enable-device but _after_
         * stop-voice. Since we don't know what's coming next, we wait for
         * these to happen before actually executing the command. */
        state.disable_device = 1;

    } else if (strncmp(command, "volume", 6) == 0) {
        int volume;

        if (sscanf(command, "%*s %d", &volume) != 1) {
            ERR("Bad volume\n");
            goto out;
        }

        ret = csd_client_volume(volume);

    } else if (strncmp(command, "mic-mute", 8) == 0) {
        int state;

        if (sscanf(command, "%*s %d", &state) != 1 || (state != 0 && state != 1)) {
            ERR("Bad volume\n");
            goto out;
        }

        ret = csd_client_mic_mute(state);

    } else if (strcmp(command, "start-voice") == 0) {
        if (state.rx_dev != -1 && state.tx_dev != -1)
            ret = csd_client_start_voice();
        else {
            DBG("Deferring start voice till devices are enabled\n");
            state.start_voice = 1;
            ret = 0;
        }

    } else if (strcmp(command, "stop-voice") == 0) {
        ret = csd_client_stop_voice();

        if (ret == 0) {
            DBG("Closing PCMs\n");
            close_pcms();
        }

        /* Reset all the things */
        state.start_voice = 0;
        state.disable_device = 0;
        state.rx_dev = state.tx_dev = -1;

    } else {
        ERR("Bad command: %s\n", command);
    }

    if (ret < 0)
        ERR("Error running command\n");

    /* For now, there are no fatal errors */

out:
    return 0;
}

int main(int argc, char **argv)
{
    char command[COMMAND_MAX];
    int fd, done = 0, ret = 0;
    unsigned int i;

    if (argc > 1) {
        ERR("Bad arguments\n");
        return -1;
    }

    if (init_lib() < 0)
        return -1;

    if (init_daemon() < 0)
        return -1;

    if ((ret = acdb_loader_init_ACDB()) < 0) {
        ERR("Could not initialise acdb loader\n");
        return ret;
    }

    if ((ret = csd_client_init()) < 0) {
        ERR("Could not initialise csd client\n");
        return ret;
    }

    while (!done) {
        fd = accept(state.sockfd, NULL, NULL);
        if (fd < 0) {
            ERR("Could not accept incoming connection\n");
            continue;
        }

        /* We now read one "command" which is a string terminated with a NULL */
        for (i = 0; i < sizeof(command); i++) {
            if (read(fd, &command[i], 1) != 1) {
                ERR("Error reading command. Ignoring.\n");
                goto next;
            }

            if (command[i] == '\0') {
                DBG("Got command: %s\n", command);
                break;
            }
        }

        if (strcmp(command, "quit") == 0) {
            DBG("Got quit message. Exiting.\n");
            done = 1;
        } else if (process(command) < 0) {
            ERR("Fatal error while processing commands. Exiting.\n");
            ret = -1;
            done = 1;
        }

        /* Send back return code */
        if (write(fd, &ret, 4) < 4)
            ERR("Error sending return code");

next:
        close(fd);
    }

    close(state.sockfd);

out:
    csd_client_deinit();
    acdb_loader_deallocate_ACDB();

    return ret;
}