From 83b4bd6a7132e5c9976ff8ff9d5733c6017d6fb1 Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Wed, 8 Aug 2012 17:03:28 +0000 Subject: [PATCH] enabling set_timeout and show_mgmtIds commands --- tools/ptpmanager/constants.h | 1 + tools/ptpmanager/network.c | 33 +++++++++++++-- tools/ptpmanager/packOutgoingMsg.c | 2 +- tools/ptpmanager/ptpmanager.c | 32 +++++--------- tools/ptpmanager/ptpmanager.h | 2 + tools/ptpmanager/show.c | 68 +++++++++++++++++++++++++----- 6 files changed, 103 insertions(+), 35 deletions(-) diff --git a/tools/ptpmanager/constants.h b/tools/ptpmanager/constants.h index b9ef3874..28b15ae9 100644 --- a/tools/ptpmanager/constants.h +++ b/tools/ptpmanager/constants.h @@ -8,6 +8,7 @@ #define MANAGEMENT 13 #define TLV_LENGTH 6 #define USER_DESCRIPTION_MAX 128 +#define DEFAULT_TIMEOUT 10 #define flip16(x) htons(x) #define flip32(x) htonl(x) diff --git a/tools/ptpmanager/network.c b/tools/ptpmanager/network.c index 6546ad95..d60ba15e 100644 --- a/tools/ptpmanager/network.c +++ b/tools/ptpmanager/network.c @@ -89,25 +89,52 @@ netSendGeneral(Octet * buf, UInteger16 length, char *ip) return (ret); } +/*Function to set timeout as provided by user */ +void +set_timeout() +{ + printf("Timeout value in seconds? "); + scanf("%d",&timeout); +} + /* Function to receive the management response/ack/error message */ ssize_t -netRecv(Octet *message, char *src) +netRecv(Octet *message, char *dest) { ssize_t ret = 0; char srcIp[16]; int try; + int ret_select; + fd_set socks; struct sockaddr_in client_addr; socklen_t len = sizeof(client_addr); + struct timeval timeout_val; + + FD_ZERO(&socks); + FD_SET(netPath->generalSock, &socks); + + timeout_val.tv_sec = timeout; + timeout_val.tv_usec = 0; - for (try = 0;try < 3; try++){ + if ((ret_select = select(netPath->generalSock + 1, &socks, NULL, NULL, + &timeout_val)) != 0) { ret = recvfrom(netPath->generalSock, message, PACKET_SIZE , 0 , (struct sockaddr *)&client_addr, &len); if (ret == 0 || (strcmp(inet_ntop(AF_INET, &(client_addr.sin_addr), srcIp, - sizeof(srcIp)), src) == 0)) + sizeof(srcIp)), dest) == 0)) return (ret); else printf("false\n"); } + else if (ret_select < 0){ + printf("Network error (select)\n"); + return 0; + } + else { + printf("Timed out. Please check that server is reachable and try again." + " You can use 'send_previous'\n"); + return 0; + } } /*shutdown the network layer*/ diff --git a/tools/ptpmanager/packOutgoingMsg.c b/tools/ptpmanager/packOutgoingMsg.c index 8421c7d6..dc7fa40b 100644 --- a/tools/ptpmanager/packOutgoingMsg.c +++ b/tools/ptpmanager/packOutgoingMsg.c @@ -61,7 +61,7 @@ packOutgoingMsg(Octet *buf) scanf("%d", &tlvtype); if (tlvtype==TLV_MANAGEMENT){ - printf(">managementId (see Table 40) (Eg: '2010' for ClockAccuracy)?"); + printf(">managementId (use command 'show_mgmtIds' to find managementId)?"); scanf("%x", &managementId); switch(managementId){ diff --git a/tools/ptpmanager/ptpmanager.c b/tools/ptpmanager/ptpmanager.c index fc0fd52a..2b406e18 100644 --- a/tools/ptpmanager/ptpmanager.c +++ b/tools/ptpmanager/ptpmanager.c @@ -27,9 +27,9 @@ getCommandId(char *command) return (6); else if (strcmp(command, "show_tlv")==0) return (7); - else if (strcmp(command, "show_clock_description")==0) + else if (strcmp(command, "show_mgmtIds") == 0) return (8); - else if (strcmp(command, "show_default_data_set")==0) + else if (strcmp(command, "set_timeout") == 0) return (9); return (0); /* for wrong command */ @@ -45,22 +45,11 @@ sendMessage(Octet* outmessage, char *dest) printf("Message sent, waiting for response...\n"); receivedFlag = FALSE; -// for (;;){ - - /* - * TODO wait for some time to receive a response - * or ack, if not received till timeout send the - * management message again, Use receivedFlag for this. - * - * Currently, 'receive' is 'blocking' and therefore program hangs if - * packet delivery fails. TODO: Use non-blocking call. - */ - memset(inmessage, 0, PACKET_SIZE); - if (netRecv(inmessage, dest)) { - handleIncomingMsg(inmessage); - in_sequence++; - } - //} + memset(inmessage, 0, PACKET_SIZE); + if (netRecv(inmessage, dest)) { + handleIncomingMsg(inmessage); + in_sequence++; + } } } @@ -81,6 +70,7 @@ main(int argc, char *argv[ ]) out_sequence = 0; in_sequence = 0; out_length = 0; + timeout = DEFAULT_TIMEOUT; netPath = (NetPath*)malloc(sizeof(NetPath)); if (outmessage == NULL || inmessage == NULL || netPath == NULL){ @@ -156,11 +146,11 @@ main(int argc, char *argv[ ]) break; case 8: - show_clock_description(); + show_mgmtIds(); break; - + case 9: - show_default_data_set(); + set_timeout(); break; default: diff --git a/tools/ptpmanager/ptpmanager.h b/tools/ptpmanager/ptpmanager.h index 99c1ebbe..3c2b3159 100644 --- a/tools/ptpmanager/ptpmanager.h +++ b/tools/ptpmanager/ptpmanager.h @@ -52,6 +52,8 @@ UInteger16 in_sequence; Boolean receivedFlag; Octet *outmessage; Octet *inmessage; +UInteger32 timeout; + /** * \brief Struct used to store network datas */ diff --git a/tools/ptpmanager/show.c b/tools/ptpmanager/show.c index 5dd5d082..1b21a33c 100644 --- a/tools/ptpmanager/show.c +++ b/tools/ptpmanager/show.c @@ -17,8 +17,7 @@ show_help() show_commonheader - to see header (Table 18 of spec)\n\ show_managementheader - to see management header (Table 37)\n\ show_tlv - to see management TLV fields (Table 35)\n\ - show_clock_description - to see dataField of clock_description TLV (Table 41)\n\ - show_default_data_set - to see fields of default data set (Table 50)\n\ + show_mgmtIds - to see all available management TLVs\n\ quit - to close the program\n\ ..etc\n\n"); } @@ -42,13 +41,62 @@ show_tlv() } void -show_clock_description() +show_mgmtIds() { - printf("Not implemented yet\n"); -} - -void -show_default_data_set() -{ - printf("Not implemented yet\n"); + printf("managementId name \tmanagementId value (hex) \tAllowed actions \tApplies to\n" + "\n" + "===Applicable to all node types===================================================================================\n" + "NULL_MANAGEMENT \t0000 \tGET, SET, COMMAND \tport\n" + "CLOCK_DESCRIPTION \t0001 \tGET \tport\n" + "USER_DESCRIPTION \t0002 \tGET, SET \tclock\n" + "SAVE_IN_NON_VOLATILE_STORAGE \t0003 \tCOMMAND \tclock\n" + "RESET_NON_VOLATILE_STORAGE \t0004 \tCOMMAND \tclock\n" + "INITIALIZE \t0005 \tCOMMAND \tclock\n" + "FAULT_LOG \t0006 \tGET \tclock\n" + "FAULT_LOG_RESET \t0007 \tCOMMAND \tclock\n" + "\n" + "===Applicable to ordinary and boundary clocks=====================================================================\n" + "DEFAULT_DATA_SET \t2000 \tGET \tclock\n" + "CURRENT_DATA_SET \t2001 \tGET \tclock\n" + "PARENT_DATA_SET \t2002 \tGET \tclock\n" + "TIME_PROPERTIES_DATA_SET \t2003 \tGET \tclock\n" + "PORT_DATA_SET \t2004 \tGET \tport\n" + "PRIORITY1 \t2005 \tGET, SET \tclock\n" + "PRIORITY2 \t2006 \tGET, SET \tclock\n" + "DOMAIN \t2007 \tGET, SET \tclock\n" + "SLAVE_ONLY \t2008 \tGET, SET \tclock\n" + "LOG_ ANNOUNCE_INTERVAL \t2009 \tGET, SET \tport\n" + "ANNOUNCE_RECEIPT_TIMEOUT \t200A \tGET, SET \tport\n" + "LOG_ SYNC_INTERVAL \t200B \tGET, SET \tport\n" + "VERSION_NUMBER \t200C \tGET, SET \tport\n" + "ENABLE_PORT \t200D \tCOMMAND \tport\n" + "DISABLE_PORT \t200E \tCOMMAND \tport\n" + "TIME \t200F \tGET, SET \tclock\n" + "CLOCK_ACCURACY \t2010 \tGET, SET \tclock\n" + "UTC_PROPERTIES \t2011 \tGET, SET \tclock\n" + "TRACEABILITY_PROPERTIES \t2012 \tGET, SET \tclock\n" + "TIMESCALE_PROPERTIES \t2013 \tGET, SET \tclock\n" + "UNICAST_NEGOTIATION_ENABLE \t2014 \tGET, SET \tport\n" + "PATH_TRACE_LIST \t2015 \tGET \tclock\n" + "PATH_TRACE_ENABLE \t2016 \tGET, SET \tclock\n" + "GRANDMASTER_CLUSTER_TABLE \t2017 \tGET, SET \tclock\n" + "UNICAST_MASTER_TABLE \t2018 \tGET, SET \tport\n" + "UNICAST_MASTER_MAX_TABLE_SIZE \t2019 \tGET \tport\n" + "ACCEPTABLE_MASTER_TABLE \t201A \tGET, SET \tclock\n" + "ACCEPTABLE_MASTER_TABLE_ENABLED \t201B \tGET, SET \tport\n" + "ACCEPTABLE_MASTER_MAX_TABLE_SIZE \t201C \tGET \tclock\n" + "ALTERNATE_MASTER \t201D \tGET, SET \tport\n" + "ALTERNATE_TIME_OFFSET_ENABLE \t201E \tGET, SET \tclock\n" + "ALTERNATE_TIME_OFFSET_NAME \t201F \tGET, SET \tclock\n" + "ALTERNATE_TIME_OFFSET_MAX_KEY \t2020 \tGET \tclock\n" + "ALTERNATE_TIME_OFFSET_PROPERTIES \t2021 \tGET, SET \tclock\n" + "\n" + "===Applicable to transparent clocks==============================================================================\n" + "TRANSPARENT_CLOCK_DEFAULT_DATA_SET \t4000 \tGET \tclock\n" + "TRANSPARENT_CLOCK_PORT_DATA_SET \t4001 \tGET \tport\n" + "PRIMARY_DOMAIN \t4002 \tGET, SET \tclock\n" + "\n" + "===Applicable to ordinary, boundary, and transparent clocks======================================================\n" + "DELAY_MECHANISM \t6000 \tGET, SET \tport\n" + "LOG_MIN_ PDELAY_REQ_INTERVAL \t6001 \tGET, SET \tport\n"); }