request_response.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "native_interface.h"
  17. #include "app-manager-export.h"
  18. #include "coap_ext.h"
  19. #include "wasm-export.h"
  20. extern void module_request_handler(request_t *request, uint32 register_id);
  21. bool wasm_response_send(int32 buffer_offset, int size)
  22. {
  23. wasm_module_inst_t module_inst = get_module_inst();
  24. char *buffer = NULL;
  25. if (!validate_app_addr(buffer_offset, size))
  26. return false;
  27. buffer = addr_app_to_native(buffer_offset);
  28. if (buffer != NULL) {
  29. response_t response[1];
  30. if (NULL == unpack_response(buffer, size, response))
  31. return false;
  32. am_send_response(response);
  33. return true;
  34. }
  35. return false;
  36. }
  37. void wasm_register_resource(int32 url_offset)
  38. {
  39. wasm_module_inst_t module_inst = get_module_inst();
  40. char *url = NULL;
  41. if (!validate_app_addr(url_offset, 1))
  42. return;
  43. url = addr_app_to_native(url_offset);
  44. if (url != NULL) {
  45. unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
  46. am_register_resource(url, module_request_handler, mod_id);
  47. }
  48. }
  49. void wasm_post_request(int32 buffer_offset, int size)
  50. {
  51. wasm_module_inst_t module_inst = get_module_inst();
  52. char *buffer = NULL;
  53. if (!validate_app_addr(buffer_offset, size))
  54. return;
  55. buffer = addr_app_to_native(buffer_offset);
  56. if (buffer != NULL) {
  57. request_t req[1];
  58. if (!unpack_request(buffer, size, req))
  59. return;
  60. // TODO: add permission check, ensure app can't do harm
  61. // set sender to help dispatch the response to the sender ap
  62. unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
  63. req->sender = mod_id;
  64. if (req->action == COAP_EVENT) {
  65. am_publish_event(req);
  66. return;
  67. }
  68. am_dispatch_request(req);
  69. }
  70. }
  71. void wasm_sub_event(int32 url_offset)
  72. {
  73. wasm_module_inst_t module_inst = get_module_inst();
  74. char *url = NULL;
  75. if (!validate_app_addr(url_offset, 1))
  76. return;
  77. url = addr_app_to_native(url_offset);
  78. if (url != NULL) {
  79. unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
  80. am_register_event(url, mod_id);
  81. }
  82. }