Skip to content

Commit fc03bd2

Browse files
Kludentwonashif
authored andcommitted
drivers: wifi: winc1500: Updated driver capabilities.
Added ap_enable and ap_disable api. The driver will open create an access point with DHCP Server ip 192.168.1.1 and no security. Added a small fix for the AF_INET issue. Added parent and remote to accept routine context. Added put implementation. Signed-off-by: Nicolai Glud <[email protected]>
1 parent d24ca1b commit fc03bd2

File tree

1 file changed

+86
-26
lines changed

1 file changed

+86
-26
lines changed

drivers/wifi/winc1500/wifi_winc1500.c

Lines changed: 86 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ NMI_API sint16 send(SOCKET sock, void *pvSendBuffer,
5454
NMI_API sint16 sendto(SOCKET sock, void *pvSendBuffer,
5555
uint16 u16SendLength, uint16 flags,
5656
struct sockaddr *pstrDestAddr, uint8 u8AddrLen);
57+
NMI_API sint8 close(SOCKET sock);
5758

5859
enum socket_errors {
5960
SOCK_ERR_NO_ERROR = 0,
@@ -303,7 +304,11 @@ static int winc1500_get(sa_family_t family,
303304
return -1;
304305
}
305306

306-
sock = socket(family, type, 0);
307+
/* winc1500 atmel uses AF_INET 2 instead of zephyrs AF_INET 1
308+
* we have checked if family is AF_INET so we can hardcode this
309+
* for now.
310+
*/
311+
sock = socket(2, type, 0);
307312
if (sock < 0) {
308313
LOG_ERR("socket error!");
309314
return -1;
@@ -421,6 +426,7 @@ static int winc1500_accept(struct net_context *context,
421426
int ret;
422427

423428
w1500_data.socket_data[socket].accept_cb = cb;
429+
w1500_data.socket_data[socket].accept_user_data = user_data;
424430

425431
ret = accept(socket, NULL, 0);
426432
if (ret) {
@@ -555,14 +561,18 @@ static int winc1500_recv(struct net_context *context,
555561
SOCKET socket = (int) context->offload_context;
556562
int ret;
557563

564+
w1500_data.socket_data[socket].recv_cb = cb;
565+
w1500_data.socket_data[socket].recv_user_data = user_data;
566+
if (!cb) {
567+
return 0;
568+
}
569+
558570
ret = prepare_pkt(&w1500_data.socket_data[socket]);
559571
if (ret) {
560572
LOG_ERR("Could not reserve packet buffer");
561573
return -ENOMEM;
562574
}
563575

564-
w1500_data.socket_data[socket].recv_cb = cb;
565-
w1500_data.socket_data[socket].recv_user_data = user_data;
566576

567577
ret = recv(socket, w1500_data.socket_data[socket].pkt_buf->data,
568578
CONFIG_WIFI_WINC1500_MAX_PACKET_SIZE, timeout);
@@ -580,7 +590,19 @@ static int winc1500_recv(struct net_context *context,
580590
*/
581591
static int winc1500_put(struct net_context *context)
582592
{
583-
return 0;
593+
SOCKET sock = (int) context->offload_context;
594+
struct socket_data *sd = &w1500_data.socket_data[sock];
595+
int ret;
596+
597+
memset(&(context->remote), 0, sizeof(struct sockaddr_in));
598+
context->flags &= ~NET_CONTEXT_REMOTE_ADDR_SET;
599+
ret = close(sock);
600+
601+
net_pkt_unref(sd->rx_pkt);
602+
603+
memset(sd, 0, sizeof(struct socket_data));
604+
605+
return ret;
584606
}
585607

586608
static struct net_offload winc1500_offload = {
@@ -799,18 +821,6 @@ static bool handle_socket_msg_recv(SOCKET sock,
799821
return false;
800822
}
801823
}
802-
803-
if (prepare_pkt(sd)) {
804-
LOG_ERR("Could not reserve packet buffer");
805-
return false;
806-
}
807-
808-
if (recv(sock, sd->pkt_buf->data,
809-
CONFIG_WIFI_WINC1500_MAX_PACKET_SIZE, 0)) {
810-
LOG_ERR("Could not receive packet in the buffer");
811-
return false;
812-
}
813-
814824
return true;
815825
}
816826

@@ -883,17 +893,33 @@ static void handle_socket_msg_accept(struct socket_data *sd, void *pvMsg)
883893
IPPROTO_TCP, &a_sd->context);
884894
if (ret < 0) {
885895
LOG_ERR("Cannot get new net context for ACCEPT");
886-
} else {
887-
a_sd->context->offload_context =
888-
(void *)((int)accept_msg->sock);
889-
890-
sd->accept_cb(a_sd->context,
891-
(struct sockaddr *)&accept_msg->strAddr,
892-
sizeof(struct sockaddr_in),
893-
(accept_msg->sock > 0) ?
894-
0 : accept_msg->sock,
895-
sd->accept_user_data);
896+
return;
896897
}
898+
/* We get a new socket from accept_msg but we need a new
899+
* context as well. The new context gives us another socket
900+
* so we have to close that one first.
901+
*/
902+
close((int)a_sd->context->offload_context);
903+
904+
a_sd->context->offload_context =
905+
(void *)((int)accept_msg->sock);
906+
/** The iface is reset when getting a new context. */
907+
a_sd->context->iface = sd->context->iface;
908+
909+
/** Setup remote */
910+
a_sd->context->remote.sa_family = AF_INET;
911+
net_sin(&a_sd->context->remote)->sin_port =
912+
accept_msg->strAddr.sin_port;
913+
net_sin(&a_sd->context->remote)->sin_addr.s_addr =
914+
accept_msg->strAddr.sin_addr.s_addr;
915+
a_sd->context->flags |= NET_CONTEXT_REMOTE_ADDR_SET;
916+
917+
sd->accept_cb(a_sd->context,
918+
(struct sockaddr *)&accept_msg->strAddr,
919+
sizeof(struct sockaddr_in),
920+
(accept_msg->sock > 0) ?
921+
0 : accept_msg->sock,
922+
sd->accept_user_data);
897923
}
898924
}
899925

@@ -1027,6 +1053,38 @@ static int winc1500_mgmt_disconnect(const struct device *device)
10271053
return 0;
10281054
}
10291055

1056+
static int winc1500_mgmt_ap_enable(const struct device *dev,
1057+
struct wifi_connect_req_params *params)
1058+
{
1059+
tstrM2MAPConfig strM2MAPConfig;
1060+
1061+
memset(&strM2MAPConfig, 0x00, sizeof(tstrM2MAPConfig));
1062+
strcpy((char *)&strM2MAPConfig.au8SSID, params->ssid);
1063+
strM2MAPConfig.u8ListenChannel = params->channel;
1064+
/** security is hardcoded as open for now */
1065+
strM2MAPConfig.u8SecType = M2M_WIFI_SEC_OPEN;
1066+
/** DHCP: 192.168.1.1 */
1067+
strM2MAPConfig.au8DHCPServerIP[0] = 0xC0;
1068+
strM2MAPConfig.au8DHCPServerIP[1] = 0xA8;
1069+
strM2MAPConfig.au8DHCPServerIP[2] = 0x01;
1070+
strM2MAPConfig.au8DHCPServerIP[3] = 0x01;
1071+
1072+
if (m2m_wifi_enable_ap(&strM2MAPConfig) != M2M_SUCCESS) {
1073+
return -EIO;
1074+
}
1075+
1076+
return 0;
1077+
}
1078+
1079+
static int winc1500_mgmt_ap_disable(const struct device *dev)
1080+
{
1081+
if (m2m_wifi_disable_ap() != M2M_SUCCESS) {
1082+
return -EIO;
1083+
}
1084+
1085+
return 0;
1086+
}
1087+
10301088
static void winc1500_iface_init(struct net_if *iface)
10311089
{
10321090
LOG_DBG("eth_init:net_if_set_link_addr:"
@@ -1047,6 +1105,8 @@ static const struct net_wifi_mgmt_offload winc1500_api = {
10471105
.scan = winc1500_mgmt_scan,
10481106
.connect = winc1500_mgmt_connect,
10491107
.disconnect = winc1500_mgmt_disconnect,
1108+
.ap_enable = winc1500_mgmt_ap_enable,
1109+
.ap_disable = winc1500_mgmt_ap_disable,
10501110
};
10511111

10521112
static int winc1500_init(const struct device *dev)

0 commit comments

Comments
 (0)