This example shows how to create a bRAWcap packet buffer, fill it with packets, sort the packets inside the buffer by their timestamp or payload and verify the sorting.
43#define TIME_DIFF_MS(start, end) ((double)(end - start) * 1000.0 / CLOCKS_PER_SEC)
59 UINT64 a_ns = ((UINT64)a_secs * 1000000000ULL) + a_nsecs;
60 UINT64 b_ns = ((UINT64)b_secs * 1000000000ULL) + b_nsecs;
61 if (a_ns < b_ns)
return -1;
62 if (a_ns > b_ns)
return 1;
67static int compare_seqid(
const unsigned char* a,
const unsigned char* b, UINT8 length) {
68 UINT32 va = 0, vb = 0;
69 for (UINT8 i = 0; i < length; ++i) {
70 va |= ((UINT32)a[i]) << (8 * i);
71 vb |= ((UINT32)b[i]) << (8 * i);
73 if (va < vb)
return -1;
74 if (va > vb)
return 1;
79bool verify_sort(
brawcap_buffer_t* pBuffer,
int type,
int order, UINT32 seqid_offset, UINT8 seqid_length) {
82 printf(
"[VERIFY] Could not get buffer count!\n");
89 printf(
"[VERIFY] Could not get packet at index %llu or %llu!\n", (
unsigned long long)(i-1), (
unsigned long long)i);
97 cmp = compare_timestamps(tPrev, tCurr);
99 const unsigned char *payloadPrev = NULL, *payloadCurr = NULL;
103 if (payloadPrev == NULL || payloadCurr == NULL ||
104 seqid_offset + seqid_length > lenPrev || seqid_offset + seqid_length > lenCurr) {
105 printf(
"[VERIFY] Invalid payload or seqid range!\n");
108 cmp = compare_seqid(payloadPrev + seqid_offset, payloadCurr + seqid_offset, seqid_length);
110 if ((order == 0 && cmp > 0) || (order == 1 && cmp < 0)) {
111 printf(
"[VERIFY] Sort error at index %llu!\n", (
unsigned long long)i);
118int main(
int argc,
char** argv)
121 const uint32_t packet_count = 10000;
122 const uint16_t payload_length = 1000;
123 const struct seqid seqid = { 520, 8 };
124 const bool verify =
true;
127 SetConsoleTitleA(
"bRAWcap Example - Packet Buffer Sorting");
130 clock_t t_start, t_end;
134 srand((
unsigned int)time(NULL));
141 unsigned char* pPayload = (
unsigned char*)malloc(payload_length);
143 if(seqid.length != 1 && seqid.length != 2 && seqid.length != 4 && seqid.length != 8) {
144 printf(
"[ERROR] Unsupported seqid length: %u. Supported lengths are 1, 2, 4 or 8 bytes.\n", seqid.length);
148 printf(
"[INFO] Configuration: \n");
149 printf(
"\t - Packets: %u\n", packet_count);
150 printf(
"\t - Payload Length: %u\n", payload_length);
151 printf(
"\t - SeqId Offset: %u\n", seqid.offset);
152 printf(
"\t - SeqId Length: %u\n", seqid.length);
155 for (
size_t i = 0; i < payload_length; ++i)
157 pPayload[i] = (
unsigned char)(rand() % 256);
164 printf(
"[ERROR] Failed to create bRAWcap packet buffer. Status: %d\n",
brawcap_last_status());
167 printf(
"[INFO] Created bRAWcap packet buffer.\n");
185 timespec_get(&ts, TIME_UTC);
188 if (seqid.offset + seqid.length < payload_length)
191 (seqid.length == 1) ? 0xFF :
192 (seqid.length == 2) ? 0xFFFF :
193 (seqid.length == 4) ? 0xFFFFFFFF :
194 (seqid.length == 8) ? 0xFFFFFFFFFFFFFFFF : 0;
195 seqid_counter = (seqid_counter + 1) & max_val;
196 for (
size_t i = 0; i < seqid.length; ++i) {
197 pPayload[seqid.offset + i] = (
unsigned char)((seqid_counter >> (8 * i)) & 0xFF);
225 elapsed_us = TIME_DIFF_MS(t_start, t_end);
226 printf(
"[INFO] Sorted buffer by timestamp rising order. Time: %.0f ms\n", elapsed_us);
228 printf(
"[VERIFY] Timestamp rising: %s\n", verify_sort(pBuffer, 0, 0, seqid.offset, seqid.length) ?
"OK" :
"FAILED");
245 elapsed_us = TIME_DIFF_MS(t_start, t_end);
246 printf(
"[INFO] Sorted buffer by timestamp falling order. Time: %.0f ms\n", elapsed_us);
248 printf(
"[VERIFY] Timestamp falling: %s\n", verify_sort(pBuffer, 0, 1, seqid.offset, seqid.length) ?
"OK" :
"FAILED");
261 printf(
"[ERROR] Failed to sort buffer by payload seqid. Status: %d\n",
brawcap_last_status());
265 elapsed_us = TIME_DIFF_MS(t_start, t_end);
266 printf(
"[INFO] Sorted buffer by payload seqid (rising order). Time: %.0f ms\n", elapsed_us);
268 printf(
"[VERIFY] Payload seqid rising: %s\n", verify_sort(pBuffer, 1, 0, seqid.offset, seqid.length) ?
"OK" :
"FAILED");
281 printf(
"[ERROR] Failed to sort buffer by payload seqid. Status: %d\n",
brawcap_last_status());
285 elapsed_us = TIME_DIFF_MS(t_start, t_end);
286 printf(
"[INFO] Sorted buffer by payload seqid (falling order). Time: %.0f ms\n", elapsed_us);
288 printf(
"[VERIFY] Payload seqid falling: %s\n", verify_sort(pBuffer, 1, 1, seqid.offset, seqid.length) ?
"OK" :
"FAILED");
brawcap_status_t brawcap_last_status()
Reads the last status appeared in bRAWcap, for the calling thread.
@ BRAWCAP_STATUS_SUCCESS
Definition: brawcap_types_shared.h:142
struct _brawcap_timestamp brawcap_timestamp_t
bRAWcap timestamp object.
Definition: brawcap_types_shared.h:608
brawcap_status_t brawcap_timestamp_value_ns_get(brawcap_timestamp_t *const pTimestamp, UINT64 *const pSeconds, UINT32 *const pNanoseconds)
Reads out the timestamp value in seconds and nanoseconds.
brawcap_status_t brawcap_timestamp_value_ns_set(brawcap_timestamp_t *const pTimestamp, const UINT64 seconds, const UINT32 nanoseconds)
Sets the timestamp value in seconds and nanoseconds.
UINT16 brawcap_packet_size_t
Type for handling the number of payload bytes per packet.
Definition: brawcap_types_shared.h:666
brawcap_status_t brawcap_packet_create(brawcap_packet_t **const pPacket, const brawcap_packet_size_t maxSize)
Creates a new packet.
brawcap_status_t brawcap_packet_payload_set_v2(brawcap_packet_t *const pPacket, const unsigned char *const pPayload, const brawcap_packet_size_t length)
Sets the packet payload of the specified packet.
struct _brawcap_packet brawcap_packet_t
bRAWcap packet object.
Definition: brawcap_types_shared.h:675
brawcap_status_t brawcap_packet_payload_get_v2(brawcap_packet_t *const pPacket, const unsigned char **const pPayload, brawcap_packet_size_t *const pLength)
Reads out the payload of the specified packet.
brawcap_status_t brawcap_packet_timestamp_get(brawcap_packet_t *const pPacket, brawcap_timestamp_t **const pTimestamp)
Reads out the timestamp object for the specified packet.
struct _brawcap_buffer brawcap_buffer_t
bRAWcap packet buffer object.
Definition: brawcap_types_shared.h:746
brawcap_status_t brawcap_buffer_sort(brawcap_buffer_t *const pBuffer, const brawcap_buffer_sort_order_t order, const brawcap_buffer_sort_types_t type, const brawcap_packet_size_t offset, const brawcap_packet_size_t length, const brawcap_buffer_sort_endian_t endian)
Sorts packets in a buffer by timestamp or by a payload field.
brawcap_status_t brawcap_buffer_add_back(brawcap_buffer_t *const pBuffer, const brawcap_packet_t *const pPacket)
Adds the specified packet at the end of the specified packet buffer.
brawcap_status_t brawcap_buffer_free(brawcap_buffer_t *pBuffer)
Frees the specified packet buffer. When this function is called the specified packet buffer becomes i...
UINT32 brawcap_buffer_packet_count_t
Type for handling the number of packets which can be stored in a packet buffer.
Definition: brawcap_types_shared.h:738
brawcap_status_t brawcap_buffer_at_index(brawcap_buffer_t *const pBuffer, const brawcap_buffer_packet_count_t index, brawcap_packet_t **const pPacket)
Reads out a buffered packet at the index of the specified packet buffer.
brawcap_status_t brawcap_buffer_count(brawcap_buffer_t *const pBuffer, brawcap_buffer_packet_count_t *const pPacketCount)
Reads out the number of currently buffered packets in the specified packet buffer.
brawcap_status_t brawcap_buffer_create(brawcap_buffer_t **const pBuffer, const brawcap_packet_size_t maxPacketPayloadSize, const brawcap_buffer_packet_count_t numPackets)
Creates a new packet buffer.
@ BRAWCAP_BUFFER_SORT_ORDER_RISING
Rising sort order.
Definition: brawcap_types_um.h:224
@ BRAWCAP_BUFFER_SORT_ORDER_FALLING
Falling sort order.
Definition: brawcap_types_um.h:225
@ BRAWCAP_BUFFER_SORT_TYPE_PAYLOAD
Definition: brawcap_types_um.h:237
@ BRAWCAP_BUFFER_SORT_TYPE_TIMESTAMP
Definition: brawcap_types_um.h:236
@ BRAWCAP_BUFFER_SORT_ENDIAN_LITTLE
Definition: brawcap_types_um.h:246