Skip to content

Commit 58ae946

Browse files
committed
Small optimization
Change aid and iid to uint32_t. Add homekit_update_config_number function. Change the mark of ge_precomp to ICACHE_RODATA_ATTR since it is 32-bit aliged visited (no need to copy to ram).
1 parent a60d305 commit 58ae946

File tree

7 files changed

+75
-93
lines changed

7 files changed

+75
-93
lines changed

src/accessories.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,32 +394,29 @@ void homekit_characteristic_ex_old_setter(homekit_characteristic_t *ch, homekit_
394394
ch->setter(value);
395395
}
396396

397-
398397
void homekit_accessories_init(homekit_accessory_t **accessories) {
399398
//设置aid 和 iid (自增1)
400-
int aid = 1;
399+
uint32_t aid = 1;
401400
for (homekit_accessory_t **accessory_it = accessories; *accessory_it; accessory_it++) {
402-
homekit_accessory_t *accessory = *accessory_it;
401+
homekit_accessory_t *accessory = *accessory_it;
403402
if (accessory->id) {
404403
if (accessory->id >= aid)
405404
aid = accessory->id+1;
406405
} else {
407406
accessory->id = aid++;
408407
}
409-
410-
int iid = 1;
408+
uint32_t iid = 1;
411409
for (homekit_service_t **service_it = accessory->services; *service_it; service_it++) {
412-
homekit_service_t *service = *service_it;
410+
homekit_service_t *service = *service_it;
413411
service->accessory = accessory;
414412
if (service->id) {
415413
if (service->id >= iid)
416414
iid = service->id+1;
417415
} else {
418416
service->id = iid++;
419417
}
420-
421418
for (homekit_characteristic_t **ch_it = service->characteristics; *ch_it; ch_it++) {
422-
homekit_characteristic_t *ch = *ch_it;
419+
homekit_characteristic_t *ch = *ch_it;
423420
ch->service = service;
424421
if (ch->id) {
425422
if (ch->id >= iid)
@@ -442,7 +439,7 @@ void homekit_accessories_init(homekit_accessory_t **accessories) {
442439
}
443440
}
444441

445-
homekit_accessory_t *homekit_accessory_by_id(homekit_accessory_t **accessories, int aid) {
442+
homekit_accessory_t *homekit_accessory_by_id(homekit_accessory_t **accessories, uint32_t aid) {
446443
for (homekit_accessory_t **accessory_it = accessories; *accessory_it; accessory_it++) {
447444
homekit_accessory_t *accessory = *accessory_it;
448445

@@ -475,7 +472,7 @@ homekit_characteristic_t *homekit_service_characteristic_by_type(homekit_service
475472
return NULL;
476473
}
477474

478-
homekit_characteristic_t *homekit_characteristic_by_aid_and_iid(homekit_accessory_t **accessories, int aid, int iid) {
475+
homekit_characteristic_t *homekit_characteristic_by_aid_and_iid(homekit_accessory_t **accessories, uint32_t aid, uint32_t iid) {
479476
for (homekit_accessory_t **accessory_it = accessories; *accessory_it; accessory_it++) {
480477
homekit_accessory_t *accessory = *accessory_it;
481478

@@ -498,7 +495,7 @@ homekit_characteristic_t *homekit_characteristic_by_aid_and_iid(homekit_accessor
498495
}
499496

500497

501-
homekit_characteristic_t *homekit_characteristic_find_by_type(homekit_accessory_t **accessories, int aid, const char *type) {
498+
homekit_characteristic_t *homekit_characteristic_find_by_type(homekit_accessory_t **accessories, uint32_t aid, const char *type) {
502499
for (homekit_accessory_t **accessory_it = accessories; *accessory_it; accessory_it++) {
503500
homekit_accessory_t *accessory = *accessory_it;
504501

src/arduino_homekit_server.cpp

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ homekit_server_t* server_new() {
8080
server->wifi_server = new WiFiServer(HOMEKIT_SERVER_PORT);
8181
server->wifi_server->begin();
8282
server->wifi_server->setNoDelay(true);
83-
DEBUG("WiFiServer begin at port: %d\n", HOMEKIT_ARDUINO_SERVER_PORT);
83+
DEBUG("WiFiServer begin at port: %d", HOMEKIT_SERVER_PORT);
8484
//FD_ZERO(&server->fds);
8585
//server->max_fd = 0;
8686
server->nfds = 0;
@@ -108,7 +108,8 @@ void server_free(homekit_server_t *server) {
108108
server->wifi_server->close();
109109
delete server->wifi_server;
110110
server->wifi_server = nullptr;
111-
}DEBUG("homekit_server_t delete WiFiServer at port: %d\n", HOMEKIT_ARDUINO_SERVER_PORT);
111+
}
112+
DEBUG("homekit_server_t delete WiFiServer at port: %d\n", HOMEKIT_SERVER_PORT);
112113

113114
if (server == running_server) {
114115
running_server = NULL;
@@ -420,7 +421,7 @@ void write_characteristic_json(json_stream *json, client_context_t *client,
420421
homekit_value_t v = value ? *value : ch->getter_ex ? ch->getter_ex(ch) : ch->value;
421422

422423
if (v.is_null) {
423-
// json_string(json, "value"); json_null(json);
424+
json_string(json, "value"); json_null(json);
424425
} else if (v.format != ch->format) {
425426
ERROR("Characteristic value format is different from characteristic format");
426427
} else {
@@ -1868,7 +1869,7 @@ bool bool_endpoint_param(const char *name, client_context_t *context) {
18681869
return param && param->value && !strcmp(param->value, "1");
18691870
}
18701871

1871-
void write_characteristic_error(json_stream *json, int aid, int iid, int status) {
1872+
void write_characteristic_error(json_stream *json, uint32_t aid, uint32_t iid, int status) {
18721873
json_object_start(json);
18731874
json_string(json, "aid");
18741875
json_uint32(json, aid);
@@ -1922,10 +1923,10 @@ void homekit_server_on_get_characteristics(client_context_t *context) {
19221923
}
19231924

19241925
*dot = 0;
1925-
int aid = atoi(ch_id);
1926-
int iid = atoi(dot + 1);
1926+
uint32_t aid = atoi(ch_id);
1927+
uint32_t iid = atoi(dot + 1);
19271928

1928-
CLIENT_DEBUG(context, "Requested characteristic info for %d.%d", aid, iid);
1929+
CLIENT_DEBUG(context, "Requested characteristic info for %u.%u", aid, iid);
19291930
homekit_characteristic_t *ch = homekit_characteristic_by_aid_and_iid(
19301931
context->server->config->accessories, aid, iid);
19311932
if (!ch) {
@@ -1957,8 +1958,8 @@ void homekit_server_on_get_characteristics(client_context_t *context) {
19571958
while ((ch_id = strsep(&_id, ","))) {
19581959
char *dot = strstr(ch_id, ".");
19591960
*dot = 0;
1960-
int aid = atoi(ch_id);
1961-
int iid = atoi(dot + 1);
1961+
uint32_t aid = atoi(ch_id);
1962+
uint32_t iid = atoi(dot + 1);
19621963

19631964
CLIENT_DEBUG(context, "Requested characteristic info for %d.%d", aid, iid);
19641965
homekit_characteristic_t *ch = homekit_characteristic_by_aid_and_iid(
@@ -2014,14 +2015,14 @@ HAPStatus process_characteristics_update(const cJSON *j_ch, client_context_t *co
20142015
return HAPStatus_NoResource;
20152016
}
20162017

2017-
int aid = j_aid->valueint;
2018-
int iid = j_iid->valueint;
2018+
uint32_t aid = (uint32_t)j_aid->valuedouble;//j_aid->valueint;
2019+
uint32_t iid = (uint32_t)j_iid->valuedouble;//j_iid->valueint;
20192020

20202021
homekit_characteristic_t *ch = homekit_characteristic_by_aid_and_iid(
20212022
context->server->config->accessories, aid, iid);
20222023
if (!ch) {
20232024
CLIENT_ERROR(context,
2024-
"Failed to process request to update %d.%d: " "no such characteristic", aid, iid);
2025+
"Failed to process request to update %u.%u: " "no such characteristic", aid, iid);
20252026
return HAPStatus_NoResource;
20262027
}
20272028

@@ -2174,8 +2175,7 @@ HAPStatus process_characteristics_update(const cJSON *j_ch, client_context_t *co
21742175

21752176
default:
21762177
CLIENT_ERROR(context, "Unexpected format when updating numeric value: %d",
2177-
ch->format)
2178-
;
2178+
ch->format);
21792179
return HAPStatus_InvalidValue;
21802180
}
21812181

@@ -2424,9 +2424,9 @@ void homekit_server_on_update_characteristics(client_context_t *context, const b
24242424

24252425
json_object_start(json1);
24262426
json_string(json1, "aid");
2427-
json_uint32(json1, cJSON_GetObjectItem(j_ch, "aid")->valueint);
2427+
json_uint32(json1, cJSON_GetObjectItem(j_ch, "aid")->valuedouble);//->valueint);
24282428
json_string(json1, "iid");
2429-
json_uint32(json1, cJSON_GetObjectItem(j_ch, "iid")->valueint);
2429+
json_uint32(json1, cJSON_GetObjectItem(j_ch, "iid")->valuedouble);//->valueint);
24302430
json_string(json1, "status");
24312431
json_uint8(json1, statuses[i]);
24322432
json_object_end(json1);
@@ -2897,7 +2897,7 @@ void homekit_client_process(client_context_t *context) {
28972897
}
28982898
return;
28992899
}
2900-
CLIENT_DEBUG(context, "Got %d incomming data, encrypted is %",
2900+
CLIENT_DEBUG(context, "Got %d incomming data, encrypted is %s",
29012901
data_len, context->encrypted ? "true" : "false");
29022902
byte *payload = (byte*) context->data;
29032903
size_t payload_size = (size_t) data_len;
@@ -3164,7 +3164,7 @@ void homekit_mdns_init(homekit_server_t *server) {
31643164
if (homekit_mdns_started) {
31653165
MDNS.close();
31663166
MDNS.begin(name->value.string_value, staIP);
3167-
INFO("MDNS.restart: %s, IP: %s", name->value.string_value, staIP.toString().c_str());
3167+
INFO("MDNS restart: %s, IP: %s", name->value.string_value, staIP.toString().c_str());
31683168
MDNS.announce();
31693169
return;
31703170
}
@@ -3173,7 +3173,7 @@ void homekit_mdns_init(homekit_server_t *server) {
31733173
WiFi.hostname(name->value.string_value);
31743174
// Must specify the MDNS runs on the IP of STA
31753175
MDNS.begin(name->value.string_value, staIP);
3176-
INFO("MDNS.begin: %s, IP: %s", name->value.string_value, staIP.toString().c_str());
3176+
INFO("MDNS begin: %s, IP: %s", name->value.string_value, staIP.toString().c_str());
31773177

31783178
MDNSResponder::hMDNSService mdns_service = MDNS.addService(name->value.string_value,
31793179
HOMEKIT_MDNS_SERVICE, HOMEKIT_MDNS_PROTO, HOMEKIT_SERVER_PORT);
@@ -3185,14 +3185,22 @@ void homekit_mdns_init(homekit_server_t *server) {
31853185
if (running_server) {
31863186
MDNS.addDynamicServiceTxt(p_hService, "sf",
31873187
(running_server->paired) ? "0" : "1");
3188+
MDNS.addDynamicServiceTxt(p_hService, "c#",
3189+
running_server->config->config_number);
31883190
}
31893191

31903192
}
31913193
);
31923194
MDNS.addServiceTxt(mdns_service, "md", model->value.string_value);
31933195
MDNS.addServiceTxt(mdns_service, "pv", "1.0");
31943196
MDNS.addServiceTxt(mdns_service, "id", server->accessory_id);
3195-
MDNS.addServiceTxt(mdns_service, "c#", String(server->config->config_number).c_str());
3197+
//"c#" is a DynamicServiceTxt
3198+
//Current configuration number. Required.
3199+
//Must update when an accessory, service, or characteristic is added or removed on the accessory server.
3200+
//Accessories must increment the config number after a firmware update.
3201+
//This must have a range of 1-65535 and wrap to 1 when it overflows.
3202+
//This value must persist across reboots, power cycles, etc.
3203+
//MDNS.addServiceTxt(mdns_service, "c#", String(server->config->config_number).c_str());
31963204
MDNS.addServiceTxt(mdns_service, "s#", "1");
31973205
MDNS.addServiceTxt(mdns_service, "ff", "0");
31983206
//"sf" is a DynamicServiceTxt
@@ -3251,6 +3259,25 @@ void homekit_mdns_init(homekit_server_t *server) {
32513259
// " and \"I Don't Have a Code\". \nThis Accessory will show on your iOS device.");
32523260
}
32533261

3262+
// Used to update the config_number ("c#" value of Bonjour)
3263+
// Call this function when an accessory, service, or characteristic is added or removed on the accessory server.
3264+
// See the official HAP specification for more information.
3265+
void homekit_update_config_number() {
3266+
if(!homekit_mdns_started) {
3267+
return ;
3268+
}
3269+
if(!running_server){
3270+
return;
3271+
}
3272+
// range of 1-65535
3273+
uint16_t c = running_server->config->config_number;
3274+
c = (c > 0 && c < 65535) ? (c + 1) : 1;
3275+
running_server->config->config_number = c;
3276+
MDNS.announce();
3277+
MDNS.update();
3278+
INFO("Update config_number to %u", c);
3279+
}
3280+
32543281
int homekit_accessory_id_generate(char *accessory_id) {
32553282
byte buf[6];
32563283
homekit_random_fill(buf, sizeof(buf));
@@ -3308,18 +3335,15 @@ void homekit_server_init(homekit_server_config_t *config) {
33083335
}
33093336

33103337
homekit_accessories_init(config->accessories);
3311-
33123338
if (!config->config_number) {
33133339
config->config_number = config->accessories[0]->config_number;
33143340
if (!config->config_number) {
33153341
config->config_number = 1;
33163342
}
33173343
}
3318-
33193344
if (!config->category) {
33203345
config->category = config->accessories[0]->category;
33213346
}
3322-
33233347
homekit_server_t *server = server_new();
33243348
running_server = server;
33253349
server->config = config;
@@ -3559,7 +3583,9 @@ void arduino_homekit_setup(homekit_server_config_t *config) {
35593583
}
35603584

35613585
void arduino_homekit_loop() {
3562-
MDNS.update();
3586+
if (homekit_mdns_started) {
3587+
MDNS.update();
3588+
}
35633589
if (running_server != nullptr) {
35643590
if (!running_server->paired) {
35653591
//If not paired or pairing was removed, preinit paring context.

src/arduino_homekit_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ void arduino_homekit_loop();
230230

231231
homekit_server_t * arduino_homekit_get_running_server();
232232
int arduino_homekit_connected_clients_count();
233+
void homekit_update_config_number();
233234

234235
#ifdef __cplusplus
235236
}

src/homekit/characteristics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
extern "C" {
88
#endif
99

10+
#define HOMEKIT_SHORT_APPLE_UUIDS
11+
1012
// MARK: - Apple UUID
1113

1214
#ifdef HOMEKIT_SHORT_APPLE_UUIDS

src/homekit/homekit.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ typedef struct {
2929

3030
homekit_accessory_category_t category;
3131

32-
int config_number;
32+
// Used for Bonjour
33+
// Current configuration number. Required.
34+
// Must update when an accessory, service, or characteristic is added or removed on the accessory server.
35+
// Accessories must increment the config number after a firmware update.
36+
// This must have a range of 1-65535 and wrap to 1 when it overflows.
37+
// This value must persist across reboots, power cycles, etc.
38+
uint16_t config_number;
3339

3440
// Password in format "111-23-456".
3541
// If password is not specified, a random password

src/homekit/types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ struct _homekit_accessory {
233233
unsigned int id;
234234

235235
homekit_accessory_category_t category;
236-
int config_number;
236+
uint16_t config_number;
237237

238238
homekit_service_t **services;
239239
};
@@ -324,13 +324,13 @@ homekit_characteristic_t *homekit_characteristic_clone(homekit_characteristic_t
324324
void homekit_accessories_init(homekit_accessory_t **accessories);
325325

326326
// Find accessory by ID. Returns NULL if not found
327-
homekit_accessory_t *homekit_accessory_by_id(homekit_accessory_t **accessories, int aid);
327+
homekit_accessory_t *homekit_accessory_by_id(homekit_accessory_t **accessories, uint32_t aid);
328328
// Find service inside accessory by service type. Returns NULL if not found
329329
homekit_service_t *homekit_service_by_type(homekit_accessory_t *accessory, const char *type);
330330
// Find characteristic inside service by type. Returns NULL if not found
331331
homekit_characteristic_t *homekit_service_characteristic_by_type(homekit_service_t *service, const char *type);
332332
// Find characteristic by accessory ID and characteristic ID. Returns NULL if not found
333-
homekit_characteristic_t *homekit_characteristic_by_aid_and_iid(homekit_accessory_t **accessories, int aid, int iid);
333+
homekit_characteristic_t *homekit_characteristic_by_aid_and_iid(homekit_accessory_t **accessories, uint32_t aid, uint32_t iid);
334334

335335
void homekit_characteristic_notify(homekit_characteristic_t *ch, const homekit_value_t value);
336336
void homekit_characteristic_add_notify_callback(

0 commit comments

Comments
 (0)