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

#include <osmocom/core/linuxlist.h>
#include <osmocom/core/talloc.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>

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,	"connectiong" },
	{ PHY_LINK_CONNECTED,	"connected" },
	{ 0, NULL }
};

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

	LOGP(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));

	/* 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;

		switch (state) {
		case PHY_LINK_CONNECTED:
			LOGP(DL1C, LOGL_INFO, "trx_set_avail(1)\n");
			trx_set_available(trx, 1);
			break;
		case PHY_LINK_SHUTDOWN:
			LOGP(DL1C, LOGL_INFO, "trx_set_avail(0)\n");
			trx_set_available(trx, 0);
			break;
		case PHY_LINK_CONNECTING:
			/* nothing to do */
			break;
		}
	}

	plink->state = state;
}

struct phy_instance *phy_instance_by_num(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)
{
	trx->role_bts.l1h = 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 */
	OSMO_ASSERT(pinst->trx->role_bts.l1h == pinst);
	pinst->trx->role_bts.l1h = 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);
}

int phy_links_open(void)
{
	struct phy_link *plink;

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

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

	return 0;
}

const char *phy_instance_name(struct phy_instance *pinst)
{
	static char buf[32];

	snprintf(buf, sizeof(buf), "phy%u.%u", pinst->phy_link->num,
		 pinst->num);
	return buf;
}