-1

I am developing a homekit device using ESP8266 and I am unable to programmatically generate services. I need to generate 16 of them. Here is the code I attempted to assign to *service:

homekit_service_t *services[18] = {
  &(homekit_service_t){
    .type = HOMEKIT_SERVICE_ACCESSORY_INFORMATION,
    .characteristics = (homekit_characteristic_t *[]){
      HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
      HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"),
      HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"),
      HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"),
      HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
      HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
      NULL,
    },
  },
  &(homekit_service_t){
    .type = HOMEKIT_SERVICE_SWITCH,
    .characteristics = (homekit_characteristic_t *[]){
      HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
      &cha_switch_on,
      NULL,
    },
  },
  NULL,
};

However, when I try to generate additional services, using the code below:

services[3] = &(homekit_service_t){
  .type = HOMEKIT_SERVICE_SWITCH,
  .characteristics = (homekit_characteristic_t *[]){
    HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
    &cha_switch_on,
    NULL,
  },
};

The compiler throws this error:

C:\Users\black\OneDrive\Desktop\homekit\my_accessory.c:34:1: error: conflicting types for 'services'
   34 | services[3] = &(homekit_service_t){
      | ^~~~~~~~
C:\Users\black\OneDrive\Desktop\homekit\my_accessory.c:10:20: note: previous definition of 'services' was here
   10 | homekit_service_t *services[18] = {
      |                    ^~~~~~~~
C:\Users\black\OneDrive\Desktop\homekit\my_accessory.c:34:15: error: invalid initializer
   34 | services[3] = &(homekit_service_t){

How can I programmatically generate additional services?

Barmar
  • 741,623
  • 53
  • 500
  • 612
BlackLotus
  • 496
  • 2
  • 9
  • 24
  • 1
    Even if you solve the syntax problem, note that the lifetime of a compound literal is the block where it's used. So the pointer will become invalid when you leave that block. Use `malloc()` if you need it to last longer. – Barmar Jun 17 '23 at 17:02
  • 1
    it seems to think that the assignment to `services[3]` is a declaration. What's on the line before it? – Barmar Jun 17 '23 at 17:04
  • @Barmar the first code block is the previous line – BlackLotus Jun 17 '23 at 17:19
  • 2
    It seems that `services[3] = &...` is outside any function block. Outside fonction only variable/function/type definition is possible, you cannot update something. – dalfaB Jun 17 '23 at 17:24
  • @dalfaB it actually the example code from [esp8266-homekit](https://github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266/blob/master/examples/Example02_Switch/my_accessory.c), do you have any suggestion to make able to add using for loop? – BlackLotus Jun 17 '23 at 17:26
  • 1
    I don't see an assignment to `services[3]` in that example code. – Barmar Jun 17 '23 at 17:37
  • @Barmar that's what I want to add, as described, I want to add 16 services, without using the copy and paste. – BlackLotus Jun 17 '23 at 17:39
  • 1
    Either do it in the declaration of the array, as in your first code block, or assign to an array element in a function. You can't assign to array elements at top-level. – Barmar Jun 17 '23 at 17:45

1 Answers1

3

You can't put executable statements like array element assignment outside a function. If you want to do this at top-level, you have to do it as part of the array declaration.

homekit_service_t *services[18] = {
  &(homekit_service_t){
    .type = HOMEKIT_SERVICE_ACCESSORY_INFORMATION,
    .characteristics = (homekit_characteristic_t *[]){
      HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
      HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"),
      HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"),
      HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"),
      HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
      HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
      NULL,
    },
  },
  &(homekit_service_t){
    .type = HOMEKIT_SERVICE_SWITCH,
    .characteristics = (homekit_characteristic_t *[]){
      HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
      &cha_switch_on,
      NULL,
    },
  },
  NULL,
  &(homekit_service_t){
    .type = HOMEKIT_SERVICE_SWITCH,
    .characteristics = (homekit_characteristic_t *[]){
      HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
      &cha_switch_on,
      NULL,
    },
  },
};
Barmar
  • 741,623
  • 53
  • 500
  • 612