Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CellularUtil.h Source File

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 
00024 namespace mbed_cellular_util {
00025 
00026 // some helper macros
00027 #define EMPTY_CHECK(val) (val ## 1)
00028 #define EMPTY(val) (EMPTY_CHECK(val) == 1)
00029 #define _CELLULAR_STRINGIFY(a) #a
00030 #define CELLULAR_STRINGIFY(a) _CELLULAR_STRINGIFY(a)
00031 
00032 static const char hex_values[] = "0123456789ABCDEF";
00033 
00034 /** Converts the given IP address to proper IPv6 address if needed.
00035  *  Conversion is done only if it's NOT IPv4 and separated with colons.
00036  *  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
00037  *  where ax are in decimal format. In this case, function converts decimals to hex with separated with colons.
00038  *
00039  *  @param ip       IP address that can be IPv4 or IPv6 in different formats from AT command +CGPADDR. Converted result uses same buffer.
00040  */
00041 void convert_ipv6(char* ip);
00042 
00043 /** Separates IP addresses from the given 'orig' string. 'orig' may contain zero, one or two IP addresses in various formats.
00044  *  See AT command +CGPIAF from 3GPP TS 27.007 for details. Does also needed conversions for IPv6 addresses.
00045  *
00046  *  @param orig          original string that contains zero, one or two IP addressees in various formats
00047  *  @param ip            preallocated buffer that might contain IP address after return
00048  *  @param ip_size       size of preallocated buffer IP
00049  *  @param ip2           preallocated buffer that might contain IP address after return
00050  *  @param ip2_size      size of preallocated buffer ip2
00051  *
00052  */
00053 void separate_ip_addresses(char* orig, char* ip, size_t ip_size, char* ip2, size_t ip2_size);
00054 
00055 /** Swaps the arrays if param IP does not contain IPv6 address but param ip2 does.
00056  *
00057  *  @param ip        IP address
00058  *  @param ip_size   size of buffer ip
00059  *  @param ip2       IP address
00060  *  @param ip2_size  size of buffer ip2
00061  */
00062 void prefer_ipv6(char* ip, size_t ip_size, char* ip2, size_t ip2_size);
00063 
00064 /** Converts the given int to two hex characters
00065  *
00066  *  @param num number to be converted to hex string
00067  *  @param buf preallocated buffer that will contain 2 char length hex value
00068  */
00069 void int_to_hex_str(uint8_t num, char* buf);
00070 
00071 /** Converts the given buffer 'str' to hex buffer 'buf. First 'len' char's are converted to two hex bytes.
00072  *
00073  *  @param str                   char buffer that is to be converted to hex
00074  *  @param len                   how many chars from str are to be converted
00075  *  @param buf                   destination buffer for hex converted chars. Buffer should be double the size of str to fit hex-encoded string.
00076  *  @param omit_leading_zero     if true then possible leading zeroes are omitted
00077  */
00078 int char_str_to_hex_str(const char* str, uint16_t len, char *buf, bool omit_leading_zero = false);
00079 
00080 /** Converts the given hex string to integer
00081  *
00082  *  @param hex_string        hex string from where chars are converted to int
00083  *  @param hex_string_length length of the param hex_string
00084  *  @return                  hex_string converted to int
00085  */
00086 int hex_str_to_int(const char *hex_string, int hex_string_length);
00087 
00088 /** Converts the given hex string str to char string to buf
00089  *
00090  *  @param str hex string that is converted to char string to buf
00091  *  @param len length of the param str/how many hex are converted
00092  *  @param buf preallocated buffer where result conversion is stored
00093  *  @return    length of the buf
00094  */
00095 int hex_str_to_char_str(const char* str, uint16_t len, char *buf);
00096 
00097 /** Converts the given uint to binary string. Fills the given str starting from [0] with the number of bits defined by bit_cnt
00098  *  For example uint_to_binary_string(9, str, 10) would fill str "0000001001"
00099  *  For example uint_to_binary_string(9, str, 3) would fill str "001"
00100  *
00101  *  @param num       uint to converts to binary string
00102  *  @param str       buffer for converted binary string
00103  *  @param str_size  size of the str buffer
00104  *  @param bit_cnt   defines how many bits are filled to buffer started from lsb
00105  */
00106 void uint_to_binary_str(uint32_t num, char* str, int str_size, int bit_cnt);
00107 
00108 /** Get dynamic port for socket
00109  *
00110  *  @return next port number above 49152
00111  */
00112 uint16_t get_dynamic_ip_port();
00113 
00114 } // namespace mbed_cellular_util
00115 
00116 #endif