uint8_t vNet_RetrieveData_M1(uint8_t *data) { uint8_t *data_pnt; data_pnt=data; if(!recvfrom(UDP_SOCK, data, dataframe.len, dataframe.ip, (uint16_t*)(&dataframe.port))) return ETH_FAIL; // Verify the incoming address, is a not conventional procedure at this layer // but is required to record the IP address in case of User Mode addresses (0x0100 - 0x64FF) #if(UMODE_ENABLE) uint16_t umrec = ((*(U16*)&data_pnt[5]) & 0xFF00); if((umrec != 0x0000) && (umrec <= VNET_ADDR_H_M1)) UserMode_Record((*(U16*)&data_pnt[5]), dataframe.ip, (uint8_t *)(&dataframe.port)); #endif // If the addressing server is used, get the subnet from the first broadcast // message received. This trick is due to the missing DHCP support in vNet/IP // and is supposed to work in most, but not all cases (fixed address will be needed // in case of failure) #if(UMODE_ENABLE && DYNAMICADDRESSING) if(addrsrv == true) { // Use the incoming IP address to get the subnet to use, we are not changing // the part of the IP address related to the vNet address uint8_t i; for(i=0;i<4;i++) { // Remove the subnet bits from the actual address stack.base_ip[i] &= ~stack.subnetmask[i]; stack.ip[i] &= ~stack.subnetmask[i]; stack.gateway[i] &= ~stack.subnetmask[i]; // Get the subnet bits from the incoming adress stack.base_ip[i] |= (dataframe.ip[i] & stack.subnetmask[i]); stack.ip[i] |= (dataframe.ip[i] & stack.subnetmask[i]); stack.gateway[i] |= (dataframe.ip[i] & stack.subnetmask[i]); } // Reset the IP drivers W5x00.setIPAddress(stack.ip); W5x00.setGatewayIp(stack.gateway); W5x00.setSubnetMask(stack.subnetmask); vNet_Begin_M1(UDP_SOCK); // Start listen on socket addrsrv = false; } #endif // Remove the header data_pnt++; dataframe.len--; memmove(data, data_pnt, dataframe.len); return dataframe.len; }
uint8_t vNet_Send_M1(uint16_t addr, oFrame *frame, uint8_t len) { uint8_t ip_addr[4]; uint16_t vNet_port; // Define the standard vNet port vNet_port = ETH_PORT; // Define the IP address to be used if((addr == 0xFFFF) || ((addr > VNET_ADDR_L_M3) && (addr < VNET_ADDR_H_M3))) { // Set the IP broadcast address for(U8 i=0;i<4;i++) ip_addr[i]=0xFF; } else { // Verify the User Mode #if(UMODE_ENABLE) if ((addr & 0xFF00) != 0x0000) { // The first byte is the User Mode Index, if in range 0x01 - 0x64 // a standard client/server connection is used with the user interface // this give routing and NATting passthrough UserMode_Get(addr, &ip_addr[0], (uint8_t*)(&vNet_port)); } else #endif eth_vNettoIP(addr, &ip_addr[0]); // Get the IP address } // Build a frame with len of payload as first byte vNetM1_header = len+1; oFrame_Define(&vNetM1_oFrame); oFrame_Set(&vNetM1_header, 0, 1, 0, frame); // Send data if(!sendto(UDP_SOCK, (uint8_t*)&vNetM1_oFrame, 0, &ip_addr[0], vNet_port)) { oFrame_Reset(); // Free the frame // Restart the socket vNet_Stop_M1(UDP_SOCK); vNet_Begin_M1(UDP_SOCK); return ETH_FAIL; // If data sent fail, return } // At this stage data are processed or socket is failed, so we can // securely reset the oFrame oFrame_Reset(); return ETH_SUCCESS; }
void vNet_SetAddress_M1(uint16_t addr) { uint8_t ip_addr[4], mac_addr[6]; // Translate and set the address eth_vNettoIP(addr, &ip_addr[0]); eth_SetIPAddress(&ip_addr[0]); // Set the MAC Address #if(AUTO_MAC) eth_vNettoMAC(addr, mac_addr); W5x00.setMACAddress(mac_addr); #else W5x00.setMACAddress((uint8_t*)MAC_ADDRESS); #endif // Set the IP W5x00.setIPAddress(stack.ip); W5x00.setGatewayIp(stack.gateway); W5x00.setSubnetMask(stack.subnetmask); vNet_Begin_M1(UDP_SOCK); // Start listen on socket // Include debug functionalities, if required #if(VNET_DEBUG) uint8_t addrval[6]; // Print MAC address W5x00.getMACAddress(addrval); VNET_LOG("(MAC)<"); for(U8 i=0; i<6; i++) { VNET_LOG(addrval[i],HEX); VNET_LOG(","); } VNET_LOG(">\r\n"); // Print IP address W5x00.getIPAddress(addrval); VNET_LOG("(IP)<"); for(U8 i=0; i<4; i++) { VNET_LOG(addrval[i],HEX); VNET_LOG(","); } VNET_LOG(">\r\n"); #endif }
void vNet_SetAddress_M1(uint16_t addr) { uint8_t ip_addr[4]; // Translate and set the address eth_vNettoIP(addr, &ip_addr[0]); eth_SetIPAddress(&ip_addr[0]); // Get the MAC Address from the Wifi controller and set it // into the uIP stack U8* mac_addr_hw = zg_get_mac(); memcpy(mac_addr, mac_addr_hw, 6); vNet_Begin_M1(0); // Start listen on socket }