打印

wiced studio

[复制链接]
800|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sd196815|  楼主 | 2020-6-13 21:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有人懂吗/** mesh_app.c
*
*/
#include "wiced.h"
#include "wiced_hal_nvram.h"
#include "wiced_hal_gpio.h"
#include "wiced_hal_wdog.h"
#include "wiced_bt_app_common.h"
#include "wiced_bt_trace.h"
#include "wiced_hal_puart.h"
#include "wiced_hal_gpio.h"
#include "wiced_hal_sflash.h"
#include "wiced_memory.h"
#include "wiced_hal_pwm.h"
#include "wiced_hal_aclk.h"
#include "wiced_hal_adc.h"
#include "mAPI.h"
#include "stdio.h"
#include "security_ota.h"
#include "string.h"
#include "wiced_hal_i2c.h"
#include "ads1015.h"


#ifdef CYW20735B1
//Tigerkin eval dongle(V2) configuration
#define BUTTON_PIN          WICED_P00
#define UART_TX_PIN         WICED_P05
#define UART_RX_PIN         WICED_P04

#define BOOT_COUNT_VS_ID    0x202
#else
//Dragon eval dongle(V1) configuration
#define BUTTON_PIN          WICED_P30
#define UART_TX_PIN         WICED_P31
#define UART_RX_PIN         WICED_P04

#define BOOT_COUNT_VS_ID    0x01
#endif

#define POLL_CENTRAL    1//中心,主要
#define POLL_INTERVAL   20      //5s poll interval  间隔

UINT8 boot_count;
uint32_t app_fine_count = 0;
uint8_t application_key[16] = {OTA_KEY};
uint8_t g_auth_size;
uint8_t g_auth_value[16];
uint8_t local_bt_address[6];
void uart_rx_cb(void *nosue)
{
    uint8_t read;

    while (wiced_hal_puart_read(&read))
    {
        //Demo:Input OOB
        WICED_BT_TRACE("rx:%x\n",read);
        g_auth_value[g_auth_size++] = read;
        if(g_auth_size == 16)
        {
            g_auth_size = 0;
            mAPI_sec2_input_OOB_auth_value(g_auth_value);
        }
    }

    wiced_hal_puart_reset_puart_interrupt();
}

void app_discoverable_timeout(void)
{
    wiced_hal_gpio_set_pin_output(WICED_P00, 1);
    //wiced_hal_gpio_set_pin_output(WICED_P27, 0);
    //wiced_hal_gpio_set_pin_output(WICED_P28, 0);
    //wiced_hal_gpio_set_pin_output(WICED_P29, 0);
}

void app_NetChanging_Handler(UINT8 type, UINT16 value)
{
    if (type == NET_CHANGE_TYPE_MESH_INFO)
    {
        if (value)
        {
            app_discoverable_timeout();
        }

        WICED_BT_TRACE("NET_CHANGE_TYPE_MESH_INFO %d\n", value);

    }
    else if (type == NET_CHANGE_TYPE_MESH_NUM)
    {
        WICED_BT_TRACE("NET_CHANGE_TYPE_MESH_NUM %d\n", value);
    }
}

void light_set(UINT8 x, UINT8 y, UINT8 status)
{
    wiced_result_t ret_status;
    if (x <= 20 && y <= 19)
    {
        wiced_hal_gpio_set_pin_output(WICED_P00, 0);
        wiced_hal_gpio_set_pin_output(WICED_P01, 0);
    }
    else if (x == 50 && y == 50)
    {
        wiced_hal_gpio_set_pin_output(WICED_P00, 1);
        wiced_hal_gpio_set_pin_output(WICED_P01, 1);
    }
    else if (x == 55 && y == 55)
    {
        wiced_hal_gpio_set_pin_output(WICED_P00, 0);
        wiced_hal_gpio_set_pin_output(WICED_P01, 0);
    }
}
//响应手机APP开关LED,调用函数light_set
//*dataPtr = 05 00 00 00 c0 00 32 32 00 00 00 00
void General_MSG_Handler(UINT8 *dataPtr, UINT8 dataLen)//接收所有响应
{
    UINT32 src_vaddr = *(UINT32 *) dataPtr;
    UINT8 i = 0;
    UINT8 message[14];
    for(i=0;i<14;i++)
        message[i] = *(dataPtr+5+i);
    UINT8 message_len = dataLen - 5;

    //UINT8 tx_msg[13];

    //UINT8 ad1_01channel[2];
    //UINT8 ad1_02channel[2];
    //UINT8 i2c_ok = 1;

    //*(ad1_01channel) = 0x32;
    //*(ad1_01channel + 1) = 0x67;
    //i2c_ok = Get_ads1015_Conversion_01Value(ad1_01channel,slave_address_GND);
    //i2c_ok = Get_ads1015_Conversion_23Value(ad1_02channel,slave_address_GND);
    //WICED_BT_TRACE("ad1_01channel %d,%d,%d,%d\n",*ad1_01channel,*(ad1_01channel+1),slave_address_GND,i2c_ok);
    //WICED_BT_TRACE("ad1_02channel %d,%d,%d,%d\n",*ad1_02channel,*(ad1_02channel+1),slave_address_GND,i2c_ok);

    //WICED_BT_TRACE("General_MSG_Handler %d\n %d\n %d\n", dataLen,*(message + 1),*(message + 2));
    //先打印“”,再打印 dataPtr
    //WICED_BT_TRACE_ARRAY(dataPtr, dataLen, "::");
    //WICED_BT_TRACE_ARRAY(message, message_len, "::");

    WICED_BT_TRACE("::");
    //WICED_BT_TRACE("%B",message);
    for(i=0;i<message_len;i++)
    {
        WICED_BT_TRACE("%02x",message[i]);
    }
    WICED_BT_TRACE("\r\n");
    /*
    switch (messageID)
    {
    case 0xc0:
        light_set(*(message + 1), *(message + 2), 1);
        break;
    case 0xc1:
        {
        //demo:reply bt address
        UINT32 vaddr = mesh_get_Local_VirtualAddr();
        UINT8 poll_response_data[11];
        memcpy(poll_response_data,&vaddr,4);
        poll_response_data[4] = 0xc2;
        memcpy(poll_response_data+5,local_bt_address,6);
        mAPI_SendData(src_vaddr,0x20,poll_response_data,11);
        }
        break;
    case 0xc2:
        WICED_BT_TRACE("poll res %B:\n", message);
        break;
    }
    */
}
UINT8 static_oob[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
//Demo:if output OOB is selected to config meshinfo
void out_oob_handler(UINT8 action,UINT8 *auth_value)
{
    if(action!=OUT_OOB_ACTION_OUTPU_ALPHANUMERIC)
    {
        UINT32 numeric_digit = *(UINT32 *)auth_value;
        WICED_BT_TRACE("out action:%d,value:%d\n",action,numeric_digit);
    }
    else
        WICED_BT_TRACE("out ALPHANUMERIC:%s\n",auth_value);
}

//Demo:if input OOB is selected to config meshinfo
void input_oob_handler(UINT8 action,UINT8 size)
{
    WICED_BT_TRACE("input action:%d,digit size:%d\n", action, size);
    g_auth_size = 0;
}
static void mlib4_interrupt_handler(void* user_data, uint8_t pin)
{
    uint32_t value = wiced_hal_gpio_get_pin_input_status(pin);
    WICED_BT_TRACE("interrupt_handler: pin:%d value:%d\n", pin, value);

    if (value == 0)
    {
        tsec2_capabilites iocap;
        //support static oob
        iocap.static_oob_type = 1;
        memcpy(iocap.static_oob_data,static_oob,16);
        //support all output oob,max size 8
        iocap.output_oob_size = 8;
        iocap.output_oob_action = ALL_OUTPUT_OOB_ACTION;
        iocap.out_oob_cb = out_oob_handler;
        //support all input oob,max size 8
        iocap.input_oob_size = 8;
        iocap.input_oob_action = ALL_INPUT_OOB_ACTION;
        iocap.input_oob_cb = input_oob_handler;
#if 0   //if need high security,new API to enter discoverable mode,more details,see description of this API
        mAPI_Discoverable_ext(60,app_discoverable_timeout,SECURITY_LEVEL_NONE,&iocap);
#else
        mAPI_Discoverable(60, app_discoverable_timeout);
#endif
    }
}

extern uint32_t malloc_num;
extern uint32_t malloc_max;

void app_NormalTimer(UINT32 count)//1s
{
    //WICED_BT_TRACE("~%d malloc num[current][max]:[%d][%d],over flow:%d\n",count,malloc_num,malloc_max,wiced_bt_did_stack_overflow());
    WICED_BT_TRACE("~%d m1\n",count);

    if (MESH_STATE_DISCOVERABLE == mAPI_get_state())
    {
        if (count % 2){
            WICED_BT_TRACE("~%d \n",count);
            light_set(55, 55, 0);
        }
        else{
            WICED_BT_TRACE("~%d \n",count);
            light_set(50, 50, 0);
        }
    }


    if (count == 2)
    {
        wiced_result_t ret_status;
        if (boot_count >= 3)
            mAPI_Discoverable(120, app_discoverable_timeout);

        boot_count = 0;
        wiced_hal_write_nvram(BOOT_COUNT_VS_ID, 1, &boot_count, &ret_status);
    }

#if POLL_CENTRAL
    if((count%POLL_INTERVAL) == 0)
    {
        UINT32 vaddr = mesh_get_Local_VirtualAddr();
        UINT8 poll_data[7];
        memcpy(poll_data,&vaddr,4);
        poll_data[4] = 0xc1;
        poll_data[5] = 0x00;
        if(((count/POLL_INTERVAL)%2) == 0)
        {
            poll_data[6] = 55;
            poll_data[7] = 55;
        }
        else
        {
            poll_data[6] = 50;
            poll_data[7] = 50;
        }
        mAPI_SendData(0xFFFFFFFF,0x20,poll_data,8);
    }
#endif
}

void app_FineTimer(UINT32 count)//10ms
{
    app_fine_count++;
}

void mesh_app_init(tInitInfo *init)
{
    UINT8 event_type;
    wiced_result_t status;
    wiced_bt_stack_check_init();
#if 0
    //debug to puart
    wiced_hal_puart_init();
    wiced_hal_puart_flow_off();
    wiced_hal_puart_select_uart_pads(UART_RX_PIN, UART_TX_PIN, 0, 0);
    wiced_set_debug_uart(WICED_ROUTE_DEBUG_TO_PUART);
    wiced_hal_puart_set_baudrate(921600);
    wiced_hal_puart_enable_tx();
    wiced_hal_puart_enable_rx();
    wiced_hal_puart_register_interrupt(uart_rx_cb);
#ifdef CYW20735B1
    wiced_hal_puart_set_watermark_level(1);
#endif
#else
    //debug to HCI uart
    //baudrate 115200,hardware flow control(CTS/RTS)
    wiced_set_debug_uart(WICED_ROUTE_DEBUG_TO_HCI_UART);
#endif

    WICED_BT_TRACE("mesh status:%d, id :%d\n", init->mesh_status,init->id);
    WICED_BT_TRACE_ARRAY(init->password, init->password_len, "password:");

    WICED_BT_TRACE("device address:%B\n",init->local_bd_addr);
    WICED_BT_TRACE("mlib version:%s\n",init->lib_version);


    //set the firmware version
    mAPI_set_Versions(APP_ID, VERSION_MAJOR, VERSION_MINOR);
    WICED_BT_TRACE("mesh_app_init left RAM %d\n", wiced_memory_get_free_bytes());


    mAPI_register_NetChanges((NET_CHANGES_CB) app_NetChanging_Handler);

    for (event_type = 0x20; event_type < 0x40; event_type++)
        mAPI_register_DataEvent_Cb(event_type, (MESH_DATAEVENT_CB) General_MSG_Handler);

    wiced_hal_gpio_register_pin_for_interrupt(BUTTON_PIN, mlib4_interrupt_handler, NULL);
    wiced_hal_gpio_configure_pin(BUTTON_PIN, GPIO_INPUT_ENABLE | GPIO_PULL_UP | GPIO_EN_INT_FALLING_EDGE, GPIO_PIN_OUTPUT_LOW);

    wiced_hal_gpio_configure_pin(WICED_P00, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_LOW);
    wiced_hal_gpio_configure_pin(WICED_P01, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_LOW);
    //wiced_hal_gpio_configure_pin(WICED_P26, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_LOW);
    //wiced_hal_gpio_configure_pin(WICED_P27, GPIO_OUTPUT_ENABLE, GPIO_PIN_OUTPUT_LOW);
    wiced_hal_gpio_configure_pin(WICED_P28, GPIO_INPUT_ENABLE, GPIO_PIN_OUTPUT_LOW);
    wiced_hal_gpio_configure_pin(WICED_P29, GPIO_INPUT_ENABLE, GPIO_PIN_OUTPUT_LOW);

    mAPI_register_Timer((MAPI_TIMER_CB) app_NormalTimer, (MAPI_TIMER_CB) app_FineTimer);

    if (wiced_hal_read_nvram(BOOT_COUNT_VS_ID, 1, &boot_count, &status) == 0)
        boot_count = 0;

    boot_count++;

    wiced_hal_write_nvram(BOOT_COUNT_VS_ID, 1, &boot_count, &status);


    mAPI_enable_security_ota(application_key);

    memcpy(local_bt_address,init->local_bd_addr,6);

    wiced_hal_i2c_init();
    wiced_hal_i2c_select_pads(WICED_P26, WICED_P27);
    //wiced_hal_i2c_set_speed(I2CM_SPEED_800KHZ);
}

UINT8 app_read_third_part_local_addr(UINT8 addr[])
{
    // keep return 0 unless you want to use a third part MAC.
    return 0;
}

使用特权

评论回复
沙发
sd196815|  楼主 | 2020-6-13 21:35 | 只看该作者

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

8

主题

20

帖子

0

粉丝