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