|
|
@@ -15,6 +15,8 @@
|
|
|
// DHCP 客户端逻辑
|
|
|
// -------------------------------------
|
|
|
|
|
|
+static void dhcp_client_timer_cb(void *arg);
|
|
|
+
|
|
|
static int ulwip_dhcp_client_run(ulwip_ctx_t* ctx, char* rxbuff, size_t len) {
|
|
|
PV_Union uIP;
|
|
|
// 检查dhcp的状态
|
|
|
@@ -34,6 +36,7 @@ static int ulwip_dhcp_client_run(ulwip_ctx_t* ctx, char* rxbuff, size_t len) {
|
|
|
|
|
|
// 看看是不是获取成功了
|
|
|
if (DHCP_STATE_CHECK == dhcp->state) {
|
|
|
+on_check:
|
|
|
uIP.u32 = dhcp->ip;
|
|
|
LLOGD("动态IP:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
|
|
|
uIP.u32 = dhcp->submask;
|
|
|
@@ -49,19 +52,26 @@ static int ulwip_dhcp_client_run(ulwip_ctx_t* ctx, char* rxbuff, size_t len) {
|
|
|
dhcp->state = DHCP_STATE_WAIT_LEASE_P1;
|
|
|
if (rxbuff) {
|
|
|
luat_heap_free(rxbuff);
|
|
|
+ rxbuff = NULL;
|
|
|
}
|
|
|
ulwip_netif_ip_event(ctx);
|
|
|
+ luat_rtos_timer_stop(ctx->dhcp_timer);
|
|
|
+ luat_rtos_timer_start(ctx->dhcp_timer, 60000, 1, dhcp_client_timer_cb, ctx);
|
|
|
return 0;
|
|
|
}
|
|
|
result = ip4_dhcp_run(dhcp, rxbuff == NULL ? NULL : &rx_msg_buf, &tx_msg_buf, &remote_ip);
|
|
|
if (rxbuff) {
|
|
|
luat_heap_free(rxbuff);
|
|
|
+ rxbuff = NULL;
|
|
|
}
|
|
|
if (result) {
|
|
|
LLOGE("ip4_dhcp_run error %d", result);
|
|
|
return 0;
|
|
|
}
|
|
|
if (!tx_msg_buf.Pos) {
|
|
|
+ if (DHCP_STATE_CHECK == dhcp->state) {
|
|
|
+ goto on_check;
|
|
|
+ }
|
|
|
return 0; // 没有数据需要发送
|
|
|
}
|
|
|
// 通过UDP发出来
|
|
|
@@ -76,13 +86,16 @@ static int ulwip_dhcp_client_run(ulwip_ctx_t* ctx, char* rxbuff, size_t len) {
|
|
|
data += q->len;
|
|
|
}
|
|
|
data = p->payload;
|
|
|
- // LLOGI("dhcp payload len %d %02X%02X%02X%02X", p->tot_len, data[0], data[1], data[2], data[3]);
|
|
|
+ LLOGI("dhcp payload len %d %02X%02X%02X%02X", p->tot_len, data[0], data[1], data[2], data[3]);
|
|
|
udp_sendto_if(ctx->dhcp_pcb, p, IP_ADDR_BROADCAST, 67, netif);
|
|
|
pbuf_free(p);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
static int ulwip_dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) {
|
|
|
+ if (addr == NULL || port != 67 || pcb == NULL) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
LLOGD("收到DHCP数据包(len=%d)", p->tot_len);
|
|
|
ulwip_ctx_t *ctx = (ulwip_ctx_t *)arg;
|
|
|
char* ptr = luat_heap_malloc(p->tot_len);
|
|
|
@@ -147,5 +160,7 @@ void ulwip_dhcp_client_start(ulwip_ctx_t *ctx) {
|
|
|
void ulwip_dhcp_client_stop(ulwip_ctx_t *ctx) {
|
|
|
if (luat_rtos_timer_is_active(ctx->dhcp_timer)) {
|
|
|
luat_rtos_timer_stop(ctx->dhcp_timer);
|
|
|
+ ctx->dhcp_client->state = DHCP_STATE_DISCOVER;
|
|
|
+ ctx->dhcp_client->discover_cnt = 0;
|
|
|
}
|
|
|
}
|