bRAWcap 1.0.1
b-plus Technologies - Ethernet Performance Transmitter Receiver
Loading...
Searching...
No Matches
02_adapter_change_notifier.c

This example shows how to register and handle adapter change notifications. It will print each received notification to command line. Each output will also include the change reason and the new adapter property value.

Attention
This example requires a valid bRAWcap license. Reading adapter properties is not part of the demo.
1/**
2 * @file 02_adapter_change_notifier.c
3 *
4 * @brief bRAWcap Example - Demonstrates the usage of the adapter handling module.
5 *
6 * It shows how to:
7 * - Receive adapter change notifications
8 * - Process change notifications
9 *
10 * This example makes use of C only.
11 *
12 * @attention This example requires a valid bRAWcap license.
13 * Receiving adapter change notifications and reading their properties is not part of the demo.
14 *
15 * @version 1.0
16 *
17 * @date 2023-03-24
18 *
19 * @copyright
20 * <b> © 2021 - b-plus technologies GmbH. All rights reserved!</b>
21 *
22 * All rights exclusively reserved for b-plus GmbH, unless expressly otherwise agreed.
23 *
24 * Redistribution in source or any other form, with or without modification, is not permitted.
25 *
26 * You may use this code under the according license terms of b-plus.
27 * Please contact b-plus at services@b-plus.com to get the appropriate terms and conditions.
28 */
29// Include bRAWcap
30#include "libbrawcap.h"
31
32// C STD
33#include <stdlib.h> // for malloc & free
34#include <stdio.h> // for printf
35
36// Check command line parameters if a custom execution time is specified
37void ParseArgs(int argc, char** argv, int* exec_time)
38{
39 if(argc > 1)
40 {
41 if(!memcmp(argv[1], "-t", 2))
42 {
43 char* pEnd = 0;
44 *exec_time = strtol(argv[2], &pEnd,0);
45 }
46 }
47}
48
49void change_notification_callback(const brawcap_adapter_name_t name, const brawcap_adapter_notify_reason_t reason,
50 const UINT32 properties, void* pUser)
51{
52 // We have entered a user context which points to a notification counter.
53 // To use it we should first of all check if it is valid and if so cast it to the correct type.
54 if(pUser)
55 {
57 int* pNotifyCounter = (int*) pUser;
58 ++(*pNotifyCounter);
59
60#if 1 // We always try to print the friendly name too (for readability...)
61 // Therefore we must retrieve it from the adapter name.
62 // If an adapter is not available anymore we can not resolve it´s friendly name.
63 // In this case it will be "null" instead.
64 char* friendlyName = 0;
65 UINT32 friendlyNameLength = 0;
66 if(brawcap_adapter_friendly_name(name, friendlyName, &friendlyNameLength) == BRAWCAP_STATUS_ERROR_OVERRUN)
67 {
68 friendlyName = malloc(friendlyNameLength);
69 status = brawcap_adapter_friendly_name(name, friendlyName, &friendlyNameLength);
70 if(status != BRAWCAP_STATUS_SUCCESS)
71 {
72 printf("Could not resolve adapter friendly name (unexpected status: %d)\n", status);
73 free(friendlyName);
74 return;
75 }
76 }
77#endif
78 printf("\n%03d. Notification\n", *pNotifyCounter);
79#if 1 // Now handle the different notification reasons.
80 switch (reason)
81 {
82#if 1 // The given reason is unknown
84 default:
85 printf("Unknown notification reason.");
86 break;
87#endif
88#if 1 // The specified adapter was added
90 printf("%s (%s) added\n", friendlyName, name);
91 break;
92#endif
93#if 1 // The specified adapter was removed
95 printf("%s (%s) removed\n", friendlyName, name);
96 break;
97#endif
98#if 1 // At least one property of the specified adapter has changed.
100#if 1 // Connection state has changed
102 {
104 brawcap_adapter_connection(name, &connectionState);
105 printf("%s (%s) new connection state: ", friendlyName, name);
106 switch (connectionState)
107 {
108 case BRAWCAP_ADAPTER_CONNECTION_UNKNOWN: printf("%s\n", "Unknown"); break;
109 case BRAWCAP_ADAPTER_CONNECTION_DOWN: printf("%s\n", "Down"); break;
110 case BRAWCAP_ADAPTER_CONNECTION_UP: printf("%s\n", "Up"); break;
111 }
112 }
113#endif
114#if 1 // Friendly name has changed
116 printf("%s new friendly name: %s\n", name, friendlyName);
117#endif
118#if 1 // IPv4 has changed
119 if(properties & BRAWCAP_ADAPTER_PROPERTY_IPV4)
120 {
121 brawcap_adapter_ipv4_t ipv4 = {0};
122 brawcap_adapter_ipv4(name, &ipv4);
123 printf("%s (%s) new IPv4: %u.%u.%u.%u\n", friendlyName, name,
124 ipv4.bytes[0], ipv4.bytes[1], ipv4.bytes[2], ipv4.bytes[3]);
125 }
126#endif
127#if 1 // IPv6 has changed
128 if(properties & BRAWCAP_ADAPTER_PROPERTY_IPV6)
129 {
130 brawcap_adapter_ipv6_t ipv6 = {0};
131 brawcap_adapter_ipv6(name, &ipv6);
132 printf("%s (%s) new IPv6: %x:%x:%x:%x:%x:%x:%x:%x\n", friendlyName, name,
133 ipv6.words[0], ipv6.words[1], ipv6.words[2], ipv6.words[3],
134 ipv6.words[4], ipv6.words[5], ipv6.words[6], ipv6.words[7]);
135 }
136#endif
137#if 1 // Max transmission unit has changed
138 if(properties & BRAWCAP_ADAPTER_PROPERTY_MTU)
139 {
140 brawcap_adapter_mtu_t mtu = 0;
141 brawcap_adapter_mtu(name, &mtu);
142 printf("%s (%s) new max transmission unit: %llu\n", friendlyName, name, mtu);
143 }
144#endif
145#if 1 // Operation state has changed
147 {
149 brawcap_adapter_operation(name, &operationState);
150 printf("%s (%s) new operation state: ", friendlyName, name);
151 switch (operationState)
152 {
153 case BRAWCAP_ADAPTER_OPERATION_UNKNOWN: printf("%s\n", "Unknown"); break;
154 case BRAWCAP_ADAPTER_OPERATION_DOWN: printf("%s\n", "Down"); break;
155 case BRAWCAP_ADAPTER_OPERATION_TESTING: printf("%s\n", "Testing"); break;
156 case BRAWCAP_ADAPTER_OPERATION_UP: printf("%s\n", "Up"); break;
157 }
158 }
159#endif
160#if 1 // Receive speed has changed
161 if(properties & BRAWCAP_ADAPTER_PROPERTY_RX_SPEED)
162 {
164 brawcap_adapter_speed_rx(name, &speed);
165 printf("%s (%s) new receive (uplink) speed: %u MBit/s\n", friendlyName, name, speed);
166 }
167#endif
168#if 1 // Transmit speed has changed
169 if(properties & BRAWCAP_ADAPTER_PROPERTY_TX_SPEED)
170 {
172 brawcap_adapter_speed_tx(name, &speed);
173 printf("%s (%s) new transmit (uplink) speed: %u MBit/s\n", friendlyName, name, speed);
174 }
175#endif
176 break;
177#endif
178 }
179 // Don´t forget to free the allocated memory...
180 free(friendlyName);
181 }
182#endif
183}
184
185// Use command line parameter -t to specify a custom execution time.
186// If it is not given the execution time is per default 5 minutes.
187int main(int argc, char** argv)
188{
189 // Set console title
190 SetConsoleTitleA("bRAWcap Example - Adapter Change Notifier");
191
192 int exec_time = 300; // 5 minutes
193 int runtime_sec = 0;
194 ParseArgs(argc, argv, &exec_time);
195
197
198 // This variable will be entered during registration, to make it available from inside the callback.
199 // ATTENTION: You are responsible for checking the pointer inside your callback, before accessing it.
200 int notifyCounter = 0;
201
202 // Register our process for bRAWcap adapter change notifications
203 brawcap_adapter_notify_callback_t callback = change_notification_callback;
204 status = brawcap_adapter_notify_register(callback, &notifyCounter);
205 if(!BRAWCAP_SUCCESS(status))
206 {
207 printf("Registration for adapter change notification failed (Status: %d). Will stop now...", status);
208 return -1;
209 }
210
211 // Our main thread does nothing than waiting until execution time is elapsed.
212 // Everything else is done by the thread which is calling our registered callback.
213 do
214 {
215 Sleep(1000);
216 } while (++runtime_sec < exec_time);
217
218 // Unregister bRAWcap adapter change notifications before exiting.
219 // NOTE: bRAWcap would automatically unregister on unloading.
220 // Therefore unregistering on exiting isn´t required...
222 if(!BRAWCAP_SUCCESS(status))
223 {
224 printf("Unregistration for adapter change noTification failed (Status: %d). Will stop now...", status);
225 return -1;
226 }
227
228 return 0;
229}
bRAWcap main header.
brawcap_status_t
bRAWcap status/return codes.
Definition: brawcap_types_shared.h:150
#define BRAWCAP_SUCCESS(status)
Checks if the returned status indicates a success with no additional info.
Definition: brawcap_types_shared.h:122
@ BRAWCAP_STATUS_SUCCESS
Definition: brawcap_types_shared.h:152
@ BRAWCAP_STATUS_ERROR_OVERRUN
A buffer overrun was detected.
Definition: brawcap_types_shared.h:245
IPv4 address.
Definition: brawcap_types_um.h:263
IPv6 address.
Definition: brawcap_types_um.h:273
UINT8 bytes[4]
Definition: brawcap_types_um.h:264
UINT16 words[8]
Definition: brawcap_types_um.h:275
brawcap_status_t brawcap_adapter_notify_unregister()
Unregisters the calling process from receiving adapter change notifications.
brawcap_status_t brawcap_adapter_mtu(const brawcap_adapter_name_t name, brawcap_adapter_mtu_t *const pMtu)
Reads out the configured MTU of the specified adapter.
brawcap_status_t brawcap_adapter_speed_rx(const brawcap_adapter_name_t name, brawcap_adapter_speed_t *const pSpeed)
Reads out the current receive speed of the specified adapter.
brawcap_status_t brawcap_adapter_ipv4(const brawcap_adapter_name_t name, brawcap_adapter_ipv4_t *const pIpv4)
Reads out the currently set IPv4 address of the specified adapter.
brawcap_status_t brawcap_adapter_friendly_name(const brawcap_adapter_name_t name, char *const friendlyName, UINT32 *const pLength)
Reads out the currently set adapter friendly name for the specified adapter.
brawcap_adapter_connection_state_t
List of adapter connection states. Values indicating the current adapter connection state (uplink/cab...
Definition: brawcap_types_um.h:306
char brawcap_adapter_name_t[BRAWCAP_ADAPTER_NAME_LENGTH]
Fixed size array containing a adapter name.
Definition: brawcap_types_um.h:257
brawcap_status_t brawcap_adapter_connection(const brawcap_adapter_name_t name, brawcap_adapter_connection_state_t *const pConnection)
Reads out the current connection state of the specified adapter.
brawcap_status_t brawcap_adapter_ipv6(const brawcap_adapter_name_t name, brawcap_adapter_ipv6_t *const pIpv6)
Reads out the currently set IPv6 address of the specified adapter.
brawcap_adapter_speed_t
List of bRAWcap adapter speeds. Values indicating the current adapter uplink speed.
Definition: brawcap_types_um.h:287
void(* brawcap_adapter_notify_callback_t)(const brawcap_adapter_name_t name, const brawcap_adapter_notify_reason_t reason, const UINT32 properties, void *pUser)
The general callback for adapter change notifications. It will be called each time a available and su...
Definition: brawcap_types_um.h:400
brawcap_adapter_operation_state_t
List of adapter operation states. Values indicating the current adapter operation state (adapter driv...
Definition: brawcap_types_um.h:317
brawcap_status_t brawcap_adapter_notify_register(brawcap_adapter_notify_callback_t const callback, void *const pUser)
Registers the calling process for receiving adapter change notifications.
brawcap_status_t brawcap_adapter_speed_tx(const brawcap_adapter_name_t name, brawcap_adapter_speed_t *const pSpeed)
Reads out the current transmit speed of the specified adapter.
brawcap_adapter_notify_reason_t
List of reasons for adapter change notifications. This values indicate why a adapter change notificat...
Definition: brawcap_types_um.h:344
UINT64 brawcap_adapter_mtu_t
Type for handling the adapter maximum transmission unit (MTU).
Definition: brawcap_types_um.h:337
brawcap_status_t brawcap_adapter_operation(const brawcap_adapter_name_t name, brawcap_adapter_operation_state_t *const pOperation)
Reads out the current operation state of the specified adapter.
@ BRAWCAP_ADAPTER_PROPERTY_CONNECTION_STATE
Definition: brawcap_types_um.h:380
@ BRAWCAP_ADAPTER_PROPERTY_IPV6
Definition: brawcap_types_um.h:382
@ BRAWCAP_ADAPTER_PROPERTY_OPERATION_STATE
Definition: brawcap_types_um.h:379
@ BRAWCAP_ADAPTER_PROPERTY_RX_SPEED
Definition: brawcap_types_um.h:376
@ BRAWCAP_ADAPTER_PROPERTY_FRIENDLY_NAME
Definition: brawcap_types_um.h:375
@ BRAWCAP_ADAPTER_PROPERTY_IPV4
Definition: brawcap_types_um.h:381
@ BRAWCAP_ADAPTER_PROPERTY_TX_SPEED
Definition: brawcap_types_um.h:377
@ BRAWCAP_ADAPTER_PROPERTY_MTU
Definition: brawcap_types_um.h:378
@ BRAWCAP_ADAPTER_CONNECTION_UP
Definition: brawcap_types_um.h:308
@ BRAWCAP_ADAPTER_CONNECTION_UNKNOWN
Definition: brawcap_types_um.h:307
@ BRAWCAP_ADAPTER_CONNECTION_DOWN
Definition: brawcap_types_um.h:309
@ BRAWCAP_ADAPTER_SPEED_UNKNOWN
Definition: brawcap_types_um.h:288
@ BRAWCAP_ADAPTER_OPERATION_DOWN
Definition: brawcap_types_um.h:320
@ BRAWCAP_ADAPTER_OPERATION_UNKNOWN
Definition: brawcap_types_um.h:318
@ BRAWCAP_ADAPTER_OPERATION_TESTING
Definition: brawcap_types_um.h:321
@ BRAWCAP_ADAPTER_OPERATION_UP
Definition: brawcap_types_um.h:319
@ BRAWCAP_ADAPTER_NOTIFY_REASON_PROPERTY_CHANGE
A adapter property has changed. This can have several reasons, for a list of all properties see brawc...
Definition: brawcap_types_um.h:363
@ BRAWCAP_ADAPTER_NOTIFY_REASON_ADD
A new bRAWcap adapter was detected. E.g. due to enabling bRAWcap driver on an adapter where it was di...
Definition: brawcap_types_um.h:353
@ BRAWCAP_ADAPTER_NOTIFY_REASON_REMOVE
A bRAWcap adapter was removed. E.g. due to disabling bRAWcap driver on an adapter where it was enable...
Definition: brawcap_types_um.h:358
@ BRAWCAP_ADAPTER_NOTIFY_REASON_UNKNOWN
The notification reason is unknown.
Definition: brawcap_types_um.h:348