📄 udp.c
字号:
//=======================================================================
// Phillip Hoang
// Arun Gupta
// May 31, 2001
//=======================================================================
// Version 1
// This code is a complete working version.
// It sets up the UDP/IP/Ethernet Headers.
// It also has the LCD and temperature components.
// There is an while(1) loop that cycles through
// reading the temperature, displaying it on the LCD,
// and then sending the UDP/IP/Ethernet packet.
// The Ethernet address is the MAC address.
//=======================================================================
// For this version, the relavent headers are of the following:
// - Ethernet
// Source: Doesn't matter, we took the one of my laptop
// Destination: The MAC address of the router in the room
// Embedded Systems Lab: 00-00-1d-bc-79-d7
// - IP
// Source: Doesn't matter, we used an IP of one of the lab computers
// Destination: 138.23.169.9
// - UDP
// Source Port: 47098 (I don't know if this is arbitrary, gotten from
// packet sniffing)
// Destination Port: 4950 (this is the port used in the listener program)
//
// NOTE: For generation the header information for the UDP/IP/Header, we
// made a UDP socket program that sent a UDP packet. A packet sniffer
// was used to get the corresponding values of the header. From then
// we modified it accordingly so that we could transmit only our
// needed information.
//=======================================================================
#pragma SMALL DB OE
#include <reg51.h>
#include "io.h"
#include "tmp_rec.h"
//=======================================================================
#define addr P2
#define data P1
/*
7 6 5 4 3 2 1 0
+-----+-----+-----+-----+----+----+----+----+
addr | N/A | N/A | RST | AEN | A3 | A2 | A1 | A0 |
+-----+-----+-----+-----+----+----+----+----+
*/
//=======================================================================
#define RxTxData 0x00 // Receive/Transmit data (port 0)
#define RxTxData1 0x02 // Receive/Transmit data (port 1)
#define TxCmd 0x04 // Transmit Command
#define TxLength 0x06 // Transmit Length
#define ISQ 0x08 // Interrupt status queue
#define PPPtr 0x0a // PacketPage pointer
#define PPData 0x0c // PacketPage data (port 0)
#define PPData1 0x0e // PacketPage data (port 1)
//=======================================================================
sbit write = P3^6;
sbit read = P3^7;
sbit aen = P2^4;
sbit rst = P2^5;
//=======================================================================
// Used to Read from the Data Bus
unsigned char ioRead(unsigned char address)
{
unsigned char value;
addr = (address&0x0f);
aen = 1;
aen = 0;
read = 0;
value = data;
read = 1;
return value;
}
// Used to write to the Data Bus
void ioWrite(unsigned char address, unsigned char value)
{
data = value;
addr = (address&0x0f);
aen = 1;
aen = 0;
write = 0;
write = 1;
}
//=======================================================================
int main (void)
{
int i;
unsigned char BusST0, BusST1;
unsigned char temperature;
InitIO();
ClearScreen();
////////////////////////////////////////////////////////////
// Initialize Registers
////////////////////////////////////////////////////////////
// Configure RxCTL fo Promiscious mode, RxOK
// (1) Write 0x0104 to PacketPage Pointer
// (2) Write 0x0180 to PacketPage Data Port
ioWrite(PPPtr, 0x04);
ioWrite(PPPtr + 1, 0x01);
ioWrite(PPData, 0x80);
ioWrite(PPData + 1, 0x01);
// Set 10BaseT, SerRxOn, SerTxOn in LineCTL
// (1) Write 0x0112 to PacketPage Pointer
// (2) Write 0x00c0 to PacketPage Data Port
ioWrite(PPPtr, 0x12);
ioWrite(PPPtr + 1, 0x01);
ioWrite(PPData, 0xc0);
ioWrite(PPData + 1, 0x00);
////////////////////////////////////////////////////////////
// Transmit
////////////////////////////////////////////////////////////
while(1)
{
temperature = PrintTemperature();
// Send the transmit command
ioWrite(TxCmd, 0xc0);
ioWrite(TxCmd + 1, 0x00);
// 60 bytes to be sent
ioWrite(TxLength, 0x3c);
ioWrite(TxLength + 1, 0x00);
// BusST
ioWrite(PPPtr, 0x38);
ioWrite(PPPtr + 1, 0x01);
// Bid for transmit
do {
BusST0 = ioRead(PPData);
BusST1 = ioRead(PPData+1);
}while(!(BusST1==0x01));
/////////////////////////////////////
// Ethernet Header
/////////////////////////////////////
// Send Destination (6 bytes)
// MAC address of Router of Embedded Systems Lab (00-00-1d-bc-79-d7)
ioWrite(RxTxData, 0x00);
ioWrite(RxTxData+1, 0x00);
ioWrite(RxTxData, 0x1d);
ioWrite(RxTxData+1, 0xbc);
ioWrite(RxTxData, 0x79);
ioWrite(RxTxData+1, 0xd7);
// Send Source (6 bytes)
// MAC address of my laptop (00-00-86-45-b7-b2)
ioWrite(RxTxData, 0x00);
ioWrite(RxTxData+1, 0x00);
ioWrite(RxTxData, 0x86);
ioWrite(RxTxData+1, 0x45);
ioWrite(RxTxData, 0xb7);
ioWrite(RxTxData+1, 0xb2);
// Send Protocol Type (2 bytes)
// 0x0806 (ARP)
// 0x0800 (IP)
ioWrite(RxTxData, 0x08);
ioWrite(RxTxData+1, 0x00);
/////////////////////////////////////
// IP Header
/////////////////////////////////////
// Version Header Length (1 byte)
// Using IPv4
ioWrite(RxTxData, 0x45);
// Service (1 byte)
// Normally set to zero because not used
ioWrite(RxTxData+1, 0x00);
// Length (2 bytes)
// 33 byte length
ioWrite(RxTxData, 0x00);
ioWrite(RxTxData+1, 0x21);
// Identifier (2 bytes)
// 0x0000
ioWrite(RxTxData, 0x00);
ioWrite(RxTxData+1, 0x00);
// Flags Fragment offset (2 bytes)
// 0x4000
ioWrite(RxTxData, 0x40);
ioWrite(RxTxData+1, 0x00);
// Time to Live (1 byte)
// 63
ioWrite(RxTxData, 0x3f);
// Protocol (1 byte)
// ICMP (1), TCP (6), UDP (17)
// UDP 0x11 = 17
ioWrite(RxTxData+1, 0x11);
// Checksum (2 bytes)
// 0xb273
// Checksum is 0xffffh - the sum of all IP header info
ioWrite(RxTxData, 0xb2);
ioWrite(RxTxData+1, 0x73);
// Source Address (4 bytes)
// IP address 138.23.204.32 (arbitrary)
ioWrite(RxTxData, 0x8a); // 138
ioWrite(RxTxData+1, 0x17); // 23
ioWrite(RxTxData, 0xcc); // 204
ioWrite(RxTxData+1, 0x20); // 32
// Destination Address (4 bytes)
// IP address 138.23.169.9 (hill)
ioWrite(RxTxData, 0x8a); // 138
ioWrite(RxTxData+1, 0x17); // 23
ioWrite(RxTxData, 0xa9); // 169
ioWrite(RxTxData+1, 0x09); // 9
/////////////////////////////////////
// UDP Header
/////////////////////////////////////
// Source Port (2 bytes)
// Port 47098 (b7fa)
ioWrite(RxTxData, 0xb7);
ioWrite(RxTxData+1, 0xfa);
// Destination Port (2 bytes)
// Port 4950 (1356h)
ioWrite(RxTxData, 0x13);
ioWrite(RxTxData+1, 0x56);
// Message Length (2 bytes)
// 13 bytes going to be sent
ioWrite(RxTxData, 0x00);
ioWrite(RxTxData+1, 0x0d);
// Checksum (2 bytes)
// 0x0000 (no checksum used)
ioWrite(RxTxData, 0x00);
ioWrite(RxTxData+1, 0x00);
// Write Message
ioWrite(RxTxData, temperature );
ioWrite(RxTxData+1, 't');
ioWrite(RxTxData, 'e');
ioWrite(RxTxData+1, 'm');
ioWrite(RxTxData, 'p');
ioWrite(RxTxData+1, 0x00);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -