ZuluSCSI_platform_network.cpp 8.0 KB

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