ZuluSCSI_platform_network.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (c) 2023 joshua stein <jcs@jcs.org>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifdef ZULUSCSI_NETWORK
  17. #include "ZuluSCSI_platform_network.h"
  18. #include "ZuluSCSI_log.h"
  19. #include "ZuluSCSI_config.h"
  20. #include <scsi.h>
  21. #include <network.h>
  22. extern "C" {
  23. #include <cyw43.h>
  24. #include <pico/cyw43_arch.h>
  25. #ifndef CYW43_IOCTL_GET_RSSI
  26. #define CYW43_IOCTL_GET_RSSI (0xfe)
  27. #endif
  28. #define PICO_W_GPIO_LED_PIN 0
  29. #define PICO_W_LED_ON() cyw43_arch_gpio_put(PICO_W_GPIO_LED_PIN, 1)
  30. #define PICO_W_LED_OFF() cyw43_arch_gpio_put(PICO_W_GPIO_LED_PIN, 0)
  31. #define PICO_W_LONG_BLINK_DELAY 200
  32. #define PICO_W_SHORT_BLINK_DELAY 75
  33. // A default DaynaPort-compatible MAC
  34. static const char defaultMAC[] = { 0x00, 0x80, 0x19, 0xc0, 0xff, 0xee };
  35. static bool network_in_use = false;
  36. bool platform_network_supported()
  37. {
  38. /* from cores/rp2040/RP2040Support.h */
  39. #if !defined(ARDUINO_RASPBERRY_PI_PICO_W)
  40. return false;
  41. #else
  42. extern bool __isPicoW;
  43. return __isPicoW;
  44. #endif
  45. }
  46. int platform_network_init(char *mac)
  47. {
  48. pico_unique_board_id_t board_id;
  49. uint8_t set_mac[6], read_mac[6];
  50. if (!platform_network_supported())
  51. return -1;
  52. // long signal blink at network initialization
  53. PICO_W_LED_OFF();
  54. PICO_W_LED_ON();
  55. delay(PICO_W_LONG_BLINK_DELAY);
  56. PICO_W_LED_OFF();
  57. logmsg(" ");
  58. logmsg("=== Network Initialization ===");
  59. memset(wifi_network_list, 0, sizeof(wifi_network_list));
  60. cyw43_deinit(&cyw43_state);
  61. cyw43_init(&cyw43_state);
  62. if (mac == NULL || (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 && mac[4] == 0 && mac[5] == 0))
  63. {
  64. mac = (char *)&set_mac;
  65. memcpy(mac, defaultMAC, sizeof(set_mac));
  66. // retain Dayna vendor but use a device id specific to this board
  67. pico_get_unique_board_id(&board_id);
  68. if (g_log_debug)
  69. logmsg("Unique board id: ", board_id.id[0], " ", board_id.id[1], " ", board_id.id[2], " ", board_id.id[3], " ",
  70. board_id.id[4], " ", board_id.id[5], " ", board_id.id[6], " ", board_id.id[7]);
  71. if (board_id.id[3] != 0 && board_id.id[4] != 0 && board_id.id[5] != 0)
  72. {
  73. mac[3] = board_id.id[3];
  74. mac[4] = board_id.id[4];
  75. mac[5] = board_id.id[5];
  76. }
  77. memcpy(scsiDev.boardCfg.wifiMACAddress, mac, sizeof(scsiDev.boardCfg.wifiMACAddress));
  78. }
  79. // setting the MAC requires libpico to be compiled with CYW43_USE_OTP_MAC=0
  80. memcpy(cyw43_state.mac, mac, sizeof(cyw43_state.mac));
  81. cyw43_arch_enable_sta_mode();
  82. cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, read_mac);
  83. logmsg("Wi-Fi MAC: ", read_mac[0],":",read_mac[1], ":", read_mac[2], ":", read_mac[3], ":", read_mac[4], ":", read_mac[5]);
  84. if (memcmp(mac, read_mac, sizeof(read_mac)) != 0)
  85. logmsg("WARNING: Wi-Fi MAC is not what was requested (",
  86. (uint8_t)mac[0], ":", (uint8_t)mac[1], ":", (uint8_t)mac[2], ":", (uint8_t)mac[3], ":", (uint8_t)mac[4], ":", (uint8_t)mac[5],
  87. "), is libpico not compiled with CYW43_USE_OTP_MAC=0?");
  88. network_in_use = true;
  89. return 0;
  90. }
  91. void platform_network_add_multicast_address(uint8_t *mac)
  92. {
  93. int ret;
  94. if ((ret = cyw43_wifi_update_multicast_filter(&cyw43_state, mac, true)) != 0)
  95. logmsg( __func__, ": cyw43_wifi_update_multicast_filter: ", ret);
  96. }
  97. bool platform_network_wifi_join(char *ssid, char *password)
  98. {
  99. int ret;
  100. if (!platform_network_supported())
  101. return false;
  102. if (password == NULL || password[0] == 0)
  103. {
  104. logmsg("Connecting to Wi-Fi SSID \"", ssid, "\" with no authentication");
  105. ret = cyw43_arch_wifi_connect_async(ssid, NULL, CYW43_AUTH_OPEN);
  106. }
  107. else
  108. {
  109. logmsg("Connecting to Wi-Fi SSID \"", ssid, "\" with WPA/WPA2 PSK");
  110. ret = cyw43_arch_wifi_connect_async(ssid, password, CYW43_AUTH_WPA2_MIXED_PSK);
  111. }
  112. if (ret != 0)
  113. {
  114. logmsg("Wi-Fi connection failed: ", ret);
  115. }
  116. else
  117. {
  118. // Short single blink at start of connection sequence
  119. PICO_W_LED_OFF();
  120. delay(PICO_W_SHORT_BLINK_DELAY);
  121. PICO_W_LED_ON();
  122. delay(PICO_W_SHORT_BLINK_DELAY);
  123. PICO_W_LED_OFF();
  124. }
  125. return (ret == 0);
  126. }
  127. void platform_network_poll()
  128. {
  129. if (!network_in_use)
  130. return;
  131. scsiNetworkPurge();
  132. cyw43_arch_poll();
  133. }
  134. int platform_network_send(uint8_t *buf, size_t len)
  135. {
  136. int ret = cyw43_send_ethernet(&cyw43_state, 0, len, buf, 0);
  137. if (ret != 0)
  138. logmsg("cyw43_send_ethernet failed: ", ret);
  139. return ret;
  140. }
  141. static int platform_network_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
  142. {
  143. struct wifi_network_entry *entry = NULL;
  144. if (!result || !result->ssid_len || !result->ssid[0])
  145. return 0;
  146. for (int i = 0; i < WIFI_NETWORK_LIST_ENTRY_COUNT; i++)
  147. {
  148. // take first available
  149. if (wifi_network_list[i].ssid[0] == '\0')
  150. {
  151. entry = &wifi_network_list[i];
  152. break;
  153. }
  154. // or if we've seen this network before, use this slot
  155. else if (strcmp((char *)result->ssid, wifi_network_list[i].ssid) == 0)
  156. {
  157. entry = &wifi_network_list[i];
  158. break;
  159. }
  160. }
  161. if (!entry)
  162. {
  163. // no available slots, insert according to our RSSI
  164. for (int i = 0; i < WIFI_NETWORK_LIST_ENTRY_COUNT; i++)
  165. {
  166. if (result->rssi > wifi_network_list[i].rssi)
  167. {
  168. // shift everything else down
  169. for (int j = WIFI_NETWORK_LIST_ENTRY_COUNT - 1; j > i; j--)
  170. wifi_network_list[j] = wifi_network_list[j - 1];
  171. entry = &wifi_network_list[i];
  172. memset(entry, 0, sizeof(struct wifi_network_entry));
  173. break;
  174. }
  175. }
  176. }
  177. if (entry == NULL)
  178. return 0;
  179. if (entry->rssi == 0 || result->rssi > entry->rssi)
  180. {
  181. entry->channel = result->channel;
  182. entry->rssi = result->rssi;
  183. }
  184. if (result->auth_mode & 7)
  185. entry->flags = WIFI_NETWORK_FLAG_AUTH;
  186. strncpy(entry->ssid, (const char *)result->ssid, sizeof(entry->ssid));
  187. entry->ssid[sizeof(entry->ssid) - 1] = '\0';
  188. memcpy(entry->bssid, result->bssid, sizeof(entry->bssid));
  189. return 0;
  190. }
  191. int platform_network_wifi_start_scan()
  192. {
  193. if (cyw43_wifi_scan_active(&cyw43_state))
  194. return -1;
  195. cyw43_wifi_scan_options_t scan_options = { 0 };
  196. memset(wifi_network_list, 0, sizeof(wifi_network_list));
  197. return cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, platform_network_wifi_scan_result);
  198. }
  199. int platform_network_wifi_scan_finished()
  200. {
  201. return !cyw43_wifi_scan_active(&cyw43_state);
  202. }
  203. void platform_network_wifi_dump_scan_list()
  204. {
  205. struct wifi_network_entry *entry = NULL;
  206. for (int i = 0; i < WIFI_NETWORK_LIST_ENTRY_COUNT; i++)
  207. {
  208. entry = &wifi_network_list[i];
  209. if (entry->ssid[0] == '\0')
  210. break;
  211. logmsg("wifi[",i,"] = ",entry->ssid,", channel ",(int)entry->channel,", rssi ",(int)entry->rssi,
  212. ", bssid ",(uint8_t) entry->bssid[0],":",(uint8_t) entry->bssid[1],":",(uint8_t) entry->bssid[2],":",
  213. (uint8_t) entry->bssid[3],":",(uint8_t) entry->bssid[4],":",(uint8_t) entry->bssid[5],", flags ", entry->flags);
  214. }
  215. }
  216. int platform_network_wifi_rssi()
  217. {
  218. int32_t rssi = 0;
  219. cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_RSSI, sizeof(rssi), (uint8_t *)&rssi, CYW43_ITF_STA);
  220. return rssi;
  221. }
  222. char * platform_network_wifi_ssid()
  223. {
  224. struct ssid_t {
  225. uint32_t ssid_len;
  226. uint8_t ssid[32 + 1];
  227. } ssid;
  228. static char cur_ssid[32 + 1];
  229. memset(cur_ssid, 0, sizeof(cur_ssid));
  230. int ret = cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_SSID, sizeof(ssid), (uint8_t *)&ssid, CYW43_ITF_STA);
  231. if (ret)
  232. {
  233. logmsg("Failed getting Wi-Fi SSID: ", ret);
  234. return NULL;
  235. }
  236. ssid.ssid[sizeof(ssid.ssid) - 1] = '\0';
  237. if (ssid.ssid_len < sizeof(ssid.ssid))
  238. ssid.ssid[ssid.ssid_len] = '\0';
  239. strlcpy(cur_ssid, (char *)ssid.ssid, sizeof(cur_ssid));
  240. return cur_ssid;
  241. }
  242. char * platform_network_wifi_bssid()
  243. {
  244. static char bssid[6];
  245. memset(bssid, 0, sizeof(bssid));
  246. /* TODO */
  247. return bssid;
  248. }
  249. int platform_network_wifi_channel()
  250. {
  251. int32_t channel = 0;
  252. cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_CHANNEL, sizeof(channel), (uint8_t *)&channel, CYW43_ITF_STA);
  253. return channel;
  254. }
  255. // these override weakly-defined functions in pico-sdk
  256. void cyw43_cb_process_ethernet(void *cb_data, int itf, size_t len, const uint8_t *buf)
  257. {
  258. scsiNetworkEnqueue(buf, len);
  259. }
  260. void cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf)
  261. {
  262. logmsg("Disassociated from Wi-Fi SSID \"", (char *)self->ap_ssid,"\"");
  263. }
  264. void cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf)
  265. {
  266. char *ssid = platform_network_wifi_ssid();
  267. if (ssid)
  268. {
  269. logmsg("Successfully connected to Wi-Fi SSID \"",ssid,"\"");
  270. // blink LED 3 times when connected
  271. PICO_W_LED_OFF();
  272. for (uint8_t i = 0; i < 3; i++)
  273. {
  274. delay(PICO_W_SHORT_BLINK_DELAY);
  275. PICO_W_LED_ON();
  276. delay(PICO_W_SHORT_BLINK_DELAY);
  277. PICO_W_LED_OFF();
  278. }
  279. }
  280. }
  281. }
  282. #endif // ZULUSCSI_NETWORK