36#define SUPPORT_SL_R1_API
41#include "freertos_drivers/common/WifiDefs.hxx"
49#include <ti/drivers/net/wifi/simplelink.h>
50#include <ti/drivers/net/wifi/source/protocol.h>
118 for (
int i = 0; i < SL_MAX_SOCKETS; ++i)
144 for (
int i = 0; i < SL_MAX_SOCKETS; ++i)
152 for (
int i = 0; i < SL_MAX_SOCKETS; ++i)
167 for (
int i = 0; i < SL_MAX_SOCKETS; ++i)
183 , ipAcquiredCallback_(nullptr)
186 , wlanRole(WlanRole::UNKNOWN)
189 , connectionFailed(0)
192 , smartConfigStart(0)
195 for (
int i = 0; i < SL_MAX_SOCKETS; ++i)
199 SL_SOCKET_FD_ZERO(&
rfds);
200 SL_SOCKET_FD_ZERO(&
wfds);
201 SL_SOCKET_FD_ZERO(&
efds);
214 return SL_WLAN_SEC_TYPE_OPEN;
216 return SL_WLAN_SEC_TYPE_WEP;
218 return SL_WLAN_SEC_TYPE_WPA_WPA2;
224 auto t = SL_WLAN_SCAN_RESULT_SEC_TYPE_BITMAP(sec_type);
228 case SL_WLAN_SECURITY_TYPE_BITMAP_OPEN:
230 case SL_WLAN_SECURITY_TYPE_BITMAP_WEP:
232 case SL_WLAN_SECURITY_TYPE_BITMAP_WPA:
233 case SL_WLAN_SECURITY_TYPE_BITMAP_WPA2:
234 case SL_WLAN_SECURITY_TYPE_BITMAP_WPA |
235 SL_WLAN_SECURITY_TYPE_BITMAP_WPA2:
246 case SL_WLAN_SEC_TYPE_OPEN:
248 case SL_WLAN_SEC_TYPE_WEP:
250 case SL_WLAN_SEC_TYPE_WPS_PBC:
251 case SL_WLAN_SEC_TYPE_WPS_PIN:
252 case SL_WLAN_SEC_TYPE_WPA_ENT:
253 case SL_WLAN_SEC_TYPE_WPA_WPA2:
263 uint16_t config_option = SL_WLAN_GENERAL_PARAM_OPT_COUNTRY_CODE;
267 int result = sl_WlanGet(SL_WLAN_CFG_GENERAL_PARAM_ID, &config_option, &len,
271 if (!strcmp((
const char*)country,
"US"))
273 return CountryCode::US;
275 else if (!strcmp((
const char*)country,
"EU"))
277 return CountryCode::EU;
279 else if (!strcmp((
const char*)country,
"JP"))
281 return CountryCode::JP;
285 return CountryCode::UNKNOWN;
298 case CountryCode::US:
301 case CountryCode::EU:
304 case CountryCode::JP:
311 int result = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
312 SL_WLAN_GENERAL_PARAM_OPT_COUNTRY_CODE, 2,
313 (
const uint8_t*)country);
334 SlWlanScanParamCommand_t param_config = {0};
335 uint16_t option = SL_WLAN_GENERAL_PARAM_OPT_SCAN_PARAMS;
336 uint16_t param_len =
sizeof(param_config);
337 int ret = sl_WlanGet(SL_WLAN_CFG_GENERAL_PARAM_ID, &option, ¶m_len,
338 (_u8 *)¶m_config);
341 if (mask >= 0 && param_config.ChannelsMask != (uint32_t)mask)
343 param_config.ChannelsMask = mask;
346 if (min_rssi < 0 && param_config.RssiThreshold != min_rssi)
348 param_config.RssiThreshold = min_rssi;
355 ret = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
356 SL_WLAN_GENERAL_PARAM_OPT_SCAN_PARAMS,
sizeof(param_config),
357 (_u8 *)¶m_config);
365 const char *key,
unsigned priority)
367 SlWlanSecParams_t sec_params;
368 sec_params.Key = (int8_t*)key;
369 sec_params.KeyLen = (key ==
nullptr) ? 0 : strlen(key);
372 int16_t result = sl_WlanProfileAdd((
const int8_t*)
ssid, strlen(
ssid),
373 nullptr, &sec_params,
nullptr,
376 return (result >= 0) ? result : -1;
384 return sl_WlanProfileDel(index);
392 for (
int i = 0; i < 7; ++i)
401 if (strcmp(name,
ssid) == 0)
404 return sl_WlanProfileDel(i);
418 SlWlanSecParams_t sec_params;
421 int16_t result = sl_WlanProfileGet(index, (int8_t*)
ssid, &ssid_len,
422 nullptr, &sec_params,
nullptr, priority);
429 ssid[ssid_len] =
'\0';
444 for (
int i = 0; i < 7; ++i)
462 SlWlanPmPolicyParams_t params;
463 int length =
sizeof(params);
465 int result = sl_WlanPolicyGet(SL_WLAN_POLICY_PM, &sl_wpp, (uint8_t*)¶ms,
477 case SL_WLAN_ALWAYS_ON_POLICY:
480 case SL_WLAN_LOW_LATENCY_POLICY:
483 case SL_WLAN_NORMAL_POLICY:
486 case SL_WLAN_LOW_POWER_POLICY:
515 sl_wpp = SL_WLAN_ALWAYS_ON_POLICY;
518 sl_wpp = SL_WLAN_LOW_LATENCY_POLICY;
521 sl_wpp = SL_WLAN_NORMAL_POLICY;
524 sl_wpp = SL_WLAN_LOW_POWER_POLICY;
527 result = sl_WlanPolicySet(SL_WLAN_POLICY_PM, sl_wpp, NULL, 0);
530 return (result != 0) ? -1 : 0;
536 uint8_t desired_policy;
538 int length =
sizeof(past_policy);
540 ret = sl_WlanPolicyGet(
541 SL_WLAN_POLICY_CONNECTION, &past_policy, 0, (_u8 *)&length);
544 LOG(
WARNING,
"Failed to get past connection policy.");
548 if (policy == WLAN_CONNECTION_NO_CHANGE)
551 mask = SL_WLAN_CONNECTION_POLICY(1, 0, 1, 1);
552 desired_policy = SL_WLAN_CONNECTION_POLICY(1, 0, 0, 0);
556 mask = SL_WLAN_CONNECTION_POLICY(1, 1, 1, 1);
557 desired_policy = SL_WLAN_CONNECTION_POLICY(
561 if ((past_policy & mask) != (desired_policy & mask))
563 sl_WlanPolicySet(SL_WLAN_POLICY_CONNECTION, desired_policy, NULL, 0);
574 unsigned long intervalInSeconds = 600;
575 sl_WlanPolicySet(SL_WLAN_POLICY_SCAN, 1 , (
unsigned char *)
576 &intervalInSeconds,
sizeof(intervalInSeconds));
584 SlWlanNetworkEntry_t* sl_entries =
new SlWlanNetworkEntry_t[count];
586 int result = sl_WlanGetNetworkList(0, count, sl_entries);
588 for (
int i = 0; i < result; ++i)
590 entries[i].
ssid.assign((
char*)sl_entries[i].Ssid, sl_entries[i].SsidLen);
592 entries[i].
rssi = sl_entries[i].Rssi;
595 delete [] sl_entries;
606 sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET,
nullptr, &len, mac);
614 sl_NetCfgSet(SL_NETCFG_MAC_ADDRESS_SET, 1, 6, mac);
628 #define CHANNEL_MASK_ALL 0x1FFF
629 #define RSSI_TH_MAX -95
631 int mode = sl_Start(0, 0, 0);
632 if (mode != ROLE_STA)
634 mode = sl_WlanSetMode(ROLE_STA);
640 sl_WlanPolicySet(SL_WLAN_POLICY_CONNECTION,
641 SL_WLAN_CONNECTION_POLICY(1, 0, 0, 0),
nullptr, 0);
644 sl_WlanProvisioning(SL_WLAN_PROVISIONING_CMD_STOP, 0xFF, 0,
nullptr, 0);
647 sl_WlanProfileDel(0xFF);
650 sl_NetCfgSet(SL_NETCFG_IPV4_STA_ADDR_MODE, SL_NETCFG_ADDR_DHCP, 0, 0);
653 uint32_t if_bitmap = !(SL_NETCFG_IF_IPV6_STA_LOCAL |
654 SL_NETCFG_IF_IPV6_STA_GLOBAL);
655 sl_NetCfgSet(SL_NETCFG_IF, SL_NETCFG_IF_STATE,
sizeof(if_bitmap),
656 (
const uint8_t*)&if_bitmap);
659 SlWlanScanParamCommand_t scan_default;
660 scan_default.ChannelsMask = CHANNEL_MASK_ALL;
661 scan_default.RssiThreshold = RSSI_TH_MAX;
662 sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
663 SL_WLAN_GENERAL_PARAM_OPT_SCAN_PARAMS,
664 sizeof(scan_default), (uint8_t*)&scan_default);
667 uint8_t config_opt = SL_WLAN_SCAN_POLICY(0, 0);
668 sl_WlanPolicySet(SL_WLAN_POLICY_SCAN, config_opt,
nullptr, 0);
672 sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID,
673 SL_WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (uint8_t*)&power);
676 sl_WlanSet(SL_WLAN_POLICY_PM, SL_WLAN_NORMAL_POLICY, 0, 0);
679 sl_NetAppMDNSUnRegisterService(0, 0, 0);
682 SlWlanRxFilterOperationCommandBuff_t rx_filter_id_mask;
683 memset(rx_filter_id_mask.FilterBitmap, 0xFF, 8);
684 sl_WlanSet(SL_WLAN_RX_FILTERS_ID, SL_WLAN_RX_FILTER_REMOVE,
685 sizeof(SlWlanRxFilterOperationCommandBuff_t),
686 (uint8_t*)&rx_filter_id_mask);
689 sl_WlanSetMode(ROLE_STA);
732 const char* security_key,
738 this->ssid[0] =
'\0';
744 SlWlanSecParams_t sec_params;
745 sec_params.Key = (_i8*)security_key;
746 sec_params.KeyLen = strlen(security_key);
749 int result = sl_WlanConnect((
signed char*)
ssid, strlen(
ssid), 0,
756 return WlanConnectResult::CONNECT_OK;
757 case SL_ERROR_WLAN_PASSWORD_ERROR:
758 return WlanConnectResult::PASSWORD_INVALID;
792 SlWlanSecParams_t sec_params;
793 sec_params.Key = (
signed char*)
"";
794 sec_params.KeyLen = 0;
795 sec_params.Type = SL_WLAN_SEC_TYPE_WPS_PBC;
797 int result = sl_WlanConnect((
signed char*)
"WPS_AP", strlen(
"WPS_AP"), NULL,
811 sl_WlanSet(SL_WLAN_CFG_AP_ID, SL_WLAN_AP_OPT_SSID, strlen(
ssid),
820 sl_WlanSet(SL_WLAN_CFG_AP_ID, SL_WLAN_AP_OPT_SECURITY_TYPE, 1,
821 (uint8_t*)&sec_type);
823 if (sec_type == SL_WLAN_SEC_TYPE_OPEN ||
824 security_key ==
nullptr)
829 HASSERT(strlen(security_key) <= 64);
830 sl_WlanSet(SL_WLAN_CFG_AP_ID, SL_WLAN_AP_OPT_PASSWORD,
831 strlen(security_key), (uint8_t*)security_key);
844 uint16_t len =
ssid->size();
845 uint16_t config_opt = SL_WLAN_AP_OPT_SSID;
846 sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt, &len, (_u8*) &(*
ssid)[0]);
851 uint16_t len =
sizeof(*security_type);
852 uint16_t config_opt = SL_WLAN_AP_OPT_SECURITY_TYPE;
853 uint8_t sl_sec_type = 0;
854 sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt, &len, &sl_sec_type);
865 uint8_t num_connected = 0;
866 uint16_t len =
sizeof(num_connected);
867 auto status = sl_NetCfgGet(
868 SL_NETCFG_AP_STATIONS_NUM_CONNECTED, NULL, &len, &num_connected);
873 return num_connected;
893 long result = sl_Start(0, 0, 0);
894 if (result == SL_ERROR_ROLE_STA_ERR)
897 result = sl_Start(0, 0, 0);
902 (
wlanRole == WlanRole::UNKNOWN && result == ROLE_AP))
905 if (result != ROLE_AP)
907 sl_WlanSetMode(ROLE_AP);
912 uint16_t len =
sizeof(
ssid);
913 memset(
ssid, 0, len);
914 uint16_t config_opt = SL_WLAN_AP_OPT_SSID;
915 sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt, &len, (_u8 *)
ssid);
927 if (result != ROLE_STA)
929 sl_WlanSetMode(ROLE_STA);
952 sl_WlanSetMode(ROLE_STA);
955 sl_WlanSetMode(ROLE_AP);
958 DIE(
"Unsupported wlan role");
971 vTaskPrioritySet(NULL, configMAX_PRIORITIES / 2);
973 SlSockAddrIn_t address;
975 wakeup = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0);
978 address.sin_family = SL_AF_INET;
979 address.sin_port = sl_Htons(8000);
980 address.sin_addr.s_addr = SL_INADDR_ANY;
981 result = sl_Bind(
wakeup, (SlSockAddr_t*)&address,
sizeof(address));
984 portENTER_CRITICAL();
994 std::vector<std::function<void()> > callbacks_to_run;
1001 for (
unsigned i = 0; i < callbacks_to_run.size(); ++i) {
1002 callbacks_to_run[i]();
1005 SlFdSet_t rfds_tmp =
rfds;
1006 SlFdSet_t wfds_tmp =
wfds;
1007 SlFdSet_t efds_tmp =
efds;
1012 result = sl_Select(
fdHighest + 1, &rfds_tmp, &wfds_tmp, &efds_tmp, &tv);
1023 SlWlanGetRxStatResponse_t response;
1024 if (sl_WlanRxStatGet(&response, 0) == 0)
1026 if (response.AvarageMgMntRssi) {
1027 rssi = response.AvarageMgMntRssi;
1032 for (
int i = 0; i < SL_MAX_SOCKETS && result > 0; ++i)
1039 if (SL_SOCKET_FD_ISSET(
slSockets[i], &wfds_tmp))
1042 portENTER_CRITICAL();
1048 portEXIT_CRITICAL();
1050 if (SL_SOCKET_FD_ISSET(
slSockets[i], &rfds_tmp))
1057 sl_Recv(
wakeup, &data, 1, 0);
1062 portENTER_CRITICAL();
1068 portEXIT_CRITICAL();
1071 if (SL_SOCKET_FD_ISSET(
slSockets[i], &efds_tmp))
1074 portENTER_CRITICAL();
1077 portEXIT_CRITICAL();
1091 SlSockAddrIn_t address;
1092 SlSocklen_t length =
sizeof(SlSockAddr_t);
1093 address.sin_family = SL_AF_INET;
1094 address.sin_port = sl_Htons(8000);
1095 address.sin_addr.s_addr = sl_Htonl(SL_IPV4_VAL(127,0,0,1));
1104 ssize_t result = sl_SendTo(
wakeup, &data, 1, 0, (SlSockAddr_t*)&address,
1109 result = sl_SendTo(
wakeup, &data, 1, 0, (SlSockAddr_t*)&address,
1119 callbacks_.emplace_back(std::move(callback));
1129 portENTER_CRITICAL();
1130 SL_SOCKET_FD_CLR(sd, &
rfds);
1131 SL_SOCKET_FD_CLR(sd, &
wfds);
1132 SL_SOCKET_FD_CLR(sd, &
efds);
1134 portEXIT_CRITICAL();
1142 portENTER_CRITICAL();
1143 if (SL_SOCKET_FD_ISSET(sd, &
rfds))
1146 portEXIT_CRITICAL();
1149 SL_SOCKET_FD_SET(sd, &
rfds);
1151 portEXIT_CRITICAL();
1160 portENTER_CRITICAL();
1161 if (SL_SOCKET_FD_ISSET(sd, &
wfds))
1164 portEXIT_CRITICAL();
1167 SL_SOCKET_FD_SET(sd, &
wfds);
1169 portEXIT_CRITICAL();
1178 if (event ==
nullptr)
1185 case SL_WLAN_EVENT_CONNECT:
1193 auto connect = &
event->Data.Connect;
1194 portENTER_CRITICAL();
1197 portEXIT_CRITICAL();
1210 case SL_WLAN_EVENT_DISCONNECT:
1212 const auto *disconnect = &
event->Data.Disconnect;
1218 if (SL_WLAN_DISCONNECT_SECURITY_FAILURE == disconnect->ReasonCode)
1232 case SL_WLAN_EVENT_STA_ADDED:
1235 case SL_WLAN_EVENT_STA_REMOVED:
1238 case SL_WLAN_EVENT_PROVISIONING_STATUS:
1239 LOG(
INFO,
"provisioning status %u %u %u", event->Data.ProvisioningStatus.ProvisioningStatus, event->Data.ProvisioningStatus.Role, event->Data.ProvisioningStatus.WlanStatus);
1242 case SL_WLAN_EVENT_PROVISIONING_PROFILE_ADDED:
1243 LOG(
INFO,
"provisioning profile added %s %s", event->Data.ProvisioningProfileAdded.Ssid, event->Data.ProvisioningProfileAdded.Reserved);
1257 if (event ==
nullptr)
1264 case SL_NETAPP_EVENT_DHCP_IPV4_ACQUIRE_TIMEOUT:
1267 case SL_NETAPP_EVENT_IPV4_ACQUIRED:
1269 const auto* ip_aquired = &
event->Data.IpAcquiredV4;
1273 case SL_NETAPP_EVENT_IPV6_ACQUIRED:
1296 case SL_NETAPP_EVENT_DHCPV4_LEASED:
1311 case SL_NETAPP_EVENT_IP_COLLISION:
1313 case SL_NETAPP_EVENT_IPV4_LOST:
1322 case SL_NETAPP_EVENT_DHCPV4_RELEASED:
1346 if (event ==
nullptr)
1354 switch (event->Event)
1356 case SL_SOCKET_TX_FAILED_EVENT:
1357 LOG(
ALWAYS,
"Socket tx fail status %d, sd %u",
1358 (
int)event->SocketAsyncEvent.SockTxFailData.Status,
1359 (
unsigned)event->SocketAsyncEvent.SockTxFailData.Sd);
1360 switch (event->SocketAsyncEvent.SockTxFailData.Status)
1362 case SL_ERROR_BSD_ECLOSE:
1368 case SL_SOCKET_ASYNC_EVENT:
1369 LOG(
ALWAYS,
"Socket async event %d, sd %u type %u val %d",
1370 (
int)event->SocketAsyncEvent.SockAsyncData.Type,
1371 (
unsigned)event->SocketAsyncEvent.SockAsyncData.Sd,
1372 (
unsigned)event->SocketAsyncEvent.SockAsyncData.Type,
1373 (
int)event->SocketAsyncEvent.SockAsyncData.Val);
1374 switch (event->SocketAsyncEvent.SockAsyncData.Type)
1381 LOG(
ALWAYS,
"Socket event %d", (
int)event->Event);
1396 LOG(
INFO,
"Socket trigger event %u %d", (
unsigned)event->Event,
1397 (
unsigned)event->EventData);
1406 if(!event || !response)
1411 switch (event->Event)
1413 case SL_NETAPP_EVENT_HTTP_TOKEN_GET:
1416 ptr = response->ResponseData.TokenValue.pData;
1417 response->ResponseData.TokenValue.Len = 0;
1418 string token((
const char *)event->EventData.HttpTokenName.pData,
1419 event->EventData.HttpTokenName.Len);
1429 if (result.size() >= SL_NETAPP_MAX_TOKEN_VALUE_LEN)
1431 result.resize(SL_NETAPP_MAX_TOKEN_VALUE_LEN);
1433 memcpy(ptr, result.data(), result.size());
1434 ptr += result.size();
1435 response->ResponseData.TokenValue.Len += result.size();
1442 case SL_NETAPP_EVENT_HTTP_TOKEN_POST:
1445 (
const char *)event->EventData.HttpPostData.TokenName.pData,
1446 event->EventData.HttpPostData.TokenName.Len);
1448 (
const char *)event->EventData.HttpPostData.TokenValue.pData,
1449 event->EventData.HttpPostData.TokenValue.Len);
1450 LOG(
VERBOSE,
"token post %s=%s", token.c_str(), val.c_str());
1464 if (!request || !response || request->AppId != SL_NETAPP_HTTP_SERVER_ID)
1470 uint32_t content_length = 0xFFFFFFFFu;
1472 uint8_t *meta_curr = request->requestData.pMetadata;
1473 uint8_t *meta_end = meta_curr + request->requestData.MetadataLen;
1474 while (meta_curr < meta_end)
1476 uint8_t meta_type = *meta_curr;
1478 uint16_t meta_len = *(_u16 *)meta_curr;
1482 case SL_NETAPP_REQUEST_METADATA_TYPE_HTTP_CONTENT_LEN:
1483 memcpy(&content_length, meta_curr, meta_len);
1485 case SL_NETAPP_REQUEST_METADATA_TYPE_HTTP_REQUEST_URI:
1486 uri.assign((
const char *)meta_curr, meta_len);
1489 meta_curr += meta_len;
1492 bool has_more = request->requestData.Flags &
1493 SL_NETAPP_REQUEST_RESPONSE_FLAGS_CONTINUATION;
1495 switch (request->Type)
1497 case SL_NETAPP_REQUEST_HTTP_POST:
1506 content_length, request->requestData.pMetadata,
1507 request->requestData.MetadataLen,
1508 request->requestData.pPayload,
1509 request->requestData.PayloadLen, has_more);
1520 response->Status = SL_NETAPP_HTTP_RESPONSE_404_NOT_FOUND;
1521 response->ResponseData.pMetadata = NULL;
1522 response->ResponseData.MetadataLen = 0;
1523 response->ResponseData.pPayload = NULL;
1524 response->ResponseData.PayloadLen = 0;
1525 response->ResponseData.Flags = 0;
1535 uint16_t ulen = *len;
1537 int ret = sl_NetAppRecv(handle, &ulen, (uint8_t *)buf, &flags);
1538 if (ret < 0 || (flags & SL_NETAPP_REQUEST_RESPONSE_FLAGS_ERROR))
1544 return (flags & SL_NETAPP_REQUEST_RESPONSE_FLAGS_CONTINUATION) != 0;
1551 uint16_t handle, uint16_t http_status,
const string &redirect)
1556 if (!redirect.empty())
1558 http_status = SL_NETAPP_HTTP_RESPONSE_302_MOVED_TEMPORARILY;
1560 md.push_back(SL_NETAPP_REQUEST_METADATA_TYPE_STATUS);
1562 md.append((
const char *)&len, 2);
1563 md.append((
const char *)&http_status, 2);
1564 if (!redirect.empty())
1566 md.push_back(SL_NETAPP_REQUEST_METADATA_TYPE_HTTP_LOCATION);
1567 len = redirect.size();
1568 md.append((
const char *)&len, 2);
1573 void *buf = malloc(md.size());
1574 memcpy(buf, md.data(), md.size());
1576 uint32_t flags = SL_NETAPP_REQUEST_RESPONSE_FLAGS_METADATA;
1577 sl_NetAppSend(handle, md.size(), (uint8_t *)buf, flags);
1585 LOG(
WARNING,
"Simplelink experienced a fatal error.");
1589static void append_num(std::string* s, uint32_t d) {
1591 integer_to_buffer(d, num);
1597static void append_ver_4(std::string* s, NUM d[4]) {
1598 for (
int i = 0; i < 4; ++i) {
1599 if (i) s->push_back(
'.');
1600 append_num(s, d[i]);
1606 SlDeviceVersion_t ver;
1607 uint8_t config_opt = SL_DEVICE_GENERAL_VERSION;
1608 uint16_t config_len =
sizeof(ver);
1610 int lRetVal = sl_DeviceGet(SL_DEVICE_GENERAL, &config_opt,
1611 &config_len, (
unsigned char *)(&ver));
1615 v += SL_DRIVER_VERSION;
1617 append_ver_4(&v, ver.NwpVersion);
1619 append_ver_4(&v, ver.FwVersion);
1622 pv[0] = ver.PhyVersion[0];
1623 pv[1] = ver.PhyVersion[1];
1624 pv[2] = ver.PhyVersion[2];
1625 pv[3] = ver.PhyVersion[3];
1626 append_ver_4(&v, pv);
1628 append_num(&v, ver.RomVersion);
1630 append_num(&v, ver.ChipId);
1641 string full_name(name);
1642 full_name.push_back(
'.');
1643 full_name.append(service);
1644 full_name.append(
".local");
1645 sl_NetAppMDNSRegisterService((
const signed char *)full_name.c_str(),
1646 full_name.size(), (
const signed char *)
"OLCB", strlen(
"OLCB"), port,
1647 200, SL_NETAPP_MDNS_OPTIONS_IS_NOT_PERSISTENT);
1655 string full_name(name);
1656 full_name.push_back(
'.');
1657 full_name.append(service);
1658 full_name.append(
".local");
1659 sl_NetAppMDNSUnRegisterService((
const signed char *)full_name.c_str(),
1660 full_name.size(), SL_NETAPP_MDNS_OPTIONS_IS_NOT_PERSISTENT);
1707 SlNetAppHttpServerEvent_t *pHttpServerEvent,
1708 SlNetAppHttpServerResponse_t *pHttpServerResponse)
1723 SlNetAppResponse_t *pNetAppResponse)
1726 "netapprq app %d type %d hdl %d mdlen %u payloadlen %u flags %u",
1727 pNetAppRequest->AppId, pNetAppRequest->Type, pNetAppRequest->Handle,
1728 pNetAppRequest->requestData.MetadataLen,
1729 pNetAppRequest->requestData.PayloadLen,
1730 (
unsigned)pNetAppRequest->requestData.Flags);
void SlCheckResult(int result, int expected)
Tests that a SimpleLink request has succeeded.
void SlCheckError(int result)
Tests that a SimpleLink request has succeeded (return >= 0).
static OSEvent wakeup
event used to wakeup select calls
int bind(int socket, const struct sockaddr *address, socklen_t address_len)
Bind a name to a socket.
int connect(int socket, const struct sockaddr *address, socklen_t address_len)
Connect a socket.
void resetblink(uint32_t pattern)
Changes the blinking pattern.
char WIFI_SSID[]
Name of wifi accesspoint to connect to.
char WIFI_PASS[]
Password of wifi connection. If empty, use no encryption.
Socket implementation for the CC32xx class WiFi devices.
uint8_t readActive
indicates our "best guess" at current socket's read active status
static CC32xxSocket * get_instance_from_sd(int sd)
Get the CC32xxSocket instance given a specific CC32xx socket descriptor.
uint8_t writeActive
indicates our "best guess" at current socket's write active status
SecurityType
Security types.
@ SEC_WEP
WEP security mode.
@ SEC_OPEN
open (no security)
@ SEC_WPA2
WPA2 security mode.
string http_get_ip_address()
Get the IP address for a http request.
bool wlan_profile_test_none()
Test if there are any saved profiles.
unsigned securityFailure
Disconnected due to wrong password.
void net_app_event_handler(NetAppEvent *event)
This function handles network events such as IP acquisition, IP leased, IP released etc.
void trigger_event_handler(SockTriggerEvent *event)
Notifies the service about a wifi asynchronous socket event callback.
int wlan_power_policy_set(WlanPowerPolicy wpp)
Set the power policy.
int wakeup
loopback socket to wakeup select()
void wlan_connection_policy_set(WlanConnectionPolicy policy)
Sets connection policy to auto connect.
std::vector< std::pair< const char *, PostFunction > > httpPostCallbacks_
List of callbacks for http post handlers.
void netapp_request_callback(NetAppRequest *request, NetAppResponse *response)
This function handles netapp request callbacks.
void stop()
Stops the Wi-Fi in preparation for a reboot.
void set_default_state()
Set the CC32xx to its default state, including station mode.
std::function< void(bool)> ipAcquiredCallback_
Callback for when IP is acquired.
void wlan_setup_ap(const char *ssid, const char *security_key, SecurityType security_type) override
Setup access point role credentials.
void wlan_set_scan_params(int mask, int min_rssi)
Sets the scan parameters.
void wlan_event_handler(WlanEvent *event)
This function handles WLAN events.
void wlan_get_ap_config(string *ssid, SecurityType *security_type)
Retrieve current AP config.
unsigned started
network processor started
void wlan_mac(uint8_t mac[6])
Get the device MAC address.
int16_t rssi
receive signal strength indicator
int wlan_get_ap_station_count()
Retrieves how many stations are connected to the wifi in AP mode.
void fatal_error_callback(FatalErrorEvent *event)
This Function Handles the Fatal errors.
void wlan_set_role(WlanRole new_role)
Change the default Wlan Role.
int wlan_profile_del(const char *ssid)
Delete a saved WLAN profile.
char ssid[33]
SSID of AP, or AP we are connected to.
void fd_remove(int16_t sd)
Remove a socket from the known sockets that might be part of the sl_Select fdset.
WlanRole wlanRole
the Wi-Fi role we are in
OSMutex lock_
Protects callbacks_ vector.
void connecting_update_blinker()
Updates the blinker based on connection state.
uint32_t ipAddress
assigned IP adress
void sock_event_handler(SockEvent *event)
This function handles socket events indication.
void run_on_network_thread(std::function< void()> callback)
Executes the given function on the network thread.
unsigned connected
AP connected state.
WlanConnectionPolicy
The Wlan reconnect policy.
@ WLAN_CONNECTION_FAST_RECONNECT
Reconnect to the last connected AP.
void fd_set_read(int16_t sd)
Add socket to the read fd set.
CountryCode wlan_country_code_get()
Get the current country code.
int wlan_profile_add(const char *ssid, SecurityType sec_type, const char *key, unsigned priority)
Add a saved WLAN profile.
static SecurityType security_type_from_simplelink(uint8_t sec_type)
Translates the SimpleLink code to SecurityType enum.
void start(WlanRole role=WlanRole::STA, WlanPowerPolicy power_policy=WLAN_NO_CHANGE_POLICY, WlanConnectionPolicy connection_policy=WLAN_CONNECTION_NO_CHANGE)
Startup the Wi-Fi.
std::vector< std::function< void()> > callbacks_
List of callbacks to execute on the network thread.
int wlan_profile_get(int index, char ssid[], SecurityType *sec_type, uint32_t *priority)
Get a saved WLAN profile by index.
WlanConnectionPolicy connectionPolicy
scan or reconnect to last AP
int wlan_network_list_get(WlanNetworkEntry *entries, size_t count)
Get a list of available networks.
static uint8_t security_type_to_simplelink(SecurityType sec_type)
Translates the SecurityType enum to the internal SimpleLink code.
void fd_set_write(int16_t sd)
Add socket to the write fd set.
void wlan_task()
Thread that will manage the WLAN connection inside object context.
void http_server_callback(HttpServerEvent *event, HttpServerResponse *response)
This function handles http server callback indication.
void select_wakeup()
Asynchronously wakeup the select call.
unsigned ipAcquired
IP address aquired state.
WlanPowerPolicy wlanPowerPolicy
the desired power policy
static std::string get_version()
Returns a string contianing the version numbers of the network interface.
void wlan_rescan()
Initiate rescanning available networks.
static void * wlan_task_entry(void *context)
Thread that will manage the WLAN connection.
void test_mode_start()
Startup the Wi-Fi in test mode.
void wlan_wps_pbc_initiate()
Initiate a WPS Push Button Control connection.
WlanConnectResult wlan_connect(const char *ssid, const char *security_key, SecurityType security_type)
Connect to access point.
WlanPowerPolicy
The WLAN power policy.
@ WLAN_ALWAYS_ON_POLICY
WLAN power policy always on.
@ WLAN_NORMAL_POLICY
WLAN power policy normal.
@ WLAN_LOW_LATENCY_POLICY
WLAN power policy low latency.
@ WLAN_LOW_POWER_POLICY
WLAN power policy low power.
@ WLAN_NO_CHANGE_POLICY
WLAN power policy to be left alone.
void wlan_set_mac(uint8_t mac[6])
Sets the device MAC address.
void add_http_get_token_callback(const char *token_name, std::function< std::string()> callback)
Add an HTTP get token callback.
void send_post_respose(uint16_t handle, uint16_t http_status=204, const string &redirect="")
Sends a POST response.
int wlan_country_code_set(CountryCode cc, bool restart=false)
Set the current country code.
static SecurityType security_type_from_scan(unsigned sec_type)
Translates the SimpleLink code from the network scan to SecurityType enum.
unsigned connectionFailed
Connection attempt failed status.
std::vector< std::pair< const char *, std::function< std::string()> > > httpGetTokenCallbacks_
List of callbacks for http get tokens.
unsigned ipLeased
IP address leased to a client(AP mode)
bool get_post_data(uint16_t handle, void *buf, size_t *len)
Retrieves additional payload for http POST operations.
void wlan_disconnect()
Disconnects from the current AP.
int wlan_power_policy_get(WlanPowerPolicy *wpp)
Get the power policy.
Class to allow convenient locking and unlocking of mutexes in a C context.
static int get_priority_max()
Get the maximum thread priority.
static CC32xxWiFi * instance()
SelectInfo selInfoRd
select wakeup metadata for read active
SelectInfo selInfoWr
select wakeup metadata for write active
#define LOG(level, message...)
Conditionally write a message to the logging output.
static const int VERBOSE
Loglevel that is usually not printed, reporting debugging information.
static const int WARNING
Loglevel that is always printed, reporting a warning or a retryable error.
static const int INFO
Loglevel that is printed by default, reporting some status information.
static const int ALWAYS
Loglevel that is always printed.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
#define DIE(MSG)
Unconditionally terminates the current process with a message.
static int fdHighest
the highest file descriptor to select on
void SimpleLinkSocketTriggerEventHandler(SlSockTriggerEvent_t *event)
Notifies the service about a wifi asynchronous socket event callback.
void SimpleLinkNetAppRequestEventHandler(SlNetAppRequest_t *pNetAppRequest, SlNetAppResponse_t *pNetAppResponse)
This function handles resource request.
void mdns_publish(const char *name, const char *service, uint16_t port)
Publish an mDNS name.
void mdns_unpublish(const char *name, const char *service)
Unpublish an mDNS name.
void new_highest()
Find the new highest fd to select on.
void SimpleLinkHttpServerEventHandler(SlNetAppHttpServerEvent_t *pHttpServerEvent, SlNetAppHttpServerResponse_t *pHttpServerResponse)
This function gets triggered when HTTP Server receives Application defined GET and POST HTTP Tokens.
static int16_t slSockets[SL_MAX_SOCKETS]
This is not a class members so that including CC32xxWiFi.hxx does not pollute the namespace with simp...
void SimpleLinkNetAppRequestMemFreeEventHandler(uint8_t *buffer)
This function handles resource release.
void SimpleLinkFatalErrorEventHandler(SlDeviceFatal_t *slFatalErrorEvent)
This Function Handles the Fatal errors.
void SimpleLinkSockEventHandler(SlSockEvent_t *pSock)
This function handles socket events indication.
void add_socket(int16_t sd)
Add an interesting socket.
void del_socket(int16_t sd)
Delete an interesting socket.
void SimpleLinkWlanEventHandler(SlWlanEvent_t *pSlWlanEvent)
This function handles WLAN events.
void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent)
This function handles general events.
int slcb_SetErrno(int Errno)
Helper function called by SimpleLink driver to set OS-specific errno value.
static SlFdSet_t wfds
This is not a class members so that including CC32xxWiFi.hxx does not pollute the namespace with simp...
static SlFdSet_t efds
This is not a class members so that including CC32xxWiFi.hxx does not pollute the namespace with simp...
void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent)
This function handles network events such as IP acquisition, IP leased, IP released etc.
static SlFdSet_t rfds
This is not a class members so that including CC32xxWiFi.hxx does not pollute the namespace with simp...
long long os_get_time_monotonic(void)
Get the monotonic time since the system started.
int os_thread_create(os_thread_t *thread, const char *name, int priority, size_t stack_size, void *(*start_routine)(void *), void *arg)
Create a thread.
OS_INLINE int os_thread_get_priority_max(void)
Get the maximum thread priority.
#define MSEC_TO_USEC(_msec)
Convert a millisecond value to a microsecond value.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
CC32xx forward declaration Helper.
metadata for a WLAN netowrk entry.
int rssi
receive signal strength indicator of the AP
SecurityType sec_type
security type of the AP
static void select_wakeup(SelectInfo *info)
Wakeup the list of clients needing woken.