PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Tools/posix_apps.py

https://gitlab.com/victorl/PX4Firmware
Python | 148 lines | 112 code | 3 blank | 33 comment | 1 complexity | 533dfcad6e00ce2eef82c78993706658 MD5 | raw file
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # Copyright (C) 2015 Mark Charlebois. All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. # 3. Neither the name PX4 nor the names of its contributors may be
  17. # used to endorse or promote products derived from this software
  18. # without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. # POSSIBILITY OF SUCH DAMAGE.
  32. #
  33. ############################################################################
  34. import glob
  35. builtins = glob.glob("builtin_commands/COMMAND*")
  36. apps = []
  37. for f in builtins:
  38. apps.append(f.split(".")[-1].split("_main")[0])
  39. print("""
  40. #include <string>
  41. #include <map>
  42. #define __EXPORT
  43. #include <px4_tasks.h>
  44. #include <px4_posix.h>
  45. #include <stdlib.h>
  46. using namespace std;
  47. extern void px4_show_devices(void);
  48. extern "C" {
  49. """)
  50. for app in apps:
  51. print("extern int "+app+"_main(int argc, char *argv[]);")
  52. print("""
  53. static int shutdown_main(int argc, char *argv[]);
  54. static int list_tasks_main(int argc, char *argv[]);
  55. static int list_files_main(int argc, char *argv[]);
  56. static int list_devices_main(int argc, char *argv[]);
  57. static int list_topics_main(int argc, char *argv[]);
  58. static int sleep_main(int argc, char *argv[]);
  59. }
  60. static map<string,px4_main_t> app_map(void);
  61. static map<string,px4_main_t> app_map(void)
  62. {
  63. static map<string,px4_main_t> apps;
  64. """)
  65. for app in apps:
  66. print('\tapps["'+app+'"] = '+app+'_main;')
  67. print('\tapps["shutdown"] = shutdown_main;')
  68. print('\tapps["list_tasks"] = list_tasks_main;')
  69. print('\tapps["list_files"] = list_files_main;')
  70. print('\tapps["list_devices"] = list_devices_main;')
  71. print('\tapps["list_topics"] = list_topics_main;')
  72. print('\tapps["sleep"] = sleep_main;')
  73. print("""
  74. return apps;
  75. }
  76. map<string,px4_main_t> apps = app_map();
  77. static void list_builtins(void)
  78. {
  79. cout << "Builtin Commands:" << endl;
  80. for (map<string,px4_main_t>::iterator it=apps.begin(); it!=apps.end(); ++it)
  81. cout << '\t' << it->first << endl;
  82. }
  83. static int shutdown_main(int argc, char *argv[])
  84. {
  85. cout.flush();
  86. cout << endl << "Shutting down" << endl;
  87. cout.flush();
  88. exit(0);
  89. }
  90. static int list_tasks_main(int argc, char *argv[])
  91. {
  92. px4_show_tasks();
  93. return 0;
  94. }
  95. static int list_devices_main(int argc, char *argv[])
  96. {
  97. px4_show_devices();
  98. return 0;
  99. }
  100. static int list_topics_main(int argc, char *argv[])
  101. {
  102. px4_show_topics();
  103. return 0;
  104. }
  105. static int list_files_main(int argc, char *argv[])
  106. {
  107. px4_show_files();
  108. return 0;
  109. }
  110. static int sleep_main(int argc, char *argv[])
  111. {
  112. if (argc != 2) {
  113. cout << "Usage: sleep <seconds>" << endl;
  114. return 1;
  115. }
  116. sleep(atoi(argv[1]));
  117. return 0;
  118. }
  119. static int usleep_main(int argc, char *argv[])
  120. {
  121. if (argc != 2) {
  122. cout << "Usage: usleep <microseconds>" << endl;
  123. return 1;
  124. }
  125. usleep(atoi(argv[1]));
  126. return 0;
  127. }
  128. """)