aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-oc2g/misc/oc2gbts_bid.c
blob: c2cd483d60eb6c1f2efd5421ec2629e3c1080e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (C) 2015 by Yves Godin <support@nuranwireless.com>
 * 
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>

#include "oc2gbts_bid.h"

#define BOARD_REV_MAJ_SYSFS	"/var/oc2g/platform/rev/major"
#define BOARD_REV_MIN_SYSFS	"/var/oc2g/platform/rev/minor"
#define BOARD_OPT_SYSFS		"/var/oc2g/platform/option"
 
static const int option_type_mask[_NUM_OPTION_TYPES] = {
	[OC2GBTS_OPTION_OCXO]		= 0x07,
	[OC2GBTS_OPTION_FPGA]		= 0x03,
	[OC2GBTS_OPTION_PA]			= 0x01,
	[OC2GBTS_OPTION_BAND]		= 0x03,
	[OC2GBTS_OPTION_TX_ATT]		= 0x01,
	[OC2GBTS_OPTION_TX_FIL]		= 0x01,
	[OC2GBTS_OPTION_RX_ATT]		= 0x01,
	[OC2GBTS_OPTION_RMS_FWD]	= 0x01,
	[OC2GBTS_OPTION_RMS_REFL]	= 0x01,
	[OC2GBTS_OPTION_DDR_32B]	= 0x01,
	[OC2GBTS_OPTION_DDR_ECC]	= 0x01,
	[OC2GBTS_OPTION_PA_TEMP]	= 0x01,
};

static const int option_type_shift[_NUM_OPTION_TYPES] = {
	[OC2GBTS_OPTION_OCXO]		= 0,
	[OC2GBTS_OPTION_FPGA]		= 3,
	[OC2GBTS_OPTION_PA]			= 5,
	[OC2GBTS_OPTION_BAND]		= 6,
	[OC2GBTS_OPTION_TX_ATT]		= 8,
	[OC2GBTS_OPTION_TX_FIL]		= 9,
	[OC2GBTS_OPTION_RX_ATT]		= 10,
	[OC2GBTS_OPTION_RMS_FWD]	= 11,
	[OC2GBTS_OPTION_RMS_REFL]	= 12,
	[OC2GBTS_OPTION_DDR_32B]	= 13,
	[OC2GBTS_OPTION_DDR_ECC]	= 14,
	[OC2GBTS_OPTION_PA_TEMP]	= 15,
};


static char board_rev_maj = -1;
static char board_rev_min = -1;
static int board_option = -1;

void oc2gbts_rev_get(char *rev_maj, char *rev_min)
{
	FILE *fp;
	char rev;

	*rev_maj = 0;
	*rev_min = 0;

	if (board_rev_maj != -1 && board_rev_min != -1) {
		*rev_maj = board_rev_maj;
		*rev_min = board_rev_min;
	}

	fp = fopen(BOARD_REV_MAJ_SYSFS, "r");
	if (fp == NULL) return;

	if (fscanf(fp, "%c", &rev) != 1) {
		fclose(fp);
		return;
	}

	fclose(fp);

	*rev_maj = board_rev_maj = rev;

	fp = fopen(BOARD_REV_MIN_SYSFS, "r");
	if (fp == NULL) return;

	if (fscanf(fp, "%c", &rev) != 1) {
		fclose(fp);
		return;
	}

	fclose(fp);

	*rev_min = board_rev_min = rev;
}

const char* get_hwversion_desc()
{
        int model;
        size_t len;
        static char model_name[64] = {0, };
        len = snprintf(model_name, sizeof(model_name), "NuRAN OC-2G BTS");

        char rev_maj = 0, rev_min = 0;

        oc2gbts_rev_get(&rev_maj, &rev_min);
        len += snprintf(model_name + len, sizeof(model_name) - len,
                        " Rev %" PRIu8 ".%" PRIu8, (uint8_t)rev_maj, (uint8_t)rev_min);

        model = oc2gbts_model_get();
        if (model >= 0) {
                snprintf(model_name + len, sizeof(model_name) - len,
                         "%s (%05X)", model_name, model);
        }
        return model_name;
}

int oc2gbts_model_get(void)
{
	FILE *fp;
	int opt;


	if (board_option == -1) {
		fp = fopen(BOARD_OPT_SYSFS, "r");
		if (fp == NULL) {
			return -1;
		}

		if (fscanf(fp, "%X", &opt) != 1) {
			fclose( fp );
			return -1;
		}
		fclose(fp);
	
		board_option = opt;
	}
	return board_option;
}

int oc2gbts_option_get(enum oc2gbts_option_type type)
{
	int rc;
	int option;

	if (type >= _NUM_OPTION_TYPES) {
		return -EINVAL;
	}

	if (board_option == -1) {
		rc = oc2gbts_model_get();
		if (rc < 0) return rc;
	}

	option = (board_option >> option_type_shift[type])
		& option_type_mask[type];

	return option;	
}