ese_data.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2020 Samsung Electronics. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include "ese_log.h"
  20. #include "ese_memory.h"
  21. #include "ese_data.h"
  22. static int32_t _ese_data_get_from_list(data_list_t *head, uint8_t *data, uint32_t *data_size)
  23. {
  24. data_list_t *cur_node;
  25. uint32_t offset = 0;
  26. if (head == NULL || data == NULL) {
  27. LOG_E("invalid header and data");
  28. return -1;
  29. }
  30. cur_node = head->next;
  31. while (cur_node != NULL) {
  32. memcpy((data + offset), cur_node->packet.data, cur_node->packet.size);
  33. offset += cur_node->packet.size;
  34. cur_node = cur_node->next;
  35. }
  36. *data_size = offset;
  37. return 0;
  38. }
  39. static int32_t _ese_data_delete_list(data_list_t *head)
  40. {
  41. data_list_t *cur, *next;
  42. if (head == NULL) {
  43. return -1;
  44. }
  45. cur = head->next;
  46. while (cur != NULL) {
  47. next = cur->next;
  48. ESE_FREE(cur->packet.data);
  49. ESE_FREE(cur);
  50. cur = NULL;
  51. cur = next;
  52. }
  53. head->next = NULL;
  54. head->packet.data = NULL;
  55. head->packet.size = 0;
  56. return 0;
  57. }
  58. ESE_STATUS ese_data_init(data_list_t *head)
  59. {
  60. head->next = NULL;
  61. head->packet.data = NULL;
  62. head->packet.size = 0;
  63. return ESESTATUS_SUCCESS;
  64. }
  65. ESE_STATUS ese_data_store(data_list_t *head, uint8_t *data, uint32_t data_size, uint8_t copy)
  66. {
  67. data_list_t *new_node = NULL;
  68. data_list_t *cur_node = NULL;
  69. new_node = ESE_MALLOC(sizeof(data_list_t));
  70. if (new_node == NULL) {
  71. return ESESTATUS_MEMORY_ALLOCATION_FAIL;
  72. }
  73. new_node->next = NULL;
  74. new_node->packet.size = data_size;
  75. if (copy) {
  76. new_node->packet.data = ESE_MALLOC(data_size);
  77. if (new_node->packet.data == NULL) {
  78. ESE_FREE(new_node);
  79. return ESESTATUS_MEMORY_ALLOCATION_FAIL;
  80. }
  81. memcpy(new_node->packet.data, data, data_size);
  82. } else {
  83. new_node->packet.data = data;
  84. }
  85. cur_node = head;
  86. while (cur_node->next != NULL) {
  87. cur_node = cur_node->next;
  88. }
  89. cur_node->next = new_node;
  90. head->packet.size += data_size;
  91. return ESESTATUS_SUCCESS;
  92. }
  93. ESE_STATUS ese_data_get(data_list_t *head, uint8_t **data, uint32_t *data_size)
  94. {
  95. uint32_t total_size = 0;
  96. uint8_t* tmp_buf = NULL;
  97. if (data != NULL && data_size != NULL) {
  98. if (head->packet.size == 0) {
  99. *data = NULL;
  100. *data_size = 0;
  101. return ESESTATUS_INVALID_BUFFER;
  102. }
  103. tmp_buf = ESE_MALLOC(head->packet.size);
  104. if (tmp_buf == NULL) {
  105. LOG_E("failed to allocate memory");
  106. _ese_data_delete_list(head);
  107. return ESESTATUS_MEMORY_ALLOCATION_FAIL;
  108. }
  109. if (_ese_data_get_from_list(head, tmp_buf, &total_size) != 0) {
  110. LOG_E("failed to get data from list");
  111. ESE_FREE(tmp_buf);
  112. _ese_data_delete_list(head);
  113. return ESESTATUS_INVALID_BUFFER;
  114. }
  115. if (total_size != head->packet.size) {
  116. LOG_E("mismatch size [%d, %d]", total_size, head->packet.size);
  117. ESE_FREE(tmp_buf);
  118. _ese_data_delete_list(head);
  119. return ESESTATUS_INVALID_BUFFER;
  120. }
  121. *data = tmp_buf;
  122. *data_size = total_size;
  123. }
  124. _ese_data_delete_list(head);
  125. return ESESTATUS_SUCCESS;
  126. }
  127. ESE_STATUS ese_data_delete(data_list_t *head)
  128. {
  129. data_list_t *cur, *next;
  130. if (head == NULL) {
  131. return -1;
  132. }
  133. cur = head->next;
  134. while (cur != NULL) {
  135. next = cur->next;
  136. ESE_FREE(cur->packet.data);
  137. ESE_FREE(cur);
  138. cur = NULL;
  139. cur = next;
  140. }
  141. head->next = NULL;
  142. head->packet.data = NULL;
  143. head->packet.size = 0;
  144. return 0;
  145. }