aboutsummaryrefslogtreecommitdiffstats
path: root/src/ipa_unit.c
blob: b5400ed337f69ccfc19bf56d9ed7ff158319e088 (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
/* (C) 2012-2013 by Pablo Neira Ayuso <pablo@gnumonks.org>
 * All Rights Reserved.
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

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

#include <stdint.h>
#include <string.h>

struct osmo_ipa_unit {
	struct llist_head head;

	uint16_t	site_id;
	uint16_t	bts_id;
	uint16_t	trx_id;
	char		*name;
	char		*hwvers;
	char		*swvers;
	uint8_t		mac_addr[6];
	char		*location1;
	char		*location2;
	char		*serno;

	uint8_t		data[0];
};

struct osmo_ipa_unit *osmo_ipa_unit_alloc(size_t datalen)
{
	struct osmo_ipa_unit *unit;

	unit = talloc_zero_size(NULL, sizeof(struct osmo_ipa_unit) + datalen);
	if (unit == NULL)
		return NULL;

	unit->name = talloc_strdup(unit, "");
	unit->hwvers = talloc_strdup(unit, "");
	unit->swvers = talloc_strdup(unit, "");
	unit->location1 = talloc_strdup(unit, "");
	unit->location2 = talloc_strdup(unit, "");
	unit->serno = talloc_strdup(unit, "");

	return unit;
}

void osmo_ipa_unit_free(struct osmo_ipa_unit *unit)
{
	if (unit->name)
		free(unit->name);
	if (unit->hwvers)
		free(unit->hwvers);
	if (unit->swvers)
		free(unit->swvers);
	if (unit->location1)
		free(unit->location1);
	if (unit->location2)
		free(unit->location2);
	if (unit->serno)
		free(unit->serno);

	talloc_free(unit);
}

void *osmo_ipa_unit_get_data(struct osmo_ipa_unit *unit)
{
	return unit->data;
}

void osmo_ipa_unit_set_site_id(struct osmo_ipa_unit *unit, uint16_t site_id)
{
	unit->site_id = site_id;
}

void osmo_ipa_unit_set_bts_id(struct osmo_ipa_unit *unit, uint16_t bts_id)
{
	unit->bts_id = bts_id;
}

void osmo_ipa_unit_set_trx_id(struct osmo_ipa_unit *unit, uint16_t trx_id)
{
	unit->trx_id = trx_id;
}

void osmo_ipa_unit_set_unit_name(struct osmo_ipa_unit *unit, const char *name)
{
	if (unit->name)
		free(unit->name);

	unit->name = talloc_strdup(unit, name);
}

void osmo_ipa_unit_set_unit_hwvers(struct osmo_ipa_unit *unit, const char *vers)
{
	if (unit->hwvers)
		free(unit->hwvers);

	unit->hwvers = talloc_strdup(unit, vers);
}

void osmo_ipa_unit_set_unit_swvers(struct osmo_ipa_unit *unit, const char *vers)
{
	if (unit->swvers)
		free(unit->swvers);

	unit->swvers = talloc_strdup(unit, vers);
}

void osmo_ipa_unit_set_unit_mac_addr(struct osmo_ipa_unit *unit, uint8_t *addr)
{
	memcpy(unit->mac_addr, addr, sizeof(unit->mac_addr));
}

void osmo_ipa_unit_set_unit_location1(struct osmo_ipa_unit *unit, const char *loc)
{
	if (unit->location1)
		free(unit->location1);

	unit->location1 = talloc_strdup(unit, loc);
}

void osmo_ipa_unit_set_unit_location2(struct osmo_ipa_unit *unit, const char *loc)
{
	if (unit->location2)
		free(unit->location2);

	unit->location2 = talloc_strdup(unit, loc);
}

void osmo_ipa_unit_set_unit_serno(struct osmo_ipa_unit *unit, const char *serno)
{
	unit->serno = talloc_strdup(unit, serno);
}

uint16_t osmo_ipa_unit_get_site_id(struct osmo_ipa_unit *unit)
{
	return unit->site_id;
}

uint16_t osmo_ipa_unit_get_bts_id(struct osmo_ipa_unit *unit)
{
	return unit->bts_id;
}

uint16_t osmo_ipa_unit_get_trx_id(struct osmo_ipa_unit *unit)
{
	return unit->trx_id;
}

const char *osmo_ipa_unit_get_unit_name(struct osmo_ipa_unit *unit)
{
	return unit->name;
}

const char *osmo_ipa_unit_get_unit_hwvers(struct osmo_ipa_unit *unit)
{
	return unit->hwvers;
}

const char *osmo_ipa_unit_get_unit_swvers(struct osmo_ipa_unit *unit)
{
	return unit->swvers;
}

uint8_t *osmo_ipa_unit_get_unit_mac_addr(struct osmo_ipa_unit *unit)
{
	return unit->mac_addr;
}

const char *osmo_ipa_unit_get_unit_location1(struct osmo_ipa_unit *unit)
{
	return unit->location1;
}

const char *osmo_ipa_unit_get_unit_location2(struct osmo_ipa_unit *unit)
{
	return unit->location2;
}

const char *osmo_ipa_unit_get_unit_serno(struct osmo_ipa_unit *unit)
{
	return unit->serno;
}

struct osmo_ipa_unit *
osmo_ipa_unit_find(struct llist_head *list, uint16_t site_id, uint16_t bts_id)
{
	struct osmo_ipa_unit *unit;

	llist_for_each_entry(unit, list, head) {
		if (unit->site_id == site_id &&
		    unit->bts_id == bts_id)
			return unit;
	}
	return NULL;
}

void osmo_ipa_unit_add(struct llist_head *list, struct osmo_ipa_unit *unit)
{
	llist_add(&unit->head, list);
}

int osmo_ipa_unit_snprintf(char *buf, size_t size, struct osmo_ipa_unit *unit)
{
	return snprintf(buf, size, "%u/%u/%u",
			unit->site_id, unit->bts_id, unit->trx_id);
}

int osmo_ipa_unit_snprintf_mac_addr(char *buf, size_t size,
				    struct osmo_ipa_unit *unit)
{
	return snprintf(buf, size, "%02x:%02x:%02x:%02x:%02x:%02x",
			unit->mac_addr[0], unit->mac_addr[1],
			unit->mac_addr[2], unit->mac_addr[3],
			unit->mac_addr[4], unit->mac_addr[5]);
}

int osmo_ipa_unit_snprintf_name(char *buf, size_t size,
				struct osmo_ipa_unit *unit)
{
	return snprintf(buf, size, "%s-%02x-%02x-%02x-%02x-%02x-%02x",
			unit->name,
			unit->mac_addr[0], unit->mac_addr[1],
			unit->mac_addr[2], unit->mac_addr[3],
			unit->mac_addr[4], unit->mac_addr[5]);
}

int osmo_ipa_unit_snprintf_loc1(char *buf, size_t size,
				struct osmo_ipa_unit *unit)
{
	return snprintf(buf, size, "%s", unit->location1);
}

int osmo_ipa_unit_snprintf_loc2(char *buf, size_t size,
				struct osmo_ipa_unit *unit)
{
	return snprintf(buf, size, "%s", unit->location2);
}

int osmo_ipa_unit_snprintf_hwvers(char *buf, size_t size,
				  struct osmo_ipa_unit *unit)
{
	return snprintf(buf, size, "%s", unit->hwvers);
}

int osmo_ipa_unit_snprintf_swvers(char *buf, size_t size,
				  struct osmo_ipa_unit *unit)
{
	return snprintf(buf, size, "%s", unit->hwvers);
}

int osmo_ipa_unit_snprintf_serno(char *buf, size_t size,
				 struct osmo_ipa_unit *unit)
{
	return snprintf(buf, size, "%s", unit->serno);
}