Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce client states #168

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ typedef enum {
characteristic_format_events = (1 << 4),
} characteristic_format_t;

typedef enum {
ENCRYPTION_STATE_FALSE = 0,
ENCRYPTION_STATE_VERIFIED,
ENCRYPTION_STATE_GOT_ACCESSORIES,
ENCRYPTION_STATE_IN_USE,
} ENCRYPTION_STATE;


typedef struct {
Srp *srp;
Expand Down Expand Up @@ -174,7 +181,7 @@ struct _client_context_t {
homekit_characteristic_t *current_characteristic;
homekit_value_t *current_value;

bool encrypted;
int encrypted;
byte read_key[32];
byte write_key[32];
int count_reads;
Expand Down Expand Up @@ -431,7 +438,7 @@ client_context_t *client_context_new() {
c->id = 0;

c->pairing_id = -1;
c->encrypted = false;
c->encrypted = ENCRYPTION_STATE_FALSE;
c->count_reads = 0;
c->count_writes = 0;

Expand Down Expand Up @@ -1055,6 +1062,8 @@ void homekit_server_on_identify(client_context_t *context) {
CLIENT_INFO(context, "Identify");
DEBUG_HEAP();

context->encrypted = ENCRYPTION_STATE_IN_USE;

if (context->server->paired) {
// Already paired
send_json_error_response(context, 400, HAPStatus_InsufficientPrivileges);
Expand Down Expand Up @@ -2309,7 +2318,7 @@ void homekit_server_on_pair_verify(client_context_t *context, const byte *data,

context->pairing_id = pairing_id;
context->permissions = permissions;
context->encrypted = true;
context->encrypted = ENCRYPTION_STATE_VERIFIED;

HOMEKIT_NOTIFY_EVENT(context->server, HOMEKIT_EVENT_CLIENT_VERIFIED);

Expand Down Expand Up @@ -2400,12 +2409,16 @@ void homekit_server_on_get_accessories(client_context_t *context) {
json_flush(json);

client_send_chunk(NULL, 0, context);

context->encrypted = ENCRYPTION_STATE_GOT_ACCESSORIES;
}

void homekit_server_on_get_characteristics(client_context_t *context) {
CLIENT_INFO(context, "Get Characteristics");
DEBUG_HEAP();

context->encrypted = ENCRYPTION_STATE_IN_USE;

if (context->server->endpoint_params.ids[0].aid == 0) {
CLIENT_ERROR(context, "Invalid get characteristics request: missing ID parameter");
send_json_error_response(context, 400, HAPStatus_InvalidValue);
Expand Down Expand Up @@ -2500,6 +2513,8 @@ void homekit_server_on_update_characteristics(client_context_t *context, const b
CLIENT_INFO(context, "Update Characteristics");
DEBUG_HEAP();

context->encrypted = ENCRYPTION_STATE_IN_USE;

cJSON *json = cJSON_Parse((char *)data);

if (!json) {
Expand Down Expand Up @@ -2973,6 +2988,8 @@ void homekit_server_on_pairings(client_context_t *context, const byte *data, siz
DEBUG("HomeKit Pairings");
DEBUG_HEAP();

context->encrypted = ENCRYPTION_STATE_IN_USE;

tlv_values_t *message = tlv_new();
if (!message) {
CLIENT_ERROR(context, "Failed to allocate memory for TLV payload");
Expand Down Expand Up @@ -3240,6 +3257,8 @@ void homekit_server_on_resource(client_context_t *context) {
CLIENT_INFO(context, "Resource");
DEBUG_HEAP();

context->encrypted = ENCRYPTION_STATE_IN_USE;

if (!context->server->config->on_resource) {
send_404_response(context);
return;
Expand Down