SingingCat 0
application
nic_registry.c
1#include "networkif.h"
2#include "sx1262/sx1262.h"
3
4static struct network_context registrations[5];
5void init_nic_registry() {
6 memset(&registrations, sizeof(registrations), 0);
7}
8
9void register_nic(struct networkif *nif, int SOURCE) {
10 int i;
11
12 for (i = 0; i < 5; i++) {
13 struct network_context *r = &registrations[i];//not nic_by_index, because we need to figure out wether it is full or not free
14 if (r->nif == NULL) {
15 r->nif = nif;
16 r->status = 0;
17 r->source = SOURCE;
18 r->mculib_handle = i + 10;
19 return;
20 }
21 }
22 printf("Too many nics registered.\r\n");
23}
24// e.g. lora/radio/bluetooth.. (or NULL)
25struct network_context *nic_by_type(int type) {
26 int i = 0;
27
28 for (;;) {
29 struct network_context *r = nic_by_index(i);
30 if (r == NULL) {
31 return NULL;
32 }
33 if (r->source == type) {
34 return r;
35 }
36 i++;
37 }
38 return NULL;
39}
40
41// NULL if none at that index (count [0..n], stop at first NULL)
42struct network_context *nic_by_index(int index) {
43 if (index > 5) {
44 return NULL;
45 }
46 struct network_context *r = &registrations[index];
47
48 if (r->nif == NULL) {
49 return NULL;
50 }
51 return r;
52}