1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
/*
 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
 */

#ifndef HAL_WIFI_H
#define HAL_WIFI_H

#include <stdint.h>
#include <hal/base.h>
#include <k_api.h>
#ifdef STM32L475xx
#include "stm32_wifi.h"
#endif

typedef struct hal_wifi_module_s hal_wifi_module_t;

enum wlan_sec_type_e
{
    SECURITY_TYPE_NONE,       /* Open system. */
    SECURITY_TYPE_WEP,        /* Wired Equivalent Privacy. WEP security. */
    SECURITY_TYPE_WPA_TKIP,   /* WPA /w TKIP */
    SECURITY_TYPE_WPA_AES,    /* WPA /w AES */
    SECURITY_TYPE_WPA2_TKIP,  /* WPA2 /w TKIP */
    SECURITY_TYPE_WPA2_AES,   /* WPA2 /w AES */
    SECURITY_TYPE_WPA2_MIXED, /* WPA2 /w AES or TKIP */
    SECURITY_TYPE_AUTO, /* It is used when calling @ref micoWlanStartAdv, MICO
                           read security type from scan result. */
};

typedef struct
{
    char ssid[32 + 1];
    char ap_power;
} ap_list_t;

/*
 *  Scan result using normal scan.
 */
typedef struct
{
    char       ap_num; /* The number of access points found in scanning. */
    ap_list_t *ap_list;
} hal_wifi_scan_result_t;

typedef struct
{
    char    ssid[32 + 1]; /* The SSID of an access point. */
    char    ap_power;     /* Signal strength, min:0, max:100 */
    char    bssid[6];     /* The BSSID of an access point. */
    char    channel;      /* The RF frequency, 1-13 */
    uint8_t security;     /* Security type, @ref wlan_sec_type_t */
} ap_list_adv_t;

typedef struct
{
    char           ap_num; /* The number of access points found in scanning. */
    ap_list_adv_t *ap_list;
} hal_wifi_scan_result_adv_t;

typedef enum
{
    NOTIFY_STATION_UP = 1,
    NOTIFY_STATION_DOWN,

    NOTIFY_AP_UP,
    NOTIFY_AP_DOWN,
} hal_wifi_event_t;


typedef struct
{
#ifdef BOARD_MK3060 /* mk3060 lib use ssid[32], so ... */
    char ssid[32]; /* SSID of the wlan that needs to be connected. Example:
                      "SSID String". */
#else
    char ssid[32 + 1]; /* SSID of the wlan that needs to be connected. Example:
                          "SSID String". */
#endif
    char bssid[6];   /* BSSID of the wlan needs to be connected. Example: {0xC8
                        0x93 0x46 0x11 0x22 0x33}. */
    uint8_t channel; /* Wlan's RF frequency, channel 0-13. 1-13 means a fixed
                        channelthat can speed up a connection procedure, 0 is
                        not a fixed input means all channels are possible*/
    uint8_t security;
} hal_wifi_ap_info_adv_t;

typedef struct
{
    char wifi_mode;           /* DHCP mode: @ref wlanInterfaceTypedef. */
    char wifi_ssid[32 + 1];   /* SSID of the wlan needs to be connected. */
    char wifi_key[64 + 1];    /* Security key of the wlan needs to be connected,
                                 ignored in an open system. */
    char local_ip_addr[16];   /* Static IP configuration, Local IP address. */
    char net_mask[16];        /* Static IP configuration, Netmask. */
    char gateway_ip_addr[16]; /* Static IP configuration, Router IP address. */
    char dns_server_ip_addr[16]; /* Static IP configuration, DNS server IP
                                    address. */
    char dhcp_mode; /* DHCP mode, @ref DHCP_Disable, @ref DHCP_Client and @ref
                       DHCP_Server. */
    char reserved[32];
    int  wifi_retry_interval; /* Retry interval if an error is occured when
                                 connecting an access point,  time unit is
                                 millisecond. */
#ifdef STM32L475xx
    WIFI_Ecn_t access_sec;
#endif
} hal_wifi_init_type_t;


typedef struct
{
    hal_wifi_ap_info_adv_t ap_info;
    char                   key[64 + 1]; /* Security key or PMK of the wlan. */
    int                    key_len;     /* The length of the key. */
    char local_ip_addr[16];   /* Static IP configuration, Local IP address. */
    char net_mask[16];        /* Static IP configuration, Netmask. */
    char gateway_ip_addr[16]; /* Static IP configuration, Router IP address. */
    char dns_server_ip_addr[16]; /* Static IP configuration, DNS server IP
                                    address. */
    char dhcp_mode; /* DHCP mode, @ref DHCP_Disable, @ref DHCP_Client and @ref
                       DHCP_Server. */
    char reserved[32];
    int  wifi_retry_interval; /* Retry interval if an error is occured when
                                 connecting an access point, time unit is
                                 millisecond. */
} hal_wifi_init_type_adv_t;


typedef struct
{
    uint8_t dhcp;  /* DHCP mode: @ref DHCP_Disable, @ref DHCP_Client, @ref
                      DHCP_Server. */
    char ip[16];   /* Local IP address on the target wlan interface: @ref
                      wlanInterfaceTypedef. */
    char gate[16]; /* Router IP address on the target wlan interface: @ref
                      wlanInterfaceTypedef. */
    char mask[16]; /* Netmask on the target wlan interface: @ref
                      wlanInterfaceTypedef. */
    char dns[16];  /* DNS server IP address. */
    char mac[16];  /* MAC address, example: "C89346112233". */
    char broadcastip[16];
} hal_wifi_ip_stat_t;

typedef enum
{
    SOFT_AP, /* Act as an access point, and other station can connect, 4
                stations Max */
    STATION  /* Act as a station which can connect to an access point */
} hal_wifi_type_t;

enum
{
    DHCP_DISABLE = 0,
    DHCP_CLIENT,
    DHCP_SERVER,
};

typedef struct
{
    int is_connected;      /* The link to wlan is established or not, 0:
                              disconnected, 1: connected. */
    int     wifi_strength; /* Signal strength of the current connected AP */
    uint8_t ssid[32 + 1];  /* SSID of the current connected wlan */
    uint8_t bssid[6];      /* BSSID of the current connected wlan */
    int     channel;       /* Channel of the current connected wlan */
} hal_wifi_link_stat_t;

typedef struct hal_wifi_link_info_s
{
    int8_t rssi; /* rssi value of received packet */
} hal_wifi_link_info_t;

/*
 * The event call back function called at specific events occurred.
 *
 * @note For HAL implementors, these callbacks must be
 *       called under normal task context, not from interrupt.
 */
typedef struct
{
    void (*connect_fail)(hal_wifi_module_t *m, int err, void *arg);
    void (*ip_got)(hal_wifi_module_t *m, hal_wifi_ip_stat_t *pnet, void *arg);
    void (*stat_chg)(hal_wifi_module_t *m, hal_wifi_event_t stat, void *arg);
    void (*scan_compeleted)(hal_wifi_module_t *     m,
                            hal_wifi_scan_result_t *result, void *arg);
    void (*scan_adv_compeleted)(hal_wifi_module_t *         m,
                                hal_wifi_scan_result_adv_t *result, void *arg);
    void (*para_chg)(hal_wifi_module_t *m, hal_wifi_ap_info_adv_t *ap_info,
                     char *key, int key_len, void *arg);
    void (*fatal_err)(hal_wifi_module_t *m, void *arg);
} hal_wifi_event_cb_t;

typedef void (*monitor_data_cb_t)(uint8_t *data, int len,
                                  hal_wifi_link_info_t *info);

struct hal_wifi_module_s
{
    hal_module_base_t          base;
    const hal_wifi_event_cb_t *ev_cb;

    int (*init)(hal_wifi_module_t *m);
    void (*get_mac_addr)(hal_wifi_module_t *m, uint8_t *mac);
    void (*set_mac_addr)(hal_wifi_module_t *m, const uint8_t *mac);
    int (*start)(hal_wifi_module_t *m, hal_wifi_init_type_t *init_para);
    int (*start_adv)(hal_wifi_module_t *       m,
                     hal_wifi_init_type_adv_t *init_para_adv);
    int (*get_ip_stat)(hal_wifi_module_t *m, hal_wifi_ip_stat_t *out_net_para,
                       hal_wifi_type_t wifi_type);
    int (*get_link_stat)(hal_wifi_module_t *m, hal_wifi_link_stat_t *out_stat);
    void (*start_scan)(hal_wifi_module_t *m);
    void (*start_scan_adv)(hal_wifi_module_t *m);
    int (*power_off)(hal_wifi_module_t *m);
    int (*power_on)(hal_wifi_module_t *m);
    int (*suspend)(hal_wifi_module_t *m);
    int (*suspend_station)(hal_wifi_module_t *m);
    int (*suspend_soft_ap)(hal_wifi_module_t *m);
    int (*set_channel)(hal_wifi_module_t *m, int ch);
    int (*get_channel)(hal_wifi_module_t *m);
    int (*get_channel_list)(hal_wifi_module_t *m, const uint8_t **chnlist);
    void (*start_monitor)(hal_wifi_module_t *m);
    void (*stop_monitor)(hal_wifi_module_t *m);
    void (*register_monitor_cb)(hal_wifi_module_t *m, monitor_data_cb_t fn);
    void (*register_wlan_mgnt_monitor_cb)(hal_wifi_module_t *m,
                                          monitor_data_cb_t  fn);
    int (*wlan_send_80211_raw_frame)(hal_wifi_module_t *m, uint8_t *buf,
                                     int len);
    int  (*start_ap)(hal_wifi_module_t *m, const char *ssid, const char *passwd, int interval, int hide);
    int  (*stop_ap)(hal_wifi_module_t *m);

    /* debug related */
    void (*start_debug_mode)(hal_wifi_module_t *m);
    void (*stop_debug_mode)(hal_wifi_module_t *m);

    /* mesh related */
    void (*mesh_register_cb)(hal_wifi_module_t *m, monitor_data_cb_t fn);
    void (*mesh_set_bssid)(hal_wifi_module_t *m, const uint8_t *mac);
    int (*mesh_enable)(hal_wifi_module_t *m);
    int (*mesh_disable)(hal_wifi_module_t *m);
    int (*mesh_radio_sleep)(hal_wifi_module_t *m);
    int (*mesh_radio_wakeup)(hal_wifi_module_t *m);

#if (WIFI_CONFIG_SUPPORT_LOWPOWER > 0)
    int (*set_listeninterval)(hal_wifi_module_t *m, uint8_t listen_interval);
    int (*enter_powersave)(hal_wifi_module_t *m, uint8_t recvDTIMs);
    int (*exit_powersave)(hal_wifi_module_t *m);
#endif
};

/**
 * Get the default wifi instance.
 * The system may have more than one wifi instance,
 * this API returns the default one.
 *
 * @return  Instance pointer, or NULL if no instance registered.
 */
hal_wifi_module_t *hal_wifi_get_default_module(void);

/**
 * Regster a wifi instance to the HAL framework.
 *
 * @param[in]  m  the wifi instance.
 */
void hal_wifi_register_module(hal_wifi_module_t *m);

/**
 * Initialize wifi instances.
 *
 * @note  This is supposed to be called during system boot,
 *        not supposed to be called by user module directly.
 *
 * @return  0 on success, otherwise failure.
 */
int hal_wifi_init(void);

/**
 * Get the MAC address of the specified wifi instance.
 *
 * @param[in]   m    the wifi instance, NULL if default.
 * @param[out]  mac  the place to hold the result.
 *
 * @return  0 on success, otherwise failure.
 */
int hal_wifi_get_mac_addr(hal_wifi_module_t *m, uint8_t *mac);

/**
 * Set the MAC address of the specified wifi instance.
 *
 * @param[in]   m    the wifi instance, NULL if default.
 * @param[in]   mac  mac value
 *
 * @return  0 on success, otherwise failure.
 */
int hal_wifi_set_mac_addr(hal_wifi_module_t *m, const uint8_t *mac);

/**
 * Start the wifi instance.
 *
 * @param[in]  m          the wifi instance, NULL if default.
 * @param[in]  init_para  the config used to start the wifi.
 *
 * @return      0 on success, otherwise failure.
 */
int hal_wifi_start(hal_wifi_module_t *m, hal_wifi_init_type_t *init_para);

/**
 * Start the wifi instance in anvanced way (more config specified).
 *
 * @param[in]  m              the wifi instance, NULL if default.
 * @param[in]  init_para_adv  the advanced config used to start the wifi.
 *
 * @return      0 on success, otherwise failure.
 */
int hal_wifi_start_adv(hal_wifi_module_t *       m,
                       hal_wifi_init_type_adv_t *init_para_adv);

/**
 * Get the status of the specified wifi instance, e.g. the IP, mask, dhcp mode,
 * etc.
 *
 * @param[in]   m             the wifi instance, NULL if default.
 * @param[out]  out_net_para  the place to hold the results.
 * @param[in]   wifi_type     SOFT_AP or STATION.
 *
 * @return      0 on success, otherwise failure.
 */
int hal_wifi_get_ip_stat(hal_wifi_module_t *m, hal_wifi_ip_stat_t *out_net_para,
                         hal_wifi_type_t wifi_type);

/**
 * Get the link status of the wifi instance ,e.g. ssid, bssid, channel, rssi,
 * etc.
 *
 * @param[in]   m         the wifi instance, NULL if default.
 * @param[out]  out_stat  the place to hold the results.
 *
 * @return      0 on success, otherwise failure.
 */
int hal_wifi_get_link_stat(hal_wifi_module_t *   m,
                           hal_wifi_link_stat_t *out_stat);

/**
 * Start the scanning of the specified wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 */
void hal_wifi_start_scan(hal_wifi_module_t *m);

/**
 * Start the scanning of the specified wifi instance in advanced way.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 */
void hal_wifi_start_scan_adv(hal_wifi_module_t *m);

/**
 * Power off the wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 *
 * @return     0 on success, otherwise failure.
 */
int hal_wifi_power_off(hal_wifi_module_t *m);

/**
 * Power on the wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 *
 * @return     0 on success, otherwise failure.
 */
int hal_wifi_power_on(hal_wifi_module_t *m);

/**
 * Suspend the wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 *
 * @return     0 on success, otherwise failure.
 */
int hal_wifi_suspend(hal_wifi_module_t *m);

/**
 * Suspend the wifi instance in station mode.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 *
 * @return     0 on success, otherwise failure.
 */
int hal_wifi_suspend_station(hal_wifi_module_t *m);

/**
 * Suspend the wifi instance in soft_ap mode.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 *
 * @return     0 on success, otherwise failure.
 */
int hal_wifi_suspend_soft_ap(hal_wifi_module_t *m);

/**
 * Set the channel of the wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 *
 * @return     0 on success, otherwise failure.
 */
int hal_wifi_set_channel(hal_wifi_module_t *m, int ch);

/**
 * Get the channel of the wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 *
 * @return     -1 on failure, otherwise current channel number.
 */
int hal_wifi_get_channel(hal_wifi_module_t *m);

/**
 * Get the channel list of the wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 * @param[out]  chnlist  channel list in array
 *
 * @return     -1 on failure, otherwise number of available channels.
 */
int hal_wifi_get_channel_list(hal_wifi_module_t *m, const uint8_t **chnlist);

/**
 * Start the monitor mode of the wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 */
void hal_wifi_start_wifi_monitor(hal_wifi_module_t *m);

/**
 * Stop the monitor mode of the wifi instance.
 *
 * @param[in]  m  The wifi instance, NULL if default.
 */
void hal_wifi_stop_wifi_monitor(hal_wifi_module_t *m);

/**
 * Register the montior callback on the wifi instance.
 *
 * @param[in]  m   the wifi instance, NULL if default.
 * @param[in]  fn  the callback function.
 */
void hal_wifi_register_monitor_cb(hal_wifi_module_t *m, monitor_data_cb_t fn);

/**
 * Register management frame montior callback on the wifi instance.
 *
 * @param[in]  m   the wifi instance, NULL if default.
 * @param[in]  fn  the callback function.
 */
void hal_wlan_register_mgnt_monitor_cb(hal_wifi_module_t *m,
                                       monitor_data_cb_t  fn);

/**
 * Send 802.11 raw frame
 *
 * @param[in]  m   the wifi instance, NULL if default.
 * @param[in]  buf frame buffer.
 * @param[in]  len length of frame buffer.
 */
int hal_wlan_send_80211_raw_frame(hal_wifi_module_t *m, uint8_t *buf, int len);

/**
 * Start debug mode of the wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 */
void hal_wifi_start_debug_mode(hal_wifi_module_t *m);

/**
 * Stop debug mode of the wifi instance.
 *
 * @param[in]  m  the wifi instance, NULL if default.
 */
void hal_wifi_stop_debug_mode(hal_wifi_module_t *m);

/**
 * Set the event callback function array for the wifi.
 * Please don't do time consuming work in these callbacks.
 *
 * @note Please don't do time consuming work in these callbacks.
 *
 * @param[in]  m   the wifi instance, NULL for default.
 * @param[in]  cb  the event callback function info.
 */
void hal_wifi_install_event(hal_wifi_module_t *        m,
                            const hal_wifi_event_cb_t *cb);

/**
 * Regster a wifi instance to the uMesh
 *
 * @param[in]  m  the wifi instance.
 */
void hal_umesh_register_wifi(hal_wifi_module_t *m);

#if (WIFI_CONFIG_SUPPORT_LOWPOWER > 0)
/**
 * Set the event listen interval for the wifi.
 * 
 * @param[in]  m   the wifi instance, NULL for default.
 * @param[uint8_t]  listen_interval   the listen interval in power save mode.
 */
int hal_wifi_set_listeninterval(hal_wifi_module_t *m, uint8_t listen_interval);

/**
 * enter power save mode.
 * 
 * @param[in]  m   the wifi instance, NULL for default.
 * @param[uint8_t] recvDTIMs set 1 to receive DTIM, set 0 not to receive DTIM .
 */
int hal_wifi_enter_powersave(hal_wifi_module_t *m, uint8_t recvDTIMs);

/**
 * exit power save mode.
 * 
 * @param[in]  m   the wifi instance, NULL for default.
 */
int hal_wifi_exit_powersave(hal_wifi_module_t *m);

#endif

#endif /* HAL_WIFI_H */