aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-sysmo/sysmobts_ctrl.c
blob: c6dac58cdd7983ca5049c0b02e6e83a190a6d2b8 (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
/* Control Interface for sysmoBTS */

/* (C) 2014 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 <errno.h>
#include <fcntl.h>

#include <osmocom/ctrl/control_cmd.h>

#include <osmo-bts/logging.h>
#include <osmo-bts/bts.h>
#include <osmo-bts/oml.h>
#include <osmo-bts/rsl.h>
#include <osmo-bts/gsm_data.h>
#include <osmo-bts/paging.h>
#include <osmo-bts/measurement.h>
#include <osmo-bts/pcu_if.h>
#include <osmo-bts/handover.h>

#include <sysmocom/femtobts/superfemto.h>
#include <sysmocom/femtobts/gsml1prim.h>
#include <sysmocom/femtobts/gsml1const.h>
#include <sysmocom/femtobts/gsml1types.h>

#include "femtobts.h"
#include "l1_if.h"


/* for control interface */

#ifndef HW_SYSMOBTS_V1
CTRL_CMD_DEFINE(clock_info, "clock-info");
static int ctrl_clkinfo_cb(struct gsm_bts_trx *trx, struct msgb *resp,
			   void *data)
{
	SuperFemto_Prim_t *sysp = msgb_sysprim(resp);
	struct ctrl_cmd_def *cd = data;
	struct ctrl_cmd *cmd = cd->cmd;

	LOGP(DL1C, LOGL_NOTICE,
		"RfClockInfo iClkCor=%d/clkSrc=%s Err=%d/ErrRes=%d/clkSrc=%s\n",
		sysp->u.rfClockInfoCnf.rfTrx.iClkCor,
		get_value_string(femtobts_clksrc_names,
				sysp->u.rfClockInfoCnf.rfTrx.clkSrc),
		sysp->u.rfClockInfoCnf.rfTrxClkCal.iClkErr,
		sysp->u.rfClockInfoCnf.rfTrxClkCal.iClkErrRes,
		get_value_string(femtobts_clksrc_names,
				sysp->u.rfClockInfoCnf.rfTrxClkCal.clkSrc));

	if (ctrl_cmd_def_is_zombie(cd)) {
		msgb_free(resp);
		return 0;
	}

	cmd->reply = talloc_asprintf(cmd, "%d,%s,%d,%d,%s",
		sysp->u.rfClockInfoCnf.rfTrx.iClkCor,
		get_value_string(femtobts_clksrc_names,
				sysp->u.rfClockInfoCnf.rfTrx.clkSrc),
		sysp->u.rfClockInfoCnf.rfTrxClkCal.iClkErr,
		sysp->u.rfClockInfoCnf.rfTrxClkCal.iClkErrRes,
		get_value_string(femtobts_clksrc_names,
				sysp->u.rfClockInfoCnf.rfTrxClkCal.clkSrc));

	ctrl_cmd_def_send(cd);

	msgb_free(resp);

	return 0;
}
static int get_clock_info(struct ctrl_cmd *cmd, void *data)
{
	struct gsm_bts_trx *trx = cmd->node;
	struct femtol1_hdl *fl1h = trx_femtol1_hdl(trx);
	struct msgb *msg = sysp_msgb_alloc();
	SuperFemto_Prim_t *sysp = msgb_sysprim(msg);
	struct ctrl_cmd_def *cd;

	/* generate a deferred control command */
	cd = ctrl_cmd_def_make(fl1h, cmd, NULL, 10);

	sysp->id = SuperFemto_PrimId_RfClockInfoReq;
	sysp->u.rfClockInfoReq.u8RstClkCal = 0;

	return l1if_req_compl(fl1h, msg, ctrl_clkinfo_cb, cd);
}
static int ctrl_set_clkinfo_cb(struct gsm_bts_trx *trx, struct msgb *resp,
				void *data)
{
	struct ctrl_cmd_def *cd = data;
	struct ctrl_cmd *cmd = cd->cmd;

	if (ctrl_cmd_def_is_zombie(cd)) {
		msgb_free(resp);
		return 0;
	}

	cmd->reply = "success";

	ctrl_cmd_def_send(cd);

	msgb_free(resp);

	return 0;
}
static int clock_setup_cb(struct gsm_bts_trx *trx, struct msgb *resp,
			void *data)
{
	msgb_free(resp);
	return 0;
}

static int set_clock_info(struct ctrl_cmd *cmd, void *data)
{
	struct gsm_bts_trx *trx = cmd->node;
	struct femtol1_hdl *fl1h = trx_femtol1_hdl(trx);
	struct msgb *msg = sysp_msgb_alloc();
	SuperFemto_Prim_t *sysp = msgb_sysprim(msg);
	struct ctrl_cmd_def *cd;

	/* generate a deferred control command */
	cd = ctrl_cmd_def_make(fl1h, cmd, NULL, 10);

	/* Set GPS/PPS as reference */
	sysp->id = SuperFemto_PrimId_RfClockSetupReq;
	sysp->u.rfClockSetupReq.rfTrx.iClkCor = get_clk_cal(fl1h);
	sysp->u.rfClockSetupReq.rfTrx.clkSrc = fl1h->clk_src;
	sysp->u.rfClockSetupReq.rfTrxClkCal.clkSrc = SuperFemto_ClkSrcId_GpsPps;
	l1if_req_compl(fl1h, msg, clock_setup_cb, NULL);

	/* Reset the error counters */
	msg = sysp_msgb_alloc();
	sysp = msgb_sysprim(msg);
	sysp->id = SuperFemto_PrimId_RfClockInfoReq;
	sysp->u.rfClockInfoReq.u8RstClkCal = 1;

	l1if_req_compl(fl1h, msg, ctrl_set_clkinfo_cb, cd);

	return CTRL_CMD_HANDLED;
}

static int verify_clock_info(struct ctrl_cmd *cmd, const char *value, void *data)
{
	return 0;
}


CTRL_CMD_DEFINE(clock_corr, "clock-correction");
static int ctrl_get_clkcorr_cb(struct gsm_bts_trx *trx, struct msgb *resp,
				void *data)
{
	SuperFemto_Prim_t *sysp = msgb_sysprim(resp);
	struct ctrl_cmd_def *cd = data;
	struct ctrl_cmd *cmd = cd->cmd;

	if (ctrl_cmd_def_is_zombie(cd)) {
		msgb_free(resp);
		return 0;
	}

	cmd->reply = talloc_asprintf(cmd, "%d",
		sysp->u.rfClockInfoCnf.rfTrx.iClkCor);

	ctrl_cmd_def_send(cd);

	msgb_free(resp);

	return 0;
}
static int get_clock_corr(struct ctrl_cmd *cmd, void *data)
{
	struct gsm_bts_trx *trx = cmd->node;
	struct femtol1_hdl *fl1h = trx_femtol1_hdl(trx);
	struct msgb *msg = sysp_msgb_alloc();
	SuperFemto_Prim_t *sysp = msgb_sysprim(msg);
	struct ctrl_cmd_def *cd;

	/* we could theoretically simply respond with a cached value, but I
	 * prefer to to ask the actual L1 about the currently used value to
	 * avoid any mistakes */

	/* generate a deferred control command */
	cd = ctrl_cmd_def_make(fl1h, cmd, NULL, 10);

	sysp->id = SuperFemto_PrimId_RfClockInfoReq;
	sysp->u.rfClockInfoReq.u8RstClkCal = 0;

	l1if_req_compl(fl1h, msg, ctrl_get_clkcorr_cb, cd);

	return CTRL_CMD_HANDLED;
}
static int ctrl_set_clkcorr_cb(struct gsm_bts_trx *trx, struct msgb *resp,
				void *data)
{
	SuperFemto_Prim_t *sysp = msgb_sysprim(resp);
	struct ctrl_cmd_def *cd = data;
	struct ctrl_cmd *cmd = cd->cmd;

	if (ctrl_cmd_def_is_zombie(cd)) {
		msgb_free(resp);
		return 0;
	}

	if (sysp->u.rfClockSetupCnf.status != GsmL1_Status_Success) {
		cmd->type = CTRL_CMD_ERROR;
		cmd->reply = "Error setting new correction value.";
	} else
		cmd->reply = "success";

	ctrl_cmd_def_send(cd);

	msgb_free(resp);

	return 0;
}
static int set_clock_corr(struct ctrl_cmd *cmd, void *data)
{
	struct gsm_bts_trx *trx = cmd->node;
	struct femtol1_hdl *fl1h = trx_femtol1_hdl(trx);
	struct msgb *msg = sysp_msgb_alloc();
	SuperFemto_Prim_t *sysp = msgb_sysprim(msg);
	struct ctrl_cmd_def *cd;

	fl1h->clk_cal = atoi(cmd->value);

	/* generate a deferred control command */
	cd = ctrl_cmd_def_make(fl1h, cmd, NULL, 10);

	sysp->id = SuperFemto_PrimId_RfClockSetupReq;
	sysp->u.rfClockSetupReq.rfTrx.iClkCor = fl1h->clk_cal;
	sysp->u.rfClockSetupReq.rfTrx.clkSrc = fl1h->clk_src;
	sysp->u.rfClockSetupReq.rfTrxClkCal.clkSrc = SuperFemto_ClkSrcId_GpsPps;

	l1if_req_compl(fl1h, msg, ctrl_set_clkcorr_cb, cd);

	return CTRL_CMD_HANDLED;
}

static int verify_clock_corr(struct ctrl_cmd *cmd, const char *value, void *data)
{
	/* FIXME: check the range */
	return 0;
}
#endif /* HW_SYSMOBTS_V1 */

int bts_model_ctrl_cmds_install(struct gsm_bts *bts)
{
	int rc = 0;

#ifndef HW_SYSMOBTS_V1
	rc |= ctrl_cmd_install(CTRL_NODE_TRX, &cmd_clock_info);
	rc |= ctrl_cmd_install(CTRL_NODE_TRX, &cmd_clock_corr);
#endif /* HW_SYSMOBTS_V1 */

	return rc;
}