flipper_http.js 12 KB

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