blob: af08a81e83b897f7774be482002409b8238d8ed6 [file] [log] [blame]
Harald Weltecbd18962019-03-03 19:02:38 +01001#pragma once
2#include <stdint.h>
3#include <stdbool.h>
4#include <pthread.h>
5#include <osmocom/core/linuxlist.h>
6
Harald Welte91a0a4a2019-03-03 20:56:06 +01007#define REMSIM_SERVER 1
8
Harald Weltecbd18962019-03-03 19:02:38 +01009struct bank_slot {
10 uint16_t bank_id;
11 uint16_t slot_nr;
12};
13
14static inline bool bank_slot_equals(const struct bank_slot *a, const struct bank_slot *b)
15{
16 if (a->bank_id == b->bank_id && a->slot_nr == b->slot_nr)
17 return true;
18 else
19 return false;
20}
21
22struct client_slot {
23 uint16_t client_id;
24 uint16_t slot_nr;
25};
26
27static inline bool client_slot_equals(const struct client_slot *a, const struct client_slot *b)
28{
29 if (a->client_id == b->client_id && a->slot_nr == b->slot_nr)
30 return true;
31 else
32 return false;
33}
34
Harald Welte91a0a4a2019-03-03 20:56:06 +010035enum slot_mapping_state {
36 SLMAP_S_NEW, /* created; not yet sent to bankd */
37 SLMAP_S_UNACKNOWLEDGED, /* created + sent to bankd but not yet acknowledge by bankd */
38 SLMAP_S_ACTIVE, /* fully active map; acknowledged by bankd */
39 SLMAP_S_DELETING, /* we were asked to delete it; bankd hasn't confirmed yet */
40};
41extern const struct value_string slot_map_state_name[];
42static inline const char *slotmap_state_name(enum slot_mapping_state st)
43{
44 return get_value_string(slot_map_state_name, st);
45}
46
Harald Weltecbd18962019-03-03 19:02:38 +010047/* slot mappings are created / removed by the server */
48struct slot_mapping {
49 /* global lits of bankd slot mappings */
50 struct llist_head list;
Harald Welte91a0a4a2019-03-03 20:56:06 +010051 struct slotmaps *maps;
52
Harald Weltecbd18962019-03-03 19:02:38 +010053 /* slot on bank side */
54 struct bank_slot bank;
55 /* slot on client side */
56 struct client_slot client;
Harald Welte91a0a4a2019-03-03 20:56:06 +010057
58#ifdef REMSIM_SERVER
59 struct llist_head bank_list;
60 enum slot_mapping_state state;
61#endif
Harald Weltecbd18962019-03-03 19:02:38 +010062};
63
64/* collection of slot mappings */
65struct slotmaps {
66 struct llist_head mappings;
67 pthread_rwlock_t rwlock;
68};
69
70/* thread-safe lookup of map by client:slot */
71struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client);
72
73/* thread-safe lookup of map by bank:slot */
74struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank);
75
76/* thread-safe creating of a new bank<->client map */
Harald Weltef4e75042019-03-03 21:54:52 +010077struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, const struct client_slot *client);
Harald Weltecbd18962019-03-03 19:02:38 +010078
79/* thread-safe removal of a bank<->client map */
80void slotmap_del(struct slotmaps *maps, struct slot_mapping *map);
81
82/* initialize the entire map collection */
83struct slotmaps *slotmap_init(void *ctx);
Harald Welte91a0a4a2019-03-03 20:56:06 +010084
85#ifdef REMSIM_SERVER
86void _slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
87 struct llist_head *new_bank_list);
88/* thread-safe way to change the state of given slot map */
89void slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
90 struct llist_head *new_bank_list);
91#endif