ipc: add master socket number/path parameters

osmo-trx-ipc needs to know which master socket it should talk to, so
pass this path in the config device args like this:
dev-args ipc_msock=/path/to/socket

Additionally, add a master socket suffix number parameter to the ipc
backend,
ipc-driver-test -n 0 to start serving /tmp/ipc_sock0

Change-Id: I24857fbb74e5fc808b2a218957c723291de2ba04
diff --git a/Transceiver52M/device/ipc/IPCDevice.cpp b/Transceiver52M/device/ipc/IPCDevice.cpp
index eef79ee..8bddfb6 100644
--- a/Transceiver52M/device/ipc/IPCDevice.cpp
+++ b/Transceiver52M/device/ipc/IPCDevice.cpp
@@ -721,18 +721,26 @@
 
 int IPCDevice::open(const std::string &args, int ref, bool swap_channels)
 {
-	//float_type sr_host, sr_rf;
-	//unsigned int i, n;
-	//int rc, dev_id;
+	std::string k,v;
+	std::string::size_type keyend;
 	int rc;
 
-	LOGC(DDEV, INFO) << "Opening IPC device..";
+	if ((keyend = args.find('=')) != std::string::npos) {
+		k = args.substr(0, keyend++);
+		v = args.substr(keyend);
+	}
+	if(k != "ipc_msock" || !v.length()) {
+			LOGC(DDEV, ERROR) << "Invalid device args provided, expected  \"dev-args ipc_msock=/path/to/socket\"\n";
+			return -1;
+	}
+
+	LOGC(DDEV, INFO) << "Opening IPC device" << v << "..";
 
 	memset(&sk_state, 0x00, sizeof(sk_state));
 	INIT_LLIST_HEAD(&sk_state.upqueue);
-	rc = osmo_sock_unix_init_ofd(&sk_state.conn_bfd, SOCK_SEQPACKET, 0, IPC_SOCK_PATH, OSMO_SOCK_F_CONNECT);
+	rc = osmo_sock_unix_init_ofd(&sk_state.conn_bfd, SOCK_SEQPACKET, 0, v.c_str(), OSMO_SOCK_F_CONNECT);
 	if (rc < 0) {
-		LOGC(DDEV, ERROR) << "Failed to connect to the BTS (" << IPC_SOCK_PATH << "). "
+		LOGC(DDEV, ERROR) << "Failed to connect to the BTS (" << v << "). "
 				  << "Retrying...\n";
 		osmo_timer_setup(&sk_state.timer, ipc_sock_timeout, NULL);
 		osmo_timer_schedule(&sk_state.timer, 5, 0);
diff --git a/Transceiver52M/device/ipc/ipc-driver-test.c b/Transceiver52M/device/ipc/ipc-driver-test.c
index 9e02bf6..a7300bf 100644
--- a/Transceiver52M/device/ipc/ipc-driver-test.c
+++ b/Transceiver52M/device/ipc/ipc-driver-test.c
@@ -33,7 +33,7 @@
 #include <sys/mman.h>
 #include <sys/stat.h> /* For mode constants */
 #include <fcntl.h> /* For O_* constants */
-
+#include <getopt.h>
 
 #include <osmocom/core/application.h>
 #include <osmocom/core/talloc.h>
@@ -193,7 +193,7 @@
 
 	chan_info = ipc_prim->u.open_cnf.chan_info;
 	for (i = 0; i < num_chans; i++) {
-		snprintf(chan_info->chan_ipc_sk_path, sizeof(chan_info->chan_ipc_sk_path), "%s_%d", IPC_SOCK_PATH, i);
+		snprintf(chan_info->chan_ipc_sk_path, sizeof(chan_info->chan_ipc_sk_path), "%s_%d", IPC_SOCK_PATH_PREFIX, i);
 		/* FIXME: dynamc chan limit, currently 8 */
 		if (i < 8)
 			ipc_sock_init(chan_info->chan_ipc_sk_path, &global_ctrl_socks[i], ipc_chan_sock_accept, i);
@@ -272,7 +272,7 @@
 		bool underrun;
 		uhdwrap_write(global_dev, chann, &underrun);
 	}
-    return 0;
+	return 0;
 }
 
 int ipc_rx_chan_start_req(struct ipc_sk_chan_if_op_void *req, uint8_t chan_nr)
@@ -392,6 +392,51 @@
 	return 0;
 }
 
+static void print_help(void)
+{
+	printf(	"ipc-driver-test Usage:\n"
+		" -h  --help		This message\n"
+		" -n  --sock-num NR	Master socket suffix number NR\n"
+	      );
+}
+
+static int msocknum = 0;
+
+static void handle_options(int argc, char **argv)
+{
+	while (1) {
+		int option_index = 0, c;
+		const struct option long_options[] = {
+			{ "help", 0, 0, 'h' },
+			{ "sock-num", 1, 0, 'n' },
+			{0,0,0,0}
+		};
+
+		c = getopt_long(argc, argv, "hn:",
+				long_options, &option_index);
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'h':
+			print_help();
+			exit(0);
+			break;
+		case 'n':
+			msocknum = atoi(optarg);
+			break;
+		default:
+			exit(2);
+			break;
+		}
+	}
+
+	if (argc > optind) {
+		fprintf(stderr, "Unsupported positional arguments on command line\n");
+		exit(2);
+	}
+}
+
 #if defined(IPCMAGIC) && defined(__cplusplus)
 extern "C" int osmo_ctx_init(const char *id);
 
@@ -404,13 +449,17 @@
 int main(int argc, char **argv)
 {
 #endif
+	char ipc_msock_path[sizeof(IPC_SOCK_PATH_PREFIX)+3];
 	tall_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
 	msgb_talloc_ctx_init(tall_ctx, 0);
 	osmo_init_logging2(tall_ctx, &log_infox);
 	log_enable_multithread();
 
+	handle_options(argc, argv);
+	snprintf(ipc_msock_path,sizeof(ipc_msock_path), "%s%d", IPC_SOCK_PATH_PREFIX, msocknum);
+
 	LOGP(DMAIN, LOGL_INFO, "Starting %s\n", argv[0]);
-	ipc_sock_init(IPC_SOCK_PATH, &global_ipc_sock_state, ipc_sock_accept, 0);
+	ipc_sock_init(ipc_msock_path, &global_ipc_sock_state, ipc_sock_accept, 0);
 	while (!ipc_exit_requested)
 		osmo_select_main(0);
 	//ipc_sock_close()
diff --git a/Transceiver52M/device/ipc/shm.h b/Transceiver52M/device/ipc/shm.h
index 5ff8f2d..0466349 100644
--- a/Transceiver52M/device/ipc/shm.h
+++ b/Transceiver52M/device/ipc/shm.h
@@ -78,7 +78,7 @@
 // Master socket
 //////////////////
 
-#define IPC_SOCK_PATH "/tmp/ipc_sock"
+#define IPC_SOCK_PATH_PREFIX "/tmp/ipc_sock"
 #define IPC_SOCK_API_VERSION 1
 
 /* msg_type */