ZuluSCSI_platform_network.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. struct __attribute__((packed)) wifi_network_entry wifi_network_list[WIFI_NETWORK_LIST_ENTRY_COUNT] = { 0 };
  34. // A default DaynaPort-compatible MAC
  35. static const char defaultMAC[] = { 0x00, 0x80, 0x19, 0xc0, 0xff, 0xee };
  36. static bool network_in_use = false;
  37. bool platform_network_supported()
  38. {
  39. /* from cores/rp2040/RP2040Support.h */
  40. #if !defined(ARDUINO_RASPBERRY_PI_PICO_W)
  41. return false;
  42. #else
  43. extern bool __isPicoW;
  44. return __isPicoW;
  45. #endif
  46. }
  47. int platform_network_init(char *mac)
  48. {
  49. pico_unique_board_id_t board_id;
  50. uint8_t set_mac[6], read_mac[6];
  51. if (!platform_network_supported())
  52. return -1;
  53. // long signal blink at network initialization
  54. PICO_W_LED_OFF();
  55. PICO_W_LED_ON();
  56. delay(PICO_W_LONG_BLINK_DELAY);
  57. PICO_W_LED_OFF();
  58. logmsg(" ");
  59. logmsg("=== Network Initialization ===");
  60. memset(wifi_network_list, 0, sizeof(wifi_network_list));
  61. cyw43_deinit(&cyw43_state);
  62. cyw43_init(&cyw43_state);
  63. if (mac == NULL || (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 && mac[4] == 0 && mac[5] == 0))
  64. {
  65. mac = (char *)&set_mac;
  66. memcpy(mac, defaultMAC, sizeof(set_mac));
  67. // retain Dayna vendor but use a device id specific to this board
  68. pico_get_unique_board_id(&board_id);
  69. if (g_log_debug)
  70. logmsg_f("Unique board id: %02x %02x %02x %02x %02x %02x %02x %02x",
  71. board_id.id[0], board_id.id[1], board_id.id[2], board_id.id[3],
  72. board_id.id[4], board_id.id[5], board_id.id[6], board_id.id[7]);
  73. if (board_id.id[3] != 0 && board_id.id[4] != 0 && board_id.id[5] != 0)
  74. {
  75. mac[3] = board_id.id[3];
  76. mac[4] = board_id.id[4];
  77. mac[5] = board_id.id[5];
  78. }
  79. memcpy(scsiDev.boardCfg.wifiMACAddress, mac, sizeof(scsiDev.boardCfg.wifiMACAddress));
  80. }
  81. // setting the MAC requires libpico to be compiled with CYW43_USE_OTP_MAC=0
  82. memcpy(cyw43_state.mac, mac, sizeof(cyw43_state.mac));
  83. cyw43_arch_enable_sta_mode();
  84. cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, read_mac);
  85. logmsg_f("Wi-Fi MAC: %02X:%02X:%02X:%02X:%02X:%02X",
  86. read_mac[0], read_mac[1], read_mac[2], read_mac[3], read_mac[4], read_mac[5]);
  87. if (memcmp(mac, read_mac, sizeof(read_mac)) != 0)
  88. logmsg_f("WARNING: Wi-Fi MAC is not what was requested (%02x:%02x:%02x:%02x:%02x:%02x), is libpico not compiled with CYW43_USE_OTP_MAC=0?",
  89. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  90. network_in_use = true;
  91. return 0;
  92. }
  93. void platform_network_add_multicast_address(uint8_t *mac)
  94. {
  95. int ret;
  96. if ((ret = cyw43_wifi_update_multicast_filter(&cyw43_state, mac, true)) != 0)
  97. logmsg_f("%s: cyw43_wifi_update_multicast_filter: %d", __func__, ret);
  98. }
  99. bool platform_network_wifi_join(char *ssid, char *password)
  100. {
  101. int ret;
  102. if (!platform_network_supported())
  103. return false;
  104. if (password == NULL || password[0] == 0)
  105. {
  106. logmsg_f("Connecting to Wi-Fi SSID \"%s\" with no authentication", ssid);
  107. ret = cyw43_arch_wifi_connect_async(ssid, NULL, CYW43_AUTH_OPEN);
  108. }
  109. else
  110. {
  111. logmsg_f("Connecting to Wi-Fi SSID \"%s\" with WPA/WPA2 PSK", ssid);
  112. ret = cyw43_arch_wifi_connect_async(ssid, password, CYW43_AUTH_WPA2_MIXED_PSK);
  113. }
  114. if (ret != 0)
  115. {
  116. logmsg_f("Wi-Fi connection failed: %d", ret);
  117. }
  118. else
  119. {
  120. // Short single blink at start of connection sequence
  121. PICO_W_LED_OFF();
  122. delay(PICO_W_SHORT_BLINK_DELAY);
  123. PICO_W_LED_ON();
  124. delay(PICO_W_SHORT_BLINK_DELAY);
  125. PICO_W_LED_OFF();
  126. }
  127. return (ret == 0);
  128. }
  129. void platform_network_poll()
  130. {
  131. if (!network_in_use)
  132. return;
  133. scsiNetworkPurge();
  134. cyw43_arch_poll();
  135. }
  136. int platform_network_send(uint8_t *buf, size_t len)
  137. {
  138. int ret = cyw43_send_ethernet(&cyw43_state, 0, len, buf, 0);
  139. if (ret != 0)
  140. logmsg_f("cyw43_send_ethernet failed: %d", ret);
  141. return ret;
  142. }
  143. static int platform_network_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
  144. {
  145. struct wifi_network_entry *entry = NULL;
  146. if (!result || !result->ssid_len || !result->ssid[0])
  147. return 0;
  148. for (int i = 0; i < WIFI_NETWORK_LIST_ENTRY_COUNT; i++)
  149. {
  150. // take first available
  151. if (wifi_network_list[i].ssid[0] == '\0')
  152. {
  153. entry = &wifi_network_list[i];
  154. break;
  155. }
  156. // or if we've seen this network before, use this slot
  157. else if (strcmp((char *)result->ssid, wifi_network_list[i].ssid) == 0)
  158. {
  159. entry = &wifi_network_list[i];
  160. break;
  161. }
  162. }
  163. if (!entry)
  164. {
  165. // no available slots, insert according to our RSSI
  166. for (int i = 0; i < WIFI_NETWORK_LIST_ENTRY_COUNT; i++)
  167. {
  168. if (result->rssi > wifi_network_list[i].rssi)
  169. {
  170. // shift everything else down
  171. for (int j = WIFI_NETWORK_LIST_ENTRY_COUNT - 1; j > i; j--)
  172. wifi_network_list[j] = wifi_network_list[j - 1];
  173. entry = &wifi_network_list[i];
  174. memset(entry, 0, sizeof(struct wifi_network_entry));
  175. break;
  176. }
  177. }
  178. }
  179. if (entry == NULL)
  180. return 0;
  181. if (entry->rssi == 0 || result->rssi > entry->rssi)
  182. {
  183. entry->channel = result->channel;
  184. entry->rssi = result->rssi;
  185. }
  186. if (result->auth_mode & 7)
  187. entry->flags = WIFI_NETWORK_FLAGS_AUTH;
  188. strncpy(entry->ssid, (const char *)result->ssid, sizeof(entry->ssid));
  189. entry->ssid[sizeof(entry->ssid) - 1] = '\0';
  190. memcpy(entry->bssid, result->bssid, sizeof(entry->bssid));
  191. return 0;
  192. }
  193. int platform_network_wifi_start_scan()
  194. {
  195. if (cyw43_wifi_scan_active(&cyw43_state))
  196. return -1;
  197. cyw43_wifi_scan_options_t scan_options = { 0 };
  198. memset(wifi_network_list, 0, sizeof(wifi_network_list));
  199. return cyw43_wifi_scan(&cyw43_state, &scan_options, NULL, platform_network_wifi_scan_result);
  200. }
  201. int platform_network_wifi_scan_finished()
  202. {
  203. return !cyw43_wifi_scan_active(&cyw43_state);
  204. }
  205. void platform_network_wifi_dump_scan_list()
  206. {
  207. struct wifi_network_entry *entry = NULL;
  208. for (int i = 0; i < WIFI_NETWORK_LIST_ENTRY_COUNT; i++)
  209. {
  210. entry = &wifi_network_list[i];
  211. if (entry->ssid[0] == '\0')
  212. break;
  213. logmsg_f("wifi[%d] = %s, channel %d, rssi %d, bssid %02x:%02x:%02x:%02x:%02x:%02x, flags %d",
  214. i, entry->ssid, entry->channel, entry->rssi,
  215. entry->bssid[0], entry->bssid[1], entry->bssid[2],
  216. entry->bssid[3], entry->bssid[4], entry->bssid[5],
  217. entry->flags);
  218. }
  219. }
  220. int platform_network_wifi_rssi()
  221. {
  222. int32_t rssi = 0;
  223. cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_RSSI, sizeof(rssi), (uint8_t *)&rssi, CYW43_ITF_STA);
  224. return rssi;
  225. }
  226. char * platform_network_wifi_ssid()
  227. {
  228. struct ssid_t {
  229. uint32_t ssid_len;
  230. uint8_t ssid[32 + 1];
  231. } ssid;
  232. static char cur_ssid[32 + 1];
  233. memset(cur_ssid, 0, sizeof(cur_ssid));
  234. int ret = cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_SSID, sizeof(ssid), (uint8_t *)&ssid, CYW43_ITF_STA);
  235. if (ret)
  236. {
  237. logmsg_f("Failed getting Wi-Fi SSID: %d", ret);
  238. return NULL;
  239. }
  240. ssid.ssid[sizeof(ssid.ssid) - 1] = '\0';
  241. if (ssid.ssid_len < sizeof(ssid.ssid))
  242. ssid.ssid[ssid.ssid_len] = '\0';
  243. strlcpy(cur_ssid, (char *)ssid.ssid, sizeof(cur_ssid));
  244. return cur_ssid;
  245. }
  246. char * platform_network_wifi_bssid()
  247. {
  248. static char bssid[6];
  249. memset(bssid, 0, sizeof(bssid));
  250. /* TODO */
  251. return bssid;
  252. }
  253. int platform_network_wifi_channel()
  254. {
  255. int32_t channel = 0;
  256. cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_CHANNEL, sizeof(channel), (uint8_t *)&channel, CYW43_ITF_STA);
  257. return channel;
  258. }
  259. // these override weakly-defined functions in pico-sdk
  260. void cyw43_cb_process_ethernet(void *cb_data, int itf, size_t len, const uint8_t *buf)
  261. {
  262. scsiNetworkEnqueue(buf, len);
  263. }
  264. void cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf)
  265. {
  266. logmsg_f("Disassociated from Wi-Fi SSID \"%s\"", self->ap_ssid);
  267. }
  268. void cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf)
  269. {
  270. char *ssid = platform_network_wifi_ssid();
  271. if (ssid)
  272. {
  273. logmsg_f("Successfully connected to Wi-Fi SSID \"%s\"", ssid);
  274. // blink LED 3 times when connected
  275. PICO_W_LED_OFF();
  276. for (uint8_t i = 0; i < 3; i++)
  277. {
  278. delay(PICO_W_SHORT_BLINK_DELAY);
  279. PICO_W_LED_ON();
  280. delay(PICO_W_SHORT_BLINK_DELAY);
  281. PICO_W_LED_OFF();
  282. }
  283. }
  284. }
  285. }
  286. #endif // ZULUSCSI_NETWORK