ZuluSCSI_platform_network.cpp 8.8 KB

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