ipmsg_filerecv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2018, Real-Thread Information Technology Ltd
  3. * All rights reserved
  4. *
  5. * Copyright (c) 2006-2018, RT-Thread Development Team
  6. *
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Change Logs:
  10. * Date Author Notes
  11. * 2019-04-28 heyuanjie87 the first version
  12. */
  13. #include <rtthread.h>
  14. #include "ipmsg.h"
  15. #include "ipmsgdef.h"
  16. #include <stdio.h>
  17. #include <sys/socket.h>
  18. #include "netdb.h"
  19. #define DBG_TAG "ipmsgfr"
  20. #define DBG_LVL DBG_LOG
  21. #include <rtdbg.h>
  22. #ifndef IPMSG_FILERECV_BUFSZ
  23. #define IPMSG_FILERECV_BUFSZ 1024
  24. #endif
  25. #ifdef IPMSG_FILERECV_ENABLE
  26. static int ipmsg_getdata_req(int sk, ipmsg_filehandler_t *fh, ipmsg_fileinfo_t *fi, char *buf, int bufsz)
  27. {
  28. char *msg;
  29. char ext[24];
  30. int len;
  31. sprintf(ext, "%x:%x:0", fh->packetid, fi->id);
  32. msg = ipmsg_msg_make(fh->im, IPMSG_GETFILEDATA, ext, NULL, buf, &bufsz);
  33. len = send(sk, msg, bufsz, 0);
  34. return (len == bufsz);
  35. }
  36. static int ipmsg_data_recv(int sk, ipmsg_filehandler_t *fh, ipmsg_fileinfo_t *fi, char *buf, int bufsz)
  37. {
  38. int len;
  39. int ret = -1;
  40. if (ipmsg_sock_wait(sk, 2000) <= 0)
  41. return 0;
  42. len = recv(sk, buf, bufsz, 0);
  43. if (len > 0)
  44. {
  45. ret = fh->data(fh, buf, len);
  46. if (ret <= 0)
  47. ret = 0;
  48. else if (ret > 0)
  49. fi->pos += ret;
  50. }
  51. if (fi->size == fi->pos)
  52. {
  53. fh->notify(fh, IPMSG_FE_COMPLETE, fi);
  54. ret = 0;
  55. }
  56. return (ret == len);
  57. }
  58. static int sock_init(ipmsg_filehandler_t *fh)
  59. {
  60. struct sockaddr_in server_addr = {0};
  61. int sock;
  62. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  63. {
  64. LOG_E("Create socket error");
  65. return -1;
  66. }
  67. server_addr.sin_family = AF_INET;
  68. server_addr.sin_port = htons(fh->im->port);
  69. server_addr.sin_addr.s_addr = fh->ip;
  70. if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
  71. {
  72. LOG_E("Connect fail!");
  73. closesocket(sock);
  74. return -1;
  75. }
  76. return sock;
  77. }
  78. static void msg_server(void *p)
  79. {
  80. int sock;
  81. char *buf;
  82. ipmsg_filehandler_t *fh = p;
  83. int stage = 0;
  84. fh->notify(fh, IPMSG_FE_ENTER, NULL);
  85. if ((sock = sock_init(fh)) < 0)
  86. goto __exit;
  87. if ((buf = rt_malloc(IPMSG_FILERECV_BUFSZ)) == NULL)
  88. goto _out;
  89. while (1)
  90. {
  91. switch (stage)
  92. {
  93. case 0:
  94. {
  95. if (fh->notify(fh, IPMSG_FE_OPEN, fh->fi) != 0)
  96. goto _out;
  97. if (ipmsg_getdata_req(sock, fh, fh->fi, buf, IPMSG_FILERECV_BUFSZ))
  98. stage++;
  99. else
  100. {
  101. //error
  102. stage = 2;
  103. }
  104. }
  105. break;
  106. case 1:
  107. {
  108. if (!ipmsg_data_recv(sock, fh, fh->fi, buf, IPMSG_FILERECV_BUFSZ))
  109. {
  110. stage++;
  111. }
  112. }
  113. break;
  114. case 2:
  115. {
  116. fh->notify(fh, IPMSG_FE_CLOSE, fh->fi);
  117. }
  118. goto _out;
  119. }
  120. }
  121. _out:
  122. closesocket(sock);
  123. rt_free(buf);
  124. __exit:
  125. fh->notify(fh, IPMSG_FE_EXIT, NULL);
  126. ipmsg_filehandler_free(fh);
  127. }
  128. int ipmsg_filerecv_start(ipmsg_filehandler_t *h)
  129. {
  130. rt_thread_t tid;
  131. int ret = -1;
  132. tid = rt_thread_create("ipmsg-r",
  133. msg_server,
  134. h,
  135. 2048,
  136. 22,
  137. 20);
  138. if (tid)
  139. {
  140. ret = rt_thread_startup(tid);
  141. }
  142. return ret;
  143. }
  144. #endif