#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

void help()
{
	printf(	"Usage:	checker file_name\n");

}

#define PKT_SIZE	1024
#define MAX_VAL		0x3FFF

int main(int argc, char *argv[])
{
	FILE *fp = NULL;
	uint8_t buf[PKT_SIZE];
	char *fn = NULL;
	uint16_t val = 0;
	uint16_t exp_val = 0;
	uint16_t *ptr = NULL;
	int ret = 0;
	int pos = 0;
	struct pkt_t *pktp = NULL;
	printf("*** Cecilie RDMA application - data checker ***\n");

	if(argc != 2){
		printf("Not enough arguments\n");
		help();
		return 1;
	}

	fn = argv[1];
	printf("File name: %s\n", fn);
	memset(buf, 0x00, sizeof(buf));

	fp = fopen(fn, "rb");
	if(!fp){
		printf("Couldn't open file %s\n", fn);
		return 1;	
	}

	
	while((ret = fread(buf, sizeof(uint8_t), sizeof(buf), fp)) == sizeof(buf)){
		if(ptr == NULL){
			ptr = (uint16_t*)buf;
			exp_val = ptr[0];
		}
		ptr = (uint16_t*)buf;
		for(int i = 0; i < sizeof(buf)/sizeof(uint16_t); i++){
			if(ptr[i] != exp_val){
				printf("Received value is not same as expected value od position %08x (val %04x != exp %04x)\n", pos*2, ptr[i], exp_val);
				exp_val = ptr[i];
			}
			exp_val++;
			if(exp_val > MAX_VAL)
				exp_val = 0;
			pos++;
		}
	}
	
	fclose(fp);
	printf("Received data are OK\n");
	return 0;
}

