|
| 1 | +#if DEVICE_WIFI |
| 2 | + |
| 3 | +#include "mbed_interface.h" |
| 4 | +#include "mbed_assert.h" |
| 5 | +#include "netsocket/nsapi_types.h" |
| 6 | + |
| 7 | +#include "wifi_emac.h" |
| 8 | + |
| 9 | +#include "cb_wlan_target_data.h" |
| 10 | +#include "cb_wlan_types.h" |
| 11 | +#include "cb_wlan.h" |
| 12 | +#include "cb_otp.h" |
| 13 | +#include "cb_main.h" |
| 14 | + |
| 15 | +#define WIFI_EMAC_MTU_SIZE (1500U) |
| 16 | +static const char _ifname[] = "WL0"; |
| 17 | + |
| 18 | +cb_boolean handleWlanTargetCopyFromDataFrame(uint8_t* buffer, cbWLANTARGET_dataFrame* frame, uint32_t size, uint32_t offsetInFrame); |
| 19 | +cb_boolean handleWlanTargetCopyToDataFrame(cbWLANTARGET_dataFrame* frame, uint8_t* buffer, uint32_t size, uint32_t offsetInFrame); |
| 20 | +cbWLANTARGET_dataFrame* handleWlanTargetAllocDataFrame(uint32_t size); |
| 21 | +void handleWlanTargetFreeDataFrame(cbWLANTARGET_dataFrame* frame); |
| 22 | +cb_uint32 handleWlanTargetGetDataFrameSize(cbWLANTARGET_dataFrame* frame); |
| 23 | +cb_uint8 handleWlanTargetGetDataFrameTID(cbWLANTARGET_dataFrame* frame); |
| 24 | +void handleWlanStatusIndication(void *dummy, cbWLAN_StatusIndicationInfo status, void *data); |
| 25 | +void handleWlanPacketIndication(void *dummy, cbWLAN_PacketIndicationInfo *packetInfo); |
| 26 | +void send_wlan_packet(void *buf); |
| 27 | + |
| 28 | +static const cbWLANTARGET_Callback _wlanTargetCallback = |
| 29 | +{ |
| 30 | + handleWlanTargetCopyFromDataFrame, |
| 31 | + handleWlanTargetCopyToDataFrame, |
| 32 | + handleWlanTargetAllocDataFrame, |
| 33 | + handleWlanTargetFreeDataFrame, |
| 34 | + handleWlanTargetGetDataFrameSize, |
| 35 | + handleWlanTargetGetDataFrameTID |
| 36 | +}; |
| 37 | + |
| 38 | +void handleWlanStatusIndication(void *dummy, cbWLAN_StatusIndicationInfo status, void *data) |
| 39 | +{ |
| 40 | + WIFI_EMAC &instance = WIFI_EMAC::get_instance(); |
| 41 | + bool linkUp = false; |
| 42 | + bool sendCb = true; |
| 43 | + (void)dummy; |
| 44 | + (void)data; |
| 45 | + |
| 46 | + switch (status) { |
| 47 | + case cbWLAN_STATUS_CONNECTED: |
| 48 | + case cbWLAN_STATUS_AP_STA_ADDED: |
| 49 | + linkUp = true; |
| 50 | + break; |
| 51 | + case cbWLAN_STATUS_STOPPED: |
| 52 | + case cbWLAN_STATUS_ERROR: |
| 53 | + case cbWLAN_STATUS_DISCONNECTED: |
| 54 | + case cbWLAN_STATUS_CONNECTION_FAILURE: |
| 55 | + break; |
| 56 | + case cbWLAN_STATUS_CONNECTING: |
| 57 | + default: |
| 58 | + sendCb = false; |
| 59 | + break; |
| 60 | + } |
| 61 | + if (sendCb && instance.emac_link_state_cb) { |
| 62 | + instance.emac_link_state_cb(linkUp); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void handleWlanPacketIndication(void *dummy, cbWLAN_PacketIndicationInfo *packetInfo) |
| 67 | +{ |
| 68 | + WIFI_EMAC &instance = WIFI_EMAC::get_instance(); |
| 69 | + (void)dummy; |
| 70 | + |
| 71 | + if (instance.emac_link_input_cb) { |
| 72 | + instance.emac_link_input_cb((void*)packetInfo->rxData); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +cb_boolean handleWlanTargetCopyFromDataFrame(uint8_t* buffer, cbWLANTARGET_dataFrame* frame, uint32_t size, uint32_t offsetInFrame) |
| 77 | +{ |
| 78 | + EMACMemoryManager *mem = WIFI_EMAC::get_instance().memory_manager; |
| 79 | + MBED_ASSERT(mem != NULL); |
| 80 | + |
| 81 | + //emac_mem_buf_t* phead = (emac_mem_buf_t *)frame; |
| 82 | + emac_mem_buf_t* pbuf = (emac_mem_buf_t *)frame; |
| 83 | + uint32_t copySize, bytesCopied = 0, pbufOffset = 0; |
| 84 | + |
| 85 | + MBED_ASSERT(frame != NULL); |
| 86 | + MBED_ASSERT(buffer != NULL); |
| 87 | + |
| 88 | + //pbuf = mem->get_next(phead); |
| 89 | + while (pbuf != NULL) { |
| 90 | + if ((pbufOffset + mem->get_len(pbuf)) >= offsetInFrame) { |
| 91 | + copySize = cb_MIN(size, mem->get_len(pbuf) - (offsetInFrame - pbufOffset)); |
| 92 | + memcpy(buffer, (int8_t *)mem->get_ptr(pbuf) + (offsetInFrame - pbufOffset), copySize); |
| 93 | + buffer += copySize; |
| 94 | + bytesCopied += copySize; |
| 95 | + pbuf = mem->get_next(pbuf); |
| 96 | + break; |
| 97 | + } |
| 98 | + pbufOffset += mem->get_len(pbuf); |
| 99 | + pbuf = mem->get_next(pbuf); |
| 100 | + } |
| 101 | + |
| 102 | + while (pbuf != NULL && bytesCopied < size) { |
| 103 | + copySize = cb_MIN(mem->get_len(pbuf), size - bytesCopied); |
| 104 | + memcpy(buffer, mem->get_ptr(pbuf), copySize); |
| 105 | + buffer += copySize; |
| 106 | + bytesCopied += copySize; |
| 107 | + pbuf = mem->get_next(pbuf); |
| 108 | + } |
| 109 | + |
| 110 | + MBED_ASSERT(bytesCopied <= size); |
| 111 | + |
| 112 | + return (bytesCopied == size); |
| 113 | +} |
| 114 | + |
| 115 | +cb_boolean handleWlanTargetCopyToDataFrame(cbWLANTARGET_dataFrame* frame, uint8_t* buffer, uint32_t size, uint32_t offsetInFrame) |
| 116 | +{ |
| 117 | + EMACMemoryManager *mem = WIFI_EMAC::get_instance().memory_manager; |
| 118 | + MBED_ASSERT(mem != NULL); |
| 119 | + |
| 120 | + //emac_mem_buf_t* phead = (emac_mem_buf_t *)frame; |
| 121 | + emac_mem_buf_t* pbuf = (emac_mem_buf_t *)frame; |
| 122 | + uint32_t copySize, bytesCopied = 0, pbufOffset = 0; |
| 123 | + |
| 124 | + MBED_ASSERT(frame != NULL); |
| 125 | + MBED_ASSERT(buffer != NULL); |
| 126 | + |
| 127 | + //pbuf = mem->get_next(phead); |
| 128 | + while (pbuf != NULL) { |
| 129 | + if ((pbufOffset + mem->get_len(pbuf)) >= offsetInFrame) { |
| 130 | + copySize = cb_MIN(size, mem->get_len(pbuf) - (offsetInFrame - pbufOffset)); |
| 131 | + memcpy((uint8_t *)mem->get_ptr(pbuf) + (offsetInFrame - pbufOffset), buffer, copySize); |
| 132 | + buffer += copySize; |
| 133 | + bytesCopied += copySize; |
| 134 | + pbuf = mem->get_next(pbuf); |
| 135 | + break; |
| 136 | + } |
| 137 | + pbufOffset += mem->get_len(pbuf); |
| 138 | + pbuf = mem->get_next(pbuf); |
| 139 | + } |
| 140 | + |
| 141 | + while (pbuf != NULL && bytesCopied < size) { |
| 142 | + copySize = cb_MIN(mem->get_len(pbuf), size - bytesCopied); |
| 143 | + memcpy(mem->get_ptr(pbuf), buffer, copySize); |
| 144 | + buffer += copySize; |
| 145 | + bytesCopied += copySize; |
| 146 | + pbuf = mem->get_next(pbuf); |
| 147 | + } |
| 148 | + |
| 149 | + MBED_ASSERT(bytesCopied <= size); |
| 150 | + |
| 151 | + return (bytesCopied == size); |
| 152 | +} |
| 153 | + |
| 154 | +cbWLANTARGET_dataFrame* handleWlanTargetAllocDataFrame(uint32_t size) |
| 155 | +{ |
| 156 | + EMACMemoryManager *mem = WIFI_EMAC::get_instance().memory_manager; |
| 157 | + MBED_ASSERT(mem != NULL); |
| 158 | + return (cbWLANTARGET_dataFrame*)mem->alloc_pool(size,0); |
| 159 | +} |
| 160 | + |
| 161 | +void handleWlanTargetFreeDataFrame(cbWLANTARGET_dataFrame* frame) |
| 162 | +{ |
| 163 | + EMACMemoryManager *mem = WIFI_EMAC::get_instance().memory_manager; |
| 164 | + MBED_ASSERT(mem != NULL); |
| 165 | + mem->free((emac_mem_buf_t*)frame); |
| 166 | +} |
| 167 | + |
| 168 | +uint32_t handleWlanTargetGetDataFrameSize(cbWLANTARGET_dataFrame* frame) |
| 169 | +{ |
| 170 | + EMACMemoryManager *mem = WIFI_EMAC::get_instance().memory_manager; |
| 171 | + MBED_ASSERT(mem != NULL); |
| 172 | + return mem->get_total_len((emac_mem_buf_t*)frame); |
| 173 | +} |
| 174 | + |
| 175 | +uint8_t handleWlanTargetGetDataFrameTID(cbWLANTARGET_dataFrame* frame) |
| 176 | +{ |
| 177 | + (void)frame; |
| 178 | + return (uint8_t)cbWLAN_AC_BE; |
| 179 | +} |
| 180 | + |
| 181 | +WIFI_EMAC::WIFI_EMAC() |
| 182 | +{ |
| 183 | + emac_link_input_cb = NULL; |
| 184 | + emac_link_state_cb = NULL; |
| 185 | + cbWLANTARGET_registerCallbacks((cbWLANTARGET_Callback*)&_wlanTargetCallback); |
| 186 | +} |
| 187 | + |
| 188 | +void send_wlan_packet(void *buf) |
| 189 | +{ |
| 190 | + cbWLAN_sendPacket(buf); |
| 191 | +} |
| 192 | + |
| 193 | +bool WIFI_EMAC::link_out(emac_mem_buf_t *buf) |
| 194 | +{ |
| 195 | + EMACMemoryManager *mem = WIFI_EMAC::get_instance().memory_manager; |
| 196 | + |
| 197 | + // Break call chain to avoid the driver affecting stack usage for the IP stack thread too much |
| 198 | + emac_mem_buf_t *new_buf = mem->alloc_pool(mem->get_total_len(buf), 0); |
| 199 | + if (new_buf != NULL) { |
| 200 | + mem->copy(new_buf, buf); |
| 201 | + int id = cbMAIN_getEventQueue()->call(send_wlan_packet, new_buf); |
| 202 | + if (id != 0) { |
| 203 | + cbMAIN_dispatchEventQueue(); |
| 204 | + } else { |
| 205 | + mem->free(new_buf); |
| 206 | + } |
| 207 | + } |
| 208 | + mem->free(buf); |
| 209 | + return true; |
| 210 | +} |
| 211 | + |
| 212 | +bool WIFI_EMAC::power_up() |
| 213 | +{ |
| 214 | + /* Initialize the hardware */ |
| 215 | + /* No-op at this stage */ |
| 216 | + return true; |
| 217 | +} |
| 218 | + |
| 219 | +uint32_t WIFI_EMAC::get_mtu_size() const |
| 220 | +{ |
| 221 | + return WIFI_EMAC_MTU_SIZE; |
| 222 | +} |
| 223 | + |
| 224 | +void WIFI_EMAC::get_ifname(char *name, uint8_t size) const |
| 225 | +{ |
| 226 | + memcpy(name, _ifname, (size < sizeof(_ifname)) ? size : sizeof(_ifname)); |
| 227 | +} |
| 228 | + |
| 229 | +uint8_t WIFI_EMAC::get_hwaddr_size() const |
| 230 | +{ |
| 231 | + return sizeof(cbWLAN_MACAddress); |
| 232 | +} |
| 233 | + |
| 234 | +bool WIFI_EMAC::get_hwaddr(uint8_t *addr) const |
| 235 | +{ |
| 236 | + cbOTP_read(cbOTP_MAC_WLAN, sizeof(cbWLAN_MACAddress), addr); |
| 237 | + return true; |
| 238 | +} |
| 239 | + |
| 240 | +void WIFI_EMAC::set_hwaddr(const uint8_t *addr) |
| 241 | +{ |
| 242 | + /* No-op at this stage */ |
| 243 | +} |
| 244 | + |
| 245 | +void WIFI_EMAC::set_link_input_cb(emac_link_input_cb_t input_cb) |
| 246 | +{ |
| 247 | + emac_link_input_cb = input_cb; |
| 248 | + |
| 249 | + cbMAIN_driverLock(); |
| 250 | + cbWLAN_registerPacketIndicationCallback(handleWlanPacketIndication, NULL); |
| 251 | + cbMAIN_driverUnlock(); |
| 252 | +} |
| 253 | + |
| 254 | +void WIFI_EMAC::set_link_state_cb(emac_link_state_change_cb_t state_cb) |
| 255 | +{ |
| 256 | + emac_link_state_cb = state_cb; |
| 257 | + |
| 258 | + cbMAIN_driverLock(); |
| 259 | + cbWLAN_registerStatusCallback(handleWlanStatusIndication, NULL); |
| 260 | + cbMAIN_driverUnlock(); |
| 261 | +} |
| 262 | + |
| 263 | +void WIFI_EMAC::power_down() |
| 264 | +{ |
| 265 | + /* No-op at this stage */ |
| 266 | +} |
| 267 | + |
| 268 | +void WIFI_EMAC::set_memory_manager(EMACMemoryManager &mem_mngr) |
| 269 | +{ |
| 270 | + memory_manager = &mem_mngr; |
| 271 | +} |
| 272 | + |
| 273 | +uint32_t WIFI_EMAC::get_align_preference() const |
| 274 | +{ |
| 275 | + return 1; // TODO not sure if there is a requirement but don't think so |
| 276 | +} |
| 277 | + |
| 278 | +void WIFI_EMAC::add_multicast_group(const uint8_t *address) |
| 279 | +{ |
| 280 | + // TODO anything to do here for WiFi? |
| 281 | +} |
| 282 | + |
| 283 | +void WIFI_EMAC::remove_multicast_group(const uint8_t *address) |
| 284 | +{ |
| 285 | + // TODO anything to do here for WiFi? |
| 286 | +} |
| 287 | + |
| 288 | +void WIFI_EMAC::set_all_multicast(bool all) |
| 289 | +{ |
| 290 | + // TODO anything to do here for WiFi? |
| 291 | +} |
| 292 | + |
| 293 | +WIFI_EMAC &WIFI_EMAC::get_instance() { |
| 294 | + static WIFI_EMAC emac; |
| 295 | + return emac; |
| 296 | +} |
| 297 | + |
| 298 | +// Weak so a module can override |
| 299 | +MBED_WEAK EMAC &EMAC::get_default_instance() |
| 300 | +{ |
| 301 | + return WIFI_EMAC::get_instance(); |
| 302 | +} |
| 303 | + |
| 304 | +#endif // DEVICE_WIFI |
0 commit comments