void handler_conf_private_trans( uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_PRIVATE_TRANSFER_DATA data; int len; int pdu_len; bool error; int bytes_sent; BACNET_NPDU_DATA npdu_data; BACNET_ADDRESS my_address; BACNET_ERROR_CLASS error_class; BACNET_ERROR_CODE error_code; len = 0; pdu_len = 0; error = false; bytes_sent = 0; error_class = ERROR_CLASS_OBJECT; error_code = ERROR_CODE_UNKNOWN_OBJECT; #if PRINT_ENABLED fprintf(stderr, "Received Confirmed Private Transfer Request!\n"); #endif /* encode the NPDU portion of the response packet as it will be needed */ /* no matter what the outcome. */ 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); #if PRINT_ENABLED fprintf(stderr, "CPT: Segmented Message. Sending Abort!\n"); #endif goto CPT_ABORT; } len = ptransfer_decode_service_request(service_request, service_len, &data); /* bad decoding - 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, "CPT: Bad Encoding. Sending Abort!\n"); #endif goto CPT_ABORT; } /* Simple example with service number of 0 for read block and 1 for write block We also only support our own vendor ID. In theory we could support others for compatability purposes but these interfaces are rarely documented... */ if ((data.vendorID == BACNET_VENDOR_ID) && (data.serviceNumber <= MY_SVC_WRITE)) { /* We only try to understand our own IDs and service numbers */ /* Will either return a result block or an app level status block */ ProcessPT(&data); if (data.serviceParametersLen == 0) { /* No respopnse means fatal error */ error = true; error_class = ERROR_CLASS_SERVICES; error_code = ERROR_CODE_OTHER; #if PRINT_ENABLED fprintf(stderr, "CPT: Error servicing request!\n"); #endif } len = ptransfer_ack_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, &data); } else { /* Not our vendor ID or bad service parameter */ error = true; error_class = ERROR_CLASS_SERVICES; error_code = ERROR_CODE_OPTIONAL_FUNCTIONALITY_NOT_SUPPORTED; #if PRINT_ENABLED fprintf(stderr, "CPT: Not our Vendor ID or invalid service code!\n"); #endif } if (error) { len = ptransfer_error_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, error_class, error_code, &data); } CPT_ABORT: pdu_len += 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)); } #endif return; }
int main(int argc, char **argv) { int nRet = 0; int nPortUDP = 1234; int nPortTCP = 9878; int i_PktNumb; struct Ctl_Pkt control_pkt; int c; //use getopt to parse the command line while ((c = getopt(argc, argv, "p:")) != EOF) { switch (c) { case 'p': printf("udp portnumber: %d\n", atoi(optarg)); nPortUDP = atoi(optarg); break; case '?': printf("ERROR: illegal option %s\n", argv[optind-1]); printf("Usage:\n"); printf("\t%s -p udp_portnumber\n", argv[0]); exit(1); break; default: printf("WARNING: no handler for option %c\n", c); printf("Usage:\n"); printf("\t%s -p udp_portnumber\n", argv[0]); exit(1); break; } } signal(SIGINT, CleanUp); UDPServer(nPortUDP); TCPServer(nPortTCP); while(1) { nRet = recv(tcpSocket, (char *) &control_pkt, sizeof(control_pkt), 0); if (nRet <= 0 ) // peer closed { break; } // what does the client want to do? PP/PT? if (nRet!=sizeof(control_pkt) || (control_pkt.option != PacketPair && control_pkt.option!=PacketTrain)) { printf("Receive unknow message %d, with size %d\n", control_pkt.option, nRet); exit(1); } i_PktNumb = control_pkt.value; switch (control_pkt.option) { case PacketPair: control_pkt.option = Ready; if (send(tcpSocket, (char *) &control_pkt, sizeof(control_pkt), 0) != sizeof(control_pkt)) { perror("Send TCP control packet error"); exit(1); } // receive PP UDPReceive(PacketPair, i_PktNumb); control_pkt.option = PacketPair; allCE = ProcessPP(i_PktNumb); control_pkt.value = (unsigned int) (allCE * 1000000); if (send(tcpSocket, (char *) &control_pkt, sizeof(control_pkt), 0) != sizeof(control_pkt)) { perror("Send TCP control packet error"); exit(1); } break; case PacketTrain: control_pkt.option = Ready; if (send(tcpSocket, (char *) &control_pkt, sizeof(control_pkt), 0) != sizeof(control_pkt)) { perror("Send TCP control packet error"); exit(1); } // receive PT UDPReceive(PacketTrain, i_PktNumb); control_pkt.option = PacketTrain; allAB = ProcessPT(i_PktNumb); control_pkt.value = (unsigned int)(allAB * 1000000); if (send(tcpSocket, (char *) &control_pkt, sizeof(control_pkt), 0) != sizeof(control_pkt)) { perror("Send TCP control packet error"); exit(1); } break; default: break; } } //end while(1) CleanUp (0); return 0; } // end main()