[Asterisk-code-review] openr2(4/6): added new cli command -- mfcr2 show links (...asterisk[16])

Friendly Automation asteriskteam at digium.com
Tue Jul 23 16:45:12 CDT 2019


Friendly Automation has submitted this change and it was merged. ( https://gerrit.asterisk.org/c/asterisk/+/11606 )

Change subject: openr2(4/6): added new cli command -- mfcr2 show links
......................................................................

openr2(4/6): added new cli command -- mfcr2 show links

* This command show the MFC/R2 links

Change-Id: I213822e1b7ef9c05bd89a2ba62df8e0856ce9f84
Signed-off-by: Oron Peled <oron.peled at xorcom.com>
---
M channels/chan_dahdi.c
1 file changed, 101 insertions(+), 0 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Friendly Automation: Approved for Submit



diff --git a/channels/chan_dahdi.c b/channels/chan_dahdi.c
index 452cdc0..789d4f9 100644
--- a/channels/chan_dahdi.c
+++ b/channels/chan_dahdi.c
@@ -15028,10 +15028,111 @@
 	return CLI_SUCCESS;
 }
 
+static void mfcr2_show_links_of(struct ast_cli_args *a, struct r2links *list_head, const char *title)
+{
+#define FORMAT "%-5s %-10s %-15s %-10s %s\n"
+	AST_LIST_LOCK(list_head);
+	if (! AST_LIST_EMPTY(list_head)) {
+		int x = 0;
+		char index[5];
+		char live_chans_str[5];
+		char channel_list[R2_LINK_CAPACITY * 4];
+		struct r2link_entry *cur;
+		ast_cli(a->fd, "%s\n", title);
+		ast_cli(a->fd, FORMAT, "Index", "Thread", "Dahdi-Device", "Channels", "Channel-List");
+		AST_LIST_TRAVERSE(list_head, cur, list) {
+			struct dahdi_mfcr2 *mfcr2 = &cur->mfcr2;
+			const char *thread_status = NULL;
+			int i;
+			int len;
+			int inside_range;
+			int channo;
+			int prev_channo;
+			x++;
+			switch (mfcr2->r2master) {
+			case 0L: thread_status = "zero"; break;
+			case AST_PTHREADT_NULL: thread_status = "none"; break;
+			default: thread_status = "created"; break;
+			}
+			snprintf(index, sizeof(index), "%d", mfcr2->index);
+			snprintf(live_chans_str, sizeof(live_chans_str), "%d", mfcr2->live_chans);
+			channo = 0;
+			prev_channo = 0;
+			inside_range = 0;
+			len = 0;
+			/* Prepare nice string in channel_list[] */
+			for (i = 0; i < mfcr2->numchans; i++) {
+				struct dahdi_pvt *p = mfcr2->pvts[i];
+				if (!p) {
+					continue;
+				}
+				channo = p->channel;
+				/* Don't show a range until we know the last channel number */
+				if (prev_channo && prev_channo == channo - 1) {
+					prev_channo = channo;
+					inside_range = 1;
+					continue;
+				}
+				if (inside_range) {
+					/* Close range */
+					len += snprintf(channel_list + len, sizeof(channel_list) - len - 1, "-%d,%d", prev_channo, channo);
+					inside_range = 0;
+				} else if (prev_channo) {
+					/* Non-sequential channel numbers */
+					len += snprintf(channel_list + len, sizeof(channel_list) - len - 1, ",%d", channo);
+				} else {
+					/* First channel number */
+					len += snprintf(channel_list + len, sizeof(channel_list) - len - 1, "%d", channo);
+				}
+				prev_channo = channo;
+			}
+			/* Handle leftover channels */
+			if (inside_range) {
+				/* Close range */
+				len += snprintf(channel_list + len, sizeof(channel_list) - len - 1, "-%d", channo);
+				inside_range = 0;
+			} else if (prev_channo) {
+				/* Non-sequential channel numbers */
+				len += snprintf(channel_list + len, sizeof(channel_list) - len - 1, ",%d", channo);
+			}
+			// channel_list[len] = '\0';
+			ast_cli(a->fd, FORMAT,
+				index,
+				thread_status,
+				(mfcr2->nodev) ? "MISSING" : "OK",
+				live_chans_str,
+				channel_list);
+		}
+	}
+	AST_LIST_UNLOCK(list_head);
+#undef FORMAT
+}
+
+static char *handle_mfcr2_show_links(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "mfcr2 show links";
+		e->usage =
+			"Usage: mfcr2 show links\n"
+			"       Shows the DAHDI MFC/R2 links.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+	if (a->argc != 3) {
+		return CLI_SHOWUSAGE;
+	}
+	mfcr2_show_links_of(a, &r2links, "Live links\n");
+	mfcr2_show_links_of(a, &nodev_r2links, "Links to be removed (device missing)\n");
+	return CLI_SUCCESS;
+}
+
 static struct ast_cli_entry dahdi_mfcr2_cli[] = {
 	AST_CLI_DEFINE(handle_mfcr2_version, "Show OpenR2 library version"),
 	AST_CLI_DEFINE(handle_mfcr2_show_variants, "Show supported MFC/R2 variants"),
 	AST_CLI_DEFINE(handle_mfcr2_show_channels, "Show MFC/R2 channels"),
+	AST_CLI_DEFINE(handle_mfcr2_show_links, "Show MFC/R2 links"),
 	AST_CLI_DEFINE(handle_mfcr2_set_debug, "Set MFC/R2 channel logging level"),
 	AST_CLI_DEFINE(handle_mfcr2_call_files, "Enable/Disable MFC/R2 call files"),
 	AST_CLI_DEFINE(handle_mfcr2_set_idle, "Reset MFC/R2 channel forcing it to IDLE"),

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/11606
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: I213822e1b7ef9c05bd89a2ba62df8e0856ce9f84
Gerrit-Change-Number: 11606
Gerrit-PatchSet: 1
Gerrit-Owner: Oron Peled <oron.peled at xorcom.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Tzafrir Cohen <tzafrir.cohen at xorcom.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190723/7cb08c6e/attachment-0001.html>


More information about the asterisk-code-review mailing list