cfr_test_app.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2019 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /**
  19. * DOC: cfr capture test application
  20. * This file provides test application to dump cfr capture from driver
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <getopt.h>
  35. #include <stdint.h>
  36. #include <sys/time.h>
  37. #include <time.h>
  38. #include <signal.h>
  39. #define CFR_DUMP_STREAMFS_FILE "/sys/kernel/debug/qdf/cfr%s/cfr_dump0"
  40. #define CFR_DUMP_FILE "/tmp/cfr_dump_%s.bin"
  41. #define MAX_FILE_SIZE (8 * 1024 * 1024)
  42. #define MAX_CAPTURE_SIZE (4096)
  43. static char rbuffer[MAX_CAPTURE_SIZE];
  44. int stop_capture;
  45. void print_usage(char *argv[])
  46. {
  47. printf("Usage:cfr_test_app -i <interfacename>\n");
  48. }
  49. void streamfs_read_handler(int sfd, int cfd)
  50. {
  51. int rlen = 0, retval = 0;
  52. memset(rbuffer, 0, sizeof(rbuffer));
  53. rlen = read(sfd, rbuffer, sizeof(rbuffer));
  54. if (rlen <= 0)
  55. return;
  56. if (lseek(cfd, 0, SEEK_CUR) + rlen > MAX_FILE_SIZE) {
  57. retval = lseek(cfd, 0, SEEK_SET);
  58. if (retval < 0) {
  59. perror("lseek()");
  60. exit(EXIT_FAILURE);
  61. }
  62. }
  63. write(cfd, rbuffer, rlen);
  64. }
  65. int start_cfr_capture_daemon(int streamfs_fd, int cfr_dump_fd)
  66. {
  67. fd_set sfs_fdset;
  68. int maxfd = 0, retval = 0;
  69. FD_ZERO(&sfs_fdset);
  70. FD_SET(streamfs_fd, &sfs_fdset);
  71. maxfd = streamfs_fd;
  72. while (!stop_capture) {
  73. retval = select(maxfd + 1, &sfs_fdset, NULL, NULL, NULL);
  74. if (retval < 0) {
  75. perror("select()");
  76. exit(EXIT_FAILURE);
  77. }
  78. if (FD_ISSET(streamfs_fd, &sfs_fdset))
  79. streamfs_read_handler(streamfs_fd, cfr_dump_fd);
  80. }
  81. close(streamfs_fd);
  82. close(cfr_dump_fd);
  83. return 0;
  84. }
  85. int open_streamfs_file(char *iface)
  86. {
  87. int fd = -1;
  88. char filename[128];
  89. snprintf(filename, sizeof(filename), CFR_DUMP_STREAMFS_FILE, iface);
  90. fd = open(filename, O_RDONLY);
  91. return fd;
  92. }
  93. int initialize_cfr_dump_file(char *iface)
  94. {
  95. int fd = -1;
  96. static char filename[128];
  97. char time[50] = {0};
  98. struct timeval tv = {0};
  99. time_t curtime = 0;
  100. struct tm *tm_val = NULL;
  101. gettimeofday(&tv, NULL);
  102. curtime = tv.tv_sec;
  103. tm_val = localtime(&curtime);
  104. if (tm_val) {
  105. strftime(time, 50, "%Y_%m_%d_%T", tm_val);
  106. snprintf(filename, sizeof(filename), CFR_DUMP_FILE, time);
  107. fd = open(filename, O_WRONLY | O_CREAT);
  108. } else {
  109. perror("Unable to get time value to generate filename \n");
  110. }
  111. return fd;
  112. }
  113. /* Signal Hnadler to overide CTRL+C */
  114. void sig_handler(int signum)
  115. {
  116. stop_capture = 1;
  117. }
  118. int main(int argc, char *argv[])
  119. {
  120. int option = 0;
  121. char *iface = NULL;
  122. int streamfs_fd = -1, cfr_dump_fd = -1;
  123. while ((option = getopt(argc, argv, "i:")) != -1) {
  124. switch (option) {
  125. case 'i':
  126. iface = optarg;
  127. break;
  128. default:
  129. printf("Invalid argument\n");
  130. print_usage(argv);
  131. exit(EXIT_FAILURE);
  132. break;
  133. }
  134. }
  135. if (iface == NULL) {
  136. printf("Invalid interface option\n");
  137. exit(EXIT_FAILURE);
  138. }
  139. streamfs_fd = open_streamfs_file(iface);
  140. if (streamfs_fd < 0) {
  141. printf("Invalid interface: %s, CFR capture file not found\n", iface);
  142. exit(EXIT_FAILURE);
  143. }
  144. cfr_dump_fd = initialize_cfr_dump_file(iface);
  145. if (cfr_dump_fd < 0) {
  146. printf(" Could not open CFR dump file for write\n");
  147. close(streamfs_fd);
  148. exit(EXIT_FAILURE);
  149. }
  150. printf("Starting CFR capture deamon, Press CTRL+C to exit \n");
  151. signal(SIGINT, sig_handler);
  152. start_cfr_capture_daemon(streamfs_fd, cfr_dump_fd);
  153. return 0;
  154. }