30StringRef pair_abort_reason_to_string(sendspin::SendspinPairAbortReason reason) {
31 using sendspin::SendspinPairAbortReason;
33 case SendspinPairAbortReason::ATTEMPT_TIMEOUT:
35 case SendspinPairAbortReason::CONCURRENT_ATTEMPT:
37 case SendspinPairAbortReason::METHOD_NOT_SUPPORTED:
39 case SendspinPairAbortReason::PAIRING_CODE_MISMATCH:
41 case SendspinPairAbortReason::USER_CANCELLED:
43 case SendspinPairAbortReason::UNKNOWN:
50void secure_wipe(
void *data,
size_t len) {
51 volatile auto *p =
static_cast<volatile uint8_t *
>(data);
52 for (
size_t i = 0; i <
len; ++i) {
58ESPPreferenceObject make_blob_pref(
const std::string &name,
size_t size) {
65std::optional<size_t> load_pref(ESPPreferenceObject &pref,
size_t size, std::span<uint8_t>
out) {
69 if (!pref.load(
out.data(),
size)) {
77bool save_pref(ESPPreferenceObject &pref, StringRef
key,
size_t size, std::span<const uint8_t> data) {
79 ESP_LOGW(TAG,
"\"%.*s\" blob of %zu bytes does not match its size of %zu; rejecting write",
83 if (!pref.save(
data.data(),
data.size())) {
84 ESP_LOGW(TAG,
"Failed to persist \"%.*s\" blob (%zu bytes)",
static_cast<int>(
key.size()),
key.c_str(),
92std::optional<size_t> parse_record_slot(StringRef
key) {
93 const StringRef prefix(sendspin::persistence_keys::RECORD_SLOT_PREFIX);
94 if (
key.size() <= prefix.size() || !
key.starts_with(prefix)) {
98 for (
const char c : StringRef(
key.begin() + prefix.size(),
key.end())) {
99 if (c <
'0' || c >
'9') {
102 slot = slot * 10 +
static_cast<size_t>(c -
'0');
112#ifdef USE_SENDSPIN_ARTWORK
114static const char *
const IMAGE_SOURCE_NAMES[] = {
"ALBUM",
"ARTIST",
"NONE"};
115static const char *
const IMAGE_FORMAT_NAMES[] = {
"JPEG",
"PNG"};
124 this->
client_ = std::make_unique<sendspin::SendspinClient>(std::move(config));
128 namespace keys = sendspin::persistence_keys;
129 this->
keypair_pref_ = make_blob_pref(
"keypair", keys::KEYPAIR_SIZE);
136 make_blob_pref(std::string(keys::RecordSlotKey(slot).view()), keys::RECORD_SLOT_SIZE);
141 this->
client_->set_listener(
this);
142 this->
client_->set_network_provider(
this);
143 this->
client_->set_persistence_provider(
this);
145#ifdef USE_SENDSPIN_ARTWORK
150#ifdef USE_SENDSPIN_COLOR
151 this->
client_->add_color().set_listener(
this);
154#ifdef USE_SENDSPIN_CONTROLLER
159#ifdef USE_SENDSPIN_METADATA
164#ifdef USE_SENDSPIN_PLAYER
168#ifdef USE_SENDSPIN_SOURCE
178 if (!capture.
data.empty()) {
186#ifdef USE_SENDSPIN_VISUALIZER
197#ifdef USE_SENDSPIN_ENABLED_SWITCH
199 this->enabled_switch_->restore(
true);
204 if (this->
enabled_.has_value() && this->unpaired_access_.has_value() &&
205 this->enabled_.value() != this->client_->is_started() && !this->status_has_error()) {
208 }
else if (!this->
client_->start()) {
214#ifdef USE_MDNS_SUPPORTS_ENABLE_DISABLE
231 const char *client_id =
"(unavailable)";
232 if (this->
client_ !=
nullptr && !this->
client_->client_id().empty()) {
233 client_id = this->
client_->client_id().c_str();
235 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
240 " Manufacturer: %s\n"
242 " Firmware version: %s\n"
243 " Task stack in PSRAM: %s\n"
244 " Unpaired access: %s\n"
245 " Pairing code method: %s",
248 YESNO(this->
client_ !=
nullptr && this->
client_->is_unpaired_access_enabled()),
249 this->pairing_code_method_());
250#ifdef USE_SENDSPIN_ENABLED_SWITCH
251 LOG_SWITCH(
" ",
"Enabled switch", this->enabled_switch_);
254#ifdef USE_SENDSPIN_ARTWORK
258 for (
const auto &preference : this->
artwork_config_.preferred_formats) {
259 ESP_LOGCONFIG(TAG,
" Artwork slot %u: %s as %s, %ux%u, display offset %" PRId32
" ms", slot++,
260 IMAGE_SOURCE_NAMES[
static_cast<uint8_t
>(preference.source)],
261 IMAGE_FORMAT_NAMES[
static_cast<uint8_t
>(preference.format)], preference.width, preference.height,
262 preference.display_offset_ms);
266#ifdef USE_SENDSPIN_SOURCE
269 " Sample rate: %" PRIu32
" Hz\n"
271 " Bits per sample: %u\n"
272 " Task stack in PSRAM: %s",
273 this->
source_config_.sample_rate, this->source_config_.channels, this->source_config_.bit_depth,
281 ESP_LOGE(TAG,
"Cannot %s: Sendspin failed to start, reboot to retry",
282 enabled ? LOG_STR_LITERAL(
"enable") : LOG_STR_LITERAL(
"disable"));
290 if (this->
client_ ==
nullptr) {
293 return this->
client_->client_pairing_token();
298 if (this->
client_ ==
nullptr) {
305void SendspinHub::set_unpaired_access_enabled(
bool enabled) {
308 if (this->
client_ !=
nullptr) {
309 this->
client_->set_unpaired_access_enabled(enabled);
313#ifdef USE_MDNS_SUPPORTS_ENABLE_DISABLE
320 bool advertise = this->
client_->is_started();
327 ESP_LOGE(TAG,
"Failed to %s mDNS service", advertise ? LOG_STR_LITERAL(
"enable") : LOG_STR_LITERAL(
"disable"));
337 this->
client_->connect_to(url);
344 this->
client_->disconnect(reason);
351 this->
client_->group_state().playback_state == sendspin::SendspinPlaybackState::PLAYING) {
359 this->
client_->confirm_pairing_window();
366 this->
client_->cancel_pairing_window();
384 for (
char &c : buf) {
385 if (c >=
'A' && c <=
'F') {
395 return LOG_STR_LITERAL(
"dynamic");
398 return LOG_STR_LITERAL(
"static");
400 return LOG_STR_LITERAL(
"none");
408 sendspin::SendspinClientConfig config;
410 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
423 config.pairing_code_out_channels = {sendspin::SendspinPairingCodeChannel::DISPLAY};
424 config.pairing_code_formats = {sendspin::SendspinPairingCodeFormat::DIGITS};
428 config.static_pairing_code_locations = {
"operator"};
465 sendspin::SendspinPairingCodeFormat ) {
476 sendspin::SendspinPairAbortReason reason) {
489 namespace keys = sendspin::persistence_keys;
490 if (
auto slot = parse_record_slot(
key); slot.has_value()) {
493 if (
key == keys::RECORD_ORDER) {
496 if (key == keys::KEYPAIR) {
499 if (
key == keys::PAIRING_PSK) {
502 if (
key == keys::LAST_PLAYED) {
505 if (
key == keys::OUTPUT_DELAY) {
514 if (pref ==
nullptr) {
515 ESP_LOGW(TAG,
"read_blob: unknown key \"%.*s\"",
static_cast<int>(
key.size()),
key.c_str());
518 return load_pref(*pref,
size,
out);
524 if (pref ==
nullptr) {
525 ESP_LOGW(TAG,
"write_blob: unknown key \"%.*s\"",
static_cast<int>(
key.size()),
key.c_str());
528 return save_pref(*pref,
key,
size, data);
536#ifdef USE_SENDSPIN_ARTWORK
539 sendspin::SendspinImageFormat
format) {
560#ifdef USE_SENDSPIN_COLOR
569#ifdef USE_SENDSPIN_CONTROLLER
572 std::optional<bool> mute) {
574 sendspin::ClientCommandControllerObject obj = {
598#ifdef USE_SENDSPIN_METADATA
619#ifdef USE_SENDSPIN_PLAYER
623 return this->
client_->player();
629#ifdef USE_SENDSPIN_SOURCE
636 this->
source_role_->set_signal(present ? sendspin::SourceSignal::PRESENT : sendspin::SourceSignal::ABSENT);
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
const StringRef & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
bool status_has_error() const
StringRef is a reference to a string owned by something else.
constexpr const char * c_str() const
static constexpr StringRef from_lit(const CharT(&s)[N])
const char * get_eth_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
bool set_service_enabled(const char *service_type, const char *proto, bool enabled)
Enable or disable a compiled-in service, matched by type and proto (e.g.
void add_data_callback(F &&data_callback)
Same callbacks as Microphone::add_data_callback; stream_info is the processed format.
Thin adapter over sendspin::SendspinClient.
const char * manufacturer_
ESPPreferenceObject output_delay_pref_
sendspin::SourceRole * source_role_
CallbackManager< void(uint8_t, const uint8_t *, size_t, sendspin::SendspinImageFormat)> artwork_image_decode_callbacks_
CallbackManager< void(uint8_t, uint32_t)> artwork_image_display_callbacks_
sendspin::PlayerRoleListener * player_listener_
CallbackManager< void(const sendspin::ServerColorStateObject &)> color_callbacks_
const char * firmware_version_
bool pairing_code_display_supported_
sendspin::MetadataRoleConfig metadata_config_
const char * get_product_name_() const
Returns the product name reported to the server: the configured model, or the device name.
std::optional< bool > unpaired_access_
void on_streaming_stopped() override
void on_controller_state_clear() override
void confirm_pairing_window()
Confirms a pairing attempt on the device.
ESPPreferenceObject record_order_pref_
microphone::MicrophoneSource * microphone_source_
LazyCallbackManager< void(const std::string &, StringRef)> pairing_failed_callbacks_
void on_track_metadata(const sendspin::TrackMetadata &metadata) override
LazyCallbackManager< void(const sendspin::ServerStateControllerObject &)> controller_state_callbacks_
std::optional< PairingToken > get_pairing_token() const
Returns the pairing token a Sendspin server uses to pair with this device.
void on_release_high_performance() override
LazyCallbackManager< void()> clear_pairing_code_callbacks_
bool is_network_ready() override
void on_pairing_code(std::string_view code, sendspin::SendspinPairingCodeFormat format) override
sendspin::MetadataRole * metadata_role_
void on_color_clear() override
void on_open_pairing_window() override
bool is_client_running() const
Returns whether the Sendspin client is running.
std::pair< ESPPreferenceObject *, size_t > pref_for_key_(StringRef key)
The preference a library key is stored in and its blob size, or {nullptr, 0} for a key with no storag...
const char * pairing_code_method_() const
LazyCallbackManager< void(const std::string &)> display_pairing_code_callbacks_
void switch_client()
Sends the SWITCH controller command; exposed as the sendspin.switch action.
void leave_group()
Leaves the current group so another source can use the speaker.
sendspin::PlayerRoleConfig player_config_
void cancel_pairing_window()
Closes an open pairing window, so a waiting pairing attempt is not confirmed.
ESPPreferenceObject last_played_pref_
void dump_config() override
LazyCallbackManager< void(const std::string &)> pairing_succeeded_callbacks_
const char * static_pairing_code_
void on_shutdown() override
static const char * get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
Writes the active network interface's MAC, in lowercase, into buf and returns its data pointer.
sendspin::PlayerRole * get_player_role()
Child components call this to get the PlayerRole instance after setup, so they can push updates to it...
void on_image_clear(uint8_t slot) override
void on_paired(std::string_view server_id) override
void on_clear_pairing_code() override
void disconnect_from_server(sendspin::SendspinGoodbyeReason reason)
Disconnects the underlying client from the current server.
CallbackManager< void(const sendspin::GroupState &)> group_state_callbacks_
std::unique_ptr< sendspin::SendspinClient > client_
sendspin::ArtworkRoleConfig artwork_config_
void update_mdns_service_()
Keeps the _sendspin mDNS service advertised while the client is running.
LazyCallbackManager< void()> source_streaming_started_callbacks_
sendspin::VisualizerRoleListener * visualizer_listener_
LazyCallbackManager< void()> source_streaming_stopped_callbacks_
sendspin::SendspinClientConfig build_client_config_()
Builds the SendspinClientConfig from ESPHome configuration and platform info.
mdns::MDNSComponent * mdns_
sendspin::ControllerRole * controller_role_
void on_pairing_aborted(std::string_view server_id, sendspin::SendspinPairAbortReason reason) override
void on_group_state(const sendspin::GroupState &changes) override
bool task_stack_in_psram_
uint32_t get_track_progress_ms() const
Returns the interpolated track progress in milliseconds, or 0 if the hub is not yet ready.
const sendspin::VisualizerRoleConfig * visualizer_config_
std::optional< size_t > read_blob(std::string_view key_view, std::span< uint8_t > out) override
std::optional< bool > enabled_
StringRef get_client_id() const
Returns the Sendspin client_id: base64url of the client's static public key.
void send_client_command(sendspin::SendspinControllerCommand command, std::optional< uint8_t > volume=std::nullopt, std::optional< bool > mute=std::nullopt)
LazyCallbackManager< void()> controller_state_clear_callbacks_
sendspin::SourceRoleConfig source_config_
void on_metadata_clear() override
CallbackManager< void(uint8_t)> artwork_image_clear_callbacks_
void on_streaming_started() override
ESPPreferenceObject keypair_pref_
CallbackManager< void(const sendspin::TrackMetadata &)> metadata_update_callbacks_
void on_close_pairing_window() override
LazyCallbackManager< void()> open_pairing_window_callbacks_
sendspin::ArtworkRole * artwork_role_
void on_request_high_performance() override
bool write_blob(std::string_view key_view, std::span< const uint8_t > data) override
void set_enabled(bool enabled)
Requests the Sendspin client, including the server, the roles and the mDNS advertisement,...
std::array< ESPPreferenceObject, SENDSPIN_RECORD_SLOTS > record_slot_prefs_
void on_image_display(uint8_t slot, uint32_t lateness_ms) override
void on_image_decode(uint8_t slot, const uint8_t *data, size_t length, sendspin::SendspinImageFormat format) override
ESPPreferenceObject pairing_psk_pref_
void on_controller_state(const sendspin::ServerStateControllerObject &state) override
LazyCallbackManager< void()> close_pairing_window_callbacks_
void connect_to_server(const std::string &url)
Connects the underlying client to the given Sendspin server.
void on_color(const sendspin::ServerColorStateObject &color) override
void set_source_signal(bool present)
Reports whether the source has audio to offer; exposed as the sendspin.source.set_signal action.
void artwork_frame_done(uint8_t slot)
Acknowledges the most recent artwork delivery (display or clear) for a slot.
bool request_high_performance()
Request high-performance mode (no power saving) for improved WiFi latency.
void release_roaming_suppression()
Same as set_roaming_suppressed(false); must be paired with a prior request.
void request_roaming_suppression()
Same as set_roaming_suppressed(true).
bool release_high_performance()
Release a high-performance mode request.
std::vector< uint8_t > & out
EthernetComponent * global_eth_component
std::span< const uint8_t > data
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
constexpr size_t SENDSPIN_RECORD_SLOTS
Pairing-record slots, handed to the client as max_pairing_records.
SendspinHub * global_sendspin_hub
ESPHOME_LOG_TAG(TAG, "sendspin.image")
WiFiComponent * global_wifi_component
ESPPreferences * global_preferences
const char * get_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in colon-separated uppercase hex notation.
void const char int const char * format
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint32_t fnv1a_hash(const char *str)
Calculate a FNV-1a hash of str.
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
bool sync()
Commit pending writes to flash.
int64_t capture_time_us
esp_timer_get_time() of the first frame, or 0 if unknown
std::span< const uint8_t > data
Only valid during the callback.