int main(int argc, char *argv[]) { char user_input[BUF_MAX]; int i; int connected = 0; // 0 means unconnected, 1 means connected pthread_t g_connector; if (argc != 5) { fprintf (stderr, "Usage: %s [gbn|sr] [window_size] [loss_rate] [corruption_rate]\n", argv[0]); exit(1); } char * protocol = argv[1]; int windowSize = atoi (argv[2]); double lossRate = atof(argv[3]); // loss rate as percentage i.e. 50% = .50 if (lossRate > 1.0f && lossRate < 0) { fprintf(stderr, "lossRate must between 0 and 1\n"); exit(1); } double corruptionRate = atof(argv[4]); // corruption rate as percentage i.e. 50% = .50 if (corruptionRate > 1.0f && corruptionRate < 0) { fprintf(stderr, "corruptionRate must between 0 and 1\n"); exit(1); } datalink_init(protocol, windowSize, lossRate, corruptionRate); pthread_create(&g_connector, NULL, &server_loop, NULL); while (1) { printf("> "); // prompt memset(user_input, 0, BUF_MAX); char* ret = fgets(user_input, BUF_MAX, stdin); if (ret == NULL) { continue; } user_input[strlen(user_input) - 1] = '\0'; if (user_input[0] == '/') { parse_control_command(user_input); continue; } else { if (strcmp(strip(user_input), "") == 0) { continue; } printf("%s: Command not found. Type '%s' for more information.\n", user_input, HELP); continue; } } return 0; }
int main( int argc, char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; unsigned timeout = 100; /* milliseconds */ (void) argc; (void) argv; Device_Set_Object_Instance_Number(126); Init_Service_Handlers(); RTOS_Initialize(); /* init the physical layer */ #ifdef BACDL_MSTP dlmstp_set_my_address(0x05); #endif datalink_init(NULL); Send_I_Am(&Handler_Transmit_Buffer[0]); /* loop forever */ for (;;) { /* input */ /* returns 0 bytes on timeout */ pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); /* process */ if (pdu_len) { npdu_handler(&src, &Rx_Buf[0], pdu_len); } /* output */ /* blink LEDs, Turn on or off outputs, etc */ } }
/** Initialize the DataLink configuration from Environment variables, * or else to defaults. * @ingroup DataLink * The items configured depend on which BACDL_ the code is built for, * eg, BACDL_BIP. * * For most items, checks first for an environment variable, and, if * found, uses that to set the item's value. Otherwise, will set * to a default value. * * The Environment Variables, by BACDL_ type, are: * - BACDL_ALL: (the general-purpose solution) * - BACNET_DATALINK to set which BACDL_ type we are using. * - (Any): * - BACNET_APDU_TIMEOUT - set this value in milliseconds to change * the APDU timeout. APDU Timeout is how much time a client * waits for a response from a BACnet device. * - BACNET_APDU_RETRIES - indicate the maximum number of times that * an APDU shall be retransmitted. * - BACNET_IFACE - set this value to dotted IP address (Windows) of * the interface (see ipconfig command on Windows) for which you * want to bind. On Linux, set this to the /dev interface * (i.e. eth0, arc0). Default is eth0 on Linux, and the default * interface on Windows. Hence, if there is only a single network * interface on Windows, the applications will choose it, and this * setting will not be needed. * - BACDL_BIP: (BACnet/IP) * - BACNET_IP_PORT - UDP/IP port number (0..65534) used for BACnet/IP * communications. Default is 47808 (0xBAC0). * - BACNET_BBMD_PORT - UDP/IP port number (0..65534) used for Foreign * Device Registration. Defaults to 47808 (0xBAC0). * - BACNET_BBMD_TIMETOLIVE - number of seconds used in Foreign Device * Registration (0..65535). Defaults to 60000 seconds. * - BACNET_BBMD_ADDRESS - dotted IPv4 address of the BBMD or Foreign * Device Registrar. * - BACDL_MSTP: (BACnet MS/TP) * - BACNET_MAX_INFO_FRAMES * - BACNET_MAX_MASTER * - BACNET_MSTP_BAUD * - BACNET_MSTP_MAC */ void dlenv_init( void) { char *pEnv = NULL; #if defined(BACDL_ALL) pEnv = getenv("BACNET_DATALINK"); if (pEnv) { datalink_set(pEnv); } else { datalink_set(NULL); } #endif #if defined(BACDL_BIP) #if defined(BIP_DEBUG) BIP_Debug = true; #endif pEnv = getenv("BACNET_IP_PORT"); if (pEnv) { bip_set_port(htons((uint16_t) strtol(pEnv, NULL, 0))); } else { /* BIP_Port is statically initialized to 0xBAC0, * so if it is different, then it was programmatically altered, * and we shouldn't just stomp on it here. * Unless it is set below 1024, since: * "The range for well-known ports managed by the IANA is 0-1023." */ if (ntohs(bip_get_port()) < 1024) bip_set_port(htons(0xBAC0)); } #elif defined(BACDL_MSTP) pEnv = getenv("BACNET_MAX_INFO_FRAMES"); if (pEnv) { dlmstp_set_max_info_frames(strtol(pEnv, NULL, 0)); } else { dlmstp_set_max_info_frames(1); } pEnv = getenv("BACNET_MAX_MASTER"); if (pEnv) { dlmstp_set_max_master(strtol(pEnv, NULL, 0)); } else { dlmstp_set_max_master(127); } pEnv = getenv("BACNET_MSTP_BAUD"); if (pEnv) { dlmstp_set_baud_rate(strtol(pEnv, NULL, 0)); } else { dlmstp_set_baud_rate(38400); } pEnv = getenv("BACNET_MSTP_MAC"); if (pEnv) { dlmstp_set_mac_address(strtol(pEnv, NULL, 0)); } else { dlmstp_set_mac_address(127); } #endif pEnv = getenv("BACNET_APDU_TIMEOUT"); if (pEnv) { apdu_timeout_set((uint16_t) strtol(pEnv, NULL, 0)); } else { #if defined(BACDL_MSTP) apdu_timeout_set(60000); #endif } pEnv = getenv("BACNET_APDU_RETRIES"); if (pEnv) { apdu_retries_set((uint8_t) strtol(pEnv, NULL, 0)); } if (!datalink_init(getenv("BACNET_IFACE"))) { exit(1); } #if (MAX_TSM_TRANSACTIONS) pEnv = getenv("BACNET_INVOKE_ID"); if (pEnv) { tsm_invokeID_set((uint8_t) strtol(pEnv, NULL, 0)); } #endif dlenv_register_as_foreign_device(); }
int main(void) { char wdrst = 0; uint8_t mcusr; mcusr = MCUSR; if ( mcusr & _BV(WDRF) ) { wdrst = 1; } MCUSR = 0; wdt_disable(); cli(); #ifdef LED1 LED1 = 1; #endif uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(115200,F_CPU)); stdout = &mystdout; sei(); if ( wdrst ) { kputs("\n***WATCHDOG RESET***\n"); } wdt_enable(WDTO_8S); wdt_reset(); kputs("\nSetting DDR registers\n"); DDRA = DDRA_SETTING; DDRB = DDRB_SETTING; DDRC = DDRC_SETTING; DDRD = DDRD_SETTING; DIDR0 = 0; DIDR1 = 0; /* kputs("Hi i2c\n"); i2c_init(); kputs("Hi humid\n"); humid_init(); humid_sleep(); kputs("Hi accel\n"); accel_init(); accel_sleep(); kputs("Hi light\n"); light_init(); light_sleep(); kputs("Hi PIR\n"); pir_wake(); kputs("Hi RTC\n"); rtctimer_init(); kputs("Go away RTC\n"); DDRC &= 0x3F; PORTC |= 0xC0; kputs("Go to sleep\n"); uart_flush(); ACSR = (1<<ACD); LED1 = LED2 = 0; cli(); wdt_disable(); SMCR = (2<<SM0) | (1<<SE); //Enable Power-Down Mode while(1) { asm volatile ("sleep"); // __sleep(); // Go to sleep } */ kputs("Initializing PCINT\n"); pcint_init(); kputs("Initializing Reports\n"); report_init(); board_init_devices(); kputs("Initializing wireless mote\n"); wdt_reset(); datalink_init(); /* PIR_VCC = 1; while(1) { if(PIR_OUT_PIN) { kputs("ON\n"); } else { kputs("OFF\n"); } _delay_ms(50); }*/ kputs("Powering down all devices\n"); board_power_down_devices(); kputs("Init logic subsystem\n"); logic_init(); /* kputs("Turning on always-on devices\n"); pir_wake(); accel_wake(); */ board_setup_reporting(); kputs("Starting RTC clock\n"); rtctimer_init(); rtctimer_set_periodic_alarm(report_interval_needed(),&rtc_timer_cb); wdt_reset(); wdt_disable(); while(1) { //wdt_enable(WDTO_2S); //wdt_reset(); pcint_check(); rtctimer_check_alarm(); wdt_disable(); datalink_tick(); #ifdef USE_PN532 rtc_timer_cb(); // causes monitor list to be run #else #if defined LOW_POWER // do we have a RTC clock and battery-powered avr_sleep(); #else avr_doze(); #endif #endif } }