Import Mbed OS hard-float snapshot
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2018, Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "nsconfig.h"
|
||||
#include "ns_types.h"
|
||||
#include "isqrt.h"
|
||||
|
||||
/**
|
||||
* \brief Calculates integer square root
|
||||
*
|
||||
* Algorithm is from http://www.codecodex.com/wiki/Calculate_an_integer_square_root
|
||||
*
|
||||
* \param n number
|
||||
*
|
||||
* \return square root of the number (rounded down)
|
||||
*/
|
||||
|
||||
uint32_t isqrt32(uint32_t n)
|
||||
{
|
||||
uint32_t root, remainder, place;
|
||||
|
||||
root = 0;
|
||||
remainder = n;
|
||||
place = 0x40000000;
|
||||
|
||||
while (place > remainder) {
|
||||
place = place >> 2;
|
||||
}
|
||||
while (place) {
|
||||
if (remainder >= root + place) {
|
||||
remainder = remainder - root - place;
|
||||
root = root + (place << 1);
|
||||
}
|
||||
root = root >> 1;
|
||||
place = place >> 2;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (c) 2014-2015, 2017-2018, Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef ISQRT_H_
|
||||
#define ISQRT_H_
|
||||
|
||||
/**
|
||||
* \brief Calculates integer square root
|
||||
*
|
||||
* \param n number
|
||||
*
|
||||
* \return square root of the number
|
||||
*/
|
||||
|
||||
uint32_t isqrt32(uint32_t n);
|
||||
|
||||
#endif /* ISQRT_H_ */
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2017-2018, Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "nsconfig.h"
|
||||
#include "ns_types.h"
|
||||
|
||||
#include "Core/include/ns_monitor.h"
|
||||
#include "mac_api.h" // for mcps_packet_ingress_rate_limit_by_memory
|
||||
#include "MAC/IEEE802_15_4/mac_mcps_sap.h" // for mcps_packet_ingress_rate_limit_by_memory
|
||||
|
||||
int ns_conf_gc_threshold_set(uint8_t percentage_high, uint8_t percentage_critical)
|
||||
{
|
||||
return ns_monitor_heap_gc_threshold_set(percentage_high, percentage_critical);
|
||||
}
|
||||
|
||||
int ns_conf_packet_ingress_rate_limit_by_mem(uint8_t free_heap_percentage)
|
||||
{
|
||||
return ns_monitor_packet_ingress_rate_limit_by_memory(free_heap_percentage);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 2017-2018, Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "ns_crc.h"
|
||||
|
||||
//#define CRC_TEST
|
||||
|
||||
#define POLYNOMIAL_CRC15_CCIT 0x1021
|
||||
#define POLYNOMIAL_CRC15_ANSI 0x8005
|
||||
|
||||
uint16_t crc16_ansi(uint8_t *message, int nBytes)
|
||||
{
|
||||
return crc16_calc(message, nBytes, POLYNOMIAL_CRC15_ANSI);
|
||||
}
|
||||
uint16_t crc16_ccitt(uint8_t *message, int nBytes)
|
||||
{
|
||||
return crc16_calc(message, nBytes, POLYNOMIAL_CRC15_CCIT);
|
||||
}
|
||||
|
||||
uint16_t crc16_calc(uint8_t *data, uint16_t data_length, uint16_t polynomial)
|
||||
{
|
||||
int crc = 0;
|
||||
int i, bit;
|
||||
for (i = 0; i < data_length; i++) {
|
||||
crc ^= (int)data[i] << 8;
|
||||
for (bit = 0; bit < 8; bit++) {
|
||||
if (crc & 0x8000) {
|
||||
crc = (crc << 1) ^ polynomial;
|
||||
} else {
|
||||
crc <<= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc & 0xffff;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef CRC_TEST //unity test
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
//#include "unity.h"
|
||||
|
||||
#ifndef TEST_ASSERT_EQUAL_INT
|
||||
#define TEST_ASSERT_EQUAL_INT(a,b, s) do{ if(a!=b){printf("%s: 0x%04x != 0x%04x - FAIL\n", s, a, b);} else printf("%s: 0x%04x == 0x%04x - PASS\n", s,a,b);}while(0)
|
||||
#endif
|
||||
|
||||
char data[] = "arm";
|
||||
/* Unity test code starts */
|
||||
void setUp(void)
|
||||
{
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
}
|
||||
|
||||
void test_param_crc16_ansi(void)
|
||||
{
|
||||
uint16_t crc = crc16_ibm(data, strlen(data));
|
||||
TEST_ASSERT_EQUAL_INT(crc, 0x2406, "CRC16-ansi");
|
||||
}
|
||||
void test_param_crc16_ccitt(void)
|
||||
{
|
||||
uint16_t crc = crc16_ccitt(data, strlen(data));
|
||||
TEST_ASSERT_EQUAL_INT(crc, 0x7F6B, "CRC16-CCIT");
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
test_param_crc16_ansi();
|
||||
test_param_crc16_ccitt();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2015-2018, Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef _NS_CRC_H_
|
||||
#define _NS_CRC_H_
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
/// @todo documentation
|
||||
|
||||
/** same as crc16_ibm */
|
||||
uint16_t crc16_ansi(uint8_t *message, int nBytes);
|
||||
uint16_t crc16_ccitt(uint8_t *message, int nBytes);
|
||||
|
||||
/**
|
||||
* @param data data which crc will be calculate
|
||||
* @param data_length Length of data pointer
|
||||
* @param polynomial Polynomial which will be used to calculate CRC, POLYNOMIAL_CRC15_CCIT, POLYNOMIAL_CRC15_ANSI
|
||||
* @return Calculated 16bit CRC value
|
||||
*/
|
||||
uint16_t crc16_calc(uint8_t *data, uint16_t data_length, uint16_t polynomial);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* _NS_CRC_H_ */
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _NS_FILE_H_
|
||||
#define _NS_FILE_H_
|
||||
|
||||
/**
|
||||
* \file ns_file.h
|
||||
* \brief Nanostack file handling API.
|
||||
*/
|
||||
|
||||
#include "ns_file_system.h"
|
||||
|
||||
/**
|
||||
* File open
|
||||
*
|
||||
* Depending on underlying file system file open for read for non-existing
|
||||
* files can return success. In that case file read will fail.
|
||||
*
|
||||
* \param filename filename
|
||||
* \param mode can be either "r" or "w"
|
||||
*
|
||||
* \return file handle
|
||||
* \return NULL on error
|
||||
*
|
||||
*/
|
||||
NS_FILE ns_fopen(const char *filename, const char *mode);
|
||||
|
||||
/**
|
||||
* File close
|
||||
*
|
||||
* \param handle file handle
|
||||
*
|
||||
* \return 0 on success
|
||||
* \return < 0 in case of errors
|
||||
*
|
||||
*/
|
||||
int ns_fclose(NS_FILE *ns_handle);
|
||||
|
||||
/**
|
||||
* File remove
|
||||
*
|
||||
* \param filename filename
|
||||
*
|
||||
* \return 0 on success
|
||||
* \return < 0 in case of errors
|
||||
*
|
||||
*/
|
||||
int ns_fremove(const char *filename);
|
||||
|
||||
/**
|
||||
* File write
|
||||
*
|
||||
* Write is not stream write. The whole file is written from start to end
|
||||
* and if function is called again, previous file content is replaced with
|
||||
* new content.
|
||||
*
|
||||
* \param handle file handle
|
||||
* \param buffer buffer
|
||||
* \param buffer buffer size
|
||||
*
|
||||
* \return bytes written
|
||||
*
|
||||
*/
|
||||
size_t ns_fwrite(NS_FILE *ns_handle, const void *buffer, size_t size);
|
||||
|
||||
/**
|
||||
* File read
|
||||
*
|
||||
* Read is not stream read. The whole file is read from start to end
|
||||
* and if function is called again, read is started from start again.
|
||||
*
|
||||
* \param handle file handle
|
||||
* \param buffer buffer
|
||||
* \param size buffer size
|
||||
*
|
||||
* \return bytes written
|
||||
*
|
||||
*/
|
||||
size_t ns_fread(NS_FILE *ns_handle, void *buffer, size_t size);
|
||||
|
||||
/**
|
||||
* File size callback
|
||||
*
|
||||
* Reads file size.
|
||||
*
|
||||
* \param handle file handle
|
||||
* \param size file size
|
||||
*
|
||||
* \return 0 on success
|
||||
* \return < 0 in case of reading file size is not supported
|
||||
*
|
||||
*/
|
||||
int ns_fsize(NS_FILE *ns_handle, size_t *size);
|
||||
|
||||
#endif /* _NS_FILE_SYSTEM_H_ */
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* Copyright (c) 2017-2018, Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "ns_types.h"
|
||||
#include "nsdynmemLIB.h"
|
||||
#include "ns_file_system.h"
|
||||
#include "ns_file.h"
|
||||
|
||||
static char *file_system_root;
|
||||
|
||||
static ns_file_open file_open_cb = NULL;
|
||||
static ns_file_close file_close_cb = NULL;
|
||||
static ns_file_remove file_remove_cb = NULL;
|
||||
static ns_file_write file_write_cb = NULL;
|
||||
static ns_file_read file_read_cb = NULL;
|
||||
static ns_file_size file_size_cb = NULL;
|
||||
|
||||
int ns_file_system_set_root_path(const char *root_path)
|
||||
{
|
||||
char *new_root_path;
|
||||
|
||||
if (root_path == NULL) {
|
||||
// File system usage disabled
|
||||
ns_dyn_mem_free(file_system_root);
|
||||
file_system_root = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
new_root_path = ns_dyn_mem_alloc(strlen(root_path) + 1);
|
||||
if (!new_root_path) {
|
||||
// mem alloc failed
|
||||
return -2;
|
||||
}
|
||||
|
||||
ns_dyn_mem_free(file_system_root);
|
||||
file_system_root = new_root_path;
|
||||
strcpy(file_system_root, root_path);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *ns_file_system_get_root_path(void)
|
||||
{
|
||||
return file_system_root;
|
||||
}
|
||||
|
||||
void ns_file_system_callbacks_set(ns_file_open open, ns_file_close close, ns_file_remove remove, ns_file_write write, ns_file_read read, ns_file_size size)
|
||||
{
|
||||
file_open_cb = open;
|
||||
file_close_cb = close;
|
||||
file_remove_cb = remove;
|
||||
file_write_cb = write;
|
||||
file_read_cb = read;
|
||||
file_size_cb = size;
|
||||
}
|
||||
|
||||
NS_FILE ns_fopen(const char *file_name, const char *mode)
|
||||
{
|
||||
if (!file_name || !mode || (*mode != 'r' && *mode != 'w')) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (file_open_cb) {
|
||||
return file_open_cb(file_name, mode);
|
||||
}
|
||||
|
||||
FILE *file = fopen(file_name, mode);
|
||||
if (file == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (NS_FILE) file;
|
||||
}
|
||||
|
||||
int ns_fclose(NS_FILE *ns_handle)
|
||||
{
|
||||
if (!ns_handle) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (file_close_cb) {
|
||||
return file_close_cb(ns_handle);
|
||||
}
|
||||
fclose((FILE *) ns_handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ns_fremove(const char *file_name)
|
||||
{
|
||||
if (file_remove_cb) {
|
||||
return file_remove_cb(file_name);
|
||||
}
|
||||
|
||||
if (!file_name) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return remove(file_name);
|
||||
}
|
||||
|
||||
size_t ns_fwrite(NS_FILE *ns_handle, const void *buffer, size_t size)
|
||||
{
|
||||
if (!ns_handle || !buffer || size == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (file_write_cb) {
|
||||
return file_write_cb(ns_handle, buffer, size);
|
||||
}
|
||||
|
||||
rewind((FILE *) ns_handle);
|
||||
return fwrite(buffer, 1, size, (FILE *) ns_handle);
|
||||
}
|
||||
|
||||
size_t ns_fread(NS_FILE *ns_handle, void *buffer, size_t size)
|
||||
{
|
||||
if (!ns_handle || !buffer || size == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (file_read_cb) {
|
||||
return file_read_cb(ns_handle, buffer, size);
|
||||
}
|
||||
|
||||
rewind((FILE *) ns_handle);
|
||||
return fread(buffer, 1, size, (FILE *) ns_handle);
|
||||
}
|
||||
|
||||
int ns_fsize(NS_FILE *ns_handle, size_t *size)
|
||||
{
|
||||
if (!ns_handle || !size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (file_size_cb) {
|
||||
return file_size_cb(ns_handle, size);
|
||||
}
|
||||
|
||||
fseek((FILE *) ns_handle, 0L, SEEK_END);
|
||||
return ftell((FILE *) ns_handle);
|
||||
}
|
||||
Reference in New Issue
Block a user