aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-litecell15/misc/lc15bts_misc.c
blob: 5ff8e312e87fa39ceeb0edbafee3ace75b78187d (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* Copyright (C) 2015 by Yves Godin <support@nuranwireless.com>
 * 
 * Based on sysmoBTS:
 *     sysmobts_misc.c
 *     (C) 2012 by Harald Welte <laforge@gnumonks.org>
 *
 * 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 <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <limits.h>
#include <time.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <osmocom/core/talloc.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/application.h>
#include <osmocom/vty/telnet_interface.h>
#include <osmocom/vty/logging.h>

#include "btsconfig.h"
#include "lc15bts_misc.h"
#include "lc15bts_par.h"
#include "lc15bts_mgr.h"
#include "lc15bts_temp.h"

/*********************************************************************
 * Temperature handling
 *********************************************************************/

static const struct {
	const char *name;
	int has_max;
	enum lc15bts_temp_sensor sensor;
	enum lc15bts_par ee_par;
} temp_data[] = {
	{
		.name = "supply",
		.has_max = 1,
		.sensor = LC15BTS_TEMP_SUPPLY,
		.ee_par = LC15BTS_PAR_TEMP_SUPPLY_MAX,
	}, {
		.name = "soc",
		.has_max = 0,
		.sensor = LC15BTS_TEMP_SOC,
		.ee_par = LC15BTS_PAR_TEMP_SOC_MAX,
	}, {
		.name = "fpga",
		.has_max = 0,
		.sensor = LC15BTS_TEMP_FPGA,
		.ee_par = LC15BTS_PAR_TEMP_FPGA_MAX,

	}, {
		.name = "logrf",
		.has_max = 1,
		.sensor = LC15BTS_TEMP_LOGRF,
		.ee_par = LC15BTS_PAR_TEMP_LOGRF_MAX,
	}, {
		.name = "ocxo",
		.has_max = 1,
		.sensor = LC15BTS_TEMP_OCXO,
		.ee_par = LC15BTS_PAR_TEMP_OCXO_MAX,
	}, {
		.name = "tx0",
		.has_max = 0,
		.sensor = LC15BTS_TEMP_TX0,
		.ee_par = LC15BTS_PAR_TEMP_TX0_MAX,
	}, {
		.name = "tx1",
		.has_max = 0,
		.sensor = LC15BTS_TEMP_TX1,
		.ee_par = LC15BTS_PAR_TEMP_TX1_MAX,
	}, {
		.name = "pa0",
		.has_max = 1,
		.sensor = LC15BTS_TEMP_PA0,
		.ee_par = LC15BTS_PAR_TEMP_PA0_MAX,
	}, {
		.name = "pa1",
		.has_max = 1,
		.sensor = LC15BTS_TEMP_PA1,
		.ee_par = LC15BTS_PAR_TEMP_PA1_MAX,
	}
};

void lc15bts_check_temp(int no_rom_write)
{
	int temp_old[ARRAY_SIZE(temp_data)];
	int temp_cur[ARRAY_SIZE(temp_data)];
	int i, rc;

	for (i = 0; i < ARRAY_SIZE(temp_data); i++) {
		int ret;
		rc = lc15bts_par_get_int(temp_data[i].ee_par, &ret);
		temp_old[i] = ret * 1000;

		temp_cur[i] = lc15bts_temp_get(temp_data[i].sensor);
		if (temp_cur[i] < 0 && temp_cur[i] > -1000) {
			LOGP(DTEMP, LOGL_ERROR, "Error reading temperature (%d)\n", temp_data[i].sensor);
			continue;
		}
	
		LOGP(DTEMP, LOGL_DEBUG, "Current %s temperature: %d.%d C\n",
		     temp_data[i].name, temp_cur[i]/1000, temp_cur[i]%1000);

		if (temp_cur[i] > temp_old[i]) {
			LOGP(DTEMP, LOGL_NOTICE, "New maximum %s "
			     "temperature: %d.%d C\n", temp_data[i].name,
			     temp_cur[i]/1000, temp_old[i]%1000);

			if (!no_rom_write) {
				rc = lc15bts_par_set_int(temp_data[i].ee_par,
						  temp_cur[i]/1000);
				if (rc < 0)
					LOGP(DTEMP, LOGL_ERROR, "error writing new %s "
					     "max temp %d (%s)\n", temp_data[i].name,
					     rc, strerror(errno));
			}
		}
	}
}

/*********************************************************************
 * Hours handling
 *********************************************************************/
static time_t last_update;

int lc15bts_update_hours(int no_rom_write)
{
	time_t now = time(NULL);
	int rc, op_hrs;

	/* first time after start of manager program */
	if (last_update == 0) {
		last_update = now;

		rc = lc15bts_par_get_int(LC15BTS_PAR_HOURS, &op_hrs);
		if (rc < 0) {
			LOGP(DTEMP, LOGL_ERROR, "Unable to read "
			     "operational hours: %d (%s)\n", rc,
			     strerror(errno));
			return rc;
		}

		LOGP(DTEMP, LOGL_INFO, "Total hours of Operation: %u\n",
		     op_hrs);

		return 0;
	}

	if (now >= last_update + 3600) {
		rc = lc15bts_par_get_int(LC15BTS_PAR_HOURS, &op_hrs);
		if (rc < 0) {
			LOGP(DTEMP, LOGL_ERROR, "Unable to read "
			     "operational hours: %d (%s)\n", rc,
			     strerror(errno));
			return rc;
		}

		/* number of hours to increase */
		op_hrs += (now-last_update)/3600;

		LOGP(DTEMP, LOGL_INFO, "Total hours of Operation: %u\n",
		     op_hrs);

		if (!no_rom_write) {
			rc = lc15bts_par_set_int(LC15BTS_PAR_HOURS, op_hrs);
			if (rc < 0)
				return rc;
		}

		last_update = now;
	}

	return 0;
}

/*********************************************************************
 * Firmware reloading
 *********************************************************************/

static const char *fw_sysfs[_NUM_FW] = {
	[LC15BTS_FW_DSP0]	= "/sys/kernel/debug/remoteproc/remoteproc0/recovery",
	[LC15BTS_FW_DSP1]	= "/sys/kernel/debug/remoteproc/remoteproc0/recovery",
};

int lc15bts_firmware_reload(enum lc15bts_firmware_type type)
{
	int fd;
	int rc;

	switch (type) {
	case LC15BTS_FW_DSP0:
	case LC15BTS_FW_DSP1: 
		fd = open(fw_sysfs[type], O_WRONLY);
	        if (fd < 0) {
	                LOGP(DFW, LOGL_ERROR, "unable ot open firmware device %s: %s\n",
                    	fw_sysfs[type], strerror(errno));
	                close(fd);
        	        return fd;
		}
		rc = write(fd, "restart", 8); 
		if (rc < 8) {
                        LOGP(DFW, LOGL_ERROR, "short write during "
                             "fw write to %s\n", fw_sysfs[type]);
                        close(fd);
                        return -EIO;
                }
		close(fd);
	default: 
		return -EINVAL;
	}
	return 0;
}