request_response.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "app_manager_export.h"
  6. #include "coap_ext.h"
  7. #include "wasm_export.h"
  8. #include "bh_assert.h"
  9. extern void module_request_handler(request_t *request, void *user_data);
  10. bool
  11. wasm_response_send(wasm_module_inst_t module_inst,
  12. int32 buffer_offset, int size)
  13. {
  14. char *buffer = NULL;
  15. if (!validate_app_addr(buffer_offset, size))
  16. return false;
  17. buffer = addr_app_to_native(buffer_offset);
  18. if (buffer != NULL) {
  19. response_t response[1];
  20. if (NULL == unpack_response(buffer, size, response))
  21. return false;
  22. am_send_response(response);
  23. return true;
  24. }
  25. return false;
  26. }
  27. void
  28. wasm_register_resource(wasm_module_inst_t module_inst, int32 url_offset)
  29. {
  30. char *url = NULL;
  31. if (!validate_app_str_addr(url_offset))
  32. return;
  33. url = addr_app_to_native(url_offset);
  34. if (url != NULL) {
  35. unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
  36. module_inst);
  37. bh_assert(mod_id != ID_NONE);
  38. am_register_resource(url, module_request_handler, mod_id);
  39. }
  40. }
  41. void
  42. wasm_post_request(wasm_module_inst_t module_inst,
  43. int32 buffer_offset, int size)
  44. {
  45. char *buffer = NULL;
  46. if (!validate_app_addr(buffer_offset, size))
  47. return;
  48. buffer = addr_app_to_native(buffer_offset);
  49. if (buffer != NULL) {
  50. request_t req[1];
  51. if (!unpack_request(buffer, size, req))
  52. return;
  53. // TODO: add permission check, ensure app can't do harm
  54. // set sender to help dispatch the response to the sender ap
  55. unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
  56. module_inst);
  57. bh_assert(mod_id != ID_NONE);
  58. req->sender = mod_id;
  59. if (req->action == COAP_EVENT) {
  60. am_publish_event(req);
  61. return;
  62. }
  63. am_dispatch_request(req);
  64. }
  65. }
  66. void
  67. wasm_sub_event(wasm_module_inst_t module_inst, int32 url_offset)
  68. {
  69. char *url = NULL;
  70. if (!validate_app_str_addr(url_offset))
  71. return;
  72. url = addr_app_to_native(url_offset);
  73. if (url != NULL) {
  74. unsigned int mod_id = app_manager_get_module_id(Module_WASM_App,
  75. module_inst);
  76. bh_assert(mod_id != ID_NONE);
  77. am_register_event(url, mod_id);
  78. }
  79. }