One mib object is initialized in master agent and data is received
from ptp4l.

Signed-off-by: Anders Selhammer <[email protected]>
---
 makefile      |   4 +-
 ptpbase_mib.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ptpbase_mib.h |  30 ++++++++++++++
 snmpd.c       |  39 ++++++++++++++++++
 4 files changed, 201 insertions(+), 2 deletions(-)
 create mode 100644 ptpbase_mib.c
 create mode 100644 ptpbase_mib.h

diff --git a/makefile b/makefile
index 68ea3e0..a458540 100644
--- a/makefile
+++ b/makefile
@@ -29,7 +29,7 @@ OBJ     = bmc.o clock.o clockadj.o clockcheck.o config.o 
e2e_tc.o fault.o \
  telecom.o tlv.o transport.o tsproc.o udp.o udp6.o uds.o util.o version.o
 
 OBJECTS        = $(OBJ) hwstamp_ctl.o nsm.o phc2sys.o phc_ctl.o pmc.o 
pmc_common.o \
- snmpd.o sysoff.o timemaster.o
+ ptpbase_mib.o snmpd.o sysoff.o timemaster.o
 SRC    = $(OBJECTS:.o=.c)
 DEPEND = $(OBJECTS:.o=.d)
 srcdir := $(dir $(lastword $(MAKEFILE_LIST)))
@@ -51,7 +51,7 @@ ptp4l: $(OBJ)
 nsm: config.o filter.o hash.o mave.o mmedian.o msg.o nsm.o print.o raw.o \
  rtnl.o sk.o transport.o tlv.o tsproc.o udp.o udp6.o uds.o util.o version.o
 
-snmpd: config.o hash.o msg.o pmc_common.o print.o raw.o sk.o \
+snmpd: config.o hash.o msg.o pmc_common.o print.o ptpbase_mib.o raw.o sk.o \
  snmpd.o tlv.o transport.o udp.o udp6.o uds.o util.o
 
 pmc: config.o hash.o msg.o pmc.o pmc_common.o print.o raw.o sk.o tlv.o \
diff --git a/ptpbase_mib.c b/ptpbase_mib.c
new file mode 100644
index 0000000..a73987b
--- /dev/null
+++ b/ptpbase_mib.c
@@ -0,0 +1,130 @@
+/**
+ * @file ptpbase_mib.c
+ * @note Copyright (C) 2018 Anders Selhammer <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include <net-snmp/net-snmp-config.h>
+#include <net-snmp/net-snmp-includes.h>
+#include <net-snmp/agent/net-snmp-agent-includes.h>
+
+#include "print.h"
+#include "ptpbase_mib.h"
+
+long longObject = 0;
+size_t long_size = sizeof(long);
+struct ptp_message* (*pmc_cb)(char *command);
+
+static void get_mgmt_data(struct ptp_message *msg,
+                  struct management_tlv **mgt,
+                  struct tlv_extra **extra)
+{
+       int action;
+       struct TLV *tlv;
+
+       if (msg_type(msg) != MANAGEMENT) {
+               return;
+       }
+
+       action = management_action(msg);
+       if (action < GET || action > ACKNOWLEDGE) {
+               return;
+       }
+
+       if (msg->tlv_count != 1) {
+               return;
+       }
+
+       tlv = (struct TLV *) msg->management.suffix;
+       if (tlv->type == TLV_MANAGEMENT) {
+               ;
+       } else if (tlv->type == TLV_MANAGEMENT_ERROR_STATUS) {
+               pr_err("MANAGEMENT_ERROR_STATUS");
+               return;
+       } else {
+               pr_err("unknown-tlv");
+               return;
+       }
+
+       *extra = TAILQ_FIRST(&msg->tlv_list);
+       *mgt = (struct management_tlv *) msg->management.suffix;
+       if ((*mgt)->length == 2 && (*mgt)->id != TLV_NULL_MANAGEMENT) {
+               pr_err("empty-tlv");
+               return;
+       }
+}
+
+static void get_msg_val(struct ptp_message *msg)
+{
+       struct management_tlv *mgt = NULL;
+       struct management_tlv_datum *mtd;
+       struct tlv_extra *extra = NULL;
+
+       get_mgmt_data(msg, &mgt, &extra);
+       if (!mgt) {
+               pr_err("error in pmc msg");
+               return;
+       }
+
+       switch (mgt->id) {
+       case TLV_VERSION_NUMBER:
+               mtd = (struct management_tlv_datum *) mgt->data;
+               pr_err("VERSION_NUMBER %hhu", mtd->val);
+               longObject = mtd->val;
+               break;
+       }
+}
+
+static int handle_tempMibObject(netsnmp_mib_handler *handler,
+                               netsnmp_handler_registration *reginfo,
+                               netsnmp_agent_request_info *reqinfo,
+                               netsnmp_request_info *requests)
+{
+       struct ptp_message *msg;
+
+       switch (reqinfo->mode) {
+       case MODE_GET:
+               msg = pmc_cb("GET VERSION_NUMBER");
+               if (msg) {
+                       get_msg_val(msg);
+                       msg_put(msg);
+               }
+               snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
+                                        &longObject, long_size);
+               break;
+       default:
+               snmp_log(LOG_ERR, "unknown mode (%d) in handle_totalClients\n",
+                        reqinfo->mode);
+               return SNMP_ERR_GENERR;
+       }
+       return SNMP_ERR_NOERROR;
+}
+
+static void init_tempMibObject(void)
+{
+       const oid tempMibObject_oid[] = { 1, 3, 6, 1, 2, 1, 241, 1, 2, 8, 1, 15 
};
+       netsnmp_register_scalar(netsnmp_create_handler_registration(
+                               "tempMibObject",
+                               handle_tempMibObject,
+                               tempMibObject_oid,
+                               OID_LENGTH(tempMibObject_oid),
+                               HANDLER_CAN_RONLY));
+}
+
+void init_ptpbaseMib(struct ptp_message* (*cb)(char *command))
+{
+       pmc_cb = cb;
+       init_tempMibObject();
+}
diff --git a/ptpbase_mib.h b/ptpbase_mib.h
new file mode 100644
index 0000000..66388ea
--- /dev/null
+++ b/ptpbase_mib.h
@@ -0,0 +1,30 @@
+/**
+ * @file ptpbase_mib.h
+ * @brief Implements PTPv2 Management Information Base (RFC 8173)
+ * @note Copyright (C) 2018 Anders Selhammer <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef PTPBASEMIB_H
+#define PTPBASEMIB_H
+
+#include "msg.h"
+
+/*
+ * function declarations 
+ */
+void init_ptpbaseMib(struct ptp_message* (*cb)(char *command));
+
+#endif /* PTPBASEMIB_H */
diff --git a/snmpd.c b/snmpd.c
index e0e9727..2cdba11 100644
--- a/snmpd.c
+++ b/snmpd.c
@@ -17,6 +17,9 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
+#include <errno.h>
+#include <poll.h>
+
 #include <net-snmp/net-snmp-config.h>
 #include <net-snmp/net-snmp-includes.h>
 #include <net-snmp/agent/net-snmp-agent-includes.h>
@@ -24,10 +27,44 @@
 #include "config.h"
 #include "pmc_common.h"
 #include "print.h"
+#include "ptpbase_mib.h"
 #include "util.h"
 
+#define SNMP_NFD 1
 static struct pmc *pmc;
 
+static struct ptp_message* run_pmc(char *command)
+{
+       struct pollfd pollfd[SNMP_NFD];
+       int cnt, tmo = 100;
+
+       pollfd[0].fd = pmc_get_transport_fd(pmc);
+       pollfd[0].events = POLLIN | POLLPRI;
+
+       if (pmc_do_command(pmc, command)) {
+               pr_err("bad command: %s", command);
+       }
+
+       while (is_running()) {
+               cnt = poll(pollfd, SNMP_NFD, tmo);
+               if (cnt < 0) {
+                       if (EINTR == errno) {
+                               continue;
+                       } else {
+                               pr_emerg("poll failed");
+                               break;
+                       }
+               } else if (!cnt) {
+                       break;
+               }
+
+               if (pollfd[0].revents & (POLLIN|POLLPRI)) {
+                       return pmc_recv(pmc);
+               }
+       }
+       return NULL;
+}
+
 static int open_pmc(struct config *cfg)
 {
        char uds_local[MAX_IFNAME_SIZE + 1];
@@ -45,6 +82,8 @@ static int open_snmp()
                               NETSNMP_DS_AGENT_ROLE, 1);
        init_agent("linuxptpAgent");
 
+       init_ptpbaseMib(&run_pmc);
+
        init_snmp("linuxptpAgent");
 
        return 0;
-- 
1.8.3.1


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus


------------------------------------------------------------------------------
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

Reply via email to