event_example.c

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Go to the documentation of this file.

1/* 2 * %NT_SOFTWARE_LICENSE% 3 */ 4 5/** 6 * @example event/event_example.c 7 * @section event_example_description Description 8 * 9 * This source file is an example of how to use the @ref EventStream 10 * "Event stream" interface in NTAPI. 11 * 12 * The following NTAPI functions are used: 13 * - @ref NT_Init() 14 * - @ref NT_EventOpen() 15 * - @ref NT_EventRead() 16 * - @ref NT_EventClose() 17 * - @ref NT_Done() 18 * - @ref NT_ExplainError() 19 * 20 * <hr> 21 * @section event_example_prerequisites Prerequisites 22 * A working system is needed with an Napatech accelerator as adapter 0. 23 * 24 * @section event_example_flow Program flow 25 * @{ 26 * The following is required to use the @ref EventStream "Event stream" 27 * interface in NTAPI: 28 * - \#include/nt.h - Applications/Tools only need to include @ref 29 * nt.h to obtain prototypes, macros etc. from NTAPI. 30 * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI 31 * library. @ref NTAPI_VERSION is a define that describes the version 32 * of the API described in the header files included by @ref 33 * nt.h. NT_Init() will ask the NTAPI library to convert return data 34 * to the @ref NTAPI_VERSION if possible. This will ensure that 35 * applications can run on NTAPI libraries of newer versions. 36 * - @ref NT_EventOpen() - Open an event stream. 37 * - ForceAnEvent() - Trigger an event. 38 * - @ref NT_EventRead() - Read the event information. 39 * - @ref NT_EventClose() - Close the stream when terminating. 40 * - @ref NT_Done() - Close down the NTAPI library. 41 * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions. 42 * 43 *<hr> 44 * @} 45 */ 46 47#include <nt.h> 48 49/* 50 * This function will set the timestamp of adapter 0 which will trigger 51 * a configuration change done event. 52 */ 53static int ForceAnEvent(void) 54{ 55 NtConfigStream_t hConfigStream; // Config stream handle 56 NtConfig_t hConfig; // Config handle 57 char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer 58 int status; // Status variable 59 60 // Open a config stream. This will be used to change the timestamp 61 // of the adapter causing it to generate an event. 62 if((status = NT_ConfigOpen(&hConfigStream, "CfgStream")) != NT_SUCCESS) { 63 // Get the status code as text 64 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 65 fprintf(stderr, "NT_ConfigOpen() failed: %s\n", errorBuffer); 66 return -1; 67 } 68 69 printf("<<< Force an event. >>>\n"); 70 71 hConfig.parm=NT_CONFIG_PARM_ADAPTER_TIMESTAMP; 72 hConfig.u.timestampWrite.adapter=0; // On adapter 0 73 hConfig.u.timestampWrite.data.ts = 0; // set time to 0 74 hConfig.u.timestampWrite.data.bCurrent=0;// and don't use current OS time 75 // Write timestamp=0 to adapter 0 76 if((status = NT_ConfigWrite(hConfigStream, &hConfig)) != NT_SUCCESS) { 77 // Get the status code as text 78 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 79 fprintf(stderr, "NT_ConfigWrite() failed: %s\n", errorBuffer); 80 return -1; 81 } 82 83 // Close the config stream again 84 if((status = NT_ConfigClose(hConfigStream)) != NT_SUCCESS) { 85 // Get the status code as text 86 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 87 fprintf(stderr, "NT_ConfigClose() failed: %s\n", errorBuffer); 88 return -1; 89 } 90 91 return status; 92} 93 94int main(void) 95{ 96 NtEventStream_t hEventStream; // Event stream handle 97 NtEvent_t hEvent; // Event handle 98 char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer 99 int status; // Status variable 100 bool found = false; 101 102 // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library 103 if((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) { 104 // Get the status code as text 105 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 106 fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer); 107 return -1; 108 } 109 110 printf("Generate an event and wait for the event before reading it.\n"); 111 112 // Open the event stream 113 if((status = NT_EventOpen(&hEventStream, "ExampleEvent", NT_EVENT_SOURCE_CONFIG | NT_EVENT_SOURCE_TIMESYNC_STATE_MACHINE)) != NT_SUCCESS) { 114 // Get the status code as text 115 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 116 fprintf(stderr, "NT_EventOpen() failed: %s\n", errorBuffer); 117 return -1; 118 } 119 120 if(ForceAnEvent() != NT_SUCCESS) { 121 return -1; 122 } 123 124 while(1) { 125 // Wait for the events to occur. 126 if((status = NT_EventRead(hEventStream, &hEvent, 10000)) != NT_SUCCESS) { 127 if(found) { 128 printf("No more events received within 10 seconds\n"); 129 break; 130 } 131 else { 132 // Get the status code as text 133 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 134 fprintf(stderr, "NT_EventRead() failed: %s\n", errorBuffer); 135 return -1; 136 } 137 } 138 139 // Print event info. 140 // As we have opened the event stream with the mask NT_EVENT_SOURCE_CONFIG | NT_EVENT_SOURCE_TIMESYNC_STATE_MACHINE 141 // only these to event will be received. 142 switch(hEvent.type) { 143 case NT_EVENT_SOURCE_TIMESYNC_STATE_MACHINE: 144 found = true; 145 // Some adapters will return this event when changing the adapter time 146 if (hEvent.u.timeSyncStateMachineEvent.action == NT_EVENT_TIMESYNC_TIME_STAMP_CLOCK_SET) { 147 printf("Event received: Timestamp clock set: New time: %lld\n", (unsigned long long)hEvent.u.timeSyncStateMachineEvent.timeStampClock); 148 } 149 break; 150 case NT_EVENT_SOURCE_CONFIG: 151 found = true; 152 // Some adapters will return this event when changing the adapter time, 153 // then the command needs to be checked in order to see which command, that 154 // caused the event to be sent. 155 if(hEvent.u.configEvent.parm == NT_CONFIG_PARM_ADAPTER_TIMESTAMP) { 156 printf("Event received: Timestamp config change: New time: %lld\n", (unsigned long long)hEvent.u.configEvent.u.timestampWrite.data.ts); 157 } 158 else { 159 fprintf(stderr, ">>> Unexpected config change received %d.\n", hEvent.u.configEvent.parm); 160 } 161 break; 162 default: 163 fprintf(stderr, ">>> Unexpected event received %d.\n", hEvent.type); 164 break; 165 } 166 } 167 168 // Close the event stream 169 if((status = NT_EventClose(hEventStream)) != NT_SUCCESS) { 170 // Get the status code as text 171 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 172 fprintf(stderr, "NT_EventClose() failed: %s\n", errorBuffer); 173 return -1; 174 } 175 176 // Close down the NTAPI library 177 NT_Done(); 178 179 printf("Done.\n"); 180 return 0; 181}