void testDevice( Test * pTest) { bool status = false; const char *name = "Patricia"; status = Device_Set_Object_Instance_Number(0); ct_test(pTest, Device_Object_Instance_Number() == 0); ct_test(pTest, status == true); status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE); ct_test(pTest, Device_Object_Instance_Number() == BACNET_MAX_INSTANCE); ct_test(pTest, status == true); status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE / 2); ct_test(pTest, Device_Object_Instance_Number() == (BACNET_MAX_INSTANCE / 2)); ct_test(pTest, status == true); status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE + 1); ct_test(pTest, Device_Object_Instance_Number() != (BACNET_MAX_INSTANCE + 1)); ct_test(pTest, status == false); Device_Set_System_Status(STATUS_NON_OPERATIONAL); ct_test(pTest, Device_System_Status() == STATUS_NON_OPERATIONAL); ct_test(pTest, Device_Vendor_Identifier() == BACNET_VENDOR_ID); Device_Set_Model_Name(name, strlen(name)); ct_test(pTest, strcmp(Device_Model_Name(), name) == 0); return; }
/************************************************************************** * 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); } }
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; }
/** Local function which responds with either the requested object name * or object ID, if the Device has a match. * @param data [in] The decoded who-has payload from the request. */ static void match_name_or_object( BACNET_WHO_HAS_DATA * data) { int object_type = 0; uint32_t object_instance = 0; bool found = false; BACNET_CHARACTER_STRING object_name; /* do we have such an object? If so, send an I-Have. note: we should have only 1 of such an object */ if (data->is_object_name) { /* valid name in my device? */ found = Device_Valid_Object_Name(&data->object.name, &object_type, &object_instance); if (found) { Send_I_Have(Device_Object_Instance_Number(), (BACNET_OBJECT_TYPE) object_type, object_instance, &data->object.name); } } else { /* valid object_name copy in my device? */ found = Device_Object_Name_Copy((BACNET_OBJECT_TYPE) data-> object.identifier.type, data->object.identifier.instance, &object_name); if (found) { Send_I_Have(Device_Object_Instance_Number(), (BACNET_OBJECT_TYPE) data->object.identifier.type, data->object.identifier.instance, &object_name); } } }
void handler_who_has( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src) { int len = 0; BACNET_WHO_HAS_DATA data; bool directed_to_me = false; int object_type = 0; uint32_t object_instance = 0; char *object_name = NULL; bool found = false; (void) src; len = whohas_decode_service_request(service_request, service_len, &data); if (len > 0) { if ((data.low_limit == -1) || (data.high_limit == -1)) directed_to_me = true; else if ((Device_Object_Instance_Number() >= (uint32_t) data.low_limit) && (Device_Object_Instance_Number() <= (uint32_t) data.high_limit)) directed_to_me = true; if (directed_to_me) { /* do we have such an object? If so, send an I-Have. note: we should have only 1 of such an object */ if (data.object_name) { /* valid name in my device? */ object_name = characterstring_value(&data.object.name); found = Device_Valid_Object_Name(object_name, &object_type, &object_instance); if (found) Send_I_Have(Device_Object_Instance_Number(), object_type, object_instance, object_name); } else { /* valid object in my device? */ object_name = Device_Valid_Object_Id(data.object.identifier.type, data.object.identifier.instance); if (object_name) Send_I_Have(Device_Object_Instance_Number(), data.object.identifier.type, data.object.identifier.instance, object_name); } } } return; }
/** Handler for Who-Has requests in the virtual routing setup, * with broadcast I-Have response. * Will respond if the device Object ID matches, and we have * the Object or Object Name requested. * * @ingroup DMDOB * @param service_request [in] The received message to be handled. * @param service_len [in] Length of the service_request message. * @param src [in] The BACNET_ADDRESS of the message's source (ignored). */ void handler_who_has_for_routing( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src) { int len = 0; BACNET_WHO_HAS_DATA data; int32_t dev_instance; int cursor = 0; /* Starting hint */ int my_list[2] = { 0, -1 }; /* Not really used, so dummy values */ BACNET_ADDRESS bcast_net; (void) src; len = whohas_decode_service_request(service_request, service_len, &data); if (len > 0) { /* Go through all devices, starting with the root gateway Device */ memset(&bcast_net, 0, sizeof(BACNET_ADDRESS)); bcast_net.net = BACNET_BROADCAST_NETWORK; /* That's all we have to set */ while (Routed_Device_GetNext(&bcast_net, my_list, &cursor)) { dev_instance = Device_Object_Instance_Number(); if ((data.low_limit == -1) || (data.high_limit == -1) || ((dev_instance >= data.low_limit) && (dev_instance <= data.high_limit))) match_name_or_object(&data); } } }
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; int32_t target_device; (void) src; len = whois_decode_service_request(service_request, service_len, &low_limit, &high_limit); if (len == 0) { Send_I_Am_Flag = true; } else if (len != BACNET_STATUS_ERROR) { /* is my device id within the limits? */ target_device = Device_Object_Instance_Number(); if (((target_device >= low_limit) && (target_device <= 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_Flag = true; } } return; }
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; int32_t target_device; (void) src; len = whois_decode_service_request(service_request, service_len, &low_limit, &high_limit); if (len == 0) { Send_I_Am_Flag = true; } else if (len != BACNET_STATUS_ERROR) { /* is my device id within the limits? */ target_device = Device_Object_Instance_Number(); if ((target_device >= low_limit) && (target_device <= high_limit)) { Send_I_Am_Flag = true; } } return; }
/** Handler for Who-Has requests, with broadcast I-Have response. * Will respond if the device Object ID matches, and we have * the Object or Object Name requested. * * @ingroup DMDOB * @param service_request [in] The received message to be handled. * @param service_len [in] Length of the service_request message. * @param src [in] The BACNET_ADDRESS of the message's source. */ void handler_who_has( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src) { int len = 0; BACNET_WHO_HAS_DATA data; bool directed_to_me = false; (void) src; len = whohas_decode_service_request(service_request, service_len, &data); if (len > 0) { if ((data.low_limit == -1) || (data.high_limit == -1)) directed_to_me = true; else if ((Device_Object_Instance_Number() >= (uint32_t) data.low_limit) && (Device_Object_Instance_Number() <= (uint32_t) data.high_limit)) directed_to_me = true; if (directed_to_me) { match_name_or_object(&data); } } }
/** 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; }
/** Handler for a ReadPropertyMultiple Service request. * @ingroup DSRPM * This handler will be invoked by apdu_handler() if it has been enabled * by a call to apdu_set_confirmed_handler(). * This handler builds a response packet, which is * - an Abort if * - the message is segmented * - if decoding fails * - if the response would be too large * - the result from each included read request, if it succeeds * - an Error if processing fails for all, or individual errors if only some fail, * or there isn't enough room in the APDU to fit the data. * * @param service_request [in] The contents of the service request. * @param service_len [in] The length of the service_request. * @param src [in] BACNET_ADDRESS of the source of the message * @param service_data [in] The BACNET_CONFIRMED_SERVICE_DATA information * decoded from the APDU header of this message. */ void handler_read_property_multiple( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) { int len = 0; uint16_t copy_len = 0; uint16_t decode_len = 0; int pdu_len = 0; BACNET_NPDU_DATA npdu_data; int bytes_sent; BACNET_ADDRESS my_address; BACNET_RPM_DATA rpmdata; int apdu_len = 0; int npdu_len = 0; int error = 0; /* jps_debug - see if we are utilizing all the buffer */ /* memset(&Handler_Transmit_Buffer[0], 0xff, sizeof(Handler_Transmit_Buffer)); */ /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); npdu_len = npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address, &npdu_data); if (service_data->segmented_message) { rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED; error = BACNET_STATUS_ABORT; #if PRINT_ENABLED fprintf(stderr, "RPM: Segmented message. Sending Abort!\r\n"); #endif goto RPM_FAILURE; } /* decode apdu request & encode apdu reply encode complex ack, invoke id, service choice */ apdu_len = rpm_ack_encode_apdu_init(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id); for (;;) { /* Start by looking for an object ID */ len = rpm_decode_object_id(&service_request[decode_len], service_len - decode_len, &rpmdata); if (len >= 0) { /* Got one so skip to next stage */ decode_len += len; } else { /* bad encoding - skip to error/reject/abort handling */ #if PRINT_ENABLED fprintf(stderr, "RPM: Bad Encoding.\n"); #endif error = len; goto RPM_FAILURE; } /* Test for case of indefinite Device object instance */ if ((rpmdata.object_type == OBJECT_DEVICE) && (rpmdata.object_instance == BACNET_MAX_INSTANCE)) { rpmdata.object_instance = Device_Object_Instance_Number(); } /* Stick this object id into the reply - if it will fit */ len = rpm_ack_encode_apdu_object_begin(&Temp_Buf[0], &rpmdata); copy_len = memcopy(&Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], apdu_len, len, MAX_APDU); if (copy_len == 0) { #if PRINT_ENABLED fprintf(stderr, "RPM: Response too big!\r\n"); #endif rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED; error = BACNET_STATUS_ABORT; goto RPM_FAILURE; } apdu_len += copy_len; /* do each property of this object of the RPM request */ for (;;) { /* Fetch a property */ len = rpm_decode_object_property(&service_request[decode_len], service_len - decode_len, &rpmdata); if (len < 0) { /* bad encoding - skip to error/reject/abort handling */ #if PRINT_ENABLED fprintf(stderr, "RPM: Bad Encoding.\n"); #endif error = len; goto RPM_FAILURE; } decode_len += len; /* handle the special properties */ if ((rpmdata.object_property == PROP_ALL) || (rpmdata.object_property == PROP_REQUIRED) || (rpmdata.object_property == PROP_OPTIONAL)) { struct special_property_list_t property_list; unsigned property_count = 0; unsigned index = 0; BACNET_PROPERTY_ID special_object_property; if (rpmdata.array_index != BACNET_ARRAY_ALL) { /* No array index options for this special property. Encode error for this object property response */ len = rpm_ack_encode_apdu_object_property(&Temp_Buf[0], rpmdata.object_property, rpmdata.array_index); copy_len = memcopy(&Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], apdu_len, len, MAX_APDU); if (copy_len == 0) { #if PRINT_ENABLED fprintf(stderr, "RPM: Too full to encode property!\r\n"); #endif rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED; error = BACNET_STATUS_ABORT; goto RPM_FAILURE; } apdu_len += len; len = rpm_ack_encode_apdu_object_property_error(&Temp_Buf[0], ERROR_CLASS_PROPERTY, ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY); copy_len = memcopy(&Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], apdu_len, len, MAX_APDU); if (copy_len == 0) { #if PRINT_ENABLED fprintf(stderr, "RPM: Too full to encode error!\r\n"); #endif rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED; error = BACNET_STATUS_ABORT; goto RPM_FAILURE; } apdu_len += len; } else { special_object_property = rpmdata.object_property; Device_Objects_Property_List(rpmdata.object_type, &property_list); property_count = RPM_Object_Property_Count(&property_list, special_object_property); if (property_count == 0) { /* handle the error code - but use the special property */ len = RPM_Encode_Property(&Handler_Transmit_Buffer [npdu_len], (uint16_t) apdu_len, MAX_APDU, &rpmdata); if (len > 0) { apdu_len += len; } else { #if PRINT_ENABLED fprintf(stderr, "RPM: Too full for special property!\r\n"); #endif error = len; goto RPM_FAILURE; } } else { for (index = 0; index < property_count; index++) { rpmdata.object_property = RPM_Object_Property(&property_list, special_object_property, index); len = RPM_Encode_Property(&Handler_Transmit_Buffer [npdu_len], (uint16_t) apdu_len, MAX_APDU, &rpmdata); if (len > 0) { apdu_len += len; } else { #if PRINT_ENABLED fprintf(stderr, "RPM: Too full for property!\r\n"); #endif error = len; goto RPM_FAILURE; } } } } } else { /* handle an individual property */ len = RPM_Encode_Property(&Handler_Transmit_Buffer[npdu_len], (uint16_t) apdu_len, MAX_APDU, &rpmdata); if (len > 0) { apdu_len += len; } else { #if PRINT_ENABLED fprintf(stderr, "RPM: Too full for individual property!\r\n"); #endif error = len; goto RPM_FAILURE; } } if (decode_is_closing_tag_number(&service_request[decode_len], 1)) { /* Reached end of property list so cap the result list */ decode_len++; len = rpm_ack_encode_apdu_object_end(&Temp_Buf[0]); copy_len = memcopy(&Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], apdu_len, len, MAX_APDU); if (copy_len == 0) { #if PRINT_ENABLED fprintf(stderr, "RPM: Too full to encode object end!\r\n"); #endif rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED; error = BACNET_STATUS_ABORT; goto RPM_FAILURE; } else { apdu_len += copy_len; } break; /* finished with this property list */ } } if (decode_len >= service_len) { /* Reached the end so finish up */ break; } } if (apdu_len > service_data->max_resp) { /* too big for the sender - send an abort */ rpmdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED; error = BACNET_STATUS_ABORT; #if PRINT_ENABLED fprintf(stderr, "RPM: Message too large. Sending Abort!\n"); #endif goto RPM_FAILURE; } RPM_FAILURE: if (error) { if (error == BACNET_STATUS_ABORT) { apdu_len = abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, abort_convert_error_code(rpmdata.error_code), true); #if PRINT_ENABLED fprintf(stderr, "RPM: Sending Abort!\n"); #endif } else if (error == BACNET_STATUS_ERROR) { apdu_len = bacerror_encode_apdu(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, SERVICE_CONFIRMED_READ_PROP_MULTIPLE, rpmdata.error_class, rpmdata.error_code); #if PRINT_ENABLED fprintf(stderr, "RPM: Sending Error!\n"); #endif } else if (error == BACNET_STATUS_REJECT) { apdu_len = reject_encode_apdu(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, reject_convert_error_code(rpmdata.error_code)); #if PRINT_ENABLED fprintf(stderr, "RPM: Sending Reject!\n"); #endif } } pdu_len = apdu_len + npdu_len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); #if PRINT_ENABLED if (bytes_sent <= 0) { fprintf(stderr, "RPM: Failed to send PDU (%s)!\n", strerror(errno)); } #else bytes_sent = bytes_sent; #endif }
/** 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; }
/** Handler for a ReadProperty Service request. * @ingroup DSRP * This handler will be invoked by apdu_handler() if it has been enabled * by a call to apdu_set_confirmed_handler(). * This handler builds a response packet, which is * - an Abort if * - the message is segmented * - if decoding fails * - if the response would be too large * - the result from Device_Read_Property(), if it succeeds * - an Error if Device_Read_Property() fails * or there isn't enough room in the APDU to fit the data. * * @param service_request [in] The contents of the service request. * @param service_len [in] The length of the service_request. * @param src [in] BACNET_ADDRESS of the source of the message * @param service_data [in] The BACNET_CONFIRMED_SERVICE_DATA information * decoded from the APDU header of this message. */ void handler_read_property( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_READ_PROPERTY_DATA rpdata; int len = 0; int pdu_len = 0; int apdu_len = -1; int npdu_len = -1; BACNET_NPDU_DATA npdu_data; bool error = true; /* assume that there is an error */ int bytes_sent = 0; BACNET_ADDRESS my_address; /* configure default error code as an abort since it is common */ rpdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED; /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); npdu_len = npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address, &npdu_data); if (service_data->segmented_message) { /* we don't support segmentation - send an abort */ len = BACNET_STATUS_ABORT; #if PRINT_ENABLED fprintf(stderr, "RP: Segmented message. Sending Abort!\n"); #endif goto RP_FAILURE; } len = rp_decode_service_request(service_request, service_len, &rpdata); #if PRINT_ENABLED if (len <= 0) { fprintf(stderr, "RP: Unable to decode Request!\n"); } #endif if (len < 0) { /* bad decoding - skip to error/reject/abort handling */ error = true; #if PRINT_ENABLED fprintf(stderr, "RP: Bad Encoding.\n"); #endif goto RP_FAILURE; } /* Test for case of indefinite Device object instance */ if ((rpdata.object_type == OBJECT_DEVICE) && (rpdata.object_instance == BACNET_MAX_INSTANCE)) { rpdata.object_instance = Device_Object_Instance_Number(); } apdu_len = rp_ack_encode_apdu_init(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, &rpdata); /* configure our storage */ rpdata.application_data = &Handler_Transmit_Buffer[npdu_len + apdu_len]; rpdata.application_data_len = sizeof(Handler_Transmit_Buffer) - (npdu_len + apdu_len); len = Device_Read_Property(&rpdata); if (len >= 0) { apdu_len += len; len = rp_ack_encode_apdu_object_property_end(&Handler_Transmit_Buffer [npdu_len + apdu_len]); apdu_len += len; if (apdu_len > service_data->max_resp) { /* too big for the sender - send an abort * Setting of error code needed here as read property processing may * have overriden the default set at start */ rpdata.error_code = ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED; len = BACNET_STATUS_ABORT; #if PRINT_ENABLED fprintf(stderr, "RP: Message too large.\n"); #endif } else { #if PRINT_ENABLED fprintf(stderr, "RP: Sending Ack!\n"); #endif error = false; } } else { #if PRINT_ENABLED fprintf(stderr, "RP: Device_Read_Property: "); if (len == BACNET_STATUS_ABORT) { fprintf(stderr, "Abort!\n"); } else if (len == BACNET_STATUS_ERROR) { fprintf(stderr, "Error!\n"); } else if (len == BACNET_STATUS_REJECT) { fprintf(stderr, "Reject!\n"); } else { fprintf(stderr, "Unknown Len=%d\n", len); } #endif } RP_FAILURE: if (error) { if (len == BACNET_STATUS_ABORT) { apdu_len = abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, abort_convert_error_code(rpdata.error_code), true); #if PRINT_ENABLED fprintf(stderr, "RP: Sending Abort!\n"); #endif } else if (len == BACNET_STATUS_ERROR) { apdu_len = bacerror_encode_apdu(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, SERVICE_CONFIRMED_READ_PROPERTY, rpdata.error_class, rpdata.error_code); #if PRINT_ENABLED fprintf(stderr, "RP: Sending Error!\n"); #endif } else if (len == BACNET_STATUS_REJECT) { apdu_len = reject_encode_apdu(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, reject_convert_error_code(rpdata.error_code)); #if PRINT_ENABLED fprintf(stderr, "RP: Sending Reject!\n"); #endif } } pdu_len = npdu_len + apdu_len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); #if PRINT_ENABLED if (bytes_sent <= 0) { fprintf(stderr, "Failed to send PDU (%s)!\n", strerror(errno)); } #else bytes_sent = bytes_sent; #endif return; }
static bool cov_send_request( BACNET_COV_SUBSCRIPTION * cov_subscription, BACNET_PROPERTY_VALUE * value_list) { int len = 0; int pdu_len = 0; BACNET_NPDU_DATA npdu_data; BACNET_ADDRESS my_address; int bytes_sent = 0; uint8_t invoke_id = 0; bool status = false; /* return value */ BACNET_COV_DATA cov_data; if (!dcc_communication_enabled()) { return status; } #if PRINT_ENABLED fprintf(stderr, "COVnotification: requested\n"); #endif datalink_get_my_address(&my_address); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); pdu_len = npdu_encode_pdu(&Handler_Transmit_Buffer[0], &cov_subscription->dest, &my_address, &npdu_data); /* load the COV data structure for outgoing message */ cov_data.subscriberProcessIdentifier = cov_subscription->subscriberProcessIdentifier; cov_data.initiatingDeviceIdentifier = Device_Object_Instance_Number(); cov_data.monitoredObjectIdentifier.type = cov_subscription->monitoredObjectIdentifier.type; cov_data.monitoredObjectIdentifier.instance = cov_subscription->monitoredObjectIdentifier.instance; cov_data.timeRemaining = cov_subscription->lifetime; cov_data.listOfValues = value_list; if (cov_subscription->flag.issueConfirmedNotifications) { invoke_id = tsm_next_free_invokeID(); if (invoke_id) { cov_subscription->invokeID = invoke_id; len = ccov_notify_encode_apdu(&Handler_Transmit_Buffer[pdu_len], invoke_id, &cov_data); } else { goto COV_FAILED; } } else { len = ucov_notify_encode_apdu(&Handler_Transmit_Buffer[pdu_len], &cov_data); } pdu_len += len; if (cov_subscription->flag.issueConfirmedNotifications) { tsm_set_confirmed_unsegmented_transaction(invoke_id, &cov_subscription->dest, &npdu_data, &Handler_Transmit_Buffer[0], (uint16_t) pdu_len); } bytes_sent = datalink_send_pdu(&cov_subscription->dest, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); if (bytes_sent > 0) { status = true; } COV_FAILED: return status; }
/** 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; }
/** 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; }
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; }
/** 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; }
void Notification_Class_common_reporting_function( BACNET_EVENT_NOTIFICATION_DATA * event_data) { /* Fill the parameters common for all types of events. */ NOTIFICATION_CLASS_INFO *CurrentNotify; BACNET_DESTINATION *pBacDest; uint32_t notify_index; uint8_t index; notify_index = Notification_Class_Instance_To_Index(event_data->notificationClass); if (notify_index < MAX_NOTIFICATION_CLASSES) CurrentNotify = &NC_Info[notify_index]; else return; /* Initiating Device Identifier */ event_data->initiatingObjectIdentifier.type = OBJECT_DEVICE; event_data->initiatingObjectIdentifier.instance = Device_Object_Instance_Number(); /* Priority and AckRequired */ switch (event_data->toState) { case EVENT_STATE_NORMAL: event_data->priority = CurrentNotify->Priority[TRANSITION_TO_NORMAL]; event_data->ackRequired = (CurrentNotify-> Ack_Required & TRANSITION_TO_NORMAL_MASKED) ? true : false; break; case EVENT_STATE_FAULT: event_data->priority = CurrentNotify->Priority[TRANSITION_TO_FAULT]; event_data->ackRequired = (CurrentNotify-> Ack_Required & TRANSITION_TO_FAULT_MASKED) ? true : false; break; case EVENT_STATE_OFFNORMAL: case EVENT_STATE_HIGH_LIMIT: case EVENT_STATE_LOW_LIMIT: event_data->priority = CurrentNotify->Priority[TRANSITION_TO_OFFNORMAL]; event_data->ackRequired = (CurrentNotify->Ack_Required & TRANSITION_TO_OFFNORMAL_MASKED) ? true : false; break; default: /* shouldn't happen */ break; } /* send notifications for active recipients */ /* pointer to first recipient */ pBacDest = &CurrentNotify->Recipient_List[0]; for (index = 0; index < NC_MAX_RECIPIENTS; index++, pBacDest++) { /* check if recipient is defined */ if (pBacDest->Recipient.RecipientType == RECIPIENT_TYPE_NOTINITIALIZED) break; /* recipient doesn't defined - end of list */ if (IsRecipientActive(pBacDest, event_data->toState) == true) { BACNET_ADDRESS dest; uint32_t device_id; unsigned max_apdu; /* Process Identifier */ event_data->processIdentifier = pBacDest->ProcessIdentifier; /* send notification */ if (pBacDest->Recipient.RecipientType == RECIPIENT_TYPE_DEVICE) { /* send notification to the specified device */ device_id = pBacDest->Recipient._.DeviceIdentifier; if (pBacDest->ConfirmedNotify == true) Send_CEvent_Notify(device_id, event_data); else if (address_get_by_device(device_id, &max_apdu, &dest)) Send_UEvent_Notify(Handler_Transmit_Buffer, event_data, &dest); } else if (pBacDest->Recipient.RecipientType == RECIPIENT_TYPE_ADDRESS) { /* send notification to the address indicated */ if (pBacDest->ConfirmedNotify == true) { if (address_get_device_id(&dest, &device_id)) Send_CEvent_Notify(device_id, event_data); } else { dest = pBacDest->Recipient._.Address; Send_UEvent_Notify(Handler_Transmit_Buffer, event_data, &dest); } } } } }