multicast.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "multicast.h"
  2. #include <stdio.h>
  3. #include "socket.h"
  4. #include "wizchip_conf.h"
  5. int32_t multicast_loopback(uint8_t sn, uint8_t* buf, uint8_t* multicast_ip, uint16_t multicast_port)
  6. {
  7. int32_t ret;
  8. uint16_t size, sentsize;
  9. uint8_t destip[4];
  10. uint16_t destport, port=3000;
  11. switch(getSn_SR(sn))
  12. {
  13. case SOCK_UDP :
  14. if((size = getSn_RX_RSR(sn)) > 0)
  15. {
  16. if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
  17. ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
  18. if(ret <= 0)
  19. {
  20. #ifdef _MULTICAST_DEBUG_
  21. printf("%d: recvfrom error. %ld\r\n",sn,ret);
  22. #endif
  23. return ret;
  24. }
  25. size = (uint16_t) ret;
  26. sentsize = 0;
  27. while(sentsize != size)
  28. {
  29. ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport);
  30. if(ret < 0)
  31. {
  32. #ifdef _MULTICAST_DEBUG_
  33. printf("%d: sendto error. %ld\r\n",sn,ret);
  34. #endif
  35. return ret;
  36. }
  37. sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
  38. }
  39. }
  40. break;
  41. case SOCK_CLOSED:
  42. #ifdef _MULTICAST_DEBUG_
  43. printf("%d:Multicast Loopback start\r\n",sn);
  44. #endif
  45. setSn_DIPR(0, multicast_ip);
  46. setSn_DPORT(0, multicast_port);
  47. if((ret = socket(sn, Sn_MR_UDP, port, Sn_MR_MULTI)) != sn)
  48. return ret;
  49. #ifdef _MULTICAST_DEBUG_
  50. printf("%d:Opened, UDP Multicast Socket\r\n", sn);
  51. printf("%d:Multicast Group IP - %d.%d.%d.%d\r\n", sn, multicast_ip[0], multicast_ip[1], multicast_ip[2], multicast_ip[3]);
  52. printf("%d:Multicast Group Port - %d\r\n", sn, multicast_port);
  53. #endif
  54. break;
  55. default :
  56. break;
  57. }
  58. return 1;
  59. }
  60. int32_t multicast_recv(uint8_t sn, uint8_t* buf, uint8_t* multicast_ip, uint16_t multicast_port)
  61. {
  62. int32_t ret;
  63. uint16_t size, port=3000;
  64. uint8_t destip[4];
  65. uint16_t destport;
  66. switch(getSn_SR(sn))
  67. {
  68. case SOCK_UDP :
  69. if((size = getSn_RX_RSR(sn)) > 0)
  70. {
  71. if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
  72. ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
  73. if(ret <= 0)
  74. {
  75. #ifdef _MULTICAST_DEBUG_
  76. printf("%d: recvfrom error. %ld\r\n",sn,ret);
  77. #endif
  78. return ret;
  79. }
  80. size = (uint16_t) ret;
  81. #ifdef _MULTICAST_DEBUG_
  82. printf("\r\nrecv size : %d\r\n", size);
  83. for(int i=0; i<size; i++)
  84. printf("%c", buf[i]);
  85. printf("\r\n");
  86. #endif
  87. }
  88. break;
  89. case SOCK_CLOSED:
  90. #ifdef _MULTICAST_DEBUG_
  91. printf("%d:Multicast Recv start\r\n",sn);
  92. #endif
  93. setSn_DIPR(sn, multicast_ip);
  94. setSn_DPORT(sn, multicast_port);
  95. if((ret = socket(sn, Sn_MR_UDP, port, Sn_MR_MULTI)) != sn)
  96. return ret;
  97. #ifdef _MULTICAST_DEBUG_
  98. printf("%d:Opened, UDP Multicast Socket\r\n", sn);
  99. printf("%d:Multicast Group IP - %d.%d.%d.%d\r\n", sn, multicast_ip[0], multicast_ip[1], multicast_ip[2], multicast_ip[3]);
  100. printf("%d:Multicast Group Port - %d\r\n", sn, multicast_port);
  101. #endif
  102. break;
  103. default :
  104. break;
  105. }
  106. return 1;
  107. }