I'm currently modifying the light switch server example so that LED 1 and LED 2 can be controlled by buttons on a client board. For example, Button 1 will turn on or off LED 1, and Button 2 will turn on or off LED 2. I'm following this example to add a server model for LED 2, and I have done the following:
- Increased ACCESS_MODEL_COUNT and ACCESS_ELEMENT COUNT by 1
/** * The number of models in the application. * * @note To fit the configuration and health models, this value must equal at least * the number of models needed by the application plus two. */ #define ACCESS_MODEL_COUNT (/* Element 0: */ \ 1 + /* Config Server */ \ 1 + /* Health Server */ \ 2 + /* Generic OnOff Server */ \ 1 + /* Default Transition Time Server */ \ 1 + /* Scene Server */ \ 1 /* Scene Setup Server (extends Scene Server) */) // added 1 generic on off server /** * The number of elements in the application. * * @warning If the application is to support _multiple instances_ of the _same_ model, these instances * cannot be in the same element and a separate element is needed for each new instance of the same model. */ #define ACCESS_ELEMENT_COUNT (2) // added +1
- Added another app_onoff_server_get_cb and app_onoff_server_set_cb
/* Callback for updating the hardware state of LED0*/ static void app_onoff_server_set_cb_0(const app_onoff_server_t * p_server, bool onoff) { /* Resolve the server instance here if required, this example uses only 1 instance. */ __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Setting GPIO value: %d\n", onoff) hal_led_pin_set(ONOFF_SERVER_0_LED, onoff); } /* Callback for updating the hardware state of LED1*/ static void app_onoff_server_set_cb_1(const app_onoff_server_t * p_server, bool onoff) { /* Resolve the server instance here if required, this example uses only 1 instance. */ __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Setting GPIO value: %d\n", onoff) hal_led_pin_set(ONOFF_SERVER_1_LED, onoff); } /* Callback for reading the hardware state of LED0 */ static void app_onoff_server_get_cb_0(const app_onoff_server_t * p_server, bool * p_present_onoff) { /* Resolve the server instance here if required, this example uses only 1 instance. */ *p_present_onoff = hal_led_pin_get(ONOFF_SERVER_0_LED); } /* Callback for reading the hardware state of LED1 */ static void app_onoff_server_get_cb_1(const app_onoff_server_t * p_server, bool * p_present_onoff) { /* Resolve the server instance here if required, this example uses only 1 instance. */ *p_present_onoff = hal_led_pin_get(ONOFF_SERVER_1_LED); }
- Added another APP_ONOFF_SERVER_DEF
/* Generic OnOff server structure definition and initialization */ APP_ONOFF_SERVER_DEF(m_onoff_server_0, APP_FORCE_SEGMENTATION, APP_MIC_SIZE, app_onoff_server_set_cb_0, app_onoff_server_get_cb_0, app_onoff_server_transition_cb) APP_ONOFF_SERVER_DEF(m_onoff_server_1, APP_FORCE_SEGMENTATION, APP_MIC_SIZE, app_onoff_server_set_cb_1, app_onoff_server_get_cb_1, app_onoff_server_transition_cb) // added
- And then initialized the new server in app_model_init
static void app_model_init(void) { /* Instantiate onoff server on element index APP_ONOFF_ELEMENT_INDEX */ ERROR_CHECK(app_onoff_init(&m_onoff_server_0, APP_ONOFF_ELEMENT_INDEX)); __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "App OnOff Model Handle: %d\n", m_onoff_server_0.server.model_handle); ERROR_CHECK(app_onoff_init(&m_onoff_server_1, APP_ONOFF_ELEMENT_INDEX+1)); // added to initialize 2nd server __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "App OnOff Model Handle: %d\n", m_onoff_server_1.server.model_handle); #if SCENE_SETUP_SERVER_INSTANCES_MAX > 0 /* Instantiate Generic Default Transition Time server as needed by Scene models */ ERROR_CHECK(app_dtt_init(&m_dtt_server_0, APP_ONOFF_ELEMENT_INDEX)); __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "App DTT Model Handle: %d\n", m_dtt_server_0.server.model_handle); /* Instantiate scene server and register onoff server to have scene support */ ERROR_CHECK(app_scene_model_init(&m_scene_server_0, APP_ONOFF_ELEMENT_INDEX)); ERROR_CHECK(app_scene_model_add(&m_scene_server_0, &m_onoff_server_0.scene_if)); ERROR_CHECK(app_onoff_scene_context_set(&m_onoff_server_0, &m_scene_server_0)); __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "App Scene Model Handle: %d\n", m_scene_server_0.scene_setup_server.model_handle); #endif }
Building the program results in no problem. However, when I flashed it into the nRF52840 DK, all 4 LEDs lit up and this is the message in Debug Terminal:
<t: 0>, main.c, 400, ----- BLE Mesh Light Switch Server Demo ----- <t: 12926>, main.c, 356, Initializing and adding models <t: 12929>, main.c, 208, App OnOff Model Handle: 2 <t: 12933>, app_error_weak.c, 115, Mesh error 19 at 0x00027901
From nrf_error.h, mesh error 19 is "Not enough resources for operation." I'm assuming that the error happened because of the second app_onoff_init, but I am not sure. Any advice on how to fix this error? Thank you.