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.

Socket.cpp 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/Socket.h"
  19. #include <cstring>
  20. using std::memset;
  21. Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
  22. }
  23. void Socket::set_blocking(bool blocking, unsigned int timeout) {
  24. _blocking = blocking;
  25. _timeout = timeout;
  26. }
  27. int Socket::init_socket(int type) {
  28. if (_sock_fd != -1)
  29. return -1;
  30. int fd = lwip_socket(AF_INET, type, 0);
  31. if (fd < 0)
  32. return -1;
  33. _sock_fd = fd;
  34. return 0;
  35. }
  36. int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
  37. return lwip_setsockopt(_sock_fd, level, optname, optval, optlen);
  38. }
  39. int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
  40. return lwip_getsockopt(_sock_fd, level, optname, optval, optlen);
  41. }
  42. int Socket::select(struct timeval *timeout, bool read, bool write) {
  43. fd_set fdSet;
  44. FD_ZERO(&fdSet);
  45. FD_SET(_sock_fd, &fdSet);
  46. fd_set* readset = (read ) ? (&fdSet) : (NULL);
  47. fd_set* writeset = (write) ? (&fdSet) : (NULL);
  48. int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
  49. return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
  50. }
  51. int Socket::wait_readable(TimeInterval& timeout) {
  52. return select(&timeout._time, true, false);
  53. }
  54. int Socket::wait_writable(TimeInterval& timeout) {
  55. return select(&timeout._time, false, true);
  56. }
  57. int Socket::close(bool shutdown) {
  58. if (_sock_fd < 0)
  59. return -1;
  60. if (shutdown)
  61. lwip_shutdown(_sock_fd, SHUT_RDWR);
  62. lwip_close(_sock_fd);
  63. _sock_fd = -1;
  64. return 0;
  65. }
  66. Socket::~Socket() {
  67. close(); //Don't want to leak
  68. }
  69. TimeInterval::TimeInterval(unsigned int ms) {
  70. _time.tv_sec = ms / 1000;
  71. _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
  72. }