blob: e43691a32ffead5aff8439a265a659e16ad5b2c8 [file] [log] [blame]
Holger Hans Peter Freyther796406b2011-02-28 17:21:49 +01001/*
2 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2010-2011 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 Affero General Public License as published by
8 * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <net-snmp/net-snmp-config.h>
22#include <net-snmp/utilities.h>
23#include <net-snmp/net-snmp-includes.h>
24
25#include <cellmgr_debug.h>
26
27#define HSCOMM "PTI-NexusWare-HSCMCONN-MIB::"
28
29#define PTMC_STREAM_A_RX0 0
30#define PTMC_STREAM_A_TX0 128
31#define PTMC_STREAM_A_RX1 1024
32#define PTMC_STREAM_A_TX1 1152
33
34
35static netsnmp_session g_session, *g_ss;
36
37static void add_pdu_var(netsnmp_pdu *pdu, const char *mib_name,
38 int id1, int id2, const char *value)
39{
40 oid oid_name[MAX_OID_LEN];
41 size_t name_length;
42
43 char buf[4096];
44 buf[4095] = '\0';
45 snprintf(buf, sizeof(buf)-1, "%s.%d.%d", mib_name, id1, id2);
46
47 name_length = MAX_OID_LEN;
48 if (snmp_parse_oid(buf, oid_name, &name_length) == NULL) {
49 snmp_perror(buf);
50 return;
51 }
52
Holger Hans Peter Freythere5929332011-03-01 18:00:52 +010053 if (snmp_add_var(pdu, oid_name, name_length, 'i', value)) {
Holger Hans Peter Freyther796406b2011-02-28 17:21:49 +010054 snmp_perror(buf);
55 return;
56 }
57}
58
59static int rx_port_get(int port)
60{
61 if (port > 60)
62 return PTMC_STREAM_A_RX1 + port;
63 else
64 return PTMC_STREAM_A_RX0 + port;
65}
66
67static int tx_port_get(int port)
68{
69 if (port > 60)
70 return PTMC_STREAM_A_TX1 + port;
71 else
72 return PTMC_STREAM_A_TX0 + port;
73}
74
75int mgcp_snmp_init()
76{
77 init_snmp("mgcp_mgw");
78 snmp_sess_init(&g_session);
79 g_session.version = SNMP_VERSION_1;
80 g_session.community = (unsigned char *) "private";
81 g_session.community_len = strlen((const char *) g_session.community);
82
83 g_session.peername = "127.0.0.1";
84 g_ss = snmp_open(&g_session);
85 if (!g_ss) {
86 snmp_perror("create failure");
87 snmp_log(LOG_ERR, "Could not connect to the remote.\n");
88 LOGP(DINP, LOGL_ERROR, "Failed to open a SNMP session.\n");
89 return -1;
90 }
91
92 return 0;
93}
94
95int mgcp_snmp_connect(int port, int trunk, int timeslot)
96{
97 int status;
Holger Hans Peter Freytherb7299a82011-03-01 15:48:05 +010098 netsnmp_pdu *response = NULL;
Holger Hans Peter Freyther796406b2011-02-28 17:21:49 +010099 netsnmp_pdu *pdu;
100 int _rx_port, _tx_port;
101 char tx_port[10];
102 char trunk_name[13], tslot_name[13];
103
104 if (!g_ss)
105 return -1;
106
107 /* have the trunk/timeslot as value */
108 snprintf(trunk_name, sizeof(trunk_name), "%d", trunk);
109 snprintf(tslot_name, sizeof(tslot_name), "%d", timeslot);
110
111 /* rx port, tx side for the port */
112 _rx_port = rx_port_get(port);
113 _tx_port = tx_port_get(port);
114 snprintf(tx_port, sizeof(tx_port), "%d", _tx_port);
115
116 pdu = snmp_pdu_create(SNMP_MSG_SET);
117 if (!pdu) {
118 LOGP(DINP, LOGL_ERROR, "Failed to allocate PDU.\n");
119 return -1;
120 }
121
122 /* This connects the TX side to the given trunk/timeslot */
123 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourceType.hscmconnStreamTrunk",
124 trunk, timeslot, "hscmconnStreamPtmc");
Holger Hans Peter Freytherd5231312011-03-02 13:34:43 +0100125 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourceTypeInstance.hscmconnStreamTrunk",
Holger Hans Peter Freyther796406b2011-02-28 17:21:49 +0100126 trunk, timeslot, "1");
127 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourceTimeslot.hscmconnStreamTrunk",
128 trunk, timeslot, tx_port);
129 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourcePattern.hscmconnStreamTrunk",
130 trunk, timeslot, "0");
131 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourceTimeslotCount.hscmconnStreamTrunk",
132 trunk, timeslot, "1");
133 add_pdu_var(pdu, HSCOMM "hscmconnConnectBidirectional.hscmconnStreamTrunk",
134 trunk, timeslot, "false");
135
136 /* This connect the RX side to the given trunk/timeslot */
137 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourceType.hscmconnStreamPtmc",
138 1, _rx_port, "hscmconnStreamTrunk");
139 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourceTypeInstance.hscmconnStreamPtmc",
140 1, _rx_port, trunk_name);
141 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourceTimeslot.hscmconnStreamPtmc",
142 1, _rx_port, tslot_name);
143 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourcePattern.hscmconnStreamPtmc",
144 1, _rx_port, "0");
145 add_pdu_var(pdu, HSCOMM "hscmconnNewDataSourceTimeslotCount.hscmconnStreamPtmc",
146 1, _rx_port, "1");
147 add_pdu_var(pdu, HSCOMM "hscmconnConnectBidirectional.hscmconnStreamPtmc",
148 1, _rx_port, "false");
149
150
151 status = snmp_synch_response(g_ss, pdu, &response);
152 if (status == STAT_ERROR) {
153 snmp_sess_perror("set failed", g_ss);
Holger Hans Peter Freytherb7299a82011-03-01 15:48:05 +0100154 goto failure;
Holger Hans Peter Freyther796406b2011-02-28 17:21:49 +0100155 } else if (status == STAT_TIMEOUT) {
156 fprintf(stderr, "Timeout for SNMP.\n");
Holger Hans Peter Freytherb7299a82011-03-01 15:48:05 +0100157 goto failure;
Holger Hans Peter Freyther796406b2011-02-28 17:21:49 +0100158 }
159
160 if (response)
161 snmp_free_pdu(response);
162 return 0;
Holger Hans Peter Freytherb7299a82011-03-01 15:48:05 +0100163
164failure:
165 if (response)
166 snmp_free_pdu(response);
167 return -1;
Holger Hans Peter Freyther796406b2011-02-28 17:21:49 +0100168}