Skip to content

Commit

Permalink
add toniebox state
Browse files Browse the repository at this point in the history
use model for rtnl audio id workaround
  • Loading branch information
SciLor committed Oct 4, 2023
1 parent e195fe0 commit 451dfe2
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 8 deletions.
4 changes: 2 additions & 2 deletions include/net_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
#define _NET_CONFIG_H

#include "settings.h"
#include "toniebox_state.h"
#define AUTH_TOKEN_LENGTH 32

typedef struct
{
settings_t *settings;
const char *box_id;
const char *box_name;
toniebox_state_t *state;
} client_ctx_t;

typedef struct
Expand Down
27 changes: 27 additions & 0 deletions include/toniebox_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

typedef struct
{
const char *id;
const char *name;
} toniebox_state_box_t;

typedef struct
{
uint64_t uid;
bool valid;
uint32_t audio_id;
} toniebox_state_tag_t;

typedef struct
{
toniebox_state_box_t box;
toniebox_state_tag_t tag;
} toniebox_state_t;

void toniebox_state_init();
toniebox_state_t *get_toniebox_state();
toniebox_state_t *get_toniebox_state_id(uint8_t id);
2 changes: 2 additions & 0 deletions include/toniesJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ typedef struct
void tonies_init();
void tonies_readJson(char *source, toniesJson_item_t **toniesCache, size_t *toniesCount);
toniesJson_item_t *tonies_byAudioId(uint32_t audio_id);
toniesJson_item_t *tonies_byModel(char *model);
toniesJson_item_t *tonies_byAudioIdModel(uint32_t audio_id, char *model);
void tonies_deinit();
23 changes: 22 additions & 1 deletion src/handler_rtnl.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ void rtnlEvent(HttpConnection *connection, TonieRtnlRPC *rpc, client_ctx_t *clie
mqtt_sendBoxEvent("TagInvalid", "", client_ctx);
break;
case RTNL3_TYPE_PLAYBACK_STOPPED:
client_ctx->state->tag.audio_id = 0;
client_ctx->state->tag.valid = false;
client_ctx->state->tag.uid = 0;

sse_sendEvent("playback", "stopped", true);
mqtt_sendBoxEvent("Playback", "OFF", client_ctx);
mqtt_sendBoxEvent("TagValid", "", client_ctx);
Expand Down Expand Up @@ -309,8 +313,11 @@ void rtnlEvent(HttpConnection *connection, TonieRtnlRPC *rpc, client_ctx_t *clie
for (size_t i = 0; i < rpc->log2->field6.len; i++)
{
osSprintf(&buffer[i * 2], "%02X", rpc->log2->field6.data[(i + 4) % 8]);
client_ctx->state->tag.uid += (rpc->log2->field6.data[i] << i);
}
}
client_ctx->state->tag.uid = strtoull(buffer, NULL, 16);
client_ctx->state->tag.valid = false;
sse_sendEvent("TagInvalid", buffer, true);
mqtt_sendBoxEvent("TagInvalid", buffer, client_ctx);
mqtt_sendBoxEvent("TagValid", "", client_ctx);
Expand All @@ -324,22 +331,36 @@ void rtnlEvent(HttpConnection *connection, TonieRtnlRPC *rpc, client_ctx_t *clie
osSprintf(&buffer[i * 2], "%02X", rpc->log2->field6.data[(i + 4) % 8]);
}
}
client_ctx->state->tag.uid = strtoull(buffer, NULL, 16);
client_ctx->state->tag.valid = true;
sse_sendEvent("TagValid", buffer, true);
mqtt_sendBoxEvent("TagValid", buffer, client_ctx);
mqtt_sendBoxEvent("TagInvalid", "", client_ctx);
}
else if ((rpc->log2->function_group == RTNL2_FUGR_AUDIO_A && (rpc->log2->function == RTNL2_FUNC_AUDIO_ID_CC3200 || rpc->log2->function == RTNL2_FUNC_AUDIO_ID_ESP32)) || (rpc->log2->function_group == RTNL2_FUGR_AUDIO_B && rpc->log2->function == RTNL2_FUNC_AUDIO_ID))
{
uint32_t audioId = read_little_endian(rpc->log2->field6.data);
client_ctx->state->tag.audio_id = audioId;
osSprintf(buffer, "%d", audioId);
toniesJson_item_t *item = tonies_byAudioId(audioId);
sse_sendEvent("ContentAudioId", buffer, true);
mqtt_sendBoxEvent("ContentAudioId", buffer, client_ctx);

if (item == NULL)
{
tonie_info_t *tonieInfo = getTonieInfoFromUid(client_ctx->state->tag.uid, client_ctx->settings);
if (tonieInfo->valid)
{
item = tonies_byModel(tonieInfo->json.tonie_model);
}
freeTonieInfo(tonieInfo);
}

if (item == NULL)
{
sse_sendEvent("ContentTitle", "Unknown", true);
mqtt_sendBoxEvent("ContentTitle", "Unknown", client_ctx);
if (audioId < 0x50000000)
if (audioId < TEDDY_BENCH_AUDIO_ID_DEDUCT)
{
/* custom tonie */
char *url = custom_asprintf("%s/img_custom.png", settings_get_string("core.host_url"));
Expand Down
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "cloud_request.h"

#include "settings.h"
#include "toniebox_state.h"
#include "mqtt.h"
#include "cert.h"
#include "toniefile.h"
Expand Down Expand Up @@ -131,6 +132,7 @@ int_t main(int argc, char *argv[])
TRACE_ERROR("Make sure the config path exists and is writable\r\n");
return -1;
}
toniebox_state_init();
platform_init();

cJSON_Hooks hooks = {.malloc_fn = osAllocMem, .free_fn = osFreeMem};
Expand Down
6 changes: 3 additions & 3 deletions src/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ void mqtt_settings_rx(t_ha_info *ha_info, const t_ha_entity *entity, void *ctx,
error_t mqtt_init_box(t_ha_info *ha_box_instance, client_ctx_t *client_ctx)
{
t_ha_entity entity;
const char *box_id = client_ctx->box_id;
const char *box_name = client_ctx->box_name;
const char *box_id = client_ctx->state->box.id;
const char *box_name = client_ctx->state->box.name;

if (!box_id)
{
Expand Down Expand Up @@ -888,7 +888,7 @@ t_ha_info *mqtt_get_box(client_ctx_t *client_ctx)
{
t_ha_info *ret = NULL;

const char *box_id = client_ctx->box_id;
const char *box_id = client_ctx->state->box.id;

char *name = custom_asprintf("%s_Box_%s", settings_get_string("mqtt.topic"), box_id);

Expand Down
6 changes: 4 additions & 2 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ error_t httpServerRequestCallback(HttpConnection *connection, const char_t *uri)
client_ctx_t *client_ctx = &connection->private.client_ctx;
osMemset(client_ctx, 0x00, sizeof(client_ctx_t));
client_ctx->settings = get_settings();
client_ctx->state = get_toniebox_state();

if (connection->tlsContext)
{
Expand All @@ -148,6 +149,7 @@ error_t httpServerRequestCallback(HttpConnection *connection, const char_t *uri)
{
client_ctx->settings = get_settings_cn(subject);
}
client_ctx->state = get_toniebox_state_id(client_ctx->settings->internal.overlayNumber);

char *ua = connection->request.userAgent;
if (ua != NULL && osStrlen(ua) > 3)
Expand Down Expand Up @@ -258,8 +260,8 @@ error_t httpServerRequestCallback(HttpConnection *connection, const char_t *uri)
}
}
}
client_ctx->box_id = client_ctx->settings->commonName;
client_ctx->box_name = client_ctx->settings->boxName;
client_ctx->state->box.id = client_ctx->settings->commonName;
client_ctx->state->box.name = client_ctx->settings->boxName;
mutex_unlock(MUTEX_CLIENT_CTX);

connection->response.keepAlive = connection->request.keepAlive;
Expand Down
21 changes: 21 additions & 0 deletions src/toniebox_state.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "toniebox_state.h"
#include "settings.h"

static toniebox_state_t Box_State_Overlay[MAX_OVERLAYS];

void toniebox_state_init()
{
for (size_t i = 0; i < MAX_OVERLAYS; i++)
{
osMemset(&Box_State_Overlay[i], 0, sizeof(toniebox_state_t));
}
}

toniebox_state_t *get_toniebox_state()
{
return get_toniebox_state_id(0);
}
toniebox_state_t *get_toniebox_state_id(uint8_t id)
{
return &Box_State_Overlay[id];
}
33 changes: 33 additions & 0 deletions src/toniesJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,39 @@ toniesJson_item_t *tonies_byAudioId(uint32_t audio_id)
}
return tonies_byAudioId_base(audio_id, toniesJsonCache, toniesJsonCount);
}
toniesJson_item_t *tonies_byModel_base(char *model, toniesJson_item_t *toniesCache, size_t toniesCount)
{
if (model == NULL || osStrcmp(model, "") == 0)
return NULL;
#if TONIES_JSON_CACHED == 1
for (size_t i = 0; i < toniesCount; i++)
{
if (osStrcmp(toniesCache[i].model, model) == 0)
return &toniesCache[i];
}
#else
// cJSON_ParseWithLengthOpts
#endif
return NULL;
}
toniesJson_item_t *tonies_byModel(char *model)
{
toniesJson_item_t *item = tonies_byModel_base(model, toniesCustomJsonCache, toniesCustomJsonCount);
if (item)
{
return item;
}
return tonies_byModel_base(model, toniesJsonCache, toniesJsonCount);
}
toniesJson_item_t *tonies_byAudioIdModel(uint32_t audio_id, char *model)
{
toniesJson_item_t *item = tonies_byAudioId(audio_id);
if (item)
{
return item;
}
return tonies_byModel(model);
}
void tonies_deinit_base(toniesJson_item_t *toniesCache, size_t *toniesCount)
{
#if TONIES_JSON_CACHED == 1
Expand Down

0 comments on commit 451dfe2

Please sign in to comment.