Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ring/teddy_cloud into develop
  • Loading branch information
g3gg0 committed Oct 8, 2023
2 parents 92931cf + 451dfe2 commit 5eebeb9
Show file tree
Hide file tree
Showing 17 changed files with 337 additions and 167 deletions.
5 changes: 3 additions & 2 deletions include/contentJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef struct
char *cloud_ruid;
uint8_t *cloud_auth;
size_t cloud_auth_len;
bool_t cloud_valid;
bool_t cloud_override;
char *tonie_model;

bool_t _stream;
Expand All @@ -25,8 +25,9 @@ typedef struct

} contentJson_t;

#define CONTENT_JSON_VERSION 4
#define CONTENT_JSON_VERSION 5

error_t load_content_json(const char *content_path, contentJson_t *content_json);
error_t save_content_json(const char *content_path, contentJson_t *content_json);
void content_json_update_model(contentJson_t *content_json, uint32_t audio_id);
void free_content_json(contentJson_t *content_json);
8 changes: 5 additions & 3 deletions include/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef struct
bool_t valid;
bool_t updated;
bool_t stream;
contentJson_t contentConfig;
contentJson_t json;
TonieboxAudioFileHeader *tafHeader;
} tonie_info_t;

Expand Down Expand Up @@ -67,7 +67,7 @@ typedef struct
size_t bufferLen;
uint32_t status;
FsFile *file;
tonie_info_t tonieInfo;
tonie_info_t *tonieInfo;
HttpConnection *connection;
client_ctx_t *client_ctx;
} cbr_ctx_t;
Expand All @@ -85,7 +85,9 @@ char *strupr(char input[]);
void getContentPathFromCharRUID(char ruid[17], char **pcontentPath, settings_t *settings);
void getContentPathFromUID(uint64_t uid, char **pcontentPath, settings_t *settings);
void setTonieboxSettings(TonieFreshnessCheckResponse *freshResp, settings_t *settings);
tonie_info_t getTonieInfo(const char *contentPath, settings_t *settings);
tonie_info_t *getTonieInfoFromUid(uint64_t uid, settings_t *settings);
tonie_info_t *getTonieInfoFromRuid(char ruid[17], settings_t *settings);
tonie_info_t *getTonieInfo(const char *contentPath, settings_t *settings);
void freeTonieInfo(tonie_info_t *tonieInfo);

void httpPrepareHeader(HttpConnection *connection, const void *contentType, size_t contentLength);
Expand Down
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
3 changes: 2 additions & 1 deletion include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define TONIES_CUSTOM_JSON_PATH "config/tonies.custom.json"
#define CONFIG_PATH "config/config.ini"
#define CONFIG_OVERLAY_PATH "config/config.overlay.ini"
#define CONFIG_VERSION 4
#define CONFIG_VERSION 5
#define MAX_OVERLAYS 16 + 1

typedef enum
Expand Down Expand Up @@ -58,6 +58,7 @@ typedef struct
bool markCustomTagByPass;
bool prioCustomContent;
bool updateOnLowerAudioId;
bool dumpRuidAuthContentJson;
} settings_cloud_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);
4 changes: 4 additions & 0 deletions include/toniesJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <stdint.h>
#include <stddef.h>

#define TEDDY_BENCH_AUDIO_ID_DEDUCT 0x50000000

typedef struct
{
uint16_t no;
Expand All @@ -23,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();
58 changes: 50 additions & 8 deletions src/contentJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "cJSON.h"
#include "net_config.h"
#include "server_helpers.h"
#include "toniesJson.h"

char *content_jsonGetString(cJSON *jsonElement, char *name)
{
Expand Down Expand Up @@ -87,6 +88,20 @@ error_t load_content_json(const char *content_path, contentJson_t *content_json)
char *jsonPath = custom_asprintf("%s.json", content_path);
error_t error = NO_ERROR;
osMemset(content_json, 0, sizeof(contentJson_t));
content_json->live = false;
content_json->nocloud = false;
content_json->source = NULL;
content_json->skip_seconds = 0;
content_json->cache = false;
content_json->_updated = false;
content_json->_stream = false;
content_json->_streamFile = custom_asprintf("%s.stream", content_path);
content_json->cloud_ruid = NULL;
content_json->cloud_auth = NULL;
content_json->cloud_auth_len = 0;
content_json->cloud_override = false;
content_json->tonie_model = NULL;
content_json->_valid = false;

if (fsFileExists(jsonPath))
{
Expand Down Expand Up @@ -128,18 +143,18 @@ error_t load_content_json(const char *content_path, contentJson_t *content_json)
content_json->cache = content_jsonGetBool(contentJson, "cache");
content_json->cloud_ruid = content_jsonGetString(contentJson, "cloud_ruid");
content_json->cloud_auth = content_jsonGetBytes(contentJson, "cloud_auth", &content_json->cloud_auth_len);
content_json->cloud_override = content_jsonGetBool(contentJson, "cloud_override");
content_json->tonie_model = content_jsonGetString(contentJson, "tonie_model");
content_json->cloud_valid = true;

// TODO: use checkCustomTonie to validate
if (osStrlen(content_json->cloud_ruid) != 16)
{
// TODO validate rUID
content_json->cloud_valid = false;
content_json->cloud_override = false;
}
if (content_json->cloud_auth_len != AUTH_TOKEN_LENGTH)
{
content_json->cloud_valid = false;
content_json->cloud_override = false;
}

if (osStrlen(content_json->source) > 0)
Expand Down Expand Up @@ -167,18 +182,22 @@ error_t load_content_json(const char *content_path, contentJson_t *content_json)
error = ERROR_FILE_NOT_FOUND;
}

if (error == NO_ERROR)
{
content_json->_valid = true;
}

if (error != NO_ERROR)
{
error = save_content_json(content_path, content_json);
if (error == NO_ERROR)
{
load_content_json(content_path, content_json);
}
}

osFreeMem(jsonPath);

if (error == NO_ERROR)
{
content_json->_valid = true;
}

return error;
}

Expand All @@ -195,6 +214,7 @@ error_t save_content_json(const char *content_path, contentJson_t *content_json)
cJSON_AddBoolToObject(contentJson, "cache", content_json->cache);
content_AddStringToObject(contentJson, "cloud_ruid", content_json->cloud_ruid);
content_AddByteArrayToObject(contentJson, "cloud_auth", content_json->cloud_auth, content_json->cloud_auth_len);
cJSON_AddBoolToObject(contentJson, "cloud_override", content_json->cloud_override);
content_AddStringToObject(contentJson, "tonie_model", content_json->tonie_model);
cJSON_AddNumberToObject(contentJson, "_version", CONTENT_JSON_VERSION);

Expand Down Expand Up @@ -232,6 +252,28 @@ error_t save_content_json(const char *content_path, contentJson_t *content_json)
return error;
}

void content_json_update_model(contentJson_t *content_json, uint32_t audio_id)
{
toniesJson_item_t *toniesJson = tonies_byAudioId(audio_id);
if (content_json->_valid)
{
if (toniesJson != NULL && osStrcmp(content_json->tonie_model, "") == 0)
{
if (osStrcmp(content_json->tonie_model, toniesJson->model) != 0)
{
osFreeMem(content_json->tonie_model);
content_json->tonie_model = strdup(toniesJson->model);
content_json->_updated = true;
}
}
else if (toniesJson == NULL && osStrcmp(content_json->tonie_model, "") != 0)
{
// TODO add to tonies.custom.json + report
TRACE_WARNING("Audio-id %08X unknown but previous content known by model %s.\r\n", audio_id, content_json->tonie_model);
}
}
}

void free_content_json(contentJson_t *content_json)
{
content_json->_valid = false;
Expand Down
Loading

0 comments on commit 5eebeb9

Please sign in to comment.