flipper_http.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. // Allow the LED to display while processing
  95. led_on: function () {
  96. serial.write("[LED/ON]");
  97. },
  98. // Disable the LED from displaying while processing
  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 the IP address of the WiFi Devboard
  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. // Get the IP address of the connected WiFi network
  163. ip_wifi: function () {
  164. serial.write("[WIFI/IP]");
  165. let response = this.read_data(500);
  166. if (response === undefined) {
  167. return "";
  168. }
  169. return this.to_string(response);
  170. },
  171. // Send a GET request to the board
  172. // I reduced this to return the first line of the response that isnt undefined
  173. // You'll also get 'out of memory' errors if you try to read/return too much data
  174. // As mjs is updated, this can be improved
  175. get_request: function (url) {
  176. serial.write('[GET]' + url);
  177. if (this.read_data(500) === "[GET/SUCCESS] GET request successful.") {
  178. while (true) {
  179. let line = this.read_data(500);
  180. if (line === "[GET/END]") {
  181. break;
  182. }
  183. if (line !== undefined) {
  184. this.clear_buffer(false); // Clear the buffer
  185. return line;
  186. }
  187. }
  188. }
  189. else {
  190. print("GET request failed");
  191. }
  192. this.clear_buffer(); // Clear the buffer
  193. return "";
  194. },
  195. // another GET request but with headers
  196. get_request_with_headers: function (url, headers) {
  197. serial.write('[GET/HTTP]{url:"' + url + '",headers:' + headers + '}');
  198. if (this.read_data(500) === "[GET/SUCCESS] GET request successful.") {
  199. while (true) {
  200. let line = this.read_data(500);
  201. if (line === "[GET/END]") {
  202. break;
  203. }
  204. if (line !== undefined) {
  205. this.clear_buffer(false); // Clear the buffer
  206. return line;
  207. }
  208. }
  209. }
  210. else {
  211. print("GET request failed");
  212. }
  213. this.clear_buffer(); // Clear the buffer
  214. return "";
  215. },
  216. // send POST request with headers
  217. post_request_with_headers: function (url, headers, data) {
  218. serial.write('[POST/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  219. if (this.read_data(500) === "[POST/SUCCESS] POST request successful.") {
  220. while (true) {
  221. let line = this.read_data(500);
  222. if (line === "[POST/END]") {
  223. break;
  224. }
  225. if (line !== undefined) {
  226. this.clear_buffer(false); // Clear the buffer
  227. return line;
  228. }
  229. }
  230. }
  231. else {
  232. print("POST request failed");
  233. }
  234. this.clear_buffer(); // Clear the buffer
  235. return "";
  236. },
  237. // send PUT request with headers
  238. put_request_with_headers: function (url, headers, data) {
  239. serial.write('[PUT/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  240. if (this.read_data(500) === "[PUT/SUCCESS] PUT request successful.") {
  241. while (true) {
  242. let line = this.read_data(500);
  243. if (line === "[PUT/END]") {
  244. break;
  245. }
  246. if (line !== undefined) {
  247. this.clear_buffer(false); // Clear the buffer
  248. return line;
  249. }
  250. }
  251. }
  252. else {
  253. print("PUT request failed");
  254. }
  255. this.clear_buffer(); // Clear the buffer
  256. return "";
  257. },
  258. // send DELETE request with headers
  259. delete_request_with_headers: function (url, headers, data) {
  260. serial.write('[DELETE/HTTP]{"url":"' + url + '","headers":' + headers + ',"payload":' + data + '}');
  261. if (this.read_data(500) === "[DELETE/SUCCESS] DELETE request successful.") {
  262. while (true) {
  263. let line = this.read_data(500);
  264. if (line === "[DELETE/END]") {
  265. break;
  266. }
  267. if (line !== undefined) {
  268. this.clear_buffer(false); // Clear the buffer
  269. return line;
  270. }
  271. }
  272. }
  273. else {
  274. print("DELETE request failed");
  275. }
  276. this.clear_buffer(); // Clear the buffer
  277. return "";
  278. },
  279. // Helper function to check if a string contains another string
  280. includes: function (text, search) {
  281. let stringLength = text.length;
  282. let searchLength = search.length;
  283. if (stringLength < searchLength) {
  284. return false;
  285. }
  286. for (let i = 0; i < stringLength; i++) {
  287. if (text[i] === search[0]) {
  288. let found = true;
  289. for (let j = 1; j < searchLength; j++) {
  290. if (text[i + j] !== search[j]) {
  291. found = false;
  292. break;
  293. }
  294. }
  295. if (found) {
  296. return true;
  297. }
  298. }
  299. }
  300. },
  301. // Convert an array of characters to a string
  302. to_string: function (text) {
  303. if (text === undefined) {
  304. return "";
  305. }
  306. let return_text = "";
  307. for (let i = 0; i < text.length; i++) {
  308. return_text += text[i];
  309. }
  310. return return_text;
  311. }
  312. };
  313. /* Example Usage:
  314. let textbox = require("textbox");
  315. textbox.setConfig("end", "text");
  316. textbox.show();
  317. textbox.addText("Flipper HTTP Example:\n\n");
  318. // Initialize the flipper http object
  319. fhttp.init();
  320. textbox.addText("Initialized!\n");
  321. // Send ping to the board
  322. let response = fhttp.ping();
  323. if (response) {
  324. textbox.addText("Ping successful\nSaving wifi settings...\n");
  325. let success = fhttp.save_wifi("JBlanked", "maingirl");
  326. if (success) {
  327. textbox.addText("Wifi settings saved\nSending GET request..\n");
  328. let url = "https://catfact.ninja/fact";
  329. let data = fhttp.get_request_with_headers(url, '{"User-Agent":"curl/7.64.1","Content-Type":"application/json"}');
  330. if (data !== undefined && data !== "") {
  331. textbox.addText("GET request successful!\n\nReturned Data: \n\n" + data + "\n\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. else {
  340. textbox.addText("GET request failed.\nDisconnecting from wifi...\n");
  341. if (fhttp.disconnect_wifi()) {
  342. textbox.addText("Disconnected from wifi.\n");
  343. }
  344. else {
  345. textbox.addText("Failed to disconnect from wifi.\n");
  346. }
  347. }
  348. }
  349. else {
  350. textbox.addText("Wifi settings failed to save.\n");
  351. }
  352. }
  353. else {
  354. textbox.addText("Ping failed.\n");
  355. }
  356. textbox.addText("Press BACK twice to exit..\n");
  357. delay(100000); // Wait for user to hit back
  358. textbox.addText("\nTimeout exceeded.\nExiting...\n");
  359. delay(5000);
  360. // Destructor
  361. fhttp.deinit();
  362. textbox.addText("Deinitialized!\n");
  363. /*