Skip to content

Commit a184ff5

Browse files
author
Arto Kinnunen
committed
Squashed 'features/frameworks/nanostack-libservice/' changes from ddd45db..2705b9b
2705b9b Add IPv4 string conversion utils 8978dac Add API to set temporary allocation heap limit (#70) 86fa01c Update max heap size to use size_t (#68) 9808289 nsdynmemlib: silence IAR warnings caused by gotos skipping variable init git-subtree-dir: features/frameworks/nanostack-libservice git-subtree-split: 2705b9b93877f18828106df80af5b8c1c441e713
1 parent d6732a1 commit a184ff5

File tree

10 files changed

+425
-22
lines changed

10 files changed

+425
-22
lines changed

mbed-client-libservice/ip4string.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2014-2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef IP4STRING_H
17+
#define IP4STRING_H
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
#include "ns_types.h"
23+
24+
/**
25+
* Print binary IPv4 address to a string.
26+
*
27+
* String must contain enough room for full address, 16 bytes exact.
28+
*
29+
* \param ip4addr IPv4 address.
30+
* \param p buffer to write string to.
31+
* \return length of generated string excluding the terminating null character
32+
*/
33+
uint_fast8_t ip4tos(const void *ip4addr, char *p);
34+
35+
/**
36+
* Convert numeric IPv4 address string to a binary.
37+
*
38+
* \param ip4addr IPv4 address in string format.
39+
* \param len Length of IPv4 string, maximum of 16..
40+
* \param dest buffer for address. MUST be 4 bytes.
41+
* \return boolean set to true if conversion succeed, false if it didn't
42+
*/
43+
bool stoip4(const char *ip4addr, size_t len, void *dest);
44+
45+
#ifdef __cplusplus
46+
}
47+
#endif
48+
#endif

mbed-client-libservice/nsdynmemLIB.h

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014-2016 ARM Limited. All rights reserved.
2+
* Copyright (c) 2014-2018 ARM Limited. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
* Licensed under the Apache License, Version 2.0 (the License); you may
55
* not use this file except in compliance with the License.
@@ -32,10 +32,10 @@ extern "C" {
3232
#include "ns_types.h"
3333

3434
// Added to maintain backward compatibility with older implementation of ns_dyn_mem APIs
35-
#define NSDYNMEMLIB_API_VERSION 2
35+
#define NSDYNMEMLIB_API_VERSION 3
3636

37-
typedef uint16_t ns_mem_block_size_t; //external interface unsigned heap block size type
38-
typedef uint16_t ns_mem_heap_size_t; //total heap size type.
37+
typedef size_t ns_mem_block_size_t; //external interface unsigned heap block size type
38+
typedef size_t ns_mem_heap_size_t; //total heap size type.
3939

4040
/*!
4141
* \enum heap_fail_t
@@ -122,6 +122,20 @@ extern void *ns_dyn_mem_alloc(ns_mem_block_size_t alloc_size);
122122
*/
123123
extern const mem_stat_t *ns_dyn_mem_get_mem_stat(void);
124124

125+
/**
126+
* \brief Set amount of free heap that must be available for temporary memory allocation to succeed.
127+
*
128+
* Temporary memory allocation will fail if system does not have defined amount of heap free.
129+
*
130+
* Note: the caller must set mem_stat_t structure in initialization.
131+
*
132+
* \param free_heap_percentage percentage of total heap that must be free for temporary memory allocation. Set free_heap_amount to 0 when this percentage value.
133+
* \param free_heap_amount Amount of free heap that must be free for temporary memory allocation. This value takes preference over percentage parameter value.
134+
*
135+
* \return 0 on success, <0 otherwise
136+
*/
137+
extern int ns_dyn_mem_set_temporary_alloc_free_heap_threshold(uint8_t free_heap_percentage, ns_mem_heap_size_t free_heap_amount);
138+
125139
/**
126140
* \brief Init and set Dynamical heap pointer and length.
127141
*
@@ -181,8 +195,24 @@ extern void *ns_mem_alloc(ns_mem_book_t *book, ns_mem_block_size_t alloc_size);
181195
*/
182196
extern const mem_stat_t *ns_mem_get_mem_stat(ns_mem_book_t *book);
183197

198+
/**
199+
* \brief Set amount of free heap that must be available for temporary memory allocation to succeed.
200+
*
201+
* Temporary memory allocation will fail if system does not have defined amount of heap free.
202+
*
203+
* Note: the caller must set mem_stat_t structure in initialization.
204+
*
205+
* \param book Address of book keeping structure
206+
* \param free_heap_percentage percentage of total heap that must be free for temporary memory allocation. Set free_heap_amount to 0 when using percentage value.
207+
* \param free_heap_amount Amount of free heap that must be free for temporary memory allocation. This value takes preference over the percentage parameter value.
208+
*
209+
* \return 0 on success, <0 otherwise
210+
*/
211+
extern int ns_mem_set_temporary_alloc_free_heap_threshold(ns_mem_book_t *book, uint8_t free_heap_percentage, ns_mem_heap_size_t free_heap_amount);
212+
184213
#ifdef __cplusplus
185214
}
186215
#endif
187216
#endif /* NSDYNMEMLIB_H_ */
188217

218+

source/libip4string/ip4tos.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2014-2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <stdio.h>
17+
#include <string.h>
18+
#include "common_functions.h"
19+
#include "ip4string.h"
20+
21+
static void ipv4_itoa(char *string, uint8_t byte);
22+
23+
/**
24+
* Print binary IPv4 address to a string.
25+
* String must contain enough room for full address, 16 bytes exact.
26+
* \param addr IPv4 address.
27+
* \p buffer to write string to.
28+
*/
29+
uint_fast8_t ip4tos(const void *ip4addr, char *p)
30+
{
31+
uint_fast8_t outputPos = 0;
32+
const uint8_t *byteArray = ip4addr;
33+
34+
for (uint_fast8_t component = 0; component < 4; ++component) {
35+
//Convert the byte to string
36+
ipv4_itoa(&p[outputPos], byteArray[component]);
37+
38+
//Move outputPos to the end of the string
39+
while (p[outputPos] != '\0') {
40+
outputPos += 1;
41+
}
42+
43+
//Append a dot if this is not the last digit
44+
if (component < 3) {
45+
p[outputPos++] = '.';
46+
}
47+
}
48+
49+
// Return length of generated string, excluding the terminating null character
50+
return outputPos;
51+
}
52+
53+
static void ipv4_itoa(char *string, uint8_t byte)
54+
{
55+
char *baseString = string;
56+
57+
//Write the digits to the buffer from the least significant to the most
58+
// This is the incorrect order but we will swap later
59+
do {
60+
*string++ = '0' + byte % 10;
61+
byte /= 10;
62+
} while(byte);
63+
64+
//We put the final \0, then go back one step on the last digit for the swap
65+
*string-- = '\0';
66+
67+
//We now swap the digits
68+
while (baseString < string) {
69+
uint8_t tmp = *string;
70+
*string-- = *baseString;
71+
*baseString++ = tmp;
72+
}
73+
}

source/libip4string/stoip4.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2014-2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <string.h>
17+
#include <stdlib.h>
18+
#include <stdint.h>
19+
#include "common_functions.h"
20+
#include "ip4string.h"
21+
22+
/**
23+
* Convert numeric IPv4 address string to a binary.
24+
* \param ip4addr IPv4 address in string format.
25+
* \param len Length of IPv4 string, maximum of 16..
26+
* \param dest buffer for address. MUST be 4 bytes.
27+
* \return boolean set to true if conversion succeded, false if it didn't
28+
*/
29+
bool stoip4(const char *ip4addr, size_t len, void *dest)
30+
{
31+
uint8_t *addr = dest;
32+
33+
if (len > 16) { // Too long, not possible
34+
return false;
35+
}
36+
37+
uint_fast8_t stringLength = 0, byteCount = 0;
38+
39+
//Iterate over each component of the IP. The exit condition is in the middle of the loop
40+
while (true) {
41+
42+
//No valid character (IPv4 addresses don't have implicit 0, that is x.y..z being read as x.y.0.z)
43+
if (stringLength == len || ip4addr[stringLength] < '0' || ip4addr[stringLength] > '9') {
44+
return false;
45+
}
46+
47+
//For each component, we convert it to the raw value
48+
uint_fast16_t byte = 0;
49+
while (stringLength < len && ip4addr[stringLength] >= '0' && ip4addr[stringLength] <= '9') {
50+
byte *= 10;
51+
byte += ip4addr[stringLength++] - '0';
52+
53+
//We go over the maximum value for an IPv4 component
54+
if (byte > 0xff) {
55+
return false;
56+
}
57+
}
58+
59+
//Append the component
60+
addr[byteCount++] = (uint8_t) byte;
61+
62+
//If we're at the end, we leave the loop. It's the only way to reach the `true` output
63+
if (byteCount == 4) {
64+
break;
65+
}
66+
67+
//If the next character is invalid, we return false
68+
if (stringLength == len || ip4addr[stringLength++] != '.') {
69+
return false;
70+
}
71+
}
72+
73+
return stringLength == len || ip4addr[stringLength] == '\0';
74+
}

0 commit comments

Comments
 (0)