From 2980805f9ede566e55c23bcdd255902231b2aaf6 Mon Sep 17 00:00:00 2001
From: Dario Lombardo <lomato@libero.it>
Date: Wed, 2 Jan 2013 14:11:32 +0100
Subject: [PATCH] Adde custom frequecy range to cell_log.

---
 src/host/layer23/src/misc/app_cell_log.c |   57 ++++++++++++++++++++++++++++--
 src/host/layer23/src/misc/cell_log.c     |    7 ++--
 2 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/src/host/layer23/src/misc/app_cell_log.c b/src/host/layer23/src/misc/app_cell_log.c
index 27290be..97ed006 100644
--- a/src/host/layer23/src/misc/app_cell_log.c
+++ b/src/host/layer23/src/misc/app_cell_log.c
@@ -40,6 +40,9 @@
 extern struct log_target *stderr_target;
 extern void *l23_ctx;
 
+extern uint16_t basic_band_range[][2];
+extern uint16_t (*band_range)[][2];
+
 char *logname = "/var/log/osmocom.log";
 int RACH_MAX = 2;
 
@@ -99,13 +102,56 @@ static int l23_getopt_options(struct option **options)
 		{"gpsd-port", 1, 0, 'p'},
 #endif
 		{"gps", 1, 0, 'g'},
-		{"baud", 1, 0, 'b'}
+		{"baud", 1, 0, 'b'},
+		{"arfcns", 1, 0, 'A'}
 	};
 
 	*options = opts;
 	return ARRAY_SIZE(opts);
 }
 
+char* print_band_range(uint16_t range[][2], char* buf)
+{
+    int i = 0;
+    int idx = 0;
+    while(range[i][0] != 0 || range[i][1] != 0) {
+        sprintf(buf + idx, "%u-%u,", range[i][0], range[i][1]);
+        i++;
+        idx = strlen(buf);
+    }
+    buf[idx-1] = '\0';
+    return buf;
+}
+
+void parse_band_range(char* s)
+{
+    unsigned i = 0;
+    char* idx = strtok(s, ",");
+    unsigned single_range_size = sizeof(uint16_t) * 2;
+    unsigned start;
+    unsigned end;
+
+    band_range = (uint16_t(*)[][2])calloc(single_range_size, i + 1);
+    while (idx != NULL) {
+        start = 0;
+        end = 0;
+        sscanf(idx, "%u-%u", &start, &end);
+        if (end == 0)
+            end = start;
+        if (end < start) {
+            fprintf(stderr, "Starting frequency must me lower than ending.\n\n");
+            exit(1);
+        }
+        (*band_range)[i][0] = start;
+        (*band_range)[i][1] = end;
+        idx = strtok(NULL, ",");
+        i++;
+        band_range = realloc(band_range, (i+1) * single_range_size);
+    }
+    (*band_range)[i][0] = 0;
+    (*band_range)[i][1] = 0;
+}
+
 static int l23_cfg_print_help()
 {
 	printf("\nApplication specific\n");
@@ -116,12 +162,15 @@ static int l23_cfg_print_help()
 	printf("  -p --port PORT	2947. gpsd port\n");
 	printf("  -f --gps DEVICE	/dev/ttyACM0. GPS serial device.\n");
 	printf("  -b --baud BAUDRAT	The baud rate of the GPS device\n");
+	printf("  -A --arfcns ARFCNS    The list of arfcns to be monitored\n");
 
 	return 0;
 }
 
 static int l23_cfg_handle(int c, const char *optarg)
 {
+    char buf[1000];
+
 	switch (c) {
 	case 'l':
 		logname = talloc_strdup(l23_ctx, optarg);
@@ -172,6 +221,10 @@ static int l23_cfg_handle(int c, const char *optarg)
 		g.gps_type = GPS_TYPE_SERIAL;
 		LOGP(DGPS, LOGL_INFO, "Setting GPS baudrate to %u\n", g.baud);
 		break;
+	case 'A':
+	    parse_band_range((char*)optarg);
+	    printf("New frequencies range: %s\n", print_band_range(*band_range, buf));
+	    break;
 	}
 	return 0;
 
@@ -182,7 +235,7 @@ cmd_line_error:
 
 static struct l23_app_info info = {
 	.copyright	= "Copyright (C) 2010 Andreas Eversberg\n",
-	.getopt_string	= "g:p:l:r:nf:b:",
+	.getopt_string	= "g:p:l:r:nf:b:A:",
 	.cfg_supported	= l23_cfg_supported,
 	.cfg_getopt_opt = l23_getopt_options,
 	.cfg_handle_opt	= l23_cfg_handle,
diff --git a/src/host/layer23/src/misc/cell_log.c b/src/host/layer23/src/misc/cell_log.c
index a6059b4..e1318c4 100644
--- a/src/host/layer23/src/misc/cell_log.c
+++ b/src/host/layer23/src/misc/cell_log.c
@@ -58,7 +58,8 @@ enum {
 };
 
 /* ranges of bands */
-static uint16_t band_range[][2] = {{0, 124}, {512, 885}, {955, 1023}, {0, 0}};
+uint16_t basic_band_range[][2] = {{0, 124}, {512, 885}, {955, 1023}, {0, 0}};
+uint16_t (*band_range)[][2] = &basic_band_range;
 
 #define INFO_FLG_PM	1
 #define INFO_FLG_SYNC	2
@@ -351,8 +352,8 @@ static void start_pm(void)
 	uint16_t from, to;
 
 	state = SCAN_STATE_PM;
-	from = band_range[pm_index][0];
-	to = band_range[pm_index][1];
+	from = (*band_range)[pm_index][0];
+	to = (*band_range)[pm_index][1];
 
 	if (from == 0 && to == 0) {
 		LOGP(DSUM, LOGL_INFO, "Measurement done\n");
-- 
1.7.9.5

