/** * @brief Process Network * @param None * @retval None */ void Network_Process(void) { static uint8_t arp_counter = 0; int i; for (i = 0; i < UIP_CONNS; i++) { uip_periodic(i); if (uip_len > 0) { uip_arp_out(); enc28j60_send_packet((uint8_t *)uip_buf, uip_len); } } #if UIP_UDP for (i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); if (uip_len > 0) { uip_arp_out(); network_send(); } } #endif /* UIP_UDP */ if (++arp_counter >= 50) { arp_counter = 0; uip_arp_timer(); } uip_len = enc28j60_recv_packet((uint8_t *)uip_buf, UIP_BUFSIZE); if (uip_len > 0) { if (((struct uip_eth_hdr *)&uip_buf[0])->type == htons(UIP_ETHTYPE_IP)) { uip_arp_ipin(); uip_input(); if (uip_len > 0) { uip_arp_out(); enc28j60_send_packet((uint8_t *)uip_buf, uip_len); } } else if (((struct uip_eth_hdr *)&uip_buf[0])->type == htons(UIP_ETHTYPE_ARP)) { uip_arp_arpin(); if (uip_len > 0) { enc28j60_send_packet((uint8_t *)uip_buf, uip_len); } } } }
void sendCoapMsg(uint8_t* packet, CoapMsg* coapMsg, char *payload) { coapMsg->coapHdr = (CoapHdr*) packet; //Reset options coapMsg->options = (CoapOptions) COAP_OPTIONS_INITIALISATION; //Swap source and destination MAC addresses memcpy(coapMsg->coapHdr->udp.ip.eth.DestAddrs, coapMsg->coapHdr->udp.ip.eth.SrcAddrs, sizeof(deviceMAC)); memcpy(coapMsg->coapHdr->udp.ip.eth.SrcAddrs, deviceMAC, sizeof(deviceMAC)); //Swap source and destination IP addresses memcpy(coapMsg->coapHdr->udp.ip.dest, coapMsg->coapHdr->udp.ip.source, sizeof(deviceIP)); memcpy(coapMsg->coapHdr->udp.ip.source, deviceIP, sizeof(deviceIP)); //Swap ports uint16_t destPort = coapMsg->coapHdr->udp.destPort; coapMsg->coapHdr->udp.destPort = coapMsg->coapHdr->udp.sourcePort; coapMsg->coapHdr->udp.sourcePort = destPort; //Setup CoAP response if (coapMsg->coapHdr->type == COAP_TYPE_CONFIRMABLE) coapMsg->coapHdr->type = COAP_TYPE_ACKNOWLEDGEMENT; coapMsg->coapHdr->code = COAP_CODE_CONTENT; //Payload marker uint8_t ff = 0xFF; printf("Token length: %u\r", coapMsg->coapHdr->tokenLength); memcpy(packet + sizeof(CoapHdr) + coapMsg->coapHdr->tokenLength, &ff, 1); //Payload uint8_t payloadLen = strlen(payload); memcpy(packet + sizeof(CoapHdr) + coapMsg->coapHdr->tokenLength + 1, payload, payloadLen); //Total packet length uint16_t len = sizeof(CoapHdr) + coapMsg->coapHdr->tokenLength + 1 + payloadLen; //Setup UDP packet coapMsg->coapHdr->udp.len = (uint16_t) HTONS((len-sizeof(IPhdr))); coapMsg->coapHdr->udp.ip.len = (uint16_t) HTONS((len-sizeof(EtherNetII))); coapMsg->coapHdr->udp.ip.ident = 0xdeee; //Calculate UDP and IP checksums coapMsg->coapHdr->udp.chksum = 0; coapMsg->coapHdr->udp.ip.chksum = 0; uint16_t pseudochksum = (uint16_t) chksum(UDPPROTOCOL + len - sizeof(IPhdr), coapMsg->coapHdr->udp.ip.source, sizeof(deviceIP) * 2); uint16_t chk1, chk2; chk1 = ~(chksum(pseudochksum, packet + sizeof(IPhdr), len - sizeof(IPhdr))); coapMsg->coapHdr->udp.chksum = (uint16_t) HTONS(chk1); chk2 = ~(chksum(0, packet + sizeof(EtherNetII), sizeof(IPhdr) - sizeof(EtherNetII))); coapMsg->coapHdr->udp.ip.chksum = (uint16_t) HTONS(chk2); //Send packet enc28j60_send_packet(packet, len); usartSendString("\rCoAP message sent OK\r"); }