summaryrefslogtreecommitdiff
path: root/bsp/w60x/oneshot/oneshot_demo.c
blob: b40e121f161ed8ce18bbbc71390cc3677316af33 (plain)
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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-01-25     tyx          add w600
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "oneshot.h"

#define NET_READY_TIME_OUT       (rt_tick_from_millisecond(30 * 1000))

struct join_info
{
    char ssid[64];
    char passwd[64];
};

static int rt_wlan_device_connetct(char *ssid, char *passwd)
{
    int result = RT_EOK;
    rt_uint8_t time_cnt = 0;

    result = rt_wlan_connect(ssid, passwd);
    if (result != RT_EOK)
    {
        rt_kprintf("\nconnect ssid %s error:%d!\n", ssid, result);
        return result;
    };

    do
    {
        rt_thread_mdelay(1000);
        time_cnt ++;
        if (rt_wlan_is_ready())
        {
            break;
        }
    }
    while (time_cnt <= (NET_READY_TIME_OUT / 1000));

    if (time_cnt <= (NET_READY_TIME_OUT / 1000))
    {
        rt_kprintf("networking ready!\n");
    }
    else
    {
        rt_kprintf("wait ip got timeout!\n");
        result = -RT_ETIMEOUT;
    }

    return result;
}

static void join_wifi_thread(void *param)
{
    struct join_info *info = param;

    rt_kprintf("connect wifi:%s\n", info->ssid);
    if (rt_strlen(info->passwd) > 0)
    {
        rt_wlan_device_connetct(info->ssid, info->passwd);
    }
    else
    {
        rt_wlan_device_connetct(info->ssid, RT_NULL);
    }
    rt_free(info);
}

static void wm_oneshot_result_cb(int state, unsigned char *ssid, unsigned char *passwd)
{
    rt_thread_t tid;
    struct join_info *info;

    if (state != 0)
    {
        rt_kprintf("Receive wifi info timeout(%d). exit!\n", state);
        return;
    }
    if (ssid == RT_NULL)
    {
        rt_kprintf("SSID is NULL. exit!\n");
        return;
    }
    rt_kprintf("Receive ssid:%s passwd:%s\n", ssid == RT_NULL ? "" : ssid, passwd == RT_NULL ? "" : passwd);
    info = rt_malloc(sizeof(struct join_info));
    if (info == RT_NULL)
    {
        return;
    }
    rt_memset(info, 0, sizeof(struct join_info));
    rt_strncpy(info->ssid, ssid, sizeof(info->ssid));
    if (passwd)
    {
        rt_strncpy(info->passwd, passwd, sizeof(info->passwd));
    }
    tid = rt_thread_create("join", join_wifi_thread, info, 2048, 22, 20);
    if (tid == RT_NULL)
    {
        rt_free(info);
        return;
    }
    rt_thread_startup(tid);
}

void oneshot_demo(int argc, char *argv[])
{
    WM_ONESHOT_MODE mode = WM_UDP;

    if (argc > 2)
    {
        rt_kprintf("use: %s [web]", __FUNCTION__);
        return;
    }
    if (argc == 2)
    {
        if (rt_strcmp("web", argv[1]) == 0)
        {
            mode = WM_APWEB;
        }
        else if(rt_strcmp("udp", argv[1]) == 0)
        {
            mode = WM_UDP;
        }
        else
        {
            rt_kprintf("use: %s [web]", __FUNCTION__);
            return;
        }
    }
    if (wm_oneshot_start(mode, wm_oneshot_result_cb) == 0)
    {
        rt_kprintf("oneshot start...\n");
    }
    else
    {
        rt_kprintf("oneshot start failed\n");
    }
}

#ifdef RT_USING_FINSH
#include "finsh.h"
MSH_CMD_EXPORT(oneshot_demo, oneshot demo);
#endif