kdriveExpress SDK 23.2.0
kdrive_express_ip_tunneling_enumerate.c
//
// Copyright (c) 2002-2023 WEINZIERL ENGINEERING GmbH
// All rights reserved.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
// WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
//
#include <stdio.h>
#include <stdlib.h>
#include <kdrive_express.h>
#define ERROR_MESSAGE_LEN (128)
#define MAX_ITEMS (50)
/*******************************
** Private Functions
********************************/
static void enumerate_tunneling_devices(int32_t ap);
static void print_tunneling_slots(ip_tunn_slot_info_t slot_infos[], uint16_t slot_infos_count);
static void error_callback(error_t e, void* user_data);
/*******************************
** Main
********************************/
int main(int argc, char* argv[])
{
int32_t ap = 0;
/*
Configure the logging level and console logger
*/
/*
We register an error callback as a convenience logger function to
print out the error message when an error occurs.
*/
kdrive_register_error_callback(&error_callback, NULL);
/*
We create a Access Port descriptor. This descriptor is then used for
all calls to that specific access port.
*/
/*
We check that we were able to allocate a new descriptor
This should always happen, unless a bad_alloc exception is internally thrown
which means the memory couldn't be allocated.
*/
{
printf("Unable to create access port. This is a terminal failure\n");
while (1)
{
;
}
}
enumerate_tunneling_devices(ap);
/* releases the access port */
return 0;
}
/*******************************
** Private Functions
********************************/
void enumerate_tunneling_devices(int32_t ap)
{
ip_tunn_dev_t items[MAX_ITEMS];
uint32_t items_length = MAX_ITEMS;
uint32_t index = 0;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Enumerating KNX IP Tunneling Interfaces");
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "========================================");
if (kdrive_ap_enum_ip_tunn(ap, items, &items_length) == KDRIVE_ERROR_NONE)
{
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "Found %d device(s)", items_length);
for (index = 0; index < items_length; ++index)
{
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "%d) Name: %s", index + 1, items[index].dev_name);
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "%s on %s", items[index].ip_address, items[index].iface_address);
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "Individual address: %04X", items[index].ind_addr);
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "Programming mode: %s", (items[index].prog_mode_enabled ? "on" : "off"));
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "MAC address: %02X:%02X:%02X:%02X:%02X:%02X",
items[index].mac_address[0], items[index].mac_address[1], items[index].mac_address[2],
items[index].mac_address[3], items[index].mac_address[4], items[index].mac_address[5]);
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "Serial number: %02X %02X %02X %02X %02X %02X",
items[index].serial_number[0], items[index].serial_number[1], items[index].serial_number[2],
items[index].serial_number[3], items[index].serial_number[4], items[index].serial_number[5]);
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "Security supported: %s", (items[index].is_security_supported ? "true" : "false"));
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "Tunneling secure: %s", (items[index].is_tunneling_secure ? "on" : "off"));
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "Management secure: %s", (items[index].is_management_secure ? "on" : "off"));
print_tunneling_slots(items[index].slot_infos, items[index].slot_infos_count);
}
}
}
void print_tunneling_slots(ip_tunn_slot_info_t slot_infos[], uint16_t slot_infos_count)
{
uint32_t index;
uint16_t address;
uint16_t status;
bool_t is_usable;
bool_t is_authorised;
bool_t is_free;
for (index = 0; index < slot_infos_count; ++index)
{
address = slot_infos[index].address;
status = slot_infos[index].status;
is_usable = (status & KDRIVE_TUNNEL_SLOT_STATUS_USABLE) ? 1 : 0;
is_authorised = (status & KDRIVE_TUNNEL_SLOT_STATUS_AUTH) ? 1 : 0;
is_free = (status & KDRIVE_TUNNEL_SLOT_STATUS_FREE) ? 1 : 0;
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "Slot %d) address: %04X (%s %s %s)",
index, address,
is_usable ? " Usable " : "!Usable ",
is_authorised ? " Authorised " : "!Authorised ",
is_free ? " Free " : "!Free ");
}
}
void error_callback(error_t e, void* user_data)
{
{
static char error_message[ERROR_MESSAGE_LEN];
kdrive_get_error_message(e, error_message, ERROR_MESSAGE_LEN);
kdrive_logger_ex(KDRIVE_LOGGER_ERROR, "kdrive error: %s", error_message);
}
}
kdriveExpress_API bool_t kdrive_ap_release(int32_t ap)
Releases the AccessPort interface.
kdriveExpress_API int32_t kdrive_ap_create(void)
Creates an internal AccessPort interface This should be the first function called when working with t...
kdriveExpress_API error_t kdrive_ap_enum_ip_tunn(int32_t ap, ip_tunn_dev_t items[], uint32_t *items_length)
Scans for all KNX IP Tunneling Interface devices The items array must exist (should be pre-allocated ...
kdriveExpress_API void kdrive_logger(uint8_t level, const char *message)
Writes to the kdrive express logger.
kdriveExpress_API void kdrive_logger_console(void)
Sets the logger to write to the console.
kdriveExpress_API void kdrive_logger_ex(uint8_t level, const char *fmt,...)
Writes to the kdrive express logger.
kdriveExpress_API void kdrive_logger_set_level(uint8_t level)
Sets the root logger level This is once of:
unsigned short uint16_t
16 bit unsigned char
Definition: kdrive_express_config.h:31
bool bool_t
Definition: kdrive_express_config.h:58
int int32_t
32 bit signed int
Definition: kdrive_express_config.h:35
unsigned int uint32_t
32 bit unsigned char
Definition: kdrive_express_config.h:32
int32_t error_t
Definition: kdrive_express_config.h:47
#define KDRIVE_TUNNEL_SLOT_STATUS_FREE
Tunneling slot status Free: 0 = not free; 1 = free.
Definition: kdrive_express_defs.h:73
#define KDRIVE_TUNNEL_SLOT_STATUS_AUTH
Tunneling slot status Authorised: 0 = authorisation is required; 1 = no authorisation is required.
Definition: kdrive_express_defs.h:77
#define KDRIVE_INVALID_DESCRIPTOR
Indicates an invalid descriptor.
Definition: kdrive_express_defs.h:39
#define KDRIVE_TUNNEL_SLOT_STATUS_USABLE
Tunneling slot status Usable: 0 = This slot is not usable; 1 = This slot is usable.
Definition: kdrive_express_defs.h:81
kdriveExpress_API void kdrive_register_error_callback(kdrive_error_callback c, void *user_data)
Registers the error callback function.
#define KDRIVE_TIMEOUT_ERROR
Timeout.
Definition: kdrive_express_error.h:26
#define KDRIVE_ERROR_NONE
No Error, Everything OK.
Definition: kdrive_express_error.h:22
kdriveExpress_API void kdrive_get_error_message(error_t e, char *str, uint32_t str_len)
Gets the error message.
#define KDRIVE_LOGGER_ERROR
An error.
Definition: kdrive_express_logger.h:28
#define KDRIVE_LOGGER_INFORMATION
An informational message, usually denoting the successful completion of an operation.
Definition: kdrive_express_logger.h:31
Structure holding ip tunneling device information.
Definition: kdrive_express_access.h:176
Structure holding ip tunnling slot information.
Definition: kdrive_express_access.h:162
uint16_t status
2 bytes of status bits, currently only the last 3 are used, they are Usable(third bit),...
Definition: kdrive_express_access.h:164
uint16_t address
the individual address of the tunnel
Definition: kdrive_express_access.h:163