luat_msgbus_air105.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_msgbus.h"
  22. #include "app_interface.h"
  23. #if 0
  24. #define QUEUE_LENGTH 0xFF
  25. #define ITEM_SIZE sizeof(rtos_msg_t)
  26. #if configSUPPORT_STATIC_ALLOCATION
  27. static StaticQueue_t xStaticQueue = {0};
  28. #endif
  29. static QueueHandle_t xQueue = {0};
  30. #if configSUPPORT_STATIC_ALLOCATION
  31. static uint8_t ucQueueStorageArea[ QUEUE_LENGTH * ITEM_SIZE ];
  32. #endif
  33. void luat_msgbus_init(void) {
  34. if (!xQueue) {
  35. #if configSUPPORT_STATIC_ALLOCATION
  36. xQueue = xQueueCreateStatic( QUEUE_LENGTH,
  37. ITEM_SIZE,
  38. ucQueueStorageArea,
  39. &xStaticQueue );
  40. #else
  41. xQueue = xQueueCreate(QUEUE_LENGTH, ITEM_SIZE);
  42. #endif
  43. }
  44. }
  45. uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
  46. if (xQueue == NULL)
  47. return 1;
  48. return xQueueSendFromISR(xQueue, msg, NULL) == pdTRUE ? 0 : 1;
  49. }
  50. uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
  51. if (xQueue == NULL)
  52. return 1;
  53. return xQueueReceive(xQueue, msg, timeout) == pdTRUE ? 0 : 1; // 要不要除portTICK_RATE_MS呢?
  54. }
  55. uint32_t luat_msgbus_freesize(void) {
  56. if (xQueue == NULL)
  57. return 1;
  58. return 1;
  59. }
  60. #endif
  61. static HANDLE prvTaskHandle;
  62. void luat_msgbus_init(void) {
  63. prvTaskHandle = Task_GetCurrent();
  64. }
  65. uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
  66. Task_SendEvent(prvTaskHandle, msg->handler, (uint32_t)msg->ptr, msg->arg1, msg->arg2);
  67. return 0;
  68. }
  69. uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
  70. return Task_GetEventByMS(prvTaskHandle, 0, msg, NULL, timeout);
  71. }
  72. uint32_t luat_msgbus_freesize(void) {
  73. return 1;
  74. }
  75. uint8_t luat_msgbus_is_empty(void)
  76. {
  77. return !Task_GetEventCnt(prvTaskHandle);
  78. }