Mistake on this page?
Report an issue in GitHub or email us
CellularUtil.h
1 /*
2  * Copyright (c) 2017, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef CELLULAR_UTIL_H_
19 #define CELLULAR_UTIL_H_
20 
21 #include <stddef.h>
22 #include <inttypes.h>
23 #include "nsapi_types.h"
24 
25 namespace mbed {
26 
27 typedef enum pdp_type {
28  DEFAULT_PDP_TYPE = DEFAULT_STACK,
29  IPV4_PDP_TYPE = IPV4_STACK,
30  IPV6_PDP_TYPE = IPV6_STACK,
31  IPV4V6_PDP_TYPE = IPV4V6_STACK,
32  NON_IP_PDP_TYPE
33 } pdp_type_t;
34 }
35 namespace mbed_cellular_util {
36 
37 // some helper macros
38 #define EMPTY_CHECK(val) (val ## 1)
39 #define EMPTY(val) (EMPTY_CHECK(val) == 1)
40 #define _CELLULAR_STRINGIFY(a) #a
41 #define CELLULAR_STRINGIFY(a) _CELLULAR_STRINGIFY(a)
42 
43 static const char hex_values[] = "0123456789ABCDEF";
44 
45 /** Converts the given IP address to proper IPv6 address if needed.
46  * Conversion is done only if it's NOT IPv4 and separated with colons.
47  * 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
48  * where ax are in decimal format. In this case, function converts decimals to hex with separated with colons.
49  *
50  * @param ip IP address that can be IPv4 or IPv6 in different formats from AT command +CGPADDR. Converted result uses same buffer.
51  * @return IP version of the address or NSAPI_UNSPEC if param ip empty or if IPv4 or IPv6 version could not be concluded.
52  */
53 nsapi_version_t convert_ipv6(char *ip);
54 
55 /** Separates IP addresses from the given 'orig' string. 'orig' may contain zero, one or two IP addresses in various formats.
56  * See AT command +CGPIAF from 3GPP TS 27.007 for details. Does also needed conversions for IPv6 addresses.
57  *
58  * @param orig original string that contains zero, one or two IP addressees in various formats
59  * @param ip preallocated buffer that might contain IP address after return
60  * @param ip_size size of preallocated buffer IP
61  * @param ip2 preallocated buffer that might contain IP address after return
62  * @param ip2_size size of preallocated buffer ip2
63  *
64  */
65 void separate_ip_addresses(char *orig, char *ip, size_t ip_size, char *ip2, size_t ip2_size);
66 
67 /** Swaps the arrays if param IP does not contain IPv6 address but param ip2 does.
68  *
69  * @param ip IP address
70  * @param ip_size size of buffer ip
71  * @param ip2 IP address
72  * @param ip2_size size of buffer ip2
73  */
74 void prefer_ipv6(char *ip, size_t ip_size, char *ip2, size_t ip2_size);
75 
76 /** Converts the given int to two hex characters
77  *
78  * @param num number to be converted to hex string
79  * @param buf preallocated buffer that will contain 2 char length hex value
80  */
81 void int_to_hex_str(uint8_t num, char *buf);
82 
83 /** Converts the given buffer 'str' to hex buffer 'buf. First 'len' char's are converted to two hex bytes.
84  *
85  * @param str char buffer that is to be converted to hex
86  * @param len how many chars from str are to be converted
87  * @param buf destination buffer for hex converted chars. Buffer should be double the size of str to fit hex-encoded string.
88  * @param omit_leading_zero if true then possible leading zeroes are omitted
89  */
90 int char_str_to_hex_str(const char *str, uint16_t len, char *buf, bool omit_leading_zero = false);
91 
92 /** Converts the given hex string to integer
93  *
94  * @param hex_string hex string from where chars are converted to int
95  * @param hex_string_length length of the param hex_string
96  * @return hex_string converted to int
97  */
98 int hex_str_to_int(const char *hex_string, int hex_string_length);
99 
100 /** Converts the given hex string str to char string to buf
101  *
102  * @param str hex string that is converted to char string to buf
103  * @param len length of the param str/how many hex are converted
104  * @param buf preallocated buffer where result conversion is stored
105  * @return length of the buf
106  */
107 int hex_str_to_char_str(const char *str, uint16_t len, char *buf);
108 
109 /** Converts the given hex string to char
110  *
111  * @param str A hex value that is converted to char
112  * @param buf A char variable where result conversion is stored
113  */
114 void hex_to_char(const char *hex, char &buf);
115 
116 /** Converts the given uint to binary string. Fills the given str starting from [0] with the number of bits defined by bit_cnt
117  * For example uint_to_binary_string(9, str, 10) would fill str "0000001001"
118  * For example uint_to_binary_string(9, str, 3) would fill str "001"
119  *
120  * @param num uint to converts to binary string
121  * @param str buffer for converted binary string
122  * @param str_size size of the str buffer
123  * @param bit_cnt defines how many bits are filled to buffer started from lsb
124  */
125 void uint_to_binary_str(uint32_t num, char *str, int str_size, int bit_cnt);
126 
127 /** Converts the given binary string to uint.
128  * For example binary_str_to_uint("0000001001", 10) would return 9
129  *
130  * @param binary_string binary string from where chars are converted to uint
131  * @param binary_string_length length of the param binary_string
132  * @return uint represented by the binary string
133  */
134 uint32_t binary_str_to_uint(const char *binary_string, int binary_string_length);
135 
136 /** Get dynamic port for socket
137  *
138  * @return next port number above 49152
139  */
140 uint16_t get_dynamic_ip_port();
141 
142 /** Converts the given pdp type in char format to enum pdp_type_t
143  *
144  * @param pdp_type pdp type in string format
145  * @return converted pdp_type_t enum
146  */
147 mbed::pdp_type_t string_to_pdp_type(const char *pdp_type);
148 
149 } // namespace mbed_cellular_util
150 
151 #endif
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.