Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backends/osmand/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ endif()

# Voice daemon pieces that the plugin does not link (it keeps an exact, reviewed
# dependency set): the process-backed audio focus. Host-testable.
add_library(voice_daemon STATIC process_focus.cpp)
add_library(voice_daemon STATIC process_focus.cpp stock_focus.cpp)
target_include_directories(voice_daemon PUBLIC ${ONC_ROOT}/include ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(voice_daemon PUBLIC navigation_ui)
target_compile_options(voice_daemon PRIVATE ${ONC_WARNINGS})
Expand All @@ -52,7 +52,7 @@ if(RHVOICE_BUILD AND RHVOICE_INCLUDE)
add_executable(navi_voiced navi_voiced.cpp rhvoice_engine.cpp ${ONC_RHVOICE_OBJECTS})
target_include_directories(navi_voiced PRIVATE ${RHVOICE_INCLUDE})
target_compile_options(navi_voiced PRIVATE ${ONC_WARNINGS} ${ONC_TARGET_FLAGS})
target_link_libraries(navi_voiced PRIVATE voice_daemon navigation_ui asound m)
target_link_libraries(navi_voiced PRIVATE voice_daemon navigation_ui asound socket m)
# libasound needs libpps.so.1, which the linker only finds through -rpath-link.
target_link_options(navi_voiced PRIVATE -static-libstdc++ -static-libgcc
-Wl,-rpath-link,${CMAKE_SYSROOT}/armle-v7/lib)
Expand Down
22 changes: 13 additions & 9 deletions backends/osmand/navi_voiced.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// navi_voiced: speaks navigation prompts sent by the OsmAnd backend.
//
// navi_voiced [--queue NAME] [--device PCM] [--rhvoice-data DIR] [--voice NAME]
// [--rate PERCENT] [--focus none|alloc] [--alloc-cmd PATH]
// [--rate PERCENT] [--focus stock|none|alloc] [--alloc-cmd PATH]
// [--alloc-route CHANNEL SOURCE ZONE] [--selftest TEXT]
//
// Receives messages (voice_link.h), synthesizes with RHVoice, plays on io-audio
// and, with --focus alloc, holds the DSP channel through the as_alloc helper
// (contracts/native/as_audio_service_allocate_channel.md). --selftest speaks one
// and waits for stock navigation allocation and preparation before playing.
// --focus alloc is retained only for explicit diagnostics. --selftest speaks one
// sentence through the same stack and exits; it is the first thing to run on the radio.
#include "process_focus.h"
#include "stock_focus.h"
#include "qnx_pcm_out.h"
#include "rhvoice_engine.h"
#include "voice_link.h"
Expand Down Expand Up @@ -38,7 +39,7 @@ struct Options {
std::string rhvoice_data = "/tmp/tts/rhvoice";
std::string voice = "Natalia";
int rate_percent = 100;
bool focus_alloc = false;
std::string focus = "stock";
std::string alloc_cmd = "/tmp/tts/as_alloc";
std::string channel = "2", source = "8", zone = "1"; // NAVIGATION / NAVI / DRIVER
std::string selftest;
Expand All @@ -59,8 +60,8 @@ bool parse(int argc, char** argv, Options& o) {
else if (a == "--rate") { std::string v; if (!value(v)) return false; o.rate_percent = std::atoi(v.c_str()); }
else if (a == "--focus") {
std::string v;
if (!value(v) || (v != "none" && v != "alloc")) return false;
o.focus_alloc = v == "alloc";
if (!value(v) || (v != "stock" && v != "none" && v != "alloc")) return false;
o.focus = v;
}
else if (a == "--alloc-cmd") { if (!value(o.alloc_cmd)) return false; }
else if (a == "--alloc-route") {
Expand All @@ -80,7 +81,7 @@ int main(int argc, char** argv) {
if (!parse(argc, argv, opt)) {
std::fprintf(stderr,
"usage: %s [--queue NAME] [--device PCM] [--rhvoice-data DIR] [--voice NAME]\n"
" [--rate PERCENT] [--focus none|alloc] [--alloc-cmd PATH]\n"
" [--rate PERCENT] [--focus stock|none|alloc] [--alloc-cmd PATH]\n"
" [--alloc-route CHANNEL SOURCE ZONE] [--selftest TEXT]\n", argv[0]);
return 64;
}
Expand All @@ -89,7 +90,10 @@ int main(int argc, char** argv) {
engine.set_rate_percent(opt.rate_percent);

std::unique_ptr<AudioFocus> focus;
if (opt.focus_alloc) {
if (opt.focus == "stock") {
focus.reset(new StockFocus());
} else if (opt.focus == "alloc") {
std::fprintf(stderr, "voice: --focus alloc is an unverified diagnostic path; use stock for guidance\n");
ProcessFocus::Options f;
f.argv = {opt.alloc_cmd, opt.channel, opt.source, opt.zone, "86400"};
f.env = {"LD_LIBRARY_PATH=/pas/lib/dll:/pas/lib:/lib:/usr/lib:/proc/boot"};
Expand Down Expand Up @@ -122,7 +126,7 @@ int main(int argc, char** argv) {
sigaction(SIGTERM, &sa, nullptr);
sigaction(SIGINT, &sa, nullptr);
std::printf("navi_voiced ready queue=%s device=%s focus=%s voice=%s\n", opt.queue.c_str(),
opt.device.c_str(), opt.focus_alloc ? "alloc" : "none", opt.voice.c_str());
opt.device.c_str(), opt.focus.c_str(), opt.voice.c_str());
std::fflush(stdout);
server.run();
g_server = nullptr;
Expand Down
12 changes: 10 additions & 2 deletions backends/osmand/qnx_pcm_out.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,23 @@ bool QnxPcmOut::open_device(unsigned sample_rate_hz) {
void QnxPcmOut::close_device() {
if (!handle_) return;
snd_pcm_t* h = static_cast<snd_pcm_t*>(handle_);
snd_pcm_plugin_flush(h, SND_PCM_CHANNEL_PLAYBACK); // drains what was written
if (focus_.valid()) snd_pcm_plugin_playback_drain(h);
else snd_pcm_plugin_flush(h, SND_PCM_CHANNEL_PLAYBACK);
snd_pcm_close(h);
handle_ = nullptr;
rate_ = 0;
}

bool QnxPcmOut::write(const short* samples, std::size_t count, unsigned sample_rate_hz) {
if (!focused_) return false;
if (!focused_ || !focus_.valid()) return false;
if (handle_ && rate_ != sample_rate_hz) close_device();
if (!handle_ && !open_device(sample_rate_hz)) return false;
snd_pcm_t* h = static_cast<snd_pcm_t*>(handle_);
const char* p = reinterpret_cast<const char*>(samples);
std::size_t left = count * sizeof(short);
unsigned stalled = 0;
while (left > 0) {
if (!focus_.valid()) return false;
std::size_t n = left < fragment_ ? left : fragment_;
ssize_t w = snd_pcm_plugin_write(h, p, n);
if (w < static_cast<ssize_t>(n)) {
Expand All @@ -98,6 +101,11 @@ bool QnxPcmOut::write(const short* samples, std::size_t count, unsigned sample_r
}
if (w < 0) w = 0;
}
if (w <= 0 && ++stalled >= 3) {
std::fprintf(stderr, "voice: pcm made no progress\n");
return false;
}
if (w > 0) stalled = 0;
p += w;
left -= static_cast<std::size_t>(w);
}
Expand Down
1 change: 1 addition & 0 deletions backends/osmand/qnx_pcm_out.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AudioFocus {
public:
virtual ~AudioFocus() = default;
virtual bool request() = 0;
virtual bool valid() { return true; }
virtual void release() = 0;
};

Expand Down
64 changes: 64 additions & 0 deletions backends/osmand/stock_focus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "stock_focus.h"
#include <cerrno>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
namespace navicore_osmand {
bool StockFocus::request() {
release();
struct sockaddr_un sa{};
if (path_.size() >= sizeof(sa.sun_path)) return false;
fd_ = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd_ < 0) return false;
fcntl(fd_, F_SETFD, FD_CLOEXEC);
fcntl(fd_, F_SETFL, fcntl(fd_, F_GETFL, 0) | O_NONBLOCK);
sa.sun_family = AF_UNIX;
std::memcpy(sa.sun_path, path_.c_str(), path_.size() + 1);
// A local listener accepts without waiting for stock. The bounded wait below
// is for its asynchronous allocation/preparation reply.
if (connect(fd_, reinterpret_cast<sockaddr*>(&sa), sizeof(sa)) != 0 ||
send(fd_, "A", 1, MSG_NOSIGNAL) != 1) {
std::fprintf(stderr, "voice: stock focus connect: %s\n", std::strerror(errno));
release(); return false;
}
const auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms_);
while (std::chrono::steady_clock::now() < end) {
struct pollfd p = {fd_, POLLIN, 0};
int rc = poll(&p, 1, 50);
if (rc < 0 && errno == EINTR) continue;
if (rc < 0) break;
if (rc > 0) {
char reply = 0;
if (recv(fd_, &reply, 1, 0) == 1 && reply == 'G') {
granted_ = true;
std::fprintf(stderr, "voice: stock focus granted\n");
return valid();
}
std::fprintf(stderr, "voice: stock focus rejected reply=%u\n", unsigned(reply));
break;
}
}
std::fprintf(stderr, "voice: stock focus unavailable\n");
release(); return false;
}
bool StockFocus::valid() {
if (!granted_ || fd_ < 0) return false;
struct pollfd p = {fd_, POLLIN, 0};
int rc = poll(&p, 1, 0);
if (rc == 0 || (rc < 0 && errno == EINTR)) return true;
// R, EOF, or any unexpected message invalidates the grant. Keep the fd open
// until release(): the server must not acknowledge before PCM is closed.
granted_ = false;
std::fprintf(stderr, "voice: stock focus revoked/disconnected\n");
return false;
}
void StockFocus::release() {
granted_ = false;
if (fd_ >= 0) { close(fd_); fd_ = -1; }
}
}
22 changes: 22 additions & 0 deletions backends/osmand/stock_focus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "qnx_pcm_out.h"
#include <string>
namespace navicore_osmand {
// The socket is owned only by the speech worker. No detached watcher or polling
// thread: each PCM chunk checks for stock revocation or a lost provider.
class StockFocus : public AudioFocus {
public:
explicit StockFocus(std::string path = "/tmp/navicore-audio.sock",
unsigned timeout_ms = 9000)
: path_(std::move(path)), timeout_ms_(timeout_ms) {}
~StockFocus() override { release(); }
bool request() override;
bool valid() override;
void release() override;
private:
std::string path_;
unsigned timeout_ms_;
int fd_ = -1;
bool granted_ = false;
};
}
6 changes: 3 additions & 3 deletions contracts/qr39/provider/slot-manifest.csv
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ adapter,slot,si_name,reachability,abi_words_after_self,abi_prototype,implementat
"9","5","SI_GPSData","reachable_typed","1","uint32_t(self;word0)","open_navi_static_9_5","g_gps_vtable","nv_navigation:0x0810cf1c","0 while ACTIVE; 1 before init/during teardown","input words only; no output or retained storage"
"9","6","SI_GPSData","reachable_typed","1","uint32_t(self;word0)","open_navi_gps_interval","g_gps_vtable","nv_navigation:0x0810cf1c","0 while ACTIVE; 1 before init/during teardown","input words only; no output or retained storage"
"10","3","SI_AudioHandling","reachable_typed","3","uint32_t(self;word0;word1;word2)","open_navi_static_10_3","g_audio_vtable","nv_navigation:0x0812e560","0 while ACTIVE; 1 before init/during teardown","input words only; no output or retained storage"
"10","5","SI_AudioHandling","reachable_typed","3","uint32_t(self;word0;word1;word2)","open_navi_static_10_5","g_audio_vtable","nv_navigation:0x0812e560","0 while ACTIVE; 1 before init/during teardown","input words only; no output or retained storage"
"10","7","SI_AudioHandling","reachable_typed","3","uint32_t(self;word0;word1;word2)","open_navi_static_10_7","g_audio_vtable","nv_navigation:0x0812e560","0 while ACTIVE; 1 before init/during teardown","input words only; no output or retained storage"
"10","8","SI_AudioHandling","reachable_typed","3","uint32_t(self;word0;word1;word2)","open_navi_static_10_8","g_audio_vtable","nv_navigation:0x0812e560","0 while ACTIVE; 1 before init/during teardown","input words only; no output or retained storage"
"10","5","SI_AudioHandling","reachable_typed","3","uint32_t(self;handle;source;result)","nc_provider_audio_allocation","g_audio_vtable","nv_navigation:0x0812fcbc","0 for active matching source/transaction; 1 for stale or inactive callback; slot 5 drives stock focus worker","three 32-bit values copied under lock; source 1; allocation and preparation grant result 1; deactivation acknowledgement waits for PCM close"
"10","7","SI_AudioHandling","reachable_typed","3","uint32_t(self;handle;source;result)","nc_provider_audio_preparation","g_audio_vtable","nv_navigation:0x0812ec04","0 for active matching source/transaction; 1 for stale or inactive callback; slot 7 drives stock focus worker","three 32-bit values copied under lock; source 1; allocation and preparation grant result 1; deactivation acknowledgement waits for PCM close"
"10","8","SI_AudioHandling","reachable_typed","3","uint32_t(self;handle;source;reason)","nc_provider_audio_deactivation","g_audio_vtable","nv_navigation:0x0812f9a4","0 for active matching source/transaction; 1 for stale or inactive callback; slot 8 drives stock focus worker","three 32-bit values copied under lock; source 1; allocation and preparation grant result 1; deactivation acknowledgement waits for PCM close"
"10","17","SI_AudioHandling","reachable_typed","3","uint32_t(self;word0;word1;word2)","open_navi_10_17","g_audio_vtable","nv_navigation:0x0812e560","0 while ACTIVE; 1 before init/during teardown","input words only; no output or retained storage"
"10","18","SI_AudioHandling","reachable_typed","3","uint32_t(self;word0;word1;word2)","open_navi_static_10_18","g_audio_vtable","nv_navigation:0x0812e560","0 while ACTIVE; 1 before init/during teardown","input words only; no output or retained storage"
"10","19","SI_AudioHandling","reachable_typed","2","uint32_t(self;word0;word1)","open_navi_audio_volume","g_audio_vtable","nv_navigation:0x0812e560","0 while ACTIVE; 1 before init/during teardown","input words only; no output or retained storage"
Expand Down
45 changes: 45 additions & 0 deletions docs/design/voice-guidance-wiring.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
# Voice guidance: wiring Ukrainian TTS into Open NaviCore


## Implemented stock focus, 2026-10-28

This section supersedes the historical design and ProcessFocus deployment notes below.
The backend continues to send prompts through `/navicore-voice`; no backend ABI change is
needed. `navi_voiced --focus stock` (the default) talks to the provider over
`/tmp/navicore-audio.sock`. One connection owns one prompt. The provider's joined worker
invokes the existing typed stock callbacks; the renderer never waits for audio.

- Outbound adapter 10 slot 6 requests **PanaNav source 1**, mapped by stock to source 21.
This is a different enum from `as_audio_service` source 8.
- Inbound slot 5 `(handle, source, result=1)` grants allocation. The worker then sends
outbound slot 7 preparation and waits for inbound slot 7 with the same handle/source
and result 1. Only then does the daemon open/write `pcmC7D0p`.
- Inbound slot 8 requests deactivation. The daemon checks revocation between PCM writes,
flushes queued samples, closes PCM, then disconnects. The provider acknowledges with
outbound slot 9 and native success result 1. Normal completion drains PCM before
disconnecting and invoking outbound slot 8 release.
- Denial, stale callbacks, a missing provider, and timeouts fail visibly. There is no
automatic fallback. Explicit `--focus alloc` and `--focus none` remain diagnostics.
A request made before the stock audio adapter is initialized can expire.

Recovered callsites: allocation `0812fee8`, preparation `0812e868`, release `0812db5c`,
deactivation reply `0812d8e4`; inbound allocation `0812fcbc`, preparation `0812ec04`,
deactivation `0812f9a4`. The provider slot manifest records the three inbound contracts.
The local protocol is `A` request, `G` granted, `D` denied, `R` revoked; disconnect releases.
Acquisition expires after 8 seconds (client 9), a held grant after 60 seconds, and a dead
revoked client after 2 seconds. The provider stops/joins its listener at deinitialization.

Implementation: `src/runtime/open_navi_audio_focus.*`, provider adapter 10 methods,
`backends/osmand/stock_focus.*`, and `qnx_pcm_out.*`. QNX's playback-drain call replaces
flush on normal completion; flush is used for revoked audio. `navi_voiced` now links
`libsocket`; the backend dependency set is unchanged.

Validation: all 36 host tests pass, including asynchronous grant/preparation, stale
callbacks, denial, disconnect, timeout, and revocation acknowledgement ordering. QNX
build, provider ABI/ELF checks, and both IFS packages pass. On M9304002366, the stock
callbacks granted source 1 and selftest reported `spoken=1`, all failure counters zero.
A prompt sent through the installed daemon's normal message queue also received both
grants and released focus. Speaker audibility has not yet been confirmed by the operator.
The external workspace report `contracts/native/navi_audio_silence_20261028.md` contains
Ghidra and live evidence paths and the installation record.

## Historical design (2026-10-20)

2026-10-20. Offline analysis, nothing was run on the radio for this note. It answers: how do
spoken guidance prompts get from the OsmAnd backend to the speakers, given what the TTS work
found about sound (`contracts/native/as_audio_service_allocate_channel.md` in the radio workspace, outside this repository)?
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(ONC_PROVIDER_INCLUDES ${ONC_ROOT}/include ${CMAKE_CURRENT_SOURCE_DIR}/compat
add_library(provider SHARED
compat/qr39/navicore_compat.c
compat/qr39/host_callbacks.c
runtime/open_navi_audio_focus.c
runtime/open_navi_api_telemetry.c
runtime/open_navi_callback_backend.c
runtime/open_navi_cluster_backend.c
Expand Down
Loading