xrp_report.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. int *ptr=report_item->buf;
  43. report_item->fn(report_item->context,report_item->buf);
  44. }
  45. int xrp_add_report(struct xrp_report_list *list,
  46. struct xrp_report_item *item)
  47. {
  48. // xrp_cond_lock(&queue->request_queue_cond);
  49. struct xrp_report_entry *entry_head=list->queue.head;
  50. struct xrp_report_entry *entry_cur=NULL;
  51. struct xrp_report_item *new_item;
  52. new_item = malloc(sizeof(struct xrp_report_item));
  53. if(!new_item)
  54. {
  55. return -1;
  56. }
  57. memcpy(new_item,item,sizeof(struct xrp_report_item));
  58. new_item->entry.next=NULL;
  59. if(NULL==entry_head)
  60. {
  61. list->queue.head= &new_item->entry;
  62. return 0;
  63. }
  64. for(;entry_head!=NULL;entry_head=entry_head->next)
  65. {
  66. entry_cur = entry_head;
  67. if(((struct xrp_report_item *)entry_head)->report_id == new_item->report_id)
  68. {
  69. DSP_PRINT(WARNING,"the report is already exist\n");
  70. return -1;
  71. }
  72. }
  73. entry_cur->next=&new_item->entry;
  74. DSP_PRINT(INFO,"add new report item %d\n",new_item->report_id);
  75. return 0;
  76. // xrp_cond_unlock(&queue->request_queue_cond);
  77. }
  78. int xrp_remove_report(struct xrp_report_list *list,int id)
  79. {
  80. struct xrp_report_entry *pre_entry=NULL;
  81. struct xrp_report_entry *cur_entry=list->queue.head;
  82. for(;cur_entry!=NULL;pre_entry=cur_entry,cur_entry=cur_entry->next)
  83. {
  84. if(((struct xrp_report_item *)cur_entry)->report_id == id)
  85. {
  86. if(pre_entry==NULL)
  87. {
  88. list->queue.head = cur_entry->next;
  89. }
  90. else{
  91. pre_entry->next=cur_entry->next;
  92. }
  93. free(cur_entry);
  94. return 0;
  95. }
  96. }
  97. return -1;
  98. }
  99. int xrp_alloc_report_id(struct xrp_report_list *list)
  100. {
  101. int new_id;
  102. int retry=0;
  103. while(1)
  104. {
  105. new_id= rand()&0x7fffffff;
  106. struct xrp_report_entry *cur_entry=list->queue.head;
  107. for(;cur_entry!=NULL;cur_entry=cur_entry->next)
  108. {
  109. if(((struct xrp_report_item *)cur_entry)->report_id == new_id)
  110. {
  111. retry++;
  112. if(retry>10)
  113. {
  114. DSP_PRINT(WARNING,"alloc report id fail");
  115. return -1;
  116. }
  117. break;
  118. }
  119. }
  120. return new_id;
  121. }
  122. }