/**
 * Copyright (C) 2021 Eric Martin
 * 
 * This file is part of wec-pcap-utils.
 * 
 * wec-pcap-utils is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * wec-pcap-utils is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with wec-pcap-utils.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "radar_4g.h"
#include "radar_pcap.h"

/**
 * @brief Get the packet headers 
 * 
 * @param packet location of the packet to analyze
 * @param ip_header location of the start of the ip header in memory
 * @param payload location of the payload in memory
 */
void pcap_packet_headers(const uint8_t *packet, const uint8_t **ip_header, const uint8_t **payload)
{

	/* Header lengths in bytes */
	uint8_t ethernet_header_length = 14; /* Doesn't change */
	uint8_t ip_header_length;
	uint8_t payload_length;

	/* Find start of IP header */
	*ip_header = packet + ethernet_header_length;
	/* The second-half of the first byte in ip_header
	   contains the IP header length (IHL). */
	ip_header_length = ((**ip_header) & 0x0F);
	/* The IHL is number of 32-bit segments. Multiply
	   by four to get a byte count for pointer arithmetic */
	ip_header_length = ip_header_length * 4;
	//printf("IP header length (IHL) in bytes: %d\n", ip_header_length);

	*payload = packet + ethernet_header_length + ip_header_length;
}

int radar_packet_channel(const uint8_t *packet)
{

	const uint8_t *ip_header;
	const uint8_t *payload;
	pcap_packet_headers(packet, &ip_header, &payload);

	uint32_t *dest = (uint32_t *)(ip_header + 16);

	switch (*dest)
	{
	case CH_A_DATA_DEST_IP:
		return CH_A_DATA;
	case CH_B_DATA_DEST_IP:
		return CH_B_DATA;
	case CH_A_REPORT_DEST_IP:
		return CH_A_REPORT;
	case CH_B_REPORT_DEST_IP:
		return CH_B_REPORT;
	default:
		return CH_UNKNOWN;
	}
}

uint16_t radar_packet_id(const uint8_t *packet)
{
	const uint8_t *ip_header;
	const uint8_t *payload;
	pcap_packet_headers(packet, &ip_header, &payload);
	uint16_t packet_id = (uint16_t)(*(ip_header + 4) << 8) + (*(ip_header + 5));
	return packet_id;
}

uint16_t radar_packet_offset(const uint8_t *packet)
{
	const uint8_t *ip_header;
	const uint8_t *payload;
	pcap_packet_headers(packet, &ip_header, &payload);
	uint16_t offset = (uint16_t)(((*(ip_header + 6) & 0x7) << 8) + (*(ip_header + 7))) * 8;
	return offset;
}

void print_radar_packet_info(const uint8_t *packet)
{
	// Print the Source / Desitination from the IPv4 Header
	const uint8_t *ip_header;
	const uint8_t *payload;
	pcap_packet_headers(packet, &ip_header, &payload);

	printf("[SRC] %i.%i.%i.%i ", *(ip_header + 12), *(ip_header + 13), *(ip_header + 14), *(ip_header + 15));
	printf("\t[DST] %i.%i.%i.%i ", *(ip_header + 16), *(ip_header + 17), *(ip_header + 18), *(ip_header + 19));

	uint8_t flags = pcap_packet_is_fragment(packet);
	int channel = radar_packet_channel(packet);
	printf("\t[CH] %i", channel);
	printf("\t[ID] %04x \t[OFFSET] %u \t [FLAGS] %02X", radar_packet_id(packet), radar_packet_offset(packet), flags);
}

void print_radar_report_2C99(const uint8_t *packet)
{
	const uint8_t *ip_header;
	const uint8_t *payload;
	const uint8_t *data;
	pcap_packet_headers(packet, &ip_header, &payload);

	data = payload + 8;

	RadarReport_02C4_99_t *report;
	report = (RadarReport_02C4_99_t *)data;

	printf("%u,", radar_packet_channel(packet));
	printf("%u,", report->range);
	printf("%u,", report->gain * 100 / 255);
	printf("%u,", report->sea_auto);
	printf("%u,", report->sea * 100 / 255);
	printf("%u,", report->rain * 100 / 255);
	printf("%u,", report->interference_rejection);
	printf("%u,", report->target_expansion);
	printf("%u", report->target_boost);
}

void print_radar_line_header(const uint8_t *packet)
{
	const uint8_t *ip_header;
	const uint8_t *payload;
	const uint8_t *data;
	pcap_packet_headers(packet, &ip_header, &payload);

	data = payload + 8;
}

uint8_t pcap_packet_is_fragment(const uint8_t *packet)
{
	const uint8_t *ip_header;
	const uint8_t *payload;
	pcap_packet_headers(packet, &ip_header, &payload);

	uint8_t flags = (uint8_t)(*(ip_header + 6) & 0xE0) >> 5;
	// we only care about the first bit.
	flags &= 0x01;
	return flags;
}


void decode_radar_frame(struct radar_frame_pkt * frame, struct timeval ts, uint8_t data[SCANS_PER_FRAME][SPOKELEN], double  time[SCANS_PER_FRAME], double angle[SCANS_PER_FRAME], int range[SCANS_PER_FRAME]) {
	struct radar_line * l;
	uint8_t buff, nib_a, nib_b;
	for (int i=0; i< SCANS_PER_FRAME; i++) {
		l = &(frame->lines[i]);
		time[i] = (double)(ts.tv_sec) + (double)(ts.tv_usec/1e6);
		angle[i] = line_angle_degrees(l);
		range[i] = line_range(l);
		
		// csv file the nibbles
		for (int j=0; j<SPOKELEN/2;j++) {
			buff = 0;
			buff = l->data[j];
			nib_a = (buff & 0xF0) >> 4;
			nib_b = (buff & 0x0F);
			data[i][j*2] = nib_a;
			data[i][j*2+1] = nib_b;
		}
	}
}

void print_radar_frame(struct radar_frame_pkt * frame, struct timeval ts, char channel)
{
	struct radar_line * l;
	uint8_t buff, nib_a, nib_b;
	for (int i=0; i< SCANS_PER_FRAME; i++) {
		l = &(frame->lines[i]);
		printf("%li.%06i,", ts.tv_sec, ts.tv_usec);
		printf("%c,%i,%i,%i,%f,%i,",channel,l->br4g.scan_number,line_heading(l), line_angle(l), line_angle_degrees(l), line_range(l));
		// csv file the nibbles
		for (int j=0; j<SPOKELEN/2;j++) {
			buff = 0;
			buff = l->data[j];
			nib_a = (buff & 0xF0) >> 4;
			nib_b = (buff & 0x0F);
			printf("%i,%i,",nib_a, nib_b);
		}
		printf("\r\n");
	}
}
