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

This example shows how to get all currently available adapters on the machine. And how to get the properties for each adapter. The adapters and their properties will be printed to command line output.

Attention
This example requires a valid bRAWcap license. Reading adapter properties is not part of the demo.
1/**
2 * @file 01_adapter_property_reader.c
3 *
4 * @brief bRAWcap Example - Demonstrates the usage of the adapter handling module.
5 *
6 * It shows how to:
7 * - Update and use the adapter list
8 * - Read adapter properties
9 *
10 * This example makes use of C only.
11 *
12 * @attention This example requires a valid bRAWcap license.
13 * Reading adapter 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
36int main(int argc, char** argv)
37{
38 // Set console title
39 SetConsoleTitleA("bRAWcap Example - Adapter Property Reader");
40
42
43 // Encapsulate all in a single loop.
44 // This allow breaking execution if something unexpected happens.
45 do
46 {
47#if 1 // Update list of available adapters
48 // NOTE: The list is initialized during loading/initializing bRAWcap.
50 if(!BRAWCAP_SUCCESS(status))
51 break;
52#endif
53#if 1 // Get number of currently available adapters
54 brawcap_adapter_count_t adapterCount = 0;
55 status = brawcap_adapter_list_count(&adapterCount);
56 if(!BRAWCAP_SUCCESS(status))
57 break;
58#endif
59#if 1 // Now loop through each entry of the list.
60 // Here we retrieve all it´s available adapter properties and print them to console.
61 for(unsigned int index = 0; index < adapterCount; ++index)
62 {
63#if 1 // Get adapter name from list
64 brawcap_adapter_name_t name = {'\0'};
65 status = brawcap_adapter_list_at(index, name);
66 if(!BRAWCAP_SUCCESS(status))
67 break;
68 printf("%02u. Adapter - Name: %s ", index + 1, name);
69#endif
70#if 1 // Resolve adapter friendly name by using it´s name
71 UINT32 friendlyNameLength = 0;
72 char* friendlyName = NULL;
73 status = brawcap_adapter_friendly_name(name, friendlyName, &friendlyNameLength);
74 // We expect this return value because our buffer is not yet initialized (zero).
75 // If we use the function like this, it will inform us of the required this by setting friendlyNameLength.
77 {
78 // friendlyNameLength is set to the required length, so lets use it to create the buffer
79 friendlyName = malloc(friendlyNameLength);
80 // Now call the function again with a valid buffer
81 status = brawcap_adapter_friendly_name(name, friendlyName, &friendlyNameLength);
82 if(!BRAWCAP_SUCCESS(status))
83 break;
84 }
85 else
86 break;
87 printf("Friendly Name: %s ", friendlyName);
88#endif
89#if 1 // Resolve adapter description by using it´s name
90 UINT32 descriptionLength = 0;
91 char* description = NULL;
92 status = brawcap_adapter_description(name, description, &descriptionLength);
93 // Here we have the same handling like we had for the friendly name...
95 {
96 // friendlyNameLength is set to the required length, so lets use it to create the buffer
97 description = malloc(descriptionLength);
98 // Now call the function again with a valid buffer
99 status = brawcap_adapter_description(name, description, &descriptionLength);
100 if(!BRAWCAP_SUCCESS(status))
101 break;
102 }
103 else
104 break;
105 printf("Description: %s ", description);
106#endif
107#if 1 // Resolve adapter MAC address by using it´s name
108 brawcap_adapter_mac_t mac = {0};
109 status = brawcap_adapter_mac(name, mac);
110 if (!BRAWCAP_SUCCESS(status))
111 break;
112 printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X ", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
113#endif
114#if 1 // Resolve adapter operation state by using it´s name
116 status = brawcap_adapter_operation(name, &operationState);
117 if(!BRAWCAP_SUCCESS(status))
118 break;
119 switch (operationState)
120 {
121 case BRAWCAP_ADAPTER_OPERATION_UNKNOWN: printf("Operation: %s ", "Unknown"); break;
122 case BRAWCAP_ADAPTER_OPERATION_DOWN: printf("Operation: %s ", "Down"); break;
123 case BRAWCAP_ADAPTER_OPERATION_TESTING: printf("Operation: %s ", "Testing"); break;
124 case BRAWCAP_ADAPTER_OPERATION_UP: printf("Operation: %s ", "Up"); break;
125 }
126#endif
127#if 1 // Resolve adapter connection state by using it´s name
129 status = brawcap_adapter_connection(name, &connectionState);
130 if(!BRAWCAP_SUCCESS(status))
131 break;
132 switch (connectionState)
133 {
134 case BRAWCAP_ADAPTER_CONNECTION_UNKNOWN: printf("Connection: %s ", "Unknown"); break;
135 case BRAWCAP_ADAPTER_CONNECTION_DOWN: printf("Connection: %s ", "Down"); break;
136 case BRAWCAP_ADAPTER_CONNECTION_UP: printf("Connection: %s ", "Up"); break;
137 }
138#endif
139#if 1 // Resolve adapter IPv4 address by using it´s name
140 brawcap_adapter_ipv4_t ipv4 = {0};
141 status = brawcap_adapter_ipv4(name, &ipv4);
142 if(!BRAWCAP_SUCCESS(status))
143 break;
144 printf("IPv4: %u.%u.%u.%u ", ipv4.bytes[0], ipv4.bytes[1], ipv4.bytes[2], ipv4.bytes[3]);
145#endif
146#if 1 // Resolve adapter IPv6 address by using it´s name
147 brawcap_adapter_ipv6_t ipv6 = {0};
148 status = brawcap_adapter_ipv6(name, &ipv6);
149 if(!BRAWCAP_SUCCESS(status))
150 break;
151 printf("IPv6: %x:%x:%x:%x:%x:%x:%x:%x ", ipv6.words[0], ipv6.words[1], ipv6.words[2], ipv6.words[3],
152 ipv6.words[4], ipv6.words[5], ipv6.words[6], ipv6.words[7]);
153#endif
154#if 1 // Resolve adapter max transmission unit (MTU) size by using it´s name
155 brawcap_adapter_mtu_t mtu = 0;
156 status = brawcap_adapter_mtu(name, &mtu);
157 if(!BRAWCAP_SUCCESS(status))
158 break;
159 printf("MTU: %llu ", mtu);
160#endif
161#if 1 // Resolve adapter receive speed (uplink speed) by using it´s name
163 status = brawcap_adapter_speed_rx(name, &rxSpeed);
164 if(!BRAWCAP_SUCCESS(status))
165 break;
166 printf("RX Speed: %d MBit/s ", rxSpeed);
167#endif
168#if 1 // Resolve adapter transmit speed (uplink speed) by using it´s name
170 status = brawcap_adapter_speed_tx(name, &txSpeed);
171 if(!BRAWCAP_SUCCESS(status))
172 break;
173 printf("TX Speed: %d MBit/s\n\n", txSpeed);
174#endif
175 // We have allocated memory above...
176 // So we should not forget to release it.
177 //
178 // NOTE: Here we have still a memory leak if
179 // loop exits due to a break after allocation.
180 // But it is only in case of a failure and we ignore it
181 // because of simplicity of the example...
182 free(friendlyName);
183 free(description);
184 }
185#endif
186 }while(0);
187
188 // We do not handle anything unexpected...
189 // Instead just print the last unexpected status to console and leave.
190 if(!BRAWCAP_SUCCESS(status))
191 {
192 printf("Unexpected status returned: %d\n", status);
193 return -1;
194 }
195 return 0;
196}
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_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_list_at(const brawcap_adapter_count_t index, brawcap_adapter_name_t name)
Reads out the adapter name of the adapter at the adapter list index.
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_list_count(brawcap_adapter_count_t *const pCount)
Reads out the current number of supported adapters in the adapter list.
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_status_t brawcap_adapter_description(const brawcap_adapter_name_t name, char *const description, UINT32 *const pLength)
Reads out the description of the specified adapter.
UINT8 brawcap_adapter_mac_t[BRAWCAP_ADAPTER_MAC_LENGTH]
Fixed size array describing a MAC address.
Definition: brawcap_types_um.h:249
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
brawcap_status_t brawcap_adapter_list_update()
Updates the list of supported adapters. This function searches the current machine for supported adap...
brawcap_status_t brawcap_adapter_mac(const brawcap_adapter_name_t name, brawcap_adapter_mac_t mac)
Reads out the MAC address of the specified adapter.
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
UINT8 brawcap_adapter_count_t
Type used for counting the available/supported adapters on a machine.
Definition: brawcap_types_um.h:242
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.
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_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