Debian Codegraph @

src/osmo-bts-oc2g/misc/oc2gbts_bid.c

raw
1 /* Copyright (C) 2015 by Yves Godin <[email protected]>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <inttypes.h>
28
29 #include "oc2gbts_bid.h"
30
31 #define BOARD_REV_MAJ_SYSFS "/var/oc2g/platform/rev/major"
32 #define BOARD_REV_MIN_SYSFS "/var/oc2g/platform/rev/minor"
33 #define BOARD_OPT_SYSFS "/var/oc2g/platform/option"
34
35 static const int option_type_mask[_NUM_OPTION_TYPES] = {
36 [OC2GBTS_OPTION_OCXO] = 0x07,
37 [OC2GBTS_OPTION_FPGA] = 0x03,
38 [OC2GBTS_OPTION_PA] = 0x01,
39 [OC2GBTS_OPTION_BAND] = 0x03,
40 [OC2GBTS_OPTION_TX_ATT] = 0x01,
41 [OC2GBTS_OPTION_TX_FIL] = 0x01,
42 [OC2GBTS_OPTION_RX_ATT] = 0x01,
43 [OC2GBTS_OPTION_RMS_FWD] = 0x01,
44 [OC2GBTS_OPTION_RMS_REFL] = 0x01,
45 [OC2GBTS_OPTION_DDR_32B] = 0x01,
46 [OC2GBTS_OPTION_DDR_ECC] = 0x01,
47 [OC2GBTS_OPTION_PA_TEMP] = 0x01,
48 };
49
50 static const int option_type_shift[_NUM_OPTION_TYPES] = {
51 [OC2GBTS_OPTION_OCXO] = 0,
52 [OC2GBTS_OPTION_FPGA] = 3,
53 [OC2GBTS_OPTION_PA] = 5,
54 [OC2GBTS_OPTION_BAND] = 6,
55 [OC2GBTS_OPTION_TX_ATT] = 8,
56 [OC2GBTS_OPTION_TX_FIL] = 9,
57 [OC2GBTS_OPTION_RX_ATT] = 10,
58 [OC2GBTS_OPTION_RMS_FWD] = 11,
59 [OC2GBTS_OPTION_RMS_REFL] = 12,
60 [OC2GBTS_OPTION_DDR_32B] = 13,
61 [OC2GBTS_OPTION_DDR_ECC] = 14,
62 [OC2GBTS_OPTION_PA_TEMP] = 15,
63 };
64
65
66 static char board_rev_maj = -1;
67 static char board_rev_min = -1;
68 static int board_option = -1;
69
70 void oc2gbts_rev_get(char *rev_maj, char *rev_min)
71 {
72 FILE *fp;
73 char rev;
74
75 *rev_maj = 0;
76 *rev_min = 0;
77
78 if (board_rev_maj != -1 && board_rev_min != -1) {
79 *rev_maj = board_rev_maj;
80 *rev_min = board_rev_min;
81 }
82
83 fp = fopen(BOARD_REV_MAJ_SYSFS, "r");
84 if (fp == NULL) return;
85
86 if (fscanf(fp, "%c", &rev) != 1) {
87 fclose(fp);
88 return;
89 }
90
91 fclose(fp);
92
93 *rev_maj = board_rev_maj = rev;
94
95 fp = fopen(BOARD_REV_MIN_SYSFS, "r");
96 if (fp == NULL) return;
97
98 if (fscanf(fp, "%c", &rev) != 1) {
99 fclose(fp);
100 return;
101 }
102
103 fclose(fp);
104
105 *rev_min = board_rev_min = rev;
106 }
107
108 const char* get_hwversion_desc()
109 {
110 int model;
111 size_t len;
112 static char model_name[64] = {0, };
113 len = snprintf(model_name, sizeof(model_name), "NuRAN OC-2G BTS");
114
115 char rev_maj = 0, rev_min = 0;
116
117 oc2gbts_rev_get(&rev_maj, &rev_min);
118 len += snprintf(model_name + len, sizeof(model_name) - len,
119 " Rev %" PRIu8 ".%" PRIu8, (uint8_t)rev_maj, (uint8_t)rev_min);
120
121 model = oc2gbts_model_get();
122 if (model >= 0) {
123 snprintf(model_name + len, sizeof(model_name) - len,
124 "%s (%05X)", model_name, model);
125 }
126 return model_name;
127 }
128
129 int oc2gbts_model_get(void)
130 {
131 FILE *fp;
132 int opt;
133
134
135 if (board_option == -1) {
136 fp = fopen(BOARD_OPT_SYSFS, "r");
137 if (fp == NULL) {
138 return -1;
139 }
140
141 if (fscanf(fp, "%X", &opt) != 1) {
142 fclose( fp );
143 return -1;
144 }
145 fclose(fp);
146
147 board_option = opt;
148 }
149 return board_option;
150 }
151
152 int oc2gbts_option_get(enum oc2gbts_option_type type)
153 {
154 int rc;
155 int option;
156
157 if (type >= _NUM_OPTION_TYPES) {
158 return -EINVAL;
159 }
160
161 if (board_option == -1) {
162 rc = oc2gbts_model_get();
163 if (rc < 0) return rc;
164 }
165
166 option = (board_option >> option_type_shift[type])
167 & option_type_mask[type];
168
169 return option;
170 }