Statistics
| Branch: | Tag: | Revision:

mongoose / examples / MSP432 / ccs / MSP432_MG_hello / wifi.c @ eaef5bd1

History | View | Annotate | Download (3.72 KB)

1
#include "cs_dbg.h"
2
#include "wifi.h"
3

    
4
#include "mongoose.h"
5
#include "simplelink.h"
6
#include "wlan.h"
7

    
8
void SimpleLinkWlanEventHandler(SlWlanEvent_t *e) {
9
  switch (e->Event) {
10
    case SL_WLAN_CONNECT_EVENT:
11
      LOG(LL_INFO, ("WiFi: connected, getting IP"));
12
      break;
13
    case SL_WLAN_STA_CONNECTED_EVENT:
14
      LOG(LL_INFO, ("WiFi: station connected"));
15
      break;
16
    case SL_WLAN_STA_DISCONNECTED_EVENT:
17
      LOG(LL_INFO, ("WiFi: station disconnected"));
18
      break;
19
    default:
20
      LOG(LL_INFO, ("WiFi: event %d", e->Event));
21
  }
22
}
23

    
24
int ip_acquired = 0;
25

    
26
void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *e) {
27
  if (e->Event == SL_NETAPP_IPV4_IPACQUIRED_EVENT) {
28
    SlIpV4AcquiredAsync_t *ed = &e->EventData.ipAcquiredV4;
29
    LOG(LL_INFO, ("IP acquired: %lu.%lu.%lu.%lu", SL_IPV4_BYTE(ed->ip, 3),
30
                  SL_IPV4_BYTE(ed->ip, 2), SL_IPV4_BYTE(ed->ip, 1),
31
                  SL_IPV4_BYTE(ed->ip, 0)));
32
    ip_acquired = 1;
33
  } else if (e->Event == SL_NETAPP_IP_LEASED_EVENT) {
34
    LOG(LL_INFO, ("IP leased"));
35
  } else {
36
    LOG(LL_INFO, ("NetApp event %d", e->Event));
37
  }
38
}
39

    
40
bool wifi_setup_ap(const char *ssid, const char *pass, int channel) {
41
  uint8_t v;
42
  if (sl_WlanSetMode(ROLE_AP) != 0) {
43
    return false;
44
  }
45
  if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(ssid),
46
                 (const uint8_t *) ssid) != 0) {
47
    return false;
48
  }
49
  v = strlen(pass) > 0 ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN;
50
  if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, &v) != 0) {
51
    return false;
52
  }
53
  if (v == SL_SEC_TYPE_WPA &&
54
      sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(pass),
55
                 (const uint8_t *) pass) != 0) {
56
    return false;
57
  }
58
  v = channel;
59
  if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, &v) != 0) {
60
    return false;
61
  }
62
  sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID);
63
  {
64
    SlNetCfgIpV4Args_t ipcfg;
65
    memset(&ipcfg, 0, sizeof(ipcfg));
66
    if (!inet_pton(AF_INET, "192.168.4.1", &ipcfg.ipV4) ||
67
        !inet_pton(AF_INET, "255.255.255.0", &ipcfg.ipV4Mask) ||
68
        /* This means "disable". 0.0.0.0 won't do. */
69
        !inet_pton(AF_INET, "255.255.255.255", &ipcfg.ipV4DnsServer) ||
70
        /* We'd like to disable gateway too, but DHCP server refuses to start.
71
           */
72
        !inet_pton(AF_INET, "192.168.4.1", &ipcfg.ipV4Gateway) ||
73
        sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4,
74
                     sizeof(ipcfg), (uint8_t *) &ipcfg) != 0) {
75
      return false;
76
    }
77
  }
78
  {
79
    SlNetAppDhcpServerBasicOpt_t dhcpcfg;
80
    memset(&dhcpcfg, 0, sizeof(dhcpcfg));
81
    dhcpcfg.lease_time = 900;
82
    if (!inet_pton(AF_INET, "192.168.4.20", &dhcpcfg.ipv4_addr_start) ||
83
        !inet_pton(AF_INET, "192.168.4.200", &dhcpcfg.ipv4_addr_last) ||
84
        sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT,
85
                     sizeof(dhcpcfg), (uint8_t *) &dhcpcfg) != 0) {
86
      return false;
87
    }
88
  }
89
  sl_Stop(0);
90
  int role = sl_Start(NULL, NULL, NULL);
91
  if (role != ROLE_AP) {
92
    LOG(LL_ERROR, ("Expected ROLE_AP (%d), got %d", ROLE_AP, role));
93
    return false;
94
  }
95
  if (sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID) != 0) {
96
    LOG(LL_ERROR, ("DHCP server failed to start"));
97
    return false;
98
  }
99
  LOG(LL_INFO, ("WiFi: AP %s configured", ssid));
100
  return true;
101
}
102

    
103
bool wifi_setup_sta(const char *ssid, const char *pass) {
104
  SlSecParams_t sp;
105
  LOG(LL_INFO, ("WiFi: connecting to %s", ssid));
106
  if (sl_WlanSetMode(ROLE_STA) != 0) return false;
107
  sl_Stop(0);
108
  sl_Start(NULL, NULL, NULL);
109
  sl_WlanDisconnect();
110
  sp.Key = (_i8 *) pass;
111
  sp.KeyLen = strlen(pass);
112
  sp.Type = sp.KeyLen ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN;
113
  if (sl_WlanConnect((const _i8 *) ssid, strlen(ssid), 0, &sp, 0) != 0) {
114
    return false;
115
  }
116
  return true;
117
}