Skip to content

Commit

Permalink
-Fix ignored overrideCloud flag for toniebox settings
Browse files Browse the repository at this point in the history
-Fix freshnessCheck response if cloud is on (hacky)
  • Loading branch information
SciLor committed Oct 26, 2023
1 parent 33e9a12 commit 4cdfc82
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
2 changes: 2 additions & 0 deletions include/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ typedef struct
uint32_t status;
FsFile *file;
tonie_info_t *tonieInfo;
void *customData;
size_t customDataLen;
HttpConnection *connection;
client_ctx_t *client_ctx;
} cbr_ctx_t;
Expand Down
88 changes: 70 additions & 18 deletions src/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,30 @@ void cbrCloudResponsePassthrough(void *src_ctx, HttpClientContext *cloud_ctx)
void cbrCloudHeaderPassthrough(void *src_ctx, HttpClientContext *cloud_ctx, const char *header, const char *value)
{
cbr_ctx_t *ctx = (cbr_ctx_t *)src_ctx;
char line[128];

if (header)
{
TRACE_INFO(">> cbrCloudHeaderPassthrough: %s = %s\r\n", header, value);
osSprintf(line, "%s: %s\r\n", header, value);
}
else
switch (ctx->api)
{
TRACE_INFO(">> cbrCloudHeaderPassthrough: NULL\r\n");
osStrcpy(line, "\r\n");
}
case V1_FRESHNESS_CHECK:
if (!header || osStrcmp(header, "Content-Length") == 0) // Skip empty line at the and + contentlen
{
break;
}
default:
char line[128];
if (header)
{
TRACE_INFO(">> cbrCloudHeaderPassthrough: %s = %s\r\n", header, value);
osSprintf(line, "%s: %s\r\n", header, value);
}
else
{
TRACE_INFO(">> cbrCloudHeaderPassthrough: NULL\r\n");
osStrcpy(line, "\r\n");
}

httpSend(ctx->connection, line, osStrlen(line), HTTP_FLAG_DELAY);
httpSend(ctx->connection, line, osStrlen(line), HTTP_FLAG_DELAY);
break;
}
ctx->status = PROX_STATUS_HEAD;
}

Expand Down Expand Up @@ -142,21 +152,63 @@ void cbrCloudBodyPassthrough(void *src_ctx, HttpClientContext *cloud_ctx, const
case V1_FRESHNESS_CHECK:
if (ctx->client_ctx->settings->toniebox.overrideCloud && length > 0 && fillCbrBodyCache(ctx, httpClientContext, payload, length))
{
TonieFreshnessCheckResponse *freshResp = tonie_freshness_check_response__unpack(NULL, ctx->bufferLen, (const uint8_t *)ctx->buffer);
setTonieboxSettings(freshResp, ctx->client_ctx->settings);
size_t packSize = tonie_freshness_check_response__get_packed_size(freshResp);
TonieFreshnessCheckResponse *freshResp = (TonieFreshnessCheckResponse *)ctx->customData;
TonieFreshnessCheckResponse *freshRespCloud = tonie_freshness_check_response__unpack(NULL, ctx->bufferLen, (const uint8_t *)ctx->buffer);
if (ctx->client_ctx->settings->toniebox.overrideCloud)
{
setTonieboxSettings(freshResp, ctx->client_ctx->settings);
}
else
{
freshResp->max_vol_spk = freshRespCloud->max_vol_spk;
freshResp->max_vol_hdp = freshRespCloud->max_vol_hdp;
freshResp->slap_en = freshRespCloud->slap_en;
freshResp->slap_dir = freshRespCloud->slap_dir;
freshResp->led = freshRespCloud->led;
freshResp->field2 = freshRespCloud->field2;
freshResp->field6 = freshRespCloud->field6;
}

for (size_t i = 0; i < freshRespCloud->n_tonie_marked; i++)
{
bool found = false;
for (size_t j = 0; j < freshResp->n_tonie_marked; j++)
{
if (freshRespCloud->tonie_marked[i] == freshResp->tonie_marked[j])
{
found = true;
break;
}
}
if (!found)
{
// handleCloudFreshnessCheck allocated space for all in freshResp.tonie_marked
if (ctx->customDataLen > freshResp->n_tonie_marked)
{
freshResp->tonie_marked[freshResp->n_tonie_marked++] = freshRespCloud->tonie_marked[i];
TRACE_INFO("Marked UID %" PRIx64 " as updated from cloud\r\n", freshRespCloud->tonie_marked[i]);
}
else
{
TRACE_WARNING("Could not add UID %" PRIx64 " to freshnessCheck response, as not enough slots allocated!\r\n", freshRespCloud->tonie_marked[i]);
}
}
}
tonie_freshness_check_response__free_unpacked(freshRespCloud, NULL);

// TODO: Check if size is stable and this is obsolete
// TODO Add live tonies here, too : freshResp.tonie_marked
size_t packSize = tonie_freshness_check_response__get_packed_size(freshResp);
if (ctx->bufferLen < packSize)
{
TRACE_WARNING(">> cbrCloudBodyPassthrough V1_FRESHNESS_CHECK: %zu / %zu\r\n", ctx->bufferLen, packSize);
osFreeMem(ctx->buffer);
ctx->bufferLen = packSize;
ctx->buffer = osAllocMem(ctx->bufferLen);
}
tonie_freshness_check_response__pack(freshResp, (uint8_t *)ctx->buffer);
tonie_freshness_check_response__free_unpacked(freshResp, NULL);

char line[128];
osSnprintf(line, 128, "Content-Length: %" PRIuSIZE "\r\n\r\n", ctx->bufferLen);
httpSend(ctx->connection, line, osStrlen(line), HTTP_FLAG_DELAY);

httpSend(ctx->connection, ctx->buffer, ctx->bufferLen, HTTP_FLAG_DELAY);
osFreeMem(ctx->buffer);
}
Expand Down
15 changes: 8 additions & 7 deletions src/handler_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,23 +663,24 @@ error_t handleCloudFreshnessCheck(HttpConnection *connection, const char_t *uri,
{
size_t dataLen = tonie_freshness_check_request__get_packed_size(&freshReqCloud);
tonie_freshness_check_request__pack(&freshReqCloud, (uint8_t *)data);
tonie_freshness_check_request__free_unpacked(freshReq, NULL);

osFreeMem(freshReqCloud.tonie_infos);
osFreeMem(freshResp.tonie_marked);

cbr_ctx_t ctx;
ctx.customData = (void *)&freshResp;
ctx.customDataLen = freshReq->n_tonie_infos; // Allocated slots
req_cbr_t cbr = getCloudCbr(connection, uri, queryString, V1_FRESHNESS_CHECK, &ctx, client_ctx);
if (!cloud_request_post(NULL, 0, "/v1/freshness-check", queryString, data, dataLen, NULL, &cbr))
{
tonie_freshness_check_request__free_unpacked(freshReq, NULL);
osFreeMem(freshReqCloud.tonie_infos);
osFreeMem(freshResp.tonie_marked);
return NO_ERROR;
}
}
else
tonie_freshness_check_request__free_unpacked(freshReq, NULL);
if (client_ctx->settings->toniebox.overrideCloud)
{
tonie_freshness_check_request__free_unpacked(freshReq, NULL);
setTonieboxSettings(&freshResp, client_ctx->settings);
}
setTonieboxSettings(&freshResp, client_ctx->settings);

size_t dataLen = tonie_freshness_check_response__get_packed_size(&freshResp);
tonie_freshness_check_response__pack(&freshResp, (uint8_t *)data);
Expand Down

0 comments on commit 4cdfc82

Please sign in to comment.