xrp_report.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <signal.h>
  8. #include "xrp_debug.h"
  9. #include "xrp_report.h"
  10. #include "xrp_thread_impl.h"
  11. #include "dsp_common.h"
  12. // void xrp_reporter_init(struct xrp_report_list *list)
  13. // {
  14. // g_list=list;
  15. // g_list->queue.head=NULL;
  16. // }
  17. struct xrp_report_item* xrp_get_report_entry(struct xrp_report_list *list,int id)
  18. {
  19. struct xrp_report_entry *cur_entry=list->queue.head;
  20. for(;cur_entry!=NULL;cur_entry=cur_entry->next)
  21. {
  22. if(((struct xrp_report_item *)cur_entry)->report_id == id)
  23. {
  24. return (struct xrp_report_item*)cur_entry;
  25. }
  26. }
  27. return NULL;
  28. }
  29. void xrp_process_report(struct xrp_report_list *list,void* data,unsigned int id)
  30. {
  31. struct xrp_report_item* report_item= xrp_get_report_entry(list,id);
  32. if(!report_item)
  33. {
  34. DSP_PRINT(WARNING,"No valid report item by id (%d)\n",id);
  35. return;
  36. }
  37. if(!report_item->fn ||
  38. (report_item->size&&!report_item->buf)){
  39. return;
  40. }
  41. // memcpy(report_item->buf,data,report_item->size);
  42. report_item->fn(report_item->context,data);
  43. }
  44. int xrp_add_report(struct xrp_report_list *list,
  45. struct xrp_report_item *item)
  46. {
  47. // xrp_cond_lock(&queue->request_queue_cond);
  48. struct xrp_report_entry *entry_head=list->queue.head;
  49. struct xrp_report_entry *entry_cur=NULL;
  50. struct xrp_report_item *new_item;
  51. new_item = malloc(sizeof(struct xrp_report_item));
  52. if(!new_item)
  53. {
  54. return -1;
  55. }
  56. memcpy(new_item,item,sizeof(struct xrp_report_item));
  57. new_item->entry.next=NULL;
  58. if(NULL==entry_head)
  59. {
  60. list->queue.head= &new_item->entry;
  61. return 0;
  62. }
  63. for(;entry_head!=NULL;entry_head=entry_head->next)
  64. {
  65. entry_cur = entry_head;
  66. if(((struct xrp_report_item *)entry_head)->report_id == new_item->report_id)
  67. {
  68. DSP_PRINT(WARNING,"the report is already exist\n");
  69. return -1;
  70. }
  71. }
  72. entry_cur->next=&new_item->entry;
  73. DSP_PRINT(INFO,"add new report item %d\n",new_item->report_id);
  74. return 0;
  75. // xrp_cond_unlock(&queue->request_queue_cond);
  76. }
  77. int xrp_remove_report(struct xrp_report_list *list,int id)
  78. {
  79. struct xrp_report_entry *pre_entry=NULL;
  80. struct xrp_report_entry *cur_entry=list->queue.head;
  81. for(;cur_entry!=NULL;pre_entry=cur_entry,cur_entry=cur_entry->next)
  82. {
  83. if(((struct xrp_report_item *)cur_entry)->report_id == id)
  84. {
  85. if(pre_entry==NULL)
  86. {
  87. list->queue.head = cur_entry->next;
  88. }
  89. else{
  90. pre_entry->next=cur_entry->next;
  91. }
  92. free(cur_entry);
  93. return 0;
  94. }
  95. }
  96. return -1;
  97. }
  98. int xrp_alloc_report_id(struct xrp_report_list *list)
  99. {
  100. int new_id;
  101. int retry=0;
  102. while(1)
  103. {
  104. new_id= rand()&0x7fffffff;
  105. struct xrp_report_entry *cur_entry=list->queue.head;
  106. for(;cur_entry!=NULL;cur_entry=cur_entry->next)
  107. {
  108. if(((struct xrp_report_item *)cur_entry)->report_id == new_id)
  109. {
  110. retry++;
  111. if(retry>10)
  112. {
  113. DSP_PRINT(WARNING,"alloc report id fail");
  114. return -1;
  115. }
  116. break;
  117. }
  118. }
  119. return new_id;
  120. }
  121. }