Skip to content

Commit

Permalink
#34
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurik72 committed Dec 14, 2020
1 parent 8051303 commit f8d7481
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 12 deletions.
Binary file modified .vs/ESPHap/v15/.suo
Binary file not shown.
2 changes: 1 addition & 1 deletion .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"",
"\\hapweb"
],
"SelectedNode": "\\homeintegration.c",
"SelectedNode": "\\storage.h",
"PreviewInSolutionExplorer": false
}
Binary file modified .vs/slnx.sqlite
Binary file not shown.
77 changes: 68 additions & 9 deletions homeintegration.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#define MAX_SERVICES 20


static callback_storagechanged callbackstorage=NULL;
static callback_storagechanged callbackstorage_integration=NULL;
static int led_gpio = -1;


Expand Down Expand Up @@ -129,7 +129,7 @@ homekit_server_config_t config = {
*/

#define MAX_HAP_SERVICES 7
#define MAX_HAP_ACCESSORIES 4
#define MAX_HAP_ACCESSORIES 7
homekit_accessory_t *hap_accessories[MAX_HAP_ACCESSORIES + 1] = { 0 };
homekit_service_t* hap_services[MAX_HAP_SERVICES + 1] = { 0 };
homekit_server_config_t hap_config = {
Expand All @@ -138,9 +138,11 @@ homekit_server_config_t hap_config = {
.setupId = "YK72"
};
void on_storage_changed(){

if(callbackstorage)
callbackstorage(get_ex_storage(),get_ex_storage_size());
//INFO("on_storage_change");
if(callbackstorage_integration)
callbackstorage_integration(get_ex_storage(),get_ex_storage_size());
// else
// INFO("on_storage_change pointer 0x%x", callbackstorage_integration);
}
#ifndef ARDUINO8266_SERVER_CPP
void init_homekit_server() {
Expand All @@ -155,9 +157,14 @@ void on_wifi_ready() {
*/
//storage handling


callback_storagechanged get_callback_storage_change() {
return callbackstorage_integration;

}
void set_callback_storage_change(callback_storagechanged fn){
callbackstorage=fn;

callbackstorage_integration =fn;
//INFO("set_callback_storage_change %d ", (long)callbackstorage_integration);
}


Expand Down Expand Up @@ -185,10 +192,14 @@ static bool paired = false;

#ifndef ARDUINO8266_SERVER_CPP
void hap_init_homekit_server() {
// INFO("callbackstorage_integration 0x%x ", callbackstorage_integration);
set_wifi_max_power();
if(hap_mainservices_current>1){
set_callback_storage(on_storage_changed);
paired = homekit_is_paired();
INFO("homekit_is_paired %d",paired);
// INFO("callbackstorage_integration 0x%x ", callbackstorage_integration);

if(base_accessory_index==-1){
hap_init_homekit_base_accessory();
}else{
Expand Down Expand Up @@ -374,7 +385,7 @@ homekit_service_t* hap_add_rgbstrip_service_as_accessory(int acctype, const cha
hap_mainaccesories_current++;
hap_accessories[hap_mainaccesories_current] = NULL;
// INFO("added light bulb as accessory , next accessory %d",hap_mainaccesories_current);
return lbservice;
return rgbservice;
}
homekit_service_t* hap_new_rgbstrip_service(const char* szname, hap_callback cb, void* context) {
return NEW_HOMEKIT_SERVICE(LIGHTBULB, .primary = true, .characteristics = (homekit_characteristic_t*[]) {
Expand Down Expand Up @@ -612,7 +623,7 @@ homekit_service_t* hap_add_hum_as_accessory(int acctype, const char* szname) {
homekit_service_t* hap_new_light_service(const char* szname, hap_callback cb, void* context){
return NEW_HOMEKIT_SERVICE(LIGHT_SENSOR, .characteristics=(homekit_characteristic_t*[]) {
NEW_HOMEKIT_CHARACTERISTIC(NAME, szname),
NEW_HOMEKIT_CHARACTERISTIC(CURRENT_AMBIENT_LIGHT_LEVEL, 5.0, .min_value = (float[]) { 0.0 }, .max_value = (float[]) { 100.0 }),
NEW_HOMEKIT_CHARACTERISTIC(CURRENT_AMBIENT_LIGHT_LEVEL, 5.0),
NULL
});
}
Expand Down Expand Up @@ -886,4 +897,52 @@ homekit_value_t HOMEKIT_UINT8_VALUE(uint8_t value) {
homekit_value.format = homekit_format_uint8;
homekit_value.int_value = value;
return homekit_value;
}

int set_wifi_max_power() {
#ifdef ESP32
esp_err_t res;
if (res = esp_wifi_set_ps(WIFI_PS_NONE) != ESP_OK) {
INFO("error esp_wifi_set_ps %d", res);
return res;
};
int8_t power;
if (res = esp_wifi_get_max_tx_power(&power) != ESP_OK) {
INFO("error esp_wifi_get_max_tx_power %d", res);
};
if (power < 78) {
if (res = esp_wifi_set_max_tx_power(78) != ESP_OK) {
INFO("error esp_wifi_set_max_tx_power %d", res);
};
}
return res;
#else
return -1;
#endif

}
int set_wifi_save_power_middle(void) {
return set_wifi_save_power(52);
}
int set_wifi_save_power(int8_t level) {
#ifdef ESP32
esp_err_t res;
if (res = esp_wifi_set_ps(WIFI_PS_MAX_MODEM) != ESP_OK) {
INFO("error esp_wifi_set_ps %d", res);
return res;
};
int8_t power;
if (res = esp_wifi_get_max_tx_power(&power) != ESP_OK) {
INFO("error esp_wifi_get_max_tx_power %d", res);
};
if (level != power) {
if (res = esp_wifi_set_max_tx_power(level) != ESP_OK) {
INFO("error esp_wifi_set_max_tx_power %d", res);
};
}
return res;
#else
return -1;
#endif

}
16 changes: 15 additions & 1 deletion homeintegration.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@

#define INFO(message, ...) printf(">>> Home Integration: " message "\n", ##__VA_ARGS__)

#define hap_constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))

#ifndef HAP_NOTIFY_CHANGES
#define HAP_NOTIFY_CHANGES(name,home_characteristic,val,tollerance) \
if (home_characteristic && abs(home_characteristic->value.name ##_value - val)>tollerance){ \
home_characteristic->value.name ##_value = val ;\
homekit_characteristic_notify(home_characteristic, home_characteristic->value); \
};
#endif
#ifndef HAP_NOTIFY_CHANGES_WITHCONSTRAIN
#define HAP_NOTIFY_CHANGES_WITHCONSTRAIN(name,home_characteristic,val,tollerance) \
name cval=hap_constrain(val,*home_characteristic->min_value,*home_characteristic->max_value); \
if (home_characteristic && abs(home_characteristic->value.name ##_value - cval)>tollerance){ \
home_characteristic->value.name ##_value = cval ;\
homekit_characteristic_notify(home_characteristic, home_characteristic->value); \
};
#endif

#define HAP_IMPLEMENT_GETVAL(name,type,service,characteristic)\
type name() { \
Expand All @@ -40,6 +50,7 @@ type name() { \

typedef void(*callback_storagechanged)(char * szstorage, int size);
void set_callback_storage_change(callback_storagechanged fn);
callback_storagechanged get_callback_storage_change();

int hap_init_storage_ex(char* szdata, int size);
int hap_get_storage_size_ex();
Expand Down Expand Up @@ -124,4 +135,7 @@ type name() { \
int hap_get_setup_uri(char *buffer, size_t buffer_size);
#endif

homekit_value_t HOMEKIT_UINT8_VALUE(uint8_t value);
homekit_value_t HOMEKIT_UINT8_VALUE(uint8_t value);
int set_wifi_max_power();
int set_wifi_save_power_middle(void);
int set_wifi_save_power(int8_t level);
3 changes: 2 additions & 1 deletion mdnsresponder.c
Original file line number Diff line number Diff line change
Expand Up @@ -960,11 +960,12 @@ static void mdns_check_network() {
// If we are in station mode and have an IP address, start a multicast UDP receive
void mdns_init()
{

sdk_os_timer_setfn(&announce_timer, mdns_announce, NULL);
sdk_os_timer_setfn(&network_monitor_timer, mdns_check_network, NULL);

err_t err;

struct netif *netif = sdk_system_get_netif(STATION_IF);
if (netif == NULL) {
printf(">>> mDNS_init: wifi opmode not station\n");
Expand Down
1 change: 1 addition & 0 deletions port.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ void homekit_overclock_end() {
}

void homekit_mdns_init() {
// printf("homekit_mdns_init()\r\n");
mdns_init();
}
void homekit_mdns_add_txt_ex(const char *key, const char *val) {
Expand Down
1 change: 1 addition & 0 deletions storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern "C" {

void set_callback_storage(callback_function val);
void on_storage_change();

#endif


Expand Down

0 comments on commit f8d7481

Please sign in to comment.