modwlan.c 47.6 KB
Newer Older
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
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Daniel Campora
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "std.h"
#include <stdint.h>
#include <stdbool.h>

#include "simplelink.h"
32
#include "py/mpconfig.h"
33
#include MICROPY_HAL_H
34
35
#include "py/obj.h"
#include "py/runtime.h"
36
37
38
39
#include "modnetwork.h"
#include "modwlan.h"
#include "pybioctl.h"
#include "debug.h"
40
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
41
#include "serverstask.h"
42
#endif
43
#include "mpexception.h"
44
45
#include "mpcallback.h"
#include "pybsleep.h"
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


/******************************************************************************
 DEFINE TYPES
 ******************************************************************************/
// Status bits - These are used to set/reset the corresponding bits in a given variable
typedef enum{
    STATUS_BIT_NWP_INIT = 0,        // If this bit is set: Network Processor is
                                    // powered up

    STATUS_BIT_CONNECTION,          // If this bit is set: the device is connected to
                                    // the AP or client is connected to device (AP)

    STATUS_BIT_IP_LEASED,           // If this bit is set: the device has leased IP to
                                    // any connected client

    STATUS_BIT_IP_ACQUIRED,          // If this bit is set: the device has acquired an IP

    STATUS_BIT_SMARTCONFIG_START,   // If this bit is set: the SmartConfiguration
                                    // process is started from SmartConfig app

    STATUS_BIT_P2P_DEV_FOUND,       // If this bit is set: the device (P2P mode)
                                    // found any p2p-device in scan

    STATUS_BIT_P2P_REQ_RECEIVED,    // If this bit is set: the device (P2P mode)
                                    // found any p2p-negotiation request

    STATUS_BIT_CONNECTION_FAILED,   // If this bit is set: the device(P2P mode)
                                    // connection to client(or reverse way) is failed

    STATUS_BIT_PING_DONE            // If this bit is set: the device has completed
                                    // the ping operation
}e_StatusBits;

typedef struct _wlan_obj_t {
81
82
83
    mp_obj_base_t       base;
    SlWlanMode_t        mode;
    uint32_t            status;
84

85
86
87
    uint32_t            ip;
    uint32_t            gateway;
    uint32_t            dns;
88

89
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
90
    bool                servers_enabled;
91
#endif
92
93
94
95
    uint8_t             security;
    uint8_t             mac[SL_MAC_ADDR_LEN];
    uint8_t             ssid[33];
    uint8_t             bssid[6];
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
} wlan_obj_t;

/******************************************************************************
 DEFINE CONSTANTS
 ******************************************************************************/
#define CLR_STATUS_BIT_ALL(status)      (status = 0)
#define SET_STATUS_BIT(status, bit)     (status |= ( 1 << (bit)))
#define CLR_STATUS_BIT(status, bit)     (status &= ~(1 << (bit)))
#define GET_STATUS_BIT(status, bit)     (0 != (status & (1 << (bit))))

#define IS_NW_PROCSR_ON(status)         GET_STATUS_BIT(status, STATUS_BIT_NWP_INIT)
#define IS_CONNECTED(status)            GET_STATUS_BIT(status, STATUS_BIT_CONNECTION)
#define IS_IP_LEASED(status)            GET_STATUS_BIT(status, STATUS_BIT_IP_LEASED)
#define IS_IP_ACQUIRED(status)          GET_STATUS_BIT(status, STATUS_BIT_IP_ACQUIRED)
#define IS_SMART_CFG_START(status)      GET_STATUS_BIT(status, STATUS_BIT_SMARTCONFIG_START)
#define IS_P2P_DEV_FOUND(status)        GET_STATUS_BIT(status, STATUS_BIT_P2P_DEV_FOUND)
#define IS_P2P_REQ_RCVD(status)         GET_STATUS_BIT(status, STATUS_BIT_P2P_REQ_RECEIVED)
#define IS_CONNECT_FAILED(status)       GET_STATUS_BIT(status, STATUS_BIT_CONNECTION_FAILED)
#define IS_PING_DONE(status)            GET_STATUS_BIT(status, STATUS_BIT_PING_DONE)

#define MODWLAN_SL_SCAN_ENABLE          1
#define MODWLAN_SL_SCAN_DISABLE         0
#define MODWLAN_SL_MAX_NETWORKS         20

#define MODWLAN_TIMEOUT_MS              5000
#define MODWLAN_MAX_NETWORKS            20
123
#define MODWLAN_SCAN_PERIOD_S           300     // 5 minutes
124
#define MODWLAN_WAIT_FOR_SCAN_MS        1050
125
126
127
128
129
130

#define ASSERT_ON_ERROR( x )            ASSERT((x) >= 0 )

#define IPV4_ADDR_STR_LEN_MAX           (16)

#define WLAN_MAX_RX_SIZE                16000
131
132
#define WLAN_MAX_TX_SIZE                1476

Daniel Campora's avatar
Daniel Campora committed
133
134
#define MODWLAN_IP_MODE_DYNAMIC         0
#define MODWLAN_IP_MODE_STATIC          1
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

#define MAKE_SOCKADDR(addr, ip, port)       sockaddr addr; \
                                            addr.sa_family = AF_INET; \
                                            addr.sa_data[0] = port >> 8; \
                                            addr.sa_data[1] = port; \
                                            addr.sa_data[2] = ip[0]; \
                                            addr.sa_data[3] = ip[1]; \
                                            addr.sa_data[4] = ip[2]; \
                                            addr.sa_data[5] = ip[3];

#define UNPACK_SOCKADDR(addr, ip, port)     port = (addr.sa_data[0] << 8) | addr.sa_data[1]; \
                                            ip[0] = addr.sa_data[2]; \
                                            ip[1] = addr.sa_data[3]; \
                                            ip[2] = addr.sa_data[4]; \
                                            ip[3] = addr.sa_data[5];

/******************************************************************************
152
 DECLARE PRIVATE DATA
153
 ******************************************************************************/
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
STATIC wlan_obj_t wlan_obj = {
        .mode = -1,
        .status = 0,
        .ip = 0,
        .gateway = 0,
        .dns = 0,
    #if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
        .servers_enabled = false,
    #endif
        .security = SL_SEC_TYPE_OPEN,
        .ssid = {0},
        .bssid = {0},
        .mac = {0},
};

169
STATIC const mp_cb_methods_t wlan_cb_methods;
170
171

/******************************************************************************
172
 DECLARE PUBLIC DATA
173
 ******************************************************************************/
174
OsiLockObj_t wlan_LockObj;
175
176
177
178
179
180

/******************************************************************************
 DECLARE PRIVATE FUNCTIONS
 ******************************************************************************/
STATIC void wlan_initialize_data (void);
STATIC void wlan_reenable (SlWlanMode_t mode);
Daniel Campora's avatar
Daniel Campora committed
181
182
STATIC void wlan_servers_start (void);
STATIC void wlan_servers_stop (void);
183
184
185
STATIC void wlan_get_sl_mac (void);
STATIC modwlan_Status_t wlan_do_connect (const char* ssid, uint32_t ssid_len, const char* bssid, uint8_t sec,
                                         const char* key, uint32_t key_len);
186
187
STATIC void wlan_lpds_callback_enable (mp_obj_t self_in);
STATIC void wlan_lpds_callback_disable (mp_obj_t self_in);
188
189
190
191
192
193
194
195
196
197

//*****************************************************************************
//
//! \brief The Function Handles WLAN Events
//!
//! \param[in]  pWlanEvent - Pointer to WLAN Event Info
//!
//! \return None
//!
//*****************************************************************************
198
void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent) {
199
    if (!pWlanEvent) {
200
201
202
203
204
205
206
207
        return;
    }

    switch(pWlanEvent->Event)
    {
        case SL_WLAN_CONNECT_EVENT:
        {
            SET_STATUS_BIT(wlan_obj.status, STATUS_BIT_CONNECTION);
208

209
210
            slWlanConnectAsyncResponse_t *pEventData = &pWlanEvent->EventData.STAandP2PModeWlanConnected;

211
            // copy the new connection data
212
            memcpy(wlan_obj.ssid, pEventData->ssid_name, pEventData->ssid_len);
213
214
215
216
217
218
219
220
            memcpy(wlan_obj.bssid, pEventData->bssid, SL_BSSID_LENGTH);
        }
            break;
        case SL_WLAN_DISCONNECT_EVENT:
        {
            CLR_STATUS_BIT(wlan_obj.status, STATUS_BIT_CONNECTION);
            CLR_STATUS_BIT(wlan_obj.status, STATUS_BIT_IP_ACQUIRED);

221
            memset(wlan_obj.ssid, 0, sizeof(wlan_obj.ssid));
222
223
224
225
            memset(wlan_obj.bssid, 0, sizeof(wlan_obj.bssid));
        }
            break;
        case SL_WLAN_STA_CONNECTED_EVENT:
226
            // TODO
227
228
            break;
        case SL_WLAN_STA_DISCONNECTED_EVENT:
229
            // TODO
230
231
            break;
        case SL_WLAN_P2P_DEV_FOUND_EVENT:
232
            // TODO
233
234
            break;
        case SL_WLAN_P2P_NEG_REQ_RECEIVED_EVENT:
235
            // TODO
236
237
            break;
        case SL_WLAN_CONNECTION_FAILED_EVENT:
238
            // TODO
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
            break;
        default:
            break;
    }
}

//*****************************************************************************
//
//! \brief This function handles network events such as IP acquisition, IP
//!           leased, IP released etc.
//!
//! \param[in]  pNetAppEvent - Pointer to NetApp Event Info
//!
//! \return None
//!
//*****************************************************************************
255
void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent) {
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    if(!pNetAppEvent) {
        return;
    }

    switch(pNetAppEvent->Event)
    {
        case SL_NETAPP_IPV4_IPACQUIRED_EVENT:
        {
            SlIpV4AcquiredAsync_t *pEventData = NULL;

            SET_STATUS_BIT(wlan_obj.status, STATUS_BIT_IP_ACQUIRED);

            // Ip Acquired Event Data
            pEventData = &pNetAppEvent->EventData.ipAcquiredV4;

271
            // Get ip, gateway and dns
Daniel Campora's avatar
Daniel Campora committed
272
273
274
            wlan_obj.gateway = pEventData->gateway;
            wlan_obj.ip      = pEventData->ip;
            wlan_obj.dns     = pEventData->dns;
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
        }
            break;
        case SL_NETAPP_IPV6_IPACQUIRED_EVENT:
            break;
        case SL_NETAPP_IP_LEASED_EVENT:
            break;
        case SL_NETAPP_IP_RELEASED_EVENT:
            break;
        default:
            break;
    }
}


//*****************************************************************************
//
//! \brief This function handles HTTP server events
//!
//! \param[in]  pServerEvent - Contains the relevant event information
//! \param[in]    pServerResponse - Should be filled by the user with the
//!                                      relevant response information
//!
//! \return None
//!
//****************************************************************************
300
void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pHttpEvent, SlHttpServerResponse_t *pHttpResponse) {
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    if (!pHttpEvent) {
        return;
    }

    switch (pHttpEvent->Event) {
    case SL_NETAPP_HTTPGETTOKENVALUE_EVENT:
        break;
    case SL_NETAPP_HTTPPOSTTOKENVALUE_EVENT:
        break;
    default:
        break;
    }
}

//*****************************************************************************
//
//! \brief This function handles General Events
//!
//! \param[in]     pDevEvent - Pointer to General Event Info
//!
//! \return None
//!
//*****************************************************************************
324
void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent) {
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
    if (!pDevEvent) {
        return;
    }
}


//*****************************************************************************
//
//! This function handles socket events indication
//!
//! \param[in]      pSock - Pointer to Socket Event Info
//!
//! \return None
//!
//*****************************************************************************
340
void SimpleLinkSockEventHandler(SlSockEvent_t *pSock) {
341
342
343
344
345
346
    if (!pSock) {
        return;
    }

    switch( pSock->Event ) {
    case SL_SOCKET_TX_FAILED_EVENT:
347
348
349
350
351
352
        switch( pSock->socketAsyncEvent.SockTxFailData.status) {
        case SL_ECLOSE:
            break;
        default:
          break;
        }
353
        break;
354
355
356
357
358
359
360
    case SL_SOCKET_ASYNC_EVENT:
         switch(pSock->socketAsyncEvent.SockAsyncData.type) {
         case SSL_ACCEPT:
             break;
         case RX_FRAGMENTATION_TOO_BIG:
             break;
         case OTHER_SIDE_CLOSE_SSL_DATA_NOT_ENCRYPTED:
361
             break;
362
363
364
         default:
             break;
         }
365
        break;
366
367
    default:
      break;
368
369
370
371
372
373
374
375
    }
}

//*****************************************************************************
// SimpleLink Asynchronous Event Handlers -- End
//*****************************************************************************

void wlan_init0 (void) {
376
    // create the wlan lock
377
    ASSERT(OSI_OK == sl_LockObjCreate(&wlan_LockObj, "WlanLock"));
378
379
380
381
382
383
384
385
386
387
388
389
390
}

void wlan_first_start (void) {
    // clear wlan data after checking any of the status flags
    wlan_initialize_data();

    if (wlan_obj.mode < 0) {
        wlan_obj.mode = sl_Start(0, 0, 0);
        sl_LockObjUnlock (&wlan_LockObj);
    }

    // get the mac address
    wlan_get_sl_mac();
391
392
393
394
395
396
}

modwlan_Status_t wlan_sl_enable (SlWlanMode_t mode, const char *ssid, uint8_t ssid_len, uint8_t sec,
                                 const char *key, uint8_t key_len, uint8_t channel) {

    if (mode == ROLE_STA || mode == ROLE_AP || mode == ROLE_P2P) {
Daniel Campora's avatar
Daniel Campora committed
397
398
        // stop the servers
        wlan_servers_stop();
399

Daniel Campora's avatar
Daniel Campora committed
400
        // do a basic start
401
        wlan_first_start();
402
403
404
405
406
407
408
409

        // Device in station-mode. Disconnect previous connection if any
        // The function returns 0 if 'Disconnected done', negative number if already
        // disconnected Wait for 'disconnection' event if 0 is returned, Ignore
        // other return-codes
        if (0 == sl_WlanDisconnect()) {
            while (IS_CONNECTED (wlan_obj.status)) {
                HAL_Delay (5);
Daniel Campora's avatar
Daniel Campora committed
410
                wlan_update();
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
            }
        }

        // Remove all profiles
        ASSERT_ON_ERROR(sl_WlanProfileDel(0xFF));

        // Enable the DHCP client
        uint8_t value = 1;
        ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE, 1, 1, &value));

        // Set PM policy to normal
        ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0));

        // Unregister mDNS services
        ASSERT_ON_ERROR(sl_NetAppMDNSUnRegisterService(0, 0));

427
428
429
        // Stop the internal HTTP server
        sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID);

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
        // Remove all 64 filters (8 * 8)
        _WlanRxFilterOperationCommandBuff_t  RxFilterIdMask;
        memset ((void *)&RxFilterIdMask, 0 ,sizeof(RxFilterIdMask));
        memset(RxFilterIdMask.FilterIdMask, 0xFF, 8);
        ASSERT_ON_ERROR(sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask, sizeof(_WlanRxFilterOperationCommandBuff_t)));

        // Set Tx power level for station or AP mode
        // Number between 0-15, as dB offset from max power - 0 will set max power
        uint8_t ucPower = 0;
        if (mode == ROLE_AP) {
            // Switch to AP mode
            ASSERT_ON_ERROR(sl_WlanSetMode(mode));
            ASSERT (ssid != NULL && key != NULL);
            ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_AP_TX_POWER, sizeof(ucPower),
                                       (unsigned char *)&ucPower));
            ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, ssid_len, (unsigned char *)ssid));
446
            memcpy(wlan_obj.ssid, (unsigned char *)ssid, ssid_len);
447
448
449
450
451
452
            ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, sizeof(uint8_t), &sec));
            ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, key_len, (unsigned char *)key));
            _u8*  country = (_u8*)"EU";
            ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_COUNTRY_CODE, 2, country));
            ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, (_u8 *)&channel));

Daniel Campora's avatar
Daniel Campora committed
453
            // stop and start again
454
455
456
            wlan_reenable(mode);
            ASSERT (wlan_obj.mode == mode);

Daniel Campora's avatar
Daniel Campora committed
457
458
459
460
461
462
463
464
465
466
467
            SlNetCfgIpV4Args_t ipV4;
            ipV4.ipV4          = (_u32)SL_IPV4_VAL(192,168,1,1);            // _u32 IP address
            ipV4.ipV4Mask      = (_u32)SL_IPV4_VAL(255,255,255,0);          // _u32 Subnet mask for this AP
            ipV4.ipV4Gateway   = (_u32)SL_IPV4_VAL(192,168,1,1);            // _u32 Default gateway address
            ipV4.ipV4DnsServer = (_u32)SL_IPV4_VAL(192,168,1,1);            // _u32 DNS server address
            ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4,
                                         sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipV4));

            // stop and start again
            wlan_reenable(mode);

468
469
470
471
472
473
474
475
476
            SlNetAppDhcpServerBasicOpt_t dhcpParams;
            dhcpParams.lease_time      =  4096;                             // lease time (in seconds) of the IP Address
            dhcpParams.ipv4_addr_start =  SL_IPV4_VAL(192,168,1,2);         // first IP Address for allocation.
            dhcpParams.ipv4_addr_last  =  SL_IPV4_VAL(192,168,1,254);       // last IP Address for allocation.
            ASSERT_ON_ERROR(sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID));      // Stop DHCP server before settings
            ASSERT_ON_ERROR(sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT,
                                         sizeof(SlNetAppDhcpServerBasicOpt_t), (_u8* )&dhcpParams));  // set parameters
            ASSERT_ON_ERROR(sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID));     // Start DHCP server with new settings

Daniel Campora's avatar
Daniel Campora committed
477
            // stop and start again
478
            wlan_reenable(mode);
Daniel Campora's avatar
Daniel Campora committed
479

480
481
            // save the security type
            wlan_obj.security = sec;
482
483
484
485
486
        }
        // STA and P2P modes
        else {
            ASSERT_ON_ERROR(sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER,
                                       sizeof(ucPower), (unsigned char *)&ucPower));
487
488
489
            ASSERT_ON_ERROR(sl_WlanSetMode(mode));
            // stop and start again
            wlan_reenable(mode);
Daniel Campora's avatar
Daniel Campora committed
490
491
            // set connection policy to Auto + Fast (tries to connect to the last connected AP)
            ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1, 1, 0, 0, 0), NULL, 0));
492
        }
Daniel Campora's avatar
Daniel Campora committed
493
494
        // start the servers before returning
        wlan_servers_start();
495
496
497
498
499
        return MODWLAN_OK;
    }
    return MODWLAN_ERROR_INVALID_PARAMS;
}

500
501
502
503
504
505
void wlan_update(void) {
#ifndef SL_PLATFORM_MULTI_THREADED
    _SlTaskEntry();
#endif
}

506
void wlan_stop (uint32_t timeout) {
Daniel Campora's avatar
Daniel Campora committed
507
    wlan_servers_stop();
508
509
510
511
512
513
514
515
    sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
    sl_Stop(timeout);
    wlan_obj.mode = -1;
}

void wlan_start (void) {
    wlan_obj.mode = sl_Start(0, 0, 0);
    sl_LockObjUnlock (&wlan_LockObj);
Daniel Campora's avatar
Daniel Campora committed
516
    wlan_servers_start();
517
518
519
520
}

void wlan_get_mac (uint8_t *macAddress) {
    if (macAddress) {
521
        memcpy (macAddress, wlan_obj.mac, SL_MAC_ADDR_LEN);
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
    }
}

void wlan_get_ip (uint32_t *ip) {
    if (ip) {
        *ip = IS_IP_ACQUIRED(wlan_obj.status) ? wlan_obj.ip : 0;
    }
}

//*****************************************************************************
// DEFINE STATIC FUNCTIONS
//*****************************************************************************

STATIC void wlan_initialize_data (void) {
    wlan_obj.status = 0;
    wlan_obj.dns = 0;
    wlan_obj.gateway = 0;
    wlan_obj.ip = 0;
540
541
    wlan_obj.security = SL_SEC_TYPE_OPEN;
    memset(wlan_obj.ssid, 0, sizeof(wlan_obj.ssid));
542
543
544
545
    memset(wlan_obj.bssid, 0, sizeof(wlan_obj.bssid));
}

STATIC void wlan_reenable (SlWlanMode_t mode) {
546
    // stop and start again
547
    sl_LockObjLock (&wlan_LockObj, SL_OS_WAIT_FOREVER);
548
    sl_Stop(SL_STOP_TIMEOUT);
549
    wlan_obj.mode = sl_Start(0, 0, 0);
550
    sl_LockObjUnlock (&wlan_LockObj);
551
552
553
    ASSERT (wlan_obj.mode == mode);
}

Daniel Campora's avatar
Daniel Campora committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
STATIC void wlan_servers_start (void) {
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
    // start the servers if they were enabled before
    if (wlan_obj.servers_enabled) {
        servers_start();
    }
#endif
}

STATIC void wlan_servers_stop (void) {
#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
    // Stop all other processes using the wlan engine
    if ((wlan_obj.servers_enabled = servers_are_enabled())) {
        servers_stop();
    }
#endif
}

572
573
STATIC modwlan_Status_t wlan_do_connect (const char* ssid, uint32_t ssid_len, const char* bssid, uint8_t sec,
                                         const char* key, uint32_t key_len) {
574
575
576
577
578
579
    SlSecParams_t secParams;
    secParams.Key = (_i8*)key;
    secParams.KeyLen = ((key != NULL) ? key_len : 0);
    secParams.Type = sec;

    if (0 == sl_WlanConnect((_i8*)ssid, ssid_len, (_u8*)bssid, &secParams, NULL)) {
580
        // Wait for the WLAN Event
581
582
583
        uint32_t waitForConnectionMs = 0;
        while (!IS_CONNECTED(wlan_obj.status)) {
            HAL_Delay (5);
Daniel Campora's avatar
Daniel Campora committed
584
            wlan_update();
585
586
587
588
589
590
591
592
593
594
595
596
            if (++waitForConnectionMs >= MODWLAN_TIMEOUT_MS) {
                return MODWLAN_ERROR_TIMEOUT;
            }
        }
        return MODWLAN_OK;
    }
    return MODWLAN_ERROR_INVALID_PARAMS;
}

STATIC void wlan_get_sl_mac (void) {
    // Get the MAC address
    uint8_t macAddrLen = SL_MAC_ADDR_LEN;
597
    sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &macAddrLen, wlan_obj.mac);
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
}

/// \method init(mode, ssid=myWlan, security=wlan.WPA_WPA2, key=myWlanKey)
///
/// Initialise the UART bus with the given parameters:
///
///   - `mode` can be ROLE_AP, ROLE_STA and ROLE_P2P.
///   - `ssid` is the network ssid in case of AP mode
///   - `security` is the security type for AP mode
///   - `key` is the key when in AP mode
///   - `channel` is the channel to use for the AP network
STATIC const mp_arg_t wlan_init_args[] = {
    { MP_QSTR_mode,         MP_ARG_REQUIRED | MP_ARG_INT,  {.u_int = ROLE_STA} },
    { MP_QSTR_ssid,         MP_ARG_KW_ONLY  | MP_ARG_OBJ,  {.u_obj = mp_const_none} },
    { MP_QSTR_security,     MP_ARG_KW_ONLY  | MP_ARG_INT,  {.u_int = SL_SEC_TYPE_OPEN} },
    { MP_QSTR_key,          MP_ARG_KW_ONLY  | MP_ARG_OBJ,  {.u_obj = mp_const_none} },
    { MP_QSTR_channel,      MP_ARG_KW_ONLY  | MP_ARG_INT,  {.u_int = 5} },
};

STATIC mp_obj_t wlan_init_helper(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    // parse args
    mp_arg_val_t args[MP_ARRAY_SIZE(wlan_init_args)];
    mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(wlan_init_args), wlan_init_args, args);

    // get the ssid
    mp_uint_t ssid_len;
    const char *ssid = mp_obj_str_get_data(args[1].u_obj, &ssid_len);

    // get the key
    mp_uint_t key_len;
    const char *key = mp_obj_str_get_data(args[3].u_obj, &key_len);

    if (key_len < 8) {
631
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
632
633
634
635
636
637
638
639
640
641
642
643
    }

    // Force the channel to be between 1-11
    uint8_t channel = args[4].u_int > 0 ? args[4].u_int % 12 : 1;

    if (MODWLAN_OK != wlan_sl_enable (args[0].u_int, ssid, ssid_len, args[2].u_int, key, key_len, channel)) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
    }

    return mp_const_none;
}

644
STATIC void wlan_lpds_callback_enable (mp_obj_t self_in) {
645
646
    mp_obj_t _callback = mpcallback_find(self_in);
    pybsleep_set_wlan_lpds_callback (_callback);
647
648
}

649
650
STATIC void wlan_lpds_callback_disable (mp_obj_t self_in) {
    pybsleep_set_wlan_lpds_callback (NULL);
651
}
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693

/******************************************************************************/
// Micro Python bindings; WLAN class

/// \class WLAN - driver for the WLAN functionality of the SoC

/// \classmethod \constructor()
/// Create a wlan obecjt and initialise the simplelink engine
//
STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
    // check arguments
    mp_arg_check_num(n_args, n_kw, 0, MP_ARRAY_SIZE(wlan_init_args), true);

    if (n_args > 0) {
        // Get the mode
        SlWlanMode_t mode = mp_obj_get_int(args[0]);
        if (mode == ROLE_AP) {
            // start the peripheral
            mp_map_t kw_args;
            mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
            wlan_init_helper(n_args, args, &kw_args);
        }
        // TODO: Only STA mode supported for the moment. What if P2P?
        else if (n_args == 1) {
            if (MODWLAN_OK != wlan_sl_enable (mode, NULL, 0, 0, NULL, 0, 0)) {
                nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
            }
        }
        else {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
        }
    } else if (wlan_obj.mode < 0) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
    }

    wlan_obj.base.type = (mp_obj_type_t*)&mod_network_nic_type_wlan;
    // register with the network module
    mod_network_register_nic(&wlan_obj);

    return &wlan_obj;
}

694
STATIC void wlan_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
695
    wlan_obj_t *self = self_in;
696
    mp_printf(print, "<WLAN, mode=%u", self->mode);
697

698
699
    // only print the bssid if in station mode
    if (self->mode != ROLE_AP && GET_STATUS_BIT(self->status, STATUS_BIT_CONNECTION)) {
700
        mp_printf(print, ", connected to: ssid=%s, bssid=%02x:%02x:%02x:%02x:%02x:%02x", self->ssid,
701
              self->bssid[0], self->bssid[1], self->bssid[2], self->bssid[3], self->bssid[4], self->bssid[5]);
702
703
    }
    else {
704
        mp_printf(print, ", ssid=%s", self->ssid);
705
    }
706
    mp_printf(print, ", security=%u>", self->security);
707
708
}

709
710
711
/// \method connect(ssid, security=OPEN, key=None, bssid=None)
//          if security is WPA/WPA2, the key must be a string
///         if security is WEP, the key must be binary
712
713
714
715
STATIC mp_obj_t wlan_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    STATIC const mp_arg_t allowed_args[] = {
        { MP_QSTR_ssid,     MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
        { MP_QSTR_security, MP_ARG_KW_ONLY  | MP_ARG_INT, {.u_int = SL_SEC_TYPE_OPEN} },
716
        { MP_QSTR_key,      MP_ARG_KW_ONLY  | MP_ARG_OBJ, {.u_obj = mp_const_none} },
717
718
719
        { MP_QSTR_bssid,    MP_ARG_KW_ONLY  | MP_ARG_OBJ, {.u_obj = mp_const_none} },
    };

720
721
722
723
724
    // check for correct wlan mode
    if (wlan_obj.mode != ROLE_STA && wlan_obj.mode != ROLE_P2P) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
    }

725
726
727
728
    // parse args
    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

729
    // get the ssid
730
731
732
    mp_uint_t ssid_len;
    const char *ssid = mp_obj_str_get_data(args[0].u_obj, &ssid_len);

733
734
735
736
    // get the security type
    mp_uint_t sec = args[1].u_int;

    // get key and its len
737
738
    mp_uint_t key_len = 0;
    const char *key = NULL;
739
740
741
742
743
744
745
746
747
748
749
    mp_buffer_info_t wepkey;
    if (args[2].u_obj != mp_const_none) {
        // wep key must be given as raw bytes
        if (sec == SL_SEC_TYPE_WEP) {
            mp_get_buffer_raise(args[2].u_obj, &wepkey, MP_BUFFER_READ);
            key = wepkey.buf;
            key_len = wepkey.len;
        }
        else {
            key = mp_obj_str_get_data(args[2].u_obj, &key_len);
        }
750
751
752
753
754
755
756
757
    }

    // get bssid
    const char *bssid = NULL;
    if (args[3].u_obj != mp_const_none) {
        bssid = mp_obj_str_get_str(args[3].u_obj);
    }

758
759
760
761
    if (GET_STATUS_BIT(wlan_obj.status, STATUS_BIT_CONNECTION)) {
        if (0 == sl_WlanDisconnect()) {
            while (IS_CONNECTED(wlan_obj.status)) {
                HAL_Delay (5);
Daniel Campora's avatar
Daniel Campora committed
762
                wlan_update();
763
764
            }
        }
765
766
767
768
769
770
771
772
773
774
    }

    // connect to the requested access point
    modwlan_Status_t status;
    status = wlan_do_connect (ssid, ssid_len, bssid, sec, key, key_len);
    if (status == MODWLAN_ERROR_TIMEOUT) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
    }
    else if (status == MODWLAN_ERROR_INVALID_PARAMS) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
775
    }
776
    wlan_obj.security = sec;
777
778
779
780
781
782

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_connect_obj, 1, wlan_connect);

/// \method wlan_disconnect()
783
/// Close the current WLAN connection
784
785
786
787
788
789
790
STATIC mp_obj_t wlan_disconnect(mp_obj_t self_in) {
    sl_WlanDisconnect();
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_disconnect_obj, wlan_disconnect);

/// \method is_connected()
791
/// Return true if connected to the AP and an IP address has been assigned. False otherwise.
792
793
794
795
796
797
798
799
800
STATIC mp_obj_t wlan_isconnected(mp_obj_t self_in) {
    if (GET_STATUS_BIT(wlan_obj.status, STATUS_BIT_CONNECTION) &&
        GET_STATUS_BIT(wlan_obj.status, STATUS_BIT_IP_ACQUIRED)) {
        return mp_const_true;
    }
    return mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_isconnected_obj, wlan_isconnected);

801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
STATIC mp_obj_t wlan_ifconfig (mp_obj_t self_in) {
    unsigned char len = sizeof(SlNetCfgIpV4Args_t);
    unsigned char dhcpIsOn;
    SlNetCfgIpV4Args_t ipV4;

    sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, &dhcpIsOn, &len, (uint8_t *)&ipV4);

    mp_obj_t ifconfig = mp_obj_new_dict(0);
    mp_obj_dict_store (ifconfig, mp_obj_new_str("ip", strlen("ip"), false), mod_network_format_ipv4_addr((uint8_t *)&wlan_obj.ip));
    mp_obj_dict_store (ifconfig, mp_obj_new_str("subnet", strlen("subnet"), false), mod_network_format_ipv4_addr((uint8_t *)&ipV4.ipV4Mask));
    mp_obj_dict_store (ifconfig, mp_obj_new_str("gateway", strlen("gateway"), false), mod_network_format_ipv4_addr((uint8_t *)&wlan_obj.gateway));
    mp_obj_dict_store (ifconfig, mp_obj_new_str("dns", strlen("dns"), false), mod_network_format_ipv4_addr((uint8_t *)&wlan_obj.dns));
    char mac_str[18];
    mp_uint_t mac_len = snprintf(mac_str, sizeof(mac_str), "%02x:%02x:%02x:%02x:%02x:%02x", wlan_obj.mac[0], wlan_obj.mac[1], wlan_obj.mac[2],
                                 wlan_obj.mac[3], wlan_obj.mac[4], wlan_obj.mac[5]);
    mp_obj_dict_store (ifconfig, mp_obj_new_str("mac", strlen("mac"), false), mp_obj_new_str(mac_str, mac_len, false));
    char *mode_str;
    if (wlan_obj.mode == ROLE_STA) {
        mode_str = "station";
    }
    else if (wlan_obj.mode == ROLE_AP) {
        mode_str = "ap";
823
824
    }
    else {
825
        mode_str = "p2p";
826
    }
827
828
829
830
    mp_obj_dict_store (ifconfig, mp_obj_new_str("mode", strlen("mode"), false), mp_obj_new_str(mode_str, strlen(mode_str), false));
    mp_obj_dict_store (ifconfig, mp_obj_new_str("ssid", strlen("ssid"), false), mp_obj_new_str((const char *)wlan_obj.ssid, strlen((const char *)wlan_obj.ssid), false));

    return ifconfig;
831
}
832
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_ifconfig_obj, wlan_ifconfig);
833

834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
STATIC mp_obj_t wlan_urn (uint n_args, const mp_obj_t *args) {
    char urn[MAX_DEVICE_URN_LEN];
    uint8_t len = MAX_DEVICE_URN_LEN;

    // an URN is given, so set it
    if (n_args == 2) {
        const char *p = mp_obj_str_get_str(args[1]);
        uint8_t len = strlen(p);

        // the call to sl_NetAppSet corrupts the input string URN=args[1], so we copy into a local buffer
        if (len > MAX_DEVICE_URN_LEN) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
        }
        strcpy(urn, p);

        if (sl_NetAppSet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, len, (unsigned char *)urn) < 0) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
        }
    }
    else {
        // get the URN
        if (sl_NetAppGet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, &len, (uint8_t *)urn) < 0) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
        }
        return mp_obj_new_str(urn, (len - 1), false);
    }

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_urn_obj, 1, 2, wlan_urn);
864
865

/// \method wlan_netlist()
866
/// Return a list of tuples with all the acces points within range
867
868
869
870
871
STATIC mp_obj_t wlan_scan(mp_obj_t self_in) {
    Sl_WlanNetworkEntry_t wlanEntry;
    uint8_t _index = 0;
    mp_obj_t nets = NULL;

872
873
874
875
876
877
878
    // trigger a new newtork scanning
    uint32_t scanSeconds = MODWLAN_SCAN_PERIOD_S;
    ASSERT_ON_ERROR(sl_WlanPolicySet(SL_POLICY_SCAN , MODWLAN_SL_SCAN_ENABLE, (_u8 *)&scanSeconds, sizeof(scanSeconds)));

    // wait for the scan to be completed
    HAL_Delay (MODWLAN_WAIT_FOR_SCAN_MS);

879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
    do {
        if (sl_WlanGetNetworkList(_index++, 1, &wlanEntry) <= 0) {
            break;
        }
        mp_obj_t tuple[4];

        tuple[0] = mp_obj_new_str((const char *)wlanEntry.ssid, wlanEntry.ssid_len, false);
        tuple[1] = mp_obj_new_str((const char *)wlanEntry.bssid, SL_BSSID_LENGTH, false);
        // 'Normalize' the security type
        if (wlanEntry.sec_type > 2) {
            wlanEntry.sec_type = 2;
        }
        tuple[2] = mp_obj_new_int(wlanEntry.sec_type);
        tuple[3] = mp_obj_new_int(wlanEntry.rssi);

        if (_index == 1) {
            // Initialize the set
            nets = mp_obj_new_set(0, NULL);
        }
        // Add the network found to the list if it's unique
        mp_obj_set_store(nets, mp_obj_new_tuple(4, tuple));

    } while (_index < MODWLAN_SL_MAX_NETWORKS);

    return (nets != NULL) ? nets : mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_scan_obj, wlan_scan);

907
/// \method callback(handler, pwrmode)
908
/// Create a callback object associated with WLAN
909
/// min num of arguments is 1 (pwrmode)
910
911
912
913
STATIC mp_obj_t wlan_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    mp_arg_val_t args[mpcallback_INIT_NUM_ARGS];
    mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, mpcallback_INIT_NUM_ARGS, mpcallback_init_args, args);

914
915
    wlan_obj_t *self = pos_args[0];
    mp_obj_t _callback = mpcallback_find(self);
916
    // check if any parameters were passed
917
    if (kw_args->used > 0 || !_callback) {
918
919
920
921
922
923
924
        // check the power mode
        if (args[4].u_int != PYB_PWR_MODE_LPDS) {
            // throw an exception since WLAN only supports LPDS mode
            nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
        }

        // create the callback
925
        _callback = mpcallback_new (self, args[1].u_obj, &wlan_cb_methods);
926
927

        // enable network wakeup
928
        pybsleep_set_wlan_lpds_callback (_callback);
929
    }
930
    return _callback;
931
932
933
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_callback_obj, 1, wlan_callback);

Daniel Campora's avatar
Daniel Campora committed
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
/// \method config_ip(mode, *, ip='192.168.1.1', subnet='255.255.255.0', gateway='192.168.1.1', dns='8.8.8.8')
STATIC mp_obj_t wlan_config_ip (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    STATIC const mp_arg_t allowed_args[] = {
        { MP_QSTR_mode,     MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = MODWLAN_IP_MODE_DYNAMIC} },
        { MP_QSTR_ip,       MP_ARG_KW_ONLY  | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
        { MP_QSTR_subnet,   MP_ARG_KW_ONLY  | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
        { MP_QSTR_gateway,  MP_ARG_KW_ONLY  | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
        { MP_QSTR_dns,      MP_ARG_KW_ONLY  | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
    };

    // parse args
    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

    if (args[0].u_int == MODWLAN_IP_MODE_DYNAMIC) {
        // only mode must be given
        if (args[1].u_obj || args[2].u_obj || args[3].u_obj || args[4].u_obj) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
        }

        wlan_servers_stop();

        // nothing to do if we are an access point
        if (wlan_obj.mode != ROLE_AP) {
            _u8 val = 1;
            sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, 1, &val);
            wlan_reenable (wlan_obj.mode);
        }
    }
    else {
        // we need all arguments at this point
        if (!args[1].u_obj || !args[2].u_obj || !args[3].u_obj || !args[4].u_obj) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
        }

        SlNetCfgIpV4Args_t ipV4;
        mod_network_parse_ipv4_addr(args[1].u_obj, (uint8_t *)&ipV4.ipV4);
        mod_network_parse_ipv4_addr(args[2].u_obj, (uint8_t *)&ipV4.ipV4Mask);
        mod_network_parse_ipv4_addr(args[3].u_obj, (uint8_t *)&ipV4.ipV4Gateway);
        mod_network_parse_ipv4_addr(args[4].u_obj, (uint8_t *)&ipV4.ipV4DnsServer);

        wlan_servers_stop();

        if (wlan_obj.mode == ROLE_AP) {
            ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipV4));

            // stop and start again
            wlan_reenable(wlan_obj.mode);

            SlNetAppDhcpServerBasicOpt_t dhcpParams;
            dhcpParams.lease_time      =  4096;                             // lease time (in seconds) of the IP Address
            dhcpParams.ipv4_addr_start =  ipV4.ipV4 + 1;                    // first IP Address for allocation.
            dhcpParams.ipv4_addr_last  =  (ipV4.ipV4 & 0xFFFFFF00) + 254;   // last IP Address for allocation.
            ASSERT_ON_ERROR(sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID));      // stop DHCP server before settings
            ASSERT_ON_ERROR(sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT,
                                         sizeof(SlNetAppDhcpServerBasicOpt_t), (_u8* )&dhcpParams));  // set parameters
            ASSERT_ON_ERROR(sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID));     // start DHCP server with new settings
        }
        else {
            ASSERT_ON_ERROR(sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipV4));
        }
        wlan_reenable (wlan_obj.mode);
    }

    wlan_servers_start();

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_config_ip_obj, 1, wlan_config_ip);

1004
1005
1006
1007
STATIC const mp_map_elem_t wlan_locals_dict_table[] = {
    { MP_OBJ_NEW_QSTR(MP_QSTR_connect),             (mp_obj_t)&wlan_connect_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_scan),                (mp_obj_t)&wlan_scan_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_disconnect),          (mp_obj_t)&wlan_disconnect_obj },
1008
1009
    { MP_OBJ_NEW_QSTR(MP_QSTR_isconnected),         (mp_obj_t)&wlan_isconnected_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_ifconfig),            (mp_obj_t)&wlan_ifconfig_obj },
1010
    { MP_OBJ_NEW_QSTR(MP_QSTR_urn),                 (mp_obj_t)&wlan_urn_obj },
1011
    { MP_OBJ_NEW_QSTR(MP_QSTR_callback),            (mp_obj_t)&wlan_callback_obj },
Daniel Campora's avatar
Daniel Campora committed
1012
    { MP_OBJ_NEW_QSTR(MP_QSTR_config_ip),           (mp_obj_t)&wlan_config_ip_obj },
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023

    // class constants
    { MP_OBJ_NEW_QSTR(MP_QSTR_OPEN),                MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_OPEN) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_WEP),                 MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WEP) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_WPA_WPA2),            MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WPA_WPA2) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_WPA_ENT),             MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WPA_ENT) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_WPS_PBC),             MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WPS_PBC) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_WPS_PIN),             MP_OBJ_NEW_SMALL_INT(SL_SEC_TYPE_WPS_PIN) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_STA),                 MP_OBJ_NEW_SMALL_INT(ROLE_STA) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_AP),                  MP_OBJ_NEW_SMALL_INT(ROLE_AP) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_P2P),                 MP_OBJ_NEW_SMALL_INT(ROLE_P2P) },
Daniel Campora's avatar
Daniel Campora committed
1024
1025
    { MP_OBJ_NEW_QSTR(MP_QSTR_DYNAMIC),             MP_OBJ_NEW_SMALL_INT(MODWLAN_IP_MODE_DYNAMIC) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_STATIC),              MP_OBJ_NEW_SMALL_INT(MODWLAN_IP_MODE_STATIC) },
1026
1027
1028
};
STATIC MP_DEFINE_CONST_DICT(wlan_locals_dict, wlan_locals_dict_table);

1029
1030
STATIC const mp_cb_methods_t wlan_cb_methods = {
    .init = wlan_callback,
1031
1032
    .enable = wlan_lpds_callback_enable,
    .disable = wlan_lpds_callback_disable,
1033
};
1034
1035
1036
1037
1038
1039
1040
1041

/******************************************************************************/
// Micro Python bindings; WLAN socket

STATIC int wlan_gethostbyname(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *out_ip, uint8_t family) {
    uint32_t ip;
    int result = sl_NetAppDnsGetHostByName((_i8 *)name, (_u16)len, (_u32*)&ip, (_u8)family);

Daniel Campora's avatar
Daniel Campora committed
1042
1043
1044
1045
    out_ip[0] = ip;
    out_ip[1] = ip >> 8;
    out_ip[2] = ip >> 16;
    out_ip[3] = ip >> 24;
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311

    return result;
}

STATIC int wlan_socket_socket(struct _mod_network_socket_obj_t *s, int *_errno) {
    // open the socket
    int16_t sd = sl_Socket(s->u_param.domain, s->u_param.type, s->u_param.proto);
    if (s->sd < 0) {
        *_errno = s->sd;
        return -1;
    }

    // mark the socket not closed
    s->closed = false;
    // save the socket descriptor
    s->sd = sd;

    // make it blocking by default
    int32_t optval = 0;
    sl_SetSockOpt(sd, SOL_SOCKET, SO_NONBLOCKING, &optval, (SlSocklen_t)sizeof(optval));

    return 0;
}

STATIC void wlan_socket_close(mod_network_socket_obj_t *s) {
    s->closed = true;
    sl_Close(s->sd);
}

STATIC int wlan_socket_bind(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno) {
    MAKE_SOCKADDR(addr, ip, port)
    int ret = sl_Bind(s->sd, &addr, sizeof(addr));
    if (ret != 0) {
        *_errno = ret;
        return -1;
    }
    return 0;
}

STATIC int wlan_socket_listen(mod_network_socket_obj_t *s, mp_int_t backlog, int *_errno) {
    int ret = sl_Listen(s->sd, backlog);
    if (ret != 0) {
        *_errno = ret;
        return -1;
    }
    return 0;
}

STATIC int wlan_socket_accept(mod_network_socket_obj_t *s, mod_network_socket_obj_t *s2, byte *ip, mp_uint_t *port, int *_errno) {
    // accept incoming connection
    int16_t sd;
    sockaddr addr;
    socklen_t addr_len = sizeof(addr);
    if ((sd = sl_Accept(s->sd, &addr, &addr_len)) < 0) {
        *_errno = sd;
        return -1;
    }

    // Mark the socket not closed and save the new descriptor
    s2->closed = false;
    s2->sd = sd;

    // return ip and port
    UNPACK_SOCKADDR(addr, ip, *port);

    return 0;
}

STATIC int wlan_socket_connect(mod_network_socket_obj_t *s, byte *ip, mp_uint_t port, int *_errno) {
    MAKE_SOCKADDR(addr, ip, port)
    int ret = sl_Connect(s->sd, &addr, sizeof(addr));
    if (ret != 0) {
        *_errno = ret;
        return -1;
    }
    return 0;
}

STATIC int wlan_socket_send(mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, int *_errno) {
    if (s->closed) {
        sl_Close (s->sd);
        *_errno = EBADF;
        return -1;
    }

    mp_int_t bytes = 0;
    if (len > 0) {
        bytes = sl_Send(s->sd, (const void *)buf, len, 0);
    }
    if (bytes <= 0) {
        *_errno = bytes;
        return -1;
    }

    return bytes;
}

STATIC int wlan_socket_recv(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, int *_errno) {
    // check if the socket is open
    if (s->closed) {
        // socket is closed, but the CC3200 may have some data remaining in its buffer, so check
        fd_set rfds;
        FD_ZERO(&rfds);
        FD_SET(s->sd, &rfds);
        timeval tv;
        tv.tv_sec = 0;
        tv.tv_usec = 2;
        int nfds = sl_Select(s->sd + 1, &rfds, NULL, NULL, &tv);
        if (nfds == -1 || !FD_ISSET(s->sd, &rfds)) {
            // no data waiting, so close socket and return 0 data
            sl_Close(s->sd);
            return 0;
        }
    }

    // cap length at WLAN_MAX_RX_SIZE
    len = MIN(len, WLAN_MAX_RX_SIZE);

    // do the recv
    int ret = sl_Recv(s->sd, buf, len, 0);
    if (ret < 0) {
        *_errno = ret;
        return -1;
    }

    return ret;
}

STATIC int wlan_socket_sendto( mod_network_socket_obj_t *s, const byte *buf, mp_uint_t len, byte *ip, mp_uint_t port, int *_errno) {
    MAKE_SOCKADDR(addr, ip, port)
    int ret = sl_SendTo(s->sd, (byte*)buf, len, 0, (sockaddr*)&addr, sizeof(addr));
    if (ret < 0) {
        *_errno = ret;
        return -1;
    }
    return ret;
}

STATIC int wlan_socket_recvfrom(mod_network_socket_obj_t *s, byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) {
    sockaddr addr;
    socklen_t addr_len = sizeof(addr);
    mp_int_t ret = sl_RecvFrom(s->sd, buf, len, 0, &addr, &addr_len);
    if (ret < 0) {
        *_errno = ret;
        return -1;
    }
    UNPACK_SOCKADDR(addr, ip, *port);
    return ret;
}

STATIC int wlan_socket_setsockopt(mod_network_socket_obj_t *socket, mp_uint_t level, mp_uint_t opt, const void *optval, mp_uint_t optlen, int *_errno) {
    int ret = sl_SetSockOpt(socket->sd, level, opt, optval, optlen);
    if (ret < 0) {
        *_errno = ret;
        return -1;
    }
    return 0;
}

STATIC int wlan_socket_settimeout(mod_network_socket_obj_t *s, mp_uint_t timeout_ms, int *_errno) {
    int ret;
    if (timeout_ms == 0 || timeout_ms == -1) {
        int optval;
        if (timeout_ms == 0) {
            // set non-blocking mode
            optval = 1;
        } else {
            // set blocking mode
            optval = 0;
        }
        ret = sl_SetSockOpt(s->sd, SOL_SOCKET, SO_NONBLOCKING, &optval, sizeof(optval));
    } else {
        // set timeout
        ret = sl_SetSockOpt(s->sd, SOL_SOCKET, SO_RCVTIMEO, &timeout_ms, sizeof(timeout_ms));
    }

    if (ret != 0) {
        *_errno = ret;
        return -1;
    }

    return 0;
}

STATIC int wlan_socket_ioctl (mod_network_socket_obj_t *s, mp_uint_t request, mp_uint_t arg, int *_errno) {
    mp_int_t ret;
    if (request == MP_IOCTL_POLL) {
        mp_uint_t flags = arg;
        ret = 0;
        int32_t sd = s->sd;

        // init fds
        fd_set rfds, wfds, xfds;
        FD_ZERO(&rfds);
        FD_ZERO(&wfds);
        FD_ZERO(&xfds);

        // set fds if needed
        if (flags & MP_IOCTL_POLL_RD) {
            FD_SET(sd, &rfds);

            // A socked that just closed is available for reading.  A call to
            // recv() returns 0 which is consistent with BSD.
            if (s->closed) {
                ret |= MP_IOCTL_POLL_RD;
            }
        }
        if (flags & MP_IOCTL_POLL_WR) {
            FD_SET(sd, &wfds);
        }
        if (flags & MP_IOCTL_POLL_HUP) {
            FD_SET(sd, &xfds);
        }

        // call simplelink select with minimum timeout
        SlTimeval_t tv;
        tv.tv_sec = 0;
        tv.tv_usec = 1;
        int32_t nfds = sl_Select(sd + 1, &rfds, &wfds, &xfds, &tv);

        // check for error
        if (nfds == -1) {
            *_errno = nfds;
            return -1;
        }

        // check return of select
        if (FD_ISSET(sd, &rfds)) {
            ret |= MP_IOCTL_POLL_RD;
        }
        if (FD_ISSET(sd, &wfds)) {
            ret |= MP_IOCTL_POLL_WR;
        }
        if (FD_ISSET(sd, &xfds)) {
            ret |= MP_IOCTL_POLL_HUP;
        }
    } else {
        *_errno = EINVAL;
        ret = -1;
    }
    return ret;
}

const mod_network_nic_type_t mod_network_nic_type_wlan = {
    .base = {
        { &mp_type_type },
        .name = MP_QSTR_WLAN,
        .print = wlan_print,
        .make_new = wlan_make_new,
        .locals_dict = (mp_obj_t)&wlan_locals_dict,
    },
    .gethostbyname = wlan_gethostbyname,
    .socket = wlan_socket_socket,
    .close = wlan_socket_close,
    .bind = wlan_socket_bind,
    .listen = wlan_socket_listen,
    .accept = wlan_socket_accept,
    .connect = wlan_socket_connect,
    .send = wlan_socket_send,
    .recv = wlan_socket_recv,
    .sendto = wlan_socket_sendto,
    .recvfrom = wlan_socket_recvfrom,
    .setsockopt = wlan_socket_setsockopt,
    .settimeout = wlan_socket_settimeout,
    .ioctl = wlan_socket_ioctl,
};