flipper_http.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. while (true) {
  117. let line = this.read_data(500);
  118. if (line === "[GET/END]") {
  119. break;
  120. }
  121. if (line !== undefined) {
  122. this.clear_buffer(false); // Clear the buffer
  123. return line;
  124. }
  125. }
  126. }
  127. else {
  128. print("GET request failed");
  129. }
  130. this.clear_buffer(); // Clear the buffer
  131. return "";
  132. },
  133. // another GET request but with headers
  134. get_request_with_headers: function (url, headers) {
  135. serial.write('[GET/HTTP]{url:"' + url + '",headers:' + headers + '}');
  136. if (this.read_data(500) === "[GET/SUCCESS] GET request successful.") {
  137. while (true) {
  138. let line = this.read_data(500);
  139. if (line === "[GET/END]") {
  140. break;
  141. }
  142. if (line !== undefined) {
  143. this.clear_buffer(false); // Clear the buffer
  144. return line;
  145. }
  146. }
  147. }
  148. else {
  149. print("GET request failed");
  150. }
  151. this.clear_buffer(); // Clear the buffer
  152. return "";
  153. },
  154. // send POST request with headers
  155. post_request_with_headers: function (url, headers, data) {
  156. serial.write('[POST/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  157. if (this.read_data(500) === "[POST/SUCCESS] POST request successful.") {
  158. while (true) {
  159. let line = this.read_data(500);
  160. if (line === "[POST/END]") {
  161. break;
  162. }
  163. if (line !== undefined) {
  164. this.clear_buffer(false); // Clear the buffer
  165. return line;
  166. }
  167. }
  168. }
  169. else {
  170. print("POST request failed");
  171. }
  172. this.clear_buffer(); // Clear the buffer
  173. return "";
  174. },
  175. // send PUT request with headers
  176. put_request_with_headers: function (url, headers, data) {
  177. serial.write('[PUT/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  178. if (this.read_data(500) === "[PUT/SUCCESS] PUT request successful.") {
  179. while (true) {
  180. let line = this.read_data(500);
  181. if (line === "[PUT/END]") {
  182. break;
  183. }
  184. if (line !== undefined) {
  185. this.clear_buffer(false); // Clear the buffer
  186. return line;
  187. }
  188. }
  189. }
  190. else {
  191. print("PUT request failed");
  192. }
  193. this.clear_buffer(); // Clear the buffer
  194. return "";
  195. },
  196. // send DELETE request with headers
  197. delete_request_with_headers: function (url, headers, data) {
  198. serial.write('[DELETE/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  199. if (this.read_data(500) === "[DELETE/SUCCESS] DELETE request successful.") {
  200. while (true) {
  201. let line = this.read_data(500);
  202. if (line === "[DELETE/END]") {
  203. break;
  204. }
  205. if (line !== undefined) {
  206. this.clear_buffer(false); // Clear the buffer
  207. return line;
  208. }
  209. }
  210. }
  211. else {
  212. print("DELETE request failed");
  213. }
  214. this.clear_buffer(); // Clear the buffer
  215. return "";
  216. },
  217. // Helper function to check if a string contains another string
  218. includes: function (text, search) {
  219. let stringLength = text.length;
  220. let searchLength = search.length;
  221. if (stringLength < searchLength) {
  222. return false;
  223. }
  224. for (let i = 0; i < stringLength; i++) {
  225. if (text[i] === search[0]) {
  226. let found = true;
  227. for (let j = 1; j < searchLength; j++) {
  228. if (text[i + j] !== search[j]) {
  229. found = false;
  230. break;
  231. }
  232. }
  233. if (found) {
  234. return true;
  235. }
  236. }
  237. }
  238. },
  239. // Convert an array of characters to a string
  240. to_string: function (text) {
  241. if (text === undefined) {
  242. return "";
  243. }
  244. let return_text = "";
  245. for (let i = 0; i < text.length; i++) {
  246. return_text += text[i];
  247. }
  248. return return_text;
  249. }
  250. };
  251. /* Example Usage:
  252. let textbox = require("textbox");
  253. textbox.setConfig("end", "text");
  254. textbox.show();
  255. textbox.addText("Flipper HTTP Example:\n\n");
  256. // Initialize the flipper http object
  257. fhttp.init();
  258. textbox.addText("Initialized!\n");
  259. // Send ping to the board
  260. let response = fhttp.ping();
  261. if (response) {
  262. textbox.addText("Ping successful\nSaving wifi settings...\n");
  263. let success = fhttp.save_wifi("JBlanked", "maingirl");
  264. if (success) {
  265. textbox.addText("Wifi settings saved\nSending GET request..\n");
  266. let url = "https://catfact.ninja/fact";
  267. let data = fhttp.get_request_with_headers(url, '{"User-Agent":"curl/7.64.1","Content-Type":"application/json"}');
  268. if (data !== undefined && data !== "") {
  269. textbox.addText("GET request successful!\n\nReturned Data: \n\n" + data + "\n\nDisconnecting from wifi...\n");
  270. if (fhttp.disconnect_wifi()) {
  271. textbox.addText("Disconnected from wifi.\n");
  272. }
  273. else {
  274. textbox.addText("Failed to disconnect from wifi.\n");
  275. }
  276. }
  277. else {
  278. textbox.addText("GET request failed.\nDisconnecting from wifi...\n");
  279. if (fhttp.disconnect_wifi()) {
  280. textbox.addText("Disconnected from wifi.\n");
  281. }
  282. else {
  283. textbox.addText("Failed to disconnect from wifi.\n");
  284. }
  285. }
  286. }
  287. else {
  288. textbox.addText("Wifi settings failed to save.\n");
  289. }
  290. }
  291. else {
  292. textbox.addText("Ping failed.\n");
  293. }
  294. textbox.addText("Press BACK twice to exit..\n");
  295. delay(100000); // Wait for user to hit back
  296. textbox.addText("\nTimeout exceeded.\nExiting...\n");
  297. delay(5000);
  298. // Destructor
  299. fhttp.deinit();
  300. textbox.addText("Deinitialized!\n");
  301. /*