1 /***************************************************************************/ /**
2  * @file
3  * @brief
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 #include "sl_si91x_protocol_types.h"
31 #include "sl_si91x_constants.h"
32 #include "sl_net_constants.h"
33 #include "sl_wifi_constants.h"
34 #include "sl_status.h"
35 #include "sl_constants.h"
36 #include "sl_wifi_types.h"
37 #include "sl_net_rsi_utility.h"
38 #include <string.h>
39 
convert_rsi_ipv4_address_to_sl_ip_address(sl_ip_address_t * ip_address_buffer,const sl_si91x_rsp_ipv4_params_t * ip_params)40 sl_status_t convert_rsi_ipv4_address_to_sl_ip_address(sl_ip_address_t *ip_address_buffer,
41                                                       const sl_si91x_rsp_ipv4_params_t *ip_params)
42 {
43   // Verify input pointers
44   SL_VERIFY_POINTER_OR_RETURN(ip_address_buffer, SL_STATUS_WIFI_NULL_PTR_ARG);
45   SL_VERIFY_POINTER_OR_RETURN(ip_params, SL_STATUS_WIFI_NULL_PTR_ARG);
46 
47   uint8_t *ip_address;
48 
49   ip_address_buffer->type = SL_IPV4;
50   ip_address              = ip_address_buffer->ip.v4.bytes;
51 
52   memcpy(ip_address, ip_params->ipaddr, sizeof(sl_ipv4_address_t));
53   return SL_STATUS_OK;
54 }
55 
convert_si91x_dns_response(sl_ip_address_t * ip_address,const sl_si91x_dns_response_t * si91x_dns_response)56 sl_status_t convert_si91x_dns_response(sl_ip_address_t *ip_address, const sl_si91x_dns_response_t *si91x_dns_response)
57 {
58   SL_VERIFY_POINTER_OR_RETURN(ip_address, SL_STATUS_WIFI_NULL_PTR_ARG);
59   SL_VERIFY_POINTER_OR_RETURN(si91x_dns_response, SL_STATUS_WIFI_NULL_PTR_ARG);
60 
61   // Check if DNS response has IP addresses
62   if ((si91x_dns_response->ip_count[0] | (si91x_dns_response->ip_count[1] << 8)) <= 0) {
63     return SL_STATUS_OK;
64   }
65 
66   // Determine IP address size (IPv4 or IPv6) and copy the address bytes
67   uint8_t ip_address_size = (si91x_dns_response->ip_version[0] | si91x_dns_response->ip_version[1] << 8)
68                                 == SL_IPV4_ADDRESS_LENGTH
69                               ? SL_IPV4_ADDRESS_LENGTH
70                               : SL_IPV6_ADDRESS_LENGTH;
71   uint8_t *sl_ip_address;
72   const uint8_t *si91x_ip_address;
73 
74   ip_address->type = ip_address_size == SL_IPV4_ADDRESS_LENGTH ? SL_IPV4 : SL_IPV6;
75 
76   si91x_ip_address = ip_address_size == SL_IPV4_ADDRESS_LENGTH ? si91x_dns_response->ip_address[0].ipv4_address
77                                                                : si91x_dns_response->ip_address[0].ipv6_address;
78   sl_ip_address    = ip_address_size == SL_IPV4_ADDRESS_LENGTH ? ip_address->ip.v4.bytes : ip_address->ip.v6.bytes;
79 
80   memcpy(sl_ip_address, si91x_ip_address, ip_address_size);
81 
82   return SL_STATUS_OK;
83 }
84 
convert_si91x_event_to_sl_net_event(const uint16_t * event,sl_net_event_t * sl_net_event)85 sl_status_t convert_si91x_event_to_sl_net_event(const uint16_t *event, sl_net_event_t *sl_net_event)
86 {
87   // Verify input pointers
88   SL_WIFI_ARGS_CHECK_NULL_POINTER(event);
89   SL_WIFI_ARGS_CHECK_NULL_POINTER(sl_net_event);
90 
91   // Map SI91X events to SimpleLink network events
92   switch (*event) {
93     case RSI_WLAN_RSP_DNS_QUERY: {
94       *sl_net_event = SL_NET_DNS_RESOLVE_EVENT;
95       return SL_STATUS_OK;
96     }
97     case RSI_WLAN_RSP_PING_PACKET: {
98       *sl_net_event = SL_NET_PING_RESPONSE_EVENT;
99       return SL_STATUS_OK;
100     }
101     case RSI_WLAN_RSP_OTA_FWUP: {
102       *sl_net_event = SL_NET_OTA_FW_UPDATE_EVENT;
103       return SL_STATUS_OK;
104     }
105     case RSI_WLAN_RSP_IPCONFV4: {
106       *sl_net_event = SL_NET_DHCP_NOTIFICATION_EVENT;
107       return SL_STATUS_OK;
108     }
109     case RSI_WLAN_RSP_IPV4_CHANGE:
110     case RSI_WLAN_RSP_IPCONFV6: {
111       *sl_net_event = SL_NET_IP_ADDRESS_CHANGE_EVENT;
112       return SL_STATUS_OK;
113     }
114     default:
115       break;
116   }
117 
118   return SL_STATUS_FAIL;
119 }
120 
121 #ifdef SLI_SI91X_INTERNAL_HTTP_CLIENT
122 // Convert integer to string
convert_itoa(uint32_t val,uint8_t * str)123 void convert_itoa(uint32_t val, uint8_t *str)
124 {
125   int16_t ii = 0;
126   int16_t jj = 0;
127   uint8_t tmp[10];
128 
129   if (val == 0) {
130     // if value is zero then handling
131     str[jj] = '0';
132     jj++;
133     str[jj] = '\0';
134     return;
135   }
136 
137   // Convert the integer to a string
138   while (val) {
139     tmp[ii] = '0' + (val % 10);
140     val /= 10;
141     ii++;
142   }
143 
144   // Reverse the string
145   for (jj = 0, ii--; ii >= 0; ii--, jj++) {
146     str[jj] = tmp[ii];
147   }
148   str[jj] = '\0';
149 }
150 
convert_si91x_event_to_sl_http_client_event(const uint16_t * event,sl_http_client_event_t * sl_http_client_event)151 sl_status_t convert_si91x_event_to_sl_http_client_event(const uint16_t *event,
152                                                         sl_http_client_event_t *sl_http_client_event)
153 {
154   // Verify input pointer
155   SL_WIFI_ARGS_CHECK_NULL_POINTER(event);
156 
157   // Map SI91X HTTP client events to SimpleLink HTTP client events
158   switch (*event) {
159     case RSI_WLAN_RSP_HTTP_CLIENT_GET: {
160       *sl_http_client_event = SL_HTTP_CLIENT_GET_RESPONSE_EVENT;
161       return SL_STATUS_OK;
162     }
163 
164     case RSI_WLAN_RSP_HTTP_CLIENT_POST:
165     case RSI_WLAN_RSP_HTTP_CLIENT_POST_DATA: {
166       *sl_http_client_event = SL_HTTP_CLIENT_POST_RESPONSE_EVENT;
167       return SL_STATUS_OK;
168     }
169 
170     case RSI_WLAN_RSP_HTTP_CLIENT_PUT: {
171       *sl_http_client_event = SL_HTTP_CLIENT_PUT_RESPONSE_EVENT;
172       return SL_STATUS_OK;
173     }
174     default:
175       break;
176   }
177 
178   return SL_STATUS_FAIL;
179 }
180 #endif
181