int rd_decode_apdu( uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, BACNET_REINITIALIZED_STATE * state, BACNET_CHARACTER_STRING * password) { int len = 0; unsigned offset = 0; if (!apdu) return -1; /* optional checking - most likely was already done prior to this call */ if (apdu[0] != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) return -1; /* apdu[1] = encode_max_segs_max_apdu(0, MAX_APDU); */ *invoke_id = apdu[2]; /* invoke id - filled in by net layer */ if (apdu[3] != SERVICE_CONFIRMED_REINITIALIZE_DEVICE) return -1; offset = 4; if (apdu_len > offset) { len = rd_decode_service_request(&apdu[offset], apdu_len - offset, state, password); } return len; }
/** Handler for a Reinitialize Device (RD) request. * @ingroup DMRD * 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 * - an Error if * - the RD password is incorrect * - the Reinitialize Device operation fails * - a Reject if the request state is invalid * - else tries to send a simple ACK for the RD on success. * * @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_reinitialize_device( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_REINITIALIZE_DEVICE_DATA rd_data; int len = 0; int pdu_len = 0; BACNET_NPDU_DATA npdu_data; BACNET_ADDRESS my_address; /* encode the NPDU portion of the packet */ 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], src, &my_address, &npdu_data); #if PRINT_ENABLED fprintf(stderr, "ReinitializeDevice!\n"); #endif if (service_data->segmented_message) { len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); #if PRINT_ENABLED fprintf(stderr, "ReinitializeDevice: Sending Abort - segmented message.\n"); #endif goto RD_ABORT; } /* decode the service request only */ len = rd_decode_service_request(service_request, service_len, &rd_data.state, &rd_data.password); #if PRINT_ENABLED if (len > 0) { fprintf(stderr, "ReinitializeDevice: state=%u password=%s\n", (unsigned) rd_data.state, characterstring_value(&rd_data.password)); } else { fprintf(stderr, "ReinitializeDevice: Unable to decode request!\n"); } #endif /* bad decoding or something we didn't understand - send an abort */ if (len < 0) { len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_OTHER, true); #if PRINT_ENABLED fprintf(stderr, "ReinitializeDevice: Sending Abort - could not decode.\n"); #endif goto RD_ABORT; } /* check the data from the request */ if (rd_data.state >= MAX_BACNET_REINITIALIZED_STATE) { len = reject_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, REJECT_REASON_UNDEFINED_ENUMERATION); #if PRINT_ENABLED fprintf(stderr, "ReinitializeDevice: Sending Reject - undefined enumeration\n"); #endif } else { #if BAC_ROUTING /* Check to see if the current Device supports this service. */ len = Routed_Device_Service_Approval (SERVICE_CONFIRMED_REINITIALIZE_DEVICE, (int) rd_data.state, &Handler_Transmit_Buffer[pdu_len], service_data->invoke_id); if (len > 0) goto RD_ABORT; #endif if (Device_Reinitialize(&rd_data)) { len = encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_REINITIALIZE_DEVICE); #if PRINT_ENABLED fprintf(stderr, "ReinitializeDevice: Sending Simple Ack!\n"); #endif } else { len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_REINITIALIZE_DEVICE, rd_data.error_class, rd_data.error_code); #if PRINT_ENABLED fprintf(stderr, "ReinitializeDevice: Sending Error.\n"); #endif } } RD_ABORT: pdu_len += len; len = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); if (len <= 0) { #if PRINT_ENABLED fprintf(stderr, "ReinitializeDevice: Failed to send PDU (%s)!\n", strerror(errno)); #endif } return; }
void handler_reinitialize_device( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_REINITIALIZED_STATE state; BACNET_CHARACTER_STRING their_password; int len = 0; int pdu_len = 0; BACNET_NPDU_DATA npdu_data; int bytes_sent = 0; BACNET_ADDRESS my_address; /* encode the NPDU portion of the packet */ 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], src, &my_address, &npdu_data); if (service_data->segmented_message) { len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); goto RD_ABORT; } /* decode the service request only */ len = rd_decode_service_request(service_request, service_len, &state, &their_password); /* bad decoding or something we didn't understand - send an abort */ if (len < 0) { len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_OTHER, true); goto RD_ABORT; } /* check the data from the request */ if (state >= MAX_BACNET_REINITIALIZED_STATE) { len = reject_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, REJECT_REASON_UNDEFINED_ENUMERATION); } else { characterstring_init_ansi(&My_Password, Password); if (characterstring_same(&their_password, &My_Password)) { len = encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_REINITIALIZE_DEVICE); /* FIXME: now you can reboot, restart, quit, or something clever */ /* Note: you can use a mix of state and password to do specific stuff */ /* Note: if you don't do something clever like actually restart, you probably should clear any DCC status and timeouts */ /* Note: you probably need to send the reply BEFORE restarting */ } else { len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_REINITIALIZE_DEVICE, ERROR_CLASS_SERVICES, ERROR_CODE_PASSWORD_FAILURE); } } RD_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); return; }