Skip to content

Commit

Permalink
Home Accessory Architect v12.14.1 Merlin
Browse files Browse the repository at this point in the history
  • Loading branch information
RavenSystem committed Dec 22, 2024
1 parent 3dffae8 commit e6ebf7f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 57 deletions.
4 changes: 2 additions & 2 deletions HAA/HAA_Main/main/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "../../common/common_headers.h"

// Version
#define HAA_FIRMWARE_VERSION "12.14.0"
#define HAA_FIRMWARE_VERSION "12.14.1"
#define HAA_FIRMWARE_BETA_REVISION "" // Format: "b01"
#define HAA_FIRMWARE_CODENAME "Merlin"

Expand All @@ -29,7 +29,7 @@

#define INITIAL_SETUP_TASK_SIZE (TASK_SIZE_FACTOR * (1600))
#define NTP_TASK_SIZE (TASK_SIZE_FACTOR * (512))
#define PING_TASK_SIZE GLOBAL_TASK_SIZE
#define PING_TASK_SIZE (TASK_SIZE_FACTOR * (896))
#define AUTODIMMER_TASK_SIZE GLOBAL_TASK_SIZE
#define IRRF_TX_TASK_SIZE (TASK_SIZE_FACTOR * (456))
#define UART_ACTION_TASK_SIZE (TASK_SIZE_FACTOR * (384))
Expand Down
50 changes: 9 additions & 41 deletions libs/homekit-rsf/src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,62 +46,30 @@ void json_flush(json_stream *json) {

void json_put(json_stream *json, char c) {
json->buffer[json->pos++] = c;
if (json->pos >= json->size - 1) {
if (json->pos >= HOMEKIT_JSON_BUFFER_SIZE - 1) {
json_flush(json);
}
}

void json_write(json_stream *json, const char *data, size_t size) {
while (size) {
size_t chunk_size = size;
if (size > json->size - json->pos) {
chunk_size = json->size - json->pos;
if (size > HOMEKIT_JSON_BUFFER_SIZE - json->pos) {
chunk_size = HOMEKIT_JSON_BUFFER_SIZE - json->pos;
}

memcpy((char*) json->buffer + json->pos, data, chunk_size);

json->pos += chunk_size;
if (json->pos >= json->size - 1) {
if (json->pos >= HOMEKIT_JSON_BUFFER_SIZE - 1) {
json_flush(json);
}

data += chunk_size;
size -= chunk_size;
}
}

/*
void json_write(json_stream *json, const char *format, ...) {
va_list arg_ptr;
va_start(arg_ptr, format);
size_t len = vsnprintf((char *)json->buffer + json->pos, json->size - json->pos, format, arg_ptr);
va_end(arg_ptr);
if (len + json->pos > json->size - 1) {
json_flush(json);
if (json->error) {
return;
}
va_start(arg_ptr, format);
size_t len2 = vsnprintf((char *)json->buffer + json->pos, json->size - json->pos, format, arg_ptr);
va_end(arg_ptr);
if (len2 + 1 > json->size) {
ERROR("Too large %i/%i", len2, json->size);
DEBUG("Format = %s", format);
DEBUG("Data = %s", (char *)json->buffer);
} else {
json->pos += len2;
}
} else {
json->pos += len;
}
}
*/

void json_object_start(json_stream *json) {
if (json->error)
json->state = JSON_STATE_ERROR;
Expand Down Expand Up @@ -141,7 +109,7 @@ void json_object_end(json_stream *json) {
if (!json->nesting_idx) {
json->state = JSON_STATE_END;
} else {
switch (json->nesting[json->nesting_idx-1]) {
switch (json->nesting[json->nesting_idx - 1]) {
case JSON_NESTING_OBJECT:
json->state = JSON_STATE_OBJECT_VALUE;
break;
Expand Down Expand Up @@ -197,7 +165,7 @@ void json_array_end(json_stream *json) {
if (!json->nesting_idx) {
json->state = JSON_STATE_END;
} else {
switch (json->nesting[json->nesting_idx-1]) {
switch (json->nesting[json->nesting_idx - 1]) {
case JSON_NESTING_OBJECT:
json->state = JSON_STATE_OBJECT_VALUE;
break;
Expand Down
5 changes: 4 additions & 1 deletion libs/homekit-rsf/src/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@

#define JSON_MAX_DEPTH (30)

#ifndef HOMEKIT_JSON_BUFFER_SIZE
#define HOMEKIT_JSON_BUFFER_SIZE (1024)
#endif

typedef int (*json_flush_callback)(uint8_t *buffer, size_t size, void *context);
typedef struct _json_stream {
uint8_t* buffer;
size_t size;
size_t pos;

uint8_t state: 7;
Expand Down
27 changes: 15 additions & 12 deletions libs/homekit-rsf/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include "pairing.h"
#include "storage.h"
#include "query_params.h"

#define HOMEKIT_JSON_BUFFER_SIZE (1024)
#include "json.h"
#include "debug.h"
#include "port.h"
Expand Down Expand Up @@ -154,9 +156,8 @@ typedef struct _notification {
struct _notification* next;
} notification_t;

#define BUFFER_DATA_SIZE (1442)
#define RECEIVED_DATA_SIZE (1024 + 18)
#define ENCRYPTED_DATA_SIZE (768)
#define BUFFER_DATA_SIZE (HOMEKIT_JSON_BUFFER_SIZE) // Used by JSON buffer too. Must be 2 bytes reserved for client_send_chunk() end

typedef struct {
char *accessory_id;
ed25519_key* accessory_key;
Expand All @@ -180,8 +181,8 @@ typedef struct {

json_stream json;

byte data[BUFFER_DATA_SIZE + 18];
byte encrypted[ENCRYPTED_DATA_SIZE + 18];
byte data[BUFFER_DATA_SIZE + 16 + 2]; // Used by JSON buffer too. Must be 2 bytes reserved for client_send_chunk() end; there are 18.
byte encrypted[BUFFER_DATA_SIZE + 16 + 2];

fd_set fds;
} homekit_server_t;
Expand Down Expand Up @@ -251,8 +252,6 @@ homekit_server_t *server_new() {

FD_ZERO(&homekit_server->fds);

json_init(&homekit_server->json, NULL);
homekit_server->json.size = BUFFER_DATA_SIZE + (18 - 2); // 2 bytes reserved for client_send_chunk() end
homekit_server->json.buffer = homekit_server->data;
homekit_server->json.on_flush = client_send_chunk;

Expand Down Expand Up @@ -721,8 +720,8 @@ int client_send_encrypted(client_context_t *context, byte *payload, size_t size)

while (payload_offset < size) {
size_t chunk_size = size - payload_offset;
if (chunk_size > ENCRYPTED_DATA_SIZE)
chunk_size = ENCRYPTED_DATA_SIZE;
if (chunk_size > sizeof(homekit_server->encrypted) - 16 - 2)
chunk_size = sizeof(homekit_server->encrypted) - 16 - 2;

byte aead[2] = {chunk_size % 256, chunk_size / 256};

Expand All @@ -735,7 +734,7 @@ int client_send_encrypted(client_context_t *context, byte *payload, size_t size)
x /= 256;
}

size_t available = ENCRYPTED_DATA_SIZE + (18 - 2);
size_t available = sizeof(homekit_server->encrypted) - 2;
int r = crypto_chacha20poly1305_encrypt(
context->read_key, nonce, aead, 2,
payload + payload_offset, chunk_size,
Expand Down Expand Up @@ -2181,6 +2180,8 @@ void homekit_server_on_get_characteristics(client_context_t *context) {
CLIENT_INFO(context, "Get CH");
DEBUG_HEAP();

//unsigned int time_start = sdk_system_get_time_raw();

query_param_t *qp = context->endpoint_params;
while (qp) {
CLIENT_DEBUG(context, "Query paramter %s = %s", qp->name, qp->value);
Expand Down Expand Up @@ -2316,6 +2317,8 @@ void homekit_server_on_get_characteristics(client_context_t *context) {
client_send_chunk(NULL, 0, context);
}

//CLIENT_INFO(context, "Time %i", sdk_system_get_time_raw() - time_start);

free(id);
}

Expand Down Expand Up @@ -3308,7 +3311,7 @@ static http_parser_settings homekit_http_parser_settings = {
static inline void IRAM homekit_client_process(client_context_t *context) {
int data_len = read(context->socket,
homekit_server->data + homekit_server->data_available,
RECEIVED_DATA_SIZE - homekit_server->data_available
sizeof(homekit_server->data) - homekit_server->data_available
);

if (data_len > 0) {
Expand All @@ -3317,7 +3320,7 @@ static inline void IRAM homekit_client_process(client_context_t *context) {
size_t payload_size = (size_t) data_len;
CLIENT_DEBUG(context, "Received Payload:\n%s", (char*) payload);

size_t decrypted_size = BUFFER_DATA_SIZE - 2;
size_t decrypted_size = sizeof(homekit_server->data) - 16 - 2;

if (context->encrypted) {
CLIENT_DEBUG(context, "Decrypting data");
Expand Down
2 changes: 1 addition & 1 deletion libs/rs_ping/rs_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ int rs_ping(ip_addr_t ping_target) {

#endif
if (s < 0) {
printf("ping error creating socket (%i)\n", s);
printf("! Ping socket (%i)\n", s);
return s;
}

Expand Down

0 comments on commit e6ebf7f

Please sign in to comment.