Discover ONVIF Devices Programmatically

A step-by-step guide using the Happytimesoft ONVIF Client Library API in C.

Introduction

This tutorial demonstrates how to use the Happytimesoft ONVIF Client Library to automatically discover ONVIF-compliant devices on your network. The discovery process uses the WS-Discovery protocol.

Below is a complete code example showing the initialization, callback setup, and probe thread start.

Step 1: Initialize System and Set Callback

Before starting the discovery, initialize the library's internal buffers and set the callback function that will be triggered when a device is found.

C Code: Device Discovery Setup
// Open log file for debugging
log_init("log.txt"); 
log_set_level(LOG_DBG);  

// Initialize system buffer
sys_buf_init(10 * MAX_DEV_NUMS);

// Initialize HTTP message buffer
http_msg_buf_init(10 * MAX_DEV_NUMS);  

// Set the callback function for discovered devices
set_probe_cb(probeCallback, 0); 

Step 2: Start the Discovery Process

Start the probe thread. The library will send out discovery packets and invoke your callback for each device found.

C Code: Start Device Probe
// Start the probe thread
start_probe(NULL, 30); 

// Parameters:
//   - First parameter: IP address for discovery (NULL = default interface)
//   - Second parameter: Interval (seconds) between probe packets

// The probeCallback function signature:
void probeCallback(DEVICE_BINFO * p_res, void * p_data);

// - p_res: Contains the discovered device's basic information
// - p_data: User-defined data passed during set_probe_cb

Key Notes

  • Passing NULL as the first parameter uses the default network interface.
  • The interval of 30 seconds is a common setting, but can be adjusted based on your needs.
  • Always handle the data in the probeCallback function appropriately.