
#define LOG_TAG    "TH-CDC"

#include <th_cdc.h>
#include "main.h"
#include "usbd_cdc_if.h"
#include "navikit.h"


//global variable
extern uint8_t port_restart[];
extern uint32_t number_restart;


//task attributes
const osThreadAttr_t cdcMonitorTask_attributes = {
  .name = "cdcMonitorTask",
  .priority = (osPriority_t) osPriorityBelowNormal7,
  .stack_size = 256 * 4
};

//task instance
void CdcMonitorTask(void *argument){
	uint8_t port_restart_temp[64]={0};
	uint32_t number_restart_temp =0;
	bool beep_flag = false;
	log_d("Start CDC Monitor Task");
	for(;;){
		if(number_restart){
			number_restart_temp = number_restart;
		    memcpy(port_restart_temp,&port_restart,number_restart_temp<=12 ? number_restart_temp : 12);
			number_restart = 0;
			// turn off usb port device
			for(uint8_t i=0;i<number_restart_temp;i++){
				if(port_restart_temp[i]<12){
					PWR_Enable(port_restart_temp[i], false,200);
					log_i("USB Port%d power off",port_restart_temp[i]);
					CDC_Transmit_FS(&port_restart_temp[i],1);
				}
			}
			//turn on usb port device
			beep_flag = false;
			for(uint8_t i=0;i<number_restart_temp;i++){
				if(port_restart_temp[i]<12){
					PWR_Enable(port_restart_temp[i], true,200);
					log_i("USB Port%d power on",port_restart_temp[i]);
					CDC_Transmit_FS(&port_restart_temp[i],1);
					beep_flag = true;
				}
			}
			if(beep_flag){
				beep_flag = false;
				TaskBeep(50, 1);
			}
		}
		osDelay(100);
	}
}
