Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UDPSocket.cpp 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright (C) 2012 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without restriction,
  5. * including without limitation the rights to use, copy, modify, merge, publish, distribute,
  6. * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  7. * furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #include "Socket/UDPSocket.h"
  19. #include <cstring>
  20. using std::memset;
  21. UDPSocket::UDPSocket() {
  22. }
  23. int UDPSocket::init(void) {
  24. return init_socket(SOCK_DGRAM);
  25. }
  26. // Server initialization
  27. int UDPSocket::bind(int port) {
  28. if (init_socket(SOCK_DGRAM) < 0)
  29. return -1;
  30. struct sockaddr_in localHost;
  31. std::memset(&localHost, 0, sizeof(localHost));
  32. localHost.sin_family = AF_INET;
  33. localHost.sin_port = htons(port);
  34. localHost.sin_addr.s_addr = INADDR_ANY;
  35. if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
  36. close();
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. int UDPSocket::join_multicast_group(const char* address) {
  42. struct ip_mreq mreq;
  43. // Set up group address
  44. mreq.imr_multiaddr.s_addr = inet_addr(address);
  45. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  46. return set_option(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
  47. }
  48. int UDPSocket::set_broadcasting(bool broadcast) {
  49. int option = (broadcast) ? (1) : (0);
  50. return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option));
  51. }
  52. // -1 if unsuccessful, else number of bytes written
  53. int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
  54. if (_sock_fd < 0)
  55. return -1;
  56. if (!_blocking) {
  57. TimeInterval timeout(_timeout);
  58. if (wait_writable(timeout) != 0)
  59. return 0;
  60. }
  61. return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost));
  62. }
  63. // -1 if unsuccessful, else number of bytes received
  64. int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
  65. if (_sock_fd < 0)
  66. return -1;
  67. if (!_blocking) {
  68. TimeInterval timeout(_timeout);
  69. if (wait_readable(timeout) != 0)
  70. return 0;
  71. }
  72. remote.reset_address();
  73. socklen_t remoteHostLen = sizeof(remote._remoteHost);
  74. return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
  75. }