flipper_http.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Description: Flipper HTTP API for the Flipper Wifi Developer Board.
  2. // Global: flipper_http_init, flipper_http_deinit, flipper_http_rx_callback(), flipper_http_send_data, flipper_http_connect_wifi, flipper_http_disconnect_wifi, flipper_http_ping, flipper_http_save_wifi, flipper_http_get_request
  3. // License: MIT
  4. // Author: JBlanked
  5. // File: flipper_http.js
  6. let serial = require("serial");
  7. // Define the global `fhttp` object with all the functions
  8. let fhttp = {
  9. // Constructor
  10. init: function () {
  11. serial.setup("usart", 115200);
  12. },
  13. // Deconstructor
  14. deinit: function () {
  15. serial.end();
  16. },
  17. // Read data from the serial port and return it line by line
  18. read_data: function (delay_ms) {
  19. let line = serial.readln(delay_ms);
  20. let i = 5;
  21. while (line === undefined && i > 0) {
  22. line = serial.readln(delay_ms);
  23. i--;
  24. }
  25. return line;
  26. },
  27. // Send data to the serial port
  28. send_data: function (data) {
  29. if (data === "") {
  30. return;
  31. }
  32. serial.write(data);
  33. },
  34. // Clear the incoming serial by up to 10 lines
  35. clear_buffer: function (search_for_success) {
  36. let data = this.read_data(100);
  37. let sdata = this.to_string(data);
  38. let i = 0;
  39. // clear all data until we get an expected response
  40. while (i < 5 &&
  41. (data !== undefined &&
  42. (!search_for_success || (search_for_success && !this.includes(sdata, "[SUCCESS]"))) &&
  43. !this.includes(sdata, "[ERROR]") &&
  44. !this.includes(sdata, "[INFO]") &&
  45. !this.includes(sdata, "[PONG]") &&
  46. !this.includes(sdata, "[DISCONNECTED]") &&
  47. !this.includes(sdata, "[CONNECTED]") &&
  48. !this.includes(sdata, "[GET/STARTED]") &&
  49. !this.includes(sdata, "[GET/END]"))) {
  50. data = this.read_data(100);
  51. sdata = this.to_string(data);
  52. i++;
  53. }
  54. },
  55. // Connect to wifi
  56. connect_wifi: function () {
  57. serial.write("[WIFI/CONNECT]");
  58. let response = this.read_data(500);
  59. if (response === undefined) {
  60. return false;
  61. }
  62. this.clear_buffer(true); // Clear the buffer
  63. return this.includes(this.to_string(response), "[SUCCESS]") || this.includes(this.to_string(response), "[CONNECTED]") || this.includes(this.to_string(response), "[INFO]");
  64. },
  65. // Disconnect from wifi
  66. disconnect_wifi: function () {
  67. serial.write("[WIFI/DISCONNECT]");
  68. let response = this.read_data(500);
  69. if (response === undefined) {
  70. return false;
  71. }
  72. this.clear_buffer(true); // Clear the buffer
  73. return this.includes(this.to_string(response), "[DISCONNECTED]") || this.includes(this.to_string(response), "WiFi stop");
  74. },
  75. // Send a ping to the board
  76. ping: function () {
  77. serial.write("[PING]");
  78. let response = this.read_data(100);
  79. if (response === undefined) {
  80. return false;
  81. }
  82. this.clear_buffer(true); // Clear the buffer
  83. return this.includes(this.to_string(response), "[PONG]");
  84. },
  85. // Save wifi settings
  86. save_wifi: function (ssid, password) {
  87. if (ssid === "" || password === "") {
  88. return false;
  89. }
  90. let command = '[WIFI/SAVE]{"ssid":"' + ssid + '","password":"' + password + '"}';
  91. serial.write(command);
  92. let response = this.read_data(500);
  93. if (response === undefined) {
  94. this.clear_buffer(false); // Clear the buffer
  95. return false;
  96. }
  97. let sresponse = this.to_string(response);
  98. if (this.includes(sresponse, "[SUCCESS]")) {
  99. this.clear_buffer(false); // Clear the buffer
  100. this.clear_buffer(false); // Clear the buffer
  101. return true;
  102. }
  103. else {
  104. print("Failed to save: " + response);
  105. this.clear_buffer(false); // Clear the buffer
  106. return false;
  107. }
  108. },
  109. // Send a GET request to the board
  110. // I reduced this to return the first line of the response that isnt undefined
  111. // You'll also get 'out of memory' errors if you try to read/return too much data
  112. // As mjs is updated, this can be improved
  113. get_request: function (url) {
  114. serial.write('[GET]' + url);
  115. if (this.read_data(500) === "[GET/SUCCESS] GET request successful.") {
  116. print("GET request successful");
  117. while (true) {
  118. let line = this.read_data(500);
  119. if (line === "[GET/END]") {
  120. break;
  121. }
  122. if (line !== undefined) {
  123. this.clear_buffer(false); // Clear the buffer
  124. return line;
  125. }
  126. }
  127. }
  128. else {
  129. print("GET request failed");
  130. }
  131. this.clear_buffer(); // Clear the buffer
  132. return "";
  133. },
  134. // another GET request but with headers
  135. get_request_with_headers: function (url, headers) {
  136. serial.write('[GET/HTTP]{url:"' + url + '",headers:' + headers + '}');
  137. if (this.read_data(500) === "[GET/SUCCESS] GET request successful.") {
  138. print("GET request successful");
  139. while (true) {
  140. let line = this.read_data(500);
  141. if (line === "[GET/END]") {
  142. break;
  143. }
  144. if (line !== undefined) {
  145. this.clear_buffer(false); // Clear the buffer
  146. return line;
  147. }
  148. }
  149. }
  150. else {
  151. print("GET request failed");
  152. }
  153. this.clear_buffer(); // Clear the buffer
  154. return "";
  155. },
  156. // send POST request with headers
  157. post_request_with_headers: function (url, headers, data) {
  158. serial.write('[POST/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  159. if (this.read_data(500) === "[POST/SUCCESS] POST request successful.") {
  160. print("POST request successful");
  161. while (true) {
  162. let line = this.read_data(500);
  163. if (line === "[POST/END]") {
  164. break;
  165. }
  166. if (line !== undefined) {
  167. this.clear_buffer(false); // Clear the buffer
  168. return line;
  169. }
  170. }
  171. }
  172. else {
  173. print("POST request failed");
  174. }
  175. this.clear_buffer(); // Clear the buffer
  176. return "";
  177. },
  178. // send PUT request with headers
  179. put_request_with_headers: function (url, headers, data) {
  180. serial.write('[PUT/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  181. if (this.read_data(500) === "[PUT/SUCCESS] PUT request successful.") {
  182. print("PUT request successful");
  183. while (true) {
  184. let line = this.read_data(500);
  185. if (line === "[PUT/END]") {
  186. break;
  187. }
  188. if (line !== undefined) {
  189. this.clear_buffer(false); // Clear the buffer
  190. return line;
  191. }
  192. }
  193. }
  194. else {
  195. print("PUT request failed");
  196. }
  197. this.clear_buffer(); // Clear the buffer
  198. return "";
  199. },
  200. // Helper function to check if a string contains another string
  201. includes: function (text, search) {
  202. let stringLength = text.length;
  203. let searchLength = search.length;
  204. if (stringLength < searchLength) {
  205. return false;
  206. }
  207. for (let i = 0; i < stringLength; i++) {
  208. if (text[i] === search[0]) {
  209. let found = true;
  210. for (let j = 1; j < searchLength; j++) {
  211. if (text[i + j] !== search[j]) {
  212. found = false;
  213. break;
  214. }
  215. }
  216. if (found) {
  217. return true;
  218. }
  219. }
  220. }
  221. },
  222. // Convert an array of characters to a string
  223. to_string: function (text) {
  224. if (text === undefined) {
  225. return "";
  226. }
  227. let return_text = "";
  228. for (let i = 0; i < text.length; i++) {
  229. return_text += text[i];
  230. }
  231. return return_text;
  232. }
  233. };
  234. /* Example Usage:
  235. let textbox = require("textbox");
  236. textbox.setConfig("end", "text");
  237. textbox.show();
  238. textbox.addText("Flipper HTTP Example:\n\n");
  239. // Initialize the flipper http object
  240. fhttp.init();
  241. textbox.addText("Initialized!\n");
  242. // Send ping to the board
  243. let response = fhttp.ping();
  244. if (response) {
  245. textbox.addText("Ping successful\nSaving wifi settings...\n");
  246. let success = fhttp.save_wifi("JBlanked", "maingirl");
  247. if (success) {
  248. textbox.addText("Wifi settings saved\nSending GET request..\n");
  249. let url = "https://catfact.ninja/fact";
  250. let data = fhttp.get_request_with_headers(url, '{"User-Agent":"curl/7.64.1","Content-Type":"application/json"}');
  251. if (data !== undefined && data !== "") {
  252. textbox.addText("GET request successful!\n\nReturned Data: \n\n" + data + "\n\nDisconnecting from wifi...\n");
  253. if (fhttp.disconnect_wifi()) {
  254. textbox.addText("Disconnected from wifi.\n");
  255. }
  256. else {
  257. textbox.addText("Failed to disconnect from wifi.\n");
  258. }
  259. }
  260. else {
  261. textbox.addText("GET request failed.\nDisconnecting from wifi...\n");
  262. if (fhttp.disconnect_wifi()) {
  263. textbox.addText("Disconnected from wifi.\n");
  264. }
  265. else {
  266. textbox.addText("Failed to disconnect from wifi.\n");
  267. }
  268. }
  269. }
  270. else {
  271. textbox.addText("Wifi settings failed to save.\n");
  272. }
  273. }
  274. else {
  275. textbox.addText("Ping failed.\n");
  276. }
  277. textbox.addText("Press BACK twice to exit..\n");
  278. delay(100000); // Wait for user to hit back
  279. textbox.addText("\nTimeout exceeded.\nExiting...\n");
  280. delay(5000);
  281. // Destructor
  282. fhttp.deinit();
  283. textbox.addText("Deinitialized!\n");
  284. /*