// SPDX-License-Identifier: MIT #include #include #include #include #include #include /* Note: These parameters have little relevance (no audio data written) */ #define VOICE_RATE 8000 #define VOICE_CHANNELS 1 #define VOICE_PERIOD 160 struct q6voiced { unsigned int card, device; snd_pcm_t *tx, *rx; }; static const char *voice_device(struct q6voiced *v) { static char path[32]; snprintf(path, sizeof(path), "hw:%u,%u", v->card, v->device); return path; } static snd_pcm_t *open_pcm(struct q6voiced *v, snd_pcm_stream_t stream) { snd_pcm_hw_params_t *hw; snd_pcm_t *pcm = NULL; snd_pcm_uframes_t frames = VOICE_PERIOD; unsigned int rate = VOICE_RATE; int err; err = snd_pcm_open(&pcm, voice_device(v), stream, 0); if (err < 0) { fprintf(stderr, "q6voiced: open %s: %s\n", voice_device(v), snd_strerror(err)); return NULL; } snd_pcm_hw_params_alloca(&hw); snd_pcm_hw_params_any(pcm, hw); snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(pcm, hw, SND_PCM_FORMAT_S16_LE); snd_pcm_hw_params_set_channels(pcm, hw, VOICE_CHANNELS); snd_pcm_hw_params_set_rate_near(pcm, hw, &rate, NULL); snd_pcm_hw_params_set_period_size_near(pcm, hw, &frames, NULL); snd_pcm_hw_params_set_buffer_size_near(pcm, hw, &frames); err = snd_pcm_hw_params(pcm, hw); if (err < 0) { fprintf(stderr, "q6voiced: hw_params %s: %s\n", voice_device(v), snd_strerror(err)); snd_pcm_close(pcm); return NULL; } err = snd_pcm_prepare(pcm); if (err < 0) { fprintf(stderr, "q6voiced: prepare %s: %s\n", voice_device(v), snd_strerror(err)); snd_pcm_close(pcm); return NULL; } return pcm; } static void q6voiced_open(struct q6voiced *v) { if (v->tx) return; /* Already active */ /* * Opening the PCM devices starts the stream. * This should be replaced by a codec2codec link probably. */ v->tx = open_pcm(v, SND_PCM_STREAM_CAPTURE); v->rx = open_pcm(v, SND_PCM_STREAM_PLAYBACK); printf("PCM devices were opened.\n"); } static void q6voiced_close(struct q6voiced *v) { if (!v->tx && !v->rx) return; /* Not active */ if (v->rx) snd_pcm_close(v->rx); if (v->tx) snd_pcm_close(v->tx); v->rx = v->tx = NULL; printf("PCM devices were closed.\n"); } /* See ModemManager-enums.h */ enum MMCallState { MM_CALL_STATE_DIALING = 1, MM_CALL_STATE_RINGING_OUT = 2, MM_CALL_STATE_ACTIVE = 4, }; static bool mm_state_is_active(int state) { /* * Some modems seem to be incapable of reporting DIALING -> ACTIVE. * Therefore we also consider DIALING/RINGING_OUT as active. */ switch (state) { case MM_CALL_STATE_DIALING: case MM_CALL_STATE_RINGING_OUT: case MM_CALL_STATE_ACTIVE: return true; default: return false; } } static void handle_signal(struct q6voiced *v, DBusMessage *msg, DBusError *err) { if (dbus_message_is_signal(msg, "org.ofono.VoiceCallManager", "CallAdded")) { q6voiced_open(v); } else if (dbus_message_is_signal(msg, "org.ofono.VoiceCallManager", "CallRemoved")) { q6voiced_close(v); } else if (dbus_message_is_signal(msg, "org.freedesktop.ModemManager1.Call", "StateChanged")) { /* * For ModemManager call objects are created in advance * and not necessarily immediately started. * Need to listen for call state changes. */ int old_state, new_state; if (!dbus_message_get_args(msg, err, DBUS_TYPE_INT32, &old_state, DBUS_TYPE_INT32, &new_state, DBUS_TYPE_INVALID)) return; if (old_state == new_state) return; /* No change */ if (mm_state_is_active(new_state)) q6voiced_open(v); else if (mm_state_is_active(old_state) && !mm_state_is_active(new_state)) q6voiced_close(v); } } int main(int argc, char **argv) { struct q6voiced v = {0}; DBusMessage *msg; DBusConnection *conn; DBusError err; if (argc != 2 || sscanf(argv[1], "hw:%u,%u", &v.card, &v.device) != 2) { fprintf(stderr, "Usage: q6voiced hw:,\n"); return 1; } dbus_error_init(&err); conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Connection error: %s\n", err.message); dbus_error_free(&err); return 1; } if (!conn) return 1; dbus_bus_add_match(conn, "type='signal',interface='org.ofono.VoiceCallManager'", &err); dbus_bus_add_match(conn, "type='signal',interface='org.freedesktop.ModemManager1.Call'", &err); dbus_connection_flush(conn); if (dbus_error_is_set(&err)) { fprintf(stderr, "Match error: %s\n", err.message); dbus_error_free(&err); return 1; } /* Drain messages produced while activating the match rules */ while (dbus_connection_read_write(conn, 0)) { while ((msg = dbus_connection_pop_message(conn))) { handle_signal(&v, msg, &err); if (dbus_error_is_set(&err)) dbus_error_free(&err); dbus_message_unref(msg); } if (dbus_connection_get_dispatch_status(conn) != DBUS_DISPATCH_DATA_REMAINS) break; } while (dbus_connection_read_write(conn, -1)) { while ((msg = dbus_connection_pop_message(conn))) { handle_signal(&v, msg, &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Failed to handle signal: %s\n", err.message); dbus_error_free(&err); } dbus_message_unref(msg); } } return 0; }