LWIPBP3595Interface library for mbed-os.

Dependents:   LWIPBP3595Interface_STA_for_mbed-os

Fork of LWIPBP3595Interface by Rohm

LWIPBP3595Interface.cpp

Committer:
dkato
Date:
2016-09-13
Revision:
2:c7e325599570
Parent:
0:a933851e5d22
Child:
3:2ff2514e4fca

File content as of revision 2:c7e325599570:

/* LWIP implementation of NetworkInterfaceAPI
 * Copyright (c) 2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "LWIPBP3595Interface.h"
#include "LWIPBP3595Interface_BssType.h"
#include "lwip_wifi_stack.h"
#include "WlanBP3595.h"

static void _wlan_inf_callback(uint8_t ucType, uint16_t usWid, uint16_t usSize, uint8_t *pucData) {
    if ((ucType == 'I') && (usWid == 0x0005)) {
        if (pucData[0] == 0x01) {     // CONNECTED
            /* Notify the EthernetInterface driver that WLAN was connected */
            WlanBP3595_Connected();
        } else {
            /* Notify the EthernetInterface driver that WLAN was disconnected */
            WlanBP3595_Disconnected();
        }
    }
}

static int _wlan_init() {
    uint32_t status;

    /* Initialize WlanBP3595 */
    if (WlanBP3595_Init(&_wlan_inf_callback) != 0) {
        return -1;
    }

    /* Wait until WLAN_BP3595_START  timeout 60s */
    while (1) {
        Thread::wait(200);
        status = WlanBP3595_GetWlanSts();
        if (status == WLAN_BP3595_START) {
            break;
        }
    }

    return 0;
}

static int _wlan_setting(const char *ssid, const char *pass, nsapi_security_t security)
{
    int     ret;
    grp_u8  ucWidData8;     // 8bit wid data
    grp_wld_byte_array  tBAWidData;     // byte array wid data

    // Set BSS type
    ucWidData8 = BSS_TYPE;
    ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_BSS_TYPE, &ucWidData8);
    if (ret != 0) {
        return -1;
    }

    // Set SSID
    tBAWidData.pucData = (grp_u8 *)ssid;
    tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
    ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_SSID, &tBAWidData);
    if (ret != 0) {
        return -1;
    }

    if ((security == NSAPI_SECURITY_WPA) || (security == NSAPI_SECURITY_WPA2)) {
        // Set PSK
        tBAWidData.pucData = (grp_u8 *)pass;
        tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
        ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_PSK, &tBAWidData);
        if (ret != 0) {
            return -1;
        }
    }

    // Set 11i mode
    switch (security) {
        case NSAPI_SECURITY_WEP:
            ret = strlen(pass);
            if (ret == 5) {
                ucWidData8 = 0x03;  // WEP64
            } else if (ret == 13) {
                ucWidData8 = 0x07;  // WEP128
            } else {
                return -1;
            }
            break;
        case NSAPI_SECURITY_WPA:
        case NSAPI_SECURITY_WPA2:
            ucWidData8 = 0x79;  // WPA/WPA2 Mixed
            break;
        case NSAPI_SECURITY_NONE:
        default:
            ucWidData8 = 0x00;
            break;
    }
    ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_11I_MODE, &ucWidData8);
    if (ret != 0) {
        return -1;
    }

    if (security == NSAPI_SECURITY_WEP) {
        // Set WEP KEY
        tBAWidData.pucData = (grp_u8 *)pass;
        tBAWidData.ulSize  = strlen((char *)tBAWidData.pucData);
        ret = WlanBP3595_Ioctl(GRP_WLD_IOCTL_SET_WEP_KEY, &tBAWidData);
        if (ret != 0) {
            return -1;
        }
    }

    return 0;
}

/* Interface implementation */
int LWIPBP3595Interface::connect(const char *ssid, const char *pass, nsapi_security_t security)
{
    _wlan_init();
    if (lwip_wifi_init() == 0) {
        _wlan_setting(ssid, pass, security);
    }

    return lwip_wifi_bringup();
}

int LWIPBP3595Interface::disconnect()
{
    lwip_wifi_bringdown();
    return 0;
}

const char *LWIPBP3595Interface::get_ip_address()
{
    return lwip_wifi_get_ip_address();
}

const char *LWIPBP3595Interface::get_mac_address()
{
    return lwip_wifi_get_mac_address();
}

NetworkStack *LWIPBP3595Interface::get_stack()
{
    return nsapi_create_stack(&lwip_wifi_stack);
}