Example #1
0
void handler_who_is(
    uint8_t * service_request,
    uint16_t service_len,
    BACNET_ADDRESS * src)
{
    int len = 0;
    int32_t low_limit = 0;
    int32_t high_limit = 0;

    (void) src;
    len =
        whois_decode_service_request(service_request, service_len, &low_limit,
        &high_limit);
    if (len == 0)
        Send_I_Am(&Handler_Transmit_Buffer[0]);
    else if (len != -1) {
        /* is my device id within the limits? */
        if (((Device_Object_Instance_Number() >= (uint32_t) low_limit) &&
                (Device_Object_Instance_Number() <= (uint32_t) high_limit))
            ||
            /* BACnet wildcard is the max instance number - everyone responds */
            ((BACNET_MAX_INSTANCE >= (uint32_t) low_limit) &&
                (BACNET_MAX_INSTANCE <= (uint32_t) high_limit)))
            Send_I_Am(&Handler_Transmit_Buffer[0]);
    }

    return;
}
Example #2
0
/**************************************************************************
* Description: handles recurring task
* Returns: none
* Notes: none
**************************************************************************/
void bacnet_task(void)
{
    struct mstp_rx_packet pkt = {{0}};
    bool pdu_available = false;

    /* hello, World! */
    if (Device_ID != Device_Object_Instance_Number()) {
        Device_ID = Device_Object_Instance_Number();
        Send_I_Am(&Handler_Transmit_Buffer[0]);
    }
    /* handle the timers */
    if (timer_interval_expired(&DCC_Timer)) {
        timer_interval_reset(&DCC_Timer);
        dcc_timer_seconds(DCC_CYCLE_SECONDS);
        led_on_interval(LED_DEBUG,500);
    }
    if (timer_interval_expired(&TSM_Timer)) {
        timer_interval_reset(&TSM_Timer);
        tsm_timer_milliseconds(timer_interval(&TSM_Timer));
    }
    reinit_task();
	bacnet_test_task();
    /* handle the messaging */
    if ((!dlmstp_send_pdu_queue_full()) &&
        (!Ringbuf_Empty(&Receive_Queue))) {
        Ringbuf_Pop(&Receive_Queue, (uint8_t *)&pkt);
        pdu_available = true;
    }
    if (pdu_available) {
        led_on_interval(LED_APDU,125);
        npdu_handler(&pkt.src, &pkt.buffer[0], pkt.length);
    }
}
Example #3
0
void main(
    void)
{
    RCONbits.NOT_POR = 1;
    RCONbits.NOT_RI = 1;
    Hardware_Initialize();
    Initialize_Variables();
    /* initialize BACnet Data Link Layer */
    dlmstp_set_my_address(42);
    dlmstp_set_max_info_frames(1);
    dlmstp_set_max_master(127);
    RS485_Set_Baud_Rate(38400);
    dlmstp_init();
    /* Handle anything that needs to be done on powerup */
    /* Greet the BACnet world! */
    Send_I_Am(&Handler_Transmit_Buffer[0]);
    /* Main loop */
    while (TRUE) {
        RESTART_WDT();
        dlmstp_task();
        MainTasks();
        Global_Int(INT_ENABLED);
        ENABLE_TIMER4_INT();
    }
}
Example #4
0
int main(
    void)
{
    uint16_t pdu_len = 0;
    BACNET_ADDRESS src; /* source address */
    uint8_t pdu[MAX_MPDU];      /* PDU data */

    Timer_Init();
    bacnet_init();
    /* broadcast an I-Am on startup */
    Send_I_Am(&Handler_Transmit_Buffer[0]);
    for (;;) {
        millisecond_timer();
        if (!DCC_Timer) {
            dcc_timer_seconds(1);
            DCC_Timer = 1000;
        }
        /* BACnet handling */
        pdu_len = datalink_receive(&src, &pdu[0], MAX_MPDU, 0);
        if (pdu_len) {
#ifndef DLMSTP_TEST
            npdu_handler(&src, &pdu[0], pdu_len);
#endif
        }
    }
}
Example #5
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 */
    BACNET_ADDRESS my_address, broadcast_address;

    (void) argc;
    (void) argv;
    Device_Set_Object_Instance_Number(4194300);
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    datalink_get_broadcast_address(&broadcast_address);
    print_address("Broadcast", &broadcast_address);
    datalink_get_my_address(&my_address);
    print_address("Address", &my_address);
    printf("BACnet stack running...\n");
    /* 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);
        }
        if (I_Am_Request) {
            I_Am_Request = false;
            Send_I_Am(&Handler_Transmit_Buffer[0]);
        } else if (Who_Is_Request) {
            Who_Is_Request = false;
            Send_WhoIs(-1, -1);
        } else {
            Read_Properties();
        }

        /* output */

        /* blink LEDs, Turn on or off outputs, etc */

        /* wait for ESC from keyboard before quitting */
        if (kbhit() && (getch() == 0x1B))
            break;
    }

    print_address_cache();

    return 0;
}
Example #6
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 */
    }
}
Example #7
0
/** Main function of server demo.
 *
 * @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
 *      datalink_receive, npdu_handler,
 *      dcc_timer_seconds, bvlc_maintenance_timer,
 *      Load_Control_State_Machine_Handler, handler_cov_task,
 *      tsm_timer_milliseconds
 *
 * @param argc [in] Arg count.
 * @param argv [in] Takes one argument: the Device Instance #.
 * @return 0 on success.
 */
int main(
    int argc,
    char *argv[])
{
    BACNET_ADDRESS src = {
        0
    };  /* address where message came from */
    uint16_t pdu_len = 0;
    unsigned timeout = 1;       /* milliseconds */
    time_t last_seconds = 0;
    time_t current_seconds = 0;
    uint32_t elapsed_seconds = 0;
    uint32_t elapsed_milliseconds = 0;
    uint32_t address_binding_tmr = 0;
    uint32_t recipient_scan_tmr = 0;
    int uci_id = 0;
    float val_f, pval_f;
    int val_i, pval_i;
    struct uci_context *ctx;
    time_t chk_mtime = 0;
    time_t ucimodtime_bacnet_bi = 0;
    time_t ucimodtime_bacnet_av = 0;
    time_t ucimodtime_bacnet_ao = 0;
    time_t ucimodtime_bacnet_mv = 0;
    int uci_idx = 0;
    char *section;
    char *type;
    char *pEnv = NULL;
    int rewrite;

    pEnv = getenv("UCI_SECTION");
    ctx = ucix_init("bacnet_dev");
#if PRINT_ENABLED
    if(!ctx)
        fprintf(stderr,  "Failed to load config file bacnet_dev\n");
#endif
    uci_id = ucix_get_option_int(ctx, "bacnet_dev", pEnv, "id", 0);
    if (uci_id != 0) {
        Device_Set_Object_Instance_Number(uci_id);
    } else {
        /* allow the device ID to be set */
        if (argc > 1)
            Device_Set_Object_Instance_Number(strtol(argv[1], NULL, 0));
    }
    if(ctx)
        ucix_cleanup(ctx);

#if PRINT_ENABLED
    printf("BACnet Server with uci\n" "BACnet Stack Version %s\n"
        "BACnet Device ID: %u\n" "Max APDU: %d\n", BACnet_Version,
        Device_Object_Instance_Number(), MAX_APDU);
#endif
    /* load any static address bindings to show up
       in our device bindings list */
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    /* configure the timeout values */
    last_seconds = time(NULL);
    /* broadcast an I-Am on startup */
    Send_I_Am(&Handler_Transmit_Buffer[0]);
    /* loop forever */
    for (;;) {
        /* input */
        current_seconds = time(NULL);

        /* 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);
        }
        /* at least one second has passed */
        elapsed_seconds = (uint32_t) (current_seconds - last_seconds);
        if (elapsed_seconds) {
            last_seconds = current_seconds;
            dcc_timer_seconds(elapsed_seconds);
#if defined(BACDL_BIP) && BBMD_ENABLED
            bvlc_maintenance_timer(elapsed_seconds);
#endif
            dlenv_maintenance_timer(elapsed_seconds);
            Load_Control_State_Machine_Handler();
            elapsed_milliseconds = elapsed_seconds * 1000;
            handler_cov_timer_seconds(elapsed_seconds);
            tsm_timer_milliseconds(elapsed_milliseconds);
            trend_log_timer(elapsed_seconds);
#if defined(INTRINSIC_REPORTING)
            Device_local_reporting();
#endif
        }
        handler_cov_task();
        /* scan cache address */
        address_binding_tmr += elapsed_seconds;
        if (address_binding_tmr >= 60) {
            address_cache_timer(address_binding_tmr);
            address_binding_tmr = 0;
        }
#if defined(INTRINSIC_REPORTING)
        /* try to find addresses of recipients */
        recipient_scan_tmr += elapsed_seconds;
        if (recipient_scan_tmr >= NC_RESCAN_RECIPIENTS_SECS) {
            Notification_Class_find_recipient();
            recipient_scan_tmr = 0;
        }
#endif
#if false
        /* output */
        rewrite++;
        if (rewrite>10000) {
            rewrite=0;
            printf("rewrite %i\n", rewrite);
        }
        /* update Analog Value from uci */
        section = "bacnet_av";
        type = "av";
        chk_mtime = 0;
        chk_mtime = check_uci_update(section, ucimodtime_bacnet_av);
        if ( rewrite == 0) {
            chk_mtime = ucimodtime_bacnet_av;
#if PRINT_ENABLED
            printf("rewrite %i\n", rewrite);
#endif
        }
        if(chk_mtime != 0) {
            sleep(1);
            ucimodtime_bacnet_av = chk_mtime;
#if PRINT_ENABLED
            printf("Config changed, reloading %s\n",section);
#endif
            ctx = ucix_init(section);
            struct uci_itr_ctx itr;
            value_tuple_t *cur;
            itr.list = NULL;
            itr.section = section;
            itr.ctx = ctx;
            ucix_for_each_section_type(ctx, section, type,
                (void *)load_value, &itr);
            for( cur = itr.list; cur; cur = cur->next ) {
#if PRINT_ENABLED
                printf("section %s idx %s \n", section, cur->idx);
#endif
                val_f = strtof(cur->value,NULL);
                uci_idx = atoi(cur->idx);
#if PRINT_ENABLED
                printf("idx %s ",cur->idx);
                printf("value %s\n",cur->value);
#endif
                pval_f = Analog_Value_Present_Value(uci_idx);
                if ( val_f != pval_f ) {
                    Analog_Value_Present_Value_Set(uci_idx,val_f,16);
                }
                if (cur->Out_Of_Service == 0) {
                    if (Analog_Value_Out_Of_Service(uci_idx))
                        Analog_Value_Out_Of_Service_Set(uci_idx,0);
                    if (Analog_Value_Reliability(uci_idx))
                        Analog_Value_Reliability_Set(uci_idx,
                            RELIABILITY_NO_FAULT_DETECTED);
                } else {
#if PRINT_ENABLED
                    printf("idx %s ",cur->idx);
                    printf("Out_Of_Service\n");
#endif
                    Analog_Value_Out_Of_Service_Set(uci_idx,1);
                    Analog_Value_Reliability_Set(uci_idx,
                        RELIABILITY_COMMUNICATION_FAILURE);
                }
            }
            ucix_cleanup(ctx);
        }
        /* update end */
        /* update Analog Value from uci */
        section = "bacnet_ao";
        type = "ao";
        chk_mtime = 0;
        chk_mtime = check_uci_update(section, ucimodtime_bacnet_ao);
        if ( rewrite == 0) {
            chk_mtime = ucimodtime_bacnet_ao;
        }
        if(chk_mtime != 0) {
            sleep(1);
            ucimodtime_bacnet_ao = chk_mtime;
#if PRINT_ENABLED
            printf("Config changed, reloading %s\n",section);
#endif
            ctx = ucix_init(section);
            struct uci_itr_ctx itr;
            value_tuple_t *cur;
            itr.list = NULL;
            itr.section = section;
            itr.ctx = ctx;
            ucix_for_each_section_type(ctx, section, type,
                (void *)load_value, &itr);
            for( cur = itr.list; cur; cur = cur->next ) {
#if PRINT_ENABLED
                printf("section %s idx %s \n", section, cur->idx);
#endif
                val_f = strtof(cur->value,NULL);
                uci_idx = atoi(cur->idx);
#if PRINT_ENABLED
                printf("idx %s ",cur->idx);
                printf("value %s\n",cur->value);
#endif
                pval_f = Analog_Output_Present_Value(uci_idx);
                if ( val_f != pval_f ) {
                    Analog_Output_Present_Value_Set(uci_idx,val_f,16);
                }
                if (cur->Out_Of_Service == 0) {
                    if (Analog_Output_Out_Of_Service(uci_idx))
                        Analog_Output_Out_Of_Service_Set(uci_idx,0);
                    if (Analog_Output_Reliability(uci_idx))
                        Analog_Output_Reliability_Set(uci_idx,
                            RELIABILITY_NO_FAULT_DETECTED);
                } else {
#if PRINT_ENABLED
                    printf("idx %s ",cur->idx);
                    printf("Out_Of_Service\n");
#endif
                    Analog_Output_Out_Of_Service_Set(uci_idx,1);
                    Analog_Output_Reliability_Set(uci_idx,
                        RELIABILITY_COMMUNICATION_FAILURE);
                }
            }
            ucix_cleanup(ctx);
        }
        /* update end */

        /* update Multistate Value from uci */
        section = "bacnet_mv";
        type = "mv";
        chk_mtime = 0;
        chk_mtime = check_uci_update(section, ucimodtime_bacnet_mv);
        if ( rewrite == 0) {
            chk_mtime = ucimodtime_bacnet_mv;
        }
        if(chk_mtime != 1) {
            ucimodtime_bacnet_mv = chk_mtime;
#if PRINT_ENABLED
            printf("Config changed, reloading %s\n",section);
#endif
            ctx = ucix_init(section);
            struct uci_itr_ctx itr;
            value_tuple_t *cur;
            itr.list = NULL;
            itr.section = section;
            itr.ctx = ctx;
            ucix_for_each_section_type(ctx, section, type,
                (void *)load_value, &itr);
            for( cur = itr.list; cur; cur = cur->next ) {
#if PRINT_ENABLED
                printf("section %s idx %s \n", section, cur->idx);
#endif
                val_f = strtof(cur->value,NULL);
                uci_idx = atoi(cur->idx);
                if (val_f || !strcmp(cur->value, "0")) {
#if PRINT_ENABLED
                    printf("idx %s ",cur->idx);
                    printf("value %s\n",cur->value);
#endif
                    pval_f = Multistate_Value_Present_Value(uci_idx);
                    if ( val_f != pval_f ) {
                        Multistate_Value_Present_Value_Set(uci_idx,val_f,16);
                    }
                    if (Multistate_Value_Out_Of_Service(uci_idx))
                        Multistate_Value_Out_Of_Service_Set(uci_idx,0);
                    if (Multistate_Value_Reliability(uci_idx))
                        Multistate_Value_Reliability_Set(uci_idx,
                            RELIABILITY_NO_FAULT_DETECTED);
                } else {
#if PRINT_ENABLED
                    printf("idx %s ",cur->idx);
                    printf("Out_Of_Service\n");
#endif
                    Multistate_Value_Out_Of_Service_Set(uci_idx,1);
                    Multistate_Value_Reliability_Set(uci_idx,
                        RELIABILITY_COMMUNICATION_FAILURE);
                }
                if (cur->Out_Of_Service == 0) {
                    if (Multistate_Value_Out_Of_Service(uci_idx))
                        Multistate_Value_Out_Of_Service_Set(uci_idx,0);
                    if (Multistate_Value_Reliability(uci_idx))
                        Multistate_Value_Reliability_Set(uci_idx,
                            RELIABILITY_NO_FAULT_DETECTED);
                } else {
#if PRINT_ENABLED
                    printf("idx %s ",cur->idx);
                    printf("Out_Of_Service\n");
#endif
                    Multistate_Value_Out_Of_Service_Set(uci_idx,1);
                    Multistate_Value_Reliability_Set(uci_idx,
                        RELIABILITY_COMMUNICATION_FAILURE);
                }
            }
            ucix_cleanup(ctx);
        }
        /* update end */

        /* update Binary Input from uci */
        section = "bacnet_bi";
        type = "bi";
        chk_mtime = 0;
        chk_mtime = check_uci_update(section, ucimodtime_bacnet_bi);
        if ( rewrite == 0) {
            chk_mtime = ucimodtime_bacnet_bi;
        }
        if(chk_mtime != 0) {
            ucimodtime_bacnet_bi = chk_mtime;
#if PRINT_ENABLED
            printf("Config changed, reloading %s\n",section);
#endif
            ctx = ucix_init(section);
            struct uci_itr_ctx itr;
            value_tuple_t *cur;
            itr.list = NULL;
            itr.section = section;
            itr.ctx = ctx;
            ucix_for_each_section_type(ctx, section, type,
                (void *)load_value, &itr);
            for( cur = itr.list; cur; cur = cur->next ) {
#if PRINT_ENABLED
                printf("section %s idx %s \n", section, cur->idx);
#endif
                if (cur->value) {
#if PRINT_ENABLED
                    printf("idx %s ",cur->idx);
                    printf("value %s\n",cur->value);
#endif
                    val_i = atoi(cur->value);
                    uci_idx = atoi(cur->idx);
                    pval_i = Binary_Input_Present_Value(uci_idx);
                    if ( val_i != pval_i ) {
                        Binary_Input_Present_Value_Set(uci_idx,val_i,16);
                    }
                }
                if (cur->Out_Of_Service == 0) {
                    if (Binary_Input_Out_Of_Service(uci_idx))
                        Binary_Input_Out_Of_Service_Set(uci_idx,0);
                    if (Binary_Input_Reliability(uci_idx))
                        Binary_Input_Reliability_Set(uci_idx,
                            RELIABILITY_NO_FAULT_DETECTED);
                } else {
#if PRINT_ENABLED
                    printf("idx %s ",cur->idx);
                    printf("Out_Of_Service\n");
#endif
                    Binary_Input_Out_Of_Service_Set(uci_idx,1);
                    Binary_Input_Reliability_Set(uci_idx,
                        RELIABILITY_COMMUNICATION_FAILURE);
                }
            }
            ucix_cleanup(ctx);
        }
        /* update end */
#endif
        /* blink LEDs, Turn on or off outputs, etc */
    }

    return 0;
}
Example #8
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 */
    unsigned count = 0; /* milliseconds */
    time_t start_time;
    time_t new_time = 0;

    start_time = time(NULL);    /* get current time */
    /* Linux specials */
    signal(SIGINT, sig_handler);
    signal(SIGHUP, sig_handler);
    signal(SIGTERM, sig_handler);
    /* setup this BACnet Server device */
    Device_Set_Object_Instance_Number(111);
    Init_Service_Handlers();
    dlenv_init();
    /* loop forever */
    for (;;) {
        /* input */
        new_time = time(NULL);
        /* 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);
        }
        if (new_time > start_time) {
            tsm_timer_milliseconds(new_time - start_time * 1000);
            start_time = new_time;
        }
        if (I_Am_Request) {
            I_Am_Request = false;
            Send_I_Am(&Handler_Transmit_Buffer[0]);
        } else if (Who_Is_Request) {
            Who_Is_Request = false;
            Send_WhoIs(-1, -1);
        }
        /* output */
        /* some round robin task switching */
        count++;
        switch (count) {
            case 1:
                /* used for testing, but kind of noisy on the network */
                /*Read_Properties(); */
                break;
            case 2:
                break;
            default:
                count = 0;
                break;
        }

        /* blink LEDs, Turn on or off outputs, etc */
    }

    return 0;
}
Example #9
0
/** Main function of server demo.
 *
 * @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
 *      datalink_receive, npdu_handler,
 *      dcc_timer_seconds, bvlc_maintenance_timer,
 *      handler_cov_task,
 *      tsm_timer_milliseconds
 *
 * @param argc [in] Arg count.
 * @param argv [in] Takes one argument: the Device Instance #.
 * @return 0 on success.
 */
int main(
    int argc,
    char *argv[])
{
    BACNET_ADDRESS src = {
        0
    };  /* address where message came from */
    uint16_t pdu_len = 0;
    unsigned timeout = 1;       /* milliseconds */
    time_t last_seconds = 0;
    time_t current_seconds = 0;
    uint32_t elapsed_seconds = 0;
    uint32_t elapsed_milliseconds = 0;
    uint32_t address_binding_tmr = 0;
        
    /* allow the device ID to be set */
    if (argc > 1) {
        Device_Set_Object_Instance_Number(strtol(argv[1], NULL, 0));
    }
    printf("BACnet Raspberry Pi PiFace Digital Demo\n" 
        "BACnet Stack Version %s\n"
        "BACnet Device ID: %u\n" 
        "Max APDU: %d\n", BACnet_Version,
        Device_Object_Instance_Number(), MAX_APDU);
    /* load any static address bindings to show up
       in our device bindings list */
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    piface_init();
    atexit(piface_cleanup);
    /* configure the timeout values */
    last_seconds = time(NULL);
    /* broadcast an I-Am on startup */
    Send_I_Am(&Handler_Transmit_Buffer[0]);
    /* loop forever */
    for (;;) {
        /* input */
        current_seconds = time(NULL);

        /* 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);
        }
        /* at least one second has passed */
        elapsed_seconds = (uint32_t) (current_seconds - last_seconds);
        if (elapsed_seconds) {
            last_seconds = current_seconds;
            dcc_timer_seconds(elapsed_seconds);
#if defined(BACDL_BIP) && BBMD_ENABLED
            bvlc_maintenance_timer(elapsed_seconds);
#endif
            dlenv_maintenance_timer(elapsed_seconds);
            elapsed_milliseconds = elapsed_seconds * 1000;
            handler_cov_timer_seconds(elapsed_seconds);
            tsm_timer_milliseconds(elapsed_milliseconds);
        }
        handler_cov_task();
        /* scan cache address */
        address_binding_tmr += elapsed_seconds;
        if (address_binding_tmr >= 60) {
            address_cache_timer(address_binding_tmr);
            address_binding_tmr = 0;
        }
        /* output/input */
        piface_task();
    }

    return 0;
}
Example #10
0
int main(
    int argc,
    char *argv[])
{
    BACNET_ADDRESS src = {
        0
    };  /* address where message came from */
    uint16_t pdu_len = 0;
    unsigned timeout = 1;       /* milliseconds */
    time_t last_seconds = 0;
    time_t current_seconds = 0;
    uint32_t elapsed_seconds = 0;
    uint32_t elapsed_milliseconds = 0;
    uint32_t address_binding_tmr = 0;
    uint32_t recipient_scan_tmr = 0;
    uint16_t count = 0;

    /* allow the device ID to be set */
    if (argc > 1)
        Device_Set_Object_Instance_Number(strtol(argv[1], NULL, 0));
    printf("BACnet Server Demo\n" "BACnet Stack Version %s\n"
        "BACnet Device ID: %u\n" "Max APDU: %d\n", BACnet_Version,
        Device_Object_Instance_Number(), MAX_APDU);
    /* load any static address bindings to show up
       in our device bindings list */
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    /* configure the timeout values */
    last_seconds = time(NULL);
    /* broadcast an I-Am on startup */
    Send_I_Am(&Handler_Transmit_Buffer[0]);

//////////////////////////////////////
	initPin(GPIO_PIN1);
	initPin(GPIO_PIN2);
	setPinDirection(GPIO_PIN1, IN);
	setPinDirection(GPIO_PIN2, IN);

  struct itimerval it_val;	/* for setting itimer */

  /* Upon SIGALRM, call DoStuff().
   * Set interval timer.  We want frequency in ms, 
   * but the setitimer call needs seconds and useconds. */
  if (signal(SIGALRM, (void (*)(int)) countPeopleInFunc) == SIG_ERR) {
    perror("Unable to catch SIGALRM");
    exit(1);
  }
/*
  if (signal(SIGALRM, (void (*)(int)) countPeopleOutFunc) == SIG_ERR) {
    perror("Unable to catch SIGALRM");
    exit(1);
  }
*/
  it_val.it_value.tv_sec =     INTERVAL/1000;
  it_val.it_value.tv_usec =    (INTERVAL*1000) % 1000000;	
  it_val.it_interval = it_val.it_value;
  if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) {
    perror("error calling setitimer()");
    exit(1);
  }
////////////////////////////////////////////////////



    /* loop forever */
    for (;;) {
        /* input */
        current_seconds = time(NULL);

        /* 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);
        }
        /* at least one second has passed */
        elapsed_seconds = (uint32_t) (current_seconds - last_seconds);
        if (elapsed_seconds) {
            last_seconds = current_seconds;
            dcc_timer_seconds(elapsed_seconds);
#if defined(BACDL_BIP) && BBMD_ENABLED
            bvlc_maintenance_timer(elapsed_seconds);
#endif
            dlenv_maintenance_timer(elapsed_seconds);
            Load_Control_State_Machine_Handler();
            elapsed_milliseconds = elapsed_seconds * 1000;
            handler_cov_timer_seconds(elapsed_seconds);
            tsm_timer_milliseconds(elapsed_milliseconds);
            trend_log_timer(elapsed_seconds);
#if defined(INTRINSIC_REPORTING)
            Device_local_reporting();
#endif
        }
        handler_cov_task();
        /* scan cache address */
        address_binding_tmr += elapsed_seconds;
        if (address_binding_tmr >= 60) {
            address_cache_timer(address_binding_tmr);
            address_binding_tmr = 0;
        }
#if defined(INTRINSIC_REPORTING)
        /* try to find addresses of recipients */
        recipient_scan_tmr += elapsed_seconds;
        if (recipient_scan_tmr >= NC_RESCAN_RECIPIENTS_SECS) {
            Notification_Class_find_recipient();
            recipient_scan_tmr = 0;
        }
#endif
	
        /* output */

        /* blink LEDs, Turn on or off outputs, etc */
    }

    return 0;
}
Example #11
0
/** Main function of server demo.
 *
 * @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
 *      datalink_receive, npdu_handler,
 *      dcc_timer_seconds, bvlc_maintenance_timer,
 *      Load_Control_State_Machine_Handler, handler_cov_task,
 *      tsm_timer_milliseconds
 *
 * @param argc [in] Arg count.
 * @param argv [in] Takes one argument: the Device Instance #.
 * @return 0 on success.
 */
int main(
    int argc,
    char *argv[])
{
    BACNET_ADDRESS src = {
        0
    };  /* address where message came from */
    uint16_t pdu_len = 0;
    unsigned timeout = 1;       /* milliseconds */
    time_t last_seconds = 0;
    time_t current_seconds = 0;
    uint32_t elapsed_seconds = 0;
    uint32_t elapsed_milliseconds = 0;
    uint32_t address_binding_tmr = 0;
    uint32_t recipient_scan_tmr = 0;
#if defined(BAC_UCI)
    int uciId = 0;
    struct uci_context *ctx;
#endif
    int argi = 0;
    char *filename = NULL;

    filename = filename_remove_path(argv[0]);
    for (argi = 1; argi < argc; argi++) {
        if (strcmp(argv[argi], "--help") == 0) {
            print_usage(filename);
            print_help(filename);
            return 0;
        }
        if (strcmp(argv[argi], "--version") == 0) {
            printf("%s %s\n", filename, BACNET_VERSION_TEXT);
            printf("Copyright (C) 2014 by Steve Karg and others.\n"
                "This is free software; see the source for copying conditions.\n"
                "There is NO warranty; not even for MERCHANTABILITY or\n"
                "FITNESS FOR A PARTICULAR PURPOSE.\n");
            return 0;
        }
    }
#if defined(BAC_UCI)
    ctx = ucix_init("bacnet_dev");
    if (!ctx)
        fprintf(stderr, "Failed to load config file bacnet_dev\n");
    uciId = ucix_get_option_int(ctx, "bacnet_dev", "0", "Id", 0);
    printf("ID: %i", uciId);
    if (uciId != 0) {
        Device_Set_Object_Instance_Number(uciId);
    } else {
#endif /* defined(BAC_UCI) */
        /* allow the device ID to be set */
        if (argc > 1) {
            Device_Set_Object_Instance_Number(strtol(argv[1], NULL, 0));
        }
        if (argc > 2) {
            Device_Object_Name_ANSI_Init(argv[2]);
        }
#if defined(BAC_UCI)
    }
    ucix_cleanup(ctx);
#endif /* defined(BAC_UCI) */

    printf("BACnet Server Demo\n" "BACnet Stack Version %s\n"
        "BACnet Device ID: %u\n" "Max APDU: %d\n", BACnet_Version,
        Device_Object_Instance_Number(), MAX_APDU);
    /* load any static address bindings to show up
       in our device bindings list */
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    /* configure the timeout values */
    last_seconds = time(NULL);
    /* broadcast an I-Am on startup */
    Send_I_Am(&Handler_Transmit_Buffer[0]);
    /* loop forever */
    for (;;) {
        /* input */
        current_seconds = time(NULL);

        /* 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);
        }
        /* at least one second has passed */
        elapsed_seconds = (uint32_t) (current_seconds - last_seconds);
        if (elapsed_seconds) {
            last_seconds = current_seconds;
            dcc_timer_seconds(elapsed_seconds);
#if defined(BACDL_BIP) && BBMD_ENABLED
            bvlc_maintenance_timer(elapsed_seconds);
#endif
            dlenv_maintenance_timer(elapsed_seconds);
            Load_Control_State_Machine_Handler();
            elapsed_milliseconds = elapsed_seconds * 1000;
            handler_cov_timer_seconds(elapsed_seconds);
            tsm_timer_milliseconds(elapsed_milliseconds);
            trend_log_timer(elapsed_seconds);
#if defined(INTRINSIC_REPORTING)
            Device_local_reporting();
#endif
        }
        handler_cov_task();
        /* scan cache address */
        address_binding_tmr += elapsed_seconds;
        if (address_binding_tmr >= 60) {
            address_cache_timer(address_binding_tmr);
            address_binding_tmr = 0;
        }
#if defined(INTRINSIC_REPORTING)
        /* try to find addresses of recipients */
        recipient_scan_tmr += elapsed_seconds;
        if (recipient_scan_tmr >= NC_RESCAN_RECIPIENTS_SECS) {
            Notification_Class_find_recipient();
            recipient_scan_tmr = 0;
        }
#endif
        /* output */

        /* blink LEDs, Turn on or off outputs, etc */
    }

    return 0;
}
Example #12
0
/** Main function of server demo.
 *
 * @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
 *      datalink_receive, npdu_handler,
 *      dcc_timer_seconds, bvlc_maintenance_timer,
 *      Load_Control_State_Machine_Handler, handler_cov_task,
 *      tsm_timer_milliseconds
 *
 * @param argc [in] Arg count.
 * @param argv [in] Takes one argument: the Device Instance #.
 * @return 0 on success.
 */
int main(
    int argc,
    char *argv[])
{
    BACNET_ADDRESS src = {
        0
    };  /* address where message came from */
    uint16_t pdu_len = 0;
    unsigned timeout = 1;       /* milliseconds */
    time_t last_seconds = 0;
    time_t current_seconds = 0;
    uint32_t elapsed_seconds = 0;
    uint32_t elapsed_milliseconds = 0;
    uint32_t address_binding_tmr = 0;
#if defined(INTRINSIC_REPORTING)
    uint32_t recipient_scan_tmr = 0;
#endif
#if defined(BACNET_TIME_MASTER)
    BACNET_DATE_TIME bdatetime;
#endif
#if defined(BAC_UCI)
    int uciId = 0;
    const char *uciName;
    struct uci_context *ctx;
#if defined(AI)
    time_t ucimodtime_bacnet_ai = 0;
#endif
#if defined(AO)
    time_t ucimodtime_bacnet_ao = 0;
#endif
#if defined(AV)
    time_t ucimodtime_bacnet_av = 0;
#endif
#if defined(BI)
    time_t ucimodtime_bacnet_bi = 0;
#endif
#if defined(BO)
    time_t ucimodtime_bacnet_bo = 0;
#endif
#if defined(BV)
    time_t ucimodtime_bacnet_bv = 0;
#endif
#if defined(MSI)
    time_t ucimodtime_bacnet_mi = 0;
#endif
#if defined(MSO)
    time_t ucimodtime_bacnet_mo = 0;
#endif
#if defined(MSV)
    time_t ucimodtime_bacnet_mv = 0;
#endif
    int rewrite = 0;
#endif
    int argi = 0;
    const char *filename = NULL;

    filename = filename_remove_path(argv[0]);
    for (argi = 1; argi < argc; argi++) {
        if (strcmp(argv[argi], "--help") == 0) {
            print_usage(filename);
            print_help(filename);
            return 0;
        }
        if (strcmp(argv[argi], "--version") == 0) {
            printf("%s %s\n", filename, BACNET_VERSION_TEXT);
            printf("Copyright (C) 2014 by Steve Karg and others.\n"
                "This is free software; see the source for copying conditions.\n"
                "There is NO warranty; not even for MERCHANTABILITY or\n"
                "FITNESS FOR A PARTICULAR PURPOSE.\n");
            return 0;
        }
    }
#if defined(BAC_UCI)
    char *pEnv = getenv("UCI_SECTION");
    if (!pEnv) {
       	pEnv = "0";
#if PRINT_ENABLED
        fprintf(stderr,  "Failed to getenv(UCI_SECTION)\n");
    } else {
	    fprintf(stderr,  "load config file bacnet_dev %s\n",pEnv);
#endif
    }

    ctx = ucix_init("bacnet_dev");
#if PRINT_ENABLED
    if(!ctx)
        fprintf(stderr,  "Failed to load config file bacnet_dev\n");
#endif

    uciId = ucix_get_option_int(ctx, "bacnet_dev", pEnv, "Id", 0);
    uciName = ucix_get_option(ctx, "bacnet_dev", pEnv, "name");
    if(ctx) 
        ucix_cleanup(ctx);
    if (uciId != 0) {
        Device_Set_Object_Instance_Number(uciId);
        if (uciName)
#if PRINT_ENABLED
            fprintf(stderr, "BACnet Device Name: %s\n", uciName);
#endif
            Device_Object_Name_ANSI_Init(uciName);
    } else {
#endif /* defined(BAC_UCI) */
        /* allow the device ID to be set */
        if (argc > 1) {
            Device_Set_Object_Instance_Number(strtol(argv[1], NULL, 0));
        }
        if (argc > 2) {
            Device_Object_Name_ANSI_Init(argv[2]);
        }
#if defined(BAC_UCI)
    }

#if defined(AI)
    char ai_path[128];
    struct stat ai_s;
	snprintf(ai_path, sizeof(ai_path), "/etc/config/bacnet_ai");
	if( stat(ai_path, &ai_s) > -1 )
		ucimodtime_bacnet_ai = ai_s.st_mtime;
#endif
#if defined(AO)
    char ao_path[128];
    struct stat ao_s;
	snprintf(ao_path, sizeof(ao_path), "/etc/config/bacnet_ao");
	if( stat(ao_path, &ao_s) > -1 )
		ucimodtime_bacnet_ao = ao_s.st_mtime;
#endif
#if defined(AV)
    char av_path[128];
    struct stat av_s;
	snprintf(av_path, sizeof(av_path), "/etc/config/bacnet_av");
	if( stat(av_path, &av_s) > -1 )
		ucimodtime_bacnet_av = av_s.st_mtime;
#endif
#if defined(BI)
    char bi_path[128];
    struct stat bi_s;
	snprintf(bi_path, sizeof(bi_path), "/etc/config/bacnet_bi");
	if( stat(bi_path, &bi_s) > -1 )
		ucimodtime_bacnet_bi = bi_s.st_mtime;
#endif
#if defined(BO)
    char bo_path[128];
    struct stat bo_s;
	snprintf(bo_path, sizeof(bo_path), "/etc/config/bacnet_bo");
	if( stat(bo_path, &bo_s) > -1 )
		ucimodtime_bacnet_bo = bo_s.st_mtime;
#endif
#if defined(BV)
    char bv_path[128];
    struct stat bv_s;
	snprintf(bv_path, sizeof(bv_path), "/etc/config/bacnet_bv");
	if( stat(bv_path, &bv_s) > -1 )
		ucimodtime_bacnet_bv = bv_s.st_mtime;
#endif
#if defined(MSI)
    char msi_path[128];
    struct stat msi_s;
	snprintf(msi_path, sizeof(msi_path), "/etc/config/bacnet_mi");
	if( stat(msi_path, &msi_s) > -1 )
		ucimodtime_bacnet_mi = msi_s.st_mtime;
#endif
#if defined(MSO)
    char mso_path[128];
    struct stat mso_s;
	snprintf(mso_path, sizeof(mso_path), "/etc/config/bacnet_mo");
	if( stat(mso_path, &mso_s) > -1 )
		ucimodtime_bacnet_mo = mso_s.st_mtime;
#endif
#if defined(MSV)
    char msv_path[128];
    struct stat msv_s;
	snprintf(msv_path, sizeof(msv_path), "/etc/config/bacnet_mv");
	if( stat(msv_path, &msv_s) > -1 )
		ucimodtime_bacnet_mv = msv_s.st_mtime;
#endif
#endif /* defined(BAC_UCI) */

#if PRINT_ENABLED
    printf("BACnet Server Demo\n" "BACnet Stack Version %s\n"
        "BACnet Device ID: %u\n" "Max APDU: %d\n", BACnet_Version,
        Device_Object_Instance_Number(), MAX_APDU);
#endif
    /* load any static address bindings to show up
       in our device bindings list */
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    /* configure the timeout values */
    last_seconds = time(NULL);
    /* broadcast an I-Am on startup */
    Send_I_Am(&Handler_Transmit_Buffer[0]);
    /* loop forever */
    for (;;) {
        /* input */
        current_seconds = time(NULL);

        /* 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);
        }
        /* at least one second has passed */
        elapsed_seconds = (uint32_t) (current_seconds - last_seconds);
        if (elapsed_seconds) {
            last_seconds = current_seconds;
            dcc_timer_seconds(elapsed_seconds);
#if defined(BACDL_BIP) && BBMD_ENABLED
            bvlc_maintenance_timer(elapsed_seconds);
#endif
            dlenv_maintenance_timer(elapsed_seconds);
#if defined(LC)
            Load_Control_State_Machine_Handler();
#endif
            elapsed_milliseconds = elapsed_seconds * 1000;
            handler_cov_timer_seconds(elapsed_seconds);
            tsm_timer_milliseconds(elapsed_milliseconds);
#if defined(TRENDLOG)
            trend_log_timer(elapsed_seconds);
#endif
#if defined(INTRINSIC_REPORTING)
            Device_local_reporting();
#endif
#if defined(BACNET_TIME_MASTER)
            Device_getCurrentDateTime(&bdatetime);
            handler_timesync_task(&bdatetime);
#endif
        }
        handler_cov_task();
        /* scan cache address */
        address_binding_tmr += elapsed_seconds;
        if (address_binding_tmr >= 60) {
            address_cache_timer(address_binding_tmr);
            address_binding_tmr = 0;
        }
#if defined(INTRINSIC_REPORTING)
        /* try to find addresses of recipients */
        recipient_scan_tmr += elapsed_seconds;
        if (recipient_scan_tmr >= NC_RESCAN_RECIPIENTS_SECS) {
            Notification_Class_find_recipient();
            recipient_scan_tmr = 0;
        }
#endif
        /* output */
#if defined(BAC_UCI)
        rewrite++;
        if (rewrite>100000) {
#if PRINT_ENABLED
            printf("rewrite interval %i\n", rewrite);
#endif
            rewrite=0;
        }
#if defined(AI)
        /* update Analog Input from uci */
        ucimodtime_bacnet_ai = uci_Update(ucimodtime_bacnet_ai,OBJECT_ANALOG_INPUT,rewrite);
#endif
#if defined(AO)
        /* update Analog Output from uci */
        ucimodtime_bacnet_ao = uci_Update(ucimodtime_bacnet_ao,OBJECT_ANALOG_OUTPUT,rewrite);
#endif
#if defined(AV)
        /* update Analog Value from uci */
        ucimodtime_bacnet_av = uci_Update(ucimodtime_bacnet_av,OBJECT_ANALOG_VALUE,rewrite);
#endif
#if defined(BI)
        /* update Binary Input from uci */
        ucimodtime_bacnet_bi = uci_Update(ucimodtime_bacnet_bi,OBJECT_BINARY_INPUT,rewrite);
#endif
#if defined(BO)
        /* update Binary Output from uci */
        ucimodtime_bacnet_bo = uci_Update(ucimodtime_bacnet_bo,OBJECT_BINARY_OUTPUT,rewrite);
#endif
#if defined(BV)
        /* update Binary Value from uci */
        ucimodtime_bacnet_bv = uci_Update(ucimodtime_bacnet_bv,OBJECT_BINARY_VALUE,rewrite);
#endif
#if defined(MSI)
        /* update Multistate Input from uci */
        ucimodtime_bacnet_mi = uci_Update(ucimodtime_bacnet_mi,OBJECT_MULTI_STATE_INPUT,rewrite);
#endif
#if defined(MSO)
        /* update Multistate Output from uci */
        ucimodtime_bacnet_mo = uci_Update(ucimodtime_bacnet_mo,OBJECT_MULTI_STATE_OUTPUT,rewrite);
#endif
#if defined(MSV)
        /* update Multistate Value from uci */
        ucimodtime_bacnet_mv = uci_Update(ucimodtime_bacnet_mv,OBJECT_MULTI_STATE_VALUE,rewrite);
#endif
#endif /* defined(BAC_UCI) */

        /* blink LEDs, Turn on or off outputs, etc */
    }

    return 0;
}
Example #13
0
/** Process the APDU header and invoke the appropriate service handler
 * to manage the received request.
 * Almost all requests and ACKs invoke this function.
 * @ingroup MISCHNDLR
 *
 * @param src [in] The BACNET_ADDRESS of the message's source.
 * @param apdu [in] The apdu portion of the request, to be processed.
 * @param apdu_len [in] The total (remaining) length of the apdu.
 */
void apdu_handler(
    BACNET_ADDRESS * src,
    uint8_t * apdu,     /* APDU data */
    uint16_t apdu_len)
{
    BACNET_CONFIRMED_SERVICE_DATA service_data = { 0 };
    BACNET_CONFIRMED_SERVICE_ACK_DATA service_ack_data = { 0 };
    //uint8_t invoke_id = 0;
    uint8_t service_choice = 0;
    uint8_t *service_request = NULL;
    uint16_t service_request_len = 0;
    int len = 0;        /* counts where we are in PDU */
    uint8_t tag_number = 0;
    uint32_t len_value = 0;
    uint32_t error_code = 0;
    uint32_t error_class = 0;
   // uint8_t reason = 0;
   // bool server = false;
    if (apdu) {	 
        /* PDU Type */
        switch (apdu[0] & 0xF0) {
            case PDU_TYPE_CONFIRMED_SERVICE_REQUEST: 	
                len =
                    (int) apdu_decode_confirmed_service_request(&apdu[0],
                    apdu_len, &service_data, &service_choice, &service_request,
                    &service_request_len);
				
				#if 0
                if (apdu_confirmed_dcc_disabled(service_choice)) {     tbd: chelsea
                    /* When network communications are completely disabled,
                       only DeviceCommunicationControl and ReinitializeDevice APDUs
                       shall be processed and no messages shall be initiated. */
                    break;
                }
                if ((service_choice < MAX_BACNET_CONFIRMED_SERVICE) &&
                    (Confirmed_Function[service_choice]))
                    Confirmed_Function[service_choice] (service_request,
                        service_request_len/*, src, &service_data*/);
                else if (Unrecognized_Service_Handler)
                    Unrecognized_Service_Handler(service_request,
                        service_request_len/*, src, &service_data*/);
				#endif

//				#if READ_WRITE_PROPERTY
				#if 1
				if (service_choice == SERVICE_CONFIRMED_READ_PROPERTY) {
                handler_read_property(service_request,
                    service_request_len, src, &service_data);
	            } else if (service_choice == SERVICE_CONFIRMED_WRITE_PROPERTY) {
	                handler_write_property(service_request,
	                 service_request_len, src, &service_data);
	            } else 
				#endif

				if (service_choice == SERVICE_CONFIRMED_PRIVATE_TRANSFER) 
				{ 	 
	             handler_private_transfer(apdu,apdu_len,src);	 // add private transfer by chelsea
				}
				else
				{ 		
	               handler_unrecognized_service(service_request,
	                   service_request_len, src, &service_data);
	            } 								  
                break;
            case PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST:
			{	
				//U8_T i;
				
                service_choice = apdu[1];
                service_request = &apdu[2];
                service_request_len = apdu_len - 2;
				#if 0
                if (apdu_unconfirmed_dcc_disabled(service_choice)) {
                    /* When network communications are disabled,
                       only DeviceCommunicationControl and ReinitializeDevice APDUs
                       shall be processed and no messages shall be initiated.
                       If communications have been initiation disabled, then
                       WhoIs may be processed. */
                    break;
                }
				
                if (service_choice < MAX_BACNET_UNCONFIRMED_SERVICE) {
                    if (Unconfirmed_Function[service_choice])
                        Unconfirmed_Function[service_choice] (
						service_request,
                            service_request_len/*, src*/);		// tbd: chelsea
                }
				#endif				
				if (service_choice == SERVICE_UNCONFIRMED_WHO_IS) 
				{	
					if(	modbus.protocal == BAC_MSTP)
					{	 
	                	handler_who_is(service_request,
	                     service_request_len, src);
					}
					else if((modbus.protocal == BAC_IP) || (modbus.protocal == BAC_GSM))
					{  	
						Send_I_Am(&Handler_Transmit_Buffer[0]);
					}
            	}
				else if (service_choice == SERVICE_UNCONFIRMED_PRIVATE_TRANSFER) 
				{ // add unconfirmedPrivateTransfer handler, for TEMCO private
					handler_private_transfer(apdu,apdu_len,src);
				}
					
			 }
                break;
            case PDU_TYPE_SIMPLE_ACK: 
//                invoke_id = apdu[1];
                service_choice = apdu[2];
                switch (service_choice) {
                    case SERVICE_CONFIRMED_ACKNOWLEDGE_ALARM:
                    case SERVICE_CONFIRMED_COV_NOTIFICATION:
                    case SERVICE_CONFIRMED_EVENT_NOTIFICATION:
                    case SERVICE_CONFIRMED_SUBSCRIBE_COV:
                    case SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY:
                    case SERVICE_CONFIRMED_LIFE_SAFETY_OPERATION:
                        /* Object Access Services */
                    case SERVICE_CONFIRMED_ADD_LIST_ELEMENT:
                    case SERVICE_CONFIRMED_REMOVE_LIST_ELEMENT:
                    case SERVICE_CONFIRMED_DELETE_OBJECT:
                    case SERVICE_CONFIRMED_WRITE_PROPERTY:
                    case SERVICE_CONFIRMED_WRITE_PROP_MULTIPLE:
                        /* Remote Device Management Services */
                    case SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL:
                    case SERVICE_CONFIRMED_REINITIALIZE_DEVICE:
                    case SERVICE_CONFIRMED_TEXT_MESSAGE:
                        /* Virtual Terminal Services */
                    case SERVICE_CONFIRMED_VT_CLOSE:
                        /* Security Services */
                    case SERVICE_CONFIRMED_REQUEST_KEY:
                        if (Confirmed_ACK_Function[service_choice] != NULL) {
							// tbd: chelsea
//                            ((confirmed_simple_ack_function)
//                                Confirmed_ACK_Function[service_choice]) (src,
//                                invoke_id);
                        }
                    //    tsm_free_invoke_id(invoke_id);
                        break;
                    default:
                        break;
                }
                break;
            case PDU_TYPE_COMPLEX_ACK:
                service_ack_data.segmented_message =
                    (apdu[0] & BIT3) ? true : false;
                service_ack_data.more_follows =
                    (apdu[0] & BIT2) ? true : false;
//                invoke_id = service_ack_data.invoke_id = apdu[1];
                len = 2;
                if (service_ack_data.segmented_message) {
                    service_ack_data.sequence_number = apdu[len++];
                    service_ack_data.proposed_window_number = apdu[len++];
                }
                service_choice = apdu[len++];
                service_request = &apdu[len];
                service_request_len = apdu_len - (uint16_t) len;   
                switch (service_choice) {
                    case SERVICE_CONFIRMED_GET_ALARM_SUMMARY:
                    case SERVICE_CONFIRMED_GET_ENROLLMENT_SUMMARY:
                    case SERVICE_CONFIRMED_GET_EVENT_INFORMATION:
                        /* File Access Services */
                    case SERVICE_CONFIRMED_ATOMIC_READ_FILE:
                    case SERVICE_CONFIRMED_ATOMIC_WRITE_FILE:
                        /* Object Access Services */
                    case SERVICE_CONFIRMED_CREATE_OBJECT:
                    case SERVICE_CONFIRMED_READ_PROPERTY:
                    case SERVICE_CONFIRMED_READ_PROP_CONDITIONAL:
                    case SERVICE_CONFIRMED_READ_PROP_MULTIPLE:
                    case SERVICE_CONFIRMED_READ_RANGE:
                    case SERVICE_CONFIRMED_PRIVATE_TRANSFER:
                        /* Virtual Terminal Services */
                    case SERVICE_CONFIRMED_VT_OPEN:
                    case SERVICE_CONFIRMED_VT_DATA:
                        /* Security Services */
                    case SERVICE_CONFIRMED_AUTHENTICATE:
                        if (Confirmed_ACK_Function[service_choice] != NULL) { 						
//                            (Confirmed_ACK_Function[service_choice])
//                                (service_request, service_request_len/*, src,
//                                &service_ack_data*/);		// tbd: chelsea
                        }
                    //    tsm_free_invoke_id(invoke_id);
                        break;
                    default:
                        break;
                }
                break;
            case PDU_TYPE_SEGMENT_ACK:	 
                /* FIXME: what about a denial of service attack here?
                   we could check src to see if that matched the tsm */
             //   tsm_free_invoke_id(invoke_id);
                break;
            case PDU_TYPE_ERROR:	
//                invoke_id = apdu[1];
                service_choice = apdu[2];
                len = 3;

                /* FIXME: Currently special case for C_P_T but there are others which may
                   need consideration such as ChangeList-Error, CreateObject-Error,
                   WritePropertyMultiple-Error and VTClose_Error but they may be left as
                   is for now until support for these services is added */

                if (service_choice == SERVICE_CONFIRMED_PRIVATE_TRANSFER) {     /* skip over opening tag 0 */
                    if (decode_is_opening_tag_number(&apdu[len], 0)) {
                        len++;  /* a tag number of 0 is not extended so only one octet */
                    }
                }
                len +=
                    decode_tag_number_and_value(&apdu[len], &tag_number,
                    &len_value);
                /* FIXME: we could validate that the tag is enumerated... */
                len += decode_enumerated(&apdu[len], len_value, &error_class);
                len +=
                    decode_tag_number_and_value(&apdu[len], &tag_number,
                    &len_value);
                /* FIXME: we could validate that the tag is enumerated... */
                len += decode_enumerated(&apdu[len], len_value, &error_code);

                if (service_choice == SERVICE_CONFIRMED_PRIVATE_TRANSFER) {     /* skip over closing tag 0 */
                    if (decode_is_closing_tag_number(&apdu[len], 0)) {
                        len++;  /* a tag number of 0 is not extended so only one octet */
                    }
                }
                if (service_choice < MAX_BACNET_CONFIRMED_SERVICE) {
//                    if (Error_Function[service_choice])
//                        Error_Function[service_choice] (src, invoke_id/*,
//                            (BACNET_ERROR_CLASS) error_class,
//                            (BACNET_ERROR_CODE) error_code*/);	  // tbd: chelsea
                }
              //  tsm_free_invoke_id(invoke_id);
                break;
            case PDU_TYPE_REJECT:	  
//                invoke_id = apdu[1];
//                reason = apdu[2];
              //  if (Reject_Function)
              //      Reject_Function(src, invoke_id, reason);
              //  tsm_free_invoke_id(invoke_id);
                break;
            case PDU_TYPE_ABORT:	
//                server = apdu[0] & 0x01;
//                invoke_id = apdu[1];
//                reason = apdu[2];
             //   if (Abort_Function)
             //       Abort_Function(src, invoke_id, reason, server);
            //    tsm_free_invoke_id(invoke_id);
                break;
            default:   
                break;
        }

		memset(Handler_Transmit_Buffer,0,MAX_PDU);
		//printf("apdu_done\r\n");
    }

    return;
}
Example #14
0
/** Main function of server demo.
 *
 * @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
 *      datalink_receive, npdu_handler,
 *      dcc_timer_seconds, bvlc_maintenance_timer,
 *      Load_Control_State_Machine_Handler, handler_cov_task,
 *      tsm_timer_milliseconds
 *
 * @param argc [in] Arg count.
 * @param argv [in] Takes one argument: the Device Instance #.
 * @return 0 on success.
 */
int main(
    int argc,
    char *argv[])
{
    BACNET_ADDRESS src = {
        0
    };  /* address where message came from */
    uint16_t pdu_len = 0;
    unsigned timeout = 1;       /* milliseconds */
    time_t last_seconds = 0;
    time_t current_seconds = 0;
    uint32_t elapsed_seconds = 0;
    uint32_t elapsed_milliseconds = 0;
    uint32_t address_binding_tmr = 0;
    uint32_t recipient_scan_tmr = 0;

    /* allow the device ID to be set */
    if (argc > 1)
        Device_Set_Object_Instance_Number(strtol(argv[1], NULL, 0));
    printf("BACnet Server Demo\n" "BACnet Stack Version %s\n"
        "BACnet Device ID: %u\n" "Max APDU: %d\n", BACnet_Version,
        Device_Object_Instance_Number(), MAX_APDU);
    /* load any static address bindings to show up
       in our device bindings list */
    address_init();
    Init_Service_Handlers();
    dlenv_init();
    atexit(datalink_cleanup);
    /* configure the timeout values */
    last_seconds = time(NULL);
    /* broadcast an I-Am on startup */
    Send_I_Am(&Handler_Transmit_Buffer[0]);
    /* loop forever */
    for (;;) {
        /* input */
        current_seconds = time(NULL);

        /* 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);
        }
        /* at least one second has passed */
        elapsed_seconds = (uint32_t) (current_seconds - last_seconds);
        if (elapsed_seconds) {
            last_seconds = current_seconds;
            dcc_timer_seconds(elapsed_seconds);
#if defined(BACDL_BIP) && BBMD_ENABLED
            bvlc_maintenance_timer(elapsed_seconds);
#endif
            dlenv_maintenance_timer(elapsed_seconds);
            Load_Control_State_Machine_Handler();
            elapsed_milliseconds = elapsed_seconds * 1000;
            handler_cov_timer_seconds(elapsed_seconds);
            tsm_timer_milliseconds(elapsed_milliseconds);
            trend_log_timer(elapsed_seconds);
#if defined(INTRINSIC_REPORTING)
            Device_local_reporting();
#endif
        }
        handler_cov_task();
        /* scan cache address */
        address_binding_tmr += elapsed_seconds;
        if (address_binding_tmr >= 60) {
            address_cache_timer(address_binding_tmr);
            address_binding_tmr = 0;
        }
#if defined(INTRINSIC_REPORTING)
        /* try to find addresses of recipients */
        recipient_scan_tmr += elapsed_seconds;
        if (recipient_scan_tmr >= NC_RESCAN_RECIPIENTS_SECS) {
            Notification_Class_find_recipient();
            recipient_scan_tmr = 0;
        }
#endif
        /* output */

        /* blink LEDs, Turn on or off outputs, etc */
    }

    return 0;
}