Manually Add an ONVIF Device via C API

A comprehensive guide to configuring and connecting to an ONVIF device using the Happytimesoft Client Library.

Introduction

This tutorial demonstrates how to programmatically connect to a specific ONVIF device by manually setting its network address and credentials. This method is useful when automatic discovery is not feasible or you need direct control over the connection parameters.

Step 1: Initialize the Library

Before defining the device, initialize the logging and internal buffers of the ONVIF client library.

C Code: Initialization
// Define an ONVIF_DEVICE variable
ONVIF_DEVICE g_device; 

// Open log file for debugging
log_init("log.txt"); 
log_set_level(LOG_DBG);  

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

// Init http message buffer
http_msg_buf_init(10 * MAX_DEV_NUMS);  

// Initialize the device structure
memset(&g_device, 0, sizeof(g_device));

Step 2: Configure Device Address

Set the IP address, port, and ONVIF service URL of the target device. Adjust these values according to your camera's configuration.

C Code: Set Device Address
// Set HTTPS flag (1 for HTTPS, 0 for HTTP)
g_device.binfo.XAddr.https = 0; 

// Specify the ONVIF service port (commonly 80, 8000, or 8899)
g_device.binfo.XAddr.port = 8000; 

// Specify the IP address of the device
strcpy(g_device.binfo.XAddr.host, "192.168.1.3"); 

// Specify the ONVIF device service endpoint
strcpy(g_device.binfo.XAddr.url, "/onvif/device_service");

Step 3: Set Authentication

Provide the username and password required to authenticate with the ONVIF device. Choose the appropriate authentication method.

C Code: Set Credentials
// Set device login information
onvif_SetAuthInfo(&g_device, "admin", "admin");

// Set authentication method (UsernameToken recommended)
onvif_SetAuthMethod(&g_device, AuthMethod_UsernameToken);

// Optional: Set request timeout in milliseconds
onvif_SetReqTimeout(&g_device, 5000);

Step 4: Query Device Capabilities

After configuration, call core ONVIF services to verify the connection and retrieve device information.

C Code: Test Connection & Get Info
// Next you can call other API interfaces
if (!GetSystemDateAndTime(&g_device))
{   
    printf("%s, GetSystemDateAndTime failed\r\n", g_device.binfo.XAddr.host);
}  

if (!GetCapabilities(&g_device))
{   
    printf("%s, GetCapabilities failed\r\n", g_device.binfo.XAddr.host);
} 

if (!GetServices(&g_device))
{   
    printf("%s, GetServices failed\r\n", g_device.binfo.XAddr.host);
} 

if (!GetDeviceInformation(&g_device))
{   
    printf("%s, GetDeviceInformation failed\r\n", g_device.binfo.XAddr.host);
}

Key Configuration Points

  • IP Address: Replace 192.168.1.3 with your device's actual IP.
  • Port: Common ports are 80, 8000, 8899. Check your camera's documentation.
  • URL: The standard endpoint is /onvif/device_service
  • Credentials: Use the correct username and password for your device.
  • HTTPS: Set to 1 if connecting securely.