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

This example shows how to open and close bRAWcap handles to each available adapter on the machine. If any unexpected status is returned, it will close all handles and print the unexpected status to console. Therefore brawcap_last_status is used.

1/**
2 * @file 03_generic_handle_opener.c
3 *
4 * @brief bRAWcap Example - Demonstrates the usage of the generic bRAWcap functions.
5 *
6 * It shows how to:
7 * - Open a bRAWcap handle
8 * - Close a previously opened handle
9 * - Check for unexpected status by using @ref brawcap_last_status.
10 *
11 * This example makes use of C only.
12 *
13 * @version 1.0
14 *
15 * @date 2023-03-28
16 *
17 * @copyright
18 * <b> © 2021 - b-plus technologies GmbH. All rights reserved!</b>
19 *
20 * All rights exclusively reserved for b-plus GmbH, unless expressly otherwise agreed.
21 *
22 * Redistribution in source or any other form, with or without modification, is not permitted.
23 *
24 * You may use this code under the according license terms of b-plus.
25 * Please contact b-plus at services@b-plus.com to get the appropriate terms and conditions.
26 */
27// Include bRAWcap
28#include "libbrawcap.h"
29
30// C STD
31#include <stdlib.h> // for malloc & free
32#include <stdio.h> // for printf
33
34int main(int argc, char** argv)
35{
36 // Set console title
37 SetConsoleTitleA("bRAWcap Example - Handle Opener");
38
39 brawcap_handle_t** pHandleArray = 0;
40 brawcap_adapter_count_t numberAdapters = 0;
42
43 do
44 {
45#if 1 // Get number of available adapters
46 status = brawcap_adapter_list_count(&numberAdapters);
47 if(!BRAWCAP_SUCCESS(status))
48 break;
49#endif
50
51#if 1 // Allocate array for storing opened bRAWcap handles
52 pHandleArray = (brawcap_handle_t**) calloc(numberAdapters, sizeof(brawcap_handle_t*));
53 memset(pHandleArray, 0, numberAdapters);
54#endif
55
56#if 1 // Get name for each available adapter and try to open a bRAWcap handle
57 for(int index = 0; index < numberAdapters; ++index)
58 {
59 brawcap_adapter_name_t name = { '\0'};
60 status = brawcap_adapter_list_at(index, name);
61 if(!BRAWCAP_SUCCESS(status))
62 break;
63
64 status = brawcap_open(name, &pHandleArray[index]);
65 if(!BRAWCAP_SUCCESS(status))
66 break;
67
68 printf("%02d. bRAWcap handle (%p) opened to %s.\n", index + 1, pHandleArray[index], name);
69 }
70 #endif
71 }while(0);
72
73 printf("\n");
74
75 // Close all opened handles
76 for(int index = numberAdapters - 1; index >= 0; --index)
77 {
78 if (pHandleArray[index])
79 {
80 status = brawcap_close(pHandleArray[index]);
81 printf("%02d. bRAWcap handle (%p) closed.\n", index + 1, pHandleArray[index]);
82 }
83 }
84
85 // Free alloacted handle array
86 free(pHandleArray);
87
88 // If anything unexpected was returned,
89 // request the status again and print it to console.
90#if 1
91 if(!BRAWCAP_SUCCESS(status))
92 {
93 printf("Unexpected status occurred: %d", brawcap_last_status());
94 return -1;
95 }
96#endif
97
98 return 0;
99}
bRAWcap main header.
struct _brawcap_handle brawcap_handle_t
A bRAWcap handle.
Definition: brawcap_types_um.h:173
brawcap_status_t brawcap_close(brawcap_handle_t *pHandle)
Closes the specified bRAWcap handle.
brawcap_status_t brawcap_open(const brawcap_adapter_name_t name, brawcap_handle_t **const pHandle)
Opens a new bRAWcap handle on the adapter, specified by it´s name.
brawcap_status_t brawcap_last_status()
Reads the last status appeared in bRAWcap, for the calling thread.
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_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_list_count(brawcap_adapter_count_t *const pCount)
Reads out the current number of supported adapters in the adapter list.
char brawcap_adapter_name_t[BRAWCAP_ADAPTER_NAME_LENGTH]
Fixed size array containing a adapter name.
Definition: brawcap_types_um.h:257
UINT8 brawcap_adapter_count_t
Type used for counting the available/supported adapters on a machine.
Definition: brawcap_types_um.h:242