From bdc10744fb338ae197692713a0b48a7ccc36f566 Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 26 Apr 2020 10:25:59 +0200 Subject: Add Nimble in libs directory --- .../mynewt-nimble/porting/npl/linux/test/Makefile | 120 ++++++++++++++++ .../porting/npl/linux/test/test_npl_callout.c | 116 +++++++++++++++ .../porting/npl/linux/test/test_npl_eventq.c | 131 +++++++++++++++++ .../porting/npl/linux/test/test_npl_mempool.c | 111 +++++++++++++++ .../porting/npl/linux/test/test_npl_sem.c | 155 +++++++++++++++++++++ .../porting/npl/linux/test/test_npl_task.c | 98 +++++++++++++ .../porting/npl/linux/test/test_util.h | 56 ++++++++ 7 files changed, 787 insertions(+) create mode 100644 src/libs/mynewt-nimble/porting/npl/linux/test/Makefile create mode 100644 src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_callout.c create mode 100644 src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_eventq.c create mode 100644 src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_mempool.c create mode 100644 src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_sem.c create mode 100644 src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_task.c create mode 100644 src/libs/mynewt-nimble/porting/npl/linux/test/test_util.h (limited to 'src/libs/mynewt-nimble/porting/npl/linux/test') diff --git a/src/libs/mynewt-nimble/porting/npl/linux/test/Makefile b/src/libs/mynewt-nimble/porting/npl/linux/test/Makefile new file mode 100644 index 00000000..c0be3d5f --- /dev/null +++ b/src/libs/mynewt-nimble/porting/npl/linux/test/Makefile @@ -0,0 +1,120 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# * http://www.apache.org/licenses/LICENSE-2.0 +# * Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +# Makefile + +PROJ_ROOT = ../../../.. + +### ===== Toolchain ===== + +CROSS_COMPILE ?= +CC = ccache $(CROSS_COMPILE)gcc +CPP = ccache $(CROSS_COMPILE)g++ +LD = $(CROSS_COMPILE)gcc +AR = $(CROSS_COMPILE)ar + +### ===== Compiler Flags ===== + +INCLUDES = \ + -I. \ + -I$(PROJ_ROOT)/nimble/include \ + -I$(PROJ_ROOT)/porting/npl/linux/include \ + -I$(PROJ_ROOT)/porting/npl/linux/src \ + -I$(PROJ_ROOT)/porting/nimble/include \ + $(NULL) + +DEFINES = + +CFLAGS = \ + $(INCLUDES) $(DEFINES) \ + -g \ + -D_GNU_SOURCE \ + $(NULL) + +LIBS = -lrt -lpthread -lstdc++ + +LDFLAGS = + +### ===== Sources ===== + +OSAL_PATH = $(PROJ_ROOT)/porting/npl/linux/src + +SRCS = $(shell find $(OSAL_PATH) -maxdepth 1 -name '*.c') +SRCS += $(shell find $(OSAL_PATH) -maxdepth 1 -name '*.cc') +SRCS += $(PROJ_ROOT)/porting/nimble/src/os_mempool.c + +OBJS = $(patsubst %.c, %.o,$(filter %.c, $(SRCS))) +OBJS += $(patsubst %.cc,%.o,$(filter %.cc, $(SRCS))) + +TEST_SRCS = $(shell find . -maxdepth 1 -name '*.c') +TEST_SRCS += $(shell find . -maxdepth 1 -name '*.cc') + +TEST_OBJS = $(patsubst %.c, %.o,$(filter %.c, $(SRCS))) +TEST_OBJS += $(patsubst %.cc,%.o,$(filter %.cc, $(SRCS))) + +### ===== Rules ===== + +all: depend \ + test_npl_task.exe \ + test_npl_callout.exe \ + test_npl_eventq.exe \ + test_npl_sem.exe \ + $(NULL) + +test_npl_task.exe: test_npl_task.o $(OBJS) + $(LD) -o $@ $^ $(LDFLAGS) $(LIBS) + +test_npl_eventq.exe: test_npl_eventq.o $(OBJS) + $(LD) -o $@ $^ $(LDFLAGS) $(LIBS) + +test_npl_callout.exe: test_npl_callout.o $(OBJS) + $(LD) -o $@ $^ $(LDFLAGS) $(LIBS) + +test_npl_sem.exe: test_npl_sem.o $(OBJS) + $(LD) -o $@ $^ $(LDFLAGS) $(LIBS) + +test: all + ./test_npl_task.exe + ./test_npl_callout.exe + ./test_npl_eventq.exe + ./test_npl_sem.exe + +show_objs: + @echo $(OBJS) + +### ===== Clean ===== +clean: + @echo "Cleaning artifacts." + rm *~ .depend $(OBJS) *.o *.exe + +### ===== Dependencies ===== +### Rebuild if headers change +depend: .depend + +.depend: $(SRCS) $(TEST_SRCS) + @echo "Building dependencies." + rm -f ./.depend + $(CC) $(CFLAGS) -MM $^ > ./.depend; + +include .depend + +### Generic rules based on extension +%.o: %.c + $(CC) -c $(CFLAGS) $< -o $@ + +%.o: %.cc + $(CPP) -c $(CFLAGS) $< -o $@ diff --git a/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_callout.c b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_callout.c new file mode 100644 index 00000000..d04303f8 --- /dev/null +++ b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_callout.c @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + Unit tests for the ble_npl_callout api: + + void ble_npl_callout_init(struct ble_npl_callout *cf, struct ble_npl_eventq *evq, + ble_npl_event_fn *ev_cb, void *ev_arg); + int ble_npl_callout_reset(struct ble_npl_callout *, int32_t); + int ble_npl_callout_queued(struct ble_npl_callout *c); + void ble_npl_callout_stop(struct ble_npl_callout *c); +*/ + +#include "test_util.h" +#include "nimble/nimble_npl.h" + +#define TEST_ARGS_VALUE (55) +#define TEST_INTERVAL (100) + +static bool s_tests_running = true; +static struct ble_npl_task s_task; +static struct ble_npl_callout s_callout; +static int s_callout_args = TEST_ARGS_VALUE; + +static struct ble_npl_eventq s_eventq; + + +void on_callout(struct ble_npl_event *ev) +{ + VerifyOrQuit(ev->ev_arg == &s_callout_args, + "callout: wrong args passed"); + + VerifyOrQuit(*(int*)ev->ev_arg == TEST_ARGS_VALUE, + "callout: args corrupted"); + + s_tests_running = false; +} + +/** + * ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq, + * ble_npl_event_fn *ev_cb, void *ev_arg) + */ +int test_init(void) +{ + ble_npl_callout_init(&s_callout, + &s_eventq, + on_callout, + &s_callout_args); + return PASS; +} + +int test_queued(void) +{ + //VerifyOrQuit(ble_npl_callout_queued(&s_callout), + // "callout: not queued when expected"); + return PASS; +} + +int test_reset(void) +{ + return ble_npl_callout_reset(&s_callout, TEST_INTERVAL); +} + +int test_stop(void) +{ + return PASS; +} + + +/** + * ble_npl_callout_init(struct ble_npl_callout *c, struct ble_npl_eventq *evq, + * ble_npl_event_fn *ev_cb, void *ev_arg) + */ +void *test_task_run(void *args) +{ + SuccessOrQuit(test_init(), "callout_init failed"); + SuccessOrQuit(test_queued(), "callout_queued failed"); + SuccessOrQuit(test_reset(), "callout_reset failed"); + + while (s_tests_running) + { + ble_npl_eventq_run(&s_eventq); + } + + printf("All tests passed\n"); + exit(PASS); + + return NULL; +} + +int main(void) +{ + ble_npl_eventq_init(&s_eventq); + + SuccessOrQuit(ble_npl_task_init(&s_task, "s_task", test_task_run, + NULL, 1, 0, NULL, 0), + "task: error initializing"); + + while (1) {} +} diff --git a/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_eventq.c b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_eventq.c new file mode 100644 index 00000000..f0c362b9 --- /dev/null +++ b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_eventq.c @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + Unit tests for the ble_npl_eventq api: + + void ble_npl_eventq_init(struct ble_npl_eventq *); + void ble_npl_eventq_put(struct ble_npl_eventq *, struct ble_npl_event *); + struct ble_npl_event *ble_npl_eventq_get_no_wait(struct ble_npl_eventq *evq); + struct ble_npl_event *ble_npl_eventq_get(struct ble_npl_eventq *); + void ble_npl_eventq_run(struct ble_npl_eventq *evq); + struct ble_npl_event *ble_npl_eventq_poll(struct ble_npl_eventq **, int, ble_npl_time_t); + void ble_npl_eventq_remove(struct ble_npl_eventq *, struct ble_npl_event *); + struct ble_npl_eventq *ble_npl_eventq_dflt_get(void); +*/ + +#include +#include +#include "test_util.h" +#include "nimble/nimble_npl.h" + +#define TEST_ARGS_VALUE (55) +#define TEST_STACK_SIZE (1024) + +static bool s_tests_running = true; +static struct ble_npl_task s_task_runner; +static struct ble_npl_task s_task_dispatcher; + +static struct ble_npl_eventq s_eventq; +static struct ble_npl_event s_event; +static int s_event_args = TEST_ARGS_VALUE; + + +void on_event(struct ble_npl_event *ev) +{ + VerifyOrQuit(ev->ev_arg == &s_event_args, + "callout: wrong args passed"); + + VerifyOrQuit(*(int*)ev->ev_arg == TEST_ARGS_VALUE, + "callout: args corrupted"); + + s_tests_running = false; +} + +int test_init(void) +{ + //VerifyOrQuit(!ble_npl_eventq_inited(&s_eventq), "eventq: empty q initialized"); + ble_npl_eventq_init(&s_eventq); + //VerifyOrQuit(ble_npl_eventq_inited(&s_eventq), "eventq: not initialized"); + + return PASS; +} + +int test_run(void) +{ + while (s_tests_running) + { + ble_npl_eventq_run(&s_eventq); + } + + return PASS; +} + +int test_put(void) +{ + s_event.ev_cb = on_event; + s_event.ev_arg = &s_event_args; + ble_npl_eventq_put(&s_eventq, &s_event); + return PASS; +} + +int test_get_no_wait(void) +{ + //struct ble_npl_event *ev = ble_npl_eventq_get_no_wait(&s_eventq); + return FAIL; +} + +int test_get(void) +{ + struct ble_npl_event *ev = ble_npl_eventq_get(&s_eventq, + BLE_NPL_TIME_FOREVER); + + VerifyOrQuit(ev == &s_event, + "callout: wrong event passed"); + + return PASS; +} + + +void *task_test_runner(void *args) +{ + int count = 1000000000; + + SuccessOrQuit(test_init(), "eventq_init failed"); + SuccessOrQuit(test_put(), "eventq_put failed"); + SuccessOrQuit(test_get(), "eventq_get failed"); + SuccessOrQuit(test_put(), "eventq_put failed"); + SuccessOrQuit(test_run(), "eventq_run failed"); + + printf("All tests passed\n"); + exit(PASS); + + return NULL; +} + +int main(void) +{ + SuccessOrQuit(ble_npl_task_init(&s_task_runner, + "task_test_runner", + task_test_runner, + NULL, 1, 0, NULL, 0), + "task: error initializing"); + + while (1) {} +} diff --git a/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_mempool.c b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_mempool.c new file mode 100644 index 00000000..2dac0bb1 --- /dev/null +++ b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_mempool.c @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "test_util.h" +#include "nimble/nimble_npl.h" + +#define TEST_MEMPOOL_BLOCKS 4 +#define TEST_MEMPOOL_BLOCK_SIZE 128 + +static struct ble_npl_mempool s_mempool; + +static os_membuf_t s_mempool_mem[OS_MEMPOOL_SIZE(TEST_MEMPOOL_BLOCKS, + TEST_MEMPOOL_BLOCK_SIZE)]; + +static void *s_memblock[TEST_MEMPOOL_BLOCKS]; + +/** + * Unit test for initializing a mempool. + * + * ble_npl_error_t ble_npl_mempool_init(struct ble_npl_mempool *mp, int blocks, + * int block_size, void *membuf, char *name); + * + */ +int test_init(void) +{ + int err; + err = ble_npl_mempool_init(NULL, + TEST_MEMPOOL_BLOCKS, + TEST_MEMPOOL_BLOCK_SIZE, + NULL, + "Null mempool"); + VerifyOrQuit(err, "ble_npl_mempool_init accepted NULL parameters."); + + err = ble_npl_mempool_init(&s_mempool, + TEST_MEMPOOL_BLOCKS, + TEST_MEMPOOL_BLOCK_SIZE, + s_mempool_mem, + "s_mempool"); + return err; +} + +/** + * Test integrity check of a mempool. + * + * bool ble_npl_mempool_is_sane(const struct ble_npl_mempool *mp); + */ +int test_is_sane(void) +{ + return (ble_npl_mempool_is_sane(&s_mempool)) ? PASS : FAIL; +} + +/** + * Test getting a memory block from the pool, putting it back, + * and checking if it is still valid. + * + * void *ble_npl_memblock_get(struct ble_npl_mempool *mp); + * + * ble_npl_error_t ble_npl_memblock_put(struct ble_npl_mempool *mp, void *block_addr); + * + * int ble_npl_memblock_from(const struct ble_npl_mempool *mp, const void *block_addr); + */ +int test_stress(void) +{ + int loops = 3; + while(loops--) + { + for (int i = 0; i < 4; i++) + { + s_memblock[i] = ble_npl_memblock_get(&s_mempool); + VerifyOrQuit(ble_npl_memblock_from(&s_mempool, s_memblock[i]), + "ble_npl_memblock_get return invalid block."); + } + + + for (int i = 0; i < 4; i++) + { + SuccessOrQuit(ble_npl_memblock_put(&s_mempool, s_memblock[i]), + "ble_npl_memblock_put refused to take valid block."); + //VerifyOrQuit(!ble_npl_memblock_from(&s_mempool, s_memblock[i]), + // "Block still valid after ble_npl_memblock_put."); + } + + } + return PASS; +} + +int main(void) +{ + SuccessOrQuit(test_init(), "Failed: ble_npl_mempool_init"); + SuccessOrQuit(test_is_sane(), "Failed: ble_npl_mempool_is_sane"); + SuccessOrQuit(test_stress(), "Failed: ble_npl_mempool stree test"); + SuccessOrQuit(test_is_sane(), "Failed: ble_npl_mempool_is_sane"); + printf("All tests passed\n"); + return PASS; +} diff --git a/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_sem.c b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_sem.c new file mode 100644 index 00000000..b62f8e2a --- /dev/null +++ b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_sem.c @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + Unit tests for the Semaphore api (ble_npl_sem): + + ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens); + ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem); + ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, uint32_t timeout); + uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem); +*/ + +#include "test_util.h" +#include "nimble/nimble_npl.h" +//#include "os/os.h" + +#define TEST_ITERATIONS 10 + +#define TASK1_PRIO 1 +#define TASK2_PRIO 1 + +#define TASK1_STACK_SIZE 1028 +#define TASK2_STACK_SIZE 1028 + +static struct ble_npl_task task1; +static struct ble_npl_task task2; + +static ble_npl_stack_t task1_stack[TASK1_STACK_SIZE]; +static ble_npl_stack_t task2_stack[TASK2_STACK_SIZE]; + +struct ble_npl_sem task1_sem; +struct ble_npl_sem task2_sem; + +/* Task 1 handler function */ +void * +task1_handler(void *arg) +{ + for (int i = 0; i < TEST_ITERATIONS; i++) + { + /* Release semaphore to task 2 */ + SuccessOrQuit(ble_npl_sem_release(&task1_sem), + "ble_npl_sem_release: error releasing task2_sem."); + + /* Wait for semaphore from task 2 */ + SuccessOrQuit(ble_npl_sem_pend(&task2_sem, BLE_NPL_TIME_FOREVER), + "ble_npl_sem_pend: error waiting for task2_sem."); + } + + printf("All tests passed\n"); + exit(PASS); + + return NULL; +} + +/* Task 2 handler function */ +void * +task2_handler(void *arg) +{ + while(1) + { + /* Wait for semaphore from task1 */ + SuccessOrQuit(ble_npl_sem_pend(&task1_sem, BLE_NPL_TIME_FOREVER), + "ble_npl_sem_pend: error waiting for task1_sem."); + + /* Release task2 semaphore */ + SuccessOrQuit(ble_npl_sem_release(&task2_sem), + "ble_npl_sem_release: error releasing task1_sem."); + } + + return NULL; +} + + +/* Initialize task 1 exposed data objects */ +void +task1_init(void) +{ + /* Initialize task1 semaphore */ + SuccessOrQuit(ble_npl_sem_init(&task1_sem, 0), + "ble_npl_sem_init: task1 returned error."); +} + +/* Initialize task 2 exposed data objects */ +void +task2_init(void) +{ + /* Initialize task1 semaphore */ + SuccessOrQuit(ble_npl_sem_init(&task2_sem, 0), + "ble_npl_sem_init: task2 returned error."); +} + +/** + * init_app_tasks + * + * This function performs initializations that are required before tasks run. + * + * @return int 0 success; error otherwise. + */ +static int +init_app_tasks(void) +{ + /* + * Call task specific initialization functions to initialize any shared objects + * before initializing the tasks with the OS. + */ + task1_init(); + task2_init(); + + /* + * Initialize tasks 1 and 2 with the OS. + */ + ble_npl_task_init(&task1, "task1", task1_handler, NULL, TASK1_PRIO, + BLE_NPL_TIME_FOREVER, task1_stack, TASK1_STACK_SIZE); + + ble_npl_task_init(&task2, "task2", task2_handler, NULL, TASK2_PRIO, + BLE_NPL_TIME_FOREVER, task2_stack, TASK2_STACK_SIZE); + + return 0; +} + +/** + * main + * + * The main function for the application. This function initializes the system and packages, + * calls the application specific task initialization function, then waits and dispatches + * events from the OS default event queue in an infinite loop. + */ +int +main(int argc, char **arg) +{ + /* Initialize application specific tasks */ + init_app_tasks(); + + while (1) + { + ble_npl_eventq_run(ble_npl_eventq_dflt_get()); + } + /* main never returns */ +} diff --git a/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_task.c b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_task.c new file mode 100644 index 00000000..66310cd7 --- /dev/null +++ b/src/libs/mynewt-nimble/porting/npl/linux/test/test_npl_task.c @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "test_util.h" +#include "nimble/nimble_npl.h" + +#include + +#define TASK0_ARG 55 +#define TASK1_ARG 66 + +static struct ble_npl_task s_task[2]; +static int s_task_arg[2] = +{ + TASK0_ARG, TASK1_ARG +}; + + +void *task0_run(void *args) +{ + int i = 10000; + VerifyOrQuit(args == &s_task_arg[0], "Wrong args passed to task0"); + + while (i--) + { + } + + return NULL; +} + +void *task1_run(void *args) +{ + int i = 10000; + VerifyOrQuit(args == &s_task_arg[1], "Wrong args passed to task0"); + + while (i--) + { + } + + printf("All tests passed\n"); + exit(PASS); + + return NULL; +} + +/** + * Unit test for initializing a task. + * + * int ble_npl_task_init(struct ble_npl_task *t, const char *name, ble_npl_task_func_t func, + * void *arg, uint8_t prio, ble_npl_time_t sanity_itvl, + * ble_npl_stack_t *stack_bottom, uint16_t stack_size) + * + */ +int test_init(void) +{ + int err; + err = ble_npl_task_init(NULL, + "Null task", + NULL, NULL, 1, 0, NULL, 0); + VerifyOrQuit(err, "ble_npl_task_init accepted NULL parameters."); + + err = ble_npl_task_init(&s_task[0], + "s_task[0]", + task0_run, &s_task_arg[0], 1, 0, NULL, 0); + SuccessOrQuit(err, "ble_npl_task_init failed."); + + err = ble_npl_task_init(&s_task[1], + "s_task[1]", + task1_run, &s_task_arg[1], 1, 0, NULL, 0); + + return err; +} + +int main(void) +{ + int ret = PASS; + SuccessOrQuit(test_init(), "Failed: ble_npl_task_init"); + + pthread_exit(&ret); + + return ret; +} diff --git a/src/libs/mynewt-nimble/porting/npl/linux/test/test_util.h b/src/libs/mynewt-nimble/porting/npl/linux/test/test_util.h new file mode 100644 index 00000000..90985c3f --- /dev/null +++ b/src/libs/mynewt-nimble/porting/npl/linux/test/test_util.h @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _TEST_UTIL_H_ +#define _TEST_UTIL_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#define PASS (0) +#define FAIL (-1) + +#define SuccessOrQuit(ERR, MSG) \ + do { \ + if ((ERR)) \ + { \ + fprintf(stderr, "\nFAILED %s:%d - %s\n", __FUNCTION__, __LINE__, MSG); \ + exit(-1); \ + } \ + } while (false) + +#define VerifyOrQuit(TST, MSG) \ + do { \ + if (!(TST)) \ + { \ + fprintf(stderr, "\nFAILED %s:%d - %s\n", __FUNCTION__, __LINE__, MSG); \ + exit(-1); \ + } \ + } while (false) + + +#ifdef __cplusplus +} +#endif + +#endif /* _TEST_UTIL_H_ */ -- cgit v1.2.3