PageRenderTime 84ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/L5_Application/examples/rn_xv_task.cpp

https://gitlab.com/gauraochaudhari/SmartBlindStick
C++ | 378 lines | 282 code | 56 blank | 40 comment | 44 complexity | 20ff475e6da9a4b293ef5ec2091d7956 MD5 | raw file
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include "rn_xv_task.hpp"
  6. #include "file_logger.h"
  7. #include "tlm/c_tlm_comp.h"
  8. #include "tlm/c_tlm_var.h"
  9. #include "utilities.h"
  10. #include "str.hpp"
  11. void wifiTask::wifiFlush(void)
  12. {
  13. char c = 0;
  14. while(mWifi.getChar(&c, OS_MS(500))) {
  15. if(mWifiEcho) {
  16. putchar(c);
  17. }
  18. }
  19. }
  20. void wifiTask::wifiSendCmd(const char* pCmd, const char* pParam)
  21. {
  22. mWifi.put(pCmd);
  23. if(0 != pParam) {
  24. mWifi.put(pParam);
  25. }
  26. mWifi.put("\r\n");
  27. wifiFlush();
  28. }
  29. void wifiTask::wifiEnterCmdMode(void)
  30. {
  31. /* Exit out of command mode just in case */
  32. mWifi.putline("exit");
  33. vTaskDelayMs(260);
  34. mWifi.put("$$$");
  35. vTaskDelayMs(260);
  36. wifiFlush();
  37. }
  38. bool wifiTask::wifiHandleHttpReq(web_req_type* request)
  39. {
  40. bool success = false;
  41. if(NULL == request->http_ip_host ||
  42. NULL == request->http_get_request ||
  43. NULL == request->http_response ||
  44. '\0' == request->http_get_request[0] ||
  45. '\0' == request->http_ip_host[0] ||
  46. request->http_response_size <= 0)
  47. {
  48. success = false;
  49. return success;
  50. }
  51. wifiEnterCmdMode();
  52. wifiSendCmd("set comm open @");
  53. /* ********************************************************
  54. * Open connection
  55. * RN-XV will exit command mode after connection is open,
  56. * so 'save' and 'exit' commands are not required
  57. */
  58. mWifi.put("open ");
  59. mWifi.put(request->http_ip_host);
  60. mWifi.putline(" 80");
  61. /* Need to wait for connection */
  62. // puts("Wait for connection char ...");
  63. char c = 0;
  64. while (c != '@' && mWifi.getChar(&c, OS_MS(30 * 1000))) {
  65. // putchar(c);
  66. }
  67. wifiFlush();
  68. success = ('@' == c);
  69. if (!success) {
  70. LOG_WARN("No connection char while servicing HTTP request");
  71. }
  72. /* Send our get request */
  73. mWifi.put("GET ");
  74. mWifi.put(request->http_get_request);
  75. mWifi.putline("\n\n\r\n");
  76. /* Wait higher timeout to get first server response */
  77. success = mWifi.getChar(&c, OS_MS(10 * 1000));
  78. if (!success) {
  79. LOG_WARN("No response data from HTTP server for 10 seconds");
  80. }
  81. if (success && request->http_discard_until) {
  82. /* Discard input until we see the discard_until char */
  83. while(request->http_discard_until != c) {
  84. if(!mWifi.getChar(&c, OS_MS(500))) {
  85. break;
  86. }
  87. }
  88. }
  89. /* Get the rest of the data with lower timeout and not that we
  90. * already received 1 byte above
  91. */
  92. request->http_response[0] = c;
  93. int len = request->http_response_size - 1;
  94. char *store = request->http_response + 1;
  95. while(len--) {
  96. if (! mWifi.getChar(&c, OS_MS(500))) {
  97. break;
  98. }
  99. *store++ = c;
  100. }
  101. *store = '\0';
  102. request->http_response_size = (store - request->http_response);
  103. wifiFlush();
  104. wifiEnterCmdMode();
  105. wifiSendCmd("close");
  106. wifiSendCmd("set comm open 0");
  107. wifiSendCmd("exit");
  108. return success;
  109. }
  110. bool wifiTask::wifiConnect(void)
  111. {
  112. char *wifi_ssid = mWifiSsid;
  113. char *wifi_key = mWifiKey;
  114. if(wifi_ssid && wifi_key && *wifi_ssid && *wifi_key)
  115. {
  116. puts("Using SSID/KEY from disk: ");
  117. puts(wifi_ssid);
  118. puts(wifi_key);
  119. puts("");
  120. }
  121. else
  122. {
  123. puts("Please configure wifi settings.");
  124. return false;
  125. }
  126. wifiEnterCmdMode();
  127. // Disable extra printing
  128. puts("Disable extra printing");
  129. wifiSendCmd("set sys printlvl 0");
  130. wifiSendCmd("set uart mode 0");
  131. // Set connection parameters
  132. puts("Set connection parameters");
  133. wifiSendCmd("set ip dhcp 1");
  134. wifiSendCmd("set wlan ssid ", wifi_ssid);
  135. wifiSendCmd("set wlan auth 4");
  136. wifiSendCmd("set wlan phrase ", wifi_key);
  137. wifiSendCmd("set wlan channel 0");
  138. wifiSendCmd("set wlan mask 0x1FFF");
  139. // Set greeting parameters
  140. puts("Set greeting parameters");
  141. wifiSendCmd("set comm close 0");
  142. wifiSendCmd("set comm open 0");
  143. wifiSendCmd("set comm remote 0");
  144. // Set flush parameters
  145. puts("Set buffer parameters");
  146. wifiSendCmd("set comm match 0");
  147. wifiSendCmd("set comm timeout 10");
  148. wifiSendCmd("set comm size 1024");
  149. //wifi_SendCommand(w, "set comm idle 0");
  150. wifiSendCmd("set comm idle 10");
  151. // Set our TCP/IP server parameters
  152. puts("Setup TCP/IP");
  153. wifiSendCmd("set ip protocol 2"); /* TCP Server and Client */
  154. wifiSendCmd("set ip localport " WIFI_PORT);
  155. puts("Reboot");
  156. wifiSendCmd("save");
  157. wifiSendCmd("reboot");
  158. wifiFlush();
  159. return true;
  160. }
  161. bool wifiTask::wifiIsConnected(void)
  162. {
  163. wifiEnterCmdMode();
  164. mWifi.putline("show connection");
  165. /**
  166. * Get response, which could either be : "show connection\n####" (uart echo)
  167. * or the response could be just "####" (status)
  168. */
  169. STR_ON_STACK(rsp, 128);
  170. mWifi.gets((char*)rsp(), rsp.getCapacity(), 1000);
  171. if(rsp.beginsWithIgnoreCase("show")) {
  172. mWifi.gets((char*)rsp(), rsp.getCapacity(), 1000);
  173. }
  174. mWifi.putline("exit");
  175. wifiFlush();
  176. // Response is in hex: 8ABC
  177. // Bits 4 and 5 indicate if connection is established
  178. char lsbHex = rsp[2];
  179. if(lsbHex >= '0' && lsbHex <= '9') {
  180. lsbHex -= '0';
  181. }
  182. else if(lsbHex >= 'A' && lsbHex <= 'F') {
  183. lsbHex -= 'A' + 10;
  184. }
  185. bool connected = (lsbHex & 0x3);
  186. return connected;
  187. }
  188. void wifiTask::wifiSendTestCmd(void)
  189. {
  190. wifiFlush();
  191. wifiEnterCmdMode();
  192. wifiFlush();
  193. mWifi.putline("ver");
  194. }
  195. bool wifiTask::wifiInitBaudRate(void)
  196. {
  197. STR_ON_STACK(s, 128);
  198. /* If we can communicate right now, we've got baud rate match so return out of here */
  199. printf(" Wifi attempt communication @ %i bps\n", (int)mWifiBaudRate);
  200. mWifi.setBaudRate(mWifiBaudRate);
  201. wifiSendTestCmd();
  202. /* Wifi should either echo back "ver" or send response with "wifly" in it */
  203. s.clear();
  204. mWifi.gets((char*)s(), s.getCapacity(), 1000);
  205. if (s.containsIgnoreCase("wifly") || s.containsIgnoreCase("ver")) {
  206. printf(" Wifi Baud Rate confirmed @ %i\n", (int)mWifiBaudRate);
  207. wifiSendCmd("exit");
  208. return true;
  209. }
  210. /* We cannot communicate, so try different baud rates */
  211. int baudRatesToTry[] = {9600, 38400, 115200, 230400, 460800};
  212. const int num_baud_rates = sizeof(baudRatesToTry) / sizeof(baudRatesToTry[0]);
  213. for (int i = 0; i < num_baud_rates; i++) {
  214. const int baudRateToTry = baudRatesToTry[i];
  215. printf(" Wifi attempt communication @ %i bps\n", baudRateToTry);
  216. wifiFlush();
  217. mWifi.setBaudRate(baudRateToTry);
  218. wifiSendTestCmd();
  219. /* If we can enter command mode now, then we've found the baud rate!
  220. * So set/change the desired baud rate on the wifi module
  221. */
  222. s.clear();
  223. if (mWifi.gets((char*)s(), s.getCapacity(), 1000)) {
  224. if(s.containsIgnoreCase("wifly") || s.containsIgnoreCase("ver")) {
  225. printf(" Wifi Baud Rate is: %i bps\n", baudRateToTry);
  226. printf(" Changing Wifi to %i bps\n", (int)mWifiBaudRate);
  227. s.printf("set uart baudrate %i", mWifiBaudRate);
  228. wifiSendCmd(s());
  229. wifiSendCmd("save");
  230. wifiSendCmd("reboot");
  231. delay_ms(2000);
  232. wifiFlush();
  233. return true;
  234. }
  235. else {
  236. printf(" Wifi bad response: %s\n", s());
  237. }
  238. }
  239. }
  240. mWifi.setBaudRate(mWifiBaudRate);
  241. printf(" Wifi Baud Rate is UNKNOWN. Set baud rate back to %i\n", (int) mWifiBaudRate);
  242. return false;
  243. }
  244. wifiTask::wifiTask(UartDev& uartForWifi, uint8_t priority) :
  245. scheduler_task("rnxv", 512*8, priority),
  246. mWifi(uartForWifi),
  247. mWifiBaudRate(WIFI_BAUD_RATE),
  248. mWifiEcho(true)
  249. {
  250. mHttpReqQueue = xQueueCreate(1, sizeof(web_req_type*));
  251. memset(mWifiSsid, 0, sizeof(mWifiSsid));
  252. memset(mWifiKey, 0, sizeof(mWifiKey));
  253. /* Default values, can be changed/saved during runtime */
  254. strncpy(mWifiSsid, WIFI_SSID, sizeof(mWifiSsid) - 1);
  255. strncpy(mWifiKey, WIFI_KEY , sizeof(mWifiKey) - 1);
  256. }
  257. bool wifiTask::init()
  258. {
  259. return addSharedObject(WIFI_SHR_OBJ, mHttpReqQueue);
  260. }
  261. bool wifiTask::regTlm()
  262. {
  263. #if SYS_CFG_ENABLE_TLM
  264. /* User can change this during run-time by setting telemetry variable */
  265. tlm_component *disk = tlm_component_get_by_name(SYS_CFG_DISK_TLM_NAME);
  266. TLM_REG_VAR(disk, mWifiSsid, tlm_string);
  267. TLM_REG_VAR(disk, mWifiKey, tlm_string);
  268. TLM_REG_VAR(disk, mWifiEcho, tlm_bit_or_bool);
  269. TLM_REG_VAR(disk, mWifiBaudRate, tlm_uint);
  270. #endif
  271. return true;
  272. }
  273. bool wifiTask::taskEntry()
  274. {
  275. // Not ready until changed otherwise
  276. mWifi.setReady(false);
  277. /* If we cannot detect baud rate, error out from here, but return true
  278. * so that this task doesn't halt the whole system due to this error.
  279. */
  280. if(!wifiInitBaudRate()) {
  281. return true;
  282. }
  283. if(!wifiIsConnected()) {
  284. puts(" Wifi not connected");
  285. if(wifiConnect()) {
  286. puts(" Wifi is now connected!");
  287. }
  288. else {
  289. puts(" Wifi ERROR connecting");
  290. }
  291. }
  292. /* Display the IP address */
  293. do {
  294. wifiEnterCmdMode();
  295. mWifi.putline("get ip");
  296. char buffer[128] = { 0 };
  297. mWifi.gets(buffer, sizeof(buffer), 1000); /* echo */
  298. mWifi.gets(buffer, sizeof(buffer), 1000); /* IF=UP */
  299. mWifi.gets(buffer, sizeof(buffer), 1000); /* DHCP=ON */
  300. mWifi.gets(buffer, sizeof(buffer), 1000); /* IP : Port */
  301. mWifi.putline("exit");
  302. printf(" Wifi %s\n", buffer);
  303. } while (0) ;
  304. wifiFlush();
  305. mWifi.setReady(true);
  306. return true;
  307. }
  308. bool wifiTask::run(void* p)
  309. {
  310. web_req_type *request = 0;
  311. if (xQueueReceive(mHttpReqQueue, &(request), portMAX_DELAY)) {
  312. mWifi.setReady(false);
  313. {
  314. request->success = wifiHandleHttpReq(request);
  315. if(request->req_done_signal) {
  316. xSemaphoreGive( (request->req_done_signal));
  317. }
  318. }
  319. mWifi.setReady(true);
  320. }
  321. return true;
  322. }