예제 #1
0
/**
 * Prints a string representation of FieldData to stdout.
 */
void printFieldData(FieldType type, FieldData* pattern) {
	char* s;
	
	switch (type.id) {
		case IPFIX_TYPEID_protocolIdentifier:
			printf("protocolIdentifier:");
			printProtocol(type, pattern);
			break;
		case IPFIX_TYPEID_sourceIPv4Address:
			printf("sourceIPv4Address:");
			printIPv4(type, pattern);
			break;
		case IPFIX_TYPEID_destinationIPv4Address:
			printf("destinationIPv4Address:");
			printIPv4(type, pattern);
			break;				
		case IPFIX_TYPEID_sourceTransportPort:
			printf("sourceTransportPort:");
			printPort(type, pattern);
			break;
		case IPFIX_TYPEID_destinationtransportPort:
			printf("destinationtransportPort:");
			printPort(type, pattern);
			break;
		default:
			s = typeid2string(type.id);
			if (s != NULL) {
				printf("%s:", s);
				printUint(type, pattern);
				} else {
				DPRINTF("Field with ID %d unparseable\n", type.id);
				}
			break;
		}
}
예제 #2
0
/**
 * Prints a string representation of IpfixRecord::Data to stdout.
 */
void printFieldData(IpfixRecord::FieldInfo::Type type, IpfixRecord::Data* pattern) {
	char* s;
	timeval t;
	uint64_t hbnum;

	switch (type.id) {
	case IPFIX_TYPEID_protocolIdentifier:
		printf("protocolIdentifier: ");
		printProtocol(type, pattern);
		break;
	case IPFIX_TYPEID_sourceIPv4Address:
		printf("sourceIPv4Address: ");
		printIPv4(type, pattern);
		break;
	case IPFIX_TYPEID_destinationIPv4Address:
		printf("destinationIPv4Address: ");
		printIPv4(type, pattern);
		break;
	case IPFIX_TYPEID_sourceTransportPort:
		printf("sourceTransportPort: ");
		printPort(type, pattern);
		break;
	case IPFIX_TYPEID_destinationTransportPort:
		printf("destinationTransportPort: ");
		printPort(type, pattern);
		break;
	case IPFIX_TYPEID_flowStartNanoSeconds:
	case IPFIX_TYPEID_flowEndNanoSeconds:
	case IPFIX_ETYPEID_revFlowStartNanoSeconds:
	case IPFIX_ETYPEID_revFlowEndNanoSeconds:
		printf("%s: ", typeid2string(type.id));
		hbnum = ntohll(*(uint64_t*)pattern);
		if (hbnum>0) {
			t = timentp64(*((ntp64*)(&hbnum)));
			printf("%d.%06d seconds", (int32_t)t.tv_sec, (int32_t)t.tv_usec);
		} else {
			printf("no value (only zeroes in field)");
		}
		break;
	case IPFIX_ETYPEID_frontPayload:
	case IPFIX_ETYPEID_revFrontPayload:
		printf("%s: ", typeid2string(type.id));
		printFrontPayload(type, pattern);
		break;
	default:
		s = typeid2string(type.id);
		if (s != NULL) {
			printf("%s: ", s);
			printUint(type, pattern);
		} else {
			DPRINTF("Field with ID %d unparseable\n", type.id);
		}
		break;
	}
}
예제 #3
0
/**
 * prints a datarecord in a special, easy-to-read data format in one line
 */
void IpfixPrinter::printOneLineRecord(IpfixDataRecord* record)
{
	boost::shared_ptr<IpfixRecord::DataTemplateInfo> dataTemplateInfo = record->templateInfo;
		char buf[100], buf2[100];

		if (linesPrinted==0 || linesPrinted>50) {
			printf("%22s %20s %8s %5s %21s %21s %5s %5s\n", "Flow recvd.", "Flow start", "Duratn", "Prot", "Src IP:Port", "Dst IP:Port", "Pckts", "Bytes");
			printf("-----------------------------------------------------------------------------------------------------------------\n");
			linesPrinted = 0;
		}
		struct tm* tm;
		struct timeval tv;
		gettimeofday(&tv, 0);
		tm = localtime(reinterpret_cast<time_t*>(&tv.tv_sec));
		strftime(buf, ARRAY_SIZE(buf), "%F %T", tm);
		snprintf(buf2, ARRAY_SIZE(buf2), "%s.%03ld", buf, tv.tv_usec/1000);
		printf("%22s ", buf2);

		uint32_t timetype = 0;
		uint32_t starttime = 0;
		IpfixRecord::FieldInfo* fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_flowStartSeconds, 0);
		if (fi != NULL) {
			timetype = IPFIX_TYPEID_flowStartSeconds;
			time_t t = ntohl(*reinterpret_cast<time_t*>(record->data+fi->offset));
			starttime = t;
			tm = localtime(&t);
			strftime(buf, 50, "%F %T", tm);
		} else {
			fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_flowStartMilliSeconds, 0);
			if (fi != NULL) {
				timetype = IPFIX_TYPEID_flowStartMilliSeconds;
				uint64_t t2 = ntohll(*reinterpret_cast<uint64_t*>(record->data+fi->offset));
				time_t t = t2/1000;
				starttime = t;
				tm = localtime(&t);
				strftime(buf, 50, "%F %T", tm);
			} else {
				fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_flowStartSysUpTime, 0);
				if (fi != NULL) {
					timetype = IPFIX_TYPEID_flowStartSysUpTime;
					starttime = ntohl(*reinterpret_cast<uint32_t*>(record->data+fi->offset));
					snprintf(buf, 50, "%u:%02u.%04u", starttime/60000, (starttime%60000)/1000, starttime%1000);
				} else {
					fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_flowStartSeconds, 0);
					if (fi != NULL) {
						timetype = IPFIX_TYPEID_flowStartNanoSeconds;
						uint64_t t2 = ntohll(*reinterpret_cast<uint64_t*>(record->data+fi->offset));
						timeval t = timentp64(*((ntp64*)(&t2)));
						tm = localtime(&t.tv_sec);
						strftime(buf, 50, "%F %T", tm);
						starttime = t.tv_sec;
					}
				}
			}
		}
		if (timetype != 0) {
			printf("%20s ", buf);

			uint32_t dur = 0;
			switch (timetype) {
				case IPFIX_TYPEID_flowStartSeconds:
					fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_flowEndSeconds, 0);
					if (fi != NULL) {
						dur = ntohl(*reinterpret_cast<uint32_t*>(record->data+fi->offset)) - starttime;
						dur *= 1000;
					}
					break;
				case IPFIX_TYPEID_flowStartMilliSeconds:
					fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_flowEndMilliSeconds, 0);
					if (fi != NULL) {
						dur = ntohll(*reinterpret_cast<uint64_t*>(record->data+fi->offset)) - starttime;
						dur *= 1000;
					}
					break;
				case IPFIX_TYPEID_flowStartSysUpTime:
					fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_flowEndSysUpTime, 0);
					if (fi != NULL) {
						dur = ntohl(*reinterpret_cast<uint32_t*>(record->data+fi->offset)) - starttime;
						dur *= 1000;
					}
					break;
				case IPFIX_TYPEID_flowStartNanoSeconds:
					fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_flowEndNanoSeconds, 0);
					if (fi != NULL) {
						uint64_t t2 = ntohll(*reinterpret_cast<uint64_t*>(record->data+fi->offset));
						timeval t = timentp64(*((ntp64*)(&t2)));
						dur = t.tv_sec*1000+t.tv_usec/1000 - starttime;
					}
			}
			snprintf(buf, 50, "%u.%04u", (dur)/1000, dur%1000);
			printf("%8s ", buf);
		}
		else {
			printf("%20s %8s ", "---", "---");
		}

		fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_protocolIdentifier, 0);
		if (fi != NULL && fi->type.length==1) {
			snprintf(buf, ARRAY_SIZE(buf), "%hhu", *reinterpret_cast<uint8_t*>(record->data+fi->offset));
		} else {
			snprintf(buf, ARRAY_SIZE(buf), "---");
		}
		printf("%5s ", buf);

		fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_sourceIPv4Address, 0);
		uint32_t srcip = 0;
		if (fi != NULL && fi->type.length>=4) {
			srcip = *reinterpret_cast<uint32_t*>(record->data+fi->offset);
		}
		fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_sourceTransportPort, 0);
		uint32_t srcport = 0;
		if (fi != NULL && fi->type.length==2) {
			srcport = *reinterpret_cast<uint16_t*>(record->data+fi->offset);
		}
		snprintf(buf, ARRAY_SIZE(buf), "%hhu.%hhu.%hhu.%hhu:%hu", (srcip>>0)&0xFF, (srcip>>8)&0xFF, (srcip>>16)&0xFF, (srcip>>24)&0xFF, srcport);
		printf("%21s ", buf);

		fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_destinationIPv4Address, 0);
		uint32_t dstip = 0;
		if (fi != NULL && fi->type.length>=4) {
			dstip = *reinterpret_cast<uint32_t*>(record->data+fi->offset);
		}
		fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_destinationTransportPort, 0);
		uint32_t dstport = 0;
		if (fi != NULL && fi->type.length==2) {
			dstport = *reinterpret_cast<uint16_t*>(record->data+fi->offset);
		}
		snprintf(buf, ARRAY_SIZE(buf), "%hhu.%hhu.%hhu.%hhu:%hu", (dstip>>0)&0xFF, (dstip>>8)&0xFF, (dstip>>16)&0xFF, (dstip>>24)&0xFF, dstport);
		printf("%21s ", buf);

		fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_packetDeltaCount, 0);
		if (fi != NULL) {
			printUint(buf, fi->type, record->data+fi->offset);
		} else {
			snprintf(buf, ARRAY_SIZE(buf), "---");
		}
		printf("%5s ", buf);

		fi = dataTemplateInfo->getFieldInfo(IPFIX_TYPEID_octetDeltaCount, 0);
		if (fi != NULL) {
			printUint(buf, fi->type, record->data+fi->offset);
		} else {
			snprintf(buf, ARRAY_SIZE(buf), "---");
		}
		printf("%5s \n", buf);
		linesPrinted++;
}
예제 #4
0
파일: main.c 프로젝트: eddyem/STM8_samples
int main() {
    unsigned long T = 0L;
    int Ival;
    U8 rb, Num;

    CFG_GCR |= 1; // disable SWIM

    // Configure clocking
    CLK_CKDIVR = 0; // F_HSI = 16MHz, f_CPU = 16MHz

    // Timer 4 (8 bit) used as system tick timer
    // prescaler == 128 (2^7), Tfreq = 125kHz
    // period = 1ms, so ARR = 125
    TIM4_PSCR = 7;
    TIM4_ARR = 125;
    // interrupts: update
    TIM4_IER = TIM_IER_UIE;
    // auto-reload + interrupt on overflow + enable
    TIM4_CR1 = TIM_CR1_APRE | TIM_CR1_URS | TIM_CR1_CEN;

    // Configure pins
    // PC2 - PP output (on-board LED)
    PORT(LED_PORT, DDR) |= LED_PIN;
    PORT(LED_PORT, CR1) |= LED_PIN;
    // PD5 - UART2_TX -- pseudo open-drain output; don't forget an pullup resistor!
    PORT(UART_PORT, DDR) |= UART_TX_PIN;
    PORT(UART_PORT, ODR) |= UART_TX_PIN; // torn off N push-down ???
    //PORT(UART_PORT, CR1) |= UART_TX_PIN;

    // Configure UART
    // 9 bit, no parity, 1 stop (UART_CR3 = 0 - reset value)
    // 57600 on 16MHz: BRR1=0x11, BRR2=0x06
    UART2_BRR1 = 0x11; UART2_BRR2 = 0x06;
    UART2_CR1  = UART_CR1_M; // M = 1 -- 9bits
    UART2_CR2  = UART_CR2_REN | UART_CR2_RIEN; // Allow RX, generate ints on rx

    setup_stepper_pins();


    // enable all interrupts
    enableInterrupts();
    // Loop
    do{
        if((Global_time - T > paused_val) || (T > Global_time)){
            T = Global_time;
            PORT(LED_PORT, ODR) ^= LED_PIN; // blink on-board LED
        }
        if(UART_read_byte(&rb)){ // buffer isn't empty
            switch(rb){
                case 'h': // help
                case 'H':
                    uart_write("\nPROTO:\n"
                        "+/-\tLED period\n"
                        "Ex/ex\tset/get end-switches stored\n"
                        "p\tget HW end-switches\n"
                        "Mx\tstop on end-switch\n"
                        "Sx/sx\tset/get Mspeed\n"
                        "mx\tget steps\n"
                        "Px\tpause/resume\n"
                        "Xx\tstop\n"
                        "0..2N\tmove xth motor for N steps\n"
                        "=\tinfinity moving (after 0..2)"
                        "U/u\tset/get U-stepping\n"
                        "I\tget serial ID\n"
                        "N\tchange HW number\n"
                        "n\tshow HW number\n"
                        );
                break;
                case 'I': // get serial id
                    show_uid();
                break;
                case '+':
                    paused_val += 100;
                    if(paused_val > 10000)
                        paused_val = 500; // but not more than 10s
                break;
                case '-':
                    paused_val -= 100;
                    if(paused_val < 100)  // but not less than 0.1s
                        paused_val = 500;
                break;
                case 'E': // set end-switches value
                    if(get_motor_number(&Num)){
                        if(readInt(&Ival) && (Ival == (Ival & 0x1f))){
                            if(Num)
                                EPs[Num] = Ival & 0x0f; // 4 bits in motors 1&2
                            else
                                EPs[0] = Ival; // all 5 bits in motor 0
                        }else
                            error_msg("bad EP");
                    }
                break;
                case 'e': // get stored end-switches value
                    if(get_motor_number(&Num)){
                        printUint(&EPs[Num], 1);
                    }
                break;
                case 'p': // get hardware end-switches value
                    if(get_motor_number(&Num)){
                        Num = get_ep_value(Num);
                        printUint(&Num, 1);
                    }
                break;
                case 'S': // set stepper speed
                    if(get_motor_number(&Num)){
                        if(readInt(&Ival) && Ival > MIN_STEP_LENGTH)
                            set_stepper_speed(Num, Ival);
                        else
                            error_msg("bad speed");
                    }
                break;
                case 's': // get stepper speed
                    if(get_motor_number(&Num))
                        printUint((U8*)&Stepper_speed[Num], 2);
                break;
                case 'M': // move till EP, you can call it before starting motor
                    if(get_motor_number(&Num))
                        Stop_on_EP[Num] = 1;
                break;
                case 'm': // how much steps there is to the end of moving
                    if(get_motor_number(&Num))
                        printUint((U8*)&Nsteps[Num], 2);
                break;
                case 'X': // stop
                    if(get_motor_number(&Num))
                        stop_motor(Num);
                break;
                case 'P': // pause/resume
                    if(get_motor_number(&Num))
                        pause_resume(Num);
                break;
                case 'N':
                    if(readInt(&Ival) && Ival > 0 && Ival < 256)
                        if(!change_progmem_value(&UART_devNUM, (unsigned int) Ival))
                            error_msg("can't change val");
                break;
                case 'n': // show HW num
                    printUint(&UART_devNUM, 1);
                break;
                case 'u': // show UStepping
                    printUint(&USteps, 1);
                break;
                case 'U': // set UStepping
                    if(readInt(&Ival) && Ival > 0 && Ival < 256)
                        USteps = Ival;
                break;
                case '=': // infinity moving: just don't decrement steps
                    StepperInfty = 1;
                break;
                default:
                    if(rb >= '0' && rb <= '2'){ // run motor
                        Num = rb - '0';
                        if(readInt(&Ival) && Ival)
                            move_motor(Num, Ival);
                        else{
                            error_msg("bad Nsteps");
                        }
                    }
            }
        }
    }while(1);
}
예제 #5
0
/**
 * Prints a string representation of IpfixRecord::Data to stdout.
 */
void PrintHelpers::printFieldData(InformationElement::IeInfo type, IpfixRecord::Data* pattern) {

	timeval t;
	uint64_t hbnum;
	string typeStr = type.toString();

	// try to get the values aligned
	if (typeStr.length() < 60)
		fprintf(fh, "%-60s: ", type.toString().c_str());
	else
		fprintf(fh, "%s: ", type.toString().c_str());

	switch (type.enterprise) {
		case 0:
			switch (type.id) {
				case IPFIX_TYPEID_protocolIdentifier:
					printProtocol(type, pattern);
					return;
				case IPFIX_TYPEID_sourceIPv4Address:
					printIPv4(type, pattern);
					return;
				case IPFIX_TYPEID_destinationIPv4Address:
					printIPv4(type, pattern);
					return;
				case IPFIX_TYPEID_sourceTransportPort:
					printPort(type, pattern);
					return;
				case IPFIX_TYPEID_destinationTransportPort:
					printPort(type, pattern);
					return;
				case IPFIX_TYPEID_flowStartSeconds:
				case IPFIX_TYPEID_flowEndSeconds:
				case IPFIX_TYPEID_flowStartMilliSeconds:
				case IPFIX_TYPEID_flowEndMilliSeconds:
				case PSAMP_TYPEID_observationTimeSeconds:
					printLocaltime(type, pattern);
					return;
				case IPFIX_TYPEID_flowStartNanoSeconds:
				case IPFIX_TYPEID_flowEndNanoSeconds:
					hbnum = ntohll(*(uint64_t*)pattern);
					if (hbnum>0) {
						t = timentp64(*((ntp64*)(&hbnum)));
						fprintf(fh, "%u.%06d seconds", (int32_t)t.tv_sec, (int32_t)t.tv_usec);
					} else {
						fprintf(fh, "no value (only zeroes in field)");
					}
					return;
			}
			break;

		case IPFIX_PEN_reverse:
			switch (type.id) {
				case IPFIX_TYPEID_flowStartNanoSeconds:
				case IPFIX_TYPEID_flowEndNanoSeconds:
					hbnum = ntohll(*(uint64_t*)pattern);
					if (hbnum>0) {
						t = timentp64(*((ntp64*)(&hbnum)));
						fprintf(fh, "%u.%06d seconds", (int32_t)t.tv_sec, (int32_t)t.tv_usec);
					} else {
						fprintf(fh, "no value (only zeroes in field)");
					}
					return;
			}
			break;

		default:
		{
			if (type==InformationElement::IeInfo(IPFIX_ETYPEID_frontPayload, IPFIX_PEN_vermont) ||
				type==InformationElement::IeInfo(IPFIX_ETYPEID_frontPayload, IPFIX_PEN_vermont|IPFIX_PEN_reverse)) {
				printFrontPayload(type, pattern);
				return;
			}
		}
	}

	printUint(type, pattern);
}
예제 #6
0
int main() {
	unsigned long T = 0L;
	int Ival;
	U8 rb;
	CFG_GCR |= 1; // disable SWIM
	// Configure clocking
	CLK_CKDIVR = 0; // F_HSI = 16MHz, f_CPU = 16MHz

	// Configure timer 1 - systick
	// prescaler = f_{in}/f_{tim1} - 1
	// set Timer1 to 1MHz: 1/1 - 1 = 15
	TIM1_PSCRH = 0;
	TIM1_PSCRL = 15; // LSB should be written last as it updates prescaler
	// auto-reload each 1ms: TIM_ARR = 1000 = 0x03E8
	TIM1_ARRH = 0x03;
	TIM1_ARRL = 0xE8;
	// interrupts: update
	TIM1_IER = TIM_IER_UIE;
	// auto-reload + interrupt on overflow + enable
	TIM1_CR1 = TIM_CR1_APRE | TIM_CR1_URS | TIM_CR1_CEN;

	// Configure pins
	// PC2 - PP output (on-board LED)
	PORT(LED_PORT, DDR) |= LED_PIN;
	PORT(LED_PORT, CR1) |= LED_PIN;
	// PD5 - UART2_TX
	PORT(UART_PORT, DDR) |= UART_TX_PIN;
	PORT(UART_PORT, CR1) |= UART_TX_PIN;

	// Configure UART
	// 8 bit, no parity, 1 stop (UART_CR1/3 = 0 - reset value)
	// 57600 on 16MHz: BRR1=0x11, BRR2=0x06
	UART2_BRR1 = 0x11; UART2_BRR2 = 0x06;
	UART2_CR2 = UART_CR2_TEN | UART_CR2_REN | UART_CR2_RIEN; // Allow RX/TX, generate ints on rx

	// enable all interrupts
	enableInterrupts();

	set_stepper_speed(1000);
	setup_stepper_pins();

	// Loop
	do{
		if((Global_time - T > paused_val) || (T > Global_time)){
			T = Global_time;
			PORT(LED_PORT, ODR) ^= LED_PIN; // blink on-board LED
		}
		if(UART_read_byte(&rb)){ // buffer isn't empty
			switch(rb){
				case 'h': // help
				case 'H':
					uart_write("\nPROTO:\n+/-\tLED period\nS/s\tset/get Mspeed\n"
					"m\tget steps\nx\tstop\np\tpause/resume\nM\tmove motor\na\tadd Nstps\n"
					"u\tunipolar motor\nb\tbipolar motor\n");
				break;
				case '+':
					paused_val += 100;
					if(paused_val > 10000)
						paused_val = 500; // but not more than 10s
				break;
				case '-':
					paused_val -= 100;
					if(paused_val < 100)  // but not less than 0.1s
						paused_val = 500;
				break;
				case 'S': // set stepper speed
					if(readInt(&Ival) && Ival > MIN_STEP_LENGTH)
						set_stepper_speed(Ival);
					else
						error_msg("bad speed");
				break;
				case 's': // get stepper speed
					printUint((U8*)&Stepper_speed, 2);
				break;
				case 'm': // how much steps there is to the end of moving
					printUint((U8*)&Nsteps, 4);
				break;
				case 'M': // move motor
					if(Nsteps){
						error_msg("moving!");
						break;
					}
					if(readInt(&Ival) && Ival)
						move_motor(Ival);
					else{
						error_msg("bad Nsteps");
					}
				break;
				case 'x': // stop
					stop_motor();
				break;
				case 'p': // pause/resume
					pause_resume();
				break;
				case 'a': // add N steps
					if(readInt(&Ival) && Ival){
						add_steps(Ival);
					}else{
						error_msg("bad value");
					}
				break;
				case 'u': // unipolar
					usteps = ustepsUNI;
				break;
				case 'b': // bipolar
					usteps = ustepsBIP;
				break;
			}
		}
	}while(1);
}