Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
CellularUtil.h
00001 /* 00002 * Copyright (c) 2017, Arm Limited and affiliates. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #ifndef CELLULAR_UTIL_H_ 00019 #define CELLULAR_UTIL_H_ 00020 00021 #include <stddef.h> 00022 #include <inttypes.h> 00023 #include "nsapi_types.h" 00024 00025 namespace mbed { 00026 00027 typedef enum pdp_type { 00028 DEFAULT_PDP_TYPE = DEFAULT_STACK, 00029 IPV4_PDP_TYPE = IPV4_STACK, 00030 IPV6_PDP_TYPE = IPV6_STACK, 00031 IPV4V6_PDP_TYPE = IPV4V6_STACK, 00032 NON_IP_PDP_TYPE 00033 } pdp_type_t; 00034 } 00035 namespace mbed_cellular_util { 00036 00037 // some helper macros 00038 #define EMPTY_CHECK(val) (val ## 1) 00039 #define EMPTY(val) (EMPTY_CHECK(val) == 1) 00040 #define _CELLULAR_STRINGIFY(a) #a 00041 #define CELLULAR_STRINGIFY(a) _CELLULAR_STRINGIFY(a) 00042 00043 static const char hex_values[] = "0123456789ABCDEF"; 00044 00045 /** Converts the given IP address to proper IPv6 address if needed. 00046 * Conversion is done only if it's NOT IPv4 and separated with colons. 00047 * AT command +CGPADDR can give IP address in format of a1.a2.a3.a4.a5.a6.a7.a8.a9.a10.a11.a12.a13.a14.a15.a16 for IPv6 00048 * where ax are in decimal format. In this case, function converts decimals to hex with separated with colons. 00049 * 00050 * @param ip IP address that can be IPv4 or IPv6 in different formats from AT command +CGPADDR. Converted result uses same buffer. 00051 * @return IP version of the address or NSAPI_UNSPEC if param ip empty or if IPv4 or IPv6 version could not be concluded. 00052 */ 00053 nsapi_version_t convert_ipv6(char *ip); 00054 00055 /** Separates IP addresses from the given 'orig' string. 'orig' may contain zero, one or two IP addresses in various formats. 00056 * See AT command +CGPIAF from 3GPP TS 27.007 for details. Does also needed conversions for IPv6 addresses. 00057 * 00058 * @param orig original string that contains zero, one or two IP addressees in various formats 00059 * @param ip preallocated buffer that might contain IP address after return 00060 * @param ip_size size of preallocated buffer IP 00061 * @param ip2 preallocated buffer that might contain IP address after return 00062 * @param ip2_size size of preallocated buffer ip2 00063 * 00064 */ 00065 void separate_ip_addresses(char *orig, char *ip, size_t ip_size, char *ip2, size_t ip2_size); 00066 00067 /** Swaps the arrays if param IP does not contain IPv6 address but param ip2 does. 00068 * 00069 * @param ip IP address 00070 * @param ip_size size of buffer ip 00071 * @param ip2 IP address 00072 * @param ip2_size size of buffer ip2 00073 */ 00074 void prefer_ipv6(char *ip, size_t ip_size, char *ip2, size_t ip2_size); 00075 00076 /** Converts the given int to two hex characters 00077 * 00078 * @param num number to be converted to hex string 00079 * @param buf preallocated buffer that will contain 2 char length hex value 00080 */ 00081 void int_to_hex_str(uint8_t num, char *buf); 00082 00083 /** Converts the given buffer 'str' to hex buffer 'buf. First 'len' char's are converted to two hex bytes. 00084 * 00085 * @param str char buffer that is to be converted to hex 00086 * @param len how many chars from str are to be converted 00087 * @param buf destination buffer for hex converted chars. Buffer should be double the size of str to fit hex-encoded string. 00088 * @param omit_leading_zero if true then possible leading zeroes are omitted 00089 */ 00090 int char_str_to_hex_str(const char *str, uint16_t len, char *buf, bool omit_leading_zero = false); 00091 00092 /** Converts the given hex string to integer 00093 * 00094 * @param hex_string hex string from where chars are converted to int 00095 * @param hex_string_length length of the param hex_string 00096 * @return hex_string converted to int 00097 */ 00098 int hex_str_to_int(const char *hex_string, int hex_string_length); 00099 00100 /** Converts the given hex string str to char string to buf 00101 * 00102 * @param str hex string that is converted to char string to buf 00103 * @param len length of the param str/how many hex are converted 00104 * @param buf preallocated buffer where result conversion is stored 00105 * @return length of the buf 00106 */ 00107 int hex_str_to_char_str(const char *str, uint16_t len, char *buf); 00108 00109 /** Converts the given hex string to char 00110 * 00111 * @param str A hex value that is converted to char 00112 * @param buf A char variable where result conversion is stored 00113 */ 00114 void hex_to_char(const char *hex, char &buf); 00115 00116 /** Converts the given uint to binary string. Fills the given str starting from [0] with the number of bits defined by bit_cnt 00117 * For example uint_to_binary_string(9, str, 10) would fill str "0000001001" 00118 * For example uint_to_binary_string(9, str, 3) would fill str "001" 00119 * 00120 * @param num uint to converts to binary string 00121 * @param str buffer for converted binary string 00122 * @param str_size size of the str buffer 00123 * @param bit_cnt defines how many bits are filled to buffer started from lsb 00124 */ 00125 void uint_to_binary_str(uint32_t num, char *str, int str_size, int bit_cnt); 00126 00127 /** Converts the given binary string to uint. 00128 * For example binary_str_to_uint("0000001001", 10) would return 9 00129 * 00130 * @param binary_string binary string from where chars are converted to uint 00131 * @param binary_string_length length of the param binary_string 00132 * @return uint represented by the binary string 00133 */ 00134 uint32_t binary_str_to_uint(const char *binary_string, int binary_string_length); 00135 00136 /** Get dynamic port for socket 00137 * 00138 * @return next port number above 49152 00139 */ 00140 uint16_t get_dynamic_ip_port(); 00141 00142 /** Converts the given pdp type in char format to enum pdp_type_t 00143 * 00144 * @param pdp_type pdp type in string format 00145 * @return converted pdp_type_t enum 00146 */ 00147 mbed::pdp_type_t string_to_pdp_type(const char *pdp_type); 00148 00149 } // namespace mbed_cellular_util 00150 00151 #endif
Generated on Tue Jul 12 2022 13:54:05 by
1.7.2