OpenThread_app
Loading...
Searching...
No Matches
Button Device URI Management

CoAP URI management for button device. More...

Functions

otapp_coap_uri_tad_btn_uri_getList_clb (void)
 Driver callback to get list of registered CoAP URI handlers.
uint8_t ad_btn_uri_getListSize (void)
 Get number of registered URI handlers.
void ad_btn_uri_init (ot_app_devDrv_t *devDrv)
 Initialize URI handlers for button device.

Detailed Description

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:

  1. URI Registration: URI is stored in static array ad_button_uri[] (ad_btn_uri.c)
  2. Driver Callback: ad_btn_uri_getList_clb() is registered with the device driver
  3. Driver Access: The framework calls the callback to retrieve registered URI
  4. Request Handling: CoAP requests to the URI invoke the handler

URI Request Flow

CoAP Request → Driver → ad_btn_uri_getList_clb() → URI List
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:

void my_custom_uri_handler(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
{
int8_t result = 0;
result = drv->api.coap.processUriRequest(aMessage, aMessageInfo,
OTAPP_CUSTOM_TYPE,
coapBuffer,
if(result == OTAPP_COAP_OK) {
// Handle URI request here
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
Definition mock_ip6.h:93
Definition mock_ip6.h:96

Step 2: Add URI to the array in ad_btn_uri.c:

static otapp_coap_uri_t ad_button_uri[] = {
// Add your custom URI here
{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

// Initialize device driver
ot_app_devDrv_t *driver = get_device_driver();
// Initialize URI handler - registers callback with driver
// Driver will call ad_btn_uri_getList_clb() to retrieve URI list
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

// Get registered URI via driver callback
uint8_t count = ad_btn_uri_getListSize();
printf("Registered URI: %s (count: %d)\n", uri[0].coap_uri.mUriPath, count);
// Output: Registered URI: btn/state (count: 1)
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)

Function Documentation

◆ ad_btn_uri_getList_clb()

otapp_coap_uri_t * ad_btn_uri_getList_clb ( void )

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:

  1. ad_btn_uri_init() registers this callback with driver
  2. Driver stores callback pointer: driver->uriGetList_clb = ad_btn_uri_getList_clb
  3. When needed, driver invokes: otapp_coap_uri_t *list = driver->uriGetList_clb()
  4. 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_uriArray containing single URI: "btn/state"
NULLCan 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
1Current implementation registers single URI: "btn/state"
0Can 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()

void ad_btn_uri_init ( ot_app_devDrv_t * devDrv)

Initialize URI handlers for button device.

Parameters
[in]devDrvPointer to device driver structure

This function performs the following initialization steps:

  1. Validates device driver pointer
  2. Stores driver reference in static variable drv for use by URI handler
  3. 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:

// ... initialize driver structure ...
ad_btn_uri_init(&driver); // Register URI callback
ad_button_Init(); // Complete device initialization
void ad_button_Init(char *deviceNameGroup)
Initialize button device driver with custom device name group.
Definition ad_button.c:284
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