aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-sysmo/misc/sysmobts_mgr_2050.c
blob: 50e35dc15a035af7edea60670e697cbfcc9faa31 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* (C) 2014 by s.f.m.c. GmbH
 *
 * 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 "sysmobts_misc.h"
#include "sysmobts_par.h"
#include "sysmobts_mgr.h"
#include "btsconfig.h"

#include <osmocom/core/logging.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/timer.h>

#include <errno.h>
#include <unistd.h>

#ifdef BUILD_SBTS2050
#include <sysmocom/femtobts/sbts2050_header.h>

#define SERIAL_ALLOC_SIZE	300
#define SIZE_HEADER_RSP		5
#define SIZE_HEADER_CMD		4

struct uc {
	int id;
	int fd;
	const char *path;
};

struct ucinfo {
	uint16_t id;
	int master;
	int slave;
	int pa;
};

static struct uc ucontrol0 = {
	.id = 0,
	.path = "/dev/ttyS0"
};

/**********************************************************************
 *	Functions read/write from serial interface
 *********************************************************************/
static int hand_serial_read(int fd, struct msgb *msg, int numbytes)
{
	int rc, bread = 0;

	if (numbytes > msgb_tailroom(msg))
		return -ENOSPC;

	while (bread < numbytes) {
		rc = read(fd, msg->tail, numbytes - bread);
		if (rc < 0)
			return -1;
		if (rc == 0)
			break;

		bread += rc;
		msgb_put(msg, rc);
	}

	return bread;
}

static int hand_serial_write(int fd, struct msgb *msg)
{
	int rc, bwritten = 0;

	while (msg->len > 0) {
		rc = write(fd, msg->data, msg->len);
		if (rc <= 0)
			return -1;

		msgb_pull(msg, rc);
		bwritten += rc;
	}

	return bwritten;
}

/**********************************************************************
 *	Functions request information to Microcontroller
 *********************************************************************/
static void add_parity(cmdpkt_t *command)
{
	int n;
	uint8_t parity = 0x00;
	for (n = 0; n < SIZE_HEADER_CMD+command->u8Len; n++)
		parity ^= ((uint8_t *)command)[n];

	command->cmd.raw[command->u8Len] = parity;
}

static struct msgb *sbts2050_ucinfo_get(struct uc *ucontrol, const struct ucinfo *info)
{
	int num, rc;
	cmdpkt_t *command;
	rsppkt_t *response;
	struct msgb *msg;
	fd_set fdread;
	struct timeval tout = {
		.tv_sec = 10,
	};

	switch (info->id) {
	case SBTS2050_TEMP_RQT:
		num = sizeof(command->cmd.tempGet);
		break;
	case SBTS2050_PWR_RQT:
		num = sizeof(command->cmd.pwrSetState);
		break;
	case SBTS2050_PWR_STATUS:
		num = sizeof(command->cmd.pwrGetStatus);
		break;
	default:
		return NULL;
	}
	num = num + SIZE_HEADER_CMD+1;

	msg = msgb_alloc(SERIAL_ALLOC_SIZE, "Message Microcontroller");
	if (msg == NULL) {
		LOGP(DTEMP, LOGL_ERROR, "Error creating msg\n");
		return NULL;
	}
	command = (cmdpkt_t *) msgb_put(msg, num);

	command->u16Magic = 0xCAFE;
	switch (info->id) {
	case SBTS2050_TEMP_RQT:
		command->u8Id = info->id;
		command->u8Len = sizeof(command->cmd.tempGet);
		break;
	case SBTS2050_PWR_RQT:
		command->u8Id = info->id;
		command->u8Len = sizeof(command->cmd.pwrSetState);
		command->cmd.pwrSetState.u1MasterEn = !!info->master;
		command->cmd.pwrSetState.u1SlaveEn  = !!info->slave;
		command->cmd.pwrSetState.u1PwrAmpEn = !!info->pa;
		break;
	case SBTS2050_PWR_STATUS:
		command->u8Id     = info->id;
		command->u8Len    = sizeof(command->cmd.pwrGetStatus);
		break;
	default:
		goto err;
	}

	add_parity(command);

	if (hand_serial_write(ucontrol->fd, msg) < 0)
		goto err;

	msgb_reset(msg);

	FD_ZERO(&fdread);
	FD_SET(ucontrol->fd, &fdread);

	num = SIZE_HEADER_RSP;
	while (1) {
		rc = select(ucontrol->fd+1, &fdread, NULL, NULL, &tout);
		if (rc > 0) {
			if (hand_serial_read(ucontrol->fd, msg, num) < 0)
				goto err;

			response = (rsppkt_t *)msg->data;

			if (response->u8Id != info->id || msg->len <= 0 ||
			    response->i8Error != RQT_SUCCESS)
				goto err;

			if (msg->len == SIZE_HEADER_RSP + response->u8Len + 1)
				break;

			num = response->u8Len + 1;
		} else
			goto err;
	}

	return msg;

err:
	msgb_free(msg);
	return NULL;
}

/**********************************************************************
 *	Get power status function
 *********************************************************************/
int sbts2050_uc_get_status(enum sbts2050_status_rqt status)
{
	struct msgb *msg;
	const struct ucinfo info = {
		.id = SBTS2050_PWR_STATUS,
	};
	rsppkt_t *response;
	int val_status;

	msg = sbts2050_ucinfo_get(&ucontrol0, &info);

	if (msg == NULL) {
		LOGP(DTEMP, LOGL_ERROR,
			"Error requesting power status: %d\n", status);
		return -1;
	}

	response = (rsppkt_t *)msg->data;

	switch (status) {
	case SBTS2050_STATUS_MASTER:
		val_status = response->rsp.pwrGetStatus.u1MasterEn;
		break;
	case SBTS2050_STATUS_SLAVE:
		val_status = response->rsp.pwrGetStatus.u1SlaveEn;
		break;
	case SBTS2050_STATUS_PA:
		val_status = response->rsp.pwrGetStatus.u1PwrAmpEn;
		break;
	default:
		msgb_free(msg);
		return -1;
	}
	msgb_free(msg);
	return val_status;
}

/**********************************************************************
 *	Uc Power Switching handling
 *********************************************************************/
void sbts2050_uc_set_power(int pmaster, int pslave, int ppa)
{
	struct msgb *msg;
	const struct ucinfo info = {
		.id = SBTS2050_PWR_RQT,
		.master = pmaster,
		.slave = pslave,
		.pa = ppa
	};

	msg = sbts2050_ucinfo_get(&ucontrol0, &info);

	if (msg == NULL) {
		LOGP(DTEMP, LOGL_ERROR, "Error switching off some unit.\n");
		return;
	}

	LOGP(DTEMP, LOGL_DEBUG, "Switch off/on success:\n"
				"MASTER %s\n"
				"SLAVE %s\n"
				"PA %s\n",
				pmaster ? "ON" : "OFF",
				pslave ? "ON" : "OFF",
				ppa ? "ON" : "OFF");

	msgb_free(msg);
}

/**********************************************************************
 *	Uc temperature handling
 *********************************************************************/
void sbts2050_uc_check_temp(int *temp_pa, int *temp_board)
{
	rsppkt_t *response;
	struct msgb *msg;
	const struct ucinfo info = {
		.id = SBTS2050_TEMP_RQT,
	};

	msg = sbts2050_ucinfo_get(&ucontrol0, &info);

	if (msg == NULL) {
		LOGP(DTEMP, LOGL_ERROR, "Error reading temperature\n");
		return;
	}

	response = (rsppkt_t *)msg->data;

	*temp_board = response->rsp.tempGet.i8BrdTemp;
	*temp_pa = response->rsp.tempGet.i8PaTemp;

	LOGP(DTEMP, LOGL_DEBUG, "Temperature Board: %+3d C\n"
				"Tempeture PA: %+3d C\n",
				 response->rsp.tempGet.i8BrdTemp,
				 response->rsp.tempGet.i8PaTemp);
	msgb_free(msg);
}

static struct osmo_timer_list temp_uc_timer;
static void check_uctemp_timer_cb(void *data)
{
	int temp_pa = 0, temp_board = 0;

	sbts2050_uc_check_temp(&temp_pa, &temp_board);

	osmo_timer_schedule(&temp_uc_timer, TEMP_TIMER_SECS, 0);
}

void sbts2050_uc_initialize(void)
{
	int val;

	if (sysmobts_par_get_int(SYSMOBTS_PAR_MODEL_NR, &val) < 0) {
		LOGP(DFIND, LOGL_ERROR,
		     "Failed to get Model number\n");
		return;
	}

	if (val != 2050)
		return;

	if (sysmobts_par_get_int(SYSMOBTS_PAR_TRX_NR, &val) < 0) {
		LOGP(DFIND, LOGL_ERROR, "Failed to get the TRX number\n");
		return;
	}

	if (val != 0)
		return;

	ucontrol0.fd = osmo_serial_init(ucontrol0.path, 115200);
	if (ucontrol0.fd < 0) {
		LOGP(DFIND, LOGL_ERROR,
		     "Failed to open the serial interface\n");
		return;
	}

	temp_uc_timer.cb = check_uctemp_timer_cb;
	check_uctemp_timer_cb(NULL);
}
#else
void sbts2050_uc_initialize(void)
{
	LOGP(DTEMP, LOGL_NOTICE, "sysmoBTS2050 was not enabled at compile time.\n");
}
#endif