aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/phy_link.c
blob: 352d8f72f96ccb2d193b04d2f0a2be6ea4efe532 (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
#include <stdint.h>

#include <osmocom/core/linuxlist.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/fsm.h>

#include <osmo-bts/bts.h>
#include <osmo-bts/gsm_data.h>
#include <osmo-bts/phy_link.h>
#include <osmo-bts/oml.h>
#include <osmo-bts/logging.h>
#include <osmo-bts/bts_model.h>
#include <osmo-bts/nm_common_fsm.h>

static LLIST_HEAD(g_phy_links);

struct phy_link *phy_link_by_num(int num)
{
	struct phy_link *plink;

	llist_for_each_entry(plink, &g_phy_links, list) {
		if (plink->num == num)
			return plink;
	}

	return NULL;
}

struct phy_link *phy_link_create(void *ctx, int num)
{
	struct phy_link *plink;

	if (phy_link_by_num(num))
		return NULL;

	plink = talloc_zero(ctx, struct phy_link);
	plink->num = num;
	plink->state = PHY_LINK_SHUTDOWN;
	INIT_LLIST_HEAD(&plink->instances);
	llist_add_tail(&plink->list, &g_phy_links);

	bts_model_phy_link_set_defaults(plink);

	return plink;
}

const struct value_string phy_link_state_vals[] = {
	{ PHY_LINK_SHUTDOWN, 	"shutdown" },
	{ PHY_LINK_CONNECTING,	"connecting" },
	{ PHY_LINK_CONNECTED,	"connected" },
	{ 0, NULL }
};

void phy_link_state_set(struct phy_link *plink, enum phy_link_state state)
{
	struct phy_instance *pinst;

	LOGPPHL(plink, DL1C, LOGL_INFO, "PHY link state change %s -> %s\n",
	        get_value_string(phy_link_state_vals, plink->state),
	        get_value_string(phy_link_state_vals, state));

	plink->state = state;

	/* notify all TRX associated with this phy */
	llist_for_each_entry(pinst, &plink->instances, list) {
		struct gsm_bts_trx *trx = pinst->trx;
		if (!trx)
			continue;

		osmo_fsm_inst_dispatch(trx->mo.fi,
				       state == PHY_LINK_CONNECTED ? NM_EV_PHYLINK_UP :
				                                     NM_EV_PHYLINK_DOWN,
				       NULL);
		osmo_fsm_inst_dispatch(trx->bb_transc.mo.fi,
				       state == PHY_LINK_CONNECTED ? NM_EV_PHYLINK_UP :
				                                     NM_EV_PHYLINK_DOWN,
				       NULL);
	}
}

enum phy_link_state phy_link_state_get(struct phy_link *plink)
{
	return plink->state;
}

const char *phy_link_state_name(enum phy_link_state state)
{
	return get_value_string(phy_link_state_vals, state);
}

struct phy_instance *phy_instance_by_num(const struct phy_link *plink, int num)
{
	struct phy_instance *pinst;

	llist_for_each_entry(pinst, &plink->instances, list) {
		if (pinst->num == num)
			return pinst;
	}
	return NULL;
}

struct phy_instance *phy_instance_create(struct phy_link *plink, int num)
{
	struct phy_instance *pinst;

	if (phy_instance_by_num(plink, num))
		return NULL;

	pinst = talloc_zero(plink, struct phy_instance);
	pinst->num = num;
	pinst->phy_link = plink;
	llist_add_tail(&pinst->list, &plink->instances);

	bts_model_phy_instance_set_defaults(pinst);

	return pinst;
}

void phy_instance_link_to_trx(struct phy_instance *pinst, struct gsm_bts_trx *trx)
{
	/* There might already be an associated TRX */
	OSMO_ASSERT(pinst->trx == NULL)
	trx->pinst = pinst;
	pinst->trx = trx;
}

void phy_instance_destroy(struct phy_instance *pinst)
{
	/* remove from list of instances in the link */
	llist_del(&pinst->list);

	/* remove reverse link from TRX (if associated) */
	if (pinst->trx != NULL) {
		OSMO_ASSERT(pinst->trx->pinst == pinst);
		pinst->trx->pinst = NULL;
		pinst->trx = NULL;
	}

	talloc_free(pinst);
}

void phy_link_destroy(struct phy_link *plink)
{
	struct phy_instance *pinst, *pinst2;

	llist_for_each_entry_safe(pinst, pinst2, &plink->instances, list)
		phy_instance_destroy(pinst);

	talloc_free(plink);
}

static char name_buf[32];
const char *phy_link_name(const struct phy_link *plink)
{
	snprintf(name_buf, sizeof(name_buf), "phy%u", plink->num);
	return name_buf;
}

int phy_links_open(void)
{
	const struct phy_instance *pinst;
	struct phy_link *plink;

	llist_for_each_entry(plink, &g_phy_links, list) {
		int rc;

		/* Warn about dangling PHY instances */
		llist_for_each_entry(pinst, &plink->instances, list) {
			if (pinst->trx != NULL)
				continue;
			LOGPPHI(pinst, DL1C, LOGL_NOTICE, "This PHY instance is not associated "
				"with a TRX instance, check the configuration file!\n");
		}

		rc = bts_model_phy_link_open(plink);
		if (rc < 0)
			return rc;
	}

	return 0;
}

const char *phy_instance_name(const struct phy_instance *pinst)
{
	snprintf(name_buf, sizeof(name_buf), "phy%u.%u", pinst->phy_link->num,
		 pinst->num);
	return name_buf;
}