kdriveExpress SDK 23.2.0
kdrive_express_ip_tunneling.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 TELEGRAM_TIMEOUT (1000)
#define MAX_BUFFER_SIZE (64)
#define ERROR_MESSAGE_LEN (128)
/*******************************
** Private Functions
********************************/
static void send_group_value_write(int32_t ap, uint16_t address, uint8_t value);
static void read_group_object(int32_t ap, uint16_t address);
static void on_telegram(const uint8_t* telegram, uint32_t telegram_len, void* user_data);
static void error_callback(error_t e, void* user_data);
static void event_callback(int32_t ap, uint32_t e, void* user_data);
/*******************************
** Main
********************************/
int main(int argc, char* argv[])
{
uint16_t address = 0x901;
uint32_t index = 0;
uint32_t key = 0;
uint8_t value = 127;
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)
{
;
}
}
/*
We register an event callback to notify of the Access Port Events
For example: KDRIVE_EVENT_TERMINATED
*/
kdrive_set_event_callback(ap, &event_callback, NULL);
/*
Open a Tunneling connection with a specific IP Interface,
you will probably have to change the IP address
*/
if (kdrive_ap_open_ip_tunn(ap, "192.168.2.47", 0, 0) == KDRIVE_ERROR_NONE)
{
/*
Connect the Packet Trace logging mechanism
to see the Rx and Tx packets
*/
send_group_value_write(ap, address, 1); /* Write a 1 Bit Boolean: on */
send_group_value_write(ap, address, 0); /* Write a 1 Bit Boolean: off */
/* Read the value of a Communication Object from the bus */
read_group_object(ap, 0x902);
/* now we simply go into bus monitor mode, and display received telegrams */
kdrive_ap_register_telegram_callback(ap, &on_telegram, NULL, &key);
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Entering BusMonitor Mode");
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Press [Enter] to exit the application ...");
getchar();
/* close the access port */
}
/* releases the access port */
return 0;
}
/*******************************
** Private Functions
********************************/
void send_group_value_write(int32_t ap, uint16_t address, uint8_t value)
{
static uint8_t telegram_buffer[MAX_BUFFER_SIZE];
uint32_t telegram_len = MAX_BUFFER_SIZE;
kdrive_ap_group_write(ap, address, &value, 1);
// enable the receive queue
// wait until the bus is idle for 1 second
while ((telegram_len = kdrive_ap_receive(ap, telegram_buffer, MAX_BUFFER_SIZE, TELEGRAM_TIMEOUT)) != 0)
{
kdrive_logger_dump(KDRIVE_LOGGER_INFORMATION, "Received telegram from queue :", telegram_buffer, telegram_len);
}
// disable the receive queue
}
void read_group_object(int32_t ap, uint16_t address)
{
static uint8_t telegram[MAX_BUFFER_SIZE];
uint32_t telegram_len = MAX_BUFFER_SIZE;
telegram_len = kdrive_ap_read_group_object(ap, address, telegram, telegram_len, 1000);
if (telegram_len > 0 &&
(kdrive_ap_is_group_response(telegram, telegram_len)) &&
(kdrive_ap_get_dest(telegram, telegram_len, &address) == KDRIVE_ERROR_NONE) &&
(kdrive_ap_get_group_data(telegram, telegram_len, data, &data_len) == KDRIVE_ERROR_NONE))
{
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "A_GroupValue_Response: 0x%04x ", address);
kdrive_logger_dump(KDRIVE_LOGGER_INFORMATION, "A_GroupValue_Response Data :", data, data_len);
}
else
{
printf("A_GroupValue_Response: 0x%04x - timeout", address);
}
}
void on_telegram(const uint8_t* telegram, uint32_t telegram_len, void* user_data)
{
uint16_t address = 0;
uint8_t message_code = 0;
kdrive_ap_get_message_code(telegram, telegram_len, &message_code);
if ((message_code == KDRIVE_CEMI_L_DATA_IND) &&
kdrive_ap_is_group_write(telegram, telegram_len) &&
(kdrive_ap_get_dest(telegram, telegram_len, &address) == KDRIVE_ERROR_NONE) &&
(kdrive_ap_get_group_data(telegram, telegram_len, data, &data_len) == KDRIVE_ERROR_NONE))
{
kdrive_logger_ex(KDRIVE_LOGGER_INFORMATION, "A_GroupValue_Write: 0x%04x ", address);
kdrive_logger_dump(KDRIVE_LOGGER_INFORMATION, "A_GroupValue_Write Data :", data, data_len);
}
}
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);
}
}
void event_callback(int32_t ap, uint32_t e, void* user_data)
{
switch (e)
{
break;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Access Port Opening");
break;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Access Port Opened");
break;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Access Port Closed");
break;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Access Port Closing");
break;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Access Port Terminated");
break;
break;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "KNX Bus Disconnected");
break;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Local Device Reset");
break;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Telegram Indication");
break;
break;
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Telegram Confirm Timeout");
break;
break;
default:
kdrive_logger(KDRIVE_LOGGER_INFORMATION, "Unknown kdrive event");
break;
}
}
kdriveExpress_API error_t kdrive_ap_get_group_data(const uint8_t telegram[], uint32_t telegram_len, uint8_t *data, uint32_t *data_len)
Extracts the Group Value data from the KNX telegram.
kdriveExpress_API error_t kdrive_ap_packet_trace_connect(int32_t ap)
Starts the Packet Trace, for a specific access port.
kdriveExpress_API error_t kdrive_ap_group_write(int32_t ap, uint16_t address, const uint8_t *value, uint32_t bits)
Sends a GroupValue_Write Telegram The length is specified in bits to enable values less than one byte...
kdriveExpress_API bool_t kdrive_ap_is_group_write(const uint8_t telegram[], uint32_t telegram_len)
Determines if the specified telegram is a GroupValue_Write telegram.
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_close(int32_t ap)
Closes the access port If the access port is not open nothing happens.
kdriveExpress_API error_t kdrive_ap_enable_queue(int32_t ap, bool_t enabled)
Enables/Disables the receive queue for the access port.
kdriveExpress_API error_t kdrive_ap_open_ip_tunn(int32_t ap, const char *ip_address, uint16_t port, const char *iface_address)
Opens a connection to a KNX IP Tunneling Interface device on a specific network interface or default ...
kdriveExpress_API uint32_t kdrive_ap_receive(int32_t ap, uint8_t telegram[], uint32_t telegram_len, uint32_t timeout)
Waits for an inbound KNX telegram and (if received) copies the data into the telegram buffer.
kdriveExpress_API bool_t kdrive_ap_is_group_response(const uint8_t telegram[], uint32_t telegram_len)
Determines if the specified telegram is a GroupValue_Response telegram.
kdriveExpress_API error_t kdrive_ap_get_message_code(const uint8_t telegram[], uint32_t telegram_len, uint8_t *message_code)
Extracts the Message Code from the Telegram.
kdriveExpress_API error_t kdrive_ap_get_dest(const uint8_t telegram[], uint32_t telegram_len, uint16_t *address)
Extracts the Destination Address from the Telegram.
kdriveExpress_API error_t kdrive_ap_register_telegram_callback(int32_t ap, kdrive_ap_telegram_callback c, void *user_data, uint32_t *key)
Registers a callback function.
kdriveExpress_API uint32_t kdrive_ap_read_group_object(int32_t ap, uint16_t address, uint8_t telegram[], uint32_t telegram_len, uint32_t timeout)
Sends a GroupValue_Read Telegram on the Access Port and waits for a GroupValue_Response indication te...
kdriveExpress_API void kdrive_logger(uint8_t level, const char *message)
Writes to the kdrive express logger.
kdriveExpress_API void kdrive_logger_dump(uint8_t level, const char *message, const void *buffer, uint32_t buffer_len)
Logs the given message, followed by the data in buffer.
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
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
unsigned char uint8_t
8 bit unsigned char
Definition: kdrive_express_config.h:30
#define KDRIVE_CEMI_L_DATA_IND
cEMI message code for L_Data.ind
Definition: kdrive_express_defs.h:47
#define KDRIVE_INVALID_DESCRIPTOR
Indicates an invalid descriptor.
Definition: kdrive_express_defs.h:39
#define KDRIVE_MAX_GROUP_VALUE_LEN
The Maximum Length of a Datapoint Value in a GroupValue Telegram.
Definition: kdrive_express_defs.h:33
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_EVENT_CLOSING
The closing signal is emitted when the port is about to be closed.
Definition: kdrive_express_event.h:26
kdriveExpress_API error_t kdrive_set_event_callback(int32_t ap, kdrive_event_callback c, void *user_data)
Sets the event callback, for a specific access port.
#define KDRIVE_EVENT_OPENING
The opening signal is emitted when the port is about to be opened.
Definition: kdrive_express_event.h:24
#define KDRIVE_EVENT_TELEGRAM_INDICATION
Definition: kdrive_express_event.h:34
#define KDRIVE_EVENT_TERMINATED
The terminated signal is emitted when the port was closed (internally) on error.
Definition: kdrive_express_event.h:28
#define KDRIVE_EVENT_CLOSED
The closed signal is emitted when the port was closed.
Definition: kdrive_express_event.h:27
#define KDRIVE_EVENT_INTERNAL_01
Definition: kdrive_express_event.h:38
#define KDRIVE_EVENT_KNX_BUS_DISCONNECTED
The knx bus disconnected signal is emitted when the KNX bus was disconnected.
Definition: kdrive_express_event.h:31
#define KDRIVE_EVENT_ERROR
The error signal is emitted when an error was occurred.
Definition: kdrive_express_event.h:22
#define KDRIVE_EVENT_LOCAL_DEVICE_RESET
The local device reset signal is emitted when a KNX reset.ind was received.
Definition: kdrive_express_event.h:32
#define KDRIVE_EVENT_TELEGRAM_CONFIRM_TIMEOUT
Definition: kdrive_express_event.h:36
#define KDRIVE_EVENT_KNX_BUS_CONNECTED
The knx bus connected signal is emitted when the KNX bus was connected.
Definition: kdrive_express_event.h:30
#define KDRIVE_EVENT_OPENED
The opened signal is emitted when the port was opened.
Definition: kdrive_express_event.h:25
#define KDRIVE_EVENT_TELEGRAM_CONFIRM
Definition: kdrive_express_event.h:35
#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