Configure E1 pcap file per line

In order to allow configuration of pcap files per e1_line
the vty command is now (for example line 0):

  e1_line 0 pcap /tmp/e1cap.pcap

in place of:

  pcap /tmp/e1cap.pcap

Also ensures that a configured pcap appears in 'show running-config'
and is written to the config file on issuing 'write'

This commit deprecates e1_set_pcap_fd()

Change-Id: I316c3d6a839e84c2f52a148c6b8dd6f5933cf4bf
diff --git a/src/e1_input_vty.c b/src/e1_input_vty.c
index d915c19..64f5f63 100644
--- a/src/e1_input_vty.c
+++ b/src/e1_input_vty.c
@@ -245,33 +245,67 @@
 	return CMD_SUCCESS;
 }
 
-DEFUN_ATTR(cfg_e1_pcap, cfg_e1_pcap_cmd,
-	   "pcap .FILE",
-	   "Setup a pcap recording of all E1 traffic\n"
+DEFUN_ATTR(cfg_e1line_pcap, cfg_e1line_pcap_cmd,
+	   "e1_line <0-255> pcap .FILE",
+	   E1_LINE_HELP "Setup a pcap recording of E1 traffic for line\n"
 	   "Filename to save the packets to\n", CMD_ATTR_IMMEDIATE)
 {
+	struct e1inp_line *line;
 	int fd;
+	int rc;
+	int e1_nr = atoi(argv[0]);
 
-	fd = open(argv[0], O_WRONLY | O_CREAT | O_TRUNC, 0660);
-	if (fd < 0) {
-		vty_out(vty, "Failed to setup E1 pcap recording to %s.%s", argv[0], VTY_NEWLINE);
+	line = e1inp_line_find(e1_nr);
+	if (!line) {
+		vty_out(vty, "%% Line %d doesn't exist%s", e1_nr, VTY_NEWLINE);
 		return CMD_WARNING;
 	}
 
-	e1_set_pcap_fd(fd);
+	fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0660);
+	if (fd < 0) {
+		vty_out(vty, "Failed to setup E1 pcap recording to %s%s", argv[1], VTY_NEWLINE);
+		return CMD_WARNING;
+	}
 
+	rc = e1_set_pcap_fd2(line, fd);
+	if (rc < 0) {
+		vty_out(vty, "Failed to write to E1 pcap file %s%s", argv[1], VTY_NEWLINE);
+		close(fd);
+		return CMD_WARNING;
+	}
+
+	osmo_talloc_replace_string(line, &line->pcap_file, argv[1]);
 	return CMD_SUCCESS;
 }
 
-DEFUN_ATTR(cfg_e1_no_pcap, cfg_e1_no_pcap_cmd,
-	   "no pcap",
-	   NO_STR "Disable pcap recording of all E1 traffic\n",
+DEFUN_ATTR(cfg_e1line_no_pcap, cfg_e1line_no_pcap_cmd,
+	   "no e1_line <0-255> pcap",
+	   NO_STR E1_LINE_HELP "Disable pcap recording of E1 traffic for line\n",
 	   CMD_ATTR_IMMEDIATE)
 {
-	e1_set_pcap_fd(-1);
+	struct e1inp_line *line;
+	int e1_nr = atoi(argv[0]);
+	line = e1inp_line_find(e1_nr);
+
+	e1_set_pcap_fd2(line, -1);
+	if (line->pcap_file) {
+		talloc_free(line->pcap_file);
+		line->pcap_file = NULL;
+	}
 	return CMD_SUCCESS;
 }
 
+DEFUN_DEPRECATED(cfg_e1_pcap_deprec, cfg_e1_pcap_deprec_cmd,
+	     "pcap .FILE", "Legacy")
+{
+	vty_out(vty, "%% 'pcap' is deprecated and has no effect: use e1_line <0-255> pcap%s",
+		VTY_NEWLINE);
+	return CMD_WARNING;
+}
+
+ALIAS_DEPRECATED(cfg_e1_pcap_deprec, cfg_e1_pcap_deprec_no_cmd,
+	     "no pcap", NO_STR);
+
 DEFUN_ATTR(cfg_e1inp, cfg_e1inp_cmd,
 	   "e1_input",
 	   "Configure E1/T1/J1 TDM input\n", CMD_ATTR_IMMEDIATE)
@@ -329,6 +363,9 @@
 			vty_out(vty, " e1_line %u ipa-keepalive %d %d%s", line->num,
 				line->ipa_kap->interval, line->ipa_kap->wait_for_resp,
 				VTY_NEWLINE);
+		if (line->pcap_file)
+			vty_out(vty, " e1_line %u pcap %s%s", line->num,
+				line->pcap_file, VTY_NEWLINE);
 	}
 
 	const char *ipa_bind = e1inp_ipa_get_bind_addr();
@@ -365,6 +402,8 @@
 	vty_out(vty, "E1 Line Number %u, Name %s, Driver %s%s",
 		line->num, line->name ? line->name : "",
 		line->driver->name, VTY_NEWLINE);
+	if (line->pcap_file)
+		vty_out(vty, "PCAP %s%s", line->pcap_file, VTY_NEWLINE);
 	if (line->driver->vty_show)
 		line->driver->vty_show(vty, line);
 	if (stats)
@@ -477,8 +516,10 @@
 	install_lib_element(CONFIG_NODE, &cfg_e1inp_cmd);
 	install_node(&e1inp_node, e1inp_config_write);
 
-	install_lib_element(L_E1INP_NODE, &cfg_e1_pcap_cmd);
-	install_lib_element(L_E1INP_NODE, &cfg_e1_no_pcap_cmd);
+	install_lib_element(L_E1INP_NODE, &cfg_e1line_pcap_cmd);
+	install_lib_element(L_E1INP_NODE, &cfg_e1line_no_pcap_cmd);
+	install_lib_element(L_E1INP_NODE, &cfg_e1_pcap_deprec_cmd);
+	install_lib_element(L_E1INP_NODE, &cfg_e1_pcap_deprec_no_cmd);
 
 	install_lib_element(L_E1INP_NODE, &cfg_e1_line_driver_cmd);
 	install_lib_element(L_E1INP_NODE, &cfg_e1_line_port_cmd);