tRadioDriver *Radio = 0;
uint8_t cmpBuf[256] ;
OS_Q  lora_q;

void lora_send(char *tx_data, uint32_t tx_len)
{
	char *p_msg = NULL;
	uint32_t msg_len;
	OS_ERR      err;	

	/* 接受消息, 发往net_proc模块统一处理 */
	p_msg = (char *)net_queue_mem_calloc();	
	if(p_msg) {
		memcpy(p_msg, tx_data, tx_len);
		OS_QPost(&lora_q, p_msg, msg_len, OS_OPT_POST_FIFO, 0, &err);
	}
}

uint32_t lora_recv(char *tx_data, uint32_t *tx_len)
{
	int8_t snr = 0;
	double rssi=0;
	
	*tx_len = SX1276_Read_Data(tx_data ,100, &snr, &rssi);\
	if(*tx_len > 0) {
		if(memcmp(cmpBuf, tx_data, *tx_len)){//和上次数据不同，数据有效			
			memcpy(cmpBuf, tx_data, *tx_len);
		} else {
			return 0;
		}
	}
	
	return *tx_len;
}

void lora_task(void)
{
	inner_msg_format_t *p_msg = NULL;
	uint32_t msg_len;
	OS_ERR      err;	

	/* 接受消息, 发往net_proc模块统一处理 */
	p_msg = (inner_msg_format_t *)net_queue_mem_calloc();
	if(p_msg) {
		lora_recv(p_msg, &msg_len);
		if(msg_len) {
			net_queue_insert((char *)p_msg, msg_len);
		} else {
			net_queue_mem_free(p_msg);
		}
	}

	/* 发送消息 */
	while(p_msg = (inner_msg_format_t *)OSQPend(&lora_q, 0, OS_OPT_PEND_NON_BLOCKING, &msg_len, NULL, &err)) {	/* none blocking. */
		if(p_msg&&msg_len) {
			SX1276_Send_Data_Listen_Ch((char *)p_msg, msg_len & 0xFF, 300, 2);
			//发送完成后重新开启接收
			Radio->StartRx();
		}
	}	
}

void lora_init(char *device_id)
{
	int i = 0, temp ;
	OS_ERR      err;	
	temp = 0;

	OSQCreate(&lora_q, "lora_queue", 32, &err);//创建网络消息处理队列	
	Radio = RadioDriverInit();
	if(Radio == NULL) {
		printf("RADIO is not defined!!!! ");
		return -1;
	}
	Radio->Init();               			
	Radio->StartRx();
	
	return temp;
}
