wifi.c
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014-2019 Inertial Sense, Inc. - http://inertialsense.com
5 
6 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 :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 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.
11 */
12 
13 // #define WIFI_AUTH M2M_WIFI_SEC_WPA_PSK
14 // #define WIFI_SSID "BobaMachine"
15 // #define WIFI_PSK "pineapple"
16 
17 // #define WIFI_AUTH M2M_WIFI_SEC_WPA_PSK
18 // #define WIFI_SSID "Walt Johnson's iPhone"
19 // #define WIFI_PSK "magicctech"
20 
21 #define WIFI_AUTH M2M_WIFI_SEC_WPA_PSK
22 #define MAIN_WIFI_M2M_PRODUCT_NAME "EVB-2"
23 
24 #include <asf.h>
25 #include <string.h>
26 #include "wifi.h"
28 #include "driver/source/nmasic.h"
30 #include "socket/include/socket.h"
31 #include "globals.h"
32 
35 
38 static int g_init_wifi = 0; // 1=enable, -1=disable
39 static uint8_t g_wifi_connected = 0;
40 
41 static struct sockaddr_in g_addr = {0};
42 
44 typedef struct s_msg_wifi_product {
45  uint8_t name[20];
47 
50 };
51 
53 #define WIFI_BUFFER_SIZE 1460
56 static int g_indicateWiFiRxMs=0;
57 
58 
84 static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
85 {
86  switch (u8MsgType)
87  {
89  {
90  tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg;
91  if (pstrWifiState->u8CurrState == M2M_WIFI_CONNECTED)
92  {
93  g_wifi_connected = 1;
96  } else if (pstrWifiState->u8CurrState == M2M_WIFI_DISCONNECTED)
97  {
98  g_wifi_connected = 0;
100  char* wifi_ssid = (char*)(g_flashCfg->wifi[EVB_CFG_BITS_IDX_WIFI(g_flashCfg->bits)].ssid);
101  char* wifi_psk = (char*)(g_flashCfg->wifi[EVB_CFG_BITS_IDX_WIFI(g_flashCfg->bits)].psk);
102  m2m_wifi_connect(wifi_ssid, strlen(wifi_ssid), WIFI_AUTH, wifi_psk, M2M_WIFI_CH_ALL);
103  }
104 
105  break;
106  }
107 
109  {
110 // uint8_t *pu8IPAddress = (uint8_t *)pvMsg;
111 // printf("wifi_cb: M2M_WIFI_REQ_DHCP_CONF: IP is %u.%u.%u.%u\r\n", pu8IPAddress[0], pu8IPAddress[1], pu8IPAddress[2], pu8IPAddress[3]);
112  g_status.wifiIpAddr = *(uint32_t *)pvMsg;
113  break;
114  }
115 
116  default:
117  {
118  break;
119  }
120  }
121 }
122 
123 
144 static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg)
145 {
146  switch (u8Msg) {
147  /* Socket connected */
148  case SOCKET_MSG_CONNECT:
149  {
150  tstrSocketConnectMsg *pstrConnect = (tstrSocketConnectMsg *)pvMsg;
151  if (pstrConnect && pstrConnect->s8Error >= 0)
152  {
153  //socket_cb: connect success!
154  //Start receive process
156 // send(tcp_client_socket, &msg_wifi_product, sizeof(t_msg_wifi_product), 0);
157  }
158  else
159  {
160  //socket_cb: connect error!
162  tcp_client_socket = -1;
163  }
164  }
165  break;
166 
167  /* Message send */
168  case SOCKET_MSG_SEND:
169  {
170  //socket_cb: send success!
172  }
173  break;
174 
175  /* Message receive */
176  case SOCKET_MSG_RECV:
177  {
178  tstrSocketRecvMsg *pstrRecv = (tstrSocketRecvMsg *)pvMsg;
179  if (pstrRecv && pstrRecv->s16BufferSize > 0) {
180  //socket_cb: recv success!
181  g_indicateWiFiRxMs = 20;
182 
184  {
185 #if 0 // Send data to com task
187 #else
189  comWrite(EVB2_PORT_UINS1, socketRxBuffer, pstrRecv->s16BufferSize, LED_INS_TXD_PIN);
191 #endif
192  }
193 
194  //Start new receive process
196  } else {
197  //socket_cb: recv error!
199  tcp_client_socket = -1;
200  }
201  }
202 
203  break;
204 
205  default:
206  break;
207  }
208 }
209 
210 
211 static void init_tcp_socket(void)
212 {
213  if(tcp_client_socket != -1)
214  {
216  tcp_client_socket = -1;
217  }
218 
219  /* Initialize socket module */
220  socketInit();
222 }
223 
224 
225 static void deinit_tcp_socket(void)
226 {
227  if(tcp_client_socket != -1)
228  {
230  tcp_client_socket = -1;
231  socketDeinit();
232  }
233 }
234 
235 
236 void wifi_enable(int enable)
237 {
238  if(enable)
239  { // Init wifi ONLY if it was off
241  {
242  g_init_wifi = 1;
243  }
244  }
245  else
246  { // Turn off wifi
247  g_init_wifi = -1;
248  }
249 }
250 
251 
252 void wifi_reinit(void)
253 {
254  g_init_wifi = 1;
255 }
256 
257 
258 static void init_wifi(void)
259 {
260  g_wifi_connected = 0;
263 
265  int8_t ret;
266 
267  /* Initialize the BSP. */
268  nm_bsp_init();
269 
270  /* Initialize Wi-Fi parameters structure. */
271  memset((uint8_t *)&param, 0, sizeof(tstrWifiInitParam));
272 
273  /* Initialize socket address structure. */
274  g_addr.sin_family = AF_INET;
277 
278  /* Initialize Wi-Fi driver with data and status callbacks. */
279  param.pfAppWifiCb = wifi_cb;
280  ret = m2m_wifi_reinit(&param);
281  if (M2M_SUCCESS != ret)
282  {
283  //m2m_wifi_init call error!
284  while (1) {}
285  }
286 
287  /* Initialize socket module */
288  init_tcp_socket();
289 
290  /* Connect to router. */
291  char* wifi_ssid = (char*)(g_flashCfg->wifi[EVB_CFG_BITS_IDX_WIFI(g_flashCfg->bits)].ssid);
292  char* wifi_psk = (char*)(g_flashCfg->wifi[EVB_CFG_BITS_IDX_WIFI(g_flashCfg->bits)].psk);
293  m2m_wifi_connect(wifi_ssid, strlen(wifi_ssid), WIFI_AUTH, wifi_psk, M2M_WIFI_CH_ALL);
294 }
295 
296 
297 static void deinit_wifi(void)
298 {
301 
302  /* Power down the BSP. */
303  nm_bsp_deinit();
304 
305  g_wifi_connected = 0;
308 }
309 
310 
311 static void update_wifi_leds(rtos_task_t * task)
312 {
314  {
315  // Inverted LED. On shows hot spot connectivity and blink off shows data received.
316  if(g_indicateWiFiRxMs > 0)
317  {
320  }
321  else
322  {
324  }
325  }
326  else
327  {
329  }
330 
332 }
333 
334 
335 static void step_wifi(rtos_task_t * task)
336 {
337  int8_t ret;
338 
339  /* Handle pending events from network controller. */
341 
343  {
344  /* Open client socket. */
345  if (tcp_client_socket < 0) // Open socket
346  {
347  if ((tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
348  {
349  //Failed to create TCP client socket error!
350  return;
351  }
352 
353  /* Connect server */
354  ret = connect(tcp_client_socket, (struct sockaddr *)&g_addr, sizeof(struct sockaddr_in));
355 
356  if (ret < 0)
357  {
359  tcp_client_socket = -1;
360  }
361  }
362  else // Socket open, process data
363  {
364  int len = 0;
365 
366  // Forward uINS Ser1 port data to WiFi socket
368  {
369 #if 0
371 #else
373  len = comRead(EVB2_PORT_UINS1, socketTxBuffer, WIFI_BUFFER_SIZE, LED_INS_RXD_PIN);
375 #endif
376  if(len > 0)
377  {
378  send(tcp_client_socket, socketTxBuffer, len, 1000);
380  }
381  }
382  }
383 
384  // Cause loop to sleep a while
385  vTaskDelay(task->periodMs);
386  }
387  else
388  { // We are not connected, wait and try again
390  vTaskDelay(500);
391  }
392 }
393 
394 
395 void vTaskWiFi(void *pvParameters)
396 {
397  (void)pvParameters;
399 
402 
403  while (1)
404  {
405  switch(g_init_wifi)
406  {
407  case 1:
408  g_init_wifi = 0;
409  deinit_wifi();
410  init_wifi();
411  break;
412 
413  case -1: // Disabled wifi
414  g_init_wifi = 0;
415  deinit_wifi();
416  break;
417  }
418 
419  step_wifi(task);
420 
421  update_wifi_leds(task); // Call right after step_wifi() (task delay)
422  }
423 }
uint32_t bits
Definition: data_sets.h:3154
NMI_API sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
Definition: socket.c:674
#define LED_ON(led)
Definition: user_board.h:236
NMI_API sint8 m2m_wifi_connect(char *pcSsid, uint8 u8SsidLen, uint8 u8SecType, void *pvAuthInfo, uint16 u16Ch)
Asynchronous API to request connection to a specified access point.
Definition: m2m_wifi.c:577
This module contains common APIs declarations.
bool param(const std::string &param_name, T &param_val, const T &default_val)
uint32_t cbf[EVB2_PORT_COUNT]
Definition: data_sets.h:3148
#define LED_OFF(led)
Definition: user_board.h:235
static void socket_cb(SOCKET sock, uint8_t u8Msg, void *pvMsg)
Callback to get the Data from socket.
Definition: wifi.c:144
NMI_API SOCKET socket(uint16 u16Domain, uint8 u8Type, uint8 u8Flags)
Definition: socket.c:477
uint16 sin_port
Definition: socket.h:462
static struct sockaddr_in g_addr
Definition: wifi.c:41
int comWrite(int serialNum, const unsigned char *buf, int size, uint32_t ledPin)
uint32_t evbStatus
Definition: data_sets.h:3069
#define LED_WIFI_RXD_PIN
Definition: user_board.h:285
NMI_API sint8 m2m_wifi_request_dhcp_client(void)
Legacy (deprecated) Asynchronous API for starting a DHCP client on the WINC IC.
Definition: m2m_wifi.c:717
NMI_API sint8 m2m_wifi_deinit(void *arg)
Synchronous API that de-initialises the WINC driver.
Definition: m2m_wifi.c:534
NMI_API sint16 recv(SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32Timeoutmsec)
Definition: socket.c:822
Socket recv status.
Definition: socket.h:651
evb_rtos_info_t g_rtos
Definition: globals.c:27
Structure, holding the Wi-fi configuration attributes such as the wi-fi callback , monitoring mode callback and Ethernet parameter initialization structure. Such configuration parameters are required to be set before calling the wi-fi initialization function m2m_wifi_init. pfAppWifiCb attribute must be set to handle the wi-fi callback operations. pfAppMonCb attribute, is optional based on whether the application requires the monitoring mode configuration, and can there not be set before the initialization. strEthInitParam structure, is another optional configuration based on whether the bypass mode is set.
Definition: m2m_wifi.h:398
size_t xStreamBufferReceive(StreamBufferHandle_t xStreamBuffer, void *pvRxData, size_t xBufferLengthBytes, TickType_t xTicksToWait) PRIVILEGED_FUNCTION
#define M2M_SUCCESS
Definition: nm_common.h:51
#define AF_INET
Definition: socket.h:72
static t_msg_wifi_product msg_wifi_product
Definition: wifi.c:48
Socket address structure for IPV4 addresses. Used to specify socket address information to connect to...
Definition: socket.h:455
rtos_task_t task[UINS_RTOS_NUM_TASKS]
Definition: data_sets.h:3457
#define NULL
Definition: nm_bsp.h:52
static uint8_t socketTxBuffer[WIFI_BUFFER_SIZE]
Definition: wifi.c:55
#define LED_INS_TXD_PIN
Definition: user_board.h:280
#define LED_WIFI_TXD_PIN
Definition: user_board.h:286
char psk[WIFI_SSID_PSK_SIZE]
Definition: data_sets.h:3093
uint32_t port
Definition: data_sets.h:3106
static int g_indicateWiFiRxMs
Definition: wifi.c:56
static void update_wifi_leds(rtos_task_t *task)
Definition: wifi.c:311
uint32_t u32
Definition: data_sets.h:3101
uint8_t name[20]
Definition: wifi.c:45
WINC3400 IoT Application Interface.
#define NULLPTR
Definition: ISConstants.h:426
#define WIFI_BUFFER_SIZE
Definition: wifi.c:53
This struct contains the Wi-Fi connection state.
Definition: m2m_types.h:1399
#define END_CRITICAL_SECTION
Definition: rtos.h:34
#define STREAM_BUFFER_SIZE
Definition: globals.h:82
This module contains WINC3400 ASIC specific internal APIs.
evb_status_t g_status
Definition: globals.c:21
StreamBufferHandle_t g_xStreamBufferWiFiTx
Generic socket address structure.
Definition: socket.h:436
NMI_API void socketInit(void)
Definition: socket.c:394
static void init_wifi(void)
Definition: wifi.c:258
StreamBufferHandle_t g_xStreamBufferWiFiRx
void wifi_enable(int enable)
Definition: wifi.c:236
NMI_API sint8 m2m_wifi_handle_events(void *arg)
Synchronous M2M event handler function.
Definition: m2m_wifi.c:567
static void step_wifi(rtos_task_t *task)
Definition: wifi.c:335
NMI_API void socketDeinit(void)
Socket Layer De-initialization.
Definition: socket.c:423
static void deinit_tcp_socket(void)
Definition: wifi.c:225
void wifi_reinit(void)
Definition: wifi.c:252
int comRead(int serialNum, unsigned char *buf, int bufSize, uint32_t ledPin)
#define BEGIN_CRITICAL_SECTION
Definition: rtos.h:33
sint8 nm_bsp_init(void)
Definition: nm_bsp_same70.c:71
uint32_t wifiIpAddr
Definition: data_sets.h:3078
union evb_server_t::@40 ipAddr
evb_server_t server[NUM_WIFI_PRESETS]
Definition: data_sets.h:3169
Socket connect status.
Definition: socket.h:621
static void init_tcp_socket(void)
Definition: wifi.c:211
char ssid[WIFI_SSID_PSK_SIZE]
Definition: data_sets.h:3090
#define EVB_CFG_BITS_IDX_SERVER(bits)
Definition: data_sets.h:3125
uint32_t periodMs
Definition: data_sets.h:3423
static int g_init_wifi
Definition: wifi.c:38
static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
Callback to get the Wi-Fi status update.
Definition: wifi.c:84
#define MAIN_WIFI_M2M_PRODUCT_NAME
Definition: wifi.c:22
NMI_API sint8 close(SOCKET sock)
Definition: socket.c:879
static uint8_t g_wifi_connected
Definition: wifi.c:39
static SOCKET tcp_client_socket
Definition: wifi.c:37
evb_flash_cfg_t * g_flashCfg
Definition: globals.c:22
#define LED_INS_RXD_PIN
Definition: user_board.h:279
#define _htons(A)
Definition: socket.h:276
void vTaskWiFi(void *pvParameters)
Definition: wifi.c:395
struct s_msg_wifi_product t_msg_wifi_product
size_t xStreamBufferSend(StreamBufferHandle_t xStreamBuffer, const void *pvTxData, size_t xDataLengthBytes, TickType_t xTicksToWait) PRIVILEGED_FUNCTION
void * StreamBufferHandle_t
Definition: stream_buffer.h:65
void vTaskDelay(const TickType_t xTicksToDelay) PRIVILEGED_FUNCTION
uint16 sin_family
Definition: socket.h:456
sint8 m2m_wifi_reinit(tstrWifiInitParam *param)
Deinitialize and reinitialize wifi. Resets the WINC. Parameter may be set to NULL, in which case there is no change to the parameters given at initialization.
Definition: m2m_wifi.c:552
Autogenerated API include file for the Atmel Software Framework (ASF)
static void deinit_wifi(void)
Definition: wifi.c:297
sint8 nm_bsp_deinit(void)
De-iInitialize BSP.
Definition: nm_bsp_same70.c:89
NMI_API sint16 send(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 u16Flags)
Definition: socket.c:715
BSD compatible socket interface.
uint32 s_addr
Definition: socket.h:418
#define EVB_CFG_BITS_IDX_WIFI(bits)
Definition: data_sets.h:3124
in_addr sin_addr
Definition: socket.h:469
evb_wifi_t wifi[NUM_WIFI_PRESETS]
Definition: data_sets.h:3166
sint8 SOCKET
Definition for socket handler data type. Socket ID,used with all socket operations to uniquely identi...
NMI_API void registerSocketCallback(tpfAppSocketCb socket_cb, tpfAppResolveCb resolve_cb)
Definition: socket.c:451
sint16 s16BufferSize
Definition: socket.h:656
#define WIFI_AUTH
Definition: wifi.c:21
static uint8_t socketRxBuffer[WIFI_BUFFER_SIZE]
Definition: wifi.c:54
#define SOCK_STREAM
Definition: socket.h:79


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58