-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhcp.c
67 lines (52 loc) · 1.32 KB
/
dhcp.c
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
#include <stdint.h>
#include "radio.h"
#include "palawan.h"
struct dhcp_request {
uint8_t addr;
uint64_t uid;
} __attribute__((packed));
static long long get_uid(void) {
long long uidh = *((uint32_t *)0x40048058);
long long uidm = *((uint32_t *)0x4004805c);
long long uidl = *((uint32_t *)0x40048060);
long long uid;
uid = (uidh << 48) | (uidm << 16) | (uidl >> 16);
return uid;
}
static uint8_t dhcp_requesting;
static void prot_dhcp(uint8_t port,
uint8_t src,
uint8_t dst,
uint8_t length,
const void *data) {
(void)port;
(void)dst;
(void)length;
const struct dhcp_request *req = data;
/* Client code (e.g. we got a response) */
if (req->uid == get_uid()) {
radioSetAddress(radioDevice, req->addr);
dhcp_requesting = 0;
}
return;
}
int dhcpRequestAddress(int timeout_ms) {
struct dhcp_request request;
int ms = 0;
request.uid = get_uid();
request.addr = 0;
dhcp_requesting = 1;
radioSetHandler(radioDevice, 3, prot_dhcp);
radioSend(radioDevice, 0xff, 3, sizeof(request), &request);
while (dhcp_requesting && (ms < timeout_ms)) {
radioPoll(radioDevice);
int i;
for (i = 0; i < 5000; i++)
asm("nop");
ms++;
}
if (dhcp_requesting)
ms = -1;
dhcp_requesting = 0;
return ms;
}