flipper_http.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. // Get Wifi network list
  86. scan_wifi: function () {
  87. serial.write("[WIFI/SCAN]");
  88. let response = this.read_data(500);
  89. if (response === undefined) {
  90. return "";
  91. }
  92. return this.to_string(response);
  93. },
  94. // Save wifi settings
  95. save_wifi: function (ssid, password) {
  96. if (ssid === "" || password === "") {
  97. return false;
  98. }
  99. let command = '[WIFI/SAVE]{"ssid":"' + ssid + '","password":"' + password + '"}';
  100. serial.write(command);
  101. let response = this.read_data(500);
  102. if (response === undefined) {
  103. this.clear_buffer(false); // Clear the buffer
  104. return false;
  105. }
  106. let sresponse = this.to_string(response);
  107. if (this.includes(sresponse, "[SUCCESS]")) {
  108. this.clear_buffer(false); // Clear the buffer
  109. this.clear_buffer(false); // Clear the buffer
  110. return true;
  111. }
  112. else {
  113. print("Failed to save: " + response);
  114. this.clear_buffer(false); // Clear the buffer
  115. return false;
  116. }
  117. },
  118. // Send a GET request to the board
  119. // I reduced this to return the first line of the response that isnt undefined
  120. // You'll also get 'out of memory' errors if you try to read/return too much data
  121. // As mjs is updated, this can be improved
  122. get_request: function (url) {
  123. serial.write('[GET]' + url);
  124. if (this.read_data(500) === "[GET/SUCCESS] GET request successful.") {
  125. while (true) {
  126. let line = this.read_data(500);
  127. if (line === "[GET/END]") {
  128. break;
  129. }
  130. if (line !== undefined) {
  131. this.clear_buffer(false); // Clear the buffer
  132. return line;
  133. }
  134. }
  135. }
  136. else {
  137. print("GET request failed");
  138. }
  139. this.clear_buffer(); // Clear the buffer
  140. return "";
  141. },
  142. // another GET request but with headers
  143. get_request_with_headers: function (url, headers) {
  144. serial.write('[GET/HTTP]{url:"' + url + '",headers:' + headers + '}');
  145. if (this.read_data(500) === "[GET/SUCCESS] GET request successful.") {
  146. while (true) {
  147. let line = this.read_data(500);
  148. if (line === "[GET/END]") {
  149. break;
  150. }
  151. if (line !== undefined) {
  152. this.clear_buffer(false); // Clear the buffer
  153. return line;
  154. }
  155. }
  156. }
  157. else {
  158. print("GET request failed");
  159. }
  160. this.clear_buffer(); // Clear the buffer
  161. return "";
  162. },
  163. // send POST request with headers
  164. post_request_with_headers: function (url, headers, data) {
  165. serial.write('[POST/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  166. if (this.read_data(500) === "[POST/SUCCESS] POST request successful.") {
  167. while (true) {
  168. let line = this.read_data(500);
  169. if (line === "[POST/END]") {
  170. break;
  171. }
  172. if (line !== undefined) {
  173. this.clear_buffer(false); // Clear the buffer
  174. return line;
  175. }
  176. }
  177. }
  178. else {
  179. print("POST request failed");
  180. }
  181. this.clear_buffer(); // Clear the buffer
  182. return "";
  183. },
  184. // send PUT request with headers
  185. put_request_with_headers: function (url, headers, data) {
  186. serial.write('[PUT/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  187. if (this.read_data(500) === "[PUT/SUCCESS] PUT request successful.") {
  188. while (true) {
  189. let line = this.read_data(500);
  190. if (line === "[PUT/END]") {
  191. break;
  192. }
  193. if (line !== undefined) {
  194. this.clear_buffer(false); // Clear the buffer
  195. return line;
  196. }
  197. }
  198. }
  199. else {
  200. print("PUT request failed");
  201. }
  202. this.clear_buffer(); // Clear the buffer
  203. return "";
  204. },
  205. // send DELETE request with headers
  206. delete_request_with_headers: function (url, headers, data) {
  207. serial.write('[DELETE/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  208. if (this.read_data(500) === "[DELETE/SUCCESS] DELETE request successful.") {
  209. while (true) {
  210. let line = this.read_data(500);
  211. if (line === "[DELETE/END]") {
  212. break;
  213. }
  214. if (line !== undefined) {
  215. this.clear_buffer(false); // Clear the buffer
  216. return line;
  217. }
  218. }
  219. }
  220. else {
  221. print("DELETE request failed");
  222. }
  223. this.clear_buffer(); // Clear the buffer
  224. return "";
  225. },
  226. // Helper function to check if a string contains another string
  227. includes: function (text, search) {
  228. let stringLength = text.length;
  229. let searchLength = search.length;
  230. if (stringLength < searchLength) {
  231. return false;
  232. }
  233. for (let i = 0; i < stringLength; i++) {
  234. if (text[i] === search[0]) {
  235. let found = true;
  236. for (let j = 1; j < searchLength; j++) {
  237. if (text[i + j] !== search[j]) {
  238. found = false;
  239. break;
  240. }
  241. }
  242. if (found) {
  243. return true;
  244. }
  245. }
  246. }
  247. },
  248. // Convert an array of characters to a string
  249. to_string: function (text) {
  250. if (text === undefined) {
  251. return "";
  252. }
  253. let return_text = "";
  254. for (let i = 0; i < text.length; i++) {
  255. return_text += text[i];
  256. }
  257. return return_text;
  258. }
  259. };
  260. /* Example Usage:
  261. let textbox = require("textbox");
  262. textbox.setConfig("end", "text");
  263. textbox.show();
  264. textbox.addText("Flipper HTTP Example:\n\n");
  265. // Initialize the flipper http object
  266. fhttp.init();
  267. textbox.addText("Initialized!\n");
  268. // Send ping to the board
  269. let response = fhttp.ping();
  270. if (response) {
  271. textbox.addText("Ping successful\nSaving wifi settings...\n");
  272. let success = fhttp.save_wifi("JBlanked", "maingirl");
  273. if (success) {
  274. textbox.addText("Wifi settings saved\nSending GET request..\n");
  275. let url = "https://catfact.ninja/fact";
  276. let data = fhttp.get_request_with_headers(url, '{"User-Agent":"curl/7.64.1","Content-Type":"application/json"}');
  277. if (data !== undefined && data !== "") {
  278. textbox.addText("GET request successful!\n\nReturned Data: \n\n" + data + "\n\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. else {
  287. textbox.addText("GET request failed.\nDisconnecting from wifi...\n");
  288. if (fhttp.disconnect_wifi()) {
  289. textbox.addText("Disconnected from wifi.\n");
  290. }
  291. else {
  292. textbox.addText("Failed to disconnect from wifi.\n");
  293. }
  294. }
  295. }
  296. else {
  297. textbox.addText("Wifi settings failed to save.\n");
  298. }
  299. }
  300. else {
  301. textbox.addText("Ping failed.\n");
  302. }
  303. textbox.addText("Press BACK twice to exit..\n");
  304. delay(100000); // Wait for user to hit back
  305. textbox.addText("\nTimeout exceeded.\nExiting...\n");
  306. delay(5000);
  307. // Destructor
  308. fhttp.deinit();
  309. textbox.addText("Deinitialized!\n");
  310. /*