request_response.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "app_manager_export.h"
  17. #include "coap_ext.h"
  18. #include "wasm_export.h"
  19. extern void module_request_handler(request_t *request, void *user_data);
  20. bool
  21. wasm_response_send(wasm_module_inst_t module_inst,
  22. int32 buffer_offset, int size)
  23. {
  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
  38. wasm_register_resource(wasm_module_inst_t module_inst, int32 url_offset)
  39. {
  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
  50. wasm_post_request(wasm_module_inst_t module_inst,
  51. int32 buffer_offset, int size)
  52. {
  53. char *buffer = NULL;
  54. if (!validate_app_addr(buffer_offset, size))
  55. return;
  56. buffer = addr_app_to_native(buffer_offset);
  57. if (buffer != NULL) {
  58. request_t req[1];
  59. if (!unpack_request(buffer, size, req))
  60. return;
  61. // TODO: add permission check, ensure app can't do harm
  62. // set sender to help dispatch the response to the sender ap
  63. unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
  64. req->sender = mod_id;
  65. if (req->action == COAP_EVENT) {
  66. am_publish_event(req);
  67. return;
  68. }
  69. am_dispatch_request(req);
  70. }
  71. }
  72. void
  73. wasm_sub_event(wasm_module_inst_t module_inst, int32 url_offset)
  74. {
  75. char *url = NULL;
  76. if (!validate_app_addr(url_offset, 1))
  77. return;
  78. url = addr_app_to_native(url_offset);
  79. if (url != NULL) {
  80. unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
  81. am_register_event(url, mod_id);
  82. }
  83. }