MS_uC / fue1 / V11 2- 1
ENET with MAC controller
Programming MicrocontrollerENET (Ethernet) peripheral with
MAC (Media Access Control) controller
32K Byte 32K Byte Burst FlashBurst Flash
64K or 96K 64K or 96K Byte SRAM Byte SRAM
256K or 512K 256K or 512K Byte Burst Byte Burst
FlashFlash
OTP OTP MemMem
UARTUARTI2CI2CSPISPITIMTIMRTCRTC
EXT. EXT. Bus Bus
GPIOGPIO
USB USB 2.0FS 2.0FS
CAN CAN 2.0B 2.0B
Enet Enet MAC MAC
PFPFQ Q
BC BC
DMADMA
INTINTR R
CntlCntl
ARM96ARM966E 6E CORE CORE
w/DSPw/DSP96 MHz 96 MHz
CLK CLK CntlCntl
ADCADC
LVD LVD BODBOD
PLLPLL JTAJTAGG
ETMETM99
MS_uC / fue1 / V11 21.04.23
2
The classical web applications
Standard way of web communication Browser as web client Web server with data base
The purpose of web application Represent the data of the server on the client Save the input of the client on the server
Example of web applications Query the SBB timetable Buy books Download information about a company
MS_uC / fue1 / V11 21.04.23
3
Static web pages
Static web pages are displayed without further processing HTML, XHTML, XML CSS Plug-Ins (Multimedia)
Client Server
Web-
Browser
Web- Server
XHTML -File
1)
2)
HTTP
MS_uC / fue1 / V11 21.04.23
4
Dynamic web pages
Client-side dynamic execution Script languages (JavaScript) Execution of Programs (Applet)
Server-side dynamic execution Script languages (Perl & PHP) Execution of Programs (Servlet)
MS_uC / fue1 / V11 21.04.23
5
Client-side dynamic execution
Typical applications Context depending menus, input validation & calculation with
formula Client Server
Web-
Browser
Web
Server XHTML-File with
Client-Skript or Program
1)
2) HTTP
3)
4)
5)
Execution Script/
Program
MS_uC / fue1 / V11 21.04.23
6
Server-side dynamic execution
Typical applications Creation of individual user web pages
Client Server
Web-
Browser
Web- Server
Server-Script or
Program
1)
6) HTTP
4)
5)
Data-base
3)
2)
XHTML-File Execution
Script or Program
MS_uC / fue1 / V11 21.04.23
7
Programming languages for Internet
Script languages Client-side script languages
JavaScript, VBSkript & Jscript
Server-side script languages Perl & PHP
High-level languages C, C++, C#, Visual Basic or Java
MS_uC / fue1 / V11 21.04.23
8
Comparison between Script & High-level Languages
Script programming language
High-level programming language
Taskclient Small help programs, plug-in
server dynamic generation of web pages
ExecutionScript are executed by an interpreter
Compiler generates a byte (intermediate) or an executable code
Learning time Small until medium Medium until high
Development environment
Text editor or script-tools Development environment: JDK or Visual Studio
Integration into XHTML pages
Integration directly into XHTML pages
XHTML page contains the reference for the plug-in
Security
client
Limited access to hard disk, memory & LAN for JavaScript.
Full access with VBScript
No direct access to hard disk, memory & LAN interface for Java applets.
serverFull access to databases, I / O system, etc
Full access to databases, I / O system, etc.
MS_uC / fue1 / V11 21.04.23
9
Languages & technologies for web site development
sun Standard Microsoft
Client
Static
HTML, XHTML & CSS
XML
DynamicJavaScript
Java Applet
VBScript
Jscript JavaScript
.net
Server
JSP(Java Server Page)
Java Servlet
CGI / Perl PHP
(Pers. Homepage)
More complicateprogram
(C/C++ etc.)
ASP(Active Server
Page)
.net
MS_uC / fue1 / V11 21.04.23
10
Embedded web server
Application examples for the embedded word Machines, automation etc. which should be accessible
through Internet
Purpose of the “Embedded web server” Remote maintenance (diagnostic, setting the
parameters) Alarming Configuration
MS_uC / fue1 / V11 21.04.23
11
ISO/OSI Model for the Client-Server communication
Layer 1
Layer 2
IP
TCP
HTTP
Port 80
Client / Browser Embedded Webserver
Port >1024
Layer 1
Layer 2
IP
TCP
HTTP
Client- App. 2
Client- App. 1
CGI
Port 80 Port >1024
CGI
Server- App. 1
Server- App. 2
Transport
Network
Data link
Physical
MS_uC / fue1 / V11 21.04.23
12
Principles of the Client-Server communication
Client Embedded Webserver
Web-
Browser
Micro-
Controller XHTML-
File with the Java-Applet
fort the client in memory
2) 3) HTTP
4)
5)
1) 6)
Java
Applet
C-Program
Technical process Inputs / Outputs
Socket-Connection
MS_uC / fue1 / V11 21.04.23
13
Socket (Buchse / prise)
Socket is an endpoint for communication between two machines IP Address Protocol (TCP or UDP) Port number
General procedure for exchanging data Build a connection using two sockets Exchange of data Close the connection
MS_uC / fue1 / V11 21.04.23
14
TCP based application
TCP is a connection-oriented transport protocol Transmission Control Protocol
Creating the TCP connection First, start the server Next the client must connect himself to the server Finally the data can be exchanged in both directions
MS_uC / fue1 / V11 21.04.23
15
MS_uC / fue1 / V11 21.04.23
16
socket()
socket() creates a TCP or UDP socket Server Socket SS on the server Client Socket CS on the client
Specification of the used protocol (TCP or UDP)
MS_uC / fue1 / V11 21.04.23
17
bind()
bind() binds the socket with a local protocol address IP address & port number
bind() is only uses by the server
MS_uC / fue1 / V11 21.04.23
18
listen()
listen() sets the server in a listening mode
Maximum number of connections that will be processed simultaneously
MS_uC / fue1 / V11 21.04.23
19
accept()
accept() enables the Server to accept a connection from a client Full queue (backlog)
Next connection request from the queue
Empty queue New connection request from a client
MS_uC / fue1 / V11 21.04.23
20
connect()
connect() enables the client to build a connection with the server
Server socket creates a new socket (newSS) Dedicated line to the requested client
MS_uC / fue1 / V11 21.04.23
21
recv() and send()
The data are exchanged in both direction with recv() & send()
MS_uC / fue1 / V11 21.04.23
22
close()
After successful communication The sockets newSS & CS must be closed
MS_uC / fue1 / V11 21.04.23
23
UDP-based applications UDP is minimal and non connection-oriented transport
protocol User Datagram Protocol
MS_uC / fue1 / V11 21.04.23
24
Socket programming in C
Programming of client and server applications in C Application Programming Interface (API) functions of the TCP/IP-
Stacks
MS_uC / fue1 / V11 21.04.23
25
Data structure for the protocol addresses (1)
Common data structure struct sockaddr {
unsigned short sa_family // Address family (AF_INET)
unsigned sa_data[14]; /* Protocol-specific
address information */
}
Data structure for AF_INET (Address Family Interactive Network System) struct sockaddr_in {
unsigend short sin_family // Protocol family (PF_INET)
unsigned short sin_port; // Address port (16 bits)
struct in_addr sin_addr; // IP address (32 bits)
}
struct in_addr {
NET_IP_ADDR s_addr; // IP address (32 bits)
}
MS_uC / fue1 / V11 21.04.23
26
Data structure for the protocol addresses (2)
MS_uC / fue1 / V11 21.04.23
27
Help function
Transmission of sockaddr & sockaddr_in in big endian Network byte order
Conversion from the “host byte order” into “network byte order” uint32_t htonl (uint32_t hostlong); uint16_t htons (uint32_t hostshort);
MS_uC / fue1 / V11 21.04.23
28
socket()
Prototype int socket (int domain, int type, int protocol);
Argument domain
Application domain of the socket (PF_INET) type
Behavior of the socket (SOCK_STREAM or SOCK_DGRAM) protocol
IPPROTO_TCP or IPPROTO_UDP
Result Success: Socket-ID Error: -1
MS_uC / fue1 / V11 21.04.23
29
bind()
Prototype int bind (int sock_id, struct sockaddr *addr, int addrlen);
Argument sock_id
Result of socket() addr
Information about Port number & IP address addrlen
Size of addr
Result Success: 0 Error: -1
MS_uC / fue1 / V11 21.04.23
30
listen()
Prototype int listen (int sock_id, int backlog);
Argument sock_id
Result of socket() backlog
Maximum waiting clients
Result Success: 0 Error: -1
MS_uC / fue1 / V11 21.04.23
31
connect()
Prototype int connect (int sock_id, struct sockaddr *addr, int addrlen);
Argument sock_id
Result of socket() addr
Information about Port number & IP address of the server to connect
addrlen Size of addr
Result Success: 0 Error: -1
MS_uC / fue1 / V11 21.04.23
32
accept()
Prototype int accept (int sock_id, struct sockaddr *addr_remote, int *addrlen_remote);
Argument sock_id
Result of socket() addr_remote
Information about Port number & IP address of the client addrlen_remote
Size of addr_remote
Result Success: new socket ID Error: -1
MS_uC / fue1 / V11 21.04.23
33
recv()
Prototype int recv (int newSock_id, void *buf, int *buf_len, int flags);
Argument newSock_id or sock_id
Result of accept() on server side or socket() on client side buf
Buffer for the received data buf_len
Length of buf flags
Receive option: can be set to zero
Result Success: Number of received bytes Error: -1
MS_uC / fue1 / V11 21.04.23
34
send()
Prototype int send (int newSock_id, void *data, int datalen, int flags);
Argument newSock_id or sock_id
Result of accept() on server side or socket() on client side data
The data to transmit datalen
The length of the data to transmit flags
Transmit option: can be set to zero
Result Success: Number of transmitted bytes Error: -1
MS_uC / fue1 / V11 21.04.23
35
Programming in Java
Class Socket Socket (String host, int port);
Stream Classes for sending or receiving the data BufferedReader InputStreamReader (ASCII unicode) InputStream (ASCII)
PrintStream (unicode ASCII) OutputStream (ASCII)
MS_uC / fue1 / V11 21.04.23
36
Generating a client socket in Java
public void openSocket(){ try{ // replace parameters with real values sock = new Socket(Server_Address, Port); // Send without delay sock.setTcpNoDelay(true); inputStream = sock.getInputStream(); bufferedReader = new BufferedReader new InputStreamReader(inStream)); outputStream = sock.getOutputStream(); printStream = new PrintStream(outStream, true); } catch (SocketException e){ // error handling } catch (IOException e){ // error handling }}
MS_uC / fue1 / V11 21.04.23
37
Closing the socket and the data stream
public void closeSocket(){
try {
inputStream.close();
outputStream.close();
sock.close();
}
catch (IOException e){
// error handling
}
}
MS_uC / fue1 / V11 21.04.23
38
Transmitting & Receiving the data
public void sendData(String data){ printStream.print(data);}
public void getData(){ String data; try { data = bufferedReader.readLine(); // process data } catch (IOException e){ // error handling }}