bosko lekovic / Mbed OS testEth01
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 #define WADDR
00005 
00006 #ifdef WADDR
00007 #define IP_ADDR   "169.254.65.200"
00008 #define NET_MASK  "255.255.0.0"
00009 #define GATW_ADDR "169.254.65.1"
00010 #define IP_ADDR_REMOTE "169.254.65.199"
00011 #else
00012 #define IP_ADDR   "192.168.1.10"
00013 #define NET_MASK  "255.255.255.0"
00014 #define GATW_ADDR "192.168.1.1"
00015 #define IP_ADDR_R "192.168.1.9"
00016 #endif
00017 
00018 
00019 
00020 
00021 EthernetInterface eth;
00022 nsapi_error_t err;
00023 
00024 
00025 int main(void)
00026 {
00027   //  if( (err = eth.disconnect()) != NSAPI_ERROR_OK )  { printf(" greska diskonekcije %d \n\r", err); return 1; } 
00028   //  ((NetworkInterface *)&eth)->set_network("192.168.1.11","255.255.255.0","192.168.1.1");
00029  
00030     if( (err = eth.set_network(IP_ADDR,NET_MASK,GATW_ADDR))!= NSAPI_ERROR_OK )  { printf(" greska setovanja mreze %d \n\r", err); return 1; } 
00031     
00032     
00033      if( (err = eth.connect())  != NSAPI_ERROR_OK )  { printf(" greska konekcije %d \n\r", err); return 1; }
00034     
00035     const char *ip = eth.get_ip_address() ;
00036     
00037     
00038     printf( "MAC adresa: %s\n\r", eth.get_mac_address() );
00039     printf( "IP adresa:  %s\n\r", ip);
00040     printf( "net mask:   %s\n\r", eth.get_netmask() );
00041     printf( "GW adresa:  %s\n\r", eth.get_gateway() );
00042     
00043     
00044     /*
00045     TCPSocket socket;        
00046     socket.open(&eth);
00047     socket.connect(
00048     */
00049     
00050     UDPSocket udpSocket;
00051     if( (err = udpSocket.open( &eth ))  != NSAPI_ERROR_OK )  { printf(" greska otvaranja soketa %d \n\r", err);  return 1; }
00052     udpSocket.bind(2000);
00053     
00054     char buff[]="text poruke";
00055     char inbuff[100];
00056     printf("POCINJE\r\n");
00057     int i=0;
00058     while(1)
00059    {
00060     printf("petlja\r\n");
00061     SocketAddress sockAddr;
00062  
00063 printf("cekam prijem \n\r");
00064 int n = udpSocket.recvfrom(&sockAddr, &inbuff, sizeof(inbuff));
00065 printf(" primljeno = %d bajtova \n\r",n);
00066 for(i=0;i<n;i++)  printf(" %c ", inbuff[i] );
00067 
00068     if(0 > udpSocket.sendto(IP_ADDR_REMOTE, sockAddr.get_port(), buff, sizeof(buff))) 
00069      {
00070         printf("Error sending data\n");
00071         return -1;
00072     }
00073     else printf("super slanje %s  na adresu %s , port %d\r\n",buff,sockAddr.get_ip_address(), sockAddr.get_port());
00074     
00075     wait(1);
00076     } 
00077 }
00078 
00079 //
00080 //enum nsapi_error {
00081 //    NSAPI_ERROR_OK                  =  0,        /*!< no error */
00082 //    NSAPI_ERROR_WOULD_BLOCK         = -3001,     /*!< no data is not available but call is non-blocking */
00083 //    NSAPI_ERROR_UNSUPPORTED         = -3002,     /*!< unsupported functionality */
00084 //    NSAPI_ERROR_PARAMETER           = -3003,     /*!< invalid configuration */
00085 //    NSAPI_ERROR_NO_CONNECTION       = -3004,     /*!< not connected to a network */
00086 //    NSAPI_ERROR_NO_SOCKET           = -3005,     /*!< socket not available for use */
00087 //    NSAPI_ERROR_NO_ADDRESS          = -3006,     /*!< IP address is not known */
00088 //    NSAPI_ERROR_NO_MEMORY           = -3007,     /*!< memory resource not available */
00089 //    NSAPI_ERROR_NO_SSID             = -3008,     /*!< ssid not found */
00090 //    NSAPI_ERROR_DNS_FAILURE         = -3009,     /*!< DNS failed to complete successfully */
00091 //    NSAPI_ERROR_DHCP_FAILURE        = -3010,     /*!< DHCP failed to complete successfully */
00092 //    NSAPI_ERROR_AUTH_FAILURE        = -3011,     /*!< connection to access point failed */
00093 //    NSAPI_ERROR_DEVICE_ERROR        = -3012,     /*!< failure interfacing with the network processor */
00094 //    NSAPI_ERROR_IN_PROGRESS         = -3013,     /*!< operation (eg connect) in progress */
00095 //    NSAPI_ERROR_ALREADY             = -3014,     /*!< operation (eg connect) already in progress */
00096 //    NSAPI_ERROR_IS_CONNECTED        = -3015,     /*!< socket is already connected */
00097 //    NSAPI_ERROR_CONNECTION_LOST     = -3016,     /*!< connection lost */
00098 //    NSAPI_ERROR_CONNECTION_TIMEOUT  = -3017,     /*!< connection timed out */
00099 //};