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