SingingCat 0
application
addressing.c
1#include "main-header.h"
3
7// returns 1 if valid, 0 if not
8int adr_isvalid(long a) {
9 if (a == 0xFFFFFFFF) {
10 // broadcast is a valid address
11 return 1;
12 }
13 if (a == 0) {
14 return 0;
15 }
16 return 1;
17}
18
19// pseudo-random, mostly deterministic, unique id generator for this machine
20long adr_create_new_node_id() {
21 char buf[64];
22 byte *db;
23 long res;
24 int i, ln;
25 int rseed;
26
27 res = 0;
28 esp_get_mac((char *)&buf, 64);
29 if (strlen(buf) != 17) {
30 printf("Invalid mac: \"%s\" (%i length)\r\n", buf, (int)strlen(buf));
31 for (i = 0; i < 17; i++) {
32 buf[i] = i; // deterministic and stupid
33 }
34 }
35
36 ln = sizeof(buf);
37 mculib_mcu_get_unique_id(&ln, &buf);
38 rseed = buf[2];
39 for (;;) {
40 rseed = rseed + 5;
41 // now take the device-id of the stm32 as seed
42 db = (void *)&buf;
43 uint32_t c = 0;
44 for (i = 0; i < ln; i++) {
45 printf("%x ", db[i]);
46 c = c + db[i] + rseed;
47 }
48 printf(" == %i\r\n", c);
49 res = (c & 0xFF00) >> 8;
50 res = res | (c & 0xFF);
51
52 // parse mac, but rolling-xor the device-id to each byte
53 byte z = db[7];
54 for (i = 0; i < 6; i++) {
55 z = z ^ (db[i] + rseed);
56 res = res << 8;
57 res = res | z;
58 res = res + mculib_get_seconds_since_boot() + get_systickctr();
59 //res = res | ((atoh((char *)&buf[i * 3 + 4])) + z);
60 }
61 if (adr_isvalid(res)) {
62 break;
63 }
64 }
65 return res;
66}
67void node_to_str(long l, char *buf) {
68 ltoh(l, buf);
69}
70
71void pretty_node_to_str(long l, char *buf) {
72 buf[0] = 0;
73 char const *t;
74
75 t = NULL;
76 if (l == 0x00000001) {
77 t = "SERVER";
78 } else if (l == 0xFFFFFFFF) {
79 t = "BRDCAST";
80 } else if (l == get_my_node_id()) {
81 t = "ITSME";
82 }
83 if (t == NULL) {
84 node_to_str(l, buf);
85 } else {
86 memcpy(buf, t, strlen(t));
87 buf[strlen(t)] = 0;
88 }
89}
90void print_node_id(long l) {
91 char buf[32];
92
93 node_to_str(l, (char *)&buf);
94
95 IPLOG("[%D] NODE-ID: %s\r\n", mculib_get_seconds_since_boot(), (char *)&buf);
96}
long get_my_node_id()
get the id of my node
int esp_get_mac(char *buf, int bufsize)
get the esp8266 mac address.
Definition: esp8266.c:931