Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ conds:
- ["hap", "o", {"title": "HAP settings"}]
- ["hap.salt", "s", "", {"title": "Device verifier salt"}]
- ["hap.verifier", "s", "", {"title": "Device verifier"}]
- ["hap.setupid", "s", "", {"title": "Device setup id (Four characters)"}]
cdefs:
MGOS_HAP_SIMPLE_CONFIG: 1

Expand Down
31 changes: 31 additions & 0 deletions src/PAL/HAPPlatformAccessorySetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* limitations under the License.
*/

#include "mgos.h"
#include "mgos_hap.h"

#include "HAPPlatformAccessorySetup.h"
#include "HAPPlatformAccessorySetup+Init.h"

Expand All @@ -23,6 +26,34 @@ void HAPPlatformAccessorySetupCreate(
const HAPPlatformAccessorySetupOptions* options HAP_UNUSED) {
}

void HAPPlatformAccessorySetupLoadSetupInfo(HAPPlatformAccessorySetupRef accessorySetup, HAPSetupInfo* setupInfo) {
struct mgos_hap_load_setup_info_arg arg = {
.accessorySetup = accessorySetup,
.setupInfo = setupInfo,
};
mgos_event_trigger(MGOS_HAP_EV_LOAD_SETUP_INFO, &arg);
}

void HAPPlatformAccessorySetupLoadSetupCode(HAPPlatformAccessorySetupRef accessorySetup, HAPSetupCode* setupCode) {
struct mgos_hap_load_setup_code_arg arg = {
.accessorySetup = accessorySetup,
.setupCode = setupCode,
};
mgos_event_trigger(MGOS_HAP_EV_LOAD_SETUP_CODE, &arg);
}

void HAPPlatformAccessorySetupLoadSetupID(
HAPPlatformAccessorySetupRef accessorySetup,
bool* valid,
HAPSetupID* setupID) {
struct mgos_hap_load_setup_id_arg arg = {
.accessorySetup = accessorySetup,
.valid = valid,
.setupID = setupID,
};
mgos_event_trigger(MGOS_HAP_EV_LOAD_SETUP_ID, &arg);
}

HAPPlatformAccessorySetupCapabilities
HAPPlatformAccessorySetupGetCapabilities(HAPPlatformAccessorySetupRef accessorySetup) {
HAPPrecondition(accessorySetup);
Expand Down
71 changes: 71 additions & 0 deletions src/PAL/HAPPlatformAccessorySetupDisplay+Init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2015-2019 The HomeKit ADK Contributors
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// See [CONTRIBUTORS.md] for the list of HomeKit ADK project authors.

#ifndef HAP_PLATFORM_ACCESSORY_SETUP_DISPLAY_INIT_H
#define HAP_PLATFORM_ACCESSORY_SETUP_DISPLAY_INIT_H

#ifdef __cplusplus
extern "C" {
#endif

#include "HAPPlatform.h"

#if __has_feature(nullability)
#pragma clang assume_nonnull begin
#endif

#ifndef HAVE_DISPLAY
#define HAVE_DISPLAY 0
#endif

/**@file
* Accessory setup display.
*
* - The HAPLog APIs are used to display the setup payload and setup code.
* For a real display the implementation needs to be adjusted.
*
* **Example**

@code{.c}

// Allocate Accessory setup display.
static HAPPlatformAccessorySetupDisplay setupDisplay;

// Initialize Accessory setup display.
HAPPlatformAccessorySetupDisplayCreate(&setupDisplay);

@endcode
*/

/**
* Accessory setup display.
*/
struct HAPPlatformAccessorySetupDisplay {
// Opaque type. Do not access the instance fields directly.
/**@cond */
HAPSetupPayload setupPayload;
HAPSetupCode setupCode;
bool setupPayloadIsSet : 1;
bool setupCodeIsSet : 1;
/**@endcond */
};

/**
* Initializes Accessory setup display.
*
* @param[out] setupDisplay Accessory setup display.
*/
void HAPPlatformAccessorySetupDisplayCreate(HAPPlatformAccessorySetupDisplayRef setupDisplay);

#if __has_feature(nullability)
#pragma clang assume_nonnull end
#endif

#ifdef __cplusplus
}
#endif

#endif
54 changes: 54 additions & 0 deletions src/PAL/HAPPlatformAccessorySetupDisplay.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2015-2019 The HomeKit ADK Contributors
// Copyright (c) 2019 Deomid "rojer" Ryabkov
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// See [CONTRIBUTORS.md] for the list of HomeKit ADK project authors.

#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>

#include "mgos.h"
#include "mgos_hap.h"

#include "HAPPlatform.h"
#include "HAPPlatformAccessorySetupDisplay+Init.h"

void HAPPlatformAccessorySetupDisplayCreate(HAPPlatformAccessorySetupDisplayRef setupDisplay) {
HAPPrecondition(HAVE_DISPLAY);
HAPPrecondition(setupDisplay);

HAPRawBufferZero(setupDisplay, sizeof *setupDisplay);
}

void HAPPlatformAccessorySetupDisplayUpdateSetupPayload(
HAPPlatformAccessorySetupDisplayRef setupDisplay,
const HAPSetupPayload* _Nullable setupPayload,
const HAPSetupCode* _Nullable setupCode) {

struct mgos_hap_display_update_setup_payload_arg arg = {
.setupDisplay = setupDisplay,
.setupPayload = setupPayload,
.setupCode = setupCode,
};

mgos_event_trigger(MGOS_HAP_EV_DISPLAY_UPDATE_SETUP_PAYLOAD, &arg);
}

void HAPPlatformAccessorySetupDisplayHandleStartPairing(HAPPlatformAccessorySetupDisplayRef setupDisplay) {
struct mgos_hap_display_start_pairing_arg arg = {
.setupDisplay = setupDisplay,
};
mgos_event_trigger(MGOS_HAP_EV_DISPLAY_START_PAIRING, &arg);
}

void HAPPlatformAccessorySetupDisplayHandleStopPairing(HAPPlatformAccessorySetupDisplayRef setupDisplay) {
struct mgos_hap_display_stop_pairing_arg arg = {
.setupDisplay = setupDisplay,
};
mgos_event_trigger(MGOS_HAP_EV_DISPLAY_STOP_PAIRING, &arg);
}
31 changes: 31 additions & 0 deletions src/PAL/HAPPlatformAccessorySetupNFC.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2015-2019 The HomeKit ADK Contributors
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// See [CONTRIBUTORS.md] for the list of HomeKit ADK project authors.

#include "mgos.h"
#include "mgos_hap.h"

#include "HAPPlatform.h"

#ifndef HAVE_NFC
#define HAVE_NFC 0
#endif

#if HAVE_NFC
#include <errno.h>
#include <unistd.h>
#endif

void HAPPlatformAccessorySetupNFCUpdateSetupPayload(
HAPPlatformAccessorySetupNFCRef setupNFC,
const HAPSetupPayload* setupPayload,
bool isPairable) {
struct mgos_hap_nfc_update_setup_payload_arg arg = {
.setupNFC = setupNFC,
.setupPayload = setupPayload,
.isPairable = isPairable,
};
mgos_event_trigger(MGOS_HAP_EV_NFC_UPDATE_SETUP_PAYLOAD, &arg);
}
3 changes: 2 additions & 1 deletion src/PAL/HAPPlatformTCPStreamManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ bool HAPPlatformTCPStreamManagerIsListenerOpen(HAPPlatformTCPStreamManagerRef tc
}

static struct mg_connection* HAPMGListenerGetNextPendingConnection(HAPPlatformTCPStreamManagerRef tm) {
if (tm->listener == NULL) return NULL; // Stopping.
if (tm->listener == NULL)
return NULL; // Stopping.
struct mg_mgr* mgr = tm->listener->mgr;
struct mg_connection* result = NULL;
for (struct mg_connection* nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) {
Expand Down
127 changes: 57 additions & 70 deletions src/mgos_homekit_adk.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,7 @@
#include "mgos_hap.h"

#include "mgos.h"

void HAPPlatformAccessorySetupLoadSetupInfo(HAPPlatformAccessorySetupRef accessorySetup, HAPSetupInfo* setupInfo) {
struct mgos_hap_load_setup_info_arg arg = {
.accessorySetup = accessorySetup,
.setupInfo = setupInfo,
};
mgos_event_trigger(MGOS_HAP_EV_LOAD_SETUP_INFO, &arg);
}

void HAPPlatformAccessorySetupLoadSetupCode(HAPPlatformAccessorySetupRef accessorySetup, HAPSetupCode* setupCode) {
struct mgos_hap_load_setup_code_arg arg = {
.accessorySetup = accessorySetup,
.setupCode = setupCode,
};
mgos_event_trigger(MGOS_HAP_EV_LOAD_SETUP_CODE, &arg);
}

void HAPPlatformAccessorySetupLoadSetupID(
HAPPlatformAccessorySetupRef accessorySetup,
bool* valid,
HAPSetupID* setupID) {
struct mgos_hap_load_setup_id_arg arg = {
.accessorySetup = accessorySetup,
.valid = valid,
.setupID = setupID,
};
mgos_event_trigger(MGOS_HAP_EV_LOAD_SETUP_ID, &arg);
}

void HAPPlatformAccessorySetupDisplayUpdateSetupPayload(
HAPPlatformAccessorySetupDisplayRef setupDisplay,
const HAPSetupPayload* _Nullable setupPayload,
const HAPSetupCode* _Nullable setupCode) {

struct mgos_hap_display_update_setup_payload_arg arg = {
.setupDisplay = setupDisplay,
.setupPayload = setupPayload,
.setupCode = setupCode,
};
mgos_event_trigger(MGOS_HAP_EV_DISPLAY_UPDATE_SETUP_PAYLOAD, &arg);
}

void HAPPlatformAccessorySetupDisplayHandleStartPairing(HAPPlatformAccessorySetupDisplayRef setupDisplay) {
struct mgos_hap_display_start_pairing_arg arg = {
.setupDisplay = setupDisplay,
};
mgos_event_trigger(MGOS_HAP_EV_DISPLAY_START_PAIRING, &arg);
}

void HAPPlatformAccessorySetupDisplayHandleStopPairing(HAPPlatformAccessorySetupDisplayRef setupDisplay) {
struct mgos_hap_display_stop_pairing_arg arg = {
.setupDisplay = setupDisplay,
};
mgos_event_trigger(MGOS_HAP_EV_DISPLAY_STOP_PAIRING, &arg);
}

void HAPPlatformAccessorySetupNFCUpdateSetupPayload(
HAPPlatformAccessorySetupNFCRef setupNFC,
const HAPSetupPayload* setupPayload,
bool isPairable) {
struct mgos_hap_nfc_update_setup_payload_arg arg = {
.setupNFC = setupNFC,
.setupPayload = setupPayload,
.isPairable = isPairable,
};
mgos_event_trigger(MGOS_HAP_EV_NFC_UPDATE_SETUP_PAYLOAD, &arg);
}
#include "HAPAccessorySetup.h"

bool mgos_hap_setup_info_from_string(HAPSetupInfo* setupInfo, const char* salt, const char* verifier) {
int salt_len = strlen(salt != NULL ? salt : "");
Expand Down Expand Up @@ -112,8 +46,40 @@ bool mgos_hap_setup_info_from_string(HAPSetupInfo* setupInfo, const char* salt,
return (d == (int) sizeof(setupInfo->verifier));
}

bool mgos_hap_setup_id_from_string(HAPSetupID* setupID, const char* id) {

int id_len = strlen(id != NULL ? id : "");

// when no setup id was provided, generate an id
if (id_len == 0 || id_len > sizeof(HAPSetupID)) {

HAPAccessorySetupGenerateRandomSetupID(setupID);

// save it
mgos_sys_config_set_hap_setupid(setupID->stringValue);

char* err = NULL;
save_cfg(&mgos_sys_config, &err);
printf("Saving configuration: %s\n", err ? err : "no error");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops. Will be corrected ..

free(err);

// generated id is always valid.
return true;
}

// check if provided id is valid
if (HAPAccessorySetupIsValidSetupID(id)) {
// copy valid id string to stringValue
strncpy(setupID->stringValue, id, sizeof(HAPSetupID));
return true;
} // ending here ... if a non valid setup id was provided don't overrule it. chosen faith.

// no valid setup id was generated or read
return false;
}

#ifdef MGOS_HAP_SIMPLE_CONFIG
static void setup_info_cb(int ev, void* ev_data, void* userdata) {
static void load_setup_info_cb(int ev, void* ev_data, void* userdata) {
struct mgos_hap_load_setup_info_arg* arg = (struct mgos_hap_load_setup_info_arg*) ev_data;
if (!mgos_hap_setup_info_from_string(
arg->setupInfo, mgos_sys_config_get_hap_salt(), mgos_sys_config_get_hap_verifier())) {
Expand All @@ -123,16 +89,37 @@ static void setup_info_cb(int ev, void* ev_data, void* userdata) {
(void) userdata;
}

static void load_setup_id_cb(int ev, void* ev_data, void* userdata) {

LOG(LL_DEBUG, ("%s: Loading setup identifier...", __func__));

struct mgos_hap_load_setup_id_arg* arg = (struct mgos_hap_load_setup_id_arg*) ev_data;

if (!mgos_hap_setup_id_from_string(arg->setupID, mgos_sys_config_get_hap_setupid())) {
LOG(LL_ERROR, ("Failed to load or generate HAP accessory setup identifier!"));
*arg->valid = false;
} else {
*arg->valid = true;
LOG(LL_DEBUG, ("Success loading setup id. (Identifier is \"%s\")", arg->setupID->stringValue));
}

(void) ev;
(void) userdata;
}

bool mgos_hap_config_valid(void) {
HAPSetupInfo setupInfo;
HAPSetupID setupID;
return mgos_hap_setup_info_from_string(
&setupInfo, mgos_sys_config_get_hap_salt(), mgos_sys_config_get_hap_verifier());
&setupInfo, mgos_sys_config_get_hap_salt(), mgos_sys_config_get_hap_verifier()) &&
mgos_hap_setup_id_from_string(&setupID, mgos_sys_config_get_hap_setupid());
}
#endif

bool mgos_homekit_adk_init(void) {
#ifdef MGOS_HAP_SIMPLE_CONFIG
mgos_event_add_handler(MGOS_HAP_EV_LOAD_SETUP_INFO, setup_info_cb, NULL);
mgos_event_add_handler(MGOS_HAP_EV_LOAD_SETUP_INFO, load_setup_info_cb, NULL);
mgos_event_add_handler(MGOS_HAP_EV_LOAD_SETUP_ID, load_setup_id_cb, NULL);
#endif
mgos_event_register_base(MGOS_HAP_EV_BASE, "HAP");
return true;
Expand Down