uint8_t ps2_host_recv_response(void) { // Command may take 25ms/20ms at most([5]p.46, [3]p.21) uint8_t retry = 25; while (retry-- && !pbuf_has_data()) { _delay_ms(1); } return pbuf_dequeue(); }
/** * Update (or insert) a IP/MAC address pair in the ARP cache. * * If a pending entry is resolved, any queued packets will be sent * at this point. * * @param ipaddr IP address of the inserted ARP entry. * @param ethaddr Ethernet address of the inserted ARP entry. * @param flags Defines behaviour: * - ETHARP_TRY_HARD Allows ARP to insert this as a new item. If not specified, * only existing ARP entries will be updated. * * @return * - ERR_OK Succesfully updated ARP cache. * - ERR_MEM If we could not add a new ARP entry when ETHARP_TRY_HARD was set. * - ERR_ARG Non-unicast address given, those will not appear in ARP cache. * * @see pbuf_free() */ static err_t update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags) { s8_t i, k; LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 3, ("update_arp_entry()\n")); LWIP_ASSERT("netif->hwaddr_len != 0", netif->hwaddr_len != 0); LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n", ip4_addr1(ipaddr), ip4_addr2(ipaddr), ip4_addr3(ipaddr), ip4_addr4(ipaddr), ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2], ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5])); /* non-unicast address? */ if (ip_addr_isany(ipaddr) || ip_addr_isbroadcast(ipaddr, netif) || ip_addr_ismulticast(ipaddr)) { LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n")); return ERR_ARG; } /* find or create ARP entry */ i = find_entry(ipaddr, flags); /* bail out if no entry could be found */ if (i < 0) return (err_t)i; /* mark it stable */ arp_table[i].state = ETHARP_STATE_STABLE; LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i)); /* update address */ for (k = 0; k < netif->hwaddr_len; ++k) { arp_table[i].ethaddr.addr[k] = ethaddr->addr[k]; } /* reset time stamp */ arp_table[i].ctime = 0; /* this is where we will send out queued packets! */ #if ARP_QUEUEING while (arp_table[i].p != NULL) { /* get the first packet on the queue */ struct pbuf *p = arp_table[i].p; /* Ethernet header */ struct eth_hdr *ethhdr = p->payload; /* remember (and reference) remainder of queue */ /* note: this will also terminate the p pbuf chain */ arp_table[i].p = pbuf_dequeue(p); /* fill-in Ethernet header */ for (k = 0; k < netif->hwaddr_len; ++k) { ethhdr->dest.addr[k] = ethaddr->addr[k]; ethhdr->src.addr[k] = netif->hwaddr[k]; } ethhdr->type = htons(ETHTYPE_IP); LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: sending queued IP packet %p.\n", (void *)p)); /* send the queued IP packet */ netif->linkoutput(netif, p); /* free the queued IP packet */ pbuf_free(p); } #endif return ERR_OK; }
uint8_t ps2_host_recv(void) { if (pbuf_has_data()) { ps2_error = PS2_ERR_NONE; return pbuf_dequeue(); } else { ps2_error = PS2_ERR_NODATA; return 0; } }
/* get data received by interrupt */ uint8_t ps2_host_recv(void) { if (ps2_error) { print("x"); phex(ps2_error); ps2_host_send(0xFE); // request to resend ps2_error = PS2_ERR_NONE; } idle(); return pbuf_dequeue(); }
/** * Update (or insert) a IP/MAC address pair in the ARP cache. * * If a pending entry is resolved, any queued packets will be sent * at this point. * * @param ipaddr IP address of the inserted ARP entry. * @param ethaddr Ethernet address of the inserted ARP entry. * @param flags Defines behaviour: * - ETHARP_TRY_HARD Allows ARP to insert this as a new item. If not specified, * only existing ARP entries will be updated. * * @return * - ERR_OK Succesfully updated ARP cache. * - ERR_MEM If we could not add a new ARP entry when ETHARP_TRY_HARD was set. * - ERR_ARG Non-unicast address given, those will not appear in ARP cache. * * @see pbuf_free() */ err_t update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u32_t flags) { s8_t i, k; LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 3, ("update_arp_entry()\n")); LWIP_ASSERT("netif->hwaddr_len != 0", netif->hwaddr_len != 0); LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: %lu.%lu.%lu.%lu - %02x:%02x:%02x:%02x:%02x:%02x\n", ip4_addr1(ipaddr), ip4_addr2(ipaddr), ip4_addr3(ipaddr), ip4_addr4(ipaddr), ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2], ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5])); /* non-unicast address? */ /* XXX XXX XXX broadcast control on netif!*/ if (ip_addr_isany(ipaddr) || /*ip_addr_is_v4broadcast(ipaddr, &(al->ipaddr), &(al->netmask)) ||*/ ip_addr_ismulticast(ipaddr)) { LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n")); return ERR_ARG; } /* find or create ARP entry */ i = find_entry(ipaddr, flags); /* bail out if no entry could be found */ if (i < 0) return (err_t)i; /* mark it stable */ arp_table[i].state = (flags & ATF_PERM)?ETHARP_STATE_PERMANENT:ETHARP_STATE_STABLE; LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: updating %s entry %u\n", (flags & ATF_PERM)?"permanent":"stable",i)); //printf("%lx %lx %lx %lx\n",ipaddr->addr[0],ipaddr->addr[1],ipaddr->addr[2],ipaddr->addr[3]); /* update address */ for (k = 0; k < netif->hwaddr_len; ++k) { arp_table[i].ethaddr.addr[k] = ethaddr->addr[k]; } arp_table[i].if_id=netif->id; /* reset time stamp */ arp_table[i].ctime = 0; /* this is where we will send out queued packets! */ #if ARP_QUEUEING while (arp_table[i].p != NULL) { /* get the first packet on the queue */ struct pbuf *p = arp_table[i].p; /* Ethernet header */ struct eth_hdr *ethhdr = p->payload; /* remember (and reference) remainder of queue */ /* note: this will also terminate the p pbuf chain */ arp_table[i].p = pbuf_dequeue(p); /* fill-in Ethernet header */ for (k = 0; k < netif->hwaddr_len; ++k) { ethhdr->dest.addr[k] = ethaddr->addr[k]; ethhdr->src.addr[k] = netif->hwaddr[k]; } // Fix by Renzo Davoli // ethhdr->type = htons(ETHTYPE_IP); LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: sending queued IP packet %p.\n", (void *)p)); /* send the queued IP packet */ LINKOUTPUT(netif, p); /* free the queued IP packet */ pbuf_free(p); } #endif return ERR_OK; }
uint8_t ps2_host_recv_response(void) { while (!pbuf_has_data()) ; return pbuf_dequeue(); }
/** * Update (or insert) a IP/MAC address pair in the ARP cache. * * If a pending entry is resolved, any queued packets will be sent * at this point. * * @param ipaddr IP address of the inserted ARP entry. * @param ethaddr Ethernet address of the inserted ARP entry. * @param flags Defines behaviour: * - ARP_INSERT_FLAG Allows ARP to insert this as a new item. If not specified, * only existing ARP entries will be updated. * * @return pbuf If non-NULL, a packet that was queued on a pending entry. * You should sent it and must call pbuf_free() afterwards. * * @see pbuf_free() */ static struct pbuf * update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags) { s8_t i, k; LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 3, ("update_arp_entry()\n")); LWIP_ASSERT("netif->hwaddr_len != 0", netif->hwaddr_len != 0); LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: %u.%u.%u.%u - %02x:%02x:%02x:%02x:%02x:%02x\n", ip4_addr1(ipaddr), ip4_addr2(ipaddr), ip4_addr3(ipaddr), ip4_addr4(ipaddr), ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2], ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5])); /* do not update for 0.0.0.0 addresses */ if (ipaddr->addr == 0) { LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: will not add 0.0.0.0 to ARP cache\n")); return NULL; } /* Walk through the ARP mapping table and try to find an entry to update. If none is found, the IP -> MAC address mapping is inserted in the ARP table. */ for (i = 0; i < ARP_TABLE_SIZE; ++i) { /* Check if the source IP address of the incoming packet matches the IP address in this ARP table entry. */ if (arp_table[i].state != ETHARP_STATE_EMPTY && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) { /* pending entry? */ if (arp_table[i].state == ETHARP_STATE_PENDING) { LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: pending entry %u goes stable\n", i)); /* A pending entry was found, mark it stable */ arp_table[i].state = ETHARP_STATE_STABLE; /* fall-through to next if */ } /* stable entry? (possibly just marked to become stable) */ if (arp_table[i].state == ETHARP_STATE_STABLE) { #if ARP_QUEUEING struct pbuf *p; struct eth_hdr *ethhdr; #endif LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: updating stable entry %u\n", i)); /* An old entry found, update this and return. */ for (k = 0; k < netif->hwaddr_len; ++k) { arp_table[i].ethaddr.addr[k] = ethaddr->addr[k]; } /* reset time stamp */ arp_table[i].ctime = 0; /* this is where we will send out queued packets! */ #if ARP_QUEUEING while (arp_table[i].p != NULL) { /* get the first packet on the queue (if any) */ p = arp_table[i].p; /* remember (and reference) remainder of queue */ arp_table[i].p = pbuf_dequeue(p); /* fill-in Ethernet header */ ethhdr = p->payload; for (k = 0; k < netif->hwaddr_len; ++k) { ethhdr->dest.addr[k] = ethaddr->addr[k]; ethhdr->src.addr[k] = netif->hwaddr[k]; } ethhdr->type = htons(ETHTYPE_IP); LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: sending queued IP packet %p.\n", (void *)p)); /* send the queued IP packet */ netif->linkoutput(netif, p); /* free the queued IP packet */ pbuf_free(p); } #endif /* IP addresses should only occur once in the ARP entry, we are done */ return NULL; } } /* if STABLE */ } /* for all ARP entries */ /* no matching ARP entry was found */ LWIP_ASSERT("update_arp_entry: i == ARP_TABLE_SIZE", i == ARP_TABLE_SIZE); LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: IP address not yet in table\n")); /* allowed to insert a new entry? */ if (flags & ARP_INSERT_FLAG) { LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: adding entry to table\n")); /* find an empty or old entry. */ i = find_arp_entry(); if (i == ERR_MEM) { LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: no available entry found\n")); return NULL; } /* set IP address */ ip_addr_set(&arp_table[i].ipaddr, ipaddr); /* set Ethernet hardware address */ for (k = 0; k < netif->hwaddr_len; ++k) { arp_table[i].ethaddr.addr[k] = ethaddr->addr[k]; } /* reset time-stamp */ arp_table[i].ctime = 0; /* mark as stable */ arp_table[i].state = ETHARP_STATE_STABLE; /* no queued packet */ #if ARP_QUEUEING arp_table[i].p = NULL; #endif } else { LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: no matching stable entry to update\n")); } return NULL; }