stm32duino / STM32Ethernet

Arduino library to support Ethernet for STM32 based board
151 stars 41 forks source link

[Question]Can i use lwIP socket-API #26

Closed nopnop2002 closed 5 years ago

nopnop2002 commented 5 years ago

I tried to use lwIP socket-API. My code is:

#include <LwIP.h>
#include <STM32Ethernet.h>

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Start Ethernet
  Serial.println("Start Ethernet.begin");
  if (Ethernet.begin() == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for (;;)
      ;
  }
  Serial.println("Success to configure Ethernet using DHCP");

  Serial.print("localIP: ");
  Serial.println(Ethernet.localIP());
  Serial.print("subnetMask: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("gatewayIP: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("dnsServerIP: ");
  Serial.println(Ethernet.dnsServerIP());

  /* set up address to sendto */
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(9876);
  addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); /* send message to 255.255.255.255 */

  /* create the socket */
  int fd;
  fd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP ); // Create a UDP socket.
  LWIP_ASSERT("fd >= 0", fd >= 0);

}

void loop() {
  // Not used.
}

But compile error occure. Can i use lwIP socket-API.

\\LANDISK-HDL-AA1\disk1\Arduino\STM32F407\Ethernet_Socket\Ethernet_Socket.ino: In function 'void setup()':
Ethernet_Socket:32:22: error: aggregate 'setup()::sockaddr_in addr' has incomplete type and cannot be defined
   struct sockaddr_in addr;
                      ^~~~
Ethernet_Socket:34:21: error: 'AF_INET' was not declared in this scope
   addr.sin_family = AF_INET;
                     ^~~~~~~
\\LANDISK-HDL-AA1\disk1\Arduino\STM32F407\Ethernet_Socket\Ethernet_Socket.ino:34:21: note: suggested alternative: 'TF_INFR'
   addr.sin_family = AF_INET;
                     ^~~~~~~
                     TF_INFR
In file included from \\LANDISK-HDL-AA1\disk1\Arduino\libraries\LwIP-master\src/lwip/ip_addr.h:41,
                 from \\LANDISK-HDL-AA1\disk1\Arduino\libraries\STM32Ethernet-master\src/utility/stm32_eth.h:47,
                 from \\LANDISK-HDL-AA1\disk1\Arduino\libraries\STM32Ethernet-master\src/EthernetClient.h:7,
                 from \\LANDISK-HDL-AA1\disk1\Arduino\libraries\STM32Ethernet-master\src/STM32Ethernet.h:6,
                 from \\LANDISK-HDL-AA1\disk1\Arduino\STM32F407\Ethernet_Socket\Ethernet_Socket.ino:2:
Ethernet_Socket:36:32: error: 'INADDR_BROADCAST' was not declared in this scope
   addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); /* send message to 255.255.255.255 */
                                ^~~~~~~~~~~~~~~~
\\LANDISK-HDL-AA1\disk1\Arduino\libraries\LwIP-master\src/lwip/def.h:110:29: note: in definition of macro 'htonl'
 #define htonl(x) lwip_htonl(x)
                             ^
\\LANDISK-HDL-AA1\disk1\Arduino\STM32F407\Ethernet_Socket\Ethernet_Socket.ino:36:32: note: suggested alternative: 'IPADDR_BROADCAST'
   addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); /* send message to 255.255.255.255 */
                                ^~~~~~~~~~~~~~~~
\\LANDISK-HDL-AA1\disk1\Arduino\libraries\LwIP-master\src/lwip/def.h:110:29: note: in definition of macro 'htonl'
 #define htonl(x) lwip_htonl(x)
                             ^
Ethernet_Socket:40:29: error: 'SOCK_DGRAM' was not declared in this scope
   fd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP ); // Create a UDP socket.
                             ^~~~~~~~~~
Ethernet_Socket:40:41: error: 'IPPROTO_UDP' was not declared in this scope
   fd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP ); // Create a UDP socket.
                                         ^~~~~~~~~~~
\\LANDISK-HDL-AA1\disk1\Arduino\STM32F407\Ethernet_Socket\Ethernet_Socket.ino:40:41: note: suggested alternative: 'IP_PROTO_UDP'
   fd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP ); // Create a UDP socket.
                                         ^~~~~~~~~~~
                                         IP_PROTO_UDP
Ethernet_Socket:40:8: error: 'lwip_socket' was not declared in this scope
   fd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP ); // Create a UDP socket.
        ^~~~~~~~~~~
\\LANDISK-HDL-AA1\disk1\Arduino\STM32F407\Ethernet_Socket\Ethernet_Socket.ino:40:8: note: suggested alternative: 'lwip_strnstr'
   fd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP ); // Create a UDP socket.
        ^~~~~~~~~~~
        lwip_strnstr
exit status 1
fpistm commented 5 years ago

Hi @nopnop2002 I never use it.

It requires to modify the lwip opt: #define LWIP_SOCKET 1

Then you will have to include the required header for your needs: ex: #include "lwip/sockets.h"

but this leads to several other errors. This required to perform a proper lwip opt file. But it is up to you to perform this as we do not support this.