├── .gitattributes ├── .mbedignore ├── README.txt ├── README.md ├── CONTRIBUTING.md ├── test └── mbed-coap │ └── unittest │ ├── sn_coap_protocol │ ├── test_config.h │ ├── Makefile │ └── main.cpp │ ├── sn_coap_header_check │ ├── Makefile │ ├── main.cpp │ └── libCoap_header_test.cpp │ ├── sn_coap_parser │ ├── main.cpp │ ├── Makefile │ ├── test_sn_coap_parser.h │ └── sn_coap_parsertest.cpp │ ├── sn_coap_builder │ ├── Makefile │ ├── main.cpp │ └── libCoap_builder_test.cpp │ ├── Makefile │ ├── makefile_defines.txt │ ├── stubs │ ├── ns_list_stub.c │ ├── sn_coap_parser_stub.h │ ├── sn_coap_header_check_stub.h │ ├── sn_coap_builder_stub.h │ ├── randLIB_stub.h │ ├── sn_coap_header_check_stub.c │ ├── sn_coap_protocol_stub.h │ ├── randLIB_stub.cpp │ ├── sn_coap_builder_stub.c │ ├── sn_coap_protocol_stub.c │ └── sn_coap_parser_stub.c │ ├── run_tests │ └── MakefileWorker.mk ├── .gitignore ├── .github └── pull_request_template.md ├── Makefile ├── xsl_script.sh ├── source ├── include │ ├── sn_coap_header_internal.h │ └── sn_coap_protocol_internal.h └── sn_coap_header_check.c ├── .project ├── Makefile.test ├── junit_xsl.xslt ├── .settings ├── org.eclipse.cdt.core.prefs └── language.settings.xml ├── apache-2.0.txt ├── LICENSE ├── mbed-coap ├── sn_config.h ├── sn_coap_protocol.h └── sn_coap_header.h └── CHANGELOG.md /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.uvproj eol=lf 3 | -------------------------------------------------------------------------------- /.mbedignore: -------------------------------------------------------------------------------- 1 | test/* 2 | test_modules/* 3 | unittest/* 4 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | CoAP C library - Builder and Parser for CoAP messages. 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mbed-coap 2 | CoAP C library - Builder and Parser for CoAP messages 3 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributions to Mbed CoAP 2 | 3 | Mbed CoAP is Apache 2.0 licensed open source software. We accept contributions from 3rd parties, if they agree to submit their changes with Apache 2.0 licensing. Arm will review all contributions and possibly make some modifications before merging them. 4 | 5 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_protocol/test_config.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_CONFIG_H 2 | #define TEST_CONFIG_H 3 | 4 | #define SN_COAP_DUPLICATION_MAX_MSGS_COUNT 1 5 | #define SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE 16 6 | #define ENABLE_RESENDINGS 1 7 | #define SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE 65535 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_header_check/Makefile: -------------------------------------------------------------------------------- 1 | include ../makefile_defines.txt 2 | 3 | COMPONENT_NAME = sn_coap_header_check_unit 4 | SRC_FILES = \ 5 | ../../../../source/sn_coap_header_check.c 6 | 7 | TEST_SRC_FILES = \ 8 | main.cpp \ 9 | libCoap_header_test.cpp \ 10 | 11 | include ../MakefileWorker.mk 12 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_parser/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 ARM. All rights reserved. 3 | */ 4 | 5 | #include "CppUTest/CommandLineTestRunner.h" 6 | #include "CppUTest/TestPlugin.h" 7 | #include "CppUTest/TestRegistry.h" 8 | #include "CppUTestExt/MockSupportPlugin.h" 9 | int main(int ac, char** av) 10 | { 11 | return CommandLineTestRunner::RunAllTests(ac, av); 12 | } 13 | 14 | IMPORT_TEST_GROUP(sn_coap_parser); 15 | 16 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_parser/Makefile: -------------------------------------------------------------------------------- 1 | include ../makefile_defines.txt 2 | 3 | COMPONENT_NAME = sn_coap_parser_unit 4 | 5 | #This must be changed manually 6 | SRC_FILES = \ 7 | ../../../../source/sn_coap_parser.c 8 | 9 | TEST_SRC_FILES = \ 10 | main.cpp \ 11 | sn_coap_parsertest.cpp \ 12 | test_sn_coap_parser.c \ 13 | ../stubs/sn_coap_protocol_stub.c \ 14 | 15 | include ../MakefileWorker.mk 16 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_builder/Makefile: -------------------------------------------------------------------------------- 1 | include ../makefile_defines.txt 2 | 3 | COMPONENT_NAME = sn_coap_builder_unit 4 | SRC_FILES = \ 5 | ../../../../source/sn_coap_builder.c 6 | 7 | TEST_SRC_FILES = \ 8 | main.cpp \ 9 | libCoap_builder_test.cpp \ 10 | ../stubs/sn_coap_header_check_stub.c \ 11 | ../stubs/sn_coap_parser_stub.c \ 12 | ../stubs/sn_coap_protocol_stub.c \ 13 | 14 | include ../MakefileWorker.mk 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.a 3 | *.d 4 | *.eww 5 | *.bak 6 | *.dep 7 | *.Scr 8 | *.uvgui.* 9 | build/ 10 | *.dep 11 | *.ewd 12 | *.ewt 13 | Debug 14 | Release 15 | settings 16 | *_release 17 | Obj 18 | *_output 19 | Backup* 20 | *.r43 21 | *.htm 22 | *.uvgui.* 23 | *.lib 24 | *.crf 25 | *.uvopt 26 | *.out 27 | docs/ 28 | yotta_modules 29 | yotta_targets 30 | test/mbed-coap/unittest/coverages 31 | test/mbed-coap/unittest/results 32 | .yotta.json 33 | coverage/ 34 | lcov/ 35 | upload.tar.gz 36 | 37 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/Makefile: -------------------------------------------------------------------------------- 1 | #scan for folders having "Makefile" in them and remove 'this' to prevent loop 2 | DIRS := $(filter-out ./, $(sort $(dir $(shell find . -name 'Makefile')))) 3 | 4 | all: 5 | for dir in $(DIRS); do \ 6 | cd $$dir; make gcov; cd ..;\ 7 | done 8 | 9 | clean: 10 | for dir in $(DIRS); do \ 11 | cd $$dir; make clean; cd ..;\ 12 | done 13 | rm -rf ../source/*gcov ../source/*gcda ../source/*o 14 | rm -rf stubs/*gcov stubs/*gcda stubs/*o 15 | rm -rf results/* 16 | rm -rf coverages/* 17 | rm -rf results 18 | rm -rf coverages 19 | 20 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | [] I confirm this contribution is my own and I agree to license it with Apache 2.0. 8 | [] I confirm the moderators may change the PR before merging it in. 9 | 10 | ### Summary of changes 11 | 12 | 24 | 25 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/makefile_defines.txt: -------------------------------------------------------------------------------- 1 | #--- Inputs ----# 2 | CPPUTEST_HOME = ../../../../test_modules/cpputest 3 | CPPUTEST_USE_EXTENSIONS = Y 4 | CPPUTEST_USE_VPATH = Y 5 | CPPUTEST_USE_GCOV = Y 6 | CPPUTEST_USE_MEM_LEAK_DETECTION = N 7 | CPP_PLATFORM = gcc 8 | INCLUDE_DIRS =\ 9 | .\ 10 | ../common\ 11 | ../stubs\ 12 | ../../../..\ 13 | ../../../../source\ 14 | ../../../../mbed-coap\ 15 | ../../../../test_modules/nanostack-libservice/mbed-client-libservice\ 16 | ../../../../test_modules/mbed-trace\ 17 | ../../../../test_modules/mbed-client-randlib/mbed-client-randlib\ 18 | ../../../../../libService/libService\ 19 | ../../../../source/include\ 20 | /usr/include\ 21 | $(CPPUTEST_HOME)/include\ 22 | 23 | CPPUTESTFLAGS = -D__thumb2__ -w 24 | CPPUTEST_CFLAGS += -std=gnu99 25 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/ns_list_stub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 | 17 | #define NS_LIST_FN extern 18 | #include "ns_list.h" 19 | 20 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_protocol/Makefile: -------------------------------------------------------------------------------- 1 | include ../makefile_defines.txt 2 | 3 | MBED_CLIENT_USER_CONFIG_FILE ?= $(CURDIR)/test_config.h 4 | COMPONENT_NAME = sn_coap_protocol_unit 5 | SRC_FILES = \ 6 | ../../../../source/sn_coap_protocol.c 7 | 8 | TEST_SRC_FILES = \ 9 | main.cpp \ 10 | libCoap_protocol_test.cpp \ 11 | ../stubs/sn_coap_builder_stub.c \ 12 | ../stubs/sn_coap_parser_stub.c \ 13 | ../stubs/sn_coap_header_check_stub.c \ 14 | ../stubs/ns_list_stub.c \ 15 | ../stubs/randLIB_stub.cpp \ 16 | 17 | include ../MakefileWorker.mk 18 | 19 | # the config is needed for client application compilation too 20 | override CFLAGS += -DMBED_CLIENT_USER_CONFIG_FILE='<$(MBED_CLIENT_USER_CONFIG_FILE)>' 21 | override CXXFLAGS += -DMBED_CLIENT_USER_CONFIG_FILE='<$(MBED_CLIENT_USER_CONFIG_FILE)>' 22 | 23 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_parser/test_sn_coap_parser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 ARM. All rights reserved. 3 | */ 4 | #ifndef TEST_SN_COAP_PARSER_H 5 | #define TEST_SN_COAP_PARSER_H 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | #include 12 | 13 | bool test_sn_coap_parser(); 14 | 15 | //These splits above test to keep things more simpler 16 | bool test_sn_coap_parser_options_parsing(); 17 | bool test_sn_coap_parser_options_parsing_switches(); 18 | 19 | bool test_sn_coap_parser_options_count_needed_memory_multiple_option(); 20 | 21 | bool test_sn_coap_parser_options_parse_multiple_options(); 22 | 23 | bool test_sn_coap_parser_parsing(); 24 | //<- split 25 | 26 | bool test_sn_coap_parser_release_allocated_coap_msg_mem(); 27 | 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | #endif // TEST_SN_COAP_PARSER_H 34 | 35 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/sn_coap_parser_stub.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 __SN_COAP_PARSER_STUB_H__ 17 | #define __SN_COAP_PARSER_STUB_H__ 18 | 19 | typedef struct { 20 | sn_coap_hdr_s *expectedHeader; 21 | } sn_coap_parser_def; 22 | 23 | extern sn_coap_parser_def sn_coap_parser_stub; 24 | 25 | #endif //__SN_COAP_PARSER_STUB_H__ 26 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile for COAP library 3 | # 4 | 5 | # Define compiler toolchain with CC or PLATFORM variables 6 | # Example (GCC toolchains, default $CC and $AR are used) 7 | # make 8 | # 9 | # OR (Cross-compile GCC toolchain) 10 | # make PLATFORM=arm-linux-gnueabi- 11 | # 12 | # OR (armcc/Keil) 13 | # make CC=armcc AR=ArmAR 14 | # 15 | # OR (IAR-ARM) 16 | # make CC=iccarm 17 | 18 | LIB = libmbedcoap.a 19 | SRCS := \ 20 | source/sn_coap_protocol.c \ 21 | source/sn_coap_parser.c \ 22 | source/sn_coap_header_check.c \ 23 | source/sn_coap_builder.c \ 24 | 25 | override CFLAGS += -DVERSION='"$(VERSION)"' 26 | 27 | override CFLAGS += -Isource/include/ 28 | SERVLIB_DIR := ../libService 29 | override CFLAGS += -I$(SERVLIB_DIR)/libService 30 | override CFLAGS += -I. 31 | 32 | include ../libService/toolchain_rules.mk 33 | 34 | $(eval $(call generate_rules,$(LIB),$(SRCS))) 35 | 36 | .PHONY: release 37 | release: 38 | 7z a nsdl-c_$(VERSION).zip *.a *.lib include 39 | 40 | .PHONY: deploy_to 41 | deploy_to: all 42 | tar --transform 's,^,libcoap/,' --append -f $(TO) *.a libcoap 43 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/run_tests: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo 3 | echo Build libCoap unit tests 4 | echo 5 | 6 | # Remember to add new test folder to Makefile 7 | make clean 8 | make all 9 | 10 | echo 11 | echo Create results 12 | echo 13 | mkdir results 14 | 15 | find ./ -name '*.xml' | xargs cp -t ./results/ 16 | 17 | echo 18 | echo Create coverage document 19 | echo 20 | mkdir coverages 21 | cd coverages 22 | 23 | #copy the .gcda & .gcno for all test projects (no need to modify 24 | #cp ../../../source/*.gc* . 25 | #find ../ -name '*.gcda' | xargs cp -t . 26 | #find ../ -name '*.gcno' | xargs cp -t . 27 | #find . -name "test*" -type f -delete 28 | #find . -name "*test*" -type f -delete 29 | #find . -name "*stub*" -type f -delete 30 | #rm -rf main.* 31 | 32 | lcov -q -d ../. -c -o app.info 33 | lcov -q -r app.info "/test*" -o app.info 34 | lcov -q -r app.info "/mbed-client-libservice*" -o app.info 35 | lcov -q -r app.info "/usr*" -o app.info 36 | genhtml --no-branch-coverage app.info 37 | cd .. 38 | echo 39 | echo 40 | echo 41 | echo Have a nice bug hunt! 42 | echo 43 | echo 44 | echo 45 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_builder/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 | 17 | #include "CppUTest/CommandLineTestRunner.h" 18 | #include "CppUTest/TestPlugin.h" 19 | #include "CppUTest/TestRegistry.h" 20 | #include "CppUTestExt/MockSupportPlugin.h" 21 | 22 | 23 | 24 | int main(int ac, char **av) 25 | { 26 | return CommandLineTestRunner::RunAllTests(ac, av); 27 | } 28 | 29 | IMPORT_TEST_GROUP(libCoap_builder); 30 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_protocol/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 | 17 | #include "CppUTest/CommandLineTestRunner.h" 18 | #include "CppUTest/TestPlugin.h" 19 | #include "CppUTest/TestRegistry.h" 20 | #include "CppUTestExt/MockSupportPlugin.h" 21 | 22 | 23 | 24 | int main(int ac, char **av) 25 | { 26 | return CommandLineTestRunner::RunAllTests(ac, av); 27 | } 28 | 29 | IMPORT_TEST_GROUP(libCoap_protocol); 30 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/sn_coap_header_check_stub.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 __SN_COAP_HEADER_CHECK_STUB_H__ 17 | #define __SN_COAP_HEADER_CHECK_STUB_H__ 18 | 19 | #include 20 | 21 | typedef struct { 22 | int8_t expectedInt8; 23 | } sn_coap_header_check_stub_def; 24 | 25 | extern sn_coap_header_check_stub_def sn_coap_header_check_stub; 26 | 27 | #endif //__SN_COAP_HEADER_CHECK_STUB_H__ 28 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_header_check/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 | 17 | #include "CppUTest/CommandLineTestRunner.h" 18 | #include "CppUTest/TestPlugin.h" 19 | #include "CppUTest/TestRegistry.h" 20 | #include "CppUTestExt/MockSupportPlugin.h" 21 | 22 | 23 | 24 | int main(int ac, char **av) 25 | { 26 | return CommandLineTestRunner::RunAllTests(ac, av); 27 | } 28 | 29 | IMPORT_TEST_GROUP(libCoap_header_check); 30 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/sn_coap_builder_stub.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 __SN_COAP_BUILDER_STUB_H__ 17 | #define __SN_COAP_BUILDER_STUB_H__ 18 | 19 | typedef struct { 20 | int16_t expectedInt16; 21 | uint16_t expectedUint16; 22 | sn_coap_hdr_s *expectedHeader; 23 | } sn_coap_builder_stub_def; 24 | 25 | extern sn_coap_builder_stub_def sn_coap_builder_stub; 26 | 27 | #endif //__SN_COAP_BUILDER_STUB_H__ 28 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/randLIB_stub.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 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 M2M_RANDLIB_STUB_H 17 | #define M2M_RANDLIB_STUB_H 18 | 19 | #include 20 | 21 | //some internal test related stuff 22 | namespace randLIB_stub 23 | { 24 | extern uint8_t uint8_value; 25 | extern uint16_t uint16_value; 26 | extern uint32_t uint32_value; 27 | extern uint64_t uint64_value; 28 | extern void* void_value; 29 | } 30 | 31 | #endif // M2M_RANDLIB_STUB_H -------------------------------------------------------------------------------- /xsl_script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright (c) 2015 ARM Limited. All rights reserved. 4 | # SPDX-License-Identifier: Apache-2.0 5 | # Licensed under the Apache License, Version 2.0 (the License); you may 6 | # 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, WITHOUT 13 | # 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 | echo 18 | echo "Creating report" 19 | echo 20 | 21 | echo ' 22 | 23 | ' >> lcov/index.xml 24 | 25 | for f in lcov/results/*.xml 26 | do 27 | name=${f##*/} 28 | echo ''>> lcov/index.xml 29 | done 30 | 31 | echo '' >> lcov/index.xml 32 | 33 | echo 34 | echo "Report created to lcov/index.xml (outputs html)" 35 | echo -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/sn_coap_header_check_stub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 | 17 | /** 18 | * \file sn_coap_header_check.c 19 | * 20 | * \brief CoAP Header validity checker 21 | * 22 | * Functionality: Checks validity of CoAP Header 23 | * 24 | */ 25 | 26 | #include "ns_types.h" 27 | #include "sn_coap_header.h" 28 | 29 | #include "sn_coap_header_check_stub.h" 30 | 31 | sn_coap_header_check_stub_def sn_coap_header_check_stub; 32 | 33 | int8_t sn_coap_header_validity_check(sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version) 34 | { 35 | return sn_coap_header_check_stub.expectedInt8; 36 | } 37 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/sn_coap_protocol_stub.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 __SN_COAP_PROTOCOL_STUB_H__ 17 | #define __SN_COAP_PROTOCOL_STUB_H__ 18 | 19 | #include "sn_coap_header_internal.h" 20 | #include "sn_coap_protocol_internal.h" 21 | #include "sn_coap_header.h" 22 | 23 | typedef struct { 24 | int8_t expectedInt8; 25 | int16_t expectedInt16; 26 | struct coap_s *expectedCoap; 27 | sn_coap_hdr_s *expectedHeader; 28 | coap_send_msg_s *expectedSendMsg; 29 | } sn_coap_protocol_stub_def; 30 | 31 | extern sn_coap_protocol_stub_def sn_coap_protocol_stub; 32 | 33 | #endif //__SN_COAP_PROTOCOL_STUB_H__ 34 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_parser/sn_coap_parsertest.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 ARM. All rights reserved. 3 | */ 4 | #include "CppUTest/TestHarness.h" 5 | #include "test_sn_coap_parser.h" 6 | 7 | TEST_GROUP(sn_coap_parser) 8 | { 9 | void setup() 10 | { 11 | } 12 | 13 | void teardown() 14 | { 15 | } 16 | }; 17 | 18 | TEST(sn_coap_parser, test_sn_coap_parser) 19 | { 20 | CHECK(test_sn_coap_parser()); 21 | } 22 | 23 | TEST(sn_coap_parser, test_sn_coap_parser_options_parsing) 24 | { 25 | CHECK(test_sn_coap_parser_options_parsing()); 26 | } 27 | 28 | TEST(sn_coap_parser, test_sn_coap_parser_options_parsing_switches) 29 | { 30 | CHECK(test_sn_coap_parser_options_parsing_switches()); 31 | } 32 | 33 | TEST(sn_coap_parser, test_sn_coap_parser_options_count_needed_memory_multiple_option) 34 | { 35 | CHECK(test_sn_coap_parser_options_count_needed_memory_multiple_option()); 36 | } 37 | 38 | TEST(sn_coap_parser, test_sn_coap_parser_options_parse_multiple_options) 39 | { 40 | CHECK(test_sn_coap_parser_options_parse_multiple_options()); 41 | } 42 | 43 | TEST(sn_coap_parser, test_sn_coap_parser_parsing) 44 | { 45 | CHECK(test_sn_coap_parser_parsing()); 46 | } 47 | 48 | TEST(sn_coap_parser, test_sn_coap_parser_release_allocated_coap_msg_mem) 49 | { 50 | CHECK(test_sn_coap_parser_release_allocated_coap_msg_mem()); 51 | } 52 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/randLIB_stub.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 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 | 17 | #include "randLIB_stub.h" 18 | 19 | 20 | uint8_t randLIB_stub::uint8_value; 21 | uint16_t randLIB_stub::uint16_value; 22 | uint32_t randLIB_stub::uint32_value; 23 | uint64_t randLIB_stub::uint64_value; 24 | void* randLIB_stub::void_value; 25 | 26 | extern "C"{ 27 | 28 | void randLIB_seed_random(void){} 29 | 30 | void randLIB_add_seed(uint64_t seed){} 31 | 32 | uint8_t randLIB_get_8bit(void) 33 | { 34 | return randLIB_stub::uint8_value; 35 | } 36 | 37 | uint16_t randLIB_get_16bit(void) 38 | { 39 | return randLIB_stub::uint16_value; 40 | } 41 | 42 | uint32_t randLIB_get_32bit(void) 43 | { 44 | return randLIB_stub::uint32_value; 45 | } 46 | 47 | uint64_t randLIB_get_64bit(void) 48 | { 49 | return randLIB_stub::uint64_value; 50 | } 51 | 52 | void *randLIB_get_n_bytes_random(void *data_ptr, uint8_t count) 53 | { 54 | return randLIB_stub::void_value; 55 | } 56 | 57 | uint16_t randLIB_get_random_in_range(uint16_t min, uint16_t max) 58 | { 59 | return randLIB_stub::uint16_value; 60 | } 61 | 62 | uint32_t randLIB_randomise_base(uint32_t base, uint16_t min_factor, uint16_t max_factor) 63 | { 64 | return randLIB_stub::uint32_value; 65 | } 66 | 67 | } 68 | 69 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_header_check/libCoap_header_test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 "CppUTest/TestHarness.h" 17 | #include 18 | #include 19 | #include "sn_coap_protocol.h" 20 | #include "sn_coap_header_internal.h" 21 | 22 | 23 | TEST_GROUP(libCoap_header_check) 24 | { 25 | void setup() { 26 | 27 | } 28 | 29 | void teardown() { 30 | 31 | } 32 | }; 33 | 34 | TEST(libCoap_header_check, header_check) 35 | { 36 | sn_coap_hdr_s coap_header; 37 | 38 | memset(&coap_header, 0, sizeof(sn_coap_hdr_s)); 39 | coap_header.msg_type = COAP_MSG_TYPE_CONFIRMABLE; 40 | coap_header.msg_code = COAP_MSG_CODE_REQUEST_GET; 41 | 42 | /* Happy-happy case */ 43 | CHECK(sn_coap_header_validity_check(&coap_header, (coap_version_e)COAP_VERSION_1) == 0); 44 | 45 | 46 | CHECK(sn_coap_header_validity_check(&coap_header, (coap_version_e) 0) == -1); 47 | 48 | coap_header.msg_type = (sn_coap_msg_type_e)0x40; 49 | 50 | CHECK(sn_coap_header_validity_check(&coap_header, (coap_version_e)COAP_VERSION_1) == -1); 51 | 52 | coap_header.msg_type = (sn_coap_msg_type_e)COAP_MSG_TYPE_CONFIRMABLE; 53 | coap_header.msg_code = (sn_coap_msg_code_e)5; 54 | 55 | CHECK(sn_coap_header_validity_check(&coap_header, (coap_version_e)COAP_VERSION_1) == -1); 56 | } 57 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/sn_coap_builder_stub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2015 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 | 17 | /** 18 | * \file sn_coap_builder.c 19 | * 20 | * \brief CoAP Message builder 21 | * 22 | * Functionality: Builds CoAP message 23 | * 24 | */ 25 | 26 | /* * * * * * * * * * * * * * */ 27 | /* * * * INCLUDE FILES * * * */ 28 | /* * * * * * * * * * * * * * */ 29 | 30 | #include "ns_types.h" 31 | #include "sn_coap_header.h" 32 | #include "sn_coap_builder_stub.h" 33 | 34 | 35 | sn_coap_builder_stub_def sn_coap_builder_stub; 36 | 37 | sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, const sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code) 38 | { 39 | return sn_coap_builder_stub.expectedHeader; 40 | } 41 | 42 | int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_size) 43 | { 44 | return sn_coap_builder_stub.expectedInt16; 45 | } 46 | 47 | int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr) 48 | { 49 | return sn_coap_builder_stub.expectedInt16; 50 | } 51 | 52 | uint16_t (sn_coap_builder_calc_needed_packet_data_size_2)(const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_size) 53 | { 54 | return sn_coap_builder_stub.expectedUint16; 55 | } 56 | 57 | uint16_t (sn_coap_builder_calc_needed_packet_data_size)(const sn_coap_hdr_s *src_coap_msg_ptr) 58 | { 59 | return sn_coap_builder_stub.expectedUint16; 60 | } 61 | 62 | int16_t sn_coap_builder_options_build_add_zero_length_option(uint8_t **dst_packet_data_pptr, uint8_t option_length, uint8_t option_exist, sn_coap_option_numbers_e option_number) 63 | { 64 | return sn_coap_builder_stub.expectedInt16; 65 | } 66 | -------------------------------------------------------------------------------- /source/include/sn_coap_header_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2015 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 | 17 | /** 18 | * \file sn_coap_header_internal.h 19 | * 20 | * \brief Header file for CoAP Header part 21 | * 22 | */ 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | #ifndef SN_COAP_HEADER_INTERNAL_H_ 29 | #define SN_COAP_HEADER_INTERNAL_H_ 30 | 31 | 32 | /* * * * * * * * * * * */ 33 | /* * * * DEFINES * * * */ 34 | /* * * * * * * * * * * */ 35 | 36 | #define COAP_VERSION COAP_VERSION_1 /* Tells which IETF CoAP specification version the CoAP message supports. */ 37 | /* This value is written to CoAP message header part. */ 38 | 39 | /* CoAP Header defines */ 40 | #define COAP_HEADER_LENGTH 4 /* Fixed Header length of CoAP message as bytes */ 41 | #define COAP_HEADER_VERSION_MASK 0xC0 42 | #define COAP_HEADER_MSG_TYPE_MASK 0x30 43 | #define COAP_HEADER_TOKEN_LENGTH_MASK 0x0F 44 | #define COAP_HEADER_MSG_ID_MSB_SHIFT 8 45 | 46 | /* CoAP Options defines */ 47 | #define COAP_OPTIONS_OPTION_NUMBER_SHIFT 4 48 | 49 | /* * * * * * * * * * * * * * */ 50 | /* * * * ENUMERATIONS * * * */ 51 | /* * * * * * * * * * * * * * */ 52 | 53 | /* * * * * * * * * * * * * */ 54 | /* * * * STRUCTURES * * * */ 55 | /* * * * * * * * * * * * * */ 56 | 57 | /** 58 | * \brief This structure is returned by sn_coap_exec() for sending 59 | */ 60 | typedef struct sn_nsdl_transmit_ { 61 | uint8_t *packet_ptr; 62 | uint16_t packet_len; 63 | sn_nsdl_addr_s dst_addr_ptr; 64 | 65 | sn_nsdl_capab_e protocol; 66 | 67 | } sn_nsdl_transmit_s; 68 | 69 | /* * * * * * * * * * * * * * * * * * * * * * */ 70 | /* * * * EXTERNAL FUNCTION PROTOTYPES * * * */ 71 | /* * * * * * * * * * * * * * * * * * * * * * */ 72 | extern int8_t sn_coap_header_validity_check(const sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version); 73 | 74 | #endif /* SN_COAP_HEADER_INTERNAL_H_ */ 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | CoapCLibrary 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | ?name? 14 | 15 | 16 | 17 | org.eclipse.cdt.make.core.append_environment 18 | true 19 | 20 | 21 | org.eclipse.cdt.make.core.autoBuildTarget 22 | 23 | 24 | 25 | org.eclipse.cdt.make.core.buildArguments 26 | 27 | 28 | 29 | org.eclipse.cdt.make.core.buildCommand 30 | make 31 | 32 | 33 | org.eclipse.cdt.make.core.buildLocation 34 | ${workspace_loc:/nsdl-c-simple/etsi-server} 35 | 36 | 37 | org.eclipse.cdt.make.core.cleanBuildTarget 38 | clean 39 | 40 | 41 | org.eclipse.cdt.make.core.contents 42 | org.eclipse.cdt.make.core.activeConfigSettings 43 | 44 | 45 | org.eclipse.cdt.make.core.enableAutoBuild 46 | true 47 | 48 | 49 | org.eclipse.cdt.make.core.enableCleanBuild 50 | true 51 | 52 | 53 | org.eclipse.cdt.make.core.enableFullBuild 54 | true 55 | 56 | 57 | org.eclipse.cdt.make.core.fullBuildTarget 58 | 59 | 60 | 61 | org.eclipse.cdt.make.core.stopOnError 62 | true 63 | 64 | 65 | org.eclipse.cdt.make.core.useDefaultBuildCmd 66 | true 67 | 68 | 69 | 70 | 71 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 72 | full,incremental, 73 | 74 | 75 | 76 | 77 | 78 | org.eclipse.cdt.core.cnature 79 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 80 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 81 | 82 | 83 | -------------------------------------------------------------------------------- /Makefile.test: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile.test for combined COAP library unit tests 3 | # 4 | 5 | 6 | # List of subdirectories to build 7 | TEST_FOLDER := ./test/ 8 | # List of unit test directories for libraries 9 | UNITTESTS := $(sort $(dir $(wildcard $(TEST_FOLDER)*/unittest/*))) 10 | TESTDIRS := $(UNITTESTS:%=build-%) 11 | CLEANTESTDIRS := $(UNITTESTS:%=clean-%) 12 | COVERAGEFILE := ./lcov/coverage.info 13 | 14 | TEST_MODULES = ./test_modules 15 | TEST_MODULE_MBED_TRACE = $(TEST_MODULES)/mbed-trace 16 | TEST_MODULE_NANOSTACK = $(TEST_MODULES)/nanostack-libservice 17 | TEST_MODULE_RANDLIB = $(TEST_MODULES)/mbed-client-randlib 18 | TEST_MODULE_CCPUTEST_VERSION = "v3.8" 19 | TEST_MODULE_CPPUTEST = $(TEST_MODULES)/cpputest 20 | TEST_MODULE_CPPUTEST_LIB = $(TEST_MODULE_CPPUTEST)/lib/libCppUTest.a 21 | 22 | .PHONY: clone 23 | clone: 24 | if [ ! -d $(TEST_MODULES) ]; \ 25 | then mkdir $(TEST_MODULES); \ 26 | fi; 27 | 28 | if [ ! -d $(TEST_MODULE_MBED_TRACE) ]; \ 29 | then git clone --depth 1 git@github.com:ARMmbed/mbed-trace.git $(TEST_MODULE_MBED_TRACE); \ 30 | fi; 31 | 32 | if [ ! -d $(TEST_MODULE_NANOSTACK) ]; \ 33 | then git clone --depth 1 git@github.com:ARMmbed/nanostack-libservice.git $(TEST_MODULE_NANOSTACK); \ 34 | fi; 35 | 36 | if [ ! -d $(TEST_MODULE_RANDLIB) ]; \ 37 | then git clone --depth 1 git@github.com:ARMmbed/mbed-client-randlib.git $(TEST_MODULE_RANDLIB); \ 38 | fi; 39 | 40 | if [ ! -d $(TEST_MODULE_CPPUTEST) ]; \ 41 | then git clone --single-branch -b $(TEST_MODULE_CCPUTEST_VERSION) https://github.com/cpputest/cpputest.git $(TEST_MODULE_CPPUTEST); \ 42 | fi; 43 | 44 | .PHONY: test 45 | test: $(TEST_MODULE_CPPUTEST_LIB) $(TESTDIRS) 46 | @rm -rf ./lcov 47 | @rm -rf ./coverage 48 | @rm -rf ./valgrind_logs 49 | @mkdir -p lcov 50 | @mkdir -p lcov/results 51 | @mkdir coverage 52 | @mkdir valgrind_logs 53 | @find ./test -name '*.xml' | xargs cp -t ./lcov/results/ 54 | @rm -f lcov/index.xml 55 | @./xsl_script.sh 56 | @cp junit_xsl.xslt lcov/. 57 | @xsltproc -o lcov/testresults.html lcov/junit_xsl.xslt lcov/index.xml 58 | @rm -f lcov/junit_xsl.xslt 59 | @rm -f lcov/index.xml 60 | @gcovr -r . --filter='.*/sn_coap_builder.c' --filter='.*/sn_coap_protocol.c' --filter='.*/sn_coap_parser.c' --filter='.*/sn_coap_header_check.c' -x -o ./lcov/gcovr.xml 61 | @lcov -q -d test/. -c -o $(COVERAGEFILE) 62 | @lcov -q -r $(COVERAGEFILE) "/usr*" -o $(COVERAGEFILE) 63 | @lcov -q -r $(COVERAGEFILE) "*unittest*" -o $(COVERAGEFILE) 64 | @lcov -q -r $(COVERAGEFILE) "*test_modules*" -o $(COVERAGEFILE) 65 | @genhtml -q $(COVERAGEFILE) --show-details --output-directory lcov/html 66 | @find ./test -name \valgrind*.xml -print0 | xargs -0 cp --target-directory=./valgrind_logs/ 67 | @echo mbed-coap module unit tests built 68 | 69 | $(TEST_MODULE_CPPUTEST_LIB): 70 | cd $(TEST_MODULE_CPPUTEST) && \ 71 | ./autogen.sh && \ 72 | ./configure --disable-memory-leak-detection && \ 73 | make && \ 74 | cd $(CUR_DIR); \ 75 | 76 | $(TESTDIRS): 77 | @make -C $(@:build-%=%) 78 | 79 | $(CLEANDIRS): 80 | @make -C $(@:clean-%=%) clean 81 | 82 | $(CLEANTESTDIRS): 83 | @make -C $(@:clean-%=%) clean 84 | 85 | # Extend default clean rule 86 | clean: clean-extra 87 | 88 | clean-extra: $(CLEANDIRS) \ 89 | $(CLEANTESTDIRS) 90 | -------------------------------------------------------------------------------- /junit_xsl.xslt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | 10 | 11 |

12 | Unittest report 13 |

14 | 15 |

16 | 17 | Total tests run 18 | , failures: 19 | 20 | 21 | 22 | 23 | 24 |

25 |
26 | 27 | 28 |

29 | 30 |

31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |
Tests runTests failedOther errors
43 |
44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 |
Tests namePASS/FAILFailing caseReason
53 |
54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | PASS 65 | 66 | 67 | 68 | 69 | 70 | 71 | FAIL 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | FAIL 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 |
-------------------------------------------------------------------------------- /source/sn_coap_header_check.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2015 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 | 17 | /** 18 | * \file sn_coap_header_check.c 19 | * 20 | * \brief CoAP Header validity checker 21 | * 22 | * Functionality: Checks validity of CoAP Header 23 | * 24 | */ 25 | 26 | /* * * * INCLUDE FILES * * * */ 27 | #include "ns_types.h" 28 | #include "mbed-coap/sn_coap_header.h" 29 | #include "mbed-coap/sn_coap_protocol.h" 30 | #include "sn_coap_header_internal.h" 31 | #include "sn_coap_protocol_internal.h" 32 | #include "mbed-trace/mbed_trace.h" 33 | 34 | #define TRACE_GROUP "coap" 35 | 36 | /** 37 | * \fn int8_t sn_coap_header_validity_check(sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version) 38 | * 39 | * \brief Checks validity of given Header 40 | * 41 | * \param *src_coap_msg_ptr is source for building Packet data 42 | * \param coap_version is version of used CoAP specification 43 | * 44 | * \return Return value is status of validity check. In ok cases 0 and in 45 | * failure cases -1 46 | */ 47 | int8_t sn_coap_header_validity_check(const sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version) 48 | { 49 | /* * Check validity of CoAP Version * */ 50 | if (coap_version != COAP_VERSION_1) { 51 | return -1; 52 | } 53 | 54 | /* * Check validity of Message type * */ 55 | switch (src_coap_msg_ptr->msg_type) { 56 | case COAP_MSG_TYPE_CONFIRMABLE: 57 | case COAP_MSG_TYPE_NON_CONFIRMABLE: 58 | case COAP_MSG_TYPE_ACKNOWLEDGEMENT: 59 | case COAP_MSG_TYPE_RESET: 60 | break; /* Ok cases */ 61 | default: 62 | tr_error("sn_coap_header_validity_check - unknown message type!"); 63 | return -1; /* Failed case */ 64 | } 65 | 66 | /* * Check validity of Message code * */ 67 | switch (src_coap_msg_ptr->msg_code) { 68 | case COAP_MSG_CODE_EMPTY: 69 | case COAP_MSG_CODE_REQUEST_GET: 70 | case COAP_MSG_CODE_REQUEST_POST: 71 | case COAP_MSG_CODE_REQUEST_PUT: 72 | case COAP_MSG_CODE_REQUEST_DELETE: 73 | case COAP_MSG_CODE_RESPONSE_CREATED: 74 | case COAP_MSG_CODE_RESPONSE_DELETED: 75 | case COAP_MSG_CODE_RESPONSE_VALID: 76 | case COAP_MSG_CODE_RESPONSE_CHANGED: 77 | case COAP_MSG_CODE_RESPONSE_CONTENT: 78 | case COAP_MSG_CODE_RESPONSE_BAD_REQUEST: 79 | case COAP_MSG_CODE_RESPONSE_UNAUTHORIZED: 80 | case COAP_MSG_CODE_RESPONSE_BAD_OPTION: 81 | case COAP_MSG_CODE_RESPONSE_FORBIDDEN: 82 | case COAP_MSG_CODE_RESPONSE_NOT_FOUND: 83 | case COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED: 84 | case COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE: 85 | case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE: 86 | case COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED: 87 | case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE: 88 | case COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT: 89 | case COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR: 90 | case COAP_MSG_CODE_RESPONSE_NOT_IMPLEMENTED: 91 | case COAP_MSG_CODE_RESPONSE_BAD_GATEWAY: 92 | case COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE: 93 | case COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT: 94 | case COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED: 95 | case COAP_MSG_CODE_RESPONSE_CONTINUE: 96 | break; /* Ok cases */ 97 | default: 98 | tr_error("sn_coap_header_validity_check - unknown message code!"); 99 | return -1; /* Failed case */ 100 | } 101 | 102 | /* Success */ 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /.settings/org.eclipse.cdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | environment/project/0.2056004298.141662914.1517984773.801758434.1788862897.788959847/CC/delimiter=; 3 | environment/project/0.2056004298.141662914.1517984773.801758434.1788862897.788959847/CC/operation=append 4 | environment/project/0.2056004298.141662914.1517984773.801758434.1788862897.788959847/CC/value=gcc 5 | environment/project/0.2056004298.141662914.1517984773.801758434.1788862897.788959847/V/delimiter=; 6 | environment/project/0.2056004298.141662914.1517984773.801758434.1788862897.788959847/V/operation=append 7 | environment/project/0.2056004298.141662914.1517984773.801758434.1788862897.788959847/V/value=1 8 | environment/project/0.2056004298.141662914.1517984773.801758434.1788862897.788959847/append=true 9 | environment/project/0.2056004298.141662914.1517984773.801758434.1788862897.788959847/appendContributed=true 10 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/CPU/delimiter=; 11 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/CPU/operation=replace 12 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/CPU/value=Cortex-M0 13 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/DEBUG/delimiter=; 14 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/DEBUG/operation=replace 15 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/DEBUG/value=1 16 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/PLATFORM/delimiter=; 17 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/PLATFORM/operation=append 18 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/PLATFORM/value=arm-unknown-linux-uclibcgnueabi- 19 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/V/delimiter=; 20 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/V/operation=append 21 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/V/value=1 22 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/append=true 23 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701.1678729521/appendContributed=true 24 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/CPU/delimiter=; 25 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/CPU/operation=replace 26 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/CPU/value=Cortex-M0 27 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/DEBUG/delimiter=; 28 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/DEBUG/operation=replace 29 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/DEBUG/value=1 30 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/PLATFORM/delimiter=; 31 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/PLATFORM/operation=append 32 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/PLATFORM/value=arm-unknown-linux-uclibcgnueabi- 33 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/V/delimiter=; 34 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/V/operation=append 35 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/V/value=1 36 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/append=true 37 | environment/project/0.2056004298.141662914.2121498000.642060230.2118977701/appendContributed=true 38 | environment/project/0.2056004298.1497082229.1854995973/CC/delimiter=; 39 | environment/project/0.2056004298.1497082229.1854995973/CC/operation=append 40 | environment/project/0.2056004298.1497082229.1854995973/CC/value=ArmCC 41 | environment/project/0.2056004298.1497082229.1854995973/CPU/delimiter=; 42 | environment/project/0.2056004298.1497082229.1854995973/CPU/operation=replace 43 | environment/project/0.2056004298.1497082229.1854995973/CPU/value=Cortex-M0 44 | environment/project/0.2056004298.1497082229.1854995973/DEBUG/delimiter=; 45 | environment/project/0.2056004298.1497082229.1854995973/DEBUG/operation=replace 46 | environment/project/0.2056004298.1497082229.1854995973/DEBUG/value=1 47 | environment/project/0.2056004298.1497082229.1854995973/V/delimiter=; 48 | environment/project/0.2056004298.1497082229.1854995973/V/operation=append 49 | environment/project/0.2056004298.1497082229.1854995973/V/value=1 50 | environment/project/0.2056004298.1497082229.1854995973/append=true 51 | environment/project/0.2056004298.1497082229.1854995973/appendContributed=true 52 | environment/project/0.2056004298.1497082229/CC/delimiter=; 53 | environment/project/0.2056004298.1497082229/CC/operation=append 54 | environment/project/0.2056004298.1497082229/CC/value=ArmCC 55 | environment/project/0.2056004298.1497082229/CPU/delimiter=; 56 | environment/project/0.2056004298.1497082229/CPU/operation=append 57 | environment/project/0.2056004298.1497082229/CPU/value=Cortex-M3 58 | environment/project/0.2056004298.1497082229/V/delimiter=; 59 | environment/project/0.2056004298.1497082229/V/operation=append 60 | environment/project/0.2056004298.1497082229/V/value=1 61 | environment/project/0.2056004298.1497082229/append=true 62 | environment/project/0.2056004298.1497082229/appendContributed=true 63 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/sn_coap_protocol_stub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 | 17 | 18 | #include 19 | #include /* For libary malloc() */ 20 | #include /* For memset() and memcpy() */ 21 | #ifdef __linux__ 22 | #include 23 | #endif 24 | 25 | #include "ns_types.h" 26 | #include "sn_coap_protocol.h" 27 | #include "sn_coap_header_internal.h" 28 | #include "sn_coap_protocol_internal.h" 29 | #include "sn_coap_protocol_stub.h" 30 | 31 | uint16_t sn_coap_block_data_size = 0; 32 | 33 | uint8_t sn_coap_resending_queue_msgs = 0; 34 | uint8_t sn_coap_resending_queue_bytes = 0; 35 | uint8_t sn_coap_resending_count = 0; 36 | uint8_t sn_coap_resending_intervall = 0; 37 | 38 | uint8_t sn_coap_duplication_buffer_size = 0; 39 | 40 | sn_coap_protocol_stub_def sn_coap_protocol_stub; 41 | 42 | int8_t sn_coap_protocol_destroy(struct coap_s *handle) 43 | { 44 | return sn_coap_protocol_stub.expectedInt8; 45 | } 46 | 47 | struct coap_s *sn_coap_protocol_init(void *(*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void *), 48 | uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *), 49 | int8_t (*used_rx_callback_ptr)(sn_coap_hdr_s *, sn_nsdl_addr_s *, void *param)) 50 | { 51 | if( sn_coap_protocol_stub.expectedCoap ){ 52 | sn_coap_protocol_stub.expectedCoap->sn_coap_protocol_free = used_free_func_ptr; 53 | sn_coap_protocol_stub.expectedCoap->sn_coap_protocol_malloc = used_malloc_func_ptr; 54 | sn_coap_protocol_stub.expectedCoap->sn_coap_rx_callback = used_rx_callback_ptr; 55 | sn_coap_protocol_stub.expectedCoap->sn_coap_tx_callback = used_tx_callback_ptr; 56 | //sn_coap_protocol_stub.expectedCoap->linked_list_resent_msgs = 57 | } 58 | return sn_coap_protocol_stub.expectedCoap; 59 | } 60 | 61 | int8_t sn_coap_protocol_set_block_size(struct coap_s *handle, uint16_t block_size) 62 | { 63 | return sn_coap_protocol_stub.expectedInt8; 64 | } 65 | 66 | int8_t sn_coap_protocol_set_duplicate_buffer_size(struct coap_s *handle, uint8_t message_count) 67 | { 68 | return sn_coap_protocol_stub.expectedInt8; 69 | } 70 | 71 | int8_t sn_coap_protocol_set_retransmission_parameters(struct coap_s *handle, uint8_t resending_count, uint8_t resending_intervall) 72 | { 73 | return sn_coap_protocol_stub.expectedInt8; 74 | } 75 | 76 | int8_t sn_coap_protocol_set_retransmission_buffer(struct coap_s *handle, uint8_t buffer_size_messages, uint16_t buffer_size_bytes) 77 | { 78 | return sn_coap_protocol_stub.expectedInt8; 79 | } 80 | 81 | void sn_coap_protocol_clear_retransmission_buffer(struct coap_s *handle) 82 | { 83 | } 84 | 85 | int8_t prepare_blockwise_message(struct coap_s *handle, sn_coap_hdr_s *src_coap_msg_ptr) 86 | { 87 | return sn_coap_protocol_stub.expectedInt8; 88 | } 89 | 90 | int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, 91 | uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, void *param) 92 | { 93 | return sn_coap_protocol_stub.expectedInt16; 94 | } 95 | 96 | sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t packet_data_len, uint8_t *packet_data_ptr, void *param) 97 | { 98 | return sn_coap_protocol_stub.expectedHeader; 99 | } 100 | 101 | int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time) 102 | { 103 | return sn_coap_protocol_stub.expectedInt8; 104 | } 105 | 106 | coap_send_msg_s *sn_coap_protocol_allocate_mem_for_msg(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t packet_data_len) 107 | { 108 | return sn_coap_protocol_stub.expectedSendMsg; 109 | } 110 | 111 | void sn_coap_protocol_block_remove(struct coap_s *handle, sn_nsdl_addr_s *source_address, uint16_t payload_length, void *payload) 112 | { 113 | } 114 | 115 | void *sn_coap_protocol_malloc_copy(struct coap_s *handle, const void *source, uint_fast16_t length) 116 | { 117 | void *dest = handle->sn_coap_protocol_malloc(length); 118 | 119 | if ((dest) && (source)) { 120 | memcpy(dest, source, length); 121 | } 122 | return dest; 123 | } 124 | 125 | /* 126 | * This should logically be part and accessed via of the coap_s just as malloc() & free() 127 | * are, but that would require the client to fill one up, as a wrapper filled from this 128 | * class would need access to the handle itself. 129 | */ 130 | void *sn_coap_protocol_calloc(struct coap_s *handle, uint_fast16_t length) 131 | { 132 | void *result = handle->sn_coap_protocol_malloc(length); 133 | 134 | if (result) { 135 | memset(result, 0, length); 136 | } 137 | return result; 138 | } 139 | 140 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/stubs/sn_coap_parser_stub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 | 17 | /** 18 | *\file sn_coap_parser.c 19 | * 20 | * \brief CoAP Header parser 21 | * 22 | * Functionality: Parses CoAP Header 23 | * 24 | */ 25 | 26 | #include "ns_types.h" 27 | #include "sn_coap_header.h" 28 | #include "sn_coap_protocol_internal.h" 29 | #include "sn_coap_parser_stub.h" 30 | 31 | sn_coap_parser_def sn_coap_parser_stub; 32 | 33 | sn_coap_hdr_s *sn_coap_parser(struct coap_s *handle, uint16_t packet_data_len, uint8_t *packet_data_ptr, coap_version_e *coap_version_ptr) 34 | { 35 | return sn_coap_parser_stub.expectedHeader; 36 | } 37 | 38 | void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr) 39 | { 40 | if (freed_coap_msg_ptr != NULL) { 41 | if (freed_coap_msg_ptr->uri_path_ptr != NULL) { 42 | free(freed_coap_msg_ptr->uri_path_ptr); 43 | freed_coap_msg_ptr->uri_path_ptr = NULL; 44 | } 45 | 46 | if (freed_coap_msg_ptr->token_ptr != NULL) { 47 | free(freed_coap_msg_ptr->token_ptr); 48 | } 49 | 50 | if (freed_coap_msg_ptr->options_list_ptr != NULL) { 51 | if (freed_coap_msg_ptr->options_list_ptr->proxy_uri_ptr != NULL) { 52 | free(freed_coap_msg_ptr->options_list_ptr->proxy_uri_ptr); 53 | } 54 | 55 | if (freed_coap_msg_ptr->options_list_ptr->etag_ptr != NULL) { 56 | free(freed_coap_msg_ptr->options_list_ptr->etag_ptr); 57 | } 58 | 59 | if (freed_coap_msg_ptr->options_list_ptr->uri_host_ptr != NULL) { 60 | free(freed_coap_msg_ptr->options_list_ptr->uri_host_ptr); 61 | } 62 | 63 | if (freed_coap_msg_ptr->options_list_ptr->location_path_ptr != NULL) { 64 | free(freed_coap_msg_ptr->options_list_ptr->location_path_ptr); 65 | } 66 | 67 | if (freed_coap_msg_ptr->options_list_ptr->location_query_ptr != NULL) { 68 | free(freed_coap_msg_ptr->options_list_ptr->location_query_ptr); 69 | } 70 | 71 | if (freed_coap_msg_ptr->options_list_ptr->uri_query_ptr != NULL) { 72 | free(freed_coap_msg_ptr->options_list_ptr->uri_query_ptr); 73 | } 74 | 75 | free(freed_coap_msg_ptr->options_list_ptr); 76 | } 77 | 78 | free(freed_coap_msg_ptr); 79 | freed_coap_msg_ptr = NULL; 80 | } 81 | } 82 | 83 | sn_coap_hdr_s *sn_coap_parser_init_message(sn_coap_hdr_s *coap_msg_ptr) 84 | { 85 | /* * * * Check given pointer * * * */ 86 | if (coap_msg_ptr == NULL) { 87 | return NULL; 88 | } 89 | 90 | /* XXX not technically legal to memset pointers to 0 */ 91 | memset(coap_msg_ptr, 0x00, sizeof(sn_coap_hdr_s)); 92 | 93 | return coap_msg_ptr; 94 | } 95 | 96 | sn_coap_hdr_s *sn_coap_parser_alloc_message_with_options(struct coap_s *handle) 97 | { 98 | // check the handle just as in any other place 99 | if (handle == NULL) { 100 | return NULL; 101 | } 102 | 103 | sn_coap_hdr_s *coap_msg_ptr = sn_coap_parser_alloc_message(handle); 104 | 105 | sn_coap_options_list_s *options_list_ptr = sn_coap_parser_alloc_options(handle, coap_msg_ptr); 106 | 107 | if ((coap_msg_ptr == NULL) || (options_list_ptr == NULL)) { 108 | 109 | // oops, out of memory free if got already any 110 | free(coap_msg_ptr); 111 | free(options_list_ptr); 112 | 113 | coap_msg_ptr = NULL; 114 | } 115 | 116 | return coap_msg_ptr; 117 | } 118 | 119 | sn_coap_hdr_s *sn_coap_parser_alloc_message(struct coap_s *handle) 120 | { 121 | sn_coap_hdr_s *returned_coap_msg_ptr; 122 | 123 | /* * * * Check given pointer * * * */ 124 | if (handle == NULL) { 125 | return NULL; 126 | } 127 | 128 | /* * * * Allocate memory for returned CoAP message and initialize allocated memory with with default values * * * */ 129 | returned_coap_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_hdr_s)); 130 | 131 | return sn_coap_parser_init_message(returned_coap_msg_ptr); 132 | } 133 | 134 | sn_coap_options_list_s *sn_coap_parser_alloc_options(struct coap_s *handle, sn_coap_hdr_s *coap_msg_ptr) 135 | { 136 | /* * * * Check given pointers * * * */ 137 | if (handle == NULL || coap_msg_ptr == NULL) { 138 | return NULL; 139 | } 140 | 141 | /* * * * If the message already has options, return them * * * */ 142 | if (coap_msg_ptr->options_list_ptr) { 143 | return coap_msg_ptr->options_list_ptr; 144 | } 145 | 146 | /* * * * Allocate memory for options and initialize allocated memory with with default values * * * */ 147 | coap_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s)); 148 | 149 | if (coap_msg_ptr->options_list_ptr == NULL) { 150 | return NULL; 151 | } 152 | 153 | /* XXX not technically legal to memset pointers to 0 */ 154 | memset(coap_msg_ptr->options_list_ptr, 0x00, sizeof(sn_coap_options_list_s)); 155 | 156 | return coap_msg_ptr->options_list_ptr; 157 | } 158 | -------------------------------------------------------------------------------- /source/include/sn_coap_protocol_internal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2015 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 | 17 | /** 18 | * \file sn_coap_protocol_internal.h 19 | * 20 | * \brief Header file for CoAP Protocol part 21 | * 22 | */ 23 | 24 | #ifndef SN_COAP_PROTOCOL_INTERNAL_H_ 25 | #define SN_COAP_PROTOCOL_INTERNAL_H_ 26 | 27 | #include "ns_list.h" 28 | #include "sn_coap_header_internal.h" 29 | #include "mbed-coap/sn_config.h" 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | struct sn_coap_hdr_; 36 | 37 | #define RESPONSE_RANDOM_FACTOR 1.5 /**< Resending random factor, value is specified in IETF CoAP specification */ 38 | 39 | /* * For Option handling * */ 40 | #define COAP_OPTION_MAX_AGE_DEFAULT 60 /**< Default value of Max-Age if option not present */ 41 | #define COAP_OPTION_URI_PORT_NONE (-1) /**< Internal value to represent no Uri-Port option */ 42 | #define COAP_OPTION_BLOCK_NONE (-1) /**< Internal value to represent no Block1/2 option */ 43 | 44 | int8_t prepare_blockwise_message(struct coap_s *handle, struct sn_coap_hdr_ *coap_hdr_ptr); 45 | 46 | /* Structure which is stored to Linked list for message sending purposes */ 47 | typedef struct coap_send_msg_ { 48 | uint_fast8_t resending_counter; /* Tells how many times message is still tried to resend */ 49 | uint32_t resending_time; /* Tells next resending time */ 50 | 51 | sn_nsdl_transmit_s send_msg_ptr; 52 | 53 | void *param; /* Extra parameter that will be passed to TX/RX callback functions */ 54 | 55 | ns_list_link_t link; 56 | } coap_send_msg_s; 57 | 58 | typedef NS_LIST_HEAD(coap_send_msg_s, link) coap_send_msg_list_t; 59 | 60 | /* Structure which is stored to Linked list for message duplication detection purposes */ 61 | typedef struct coap_duplication_info_ { 62 | uint32_t timestamp; /* Tells when duplication information is stored to Linked list */ 63 | uint16_t msg_id; 64 | uint16_t packet_len; 65 | uint8_t *packet_ptr; 66 | sn_nsdl_addr_s *address; 67 | void *param; 68 | ns_list_link_t link; 69 | } coap_duplication_info_s; 70 | 71 | typedef NS_LIST_HEAD(coap_duplication_info_s, link) coap_duplication_info_list_t; 72 | 73 | /* Structure which is stored to Linked list for blockwise messages sending purposes */ 74 | typedef struct coap_blockwise_msg_ { 75 | uint32_t timestamp; /* Tells when Blockwise message is stored to Linked list */ 76 | 77 | sn_coap_hdr_s *coap_msg_ptr; 78 | 79 | void *param; 80 | uint16_t msg_id; 81 | 82 | ns_list_link_t link; 83 | } coap_blockwise_msg_s; 84 | 85 | typedef NS_LIST_HEAD(coap_blockwise_msg_s, link) coap_blockwise_msg_list_t; 86 | 87 | /* Structure which is stored to Linked list for blockwise messages receiving purposes */ 88 | typedef struct coap_blockwise_payload_ { 89 | uint8_t addr_len; 90 | uint8_t token_len; 91 | bool use_size1; 92 | uint16_t port; 93 | uint16_t payload_len; 94 | uint8_t *addr_ptr; 95 | uint32_t block_number; 96 | uint8_t *token_ptr; 97 | uint8_t *payload_ptr; 98 | uint32_t timestamp; /* Tells when Payload is stored to Linked list */ 99 | ns_list_link_t link; 100 | } coap_blockwise_payload_s; 101 | 102 | typedef NS_LIST_HEAD(coap_blockwise_payload_s, link) coap_blockwise_payload_list_t; 103 | 104 | struct coap_s { 105 | uint8_t sn_coap_resending_queue_msgs; 106 | uint8_t sn_coap_resending_count; 107 | uint8_t sn_coap_resending_intervall; 108 | uint8_t sn_coap_duplication_buffer_size; 109 | uint8_t sn_coap_internal_block2_resp_handling; /* If this is set then coap itself sends a next GET request automatically */ 110 | uint16_t sn_coap_block_data_size; 111 | #if ENABLE_RESENDINGS 112 | uint16_t count_resent_msgs; 113 | #endif 114 | #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT 115 | uint16_t count_duplication_msgs; 116 | #endif 117 | 118 | void *(*sn_coap_protocol_malloc)(uint16_t); 119 | void (*sn_coap_protocol_free)(void *); 120 | 121 | uint8_t (*sn_coap_tx_callback)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *); 122 | int8_t (*sn_coap_rx_callback)(sn_coap_hdr_s *, sn_nsdl_addr_s *, void *); 123 | 124 | #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */ 125 | coap_send_msg_list_t linked_list_resent_msgs; /* Active resending messages are stored to this Linked list */ 126 | #endif 127 | 128 | #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */ 129 | coap_duplication_info_list_t linked_list_duplication_msgs; /* Messages for duplicated messages detection is stored to this Linked list */ 130 | #endif 131 | 132 | #if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwise is not enabled, this part of code will not be compiled */ 133 | coap_blockwise_msg_list_t linked_list_blockwise_sent_msgs; /* Blockwise message to to be sent is stored to this Linked list */ 134 | coap_blockwise_payload_list_t linked_list_blockwise_received_payloads; /* Blockwise payload to to be received is stored to this Linked list */ 135 | #endif 136 | 137 | uint32_t system_time; /* System time seconds */ 138 | uint32_t sn_coap_resending_queue_bytes; 139 | }; 140 | 141 | /* Utility function which performs a call to sn_coap_protocol_malloc() and memset's the result to zero. */ 142 | void *sn_coap_protocol_calloc(struct coap_s *handle, uint_fast16_t length); 143 | 144 | /* Utility function which performs a call to sn_coap_protocol_malloc() and memcopy's the source to result buffer. */ 145 | void *sn_coap_protocol_malloc_copy(struct coap_s *handle, const void *source, uint_fast16_t length); 146 | 147 | #ifdef __cplusplus 148 | } 149 | #endif 150 | 151 | #endif /* SN_COAP_PROTOCOL_INTERNAL_H_ */ 152 | 153 | -------------------------------------------------------------------------------- /apache-2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | Apache License 4 | 5 | Version 2.0, January 2004 6 | 7 | http://www.apache.org/licenses/ 8 | 9 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 10 | 11 | 1. Definitions. 12 | 13 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 14 | 15 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 16 | 17 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 18 | 19 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 20 | 21 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 22 | 23 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 24 | 25 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 26 | 27 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 28 | 29 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 30 | 31 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 32 | 33 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 34 | 35 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 36 | 37 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 38 | 39 | You must give any other recipients of the Work or Derivative Works a copy of this License; and 40 | You must cause any modified files to carry prominent notices stating that You changed the files; and 41 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 42 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 43 | 44 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 45 | 46 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 47 | 48 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 49 | 50 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 51 | 52 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 53 | 54 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 55 | 56 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /.settings/language.settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /mbed-coap/sn_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 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 | 17 | #ifndef SN_CONFIG_H 18 | #define SN_CONFIG_H 19 | 20 | #ifdef MBED_CLIENT_USER_CONFIG_FILE 21 | #include MBED_CLIENT_USER_CONFIG_FILE 22 | #endif 23 | 24 | #ifdef MBED_CLOUD_CLIENT_USER_CONFIG_FILE 25 | #include MBED_CLOUD_CLIENT_USER_CONFIG_FILE 26 | #endif 27 | 28 | #ifdef MBED_CLOUD_CLIENT_CONFIGURATION_ENABLED 29 | #include "mbed-cloud-client/MbedCloudClientConfig.h" 30 | #endif 31 | 32 | /** 33 | * \brief Configuration options (set of defines and values) 34 | * 35 | * This lists set of compile-time options that needs to be used to enable 36 | * or disable features selectively, and set the values for the mandatory 37 | * parameters. 38 | */ 39 | 40 | /** 41 | * \def SN_COAP_DUPLICATION_MAX_MSGS_COUNT 42 | * \brief For Message duplication detection 43 | * Init value for the maximum count of messages to be stored for duplication detection 44 | * Setting of this value to 0 will disable duplication check, also reduce use of ROM memory 45 | * Default is set to 0. 46 | */ 47 | #ifdef MBED_CONF_MBED_CLIENT_SN_COAP_DUPLICATION_MAX_MSGS_COUNT 48 | #define SN_COAP_DUPLICATION_MAX_MSGS_COUNT MBED_CONF_MBED_CLIENT_SN_COAP_DUPLICATION_MAX_MSGS_COUNT 49 | #endif 50 | 51 | #ifndef SN_COAP_DUPLICATION_MAX_MSGS_COUNT 52 | #define SN_COAP_DUPLICATION_MAX_MSGS_COUNT 0 53 | #endif 54 | 55 | /** 56 | * \def SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE 57 | * \brief For Message blockwising 58 | * Init value for the maximum payload size to be sent and received at one blockwise message 59 | * Setting of this value to 0 with SN_COAP_BLOCKWISE_ENABLED will disable this feature, and 60 | * also reduce use of ROM memory. 61 | * Note: This define is common for both received and sent Blockwise messages 62 | */ 63 | #ifdef MBED_CONF_MBED_CLIENT_SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE 64 | #define SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE MBED_CONF_MBED_CLIENT_SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE 65 | #endif 66 | 67 | #ifndef SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE 68 | #define SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE 0 /**< Must be 2^x and x is at least 4. Suitable values: 0, 16, 32, 64, 128, 256, 512 and 1024 */ 69 | #endif 70 | 71 | /** 72 | * \def SN_COAP_CONSTANT_NEEDED_SIZE 73 | * \brief Avoid needed size calculations 74 | * If this is defined, sn_coap_builder_calc_needed_packet_data_size always returns that value, 75 | * saving a lot of calculation code, at the cost of outgoing TX buffers being oversized, and 76 | * with danger of them being undersized. 77 | * 78 | * sn_coap_builder_payload_build does not have any size input to limit its output, so it is 79 | * always wise for users to assert that it has not output more than the size returned by 80 | * sn_coap_builder_calc_needed_packet_size, whether this option is defined or not. 81 | */ 82 | #ifdef MBED_CONF_MBED_CLIENT_SN_COAP_CONSTANT_NEEDED_SIZE 83 | #define SN_COAP_CONSTANT_NEEDED_SIZE MBED_CONF_MBED_CLIENT_SN_COAP_CONSTANT_NEEDED_SIZE 84 | #endif 85 | 86 | //#define SN_COAP_CONSTANT_NEEDED_SIZE 1024 87 | 88 | /** 89 | * \def SN_COAP_DISABLE_RESENDINGS 90 | * \brief Disables resending feature. Resending feature should not be needed 91 | * when using CoAP with TCP transport for example. By default resendings are 92 | * enabled. Set to 1 to disable. 93 | */ 94 | #ifdef SN_COAP_DISABLE_RESENDINGS 95 | #define ENABLE_RESENDINGS 0 /** Disable resendings **/ 96 | #else 97 | #define ENABLE_RESENDINGS 1 /**< Enable / Disable resending from library in building */ 98 | #endif 99 | 100 | /** 101 | * \def SN_COAP_RESENDING_QUEUE_SIZE_MSGS 102 | * \brief Sets the number of messages stored 103 | * in the resending queue. Default is 2 104 | */ 105 | #ifdef MBED_CONF_MBED_CLIENT_SN_COAP_RESENDING_QUEUE_SIZE_MSGS 106 | #define SN_COAP_RESENDING_QUEUE_SIZE_MSGS MBED_CONF_MBED_CLIENT_SN_COAP_RESENDING_QUEUE_SIZE_MSGS 107 | #endif 108 | 109 | #ifndef SN_COAP_RESENDING_QUEUE_SIZE_MSGS 110 | #define SN_COAP_RESENDING_QUEUE_SIZE_MSGS 2 /**< Default re-sending queue size - defines how many messages can be stored. Setting this to 0 disables feature */ 111 | #endif 112 | 113 | /** 114 | * \def DEFAULT_RESPONSE_TIMEOUT 115 | * \brief Sets the CoAP re-send interval in seconds. 116 | * By default is 10 seconds. 117 | */ 118 | 119 | #ifdef MBED_CONF_MBED_CLIENT_DEFAULT_RESPONSE_TIMEOUT 120 | #define DEFAULT_RESPONSE_TIMEOUT MBED_CONF_MBED_CLIENT_DEFAULT_RESPONSE_TIMEOUT 121 | #endif 122 | 123 | #ifndef DEFAULT_RESPONSE_TIMEOUT 124 | #define DEFAULT_RESPONSE_TIMEOUT 10 /**< Default re-sending timeout as seconds */ 125 | #endif 126 | 127 | /** 128 | * \def SN_COAP_RESENDING_QUEUE_SIZE_BYTES 129 | * \brief Sets the size of the re-sending buffer. 130 | * Setting this to 0 disables this feature. 131 | * By default, this feature is disabled. 132 | */ 133 | #ifdef MBED_CONF_MBED_CLIENT_SN_COAP_RESENDING_QUEUE_SIZE_BYTES 134 | #define SN_COAP_RESENDING_QUEUE_SIZE_BYTES MBED_CONF_MBED_CLIENT_SN_COAP_RESENDING_QUEUE_SIZE_BYTES 135 | #endif 136 | 137 | #ifndef SN_COAP_RESENDING_QUEUE_SIZE_BYTES 138 | #define SN_COAP_RESENDING_QUEUE_SIZE_BYTES 0 /**< Default re-sending queue size - defines size of the re-sending buffer. Setting this to 0 disables feature */ 139 | #endif 140 | 141 | /** 142 | * \def SN_COAP_MAX_INCOMING_MESSAGE_SIZE 143 | * \brief Sets the maximum size (in bytes) that 144 | * mbed Client will allow to be handled while 145 | * receiving big payload in blockwise mode. 146 | * Application can set this value based on their 147 | * available storage capability. 148 | * By default, maximum size is UINT16_MAX, 65535 bytes. 149 | */ 150 | #ifndef SN_COAP_MAX_INCOMING_MESSAGE_SIZE 151 | #define SN_COAP_MAX_INCOMING_MESSAGE_SIZE UINT16_MAX 152 | #endif 153 | 154 | /** 155 | * \def SN_COAP_MAX_NONBLOCKWISE_PAYLOAD_SIZE 156 | * \brief Sets the maximum payload size allowed before blockwising the message. 157 | * This option should only be used when using TCP and TLS as transport 158 | * with known maximum fragment size. This optimizes the number of messages 159 | * if it is possible to send larger than 1kB messages without blockwise transfer. 160 | * If payload length is larger than SN_COAP_MAX_NONBLOCKWISE_PAYLOAD_SIZE 161 | * it will be sent using blockwise transfer. 162 | * By default, this feature is disabled, 0 disables the feature, set to positive 163 | * value larger than SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE to enable. 164 | * Note that value should be less than transport layer maximum fragment size. 165 | * Note that value has no effect if blockwise transfer is disabled. 166 | */ 167 | #ifndef SN_COAP_MAX_NONBLOCKWISE_PAYLOAD_SIZE 168 | #define SN_COAP_MAX_NONBLOCKWISE_PAYLOAD_SIZE 0 169 | #endif 170 | 171 | /** 172 | * \def SN_COAP_BLOCKWISE_ENABLED 173 | * \brief Enables the blockwise functionality in CoAP library also when blockwise payload 174 | * size is set to '0' in SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE. 175 | */ 176 | #ifndef SN_COAP_BLOCKWISE_ENABLED 177 | #define SN_COAP_BLOCKWISE_ENABLED 0 /**< Enable blockwise */ 178 | #endif 179 | 180 | /** 181 | * \def SN_COAP_RESENDING_MAX_COUNT 182 | * \brief Defines how many times CoAP library tries to re-send the CoAP packet. 183 | * By default value is 3. 184 | */ 185 | #ifdef MBED_CONF_MBED_CLIENT_RECONNECTION_COUNT 186 | #define SN_COAP_RESENDING_MAX_COUNT MBED_CONF_MBED_CLIENT_RECONNECTION_COUNT 187 | #endif 188 | 189 | #ifndef SN_COAP_RESENDING_MAX_COUNT 190 | #define SN_COAP_RESENDING_MAX_COUNT 3 191 | #endif 192 | 193 | /** 194 | * \def SN_COAP_MAX_ALLOWED_RESENDING_COUNT 195 | * \brief Maximum allowed count of re-sending that can be set at runtime via 196 | * 'sn_coap_protocol_set_retransmission_parameters()' API. 197 | * By default value is 6. 198 | */ 199 | #ifndef SN_COAP_MAX_ALLOWED_RESENDING_COUNT 200 | #define SN_COAP_MAX_ALLOWED_RESENDING_COUNT 6 /**< Maximum allowed count of re-sending */ 201 | #endif 202 | 203 | /** 204 | * \def SN_COAP_MAX_ALLOWED_RESPONSE_TIMEOUT 205 | * \brief Maximum allowed re-send interval in seconds that can be set at runtime via 206 | * 'sn_coap_protocol_set_retransmission_parameters()' API. 207 | * By default value is 40. 208 | */ 209 | #ifndef SN_COAP_MAX_ALLOWED_RESPONSE_TIMEOUT 210 | #define SN_COAP_MAX_ALLOWED_RESPONSE_TIMEOUT 40 /**< Maximum allowed re-sending timeout */ 211 | #endif 212 | 213 | /** 214 | * \def SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_MSGS 215 | * \brief Maximum allowed count of messages that can be stored into resend buffer at runtime via 216 | * 'sn_coap_protocol_set_retransmission_buffer()' API. 217 | * By default value is 6. 218 | */ 219 | #ifndef SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_MSGS 220 | #define SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_MSGS 6 /**< Maximum allowed number of saved re-sending messages */ 221 | #endif 222 | 223 | /** 224 | * \def SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_BYTES 225 | * \brief Maximum size of messages in bytes that can be stored into resend buffer at runtime via 226 | * 'sn_coap_protocol_set_retransmission_buffer()' API. 227 | * By default value is 512. 228 | */ 229 | #ifndef SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_BYTES 230 | #define SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_BYTES 512 /**< Maximum allowed size of re-sending buffer */ 231 | #endif 232 | 233 | /** 234 | * \def SN_COAP_MAX_ALLOWED_DUPLICATION_MESSAGE_COUNT 235 | * \brief Maximum allowed number of saved messages for message duplicate searching 236 | * that can be set via 'sn_coap_protocol_set_duplicate_buffer_size' API. 237 | * By default value is 6. 238 | */ 239 | #ifndef SN_COAP_MAX_ALLOWED_DUPLICATION_MESSAGE_COUNT 240 | #define SN_COAP_MAX_ALLOWED_DUPLICATION_MESSAGE_COUNT 6 241 | #endif 242 | 243 | /** 244 | * \def SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED 245 | * \brief Maximum time in seconds howe long message is kept for duplicate detection. 246 | * By default 60 seconds. 247 | */ 248 | #ifdef MBED_CONF_MBED_CLIENT_SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED 249 | #define SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED MBED_CONF_MBED_CLIENT_SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED 250 | #endif 251 | 252 | #ifndef SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED 253 | #define SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED 300 /** RESPONSE_TIMEOUT * RESPONSE_RANDOM_FACTOR * (2 ^ MAX_RETRANSMIT - 1) + the expected maximum round trip time **/ 254 | #endif 255 | 256 | /** 257 | * \def SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED 258 | * \brief Maximum time in seconds how long (messages and payload) are be stored for blockwising. 259 | * Longer time will increase the memory consumption in lossy networks. 260 | * By default 60 seconds. 261 | */ 262 | #ifdef MBED_CONF_MBED_CLIENT_SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED 263 | #define SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED MBED_CONF_MBED_CLIENT_SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED 264 | #endif 265 | 266 | #ifndef SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED 267 | #define SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED 300 /**< Maximum time in seconds of data (messages and payload) to be stored for blockwising */ 268 | #endif 269 | 270 | /** 271 | * \def SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE 272 | * \brief Maximum size of blockwise message that can be received. 273 | * By default 65535 bytes. 274 | */ 275 | #ifdef MBED_CONF_MBED_CLIENT_SN_COAP_MAX_INCOMING_MESSAGE_SIZE 276 | #define SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE MBED_CONF_MBED_CLIENT_SN_COAP_MAX_INCOMING_MESSAGE_SIZE 277 | #endif 278 | 279 | #ifndef SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE 280 | #define SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE UINT16_MAX 281 | #endif 282 | 283 | /** 284 | * \def SN_COAP_BLOCKWISE_INTERNAL_BLOCK_2_HANDLING_ENABLED 285 | * \brief A size optimization switch, which removes the blockwise Block2 response if set to 0. 286 | * handling code which is typically overridden by a call of "sn_coap_protocol_handle_block2_response_internally(coap, false);". 287 | * By default the code is there, so the override can be reversed by "sn_coap_protocol_handle_block2_response_internally(coap, true)". 288 | */ 289 | #ifndef SN_COAP_BLOCKWISE_INTERNAL_BLOCK_2_HANDLING_ENABLED 290 | #define SN_COAP_BLOCKWISE_INTERNAL_BLOCK_2_HANDLING_ENABLED 1 291 | #endif 292 | 293 | /** 294 | * \def SN_COAP_REDUCE_BLOCKWISE_HEAP_FOOTPRINT 295 | * \brief A heap optimization switch, which removes unnecessary copy of the blockwise data. 296 | * If enabled, application must NOT free the payload when it gets the COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED status. 297 | * Application must call sn_coap_protocol_block_remove() instead. 298 | */ 299 | #ifndef SN_COAP_REDUCE_BLOCKWISE_HEAP_FOOTPRINT 300 | #define SN_COAP_REDUCE_BLOCKWISE_HEAP_FOOTPRINT 0 /**< Disabled by default */ 301 | #endif 302 | 303 | #endif // SN_CONFIG_H 304 | -------------------------------------------------------------------------------- /mbed-coap/sn_coap_protocol.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2015 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 | 17 | /** 18 | * \file sn_coap_protocol.h 19 | * 20 | * \brief CoAP C-library User protocol interface header file 21 | */ 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | #ifndef SN_COAP_PROTOCOL_H_ 28 | #define SN_COAP_PROTOCOL_H_ 29 | 30 | #include "sn_coap_header.h" 31 | 32 | /** 33 | * \fn struct coap_s *sn_coap_protocol_init(void* (*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void*), 34 | uint8_t (*used_tx_callback_ptr)(sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *), 35 | int8_t (*used_rx_callback_ptr)(sn_coap_hdr_s *, sn_nsdl_addr_s *) 36 | * 37 | * \brief Initializes CoAP Protocol part. When using libNsdl, sn_nsdl_init() calls this function. 38 | * 39 | * \param *used_malloc_func_ptr is function pointer for used memory allocation function. 40 | * 41 | * \param *used_free_func_ptr is function pointer for used memory free function. Note: the implementation 42 | * must handle NULL parameter and ignore it just as typical libc's free() does. 43 | * 44 | * \param *used_tx_callback_ptr function callback pointer to tx function for sending coap messages 45 | * 46 | * \param *used_rx_callback_ptr used to return CoAP header struct with status COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED 47 | * when re-sendings exceeded. If set to NULL, no error message is returned. 48 | * 49 | * \return Pointer to handle when success 50 | * Null if failed 51 | */ 52 | 53 | extern struct coap_s *sn_coap_protocol_init(void *(*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void *), 54 | uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *), 55 | int8_t (*used_rx_callback_ptr)(sn_coap_hdr_s *, sn_nsdl_addr_s *, void *)); 56 | 57 | /** 58 | * \fn int8_t sn_coap_protocol_destroy(void) 59 | * 60 | * \brief Frees all memory from CoAP protocol part 61 | * 62 | * \param *handle Pointer to CoAP library handle 63 | * 64 | * \return Return value is always 0 65 | */ 66 | extern int8_t sn_coap_protocol_destroy(struct coap_s *handle); 67 | 68 | /** 69 | * \fn int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, void *param) 70 | * 71 | * \brief Builds Packet data from given CoAP header structure to be sent 72 | * 73 | * \param *dst_addr_ptr is pointer to destination address where CoAP message 74 | * will be sent (CoAP builder needs that information for message resending purposes) 75 | * 76 | * \param *dst_packet_data_ptr is pointer to destination of built Packet data 77 | * 78 | * \param *src_coap_msg_ptr is pointer to source of built Packet data 79 | * 80 | * \param param void pointer that will be passed to tx/rx function callback when those are called. 81 | * 82 | * \return Return value is byte count of built Packet data.\n 83 | * Note: If message is blockwised, all payload is not sent at the same time\n 84 | * In failure cases:\n 85 | * -1 = Failure in CoAP header structure\n 86 | * -2 = Failure in given pointer (= NULL)\n 87 | * -3 = Failure in Reset message\n 88 | * -4 = Failure in Resending message store\n 89 | * If there is not enough memory (or User given limit exceeded) for storing 90 | * resending messages, situation is ignored. 91 | */ 92 | extern int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, void *param); 93 | 94 | /** 95 | * \fn sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t packet_data_len, uint8_t *packet_data_ptr) 96 | * 97 | * \brief Parses received CoAP message from given Packet data 98 | * 99 | * \param *src_addr_ptr is pointer to source address of received CoAP message 100 | * (CoAP parser needs that information for Message acknowledgement) 101 | * 102 | * \param *handle Pointer to CoAP library handle 103 | * 104 | * \param packet_data_len is length of given Packet data to be parsed to CoAP message 105 | * 106 | * \param *packet_data_ptr is pointer to source of Packet data to be parsed to CoAP message 107 | * 108 | * \param param void pointer that will be passed to tx/rx function callback when those are called. 109 | * 110 | * \return Return value is pointer to parsed CoAP message structure. This structure includes also coap_status field.\n 111 | * In following failure cases NULL is returned:\n 112 | * -Given NULL pointer\n 113 | * -Failure in parsed header of non-confirmable message\ŋ 114 | * -Out of memory (malloc() returns NULL) 115 | */ 116 | extern sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t packet_data_len, uint8_t *packet_data_ptr, void *); 117 | 118 | /** 119 | * \fn int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time) 120 | * 121 | * \brief Sends CoAP messages from re-sending queue, if there is any. 122 | * Cleans also old messages from the duplication list and from block receiving list 123 | * 124 | * This function can be called e.g. once in a second but also more frequently. 125 | * 126 | * \param *handle Pointer to CoAP library handle 127 | * 128 | * \param current_time is System time in seconds. This time is 129 | * used for message re-sending timing and to identify old saved data. 130 | * 131 | * \return 0 if success 132 | * -1 if failed 133 | */ 134 | 135 | extern int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time); 136 | 137 | /** 138 | * \fn int8_t sn_coap_protocol_set_block_size(uint16_t block_size) 139 | * 140 | * \brief If block transfer is enabled, this function changes the block size. 141 | * 142 | * \param uint16_t block_size maximum size of CoAP payload. Valid sizes are 16, 32, 64, 128, 256, 512 and 1024 bytes 143 | * \return 0 = success 144 | * -1 = failure 145 | */ 146 | extern int8_t sn_coap_protocol_set_block_size(struct coap_s *handle, uint16_t block_size); 147 | 148 | /** 149 | * \fn int8_t sn_coap_protocol_set_duplicate_buffer_size(uint8_t message_count) 150 | * 151 | * \brief If dublicate message detection is enabled, this function changes buffer size. 152 | * 153 | * \param uint8_t message_count max number of messages saved for duplicate control 154 | * \return 0 = success 155 | * -1 = failure 156 | */ 157 | extern int8_t sn_coap_protocol_set_duplicate_buffer_size(struct coap_s *handle, uint8_t message_count); 158 | 159 | /** 160 | * \fn int8_t sn_coap_protocol_set_retransmission_parameters(uint8_t resending_count, uint8_t resending_intervall) 161 | * 162 | * \brief If re-transmissions are enabled, this function changes resending count and interval. 163 | * 164 | * \param uint8_t resending_count max number of resendings for message 165 | * \param uint8_t resending_intervall message resending intervall in seconds 166 | * \return 0 = success, -1 = failure 167 | */ 168 | extern int8_t sn_coap_protocol_set_retransmission_parameters(struct coap_s *handle, 169 | uint8_t resending_count, uint8_t resending_interval); 170 | 171 | /** 172 | * \fn int8_t sn_coap_protocol_set_retransmission_buffer(uint8_t buffer_size_messages, uint16_t buffer_size_bytes) 173 | * 174 | * \brief If re-transmissions are enabled, this function changes message retransmission queue size. 175 | * Set size to '0' to disable feature. If both are set to '0', then re-sendings are disabled. 176 | * 177 | * \param uint8_t buffer_size_messages queue size - maximum number of messages to be saved to queue 178 | * \param uint8_t buffer_size_bytes queue size - maximum size of messages saved to queue 179 | * \return 0 = success, -1 = failure 180 | */ 181 | extern int8_t sn_coap_protocol_set_retransmission_buffer(struct coap_s *handle, 182 | uint8_t buffer_size_messages, uint16_t buffer_size_bytes); 183 | 184 | /** 185 | * \fn void sn_coap_protocol_clear_retransmission_buffer(struct coap_s *handle) 186 | * 187 | * \param *handle Pointer to CoAP library handle 188 | * 189 | * \brief If re-transmissions are enabled, this function removes all messages from the retransmission queue. 190 | */ 191 | extern void sn_coap_protocol_clear_retransmission_buffer(struct coap_s *handle); 192 | 193 | /** 194 | * \fn sn_coap_protocol_block_remove 195 | * 196 | * \brief Remove saved block data. Can be used to remove the data from RAM to enable storing it to other place. 197 | * 198 | * \param handle Pointer to CoAP library handle 199 | * \param source_address Addres from where the block has been received. 200 | * \param payload_length Length of the coap payload of the block. 201 | * \param payload Coap payload of the block. 202 | * 203 | */ 204 | extern void sn_coap_protocol_block_remove(struct coap_s *handle, sn_nsdl_addr_s *source_address, uint16_t payload_length, void *payload); 205 | 206 | /** 207 | * \fn sn_coap_protocol_remove_sent_blockwise_message 208 | * 209 | * \brief Remove sent blockwise message from the linked list. 210 | * 211 | * \param handle Pointer to CoAP library handle 212 | * \param message_id Message id to be removed. 213 | * 214 | */ 215 | extern void sn_coap_protocol_remove_sent_blockwise_message(struct coap_s *handle, uint16_t message_id); 216 | 217 | /** 218 | * \fn void sn_coap_protocol_delete_retransmission(struct coap_s *handle) 219 | * 220 | * \param *handle Pointer to CoAP library handle 221 | * \msg_id message ID to be removed 222 | * \return returns 0 when success, -1 for invalid parameter, -2 if message was not found 223 | * 224 | * \brief If re-transmissions are enabled, this function removes message from retransmission buffer. 225 | */ 226 | extern int8_t sn_coap_protocol_delete_retransmission(struct coap_s *handle, uint16_t msg_id); 227 | 228 | /** 229 | * \fn void sn_coap_protocol_delete_retransmission_by_token(struct coap_s *handle) 230 | * 231 | * \param *handle Pointer to CoAP library handle 232 | * \token Token to be removed 233 | * \token_len Length of the token 234 | * \return returns 0 when success, -1 for invalid parameter, -2 if message was not found 235 | * 236 | * \brief If re-transmissions are enabled, this function removes message from retransmission buffer. 237 | */ 238 | extern int8_t sn_coap_protocol_delete_retransmission_by_token(struct coap_s *handle, const uint8_t *token, uint8_t token_len); 239 | 240 | /** 241 | * \fn int8_t sn_coap_convert_block_size(uint16_t block_size) 242 | * 243 | * \brief Utility function to convert block size. 244 | * 245 | * \param block_size Block size to convert. 246 | * 247 | * \return Value of range 0 - 6 248 | */ 249 | extern int8_t sn_coap_convert_block_size(uint16_t block_size); 250 | 251 | /** 252 | * \fn int8_t sn_coap_protocol_handle_block2_response_internally(struct coap_s *handle, uint8_t handle_response) 253 | * 254 | * \brief This function change the state whether CoAP library sends the block 2 response automatically or not. 255 | * 256 | * \param *handle Pointer to CoAP library handle 257 | * \param handle_response 1 if CoAP library handles the response sending otherwise 0. 258 | * 259 | * \return 0 = success, -1 = failure 260 | */ 261 | extern int8_t sn_coap_protocol_handle_block2_response_internally(struct coap_s *handle, uint8_t handle_response); 262 | 263 | /** 264 | * \fn void sn_coap_protocol_clear_sent_blockwise_messages(struct coap_s *handle) 265 | * 266 | * \brief This function clears all the sent blockwise messages from the linked list. 267 | * 268 | * \param *handle Pointer to CoAP library handle 269 | */ 270 | extern void sn_coap_protocol_clear_sent_blockwise_messages(struct coap_s *handle); 271 | 272 | /** 273 | * \fn void sn_coap_protocol_clear_received_blockwise_messages(struct coap_s *handle) 274 | * 275 | * \brief This function clears all the received blockwise messages from the linked list. 276 | * 277 | * \param *handle Pointer to CoAP library handle 278 | */ 279 | extern void sn_coap_protocol_clear_received_blockwise_messages(struct coap_s *handle); 280 | 281 | /** 282 | * \fn void sn_coap_protocol_send_rst(struct coap_s *handle, uint16_t msg_id, sn_nsdl_addr_s *addr_ptr, void *param) 283 | * 284 | * \brief This function sends a RESET message. 285 | * 286 | * \param *handle Pointer to CoAP library handle 287 | * \param msg_id Message id. 288 | * \param addr_ptr Pointer to destination address where CoAP message will be sent 289 | * \param param Pointer that will be passed to tx function callback 290 | */ 291 | extern void sn_coap_protocol_send_rst(struct coap_s *handle, uint16_t msg_id, sn_nsdl_addr_s *addr_ptr, void *param); 292 | 293 | /** 294 | * \fn uint16_t sn_coap_protocol_get_configured_blockwise_size(struct coap_s *handle) 295 | * 296 | * \brief Get configured CoAP payload blockwise size 297 | * 298 | * \param *handle Pointer to CoAP library handle 299 | */ 300 | extern uint16_t sn_coap_protocol_get_configured_blockwise_size(struct coap_s *handle); 301 | 302 | /** 303 | * \fn void sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, const uint8_t *src_addr_ptr, const uint16_t port, const uint16_t msg_id); 304 | * 305 | * \brief Removes stored Duplication info from Linked list. 306 | * 307 | * \param *handle Pointer to CoAP library handle 308 | * \param *addr_ptr is pointer to Address key to be removed 309 | * \param port is Port key to be removed 310 | * \param msg_id is Message ID key to be removed 311 | */ 312 | extern void sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, 313 | const uint8_t *src_addr_ptr, 314 | const uint16_t port, 315 | const uint16_t msg_id); 316 | 317 | #endif /* SN_COAP_PROTOCOL_H_ */ 318 | 319 | #ifdef __cplusplus 320 | } 321 | #endif 322 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | ## [v5.1.11](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.11) 3 | 4 | Block-Wise request (block1) error handling improvements: 5 | * Removed SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED usage. Block-Wise requests will now follow normal retranmission rules. 6 | * Process block1 responses only once. If response is coming in wrong order just ignore it wait next response to happen. 7 | 8 | ## [v5.1.10](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.10) 9 | 10 | - Fix regression from previous release concerning 1024 byte blocksize operations. 11 | - Do not store ACK's into duplicate list. 12 | 13 | ## [v5.1.9](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.9) 14 | 15 | - Code size optimizations. 16 | 17 | ## [v5.1.8](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.8) 18 | 19 | - Fix to blockwise code. Now uses block size received in packet, not block size defined in code. 20 | 21 | ## [v5.1.7](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.7) 22 | 23 | - Removed comparison of IP addresses when validating message resending. This avoids unnessary network errors due to load balancers causing frequent IP address changes. 24 | 25 | ## [v5.1.6](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.6) 26 | 27 | - Multiple fixes for out-ouf-bounds memory accesses, a memory leak and an infinite loop condition in packet parser. 28 | - Mapped `MBED_CONF_MBED_CLIENT_RECONNECTION_COUNT` Pelion Device Management Client configuration option to `SN_COAP_RESENDING_MAX_COUNT` in mbed-coap. 29 | - Fix the token from blockwise ACK message to be empty if the received message doesn't have one. 30 | 31 | ## [v5.1.5](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.5) 32 | 33 | - Added handling for duplicate message handling for Block2 messages. Previously CoAP was silently ignoring the duplicate messages. Now proper response will be sent. 34 | - Added extended version of `sn_coap_protocol_update_duplicate_package_data`, `sn_coap_protocol_update_duplicate_package_data_all` which will handle all CoAP data in the list. 35 | 36 | ## [v5.1.4](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.4) 37 | 38 | - Add also 4.13 (Request Entity Too Large) responses to duplicate info list. 39 | - Add client library configurations for `DEFAULT_RESPONSE_TIMEOUT` and `SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED`. 40 | - Increased the default timeouts of `DEFAULT_RESPONSE_TIMEOUT` and `SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED` to 300 seconds. 41 | * These two are critical parameters for low-bandwidth high-latency networks. The defaults should be more geared towards such networks that are likely to have issues with transmissions. 42 | * The increased defaults can increase the runtime HEAP usage when there is a lot of duplicates or retransmissions. 43 | 44 | ## [v5.1.3](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.3) 45 | 46 | - Fix potential integer overflow when calculating CoAP data packet size: IOTCLT-3748 CVE-2019-17211 - mbed-coap integer overflow 47 | - Fix buffer overflow when parsing CoAP message: IOTCLT-3749 CVE-2019-17212 - mbed-coap Buffer overflow 48 | 49 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v5.1.2...v5.1.3) 50 | 51 | 52 | ## [v5.1.2](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.2) 53 | 54 | - Compiler warning cleanups. 55 | 56 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v5.1.1...v5.1.2) 57 | 58 | 59 | ## [v5.1.1](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.1) 60 | 61 | - Delay the random initialization of message id to a later phase and not during init() so there is enough time 62 | for system to complete the rest of the initialization. 63 | 64 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v5.1.0...v5.1.1) 65 | 66 | 67 | ## [v5.1.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.0) 68 | 69 | - Introduce SN_COAP_REDUCE_BLOCKWISE_HEAP_FOOTPRINT configuration flag. 70 | Flag is disabled by default to keep the backward compatibility in place. 71 | If flag is enabled, application must NOT free the payload when it gets the COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED status. 72 | And application must call sn_coap_protocol_block_remove() instead. 73 | 74 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v5.0.0...v5.1.0) 75 | 76 | ## [v5.0.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.0.0) 77 | 78 | - Reduce heap footprint by storing only single block when receiving a blockwise message. 79 | * User is now responsible of freeing the data by calling sn_coap_protocol_block_remove() and must not free the payload separately. 80 | - Bug fix: Request blockwise transfer if incoming payload length is too large and when it comes without block indication. 81 | 82 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.8.1...v5.0.0) 83 | 84 | ## [v4.8.1](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.8.1) 85 | - Store ACK's also into duplicate info list. 86 | - ROM size optimization. Flash size has gone down ~1100 bytes. 87 | 88 | **Closed issues:** 89 | - IOTCLT-3592 - Client does not handle Duplicate ACK messages during blockwise registration correctly 90 | 91 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.8.0...v4.8.1) 92 | 93 | ## [v4.8.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.8.0) 94 | - Make `sn_coap_protocol_linked_list_duplication_info_remove` API to public. User might want to delete some messages from the duplicate list. 95 | - Enable support for unified client configuration. 96 | 97 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.7.4...v4.8.0) 98 | 99 | ## [v4.7.4](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.7.4) 100 | 101 | - Remove dependency to yotta tool 102 | - Do not remove stored (GET) blockwise message when EMPTY ACK received 103 | When non piggybacked response mode is used original GET request must not be removed from the stored message list. 104 | Message is needed for building the next (GET) blockwise message. 105 | - Move definitions to sn_config.h 106 | 107 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.7.3...v4.7.4) 108 | 109 | ## [v4.7.3](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.7.3) 110 | 111 | - Do not store EMPTY response to blockwise list 112 | An Empty message only contains the 4-byte header so it does not require any blockwise operations. 113 | This will fix unneseccary message sending timeouts which leads mbed cloud client to do unnecessary 114 | reconnections which increases the network traffic. 115 | 116 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.7.2...v4.7.3) 117 | 118 | ## [v4.7.2](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.7.2) 119 | 120 | - Fix handling of duplicate blockwise ACK's 121 | CoAP data buffer was not added into duplication info store when creating response for blockwise request. 122 | This leads to case where whole bootstrap flow just timeouts if received any duplicate messages during blockwise operation. 123 | Fixes error: IOTCLT-3188 - UDP connection fails for lost ACK sending 124 | 125 | - Remove error trace when building reset message without options 126 | This makes it possible to build the reset message without allocating option or getting error message. 127 | 128 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.7.1...v4.7.2) 129 | 130 | ## [v4.7.1](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.7.1) 131 | 132 | - Fix CoAP stored blockwise message release and list continue 133 | Add re-scan routine goto if message is caused user callback 134 | This will fix hard fault when blockwise message sending timeouts. This happens cause same list is manipulated through rx callback. 135 | 136 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.7.0...v4.7.1) 137 | 138 | ## [v4.7.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.7.0) 139 | 140 | - Add function that can be used to clear the received blockwise payloads for example in the case of a connection error. 141 | - Silence compiler warning when CoAP duplicate detection is enabled. 142 | 143 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.6.3...v4.7.0) 144 | 145 | ## [v4.6.3](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.6.3) 146 | 147 | - Bug fix: Remove timed out blockwise message from resend queue. If blockwise message was timed out message was still kept in the resend queue which causes unnecessary reconnections on client side. 148 | - Documentation: Document all the available macros. 149 | 150 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.6.2...v4.6.3) 151 | 152 | ## [v4.6.2](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.6.2) 153 | 154 | Do not clear block2 in subsequent block request. 155 | 156 | When sending a request with block2 option, eg. indicating need 157 | for response to be blockwised, copy the block2 option from the 158 | sent_blockwise list item so that the block2 option will be added 159 | to all requests. This fixes an issue where previously the block2 160 | was only sent for the first blockwise request and not for the 161 | subsequent ones, including the last request. This made the response 162 | not follow the request block2 option. 163 | 164 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.6.1...v4.6.2) 165 | 166 | ## [v4.6.1](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.6.1) 167 | **Closed issues:** 168 | - IOTCLT-2900 - Blockwise handling leaking memory in some error cases 169 | 170 | Fix unused parameter compiler warning when blockwise is not used. 171 | 172 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.6.0...v4.6.1) 173 | 174 | ## [v4.6.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.6.0) 175 | **New feature:** 176 | - Add new API which clears one item from the resend queue based on token 177 | 178 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.5.1...v4.6.0) 179 | 180 | ## [v4.5.1](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.5.1) 181 | **Closed issues:** 182 | - IOTCLT-2883 - Blockwise observations not completing 183 | 184 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.5.0...v4.5.1) 185 | 186 | ## [v4.5.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.5.0) 187 | **Closed issues:** 188 | - IIOTCLT-2769 - mbed-coap: extra response received after registration 189 | 190 | Added own flag to enable blockwise support, without setting default blockwise 191 | payload size. This allows to receive blockwise messages while still sending 192 | without blockwise. 193 | 194 | Fix CoAP request blockwise response handling 195 | When request is sent, response can have blockwise option set. All requests must 196 | be stored to the linked list. 197 | 198 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.4.4...v4.5.0) 199 | 200 | ## [v4.4.4](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.4.4) 201 | **Closed issues:** 202 | - IOTCLT-2638 [GitHub] hardfault during reconnection retry with Thread 203 | 204 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.4.3...v4.4.4) 205 | 206 | ## [v4.4.3](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.4.3) 207 | **Closed issues:** 208 | - IOTCLT-2506 [GitHub] Cannot set registration time if server does not use max age option 209 | 210 | Extend blockwise message transfer status to have states for sending as well. 211 | 212 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.4.2...v4.4.3) 213 | 214 | ## [v4.4.2](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.4.2) 215 | **Closed issues:** 216 | - IOTCLT-2469 CoAP UDP retransmission does not work for blocks after first one for requests (Eg. registration POST) 217 | 218 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.4.1...v4.4.2) 219 | 220 | ## [v4.4.1](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.4.1) 221 | **Closed issues:** 222 | - IOTCLT-2539 Block wise messaging call-backs not working logically 223 | 224 | Improve TCP+TLS transport layer to allow send larger messages without blockwising. 225 | 226 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.4.0...v4.4.1) 227 | 228 | ## [v4.4.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.4.0) 229 | **New feature:** 230 | - Make sn_coap_protocol_send_rst as public needed for CoAP ping sending 231 | - Allow disabling resendings by defining SN_COAP_DISABLE_RESENDINGS 232 | 233 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.3.0...v4.4.0) 234 | 235 | ## [v4.3.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.3.0) 236 | **New feature:** 237 | - Add new API which clears the whole sent blockwise message list 238 | 239 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.2.0...v4.3.0) 240 | 241 | ## [v4.2.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.2.0) 242 | **New feature:** 243 | - Add new API to remove sent blockwise message from the linked list 244 | 245 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.1.1...v4.2.0) 246 | 247 | ## [v4.1.1](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.1.1) 248 | **Closed issues:** 249 | - IOTCLT-2203 mbed-coap does not handle PUT or POST if they indicate a smaller block size preference (fixed regression) 250 | 251 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.1.0...v4.1.1) 252 | 253 | ## [v4.1.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.1.0) 254 | 255 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.0.10...v4.1.0) 256 | 257 | **New feature:** 258 | - New API to disable automatic GET(BLOCK2) request sending. 259 | 260 | **Closed issues:** 261 | - IOTCLT-2203 mbed-coap does not handle PUT or POST if they indicate a smaller block size preference 262 | 263 | ## [v4.0.10](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.0.10) 264 | 265 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.0.9...v4.0.10) 266 | 267 | **Closed issues:** 268 | - IOTMAC-615 Node mDS registration failure during OTA transfer 269 | 270 | ## [v4.0.9](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.0.9) 271 | 272 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.0.8...v4.0.9) 273 | 274 | **Closed issues:** 275 | - IOTCLT-1899 Maximum COAP message resending buffer size limited to 255 bytes 276 | - IOTCLT-1888 Problem with blockwise transfers that are even increments of block_size 277 | 278 | ## [v4.0.8](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.0.8) 279 | 280 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.0.4...v4.0.8) 281 | 282 | **Closed issues:** 283 | - IOTCLT-1880 Lwm2m blockwise data transfer (using Block1 option) with Leshan not working 284 | - IOTCLT-1885 Return 4.08 Request Entity Incomplete on Block transfer errors 285 | - IOTCLT-1883 Detected message duplications stop mbed-client 286 | 287 | ## [v4.0.4](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.0.4) 288 | 289 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.0.3...v4.0.4) 290 | 291 | **Closed issues:** 292 | - https://github.com/ARMmbed/mbed-client/issues/481 - Obs Con blockwise fails to transmit 2nd block 293 | 294 | ## [v4.0.3](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.0.3) 295 | 296 | -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.0.2...v4.0.3) 297 | 298 | **New feature** 299 | 300 | - CoAP Protocol Confirmable resend fix and minor memory optimization (IOTMAC-328) 301 | 302 | **Closed issues:** 303 | 304 | - IOTCLT-1439 - stuck in while loop 305 | 306 | ## [v4.0.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.0.2) 307 | 308 | **New feature** 309 | 310 | - Initial release of mbed-coap separated from mbed-client-c 311 | -------------------------------------------------------------------------------- /mbed-coap/sn_coap_header.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2015 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 | 17 | /** 18 | * \file sn_coap_header.h 19 | * 20 | * \brief CoAP C-library User header interface header file 21 | */ 22 | 23 | #ifndef SN_COAP_HEADER_H_ 24 | #define SN_COAP_HEADER_H_ 25 | 26 | #include "sn_config.h" 27 | #include "ns_types.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /* Handle structure */ 34 | struct coap_s; 35 | 36 | /* * * * * * * * * * * * * * */ 37 | /* * * * ENUMERATIONS * * * */ 38 | /* * * * * * * * * * * * * * */ 39 | 40 | /** 41 | * \brief Enumeration for CoAP Version 42 | */ 43 | typedef enum coap_version_ { 44 | COAP_VERSION_1 = 0x40, 45 | COAP_VERSION_UNKNOWN = 0xFF 46 | } coap_version_e; 47 | 48 | /** 49 | * \brief Enumeration for CoAP Message type, used in CoAP Header 50 | */ 51 | typedef enum sn_coap_msg_type_ { 52 | COAP_MSG_TYPE_CONFIRMABLE = 0x00, /**< Reliable Request messages */ 53 | COAP_MSG_TYPE_NON_CONFIRMABLE = 0x10, /**< Non-reliable Request and Response messages */ 54 | COAP_MSG_TYPE_ACKNOWLEDGEMENT = 0x20, /**< Response to a Confirmable Request */ 55 | COAP_MSG_TYPE_RESET = 0x30 /**< Answer a Bad Request */ 56 | } sn_coap_msg_type_e; 57 | 58 | /** 59 | * \brief Enumeration for CoAP Message code, used in CoAP Header 60 | */ 61 | typedef enum sn_coap_msg_code_ { 62 | COAP_MSG_CODE_EMPTY = 0, 63 | COAP_MSG_CODE_REQUEST_GET = 1, 64 | COAP_MSG_CODE_REQUEST_POST = 2, 65 | COAP_MSG_CODE_REQUEST_PUT = 3, 66 | COAP_MSG_CODE_REQUEST_DELETE = 4, 67 | 68 | COAP_MSG_CODE_RESPONSE_CREATED = 65, 69 | COAP_MSG_CODE_RESPONSE_DELETED = 66, 70 | COAP_MSG_CODE_RESPONSE_VALID = 67, 71 | COAP_MSG_CODE_RESPONSE_CHANGED = 68, 72 | COAP_MSG_CODE_RESPONSE_CONTENT = 69, 73 | COAP_MSG_CODE_RESPONSE_CONTINUE = 95, 74 | COAP_MSG_CODE_RESPONSE_BAD_REQUEST = 128, 75 | COAP_MSG_CODE_RESPONSE_UNAUTHORIZED = 129, 76 | COAP_MSG_CODE_RESPONSE_BAD_OPTION = 130, 77 | COAP_MSG_CODE_RESPONSE_FORBIDDEN = 131, 78 | COAP_MSG_CODE_RESPONSE_NOT_FOUND = 132, 79 | COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED = 133, 80 | COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE = 134, 81 | COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE = 136, 82 | COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED = 140, 83 | COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE = 141, 84 | COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT = 143, 85 | COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR = 160, 86 | COAP_MSG_CODE_RESPONSE_NOT_IMPLEMENTED = 161, 87 | COAP_MSG_CODE_RESPONSE_BAD_GATEWAY = 162, 88 | COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE = 163, 89 | COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT = 164, 90 | COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED = 165 91 | } sn_coap_msg_code_e; 92 | 93 | /** 94 | * \brief Enumeration for CoAP Option number, used in CoAP Header 95 | */ 96 | typedef enum sn_coap_option_numbers_ { 97 | COAP_OPTION_IF_MATCH = 1, 98 | COAP_OPTION_URI_HOST = 3, 99 | COAP_OPTION_ETAG = 4, 100 | COAP_OPTION_IF_NONE_MATCH = 5, 101 | COAP_OPTION_OBSERVE = 6, 102 | COAP_OPTION_URI_PORT = 7, 103 | COAP_OPTION_LOCATION_PATH = 8, 104 | COAP_OPTION_URI_PATH = 11, 105 | COAP_OPTION_CONTENT_FORMAT = 12, 106 | COAP_OPTION_MAX_AGE = 14, 107 | COAP_OPTION_URI_QUERY = 15, 108 | COAP_OPTION_ACCEPT = 17, 109 | COAP_OPTION_LOCATION_QUERY = 20, 110 | COAP_OPTION_BLOCK2 = 23, 111 | COAP_OPTION_BLOCK1 = 27, 112 | COAP_OPTION_SIZE2 = 28, 113 | COAP_OPTION_PROXY_URI = 35, 114 | COAP_OPTION_PROXY_SCHEME = 39, 115 | COAP_OPTION_SIZE1 = 60 116 | // 128 = (Reserved) 117 | // 132 = (Reserved) 118 | // 136 = (Reserved) 119 | } sn_coap_option_numbers_e; 120 | 121 | /** 122 | * \brief Enumeration for CoAP Content Format codes 123 | */ 124 | typedef enum sn_coap_content_format_ { 125 | COAP_CT_NONE = -1, // internal 126 | COAP_CT_TEXT_PLAIN = 0, 127 | COAP_CT_LINK_FORMAT = 40, 128 | COAP_CT_XML = 41, 129 | COAP_CT_OCTET_STREAM = 42, 130 | COAP_CT_EXI = 47, 131 | COAP_CT_JSON = 50, 132 | COAP_CT__MAX = 0xffff 133 | } sn_coap_content_format_e; 134 | 135 | /** 136 | * \brief Enumeration for CoAP Observe option values 137 | * 138 | * draft-ietf-core-observe-16 139 | */ 140 | typedef enum sn_coap_observe_ { 141 | COAP_OBSERVE_NONE = -1, // internal 142 | 143 | // Values for GET requests 144 | COAP_OBSERVE_REGISTER = 0, 145 | COAP_OBSERVE_DEREGISTER = 1, 146 | 147 | // In responses, value is a 24-bit opaque sequence number 148 | COAP_OBSERVE__MAX = 0xffffff 149 | } sn_coap_observe_e; 150 | 151 | /** 152 | * \brief Enumeration for CoAP status, used in CoAP Header 153 | */ 154 | typedef enum sn_coap_status_ { 155 | COAP_STATUS_OK = 0, /**< Default value is OK */ 156 | COAP_STATUS_PARSER_ERROR_IN_HEADER = 1, /**< CoAP will send Reset message to invalid message sender */ 157 | COAP_STATUS_PARSER_DUPLICATED_MSG = 2, /**< CoAP will send Acknowledgement message to duplicated message sender */ 158 | COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING = 3, /**< User will get whole message after all message blocks received. 159 | User must release messages with this status. */ 160 | COAP_STATUS_PARSER_BLOCKWISE_ACK = 4, /**< Acknowledgement for sent Blockwise message received */ 161 | COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED = 5, /**< Blockwise message received but not supported by compiling switch */ 162 | COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED = 6, /**< Blockwise message fully received and returned to app. 163 | User is responsible of freeing the data by calling sn_coap_protocol_block_remove() */ 164 | COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED = 7, /**< When re-transmissions have been done and ACK not received, CoAP library calls 165 | RX callback with this status */ 166 | 167 | COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED = 8, /**< Blockwise message sending timeout. 168 | The msg_id in sn_coap_hdr_s* parameter of RX callback is set to the same value 169 | as in the first block sent, and parameter sn_nsdl_addr_s* is set as NULL. */ 170 | COAP_STATUS_BUILDER_BLOCK_SENDING_DONE = 9 /**< Blockwise message sending, last block sent. 171 | The msg_id in sn_coap_hdr_s* parameter of RX callback is set to the same value 172 | as in the first block sent, and parameter sn_nsdl_addr_s* is set as NULL. */ 173 | 174 | } sn_coap_status_e; 175 | 176 | 177 | /* * * * * * * * * * * * * */ 178 | /* * * * STRUCTURES * * * */ 179 | /* * * * * * * * * * * * * */ 180 | 181 | /** 182 | * \brief Structure for CoAP Options 183 | */ 184 | typedef struct sn_coap_options_list_ { 185 | uint8_t etag_len; /**< 1-8 bytes. Repeatable */ 186 | bool use_size1; 187 | bool use_size2; 188 | 189 | uint16_t proxy_uri_len; /**< 1-1034 bytes. */ 190 | uint16_t uri_host_len; /**< 1-255 bytes. */ 191 | uint16_t location_path_len; /**< 0-255 bytes. Repeatable */ 192 | uint16_t location_query_len; /**< 0-255 bytes. Repeatable */ 193 | uint16_t uri_query_len; /**< 1-255 bytes. Repeatable */ 194 | 195 | sn_coap_content_format_e accept; /**< Value 0-65535. COAP_CT_NONE if not used */ 196 | 197 | uint32_t max_age; /**< Value in seconds (default is 60) */ 198 | uint32_t size1; /**< 0-4 bytes. */ 199 | uint32_t size2; /**< 0-4 bytes. */ 200 | int32_t uri_port; /**< Value 0-65535. -1 if not used */ 201 | int32_t observe; /**< Value 0-0xffffff. -1 if not used */ 202 | int32_t block1; /**< Value 0-0xffffff. -1 if not used. Not for user */ 203 | int32_t block2; /**< Value 0-0xffffff. -1 if not used. Not for user */ 204 | 205 | uint8_t *proxy_uri_ptr; /**< Must be set to NULL if not used */ 206 | uint8_t *etag_ptr; /**< Must be set to NULL if not used */ 207 | uint8_t *uri_host_ptr; /**< Must be set to NULL if not used */ 208 | uint8_t *location_path_ptr; /**< Must be set to NULL if not used */ 209 | uint8_t *location_query_ptr; /**< Must be set to NULL if not used */ 210 | uint8_t *uri_query_ptr; /**< Must be set to NULL if not used */ 211 | } sn_coap_options_list_s; 212 | 213 | /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 214 | /* !!! Main CoAP message struct !!! */ 215 | /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 216 | 217 | /** 218 | * \brief Main CoAP message struct 219 | */ 220 | typedef struct sn_coap_hdr_ { 221 | uint8_t token_len; /**< 1-8 bytes. */ 222 | 223 | sn_coap_status_e coap_status; /**< Used for telling to User special cases when parsing message */ 224 | sn_coap_msg_code_e msg_code; /**< Empty: 0; Requests: 1-31; Responses: 64-191 */ 225 | 226 | sn_coap_msg_type_e msg_type; /**< Confirmable, Non-Confirmable, Acknowledgement or Reset */ 227 | sn_coap_content_format_e content_format; /**< Set to COAP_CT_NONE if not used */ 228 | 229 | uint16_t msg_id; /**< Message ID. Parser sets parsed message ID, builder sets message ID of built coap message */ 230 | uint16_t uri_path_len; /**< 0-255 bytes. Repeatable. */ 231 | uint16_t payload_len; /**< Must be set to zero if not used */ 232 | 233 | uint8_t *token_ptr; /**< Must be set to NULL if not used */ 234 | uint8_t *uri_path_ptr; /**< Must be set to NULL if not used. E.g: temp1/temp2 */ 235 | uint8_t *payload_ptr; /**< Must be set to NULL if not used */ 236 | 237 | /* Here are not so often used Options */ 238 | sn_coap_options_list_s *options_list_ptr; /**< Must be set to NULL if not used */ 239 | } sn_coap_hdr_s; 240 | 241 | /* * * * * * * * * * * * * * */ 242 | /* * * * ENUMERATIONS * * * */ 243 | /* * * * * * * * * * * * * * */ 244 | 245 | 246 | /** 247 | * \brief Used protocol 248 | */ 249 | typedef enum sn_nsdl_capab_ { 250 | SN_NSDL_PROTOCOL_HTTP = 0x01, /**< Unsupported */ 251 | SN_NSDL_PROTOCOL_HTTPS = 0x02, /**< Unsupported */ 252 | SN_NSDL_PROTOCOL_COAP = 0x04 /**< Supported */ 253 | } sn_nsdl_capab_e; 254 | 255 | /* * * * * * * * * * * * * */ 256 | /* * * * STRUCTURES * * * */ 257 | /* * * * * * * * * * * * * */ 258 | 259 | 260 | /** 261 | * \brief Used for creating manually registration message with sn_coap_register() 262 | */ 263 | typedef struct registration_info_ { 264 | uint8_t endpoint_len; 265 | uint8_t endpoint_type_len; 266 | uint16_t links_len; 267 | 268 | uint8_t *endpoint_ptr; /**< Endpoint name */ 269 | uint8_t *endpoint_type_ptr; /**< Endpoint type */ 270 | uint8_t *links_ptr; /**< Resource registration string */ 271 | } registration_info_t; 272 | 273 | 274 | /** 275 | * \brief Address type of given address 276 | */ 277 | typedef enum sn_nsdl_addr_type_ { 278 | SN_NSDL_ADDRESS_TYPE_IPV6 = 0x01, /**< Supported */ 279 | SN_NSDL_ADDRESS_TYPE_IPV4 = 0x02, /**< Supported */ 280 | SN_NSDL_ADDRESS_TYPE_HOSTNAME = 0x03, /**< Unsupported */ 281 | SN_NSDL_ADDRESS_TYPE_NONE = 0xFF 282 | } sn_nsdl_addr_type_e; 283 | 284 | /** 285 | * \brief Address structure of Packet data 286 | */ 287 | typedef struct sn_nsdl_addr_ { 288 | uint8_t addr_len; 289 | sn_nsdl_addr_type_e type; 290 | uint16_t port; 291 | uint8_t *addr_ptr; 292 | } sn_nsdl_addr_s; 293 | 294 | 295 | /* * * * * * * * * * * * * * * * * * * * * * */ 296 | /* * * * EXTERNAL FUNCTION PROTOTYPES * * * */ 297 | /* * * * * * * * * * * * * * * * * * * * * * */ 298 | /** 299 | * \fn sn_coap_hdr_s *sn_coap_parser(struct coap_s *handle, uint16_t packet_data_len, uint8_t *packet_data_ptr, coap_version_e *coap_version_ptr) 300 | * 301 | * \brief Parses CoAP message from given Packet data 302 | * 303 | * \param *handle Pointer to CoAP library handle 304 | * 305 | * \param packet_data_len is length of given Packet data to be parsed to CoAP message 306 | * 307 | * \param *packet_data_ptr is source for Packet data to be parsed to CoAP message 308 | * 309 | * \param *coap_version_ptr is destination for parsed CoAP specification version 310 | * 311 | * \return Return value is pointer to parsed CoAP message.\n 312 | * In following failure cases NULL is returned:\n 313 | * -Failure in given pointer (= NULL)\n 314 | * -Failure in memory allocation (malloc() returns NULL) 315 | */ 316 | extern sn_coap_hdr_s *sn_coap_parser(struct coap_s *handle, uint16_t packet_data_len, uint8_t *packet_data_ptr, coap_version_e *coap_version_ptr); 317 | 318 | /** 319 | * \fn void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr) 320 | * 321 | * \brief Releases memory of given CoAP message 322 | * 323 | * Note!!! Does not release Payload part 324 | * 325 | * \param *handle Pointer to CoAP library handle 326 | * 327 | * \param *freed_coap_msg_ptr is pointer to released CoAP message 328 | */ 329 | extern void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr); 330 | 331 | /** 332 | * \fn int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr) 333 | * 334 | * \brief Builds an outgoing message buffer from a CoAP header structure. 335 | * 336 | * \param *dst_packet_data_ptr is pointer to allocated destination to built CoAP packet 337 | * 338 | * \param *src_coap_msg_ptr is pointer to source structure for building Packet data 339 | * 340 | * \return Return value is byte count of built Packet data. In failure cases:\n 341 | * -1 = Failure in given CoAP header structure\n 342 | * -2 = Failure in given pointer (= NULL) 343 | */ 344 | extern int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr); 345 | 346 | /** 347 | * \fn uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr) 348 | * 349 | * \brief Calculates needed Packet data memory size for given CoAP message 350 | * 351 | * \param *src_coap_msg_ptr is pointer to data which needed Packet 352 | * data length is calculated 353 | * 354 | * \return Return value is count of needed memory as bytes for build Packet data 355 | * Null if failed 356 | */ 357 | extern uint16_t (sn_coap_builder_calc_needed_packet_data_size)(const sn_coap_hdr_s *src_coap_msg_ptr); 358 | #ifdef SN_COAP_CONSTANT_NEEDED_SIZE 359 | #define sn_coap_builder_calc_needed_packet_data_size(m) (SN_COAP_CONSTANT_NEEDED_SIZE) 360 | #endif 361 | 362 | /** 363 | * \fn int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_size) 364 | * 365 | * \brief Builds an outgoing message buffer from a CoAP header structure. 366 | * 367 | * \param *dst_packet_data_ptr is pointer to allocated destination to built CoAP packet 368 | * 369 | * \param *src_coap_msg_ptr is pointer to source structure for building Packet data 370 | * 371 | * \param blockwise_payload_size Blockwise message maximum payload size 372 | * 373 | * \return Return value is byte count of built Packet data. In failure cases:\n 374 | * -1 = Failure in given CoAP header structure\n 375 | * -2 = Failure in given pointer (= NULL) 376 | */ 377 | extern int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size); 378 | 379 | /** 380 | * \fn uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size) 381 | * 382 | * \brief Calculates needed Packet data memory size for given CoAP message 383 | * 384 | * \param *src_coap_msg_ptr is pointer to data which needed Packet 385 | * data length is calculated 386 | * \param blockwise_payload_size Blockwise message maximum payload size 387 | * 388 | * \return Return value is count of needed memory as bytes for build Packet data 389 | * Null if failed 390 | */ 391 | extern uint16_t (sn_coap_builder_calc_needed_packet_data_size_2)(const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size); 392 | #ifdef SN_COAP_CONSTANT_NEEDED_SIZE 393 | #define sn_coap_builder_calc_needed_packet_data_size_2(m, p) (SN_COAP_CONSTANT_NEEDED_SIZE) 394 | #endif 395 | 396 | /** 397 | * \fn sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code) 398 | * 399 | * \brief Prepares generic response packet from a request packet. This function allocates memory for the resulting sn_coap_hdr_s 400 | * 401 | * \param *handle Pointer to CoAP library handle 402 | * \param *coap_packet_ptr The request packet pointer 403 | * \param msg_code response messages code 404 | * 405 | * \return *coap_packet_ptr The allocated and pre-filled response packet pointer 406 | * NULL Error in parsing the request 407 | * 408 | */ 409 | extern sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, const sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code); 410 | 411 | /** 412 | * \brief Initialise a message structure to empty 413 | * 414 | * \param *coap_msg_ptr is pointer to CoAP message to initialise 415 | * 416 | * \return Return value is pointer passed in 417 | */ 418 | extern sn_coap_hdr_s *sn_coap_parser_init_message(sn_coap_hdr_s *coap_msg_ptr); 419 | 420 | /** 421 | * \brief Allocate an empty message structure 422 | * 423 | * \param *handle Pointer to CoAP library handle 424 | * 425 | * \return Return value is pointer to an empty CoAP message.\n 426 | * In following failure cases NULL is returned:\n 427 | * -Failure in given pointer (= NULL)\n 428 | * -Failure in memory allocation (malloc() returns NULL) 429 | */ 430 | extern sn_coap_hdr_s *sn_coap_parser_alloc_message(struct coap_s *handle); 431 | 432 | /** 433 | * \brief Allocates and initializes options list structure 434 | * 435 | * \param *handle Pointer to CoAP library handle 436 | * \param *coap_msg_ptr is pointer to CoAP message that will contain the options 437 | * 438 | * If the message already has a pointer to an option structure, that pointer 439 | * is returned, rather than a new structure being allocated. 440 | * 441 | * \return Return value is pointer to the CoAP options structure.\n 442 | * In following failure cases NULL is returned:\n 443 | * -Failure in given pointer (= NULL)\n 444 | * -Failure in memory allocation (malloc() returns NULL) 445 | */ 446 | extern sn_coap_options_list_s *sn_coap_parser_alloc_options(struct coap_s *handle, sn_coap_hdr_s *coap_msg_ptr); 447 | 448 | extern sn_coap_hdr_s *sn_coap_parser_alloc_message_with_options(struct coap_s *handle); 449 | 450 | #ifdef __cplusplus 451 | } 452 | #endif 453 | 454 | #endif /* SN_COAP_HEADER_H_ */ 455 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/MakefileWorker.mk: -------------------------------------------------------------------------------- 1 | #--------- 2 | # 3 | # MakefileWorker.mk 4 | # 5 | # Include this helper file in your makefile 6 | # It makes 7 | # A static library 8 | # A test executable 9 | # 10 | # See this example for parameter settings 11 | # examples/Makefile 12 | # 13 | #---------- 14 | # Inputs - these variables describe what to build 15 | # 16 | # INCLUDE_DIRS - Directories used to search for include files. 17 | # This generates a -I for each directory 18 | # SRC_DIRS - Directories containing source file to built into the library 19 | # SRC_FILES - Specific source files to build into library. Helpful when not all code 20 | # in a directory can be built for test (hopefully a temporary situation) 21 | # TEST_SRC_DIRS - Directories containing unit test code build into the unit test runner 22 | # These do not go in a library. They are explicitly included in the test runner 23 | # TEST_SRC_FILES - Specific source files to build into the unit test runner 24 | # These do not go in a library. They are explicitly included in the test runner 25 | # MOCKS_SRC_DIRS - Directories containing mock source files to build into the test runner 26 | # These do not go in a library. They are explicitly included in the test runner 27 | #---------- 28 | # You can adjust these variables to influence how to build the test target 29 | # and where to put and name outputs 30 | # See below to determine defaults 31 | # COMPONENT_NAME - the name of the thing being built 32 | # TEST_TARGET - name the test executable. By default it is 33 | # $(COMPONENT_NAME)_tests 34 | # Helpful if you want 1 > make files in the same directory with different 35 | # executables as output. 36 | # CPPUTEST_HOME - where CppUTest home dir found 37 | # TARGET_PLATFORM - Influences how the outputs are generated by modifying the 38 | # CPPUTEST_OBJS_DIR and CPPUTEST_LIB_DIR to use a sub-directory under the 39 | # normal objs and lib directories. Also modifies where to search for the 40 | # CPPUTEST_LIB to link against. 41 | # CPPUTEST_OBJS_DIR - a directory where o and d files go 42 | # CPPUTEST_LIB_DIR - a directory where libs go 43 | # CPPUTEST_ENABLE_DEBUG - build for debug 44 | # CPPUTEST_USE_MEM_LEAK_DETECTION - Links with overridden new and delete 45 | # CPPUTEST_USE_STD_CPP_LIB - Set to N to keep the standard C++ library out 46 | # of the test harness 47 | # CPPUTEST_USE_GCOV - Turn on coverage analysis 48 | # Clean then build with this flag set to Y, then 'make gcov' 49 | # CPPUTEST_MAPFILE - generate a map file 50 | # CPPUTEST_WARNINGFLAGS - overly picky by default 51 | # OTHER_MAKEFILE_TO_INCLUDE - a hook to use this makefile to make 52 | # other targets. Like CSlim, which is part of fitnesse 53 | # CPPUTEST_USE_VPATH - Use Make's VPATH functionality to support user 54 | # specification of source files and directories that aren't below 55 | # the user's Makefile in the directory tree, like: 56 | # SRC_DIRS += ../../lib/foo 57 | # It defaults to N, and shouldn't be necessary except in the above case. 58 | #---------- 59 | # 60 | # Other flags users can initialize to sneak in their settings 61 | # CPPUTEST_CXXFLAGS - flags for the C++ compiler 62 | # CPPUTEST_CPPFLAGS - flags for the C++ AND C preprocessor 63 | # CPPUTEST_CFLAGS - flags for the C complier 64 | # CPPUTEST_LDFLAGS - Linker flags 65 | #---------- 66 | 67 | # Some behavior is weird on some platforms. Need to discover the platform. 68 | 69 | # Platforms 70 | UNAME_OUTPUT = "$(shell uname -a)" 71 | MACOSX_STR = Darwin 72 | MINGW_STR = MINGW 73 | CYGWIN_STR = CYGWIN 74 | LINUX_STR = Linux 75 | SUNOS_STR = SunOS 76 | UNKNWOWN_OS_STR = Unknown 77 | 78 | # Compilers 79 | CC_VERSION_OUTPUT ="$(shell $(CXX) -v 2>&1)" 80 | CLANG_STR = clang 81 | SUNSTUDIO_CXX_STR = SunStudio 82 | 83 | UNAME_OS = $(UNKNWOWN_OS_STR) 84 | 85 | ifeq ($(findstring $(MINGW_STR),$(UNAME_OUTPUT)),$(MINGW_STR)) 86 | UNAME_OS = $(MINGW_STR) 87 | endif 88 | 89 | ifeq ($(findstring $(CYGWIN_STR),$(UNAME_OUTPUT)),$(CYGWIN_STR)) 90 | UNAME_OS = $(CYGWIN_STR) 91 | endif 92 | 93 | ifeq ($(findstring $(LINUX_STR),$(UNAME_OUTPUT)),$(LINUX_STR)) 94 | UNAME_OS = $(LINUX_STR) 95 | endif 96 | 97 | ifeq ($(findstring $(MACOSX_STR),$(UNAME_OUTPUT)),$(MACOSX_STR)) 98 | UNAME_OS = $(MACOSX_STR) 99 | #lion has a problem with the 'v' part of -a 100 | UNAME_OUTPUT = "$(shell uname -pmnrs)" 101 | endif 102 | 103 | ifeq ($(findstring $(SUNOS_STR),$(UNAME_OUTPUT)),$(SUNOS_STR)) 104 | UNAME_OS = $(SUNOS_STR) 105 | 106 | SUNSTUDIO_CXX_ERR_STR = CC -flags 107 | ifeq ($(findstring $(SUNSTUDIO_CXX_ERR_STR),$(CC_VERSION_OUTPUT)),$(SUNSTUDIO_CXX_ERR_STR)) 108 | CC_VERSION_OUTPUT ="$(shell $(CXX) -V 2>&1)" 109 | COMPILER_NAME = $(SUNSTUDIO_CXX_STR) 110 | endif 111 | endif 112 | 113 | ifeq ($(findstring $(CLANG_STR),$(CC_VERSION_OUTPUT)),$(CLANG_STR)) 114 | COMPILER_NAME = $(CLANG_STR) 115 | endif 116 | 117 | #Kludge for mingw, it does not have cc.exe, but gcc.exe will do 118 | ifeq ($(UNAME_OS),$(MINGW_STR)) 119 | CC := gcc 120 | endif 121 | 122 | #And another kludge. Exception handling in gcc 4.6.2 is broken when linking the 123 | # Standard C++ library as a shared library. Unbelievable. 124 | ifeq ($(UNAME_OS),$(MINGW_STR)) 125 | CPPUTEST_LDFLAGS += -static 126 | endif 127 | ifeq ($(UNAME_OS),$(CYGWIN_STR)) 128 | CPPUTEST_LDFLAGS += -static 129 | endif 130 | 131 | 132 | #Kludge for MacOsX gcc compiler on Darwin9 who can't handle pendantic 133 | ifeq ($(UNAME_OS),$(MACOSX_STR)) 134 | ifeq ($(findstring Version 9,$(UNAME_OUTPUT)),Version 9) 135 | CPPUTEST_PEDANTIC_ERRORS = N 136 | endif 137 | endif 138 | 139 | ifndef COMPONENT_NAME 140 | COMPONENT_NAME = name_this_in_the_makefile 141 | endif 142 | 143 | # Debug on by default 144 | ifndef CPPUTEST_ENABLE_DEBUG 145 | CPPUTEST_ENABLE_DEBUG = Y 146 | endif 147 | 148 | # new and delete for memory leak detection on by default 149 | ifndef CPPUTEST_USE_MEM_LEAK_DETECTION 150 | CPPUTEST_USE_MEM_LEAK_DETECTION = Y 151 | endif 152 | 153 | # Use the standard C library 154 | ifndef CPPUTEST_USE_STD_C_LIB 155 | CPPUTEST_USE_STD_C_LIB = Y 156 | endif 157 | 158 | # Use the standard C++ library 159 | ifndef CPPUTEST_USE_STD_CPP_LIB 160 | CPPUTEST_USE_STD_CPP_LIB = Y 161 | endif 162 | 163 | # Use gcov, off by default 164 | ifndef CPPUTEST_USE_GCOV 165 | CPPUTEST_USE_GCOV = N 166 | endif 167 | 168 | ifndef CPPUTEST_PEDANTIC_ERRORS 169 | CPPUTEST_PEDANTIC_ERRORS = Y 170 | endif 171 | 172 | # Default warnings 173 | ifndef CPPUTEST_WARNINGFLAGS 174 | CPPUTEST_WARNINGFLAGS = -Wall -Wextra -Wshadow -Wswitch-default -Wswitch-enum -Wconversion 175 | ifeq ($(CPPUTEST_PEDANTIC_ERRORS), Y) 176 | # CPPUTEST_WARNINGFLAGS += -pedantic-errors 177 | CPPUTEST_WARNINGFLAGS += -pedantic 178 | endif 179 | ifeq ($(UNAME_OS),$(LINUX_STR)) 180 | CPPUTEST_WARNINGFLAGS += -Wsign-conversion 181 | endif 182 | CPPUTEST_CXX_WARNINGFLAGS = -Woverloaded-virtual 183 | CPPUTEST_C_WARNINGFLAGS = -Wstrict-prototypes 184 | endif 185 | 186 | #Wonderful extra compiler warnings with clang 187 | ifeq ($(COMPILER_NAME),$(CLANG_STR)) 188 | # -Wno-disabled-macro-expansion -> Have to disable the macro expansion warning as the operator new overload warns on that. 189 | # -Wno-padded -> I sort-of like this warning but if there is a bool at the end of the class, it seems impossible to remove it! (except by making padding explicit) 190 | # -Wno-global-constructors Wno-exit-time-destructors -> Great warnings, but in CppUTest it is impossible to avoid as the automatic test registration depends on the global ctor and dtor 191 | # -Wno-weak-vtables -> The TEST_GROUP macro declares a class and will automatically inline its methods. Thats ok as they are only in one translation unit. Unfortunately, the warning can't detect that, so it must be disabled. 192 | CPPUTEST_CXX_WARNINGFLAGS += -Weverything -Wno-disabled-macro-expansion -Wno-padded -Wno-global-constructors -Wno-exit-time-destructors -Wno-weak-vtables 193 | CPPUTEST_C_WARNINGFLAGS += -Weverything -Wno-padded 194 | endif 195 | 196 | # Uhm. Maybe put some warning flags for SunStudio here? 197 | ifeq ($(COMPILER_NAME),$(SUNSTUDIO_CXX_STR)) 198 | CPPUTEST_CXX_WARNINGFLAGS = 199 | CPPUTEST_C_WARNINGFLAGS = 200 | endif 201 | 202 | # Default dir for temporary files (d, o) 203 | ifndef CPPUTEST_OBJS_DIR 204 | ifndef TARGET_PLATFORM 205 | CPPUTEST_OBJS_DIR = objs 206 | else 207 | CPPUTEST_OBJS_DIR = objs/$(TARGET_PLATFORM) 208 | endif 209 | endif 210 | 211 | # Default dir for the outout library 212 | ifndef CPPUTEST_LIB_DIR 213 | ifndef TARGET_PLATFORM 214 | CPPUTEST_LIB_DIR = lib 215 | else 216 | CPPUTEST_LIB_DIR = lib/$(TARGET_PLATFORM) 217 | endif 218 | endif 219 | 220 | # No map by default 221 | ifndef CPPUTEST_MAP_FILE 222 | CPPUTEST_MAP_FILE = N 223 | endif 224 | 225 | # No extentions is default 226 | ifndef CPPUTEST_USE_EXTENSIONS 227 | CPPUTEST_USE_EXTENSIONS = N 228 | endif 229 | 230 | # No VPATH is default 231 | ifndef CPPUTEST_USE_VPATH 232 | CPPUTEST_USE_VPATH := N 233 | endif 234 | # Make empty, instead of 'N', for usage in $(if ) conditionals 235 | ifneq ($(CPPUTEST_USE_VPATH), Y) 236 | CPPUTEST_USE_VPATH := 237 | endif 238 | 239 | ifndef TARGET_PLATFORM 240 | CPPUTEST_LIB_LINK_DIR = $(CPPUTEST_HOME)/lib 241 | #CPPUTEST_LIB_LINK_DIR = /usr/lib/x86_64-linux-gnu 242 | else 243 | CPPUTEST_LIB_LINK_DIR = $(CPPUTEST_HOME)/lib/$(TARGET_PLATFORM) 244 | endif 245 | 246 | # -------------------------------------- 247 | # derived flags in the following area 248 | # -------------------------------------- 249 | 250 | # Without the C library, we'll need to disable the C++ library and ... 251 | ifeq ($(CPPUTEST_USE_STD_C_LIB), N) 252 | CPPUTEST_USE_STD_CPP_LIB = N 253 | CPPUTEST_USE_MEM_LEAK_DETECTION = N 254 | CPPUTEST_CPPFLAGS += -DCPPUTEST_STD_C_LIB_DISABLED 255 | CPPUTEST_CPPFLAGS += -nostdinc 256 | endif 257 | 258 | CPPUTEST_CPPFLAGS += -DCPPUTEST_COMPILATION 259 | 260 | ifeq ($(CPPUTEST_USE_MEM_LEAK_DETECTION), N) 261 | CPPUTEST_CPPFLAGS += -DCPPUTEST_MEM_LEAK_DETECTION_DISABLED 262 | else 263 | ifndef CPPUTEST_MEMLEAK_DETECTOR_NEW_MACRO_FILE 264 | CPPUTEST_MEMLEAK_DETECTOR_NEW_MACRO_FILE = -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h 265 | endif 266 | ifndef CPPUTEST_MEMLEAK_DETECTOR_MALLOC_MACRO_FILE 267 | CPPUTEST_MEMLEAK_DETECTOR_MALLOC_MACRO_FILE = -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h 268 | endif 269 | endif 270 | 271 | ifeq ($(CPPUTEST_ENABLE_DEBUG), Y) 272 | CPPUTEST_CXXFLAGS += -g 273 | CPPUTEST_CFLAGS += -g 274 | CPPUTEST_LDFLAGS += -g 275 | endif 276 | 277 | ifeq ($(CPPUTEST_USE_STD_CPP_LIB), N) 278 | CPPUTEST_CPPFLAGS += -DCPPUTEST_STD_CPP_LIB_DISABLED 279 | ifeq ($(CPPUTEST_USE_STD_C_LIB), Y) 280 | CPPUTEST_CXXFLAGS += -nostdinc++ 281 | endif 282 | endif 283 | 284 | ifdef $(GMOCK_HOME) 285 | GTEST_HOME = $(GMOCK_HOME)/gtest 286 | CPPUTEST_CPPFLAGS += -I$(GMOCK_HOME)/include 287 | GMOCK_LIBRARY = $(GMOCK_HOME)/lib/.libs/libgmock.a 288 | LD_LIBRARIES += $(GMOCK_LIBRARY) 289 | CPPUTEST_CPPFLAGS += -DINCLUDE_GTEST_TESTS 290 | CPPUTEST_WARNINGFLAGS = 291 | CPPUTEST_CPPFLAGS += -I$(GTEST_HOME)/include -I$(GTEST_HOME) 292 | GTEST_LIBRARY = $(GTEST_HOME)/lib/.libs/libgtest.a 293 | LD_LIBRARIES += $(GTEST_LIBRARY) 294 | endif 295 | 296 | 297 | ifeq ($(CPPUTEST_USE_GCOV), Y) 298 | CPPUTEST_CXXFLAGS += -fprofile-arcs -ftest-coverage 299 | CPPUTEST_CFLAGS += -fprofile-arcs -ftest-coverage 300 | endif 301 | 302 | CPPUTEST_CXXFLAGS += $(CPPUTEST_WARNINGFLAGS) $(CPPUTEST_CXX_WARNINGFLAGS) 303 | CPPUTEST_CPPFLAGS += $(CPPUTEST_WARNINGFLAGS) 304 | CPPUTEST_CXXFLAGS += $(CPPUTEST_MEMLEAK_DETECTOR_NEW_MACRO_FILE) 305 | CPPUTEST_CPPFLAGS += $(CPPUTEST_MEMLEAK_DETECTOR_MALLOC_MACRO_FILE) 306 | CPPUTEST_CFLAGS += $(CPPUTEST_C_WARNINGFLAGS) 307 | 308 | TARGET_MAP = $(COMPONENT_NAME).map.txt 309 | ifeq ($(CPPUTEST_MAP_FILE), Y) 310 | CPPUTEST_LDFLAGS += -Wl,-map,$(TARGET_MAP) 311 | endif 312 | 313 | # Link with CppUTest lib 314 | CPPUTEST_LIB = $(CPPUTEST_LIB_LINK_DIR)/libCppUTest.a 315 | 316 | ifeq ($(CPPUTEST_USE_EXTENSIONS), Y) 317 | CPPUTEST_LIB += $(CPPUTEST_LIB_LINK_DIR)/libCppUTestExt.a 318 | endif 319 | 320 | ifdef CPPUTEST_STATIC_REALTIME 321 | LD_LIBRARIES += -lrt 322 | endif 323 | 324 | TARGET_LIB = \ 325 | $(CPPUTEST_LIB_DIR)/lib$(COMPONENT_NAME).a 326 | 327 | ifndef TEST_TARGET 328 | ifndef TARGET_PLATFORM 329 | TEST_TARGET = $(COMPONENT_NAME)_tests 330 | else 331 | TEST_TARGET = $(COMPONENT_NAME)_$(TARGET_PLATFORM)_tests 332 | endif 333 | endif 334 | 335 | #Helper Functions 336 | get_src_from_dir = $(wildcard $1/*.cpp) $(wildcard $1/*.cc) $(wildcard $1/*.c) 337 | get_dirs_from_dirspec = $(wildcard $1) 338 | get_src_from_dir_list = $(foreach dir, $1, $(call get_src_from_dir,$(dir))) 339 | __src_to = $(subst .c,$1, $(subst .cc,$1, $(subst .cpp,$1,$(if $(CPPUTEST_USE_VPATH),$(notdir $2),$2)))) 340 | src_to = $(addprefix $(CPPUTEST_OBJS_DIR)/,$(call __src_to,$1,$2)) 341 | src_to_o = $(call src_to,.o,$1) 342 | src_to_d = $(call src_to,.d,$1) 343 | src_to_gcda = $(call src_to,.gcda,$1) 344 | src_to_gcno = $(call src_to,.gcno,$1) 345 | time = $(shell date +%s) 346 | delta_t = $(eval minus, $1, $2) 347 | debug_print_list = $(foreach word,$1,echo " $(word)";) echo; 348 | 349 | #Derived 350 | STUFF_TO_CLEAN += $(TEST_TARGET) $(TEST_TARGET).exe $(TARGET_LIB) $(TARGET_MAP) 351 | 352 | SRC += $(call get_src_from_dir_list, $(SRC_DIRS)) $(SRC_FILES) 353 | OBJ = $(call src_to_o,$(SRC)) 354 | 355 | STUFF_TO_CLEAN += $(OBJ) 356 | 357 | TEST_SRC += $(call get_src_from_dir_list, $(TEST_SRC_DIRS)) $(TEST_SRC_FILES) 358 | TEST_OBJS = $(call src_to_o,$(TEST_SRC)) 359 | STUFF_TO_CLEAN += $(TEST_OBJS) 360 | 361 | 362 | MOCKS_SRC += $(call get_src_from_dir_list, $(MOCKS_SRC_DIRS)) 363 | MOCKS_OBJS = $(call src_to_o,$(MOCKS_SRC)) 364 | STUFF_TO_CLEAN += $(MOCKS_OBJS) 365 | 366 | ALL_SRC = $(SRC) $(TEST_SRC) $(MOCKS_SRC) 367 | 368 | # If we're using VPATH 369 | ifeq ($(CPPUTEST_USE_VPATH), Y) 370 | # gather all the source directories and add them 371 | VPATH += $(sort $(dir $(ALL_SRC))) 372 | # Add the component name to the objs dir path, to differentiate between same-name objects 373 | CPPUTEST_OBJS_DIR := $(addsuffix /$(COMPONENT_NAME),$(CPPUTEST_OBJS_DIR)) 374 | endif 375 | 376 | #Test coverage with gcov 377 | GCOV_OUTPUT = gcov_output.txt 378 | GCOV_REPORT = gcov_report.txt 379 | GCOV_ERROR = gcov_error.txt 380 | GCOV_GCDA_FILES = $(call src_to_gcda, $(ALL_SRC)) 381 | GCOV_GCNO_FILES = $(call src_to_gcno, $(ALL_SRC)) 382 | TEST_OUTPUT = $(TEST_TARGET).txt 383 | STUFF_TO_CLEAN += \ 384 | $(GCOV_OUTPUT)\ 385 | $(GCOV_REPORT)\ 386 | $(GCOV_REPORT).html\ 387 | $(GCOV_ERROR)\ 388 | $(GCOV_GCDA_FILES)\ 389 | $(GCOV_GCNO_FILES)\ 390 | $(TEST_OUTPUT) 391 | 392 | #The gcda files for gcov need to be deleted before each run 393 | #To avoid annoying messages. 394 | GCOV_CLEAN = $(SILENCE)rm -f $(GCOV_GCDA_FILES) $(GCOV_OUTPUT) $(GCOV_REPORT) $(GCOV_ERROR) 395 | RUN_TEST_TARGET = $(SILENCE) $(GCOV_CLEAN) ; echo "Running $(TEST_TARGET)"; valgrind --track-origins=yes --leak-check=full --xml=yes --xml-file="valgrind_$(TEST_TARGET).xml" ./$(TEST_TARGET) $(CPPUTEST_EXE_FLAGS) -ojunit 396 | 397 | 398 | ifeq ($(CPPUTEST_USE_GCOV), Y) 399 | 400 | ifeq ($(COMPILER_NAME),$(CLANG_STR)) 401 | LD_LIBRARIES += --coverage 402 | else 403 | LD_LIBRARIES += -lgcov 404 | endif 405 | endif 406 | 407 | 408 | INCLUDES_DIRS_EXPANDED = $(call get_dirs_from_dirspec, $(INCLUDE_DIRS)) 409 | INCLUDES += $(foreach dir, $(INCLUDES_DIRS_EXPANDED), -I$(dir)) 410 | MOCK_DIRS_EXPANDED = $(call get_dirs_from_dirspec, $(MOCKS_SRC_DIRS)) 411 | INCLUDES += $(foreach dir, $(MOCK_DIRS_EXPANDED), -I$(dir)) 412 | 413 | CPPUTEST_CPPFLAGS += $(INCLUDES) $(CPPUTESTFLAGS) 414 | 415 | DEP_FILES = $(call src_to_d, $(ALL_SRC)) 416 | STUFF_TO_CLEAN += $(DEP_FILES) $(PRODUCTION_CODE_START) $(PRODUCTION_CODE_END) 417 | STUFF_TO_CLEAN += $(STDLIB_CODE_START) $(MAP_FILE) cpputest_*.xml junit_run_output 418 | 419 | # We'll use the CPPUTEST_CFLAGS etc so that you can override AND add to the CppUTest flags 420 | CFLAGS = $(CPPUTEST_CFLAGS) $(CPPUTEST_ADDITIONAL_CFLAGS) 421 | CPPFLAGS = $(CPPUTEST_CPPFLAGS) $(CPPUTEST_ADDITIONAL_CPPFLAGS) 422 | CXXFLAGS = $(CPPUTEST_CXXFLAGS) $(CPPUTEST_ADDITIONAL_CXXFLAGS) 423 | LDFLAGS = $(CPPUTEST_LDFLAGS) $(CPPUTEST_ADDITIONAL_LDFLAGS) 424 | 425 | # Don't consider creating the archive a warning condition that does STDERR output 426 | ARFLAGS := $(ARFLAGS)c 427 | 428 | DEP_FLAGS=-MMD -MP 429 | 430 | # Some macros for programs to be overridden. For some reason, these are not in Make defaults 431 | RANLIB = ranlib 432 | 433 | # Targets 434 | 435 | .PHONY: all 436 | all: start $(TEST_TARGET) 437 | $(RUN_TEST_TARGET) 438 | 439 | .PHONY: start 440 | start: $(TEST_TARGET) 441 | $(SILENCE)START_TIME=$(call time) 442 | 443 | .PHONY: all_no_tests 444 | all_no_tests: $(TEST_TARGET) 445 | 446 | .PHONY: flags 447 | flags: 448 | @echo 449 | @echo "OS ${UNAME_OS}" 450 | @echo "Compile C and C++ source with CPPFLAGS:" 451 | @$(call debug_print_list,$(CPPFLAGS)) 452 | @echo "Compile C++ source with CXXFLAGS:" 453 | @$(call debug_print_list,$(CXXFLAGS)) 454 | @echo "Compile C source with CFLAGS:" 455 | @$(call debug_print_list,$(CFLAGS)) 456 | @echo "Link with LDFLAGS:" 457 | @$(call debug_print_list,$(LDFLAGS)) 458 | @echo "Link with LD_LIBRARIES:" 459 | @$(call debug_print_list,$(LD_LIBRARIES)) 460 | @echo "Create libraries with ARFLAGS:" 461 | @$(call debug_print_list,$(ARFLAGS)) 462 | 463 | TEST_DEPS = $(TEST_OBJS) $(MOCKS_OBJS) $(PRODUCTION_CODE_START) $(TARGET_LIB) $(USER_LIBS) $(PRODUCTION_CODE_END) $(CPPUTEST_LIB) $(STDLIB_CODE_START) 464 | test-deps: $(TEST_DEPS) 465 | 466 | $(TEST_TARGET): $(TEST_DEPS) 467 | @echo Linking $@ 468 | $(SILENCE)$(CXX) -o $@ $^ $(LD_LIBRARIES) $(LDFLAGS) 469 | 470 | $(TARGET_LIB): $(OBJ) 471 | @echo Building archive $@ 472 | $(SILENCE)mkdir -p $(dir $@) 473 | $(SILENCE)$(AR) $(ARFLAGS) $@ $^ 474 | $(SILENCE)$(RANLIB) $@ 475 | 476 | test: $(TEST_TARGET) 477 | $(RUN_TEST_TARGET) | tee $(TEST_OUTPUT) 478 | 479 | vtest: $(TEST_TARGET) 480 | $(RUN_TEST_TARGET) -v | tee $(TEST_OUTPUT) 481 | 482 | $(CPPUTEST_OBJS_DIR)/%.o: %.cc 483 | @echo compiling $(notdir $<) 484 | $(SILENCE)mkdir -p $(dir $@) 485 | $(SILENCE)$(COMPILE.cpp) $(DEP_FLAGS) $(OUTPUT_OPTION) $< 486 | 487 | $(CPPUTEST_OBJS_DIR)/%.o: %.cpp 488 | @echo compiling $(notdir $<) 489 | $(SILENCE)mkdir -p $(dir $@) 490 | $(SILENCE)$(COMPILE.cpp) $(DEP_FLAGS) $(OUTPUT_OPTION) $< 491 | 492 | $(CPPUTEST_OBJS_DIR)/%.o: %.c 493 | @echo compiling $(notdir $<) 494 | $(SILENCE)mkdir -p $(dir $@) 495 | $(SILENCE)$(COMPILE.c) $(DEP_FLAGS) $(OUTPUT_OPTION) $< 496 | 497 | ifneq "$(MAKECMDGOALS)" "clean" 498 | -include $(DEP_FILES) 499 | endif 500 | 501 | .PHONY: clean 502 | clean: 503 | @echo Making clean 504 | $(SILENCE)$(RM) $(STUFF_TO_CLEAN) 505 | $(SILENCE)rm -rf gcov objs #$(CPPUTEST_OBJS_DIR) 506 | $(SILENCE)rm -rf $(CPPUTEST_LIB_DIR) 507 | $(SILENCE)find . -name "*.gcno" | xargs rm -f 508 | $(SILENCE)find . -name "*.gcda" | xargs rm -f 509 | $(SILENCE)find . -name "valgrind_*.xml" | xargs rm -f 510 | 511 | #realclean gets rid of all gcov, o and d files in the directory tree 512 | #not just the ones made by this makefile 513 | .PHONY: realclean 514 | realclean: clean 515 | $(SILENCE)rm -rf gcov 516 | $(SILENCE)find . -name "*.gdcno" | xargs rm -f 517 | $(SILENCE)find . -name "*.[do]" | xargs rm -f 518 | 519 | gcov: test 520 | ifeq ($(CPPUTEST_USE_VPATH), Y) 521 | $(SILENCE)gcov --object-directory $(CPPUTEST_OBJS_DIR) $(SRC) >> $(GCOV_OUTPUT) 2>> $(GCOV_ERROR) 522 | else 523 | $(SILENCE)for d in $(SRC_DIRS) ; do \ 524 | gcov --object-directory $(CPPUTEST_OBJS_DIR)/$$d $$d/*.c $$d/*.cpp >> $(GCOV_OUTPUT) 2>>$(GCOV_ERROR) ; \ 525 | done 526 | $(SILENCE)for f in $(SRC_FILES) ; do \ 527 | gcov --object-directory $(CPPUTEST_OBJS_DIR)/$$f $$f >> $(GCOV_OUTPUT) 2>>$(GCOV_ERROR) ; \ 528 | done 529 | endif 530 | $(CPPUTEST_HOME)/scripts/filterGcov.sh $(GCOV_OUTPUT) $(GCOV_ERROR) $(GCOV_REPORT) $(TEST_OUTPUT) 531 | # /usr/share/cpputest/scripts/filterGcov.sh $(GCOV_OUTPUT) $(GCOV_ERROR) $(GCOV_REPORT) $(TEST_OUTPUT) 532 | $(SILENCE)cat $(GCOV_REPORT) 533 | $(SILENCE)mkdir -p gcov 534 | $(SILENCE)mv *.gcov gcov 535 | $(SILENCE)mv gcov_* gcov 536 | @echo "See gcov directory for details" 537 | 538 | .PHONEY: format 539 | format: 540 | $(CPPUTEST_HOME)/scripts/reformat.sh $(PROJECT_HOME_DIR) 541 | 542 | .PHONEY: debug 543 | debug: 544 | @echo 545 | @echo "Target Source files:" 546 | @$(call debug_print_list,$(SRC)) 547 | @echo "Target Object files:" 548 | @$(call debug_print_list,$(OBJ)) 549 | @echo "Test Source files:" 550 | @$(call debug_print_list,$(TEST_SRC)) 551 | @echo "Test Object files:" 552 | @$(call debug_print_list,$(TEST_OBJS)) 553 | @echo "Mock Source files:" 554 | @$(call debug_print_list,$(MOCKS_SRC)) 555 | @echo "Mock Object files:" 556 | @$(call debug_print_list,$(MOCKS_OBJS)) 557 | @echo "All Input Dependency files:" 558 | @$(call debug_print_list,$(DEP_FILES)) 559 | @echo Stuff to clean: 560 | @$(call debug_print_list,$(STUFF_TO_CLEAN)) 561 | @echo Includes: 562 | @$(call debug_print_list,$(INCLUDES)) 563 | 564 | -include $(OTHER_MAKEFILE_TO_INCLUDE) 565 | -------------------------------------------------------------------------------- /test/mbed-coap/unittest/sn_coap_builder/libCoap_builder_test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 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 "CppUTest/TestHarness.h" 17 | #include 18 | #include 19 | #include "sn_coap_header.h" 20 | #include "sn_coap_protocol_internal.h" 21 | 22 | #include "sn_coap_header_check_stub.h" 23 | #include "sn_coap_parser_stub.h" 24 | 25 | sn_coap_hdr_s coap_header; 26 | sn_coap_options_list_s option_list; 27 | uint8_t buffer[356]; 28 | uint8_t temp[10]; 29 | 30 | uint8_t retCounter = 0; 31 | 32 | static void *own_alloc(uint16_t size) 33 | { 34 | if( retCounter > 0 ){ 35 | retCounter--; 36 | return malloc(size); 37 | } 38 | return NULL; 39 | } 40 | 41 | static void own_free(void *ptr) 42 | { 43 | free(ptr); 44 | } 45 | 46 | TEST_GROUP(libCoap_builder) 47 | { 48 | void setup() { 49 | sn_coap_header_check_stub.expectedInt8 = 0; 50 | retCounter = 0; 51 | memset(&coap_header, 0, sizeof(sn_coap_hdr_s)); 52 | memset(&option_list, 0, sizeof(sn_coap_options_list_s)); 53 | 54 | coap_header.options_list_ptr = &option_list; 55 | coap_header.msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT; 56 | coap_header.msg_code = COAP_MSG_CODE_RESPONSE_CREATED; 57 | coap_header.msg_id = 12; 58 | } 59 | 60 | void teardown() { 61 | 62 | } 63 | }; 64 | 65 | TEST(libCoap_builder, build_confirmable_response) 66 | { 67 | struct coap_s handle; 68 | sn_coap_hdr_s *response = NULL; 69 | 70 | handle.sn_coap_protocol_malloc = &own_alloc; 71 | handle.sn_coap_protocol_free = &own_free; 72 | 73 | coap_header.msg_type = COAP_MSG_TYPE_RESET; 74 | coap_header.msg_code = COAP_MSG_CODE_REQUEST_GET; 75 | coap_header.msg_id = 12; 76 | 77 | //NULL pointer 78 | CHECK(sn_coap_build_response(NULL, NULL, 0) == NULL); 79 | CHECK(sn_coap_build_response(&handle, NULL, 0) == NULL); 80 | CHECK(sn_coap_build_response(NULL, &coap_header, 0) == NULL); 81 | CHECK(sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT) == NULL); 82 | 83 | retCounter = 1; 84 | CHECK(sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT) == NULL); 85 | 86 | coap_header.msg_type = COAP_MSG_TYPE_CONFIRMABLE; 87 | 88 | retCounter = 2; 89 | response = sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT); 90 | 91 | CHECK(response != NULL); 92 | CHECK(response->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT); 93 | CHECK(response->msg_id == 12); 94 | CHECK(response->msg_code == COAP_MSG_CODE_RESPONSE_CONTENT); 95 | 96 | own_free(response); 97 | } 98 | 99 | TEST(libCoap_builder, build_non_confirmable_response) 100 | { 101 | struct coap_s handle; 102 | sn_coap_hdr_s *response = NULL; 103 | uint8_t token_val = 0x99; 104 | 105 | handle.sn_coap_protocol_malloc = &own_alloc; 106 | handle.sn_coap_protocol_free = &own_free; 107 | 108 | coap_header.msg_type = COAP_MSG_TYPE_NON_CONFIRMABLE; 109 | coap_header.msg_code = COAP_MSG_CODE_REQUEST_GET; 110 | coap_header.msg_id = 12; 111 | coap_header.token_len = 1; 112 | coap_header.token_ptr = &token_val; 113 | 114 | retCounter = 1; 115 | CHECK( NULL == sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT)); 116 | 117 | retCounter = 2; 118 | response = sn_coap_build_response(&handle, &coap_header, COAP_MSG_CODE_RESPONSE_CONTENT); 119 | 120 | CHECK(response != NULL); 121 | CHECK(response->msg_type == COAP_MSG_TYPE_NON_CONFIRMABLE); 122 | CHECK(response->msg_code == COAP_MSG_CODE_RESPONSE_CONTENT); 123 | CHECK(response->token_ptr != NULL); 124 | CHECK(memcmp(response->token_ptr, coap_header.token_ptr, coap_header.token_len) == 0); 125 | CHECK(response->token_len == coap_header.token_len); 126 | 127 | own_free(response->token_ptr); 128 | response->token_ptr = NULL; 129 | own_free(response); 130 | response = NULL; 131 | } 132 | 133 | TEST(libCoap_builder, build_message_negative_cases) 134 | { 135 | // Null pointers as a parameter 136 | CHECK(sn_coap_builder(NULL, NULL) == -2); 137 | CHECK(sn_coap_builder(NULL, &coap_header) == -2); 138 | CHECK(sn_coap_builder(buffer, NULL) == -2); 139 | 140 | // Invalid option length 141 | coap_header.token_ptr = temp; 142 | CHECK(sn_coap_builder(buffer, &coap_header) == -1); 143 | } 144 | 145 | TEST(libCoap_builder, build_message_ok_cases) 146 | { 147 | CHECK(sn_coap_builder(buffer, &coap_header) == 11); 148 | } 149 | 150 | TEST(libCoap_builder, build_message_options_token) 151 | { 152 | coap_header.token_ptr = temp; 153 | coap_header.token_len = 2; 154 | CHECK(sn_coap_builder(buffer, &coap_header) == 13); 155 | } 156 | 157 | TEST(libCoap_builder, build_message_options_uri_path) 158 | { 159 | coap_header.uri_path_ptr = temp; 160 | coap_header.uri_path_len = 2; 161 | CHECK(sn_coap_builder(buffer, &coap_header) == 14); 162 | } 163 | 164 | TEST(libCoap_builder, build_message_options_content_type) 165 | { 166 | coap_header.content_format = COAP_CT_TEXT_PLAIN; 167 | CHECK(sn_coap_builder(buffer, &coap_header) == 11); 168 | } 169 | 170 | TEST(libCoap_builder, build_message_options_max_age) 171 | { 172 | coap_header.options_list_ptr->max_age = 1; 173 | CHECK(sn_coap_builder(buffer, &coap_header) == 12); 174 | } 175 | 176 | TEST(libCoap_builder, build_message_options_proxy_uri) 177 | { 178 | coap_header.options_list_ptr->proxy_uri_ptr = temp; 179 | coap_header.options_list_ptr->proxy_uri_len = 2; 180 | CHECK(sn_coap_builder(buffer, &coap_header) == 14); 181 | } 182 | 183 | TEST(libCoap_builder, build_message_options_etag) 184 | { 185 | coap_header.options_list_ptr->etag_ptr = temp; 186 | coap_header.options_list_ptr->etag_len = 2; 187 | CHECK(sn_coap_builder(buffer, &coap_header) == 14); 188 | } 189 | 190 | TEST(libCoap_builder, build_message_options_uri_host) 191 | { 192 | coap_header.options_list_ptr->uri_host_ptr = temp; 193 | coap_header.options_list_ptr->uri_host_len = 2; 194 | CHECK(sn_coap_builder(buffer, &coap_header) == 14); 195 | } 196 | 197 | TEST(libCoap_builder, build_message_options_location_path) 198 | { 199 | coap_header.options_list_ptr->location_path_ptr = temp; 200 | coap_header.options_list_ptr->location_path_len = 2; 201 | CHECK(sn_coap_builder(buffer, &coap_header) == 14); 202 | } 203 | 204 | TEST(libCoap_builder, build_message_options_uri_port) 205 | { 206 | coap_header.options_list_ptr->uri_port = 2; 207 | CHECK(sn_coap_builder(buffer, &coap_header) == 12); 208 | } 209 | 210 | 211 | TEST(libCoap_builder, build_message_options_location_query) 212 | { 213 | coap_header.options_list_ptr->location_query_ptr = temp; 214 | coap_header.options_list_ptr->location_query_len = 2; 215 | CHECK(sn_coap_builder(buffer, &coap_header) == 14); 216 | } 217 | 218 | TEST(libCoap_builder, build_message_options_observe) 219 | { 220 | coap_header.options_list_ptr->observe = 0; 221 | CHECK(sn_coap_builder(buffer, &coap_header) == 11); 222 | } 223 | 224 | 225 | TEST(libCoap_builder, build_message_options_accept) 226 | { 227 | coap_header.options_list_ptr->accept = COAP_CT_TEXT_PLAIN; 228 | CHECK(sn_coap_builder(buffer, &coap_header) == 11); 229 | } 230 | 231 | TEST(libCoap_builder, build_message_options_uri_query) 232 | { 233 | coap_header.options_list_ptr->uri_query_ptr = temp; 234 | temp[0] = '1'; 235 | temp[1] = '&'; 236 | temp[2] = '2'; 237 | temp[3] = '&'; 238 | temp[4] = '3'; 239 | temp[5] = '\0'; 240 | coap_header.options_list_ptr->uri_query_len = 6; 241 | uint8_t val = sn_coap_builder(buffer, &coap_header); 242 | CHECK( val == 18); 243 | memset(&temp, 0, 10); 244 | } 245 | 246 | 247 | TEST(libCoap_builder, build_message_options_block1) 248 | { 249 | coap_header.options_list_ptr->block1 = 267; 250 | CHECK(sn_coap_builder(buffer, &coap_header) == 13); 251 | } 252 | 253 | TEST(libCoap_builder, build_message_options_block2) 254 | { 255 | coap_header.options_list_ptr->block2 = 267; 256 | 257 | sn_coap_header_check_stub.expectedInt8 = 44; 258 | CHECK(sn_coap_builder(buffer, &coap_header) == -1); 259 | 260 | sn_coap_header_check_stub.expectedInt8 = 0; 261 | CHECK(sn_coap_builder(buffer, &coap_header) == 13); 262 | 263 | coap_header.options_list_ptr = NULL; //return from sn_coap_builder_options_build immediately 264 | sn_coap_header_check_stub.expectedInt8 = 0; 265 | CHECK( 5 == sn_coap_builder(buffer, &coap_header) ); 266 | } 267 | 268 | TEST(libCoap_builder, sn_coap_builder_calc_needed_packet_data_size) 269 | { 270 | CHECK(sn_coap_builder_calc_needed_packet_data_size(NULL) == 0); 271 | 272 | sn_coap_hdr_s header; 273 | memset(&header, 0, sizeof(sn_coap_hdr_s)); 274 | header.msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT; 275 | header.token_ptr = (uint8_t*)malloc(10); 276 | header.token_len = 10; 277 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 278 | free(header.token_ptr); 279 | header.token_ptr = (uint8_t*)malloc(6); 280 | header.token_len = 6; 281 | 282 | //Test variations of sn_coap_builder_options_calc_option_size here --> 283 | header.uri_path_ptr = (uint8_t*)malloc(290); 284 | memset(header.uri_path_ptr, '1', 290); 285 | header.uri_path_len = 290; 286 | header.uri_path_ptr[5] = '/'; 287 | header.uri_path_ptr[285] = '/'; 288 | 289 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 290 | 291 | header.uri_path_ptr[285] = '1'; 292 | header.uri_path_ptr[170] = '/'; 293 | 294 | header.content_format = sn_coap_content_format_e(0xFFFF22); 295 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 296 | 297 | header.content_format = COAP_CT_TEXT_PLAIN; 298 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 304); 299 | 300 | sn_coap_options_list_s opt_list; 301 | memset(&opt_list, 0, sizeof(sn_coap_options_list_s)); 302 | header.options_list_ptr = &opt_list; 303 | 304 | header.options_list_ptr->accept = sn_coap_content_format_e(0xFFFF22); 305 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 306 | 307 | header.options_list_ptr->observe = COAP_OBSERVE_NONE; 308 | header.options_list_ptr->uri_port = COAP_OPTION_URI_PORT_NONE; 309 | free(header.uri_path_ptr); 310 | header.uri_path_ptr = NULL; 311 | header.content_format = COAP_CT_NONE; 312 | header.options_list_ptr->max_age = COAP_OPTION_MAX_AGE_DEFAULT; 313 | header.options_list_ptr->accept = COAP_CT_TEXT_PLAIN; 314 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 14); 315 | 316 | header.options_list_ptr->max_age = 6; 317 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 16); 318 | 319 | //proxy uri tests (4) 320 | header.options_list_ptr->proxy_uri_ptr = (uint8_t*)malloc(1800); 321 | header.options_list_ptr->proxy_uri_len = 1800; 322 | header.options_list_ptr->max_age = COAP_OPTION_MAX_AGE_DEFAULT; 323 | header.options_list_ptr->accept = COAP_CT_NONE; 324 | 325 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 326 | header.options_list_ptr->proxy_uri_len = 6; 327 | header.options_list_ptr->etag_ptr = (uint8_t*)malloc(4); 328 | header.options_list_ptr->etag_len = 0; 329 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 330 | 331 | header.options_list_ptr->proxy_uri_len = 14; 332 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 333 | 334 | // init now the buffer up to 4 bytes, as it will be accessed 335 | memset(header.options_list_ptr->etag_ptr, 0, 4); 336 | 337 | header.options_list_ptr->proxy_uri_len = 281; 338 | header.options_list_ptr->block1 = COAP_OPTION_BLOCK_NONE; 339 | header.options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE; 340 | header.options_list_ptr->etag_len = 4; 341 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 300); 342 | 343 | header.options_list_ptr->uri_host_ptr = (uint8_t*)malloc(6); 344 | header.options_list_ptr->uri_host_len = 0; 345 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 346 | 347 | header.options_list_ptr->uri_host_len = 4; 348 | header.options_list_ptr->location_path_ptr = (uint8_t*)calloc(270, 1); 349 | header.options_list_ptr->location_path_len = 270; 350 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 351 | 352 | header.options_list_ptr->uri_host_len = 44; 353 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 354 | 355 | header.options_list_ptr->location_path_len = 27; 356 | header.options_list_ptr->uri_port = 0xffff22; 357 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 358 | 359 | header.options_list_ptr->uri_port = 6; 360 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 377); 361 | 362 | header.options_list_ptr->location_query_ptr = (uint8_t*)calloc(277, 1); 363 | header.options_list_ptr->location_query_len = 277; 364 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 365 | 366 | header.options_list_ptr->location_query_len = 27; 367 | header.options_list_ptr->observe = 0; 368 | free(header.options_list_ptr->location_path_ptr); 369 | header.options_list_ptr->location_path_ptr = NULL; 370 | header.options_list_ptr->location_path_len = 0; 371 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 379); 372 | 373 | header.options_list_ptr->uri_query_ptr = (uint8_t*)malloc(6); 374 | header.options_list_ptr->uri_query_len = 0; 375 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 376 | 377 | // init the 4 bytes to something useful, leave rest uninitialized to let valgrind warn if builder is processing past buffer 378 | memset(header.options_list_ptr->uri_query_ptr, 0, 4); 379 | header.options_list_ptr->uri_query_len = 4; 380 | header.options_list_ptr->block2 = -1; 381 | header.options_list_ptr->observe = 0xFFFFFF22; 382 | header.options_list_ptr->uri_port = COAP_OPTION_URI_PORT_NONE; 383 | free(header.options_list_ptr->etag_ptr); 384 | header.options_list_ptr->etag_ptr = NULL; 385 | header.options_list_ptr->etag_len = 0; 386 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 387 | 388 | header.options_list_ptr->observe = COAP_OBSERVE_NONE; 389 | free(header.options_list_ptr->uri_host_ptr); 390 | header.options_list_ptr->uri_host_ptr = NULL; 391 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 330); 392 | 393 | header.options_list_ptr->observe = 1; 394 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 331); 395 | 396 | header.options_list_ptr->block2 = 0xFFFFFF22; 397 | header.options_list_ptr->block1 = -1; 398 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0); 399 | 400 | header.options_list_ptr->block2 = 267; 401 | free(header.options_list_ptr->location_query_ptr); 402 | header.options_list_ptr->location_query_ptr = NULL; 403 | free(header.options_list_ptr->uri_query_ptr); 404 | header.options_list_ptr->uri_query_ptr = NULL; 405 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 300); 406 | 407 | header.options_list_ptr->block1 = 0xFFFFFF22; 408 | header.payload_len = 1; 409 | CHECK(0 == sn_coap_builder_calc_needed_packet_data_size(&header)); 410 | 411 | header.options_list_ptr->block1 = 267; 412 | header.payload_len = 1; 413 | header.options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE; 414 | CHECK(302 == sn_coap_builder_calc_needed_packet_data_size(&header)); 415 | 416 | header.options_list_ptr->block1 = COAP_OPTION_BLOCK_NONE; 417 | header.options_list_ptr->size1 = 266; 418 | header.options_list_ptr->use_size1 = true; 419 | 420 | CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 303); 421 | 422 | header.options_list_ptr->size2 = 266; 423 | header.options_list_ptr->use_size2 = true; 424 | header.payload_len = 1; 425 | CHECK(306 == sn_coap_builder_calc_needed_packet_data_size(&header)); 426 | 427 | header.options_list_ptr->use_size1 = false; 428 | header.options_list_ptr->use_size2 = false; 429 | // <-- 430 | free(header.options_list_ptr->location_query_ptr); 431 | free(header.options_list_ptr->location_path_ptr); 432 | free(header.options_list_ptr->uri_host_ptr); 433 | free(header.options_list_ptr->etag_ptr); 434 | free(header.options_list_ptr->proxy_uri_ptr); 435 | header.options_list_ptr->location_query_ptr = NULL; 436 | header.options_list_ptr->location_path_ptr = NULL; 437 | header.options_list_ptr->uri_host_ptr = NULL; 438 | header.options_list_ptr->etag_ptr = NULL; 439 | header.options_list_ptr->proxy_uri_ptr = NULL; 440 | CHECK(14 == sn_coap_builder_calc_needed_packet_data_size(&header)); 441 | free(header.options_list_ptr->uri_query_ptr); 442 | 443 | //Test sn_coap_builder_options_calculate_jump_need "else" case 444 | header.options_list_ptr = NULL; 445 | CHECK( 12 == sn_coap_builder_calc_needed_packet_data_size(&header) ); 446 | 447 | //Test detecting sn_coap_builder_calc_needed_packet_data_size return value wont overflow 448 | header.payload_len = UINT16_MAX; 449 | CHECK( 0 == sn_coap_builder_calc_needed_packet_data_size(&header) ); 450 | 451 | 452 | free(header.uri_path_ptr); 453 | free(header.token_ptr); 454 | } 455 | 456 | 457 | TEST(libCoap_builder, sn_coap_builder_options_build_add_one_option) 458 | { 459 | coap_header.options_list_ptr->proxy_uri_ptr = (uint8_t*)malloc(280); 460 | memset(coap_header.options_list_ptr->proxy_uri_ptr, '1', 280); 461 | coap_header.options_list_ptr->proxy_uri_len = 2; 462 | sn_coap_header_check_stub.expectedInt8 = 0; 463 | CHECK(sn_coap_builder(buffer, &coap_header) == 14); 464 | 465 | coap_header.options_list_ptr->proxy_uri_len = 27; 466 | sn_coap_header_check_stub.expectedInt8 = 0; 467 | CHECK(40 == sn_coap_builder(buffer, &coap_header)); 468 | 469 | coap_header.options_list_ptr->proxy_uri_len = 277; 470 | sn_coap_header_check_stub.expectedInt8 = 0; 471 | CHECK(291 == sn_coap_builder(buffer, &coap_header)); 472 | 473 | free(coap_header.options_list_ptr->proxy_uri_ptr); 474 | coap_header.options_list_ptr->proxy_uri_ptr = NULL; 475 | coap_header.options_list_ptr->proxy_uri_len = 0; 476 | } 477 | 478 | TEST(libCoap_builder, sn_coap_builder_options_build_add_zero_length_option) 479 | { 480 | coap_header.options_list_ptr->proxy_uri_ptr = (uint8_t*)malloc(280); 481 | memset(coap_header.options_list_ptr->proxy_uri_ptr, '1', 280); 482 | coap_header.options_list_ptr->proxy_uri_len = 2; 483 | sn_coap_header_check_stub.expectedInt8 = 0; 484 | coap_header.options_list_ptr->observe = 1; 485 | int16_t val = sn_coap_builder(buffer, &coap_header); 486 | CHECK(val == 15); 487 | 488 | free(coap_header.options_list_ptr->proxy_uri_ptr); 489 | } 490 | 491 | TEST(libCoap_builder, sn_coap_builder_options_get_option_part_position) 492 | { 493 | sn_coap_hdr_s header; 494 | memset(&header, 0, sizeof(sn_coap_hdr_s)); 495 | sn_coap_options_list_s opt_list; 496 | memset(&opt_list, 0, sizeof(sn_coap_options_list_s)); 497 | header.options_list_ptr = &opt_list; 498 | header.options_list_ptr->accept = COAP_CT_TEXT_PLAIN; 499 | uint16_t val = sn_coap_builder(buffer, &header); 500 | CHECK(val == 11); 501 | 502 | header.options_list_ptr->accept = COAP_CT_TEXT_PLAIN; 503 | val = sn_coap_builder(buffer, &header); 504 | CHECK(val == 11); 505 | } 506 | 507 | TEST(libCoap_builder, sn_coap_builder_payload_build) 508 | { 509 | sn_coap_hdr_s header; 510 | memset(&header, 0, sizeof(sn_coap_hdr_s)); 511 | header.payload_ptr = (uint8_t*)malloc(5); 512 | header.payload_len = 5; 513 | sn_coap_options_list_s opt_list; 514 | memset(&opt_list, 0, sizeof(sn_coap_options_list_s)); 515 | header.options_list_ptr = &opt_list; 516 | header.options_list_ptr->accept = COAP_CT_TEXT_PLAIN; 517 | uint16_t val = sn_coap_builder(buffer, &header); 518 | CHECK(val == 17); 519 | 520 | header.content_format = COAP_CT_NONE; 521 | header.options_list_ptr->uri_port = -1; 522 | header.options_list_ptr->observe = COAP_OBSERVE_NONE; 523 | header.options_list_ptr->accept = COAP_CT_NONE; 524 | header.options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE; 525 | header.options_list_ptr->block1 = 13; 526 | header.options_list_ptr->use_size1 = true; 527 | header.options_list_ptr->use_size2 = true; 528 | header.options_list_ptr->max_age = COAP_OPTION_MAX_AGE_DEFAULT; 529 | 530 | val = sn_coap_builder(buffer, &header); 531 | CHECK(val == 16); 532 | 533 | free(header.payload_ptr); 534 | } 535 | --------------------------------------------------------------------------------