int write_bacnet_obj(bacnet_device_t *device, int obj_index, char *value) { uint8_t rx_buf[MAX_MPDU] = {0}; BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; unsigned timeout = 500; /* milliseconds */ unsigned max_apdu = 0; time_t elapsed_seconds = 0; time_t last_seconds = 0; time_t current_seconds = 0; time_t timeout_seconds = 0; int status = 0, i; // Set Property Tag BACNET_APPLICATION_TAG property_tag; if (device -> obj_types[obj_index] == OBJECT_BINARY_VALUE) property_tag = BACNET_APPLICATION_TAG_ENUMERATED; else property_tag = BACNET_APPLICATION_TAG_REAL; BACNET_APPLICATION_DATA_VALUE target_value; for (i = 0; i < MAX_PROPERTY_VALUES; i++) { target_value.context_specific = false; if (property_tag >= MAX_BACNET_APPLICATION_TAG) { fprintf(stderr, "Error: tag=%u - it must be less than %u\r\n", property_tag, MAX_BACNET_APPLICATION_TAG); return 1; } status = bacapp_parse_application_data(property_tag, value, &target_value); if (!status) { printf("Error: unable to parse the tag value\r\n"); return 1; } target_value.next = NULL; break; } /* configure the timeout values */ last_seconds = time(NULL); timeout_seconds = (apdu_timeout() / 1000) * apdu_retries(); /* try to bind with the device */ found = address_bind_request(device -> instance, &max_apdu, &target_address); if (!found) { Send_WhoIs(device -> instance, device -> instance); } /* loop forever */ request_invoke_id = 0; while (1) { /* increment timer - exit if timed out */ current_seconds = time(NULL); /* at least one second has passed */ if (current_seconds != last_seconds) tsm_timer_milliseconds((uint16_t) ((current_seconds - last_seconds) * 1000)); if (error_detected) break; /* wait until the device is bound, or timeout and quit */ if (!found) { found = address_bind_request(device -> instance, &max_apdu, &target_address); } if (found) { if (request_invoke_id == 0) { printf("request invoke set\n"); write_acked = 0; request_invoke_id = Send_Write_Property_Request(device -> instance, device -> obj_types[obj_index], device -> obj_ids[obj_index], PROP_PRESENT_VALUE, &target_value, MESSAGE_PRIORITY_NORMAL, -1); } else if (tsm_invoke_id_free(request_invoke_id)) break; else if (tsm_invoke_id_failed(request_invoke_id)) { fprintf(stderr, "\rError: TSM Timeout!\r\n"); tsm_free_invoke_id(request_invoke_id); error_detected = true; break; } } else { /* increment timer - exit if timed out */ elapsed_seconds += (current_seconds - last_seconds); if (elapsed_seconds > timeout_seconds) { error_detected = true; printf("\rError: APDU Timeout!\r\n"); break; } } /* 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); } /* keep track of time for next check */ last_seconds = current_seconds; } if (error_detected) { error_detected = 0; return 1; } return 0; }
int BacnetWriteProperty( int deviceInstanceNumber, int objectType, int objectInstanceNumber, int objectProperty, int objectPriority, int objectIndex, const char *tag, const char *value) { char msg[MAX_ERROR_STRING]; int isFailure = 1; if (!isWritePropertyHandlerRegistered) { /* handle the ack coming back */ apdu_set_confirmed_simple_ack_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, My_Write_Property_SimpleAck_Handler); /* handle any errors coming back */ apdu_set_error_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, My_Error_Handler); /* indicate that handlers are now registered */ isWritePropertyHandlerRegistered = true; } if (objectIndex == -1) { objectIndex = BACNET_ARRAY_ALL; } /* Loop for eary exit; */ do { /* Handle the tag/value pair */ uint8_t context_tag = 0; BACNET_APPLICATION_TAG property_tag; BACNET_APPLICATION_DATA_VALUE propertyValue; if (toupper(tag[0]) == 'C') { context_tag = strtol(&tag[1], NULL, 0); propertyValue.context_tag = context_tag; propertyValue.context_specific = true; } else { propertyValue.context_specific = false; } property_tag = strtol(tag, NULL, 0); if (property_tag >= MAX_BACNET_APPLICATION_TAG) { sprintf(msg, "Error: tag=%u - it must be less than %u", property_tag, MAX_BACNET_APPLICATION_TAG); LogError(msg); break; } if (!bacapp_parse_application_data(property_tag, value, &propertyValue)) { sprintf(msg, "Error: unable to parse the tag value"); LogError(msg); break; } propertyValue.next = NULL; /* Send out the message */ Request_Invoke_ID = Send_Write_Property_Request(deviceInstanceNumber, objectType, objectInstanceNumber, objectProperty, &propertyValue, objectPriority, objectIndex); Wait_For_Answer_Or_Timeout(100, waitAnswer); /* If we get here, then there were no explicit failures. However, there */ /* could have been implicit failures. Let's look at those also. */ isFailure = Error_Detected; } while (false); /* Clean up after ourselves. */ Error_Detected = false; return isFailure; }
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 max_apdu = 0; time_t elapsed_seconds = 0; time_t last_seconds = 0; time_t current_seconds = 0; time_t timeout_seconds = 0; bool found = false; char *value_string = NULL; bool status = false; int args_remaining = 0, tag_value_arg = 0, i = 0; BACNET_APPLICATION_TAG property_tag; uint8_t context_tag = 0; int argi = 0; /* print help if requested */ for (argi = 1; argi < argc; argi++) { if (strcmp(argv[argi], "--help") == 0) { print_usage(filename_remove_path(argv[0])); print_help(filename_remove_path(argv[0])); return 0; } if (strcmp(argv[argi], "--version") == 0) { printf("%s %s\n", filename_remove_path(argv[0]), BACNET_VERSION_TEXT); printf("Copyright (C) 2014 by Steve Karg\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 (argc < 9) { print_usage(filename_remove_path(argv[0])); return 0; } /* decode the command line parameters */ Target_Device_Object_Instance = strtol(argv[1], NULL, 0); Target_Object_Type = strtol(argv[2], NULL, 0); Target_Object_Instance = strtol(argv[3], NULL, 0); Target_Object_Property = strtol(argv[4], NULL, 0); Target_Object_Property_Priority = (uint8_t) strtol(argv[5], NULL, 0); Target_Object_Property_Index = strtol(argv[6], NULL, 0); if (Target_Object_Property_Index == -1) Target_Object_Property_Index = BACNET_ARRAY_ALL; if (Target_Device_Object_Instance > BACNET_MAX_INSTANCE) { fprintf(stderr, "device-instance=%u - it must be less than %u\n", Target_Device_Object_Instance, BACNET_MAX_INSTANCE + 1); return 1; } if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE) { fprintf(stderr, "object-type=%u - it must be less than %u\n", Target_Object_Type, MAX_BACNET_OBJECT_TYPE + 1); return 1; } if (Target_Object_Instance > BACNET_MAX_INSTANCE) { fprintf(stderr, "object-instance=%u - it must be less than %u\n", Target_Object_Instance, BACNET_MAX_INSTANCE + 1); return 1; } if (Target_Object_Property > MAX_BACNET_PROPERTY_ID) { fprintf(stderr, "property=%u - it must be less than %u\n", Target_Object_Property, MAX_BACNET_PROPERTY_ID + 1); return 1; } args_remaining = (argc - 7); for (i = 0; i < MAX_PROPERTY_VALUES; i++) { tag_value_arg = 7 + (i * 2); /* special case for context tagged values */ if (toupper(argv[tag_value_arg][0]) == 'C') { context_tag = (uint8_t) strtol(&argv[tag_value_arg][1], NULL, 0); tag_value_arg++; args_remaining--; Target_Object_Property_Value[i].context_tag = context_tag; Target_Object_Property_Value[i].context_specific = true; } else { Target_Object_Property_Value[i].context_specific = false; } property_tag = strtol(argv[tag_value_arg], NULL, 0); args_remaining--; if (args_remaining <= 0) { fprintf(stderr, "Error: not enough tag-value pairs\n"); return 1; } value_string = argv[tag_value_arg + 1]; args_remaining--; /* printf("tag[%d]=%u value[%d]=%s\n", i, property_tag, i, value_string); */ if (property_tag >= MAX_BACNET_APPLICATION_TAG) { fprintf(stderr, "Error: tag=%u - it must be less than %u\n", property_tag, MAX_BACNET_APPLICATION_TAG); return 1; } status = bacapp_parse_application_data(property_tag, value_string, &Target_Object_Property_Value[i]); if (!status) { /* FIXME: show the expected entry format for the tag */ fprintf(stderr, "Error: unable to parse the tag value\n"); return 1; } Target_Object_Property_Value[i].next = NULL; if (i > 0) { Target_Object_Property_Value[i - 1].next = &Target_Object_Property_Value[i]; } if (args_remaining <= 0) { break; } } if (args_remaining > 0) { fprintf(stderr, "Error: Exceeded %d tag-value pairs.\n", MAX_PROPERTY_VALUES); return 1; } /* setup my info */ Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE); address_init(); Init_Service_Handlers(); dlenv_init(); atexit(datalink_cleanup); /* configure the timeout values */ last_seconds = time(NULL); timeout_seconds = (apdu_timeout() / 1000) * apdu_retries(); /* try to bind with the device */ found = address_bind_request(Target_Device_Object_Instance, &max_apdu, &Target_Address); if (!found) { Send_WhoIs(Target_Device_Object_Instance, Target_Device_Object_Instance); } /* loop forever */ for (;;) { /* increment timer - exit if timed out */ current_seconds = time(NULL); /* at least one second has passed */ if (current_seconds != last_seconds) tsm_timer_milliseconds((uint16_t) ((current_seconds - last_seconds) * 1000)); if (Error_Detected) break; /* wait until the device is bound, or timeout and quit */ if (!found) { found = address_bind_request(Target_Device_Object_Instance, &max_apdu, &Target_Address); } if (found) { if (Request_Invoke_ID == 0) { Request_Invoke_ID = Send_Write_Property_Request(Target_Device_Object_Instance, Target_Object_Type, Target_Object_Instance, Target_Object_Property, &Target_Object_Property_Value[0], Target_Object_Property_Priority, Target_Object_Property_Index); } else if (tsm_invoke_id_free(Request_Invoke_ID)) break; else if (tsm_invoke_id_failed(Request_Invoke_ID)) { fprintf(stderr, "\rError: TSM Timeout!\n"); tsm_free_invoke_id(Request_Invoke_ID); Error_Detected = true; /* try again or abort? */ break; } } else { /* increment timer - exit if timed out */ elapsed_seconds += (current_seconds - last_seconds); if (elapsed_seconds > timeout_seconds) { Error_Detected = true; printf("\rError: APDU Timeout!\n"); break; } } /* 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); } /* keep track of time for next check */ last_seconds = current_seconds; } if (Error_Detected) return 1; return 0; }