#include <math.h>
#include "zstream.h"
#include "message.h"
#include "log.h"

void message_locinfo::zero_this()
{
	memset(this,0,sizeof(*this));
}

void message_locinfo::load(zistream&is,bool tdoa)
{
	zero_this();
	uint8_t b;
	uint32_t i;
	//卡类型、卡号、CT、电池状态
	is>>b>>m_card_id>>m_card_ct>>m_batty_status;
	m_card_type=b;
	m_batty_status&=0xF;

	is>>b;
	if(m_card_type==1)
	{
		m_callinfo=b;
	}
	else
	{
		m_rav=((b&0x80)?-1.:1.)*(b&0x7f)*3;
	}

	//加速度
	is>>b;
	if(m_card_type == 1 || m_card_type==4 || m_card_type==5)
		m_acc=b;
	else
		m_acc =b*0.01;
		//m_acc=((b&0x80)?-1.:1.)*(b&0x7f)*98;

	//TOF
	is>>b>>i; 	
	m_tof=b;
	m_tof=(m_tof<<32)|i;

	//天线号
	is>>m_ant_id;
	--m_ant_id;

	if(tdoa)
	{
		//同步序号，冲击响应丢弃
		is>>m_sync_ct>>skip(2);
	}

	//信号电平值
	uint16_t sp1=0,sp2=0;
	is>>sp1>>sp2;
	m_rssi=10*log10(1.*sp1*(1<<17)/pow(sp2-64.,2))-121.74;
	log_info("timestamp=%llu,type:%d,card_id:%d,ct:%d,status:%d,acc=%d,tof=%llu,ant_id:%d,spq=%d",m_time_stamp,m_card_type,m_card_id,m_card_ct,m_batty_status,m_acc,m_tof,m_ant_id,m_rssi);
}

void message_tdoasync::zero_this()
{
	memset(this,0,sizeof(*this));
}

void message_tdoasync::load(zistream&is)
{
	zero_this();
	uint8_t b;

	//本地分站ID和天线ID
	is>>m_local_site_id>>b; m_local_ant_id=b;

	//上级分站ID和天线ID
	is>>m_parent_site_id>>b; m_parent_ant_id=b;

	//根分站CT和本机级别
	is>>m_sync_ct>>m_local_level;

	uint32_t i;

	//本级发出的时间
	is>>b>>i; 	
	m_send_time=b;
	m_send_time=(m_send_time<<32)|i;

	//本级收到的时间
	is>>b>>i; 	
	m_recv_time=b;
	m_recv_time=(m_recv_time<<32)|i;
}


