CoAP URI management for button device.
More...
CoAP URI management for button device.
Overview
This module manages CoAP URI endpoints for the button device, enabling remote control and status queries over the OpenThread network.
The button device exposes a single CoAP endpoint:
- btn/state: Query button state and receive button press notifications
URI Architecture
The URI system uses a callback-based architecture:
- URI Registration: URI is stored in static array ad_button_uri[] (ad_btn_uri.c)
- Driver Callback: ad_btn_uri_getList_clb() is registered with the device driver
- Driver Access: The framework calls the callback to retrieve registered URI
- Request Handling: CoAP requests to the URI invoke the handler
URI Request Flow
↓
Find Matching URI ("btn/state")
↓
↓
processUriRequest() → Parse Message
↓
Process & Respond
void ad_button_uri_btn_state_CoreHandle(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
CoAP handler for "btn/state" URI.
Definition ad_btn_uri.c:75
otapp_coap_uri_t * ad_btn_uri_getList_clb(void)
Driver callback to get list of registered CoAP URI handlers.
Definition ad_btn_uri.c:178
Button State Endpoint
URI Path: btn/state
Type: OTAPP_SWITCH
Supported Operations:
- Observer Mode: Subscribe to button state changes (CoAP Observe)
- Direct Request: Query current button state (CoAP GET)
Use Cases:
- Remote devices can subscribe to receive notifications when button is pressed
- Other devices can query current button state
- Button device can push state updates to subscribed observers
Adding Custom URI Handlers
To add additional URI endpoints:
Step 1: Define URI handler function with CoAP handler signature:
{
int8_t result = 0;
result = drv->api.coap.processUriRequest(aMessage, aMessageInfo,
OTAPP_CUSTOM_TYPE,
coapBuffer,
printf("Custom URI payload: %s\n", (char*)coapBuffer);
} else {
printf("Custom URI error: %d\n", result);
}
}
#define OAC_URI_OBS_BUFFER_SIZE
Definition ot_app_coap_uri_obs.h:43
#define OTAPP_COAP_OK
Definition ot_app_coap.h:45
Step 2: Add URI to the array in ad_btn_uri.c:
{OTAPP_CUSTOM_TYPE, {
"btn/config", my_custom_uri_handler,
NULL,
NULL},},
};
@ OTAPP_SWITCH
Physical button or switch.
Definition ot_app.h:83
#define NULL
Definition hro_utils.h:73
Definition ot_app_coap.h:66
Step 3: Size is automatically calculated by AD_BUTTON_URI_SIZE macro:
#define AD_BUTTON_URI_SIZE (sizeof(ad_button_uri) / sizeof(ad_button_uri[0]))
URI Constraints
Usage Example
Initialization
void ad_btn_uri_init(ot_app_devDrv_t *devDrv)
Initialize URI handlers for button device.
Definition ad_btn_uri.c:190
Definition ot_app_drv.h:242
Querying Registered URI
printf("Registered URI: %s (count: %d)\n", uri[0].coap_uri.mUriPath, count);
uint8_t ad_btn_uri_getListSize(void)
Get number of registered URI handlers.
Definition ad_btn_uri.c:184
- See also
- ot_app_drv.h for otapp_coap_uri_t structure definition
-
ad_button.h for device initialization
- Version
- 0.1
- Date
- 12-11-2025
- Author
- Jan Łukaszewicz (plhar.nosp@m.eo@g.nosp@m.mail..nosp@m.com)
- Copyright
- © 2025 MIT LICENCE
◆ ad_btn_uri_getList_clb()
Driver callback to get list of registered CoAP URI handlers.
This function is a callback registered with the device driver during initialization. The driver framework calls this function to retrieve the button device's CoAP URI and its associated handler.
Callback Flow:
- ad_btn_uri_init() registers this callback with driver
- Driver stores callback pointer: driver->uriGetList_clb = ad_btn_uri_getList_clb
- When needed, driver invokes: otapp_coap_uri_t *list = driver->uriGetList_clb()
- Driver uses returned URI to route CoAP "btn/state" requests to the handler
- Returns
- Pointer to static array of otapp_coap_uri_t structures
- Return values
-
| ad_button_uri | Array containing single URI: "btn/state" |
| NULL | Can be returned if URI list is empty (currently not implemented) |
- Note
- This function returns a pointer to a static internal array valid for application lifetime
-
The returned array contains exactly 1 element (AD_BUTTON_URI_SIZE = 1)
-
This is a callback function - typically called by driver, not directly by user code
- Warning
- Do not modify the returned array - it is read-only
- See also
- ad_btn_uri_getListSize() to get number of URIs in the returned array (returns 1)
-
ad_btn_uri_init() for callback registration
◆ ad_btn_uri_getListSize()
| uint8_t ad_btn_uri_getListSize |
( |
void | | ) |
|
Get number of registered URI handlers.
Returns the count of URI endpoints registered in the internal URI list. This corresponds to the size of the array returned by ad_btn_uri_getList_clb().
- Returns
- Number of URIs in the list
- Return values
-
| 1 | Current implementation registers single URI: "btn/state" |
| 0 | Can be returned if no URIs registered (currently not implemented) |
- Note
- Current implementation returns: AD_BUTTON_URI_SIZE = 1
- See also
- ad_btn_uri_getList_clb()
◆ ad_btn_uri_init()
Initialize URI handlers for button device.
- Parameters
-
| [in] | devDrv | Pointer to device driver structure |
This function performs the following initialization steps:
- Validates device driver pointer
- Stores driver reference in static variable drv for use by URI handler
- Registers callback ad_btn_uri_getList_clb() with the driver
After initialization, the driver can:
- Call ad_btn_uri_getList_clb() to retrieve the "btn/state" URI
- Route incoming CoAP "btn/state" requests to the registered handler
- Enable remote devices to query button state or subscribe to button events
Typical initialization sequence:
- Note
- Must be called after device driver structure allocation
-
Must be called before ad_button_Init()
-
Calling with NULL pointer has no effect (safe, but initialization will fail)
- Warning
- Do not call this function multiple times with different drivers
- See also
- ad_button_Init() for complete device initialization
-
ad_btn_uri_getList_clb() for registered callback function