bRAWcap 1.1.0
b-plus Technologies - Ethernet Performance Transmitter Receiver
Loading...
Searching...
No Matches
02_adapter_change_notifier_f.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_f.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.1
16 *
17 * @date 2023-03-24
18 *
19 * HISTORY:
20 * - 2023-03-30: Initial version
21 * - 2023-08-10: Set all indentation to 4 spaces, removed unnecessary preprocessor directives to improve readability
22 *
23 * @copyright
24 * <b> © 2021 - b-plus technologies GmbH. All rights reserved!</b>
25 *
26 * All rights exclusively reserved for b-plus GmbH, unless expressly otherwise agreed.
27 *
28 * Redistribution in source or any other form, with or without modification, is not permitted.
29 *
30 * You may use this code under the according license terms of b-plus.
31 * Please contact b-plus at services@b-plus.com to get the appropriate terms and conditions.
32 */
33// Include bRAWcap
34#include "libbrawcap.h"
35
36// C STD
37#include <stdlib.h> // for malloc & free
38#include <stdio.h> // for printf
39
40// Check command line parameters if a custom execution time is specified
41void ParseArgs(int argc, char** argv, int* exec_time)
42{
43 if(argc > 1)
44 {
45 if(!memcmp(argv[1], "-t", 2))
46 {
47 char* pEnd = 0;
48 *exec_time = strtol(argv[2], &pEnd,0);
49 }
50 }
51}
52
53void change_notification_callback(const brawcap_adapter_name_t name, const brawcap_adapter_notify_reason_t reason,
54 const UINT32 properties, void* pUser)
55{
56 // We have entered a user context which points to a notification counter.
57 // To use it we should first of all check if it is valid and if so cast it to the correct type.
58 if(pUser)
59 {
61 int* pNotifyCounter = (int*) pUser;
62 ++(*pNotifyCounter);
63
64 // We always try to print the friendly name too (for readability...)
65 // Therefore we must retrieve it from the adapter name.
66 // If an adapter is not available anymore we can not resolve it´s friendly name.
67 // In this case it will be "null" instead.
68 char* friendlyName = 0;
69 UINT32 friendlyNameLength = 0;
70 if(brawcap_adapter_friendly_name(name, friendlyName, &friendlyNameLength) == BRAWCAP_STATUS_ERROR_OVERRUN)
71 {
72 friendlyName = (char*) malloc(friendlyNameLength);
73 status = brawcap_adapter_friendly_name(name, friendlyName, &friendlyNameLength);
74 if(status != BRAWCAP_STATUS_SUCCESS)
75 {
76 printf("Could not resolve adapter friendly name (unexpected status: %d)\n", status);
77 free(friendlyName);
78 return;
79 }
80 }
81
82 printf("\n%03d. Notification\n", *pNotifyCounter);
83 // Now handle the different notification reasons.
84 switch (reason)
85 {
86 // The given reason is unknown
88 default:
89 printf("Unknown notification reason.");
90 break;
91
92 // The specified adapter was added
94 printf("%s (%s) added\n", friendlyName, name);
95 break;
96
97 // The specified adapter was removed
99 printf("%s (%s) removed\n", friendlyName, name);
100 break;
101
102 // At least one property of the specified adapter has changed.
104 // Connection state has changed
106 {
108 brawcap_adapter_connection(name, &connectionState);
109 printf("%s (%s) new connection state: ", friendlyName, name);
110 switch (connectionState)
111 {
112 case BRAWCAP_ADAPTER_CONNECTION_UNKNOWN: printf("%s\n", "Unknown"); break;
113 case BRAWCAP_ADAPTER_CONNECTION_DOWN: printf("%s\n", "Down"); break;
114 case BRAWCAP_ADAPTER_CONNECTION_UP: printf("%s\n", "Up"); break;
115 }
116 }
117
118 // Friendly name has changed
120 printf("%s new friendly name: %s\n", name, friendlyName);
121
122 // IPv4 has changed
123 if(properties & BRAWCAP_ADAPTER_PROPERTY_IPV4)
124 {
125 brawcap_adapter_ipv4_t ipv4 = {0};
126 brawcap_adapter_ipv4(name, &ipv4);
127 printf("%s (%s) new IPv4: %u.%u.%u.%u\n", friendlyName, name,
128 ipv4.bytes[0], ipv4.bytes[1], ipv4.bytes[2], ipv4.bytes[3]);
129 }
130
131 // IPv6 has changed
132 if(properties & BRAWCAP_ADAPTER_PROPERTY_IPV6)
133 {
134 brawcap_adapter_ipv6_t ipv6 = {0};
135 brawcap_adapter_ipv6(name, &ipv6);
136 printf("%s (%s) new IPv6: %x:%x:%x:%x:%x:%x:%x:%x\n", friendlyName, name,
137 ipv6.words[0], ipv6.words[1], ipv6.words[2], ipv6.words[3],
138 ipv6.words[4], ipv6.words[5], ipv6.words[6], ipv6.words[7]);
139 }
140
141 // Max transmission unit has changed
142 if(properties & BRAWCAP_ADAPTER_PROPERTY_MTU)
143 {
144 brawcap_adapter_mtu_t mtu = 0;
145 brawcap_adapter_mtu(name, &mtu);
146 printf("%s (%s) new max transmission unit: %llu\n", friendlyName, name, mtu);
147 }
148
149 // Operation state has changed
151 {
153 brawcap_adapter_operation(name, &operationState);
154 printf("%s (%s) new operation state: ", friendlyName, name);
155 switch (operationState)
156 {
157 case BRAWCAP_ADAPTER_OPERATION_UNKNOWN: printf("%s\n", "Unknown"); break;
158 case BRAWCAP_ADAPTER_OPERATION_DOWN: printf("%s\n", "Down"); break;
159 case BRAWCAP_ADAPTER_OPERATION_TESTING: printf("%s\n", "Testing"); break;
160 case BRAWCAP_ADAPTER_OPERATION_UP: printf("%s\n", "Up"); break;
161 }
162 }
163
164 // Receive speed has changed
165 if(properties & BRAWCAP_ADAPTER_PROPERTY_RX_SPEED)
166 {
168 brawcap_adapter_speed_rx(name, &speed);
169 printf("%s (%s) new receive (uplink) speed: %u MBit/s\n", friendlyName, name, speed);
170 }
171
172 // Transmit speed has changed
173 if(properties & BRAWCAP_ADAPTER_PROPERTY_TX_SPEED)
174 {
176 brawcap_adapter_speed_tx(name, &speed);
177 printf("%s (%s) new transmit (uplink) speed: %u MBit/s\n", friendlyName, name, speed);
178 }
179 break;
180 }
181 // Don´t forget to free the allocated memory...
182 free(friendlyName);
183 }
184}
185
186// Use command line parameter -t to specify a custom execution time.
187// If it is not given the execution time is per default 5 minutes.
188int main(int argc, char** argv)
189{
190 // Set console title
191 SetConsoleTitleA("bRAWcap Example - Adapter Change Notifier");
192
193 int exec_time = 300; // 5 minutes
194 int runtime_sec = 0;
195 ParseArgs(argc, argv, &exec_time);
196
198
199 // This variable will be entered during registration, to make it available from inside the callback.
200 // ATTENTION: You are responsible for checking the pointer inside your callback, before accessing it.
201 int notifyCounter = 0;
202
203 // Register our process for bRAWcap adapter change notifications
204 brawcap_adapter_notify_callback_t callback = change_notification_callback;
205 status = brawcap_adapter_notify_register(callback, &notifyCounter);
206 if(!BRAWCAP_SUCCESS(status))
207 {
208 printf("Registration for adapter change notification failed (Status: %d). Will stop now...", status);
209 return -1;
210 }
211
212 // Our main thread does nothing than waiting until execution time is elapsed.
213 // Everything else is done by the thread which is calling our registered callback.
214 do
215 {
216 Sleep(1000);
217 } while (++runtime_sec < exec_time);
218
219 // Unregister bRAWcap adapter change notifications before exiting.
220 // NOTE: bRAWcap would automatically unregister on unloading.
221 // Therefore unregistering on exiting isn´t required...
223 if(!BRAWCAP_SUCCESS(status))
224 {
225 printf("Unregistration for adapter change noTification failed (Status: %d). Will stop now...", status);
226 return -1;
227 }
228
229 return 0;
230}
bRAWcap main header.
brawcap_status_t
bRAWcap status/return codes.
Definition: brawcap_types_shared.h:140
#define BRAWCAP_SUCCESS(status)
Checks if the returned status indicates a success with no additional info.
Definition: brawcap_types_shared.h:112
@ BRAWCAP_STATUS_SUCCESS
Definition: brawcap_types_shared.h:142
@ BRAWCAP_STATUS_ERROR_OVERRUN
A buffer overrun was detected.
Definition: brawcap_types_shared.h:235
IPv4 address.
Definition: brawcap_types_um.h:307
IPv6 address.
Definition: brawcap_types_um.h:317
UINT8 bytes[4]
Definition: brawcap_types_um.h:308
UINT16 words[8]
Definition: brawcap_types_um.h:319
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:350
char brawcap_adapter_name_t[BRAWCAP_ADAPTER_NAME_LENGTH]
Fixed size array containing a adapter name.
Definition: brawcap_types_um.h:301
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:331
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:441
brawcap_adapter_operation_state_t
List of adapter operation states. Values indicating the current adapter operation state (adapter driv...
Definition: brawcap_types_um.h:361
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:388
UINT64 brawcap_adapter_mtu_t
Type for handling the adapter maximum transmission unit (MTU).
Definition: brawcap_types_um.h:381
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:424
@ BRAWCAP_ADAPTER_PROPERTY_IPV6
Definition: brawcap_types_um.h:426
@ BRAWCAP_ADAPTER_PROPERTY_OPERATION_STATE
Definition: brawcap_types_um.h:423
@ BRAWCAP_ADAPTER_PROPERTY_RX_SPEED
Definition: brawcap_types_um.h:420
@ BRAWCAP_ADAPTER_PROPERTY_FRIENDLY_NAME
Definition: brawcap_types_um.h:419
@ BRAWCAP_ADAPTER_PROPERTY_IPV4
Definition: brawcap_types_um.h:425
@ BRAWCAP_ADAPTER_PROPERTY_TX_SPEED
Definition: brawcap_types_um.h:421
@ BRAWCAP_ADAPTER_PROPERTY_MTU
Definition: brawcap_types_um.h:422
@ BRAWCAP_ADAPTER_CONNECTION_UP
Definition: brawcap_types_um.h:352
@ BRAWCAP_ADAPTER_CONNECTION_UNKNOWN
Definition: brawcap_types_um.h:351
@ BRAWCAP_ADAPTER_CONNECTION_DOWN
Definition: brawcap_types_um.h:353
@ BRAWCAP_ADAPTER_SPEED_UNKNOWN
Definition: brawcap_types_um.h:332
@ BRAWCAP_ADAPTER_OPERATION_DOWN
Definition: brawcap_types_um.h:364
@ BRAWCAP_ADAPTER_OPERATION_UNKNOWN
Definition: brawcap_types_um.h:362
@ BRAWCAP_ADAPTER_OPERATION_TESTING
Definition: brawcap_types_um.h:365
@ BRAWCAP_ADAPTER_OPERATION_UP
Definition: brawcap_types_um.h:363
@ 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:407
@ 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:397
@ 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:402
@ BRAWCAP_ADAPTER_NOTIFY_REASON_UNKNOWN
The notification reason is unknown.
Definition: brawcap_types_um.h:392