I'm trying to read data from the Windows LLDP driver but each call to the ReadFileEx function is returning zero (0) bytes read.
The WaitNSeconds function is defined within a separate dynamic linked library and the "ttime.h" file is the main header file for that library. This means that copying the code to another file will not work unless you have these files.
//File includes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <io.h>
#include <windows.h>
#include "ttime.h"
void *tmpvoid;
DWORD WINAPI MainTimer(void *inobj){
//Variable declarations
unsigned long tmplong = 0;
int *tmpn = 0;
int tmpn2 = 0;
int ret = 0;
//Argument checks
if(inobj == NULL){
return TPTRISNULL;
}
//Initializations
tmpn = (int*)inobj;
//Main logic
//Wait for 20 seconds.
WaitNSeconds(20000);
//Update the argument.
*tmpn = 1;
//Returns
}
void WINAPI PrintDataRead(int dwErrorCode, \
int dwNumberOfBytesTransfered, \
OVERLAPPED *lpOverlapped){
//Variable declarations
char *tmpstr = 0;
char tmpchar = 0;
int ret = 0;
//Argument checks
//Initializations
//Main logic
//Show the data read.
//Prepare variables.
tmpstr = (char*)tmpvoid;
//Get the third byte from the data.
tmpchar = tmpstr[2];
//Check if it is a MAC address subtype.
if(tmpchar == '\x04'){
//Display the MAC address located at
//the fourth byte.
//Append a NULL character at the
//fifteenth byte.
tmpstr[14] = '\0';
//Show the MAC address.
printf("%s\n", (tmpstr + 3));
} //EndIf
}
int main(int argc, char *argv[]){
//Variable declarations
HANDLE tmphnd = 0;
HANDLE tmpthrd = 0;
HANDLE tmpevent = 0;
OVERLAPPED *tmpoverlp = 0;
int *tmpn = 0;
int ret = 0;
//Argument checks
//Initializations
//Main logic
//Create a file object to the LLDP
//driver.
tmphnd = CreateFileA("\\\\.\\LLDPCTRL", \
GENERIC_READ, \
FILE_SHARE_READ, \
NULL, \
OPEN_EXISTING, \
FILE_ATTRIBUTE_READONLY | \
FILE_FLAG_OVERLAPPED, \
NULL);
if(tmphnd == INVALID_HANDLE_VALUE){
printf("\nError: Could not open device.\n");
goto _lblfreeandexit;
}
//Allocate memory to tmpn.
tmpn = (int*)calloc(2, sizeof(int));
if(tmpn == NULL){
printf("\nError: Could not allocate memory to tmpn.\n");
goto _lblfreeandexit;
}
//Initialize tmpn to zero.
*tmpn = 0;
//Allocate memory to the buffer that
//will store the data from the LLDP
//driver.
tmpvoid = (void*)calloc(50, sizeof(char));
if(tmpvoid == NULL){
printf("\nError: Could not allocate memory to the buffer.\n");
goto _lblfreeandexit;
}
//Allocate memory for a new OVERLAPPED
//object.
tmpoverlp = (OVERLAPPED*)calloc(1, sizeof(OVERLAPPED));
if(tmpoverlp == NULL){
printf("\nError: Could not allocate memory to the overlapped object.\n");
goto _lblfreeandexit;
}
//Create the event object.
tmpevent = CreateEvent(NULL, \
TRUE, \
TRUE, \
NULL);
if(tmpevent == NULL){
printf("\nError: Could not create the event object.\n");
goto _lblfreeandexit;
}
//Set the hEvent member of the OVERLAPPED
//object.
tmpoverlp->hEvent = tmpevent;
//Create a new thread.
tmpthrd = CreateThread(NULL, \
0, \
MainTimer, \
tmpn,
0, \
NULL);
if(tmpthrd == NULL){
printf("\nError: Could not create the thread.\n");
goto _lblfreeandexit;
}
//Loop while tmpn is zero.
while(*tmpn == 0){
//Read from the LLDP driver.
ret = ReadFileEx(tmphnd, \
tmpvoid, \
40, \
tmpoverlp, \
PrintDataRead);
if(ret == 0){
printf("\nError: Reading from the file failed.\n");
goto _lblfreeandexit;
}
//Wait for 10 seconds maximum, or
//until the event is signaled.
WaitForSingleObjectEx(tmpevent, \
10000, \
TRUE);
} //End while loop
_lblfreeandexit:
//Close the handles.
if(tmphnd != NULL){
CloseHandle(tmphnd);
}
if(tmpevent != NULL){
CloseHandle(tmpevent);
}
if(tmpthrd != NULL){
CloseHandle(tmpthrd);
}
//Free variables
if(tmpvoid != NULL){
free(tmpvoid);
}
if(tmpn != NULL){
free(tmpn);
}
if(tmpoverlp != NULL){
free(tmpoverlp);
}
//Returns
}
The function runs for twenty (20) seconds but does not print anything. I have assumed that the driver uses the standard LLDP format found in the IEEE 802.AB specification.