Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2010 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | #include <snmp_mtp.h> |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 22 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 23 | |
| 24 | static void add_pdu_var(netsnmp_pdu *pdu, const char *mib_name, int id, const char *value) |
| 25 | { |
| 26 | oid oid_name[MAX_OID_LEN]; |
| 27 | size_t name_length; |
| 28 | |
| 29 | char buf[4096]; |
| 30 | buf[4095] = '\0'; |
| 31 | snprintf(buf, sizeof(buf)-1, "%s.%d", mib_name, id); |
| 32 | |
| 33 | name_length = MAX_OID_LEN; |
| 34 | if (snmp_parse_oid(buf, oid_name, &name_length) == NULL) { |
| 35 | snmp_perror(buf); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | if (snmp_add_var(pdu, oid_name, name_length, '=', value)) { |
| 40 | snmp_perror(buf); |
| 41 | return; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | static void send_pdu(netsnmp_session *ss, netsnmp_pdu *pdu) |
| 46 | { |
| 47 | int status; |
| 48 | netsnmp_pdu *response; |
| 49 | |
| 50 | status = snmp_synch_response(ss, pdu, &response); |
| 51 | if (status == STAT_ERROR) { |
| 52 | snmp_sess_perror("set failed", ss); |
| 53 | } else if (status == STAT_TIMEOUT) { |
| 54 | fprintf(stderr, "Timeout for SNMP.\n"); |
| 55 | } |
| 56 | |
| 57 | if (response) |
| 58 | snmp_free_pdu(response); |
| 59 | } |
| 60 | |
| 61 | void snmp_mtp_start_c7_datalink(struct snmp_mtp_session *session, int link_id) |
| 62 | { |
| 63 | netsnmp_pdu *pdu; |
| 64 | pdu = snmp_pdu_create(SNMP_MSG_SET); |
| 65 | |
| 66 | add_pdu_var(pdu, "PTI-NexusWareC7-MIB::nwc7DatalinkCommand", link_id, "nwc7DatalinkCmdPowerOn"); |
| 67 | add_pdu_var(pdu, "PTI-NexusWareC7-MIB::nwc7Mtp2Active", link_id, "true"); |
| 68 | send_pdu(session->ss, pdu); |
| 69 | } |
| 70 | |
| 71 | void snmp_mtp_stop_c7_datalink(struct snmp_mtp_session *session, int link_id) |
| 72 | { |
| 73 | netsnmp_pdu *pdu; |
| 74 | pdu = snmp_pdu_create(SNMP_MSG_SET); |
| 75 | |
| 76 | add_pdu_var(pdu, "PTI-NexusWareC7-MIB::nwc7Mtp2Active", link_id, "false"); |
| 77 | send_pdu(session->ss, pdu); |
| 78 | } |
| 79 | |
| 80 | struct snmp_mtp_session *snmp_mtp_session_create(char *host) |
| 81 | { |
| 82 | struct snmp_mtp_session *session = talloc_zero(NULL, struct snmp_mtp_session); |
| 83 | if (!session) |
| 84 | return NULL; |
| 85 | |
| 86 | init_snmp("cellmgr_ng"); |
| 87 | snmp_sess_init(&session->session); |
| 88 | session->session.peername = host; |
| 89 | session->session.version = SNMP_VERSION_1; |
| 90 | session->session.community = (unsigned char *) "private"; |
| 91 | session->session.community_len = strlen((const char *) session->session.community); |
| 92 | |
| 93 | session->ss = snmp_open(&session->session); |
| 94 | if (!session->ss) { |
| 95 | snmp_perror("create failure"); |
| 96 | snmp_log(LOG_ERR, "Could not connect to the remote.\n"); |
| 97 | talloc_free(session); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | return session; |
| 102 | } |
| 103 | |
| 104 | void snmp_mtp_deactivate(struct snmp_mtp_session *session) |
| 105 | { |
| 106 | snmp_mtp_stop_c7_datalink(session, 1); |
| 107 | } |
| 108 | |
| 109 | void snmp_mtp_activate(struct snmp_mtp_session *session) |
| 110 | { |
| 111 | snmp_mtp_start_c7_datalink(session, 1); |
| 112 | } |