luat_http_network.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "luat_base.h"
  2. #include "luat_http.h"
  3. #include "luat_network_adapter.h"
  4. #include "luat_rtos.h"
  5. #define LUAT_LOG_TAG "http"
  6. #include "luat_log.h"
  7. static int32_t luat_http_cb(void *pData, void *pParam) {
  8. OS_EVENT *event = (OS_EVENT *)data;
  9. luat_http_ctx_t *ctx = (luat_http_ctx_t *)pParam;
  10. network_ctrl_t *nc = (network_ctrl_t *)ctx->network;
  11. switch (event->ID)
  12. {
  13. case EV_NW_RESULT_LINK:
  14. LLOGD("http cb EV_NW_RESULT_LINK %d", event->Param1);
  15. break;
  16. case EV_NW_RESULT_CONNECT:
  17. LLOGD("http cb EV_NW_RESULT_CONNECT %d", event->Param1);
  18. break;
  19. case EV_NW_RESULT_CLOSE:
  20. LLOGD("http cb EV_NW_RESULT_CLOSE %d", event->Param1);
  21. break;
  22. case EV_NW_RESULT_TX:
  23. LLOGD("http cb EV_NW_RESULT_TX %d", event->Param1);
  24. break;
  25. case EV_NW_RESULT_EVENT:
  26. LLOGD("http cb EV_NW_RESULT_EVENT %d", event->Param1);
  27. break;
  28. default:
  29. break;
  30. }
  31. network_wait_event(nc, NULL, 0, NULL);
  32. return 0;
  33. }
  34. static int http_thread_main(void* args) {
  35. luat_http_ctx_t *ctx = (luat_http_ctx_t *)args;
  36. network_ctrl_t *nc = (network_ctrl_t *)ctx->network;
  37. if(network_connect(nc, ctx->host, strlen(ctx->host), NULL, ctx->port, 0) < 0){
  38. network_close(nc, 0);
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. int luat_http_send(luat_http_ctx_t *ctx, http_cb cb) {
  44. int adapter_index = network_get_last_register_adapter();
  45. network_ctrl_t *nc = network_alloc_ctrl(adapter_index);
  46. if (nc == NULL) {
  47. LLOGW("network alloc fail");
  48. return -1;
  49. }
  50. ctx->network = nc;
  51. network_init_ctrl(nc, NULL, luat_http_cb, ctx);
  52. network_set_base_mode(nc, 1, 15000, 0, 0, 0, 0);
  53. network_set_local_port(nc, 0);
  54. network_deinit_tls(nc);
  55. luat_thread_t t = {
  56. .entry = http_thread_main,
  57. .name = "httpc",
  58. .stack_buff = NULL,
  59. .stack_size = 4096,
  60. .network = ctx
  61. };
  62. int ret = luat_thread_start(&t);
  63. LLOGD("http thread start ret %d", ret);
  64. return ret;
  65. }