Shouldn't this be moved also from pmc.c to util:
static char *bin2str(Octet *data, int len) {
static char buf[BIN_BUF_SIZE];
return bin2str_impl(data, len, buf, sizeof(buf)); }
/Anders
-----Ursprungligt meddelande-----
Från: Richard Cochran [mailto:[email protected]]
Skickat: den 1 mars 2018 20:41
Till: [email protected]
Ämne: [Linuxptp-devel] [PATCH RFC V2 10/11] util: Relocate utility functions
from pmc.c.
The file, pmc.c, contains utility functions for printing out a port address
structure. We will want to call these functions from pmc_common.c, and so this
patch moves the utility functions where they belong.
Signed-off-by: Richard Cochran <[email protected]>
---
pmc.c | 48 ------------------------------------------------
util.c | 39 +++++++++++++++++++++++++++++++++++++++
util.h | 15 +++++++++++++++
3 files changed, 54 insertions(+), 48 deletions(-)
diff --git a/pmc.c b/pmc.c
index fde2449..7c96a4d 100644
--- a/pmc.c
+++ b/pmc.c
@@ -127,60 +127,12 @@ static char *text2str(struct PTPText *text)
return (char*)(s.text);
}
-#define MAX_PRINT_BYTES 16
-#define BIN_BUF_SIZE (MAX_PRINT_BYTES * 3 + 1)
-
-static char *bin2str_impl(Octet *data, int len, char *buf, int buf_len) -{
- int i, offset = 0;
- if (len > MAX_PRINT_BYTES)
- len = MAX_PRINT_BYTES;
- buf[0] = '\0';
- if (!data)
- return buf;
- if (len)
- offset += snprintf(buf, buf_len, "%02hhx", data[0]);
- for (i = 1; i < len; i++) {
- if (offset >= buf_len)
- /* truncated output */
- break;
- offset += snprintf(buf + offset, buf_len - offset, ":%02hhx",
data[i]);
- }
- return buf;
-}
-
static char *bin2str(Octet *data, int len) {
static char buf[BIN_BUF_SIZE];
return bin2str_impl(data, len, buf, sizeof(buf)); }
-static uint16_t align16(uint16_t *p)
-{
- uint16_t v;
- memcpy(&v, p, sizeof(v));
- return v;
-}
-
-static char *portaddr2str(struct PortAddress *addr) -{
- static char buf[BIN_BUF_SIZE];
- switch(align16(&addr->networkProtocol)) {
- case TRANS_UDP_IPV4:
- if (align16(&addr->addressLength) == 4
- && inet_ntop(AF_INET, addr->address, buf, sizeof(buf)))
- return buf;
- break;
- case TRANS_UDP_IPV6:
- if (align16(&addr->addressLength) == 16
- && inet_ntop(AF_INET6, addr->address, buf, sizeof(buf)))
- return buf;
- break;
- }
- bin2str_impl(addr->address, align16(&addr->addressLength), buf,
sizeof(buf));
- return buf;
-}
-
static void pmc_show(struct ptp_message *msg, FILE *fp) {
int action;
diff --git a/util.c b/util.c
index 62f2638..2eacafc 100644
--- a/util.c
+++ b/util.c
@@ -16,6 +16,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
@@ -68,6 +69,25 @@ const char *ev_str[] = {
"RS_PASSIVE",
};
+char *bin2str_impl(Octet *data, int len, char *buf, int buf_len) {
+ int i, offset = 0;
+ if (len > MAX_PRINT_BYTES)
+ len = MAX_PRINT_BYTES;
+ buf[0] = '\0';
+ if (!data)
+ return buf;
+ if (len)
+ offset += snprintf(buf, buf_len, "%02hhx", data[0]);
+ for (i = 1; i < len; i++) {
+ if (offset >= buf_len)
+ /* truncated output */
+ break;
+ offset += snprintf(buf + offset, buf_len - offset, ":%02hhx",
data[i]);
+ }
+ return buf;
+}
+
char *cid2str(struct ClockIdentity *id) {
static char buf[64];
@@ -100,6 +120,25 @@ char *pid2str(struct PortIdentity *id)
return buf;
}
+char *portaddr2str(struct PortAddress *addr) {
+ static char buf[BIN_BUF_SIZE];
+ switch (align16(&addr->networkProtocol)) {
+ case TRANS_UDP_IPV4:
+ if (align16(&addr->addressLength) == 4
+ && inet_ntop(AF_INET, addr->address, buf, sizeof(buf)))
+ return buf;
+ break;
+ case TRANS_UDP_IPV6:
+ if (align16(&addr->addressLength) == 16
+ && inet_ntop(AF_INET6, addr->address, buf, sizeof(buf)))
+ return buf;
+ break;
+ }
+ bin2str_impl(addr->address, align16(&addr->addressLength), buf,
sizeof(buf));
+ return buf;
+}
+
int str2mac(const char *s, unsigned char mac[MAC_LEN]) {
unsigned char buf[MAC_LEN];
diff --git a/util.h b/util.h
index 0c1a357..41fbdb2 100644
--- a/util.h
+++ b/util.h
@@ -20,11 +20,15 @@
#ifndef HAVE_UTIL_H
#define HAVE_UTIL_H
+#include <string.h>
#include <time.h>
#include "ddt.h"
#include "ether.h"
+#define MAX_PRINT_BYTES 16
+#define BIN_BUF_SIZE (MAX_PRINT_BYTES * 3 + 1)
+
/**
* Table of human readable strings, one for each port state.
*/
@@ -35,6 +39,15 @@ extern const char *ps_str[];
*/
extern const char *ev_str[];
+static inline uint16_t align16(uint16_t *p) {
+ uint16_t v;
+ memcpy(&v, p, sizeof(v));
+ return v;
+}
+
+char *bin2str_impl(Octet *data, int len, char *buf, int buf_len);
+
/**
* Convert a clock identity into a human readable string.
*
@@ -65,6 +78,8 @@ int count_char(const char *str, char c);
*/
char *pid2str(struct PortIdentity *id);
+char *portaddr2str(struct PortAddress *addr);
+
/**
* Scan a string containing a MAC address and convert it into binary form.
*
--
2.11.0
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Linuxptp-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Linuxptp-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel