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 "netsocket/nsapi_types.h"
24 
25 namespace mbed_cellular_util {
26 
27 // some helper macros
28 #define EMPTY_CHECK(val) (val ## 1)
29 #define EMPTY(val) (EMPTY_CHECK(val) == 1)
30 #define _CELLULAR_STRINGIFY(a) #a
31 #define CELLULAR_STRINGIFY(a) _CELLULAR_STRINGIFY(a)
32 
33 static const char hex_values[] = "0123456789ABCDEF";
34 
35 /** Converts the given IP address to proper IPv6 address if needed.
36  * Conversion is done only if it's NOT IPv4 and separated with colons.
37  * 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
38  * where ax are in decimal format. In this case, function converts decimals to hex with separated with colons.
39  *
40  * @param ip IP address that can be IPv4 or IPv6 in different formats from AT command +CGPADDR. Converted result uses same buffer.
41  * @return IP version of the address or NSAPI_UNSPEC if param ip empty or if IPv4 or IPv6 version could not be concluded.
42  */
43 nsapi_version_t convert_ipv6(char *ip);
44 
45 /** Separates IP addresses from the given 'orig' string. 'orig' may contain zero, one or two IP addresses in various formats.
46  * See AT command +CGPIAF from 3GPP TS 27.007 for details. Does also needed conversions for IPv6 addresses.
47  *
48  * @param orig original string that contains zero, one or two IP addressees in various formats
49  * @param ip preallocated buffer that might contain IP address after return
50  * @param ip_size size of preallocated buffer IP
51  * @param ip2 preallocated buffer that might contain IP address after return
52  * @param ip2_size size of preallocated buffer ip2
53  *
54  */
55 void separate_ip_addresses(char *orig, char *ip, size_t ip_size, char *ip2, size_t ip2_size);
56 
57 /** Swaps the arrays if param IP does not contain IPv6 address but param ip2 does.
58  *
59  * @param ip IP address
60  * @param ip_size size of buffer ip
61  * @param ip2 IP address
62  * @param ip2_size size of buffer ip2
63  */
64 void prefer_ipv6(char *ip, size_t ip_size, char *ip2, size_t ip2_size);
65 
66 /** Converts the given int to two hex characters
67  *
68  * @param num number to be converted to hex string
69  * @param buf preallocated buffer that will contain 2 char length hex value
70  */
71 void int_to_hex_str(uint8_t num, char *buf);
72 
73 /** Converts the given buffer 'str' to hex buffer 'buf. First 'len' char's are converted to two hex bytes.
74  *
75  * @param str char buffer that is to be converted to hex
76  * @param len how many chars from str are to be converted
77  * @param buf destination buffer for hex converted chars. Buffer should be double the size of str to fit hex-encoded string.
78  * @param omit_leading_zero if true then possible leading zeroes are omitted
79  */
80 int char_str_to_hex_str(const char *str, uint16_t len, char *buf, bool omit_leading_zero = false);
81 
82 /** Converts the given hex string to integer
83  *
84  * @param hex_string hex string from where chars are converted to int
85  * @param hex_string_length length of the param hex_string
86  * @return hex_string converted to int
87  */
88 int hex_str_to_int(const char *hex_string, int hex_string_length);
89 
90 /** Converts the given hex string str to char string to buf
91  *
92  * @param str hex string that is converted to char string to buf
93  * @param len length of the param str/how many hex are converted
94  * @param buf preallocated buffer where result conversion is stored
95  * @return length of the buf
96  */
97 int hex_str_to_char_str(const char *str, uint16_t len, char *buf);
98 
99 /** Converts the given hex string to char
100  *
101  * @param str A hex value that is converted to char
102  * @param buf A char variable where result conversion is stored
103  */
104 void hex_to_char(const char *hex, char &buf);
105 
106 /** Converts the given uint to binary string. Fills the given str starting from [0] with the number of bits defined by bit_cnt
107  * For example uint_to_binary_string(9, str, 10) would fill str "0000001001"
108  * For example uint_to_binary_string(9, str, 3) would fill str "001"
109  *
110  * @param num uint to converts to binary string
111  * @param str buffer for converted binary string
112  * @param str_size size of the str buffer
113  * @param bit_cnt defines how many bits are filled to buffer started from lsb
114  */
115 void uint_to_binary_str(uint32_t num, char *str, int str_size, int bit_cnt);
116 
117 /** Converts the given binary string to uint.
118  * For example binary_str_to_uint("0000001001", 10) would return 9
119  *
120  * @param binary_string binary string from where chars are converted to uint
121  * @param binary_string_length length of the param binary_string
122  * @return uint represented by the binary string
123  */
124 uint32_t binary_str_to_uint(const char *binary_string, int binary_string_length);
125 
126 /** Get dynamic port for socket
127  *
128  * @return next port number above 49152
129  */
130 uint16_t get_dynamic_ip_port();
131 
132 } // namespace mbed_cellular_util
133 
134 #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.