@@ -80,7 +80,7 @@ homekit_server_t* server_new() {
80
80
server->wifi_server = new WiFiServer (HOMEKIT_SERVER_PORT);
81
81
server->wifi_server ->begin ();
82
82
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 );
84
84
// FD_ZERO(&server->fds);
85
85
// server->max_fd = 0;
86
86
server->nfds = 0 ;
@@ -108,7 +108,8 @@ void server_free(homekit_server_t *server) {
108
108
server->wifi_server ->close ();
109
109
delete server->wifi_server ;
110
110
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);
112
113
113
114
if (server == running_server) {
114
115
running_server = NULL ;
@@ -420,7 +421,7 @@ void write_characteristic_json(json_stream *json, client_context_t *client,
420
421
homekit_value_t v = value ? *value : ch->getter_ex ? ch->getter_ex (ch) : ch->value ;
421
422
422
423
if (v.is_null ) {
423
- // json_string(json, "value"); json_null(json);
424
+ json_string (json, " value" ); json_null (json);
424
425
} else if (v.format != ch->format ) {
425
426
ERROR (" Characteristic value format is different from characteristic format" );
426
427
} else {
@@ -1868,7 +1869,7 @@ bool bool_endpoint_param(const char *name, client_context_t *context) {
1868
1869
return param && param->value && !strcmp (param->value , " 1" );
1869
1870
}
1870
1871
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) {
1872
1873
json_object_start (json);
1873
1874
json_string (json, " aid" );
1874
1875
json_uint32 (json, aid);
@@ -1922,10 +1923,10 @@ void homekit_server_on_get_characteristics(client_context_t *context) {
1922
1923
}
1923
1924
1924
1925
*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 );
1927
1928
1928
- CLIENT_DEBUG (context, " Requested characteristic info for %d.%d " , aid, iid);
1929
+ CLIENT_DEBUG (context, " Requested characteristic info for %u.%u " , aid, iid);
1929
1930
homekit_characteristic_t *ch = homekit_characteristic_by_aid_and_iid (
1930
1931
context->server ->config ->accessories , aid, iid);
1931
1932
if (!ch) {
@@ -1957,8 +1958,8 @@ void homekit_server_on_get_characteristics(client_context_t *context) {
1957
1958
while ((ch_id = strsep (&_id, " ," ))) {
1958
1959
char *dot = strstr (ch_id, " ." );
1959
1960
*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 );
1962
1963
1963
1964
CLIENT_DEBUG (context, " Requested characteristic info for %d.%d" , aid, iid);
1964
1965
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
2014
2015
return HAPStatus_NoResource;
2015
2016
}
2016
2017
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;
2019
2020
2020
2021
homekit_characteristic_t *ch = homekit_characteristic_by_aid_and_iid (
2021
2022
context->server ->config ->accessories , aid, iid);
2022
2023
if (!ch) {
2023
2024
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);
2025
2026
return HAPStatus_NoResource;
2026
2027
}
2027
2028
@@ -2174,8 +2175,7 @@ HAPStatus process_characteristics_update(const cJSON *j_ch, client_context_t *co
2174
2175
2175
2176
default :
2176
2177
CLIENT_ERROR (context, " Unexpected format when updating numeric value: %d" ,
2177
- ch->format )
2178
- ;
2178
+ ch->format );
2179
2179
return HAPStatus_InvalidValue;
2180
2180
}
2181
2181
@@ -2424,9 +2424,9 @@ void homekit_server_on_update_characteristics(client_context_t *context, const b
2424
2424
2425
2425
json_object_start (json1);
2426
2426
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);
2428
2428
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);
2430
2430
json_string (json1, " status" );
2431
2431
json_uint8 (json1, statuses[i]);
2432
2432
json_object_end (json1);
@@ -2897,7 +2897,7 @@ void homekit_client_process(client_context_t *context) {
2897
2897
}
2898
2898
return ;
2899
2899
}
2900
- CLIENT_DEBUG (context, " Got %d incomming data, encrypted is %" ,
2900
+ CLIENT_DEBUG (context, " Got %d incomming data, encrypted is %s " ,
2901
2901
data_len, context->encrypted ? " true" : " false" );
2902
2902
byte *payload = (byte*) context->data ;
2903
2903
size_t payload_size = (size_t ) data_len;
@@ -3164,7 +3164,7 @@ void homekit_mdns_init(homekit_server_t *server) {
3164
3164
if (homekit_mdns_started) {
3165
3165
MDNS.close ();
3166
3166
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 ());
3168
3168
MDNS.announce ();
3169
3169
return ;
3170
3170
}
@@ -3173,7 +3173,7 @@ void homekit_mdns_init(homekit_server_t *server) {
3173
3173
WiFi.hostname (name->value .string_value );
3174
3174
// Must specify the MDNS runs on the IP of STA
3175
3175
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 ());
3177
3177
3178
3178
MDNSResponder::hMDNSService mdns_service = MDNS.addService (name->value .string_value ,
3179
3179
HOMEKIT_MDNS_SERVICE, HOMEKIT_MDNS_PROTO, HOMEKIT_SERVER_PORT);
@@ -3185,14 +3185,22 @@ void homekit_mdns_init(homekit_server_t *server) {
3185
3185
if (running_server) {
3186
3186
MDNS.addDynamicServiceTxt (p_hService, " sf" ,
3187
3187
(running_server->paired ) ? " 0" : " 1" );
3188
+ MDNS.addDynamicServiceTxt (p_hService, " c#" ,
3189
+ running_server->config ->config_number );
3188
3190
}
3189
3191
3190
3192
}
3191
3193
);
3192
3194
MDNS.addServiceTxt (mdns_service, " md" , model->value .string_value );
3193
3195
MDNS.addServiceTxt (mdns_service, " pv" , " 1.0" );
3194
3196
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());
3196
3204
MDNS.addServiceTxt (mdns_service, " s#" , " 1" );
3197
3205
MDNS.addServiceTxt (mdns_service, " ff" , " 0" );
3198
3206
// "sf" is a DynamicServiceTxt
@@ -3251,6 +3259,25 @@ void homekit_mdns_init(homekit_server_t *server) {
3251
3259
// " and \"I Don't Have a Code\". \nThis Accessory will show on your iOS device.");
3252
3260
}
3253
3261
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
+
3254
3281
int homekit_accessory_id_generate (char *accessory_id) {
3255
3282
byte buf[6 ];
3256
3283
homekit_random_fill (buf, sizeof (buf));
@@ -3308,18 +3335,15 @@ void homekit_server_init(homekit_server_config_t *config) {
3308
3335
}
3309
3336
3310
3337
homekit_accessories_init (config->accessories );
3311
-
3312
3338
if (!config->config_number ) {
3313
3339
config->config_number = config->accessories [0 ]->config_number ;
3314
3340
if (!config->config_number ) {
3315
3341
config->config_number = 1 ;
3316
3342
}
3317
3343
}
3318
-
3319
3344
if (!config->category ) {
3320
3345
config->category = config->accessories [0 ]->category ;
3321
3346
}
3322
-
3323
3347
homekit_server_t *server = server_new ();
3324
3348
running_server = server;
3325
3349
server->config = config;
@@ -3559,7 +3583,9 @@ void arduino_homekit_setup(homekit_server_config_t *config) {
3559
3583
}
3560
3584
3561
3585
void arduino_homekit_loop () {
3562
- MDNS.update ();
3586
+ if (homekit_mdns_started) {
3587
+ MDNS.update ();
3588
+ }
3563
3589
if (running_server != nullptr ) {
3564
3590
if (!running_server->paired ) {
3565
3591
// If not paired or pairing was removed, preinit paring context.
0 commit comments