diff --git a/UNITTESTS/stubs/CMakeLists.txt b/UNITTESTS/stubs/CMakeLists.txt index a81923a..dddc151 100644 --- a/UNITTESTS/stubs/CMakeLists.txt +++ b/UNITTESTS/stubs/CMakeLists.txt @@ -62,7 +62,6 @@ ) add_subdirectory(connectivity) -add_subdirectory(events) add_library(mbed-stubs INTERFACE) diff --git a/UNITTESTS/stubs/events/CMakeLists.txt b/UNITTESTS/stubs/events/CMakeLists.txt deleted file mode 100644 index c0127aa..0000000 --- a/UNITTESTS/stubs/events/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright (c) 2021 ARM Limited. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -add_library(mbed-stubs-events) - -target_sources(mbed-stubs-events - PRIVATE - equeue_stub.c - ${mbed-os_SOURCE_DIR}/events/tests/UNITTESTS/stubs/EqueuePosix_stub.c - EventFlags_stub.cpp - EventQueue_stub.cpp - mbed_shared_queues_stub.cpp -) - -target_link_libraries(mbed-stubs-events - PRIVATE - mbed-headers - mbed-stubs-headers -) diff --git a/UNITTESTS/stubs/events/EventFlags_stub.cpp b/UNITTESTS/stubs/events/EventFlags_stub.cpp deleted file mode 100644 index 39ded93..0000000 --- a/UNITTESTS/stubs/events/EventFlags_stub.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) , Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "rtos/EventFlags.h" -#include - -/** Store the next value to be returned by Event Flags member functions */ -std::list eventFlagsStubNextRetval; - -rtos::EventFlags::EventFlags() {} -rtos::EventFlags::~EventFlags() {} -uint32_t rtos::EventFlags::set(uint32_t flags) -{ - return 0; -} -uint32_t rtos::EventFlags::clear(uint32_t flags) -{ - return 0; -} -uint32_t rtos::EventFlags::get() const -{ - return 0; -} -uint32_t rtos::EventFlags::wait_all(uint32_t flags, uint32_t timeout, bool clear) -{ - return 0; -} -uint32_t rtos::EventFlags::wait_any(uint32_t flags, uint32_t timeout, bool clear) -{ - if (!eventFlagsStubNextRetval.empty()) { - uint32_t ret = eventFlagsStubNextRetval.front(); - eventFlagsStubNextRetval.pop_front(); - return ret; - } - return 0; -} diff --git a/UNITTESTS/stubs/events/EventQueue_stub.cpp b/UNITTESTS/stubs/events/EventQueue_stub.cpp deleted file mode 100644 index e1670d5..0000000 --- a/UNITTESTS/stubs/events/EventQueue_stub.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) , Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "events/EventQueue.h" -#include "Callback.h" -#include "EventQueue_stub.h" - -using namespace mbed; - -int EventQueue_stub::int_value = 0; -unsigned EventQueue_stub::unsigned_value = 0; - -namespace events { - -EventQueue::EventQueue(unsigned event_size, unsigned char *event_pointer) -{ -} - -EventQueue::~EventQueue() -{ -} - -void EventQueue::dispatch(int ms) -{ -} - -void EventQueue::break_dispatch() -{ -} - -unsigned EventQueue::tick() -{ - return EventQueue_stub::unsigned_value; -} - -bool EventQueue::cancel(int id) -{ - return true; -} - -int EventQueue::time_left(int id) -{ - return EventQueue_stub::int_value; -} - -void EventQueue::background(Callback update) -{ -} - -int EventQueue::chain(EventQueue *target) -{ - return 0; -} - -} // namespace events diff --git a/UNITTESTS/stubs/events/equeue_stub.c b/UNITTESTS/stubs/events/equeue_stub.c deleted file mode 100644 index 5b6a068..0000000 --- a/UNITTESTS/stubs/events/equeue_stub.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) , Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "events/equeue.h" -#include - -#include "equeue_stub.h" - -equeue_stub_def equeue_stub; - -int equeue_create(equeue_t *queue, size_t size) -{ - return 0; -} - -int equeue_create_inplace(equeue_t *queue, size_t size, void *buffer) -{ - return 0; -} - -void equeue_destroy(equeue_t *queue) -{ - -} - -void equeue_dispatch(equeue_t *queue, int ms) -{ - -} - -void equeue_break(equeue_t *queue) -{ - -} - -int equeue_call(equeue_t *queue, void (*cb)(void *), void *data) -{ - return 0; -} - -int equeue_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data) -{ - return 0; -} - -void *equeue_alloc(equeue_t *queue, size_t size) -{ - return equeue_stub.void_ptr; -} - -void equeue_dealloc(equeue_t *queue, void *event) -{ - -} - -void equeue_event_delay(void *event, int ms) -{ - -} - -void equeue_event_period(void *event, int ms) -{ - -} - -void equeue_event_dtor(void *event, void (*dtor)(void *)) -{ - -} - -int equeue_post(equeue_t *queue, void (*cb)(void *), void *event) -{ - struct equeue_event *e = (struct equeue_event *)event - 1; - if (cb && equeue_stub.call_cb_immediately) { - cb(e + 1); - return 1; - } - return 0; -} - -bool equeue_cancel(equeue_t *queue, int id) -{ - return true; -} - -void equeue_background(equeue_t *queue, - void (*update)(void *timer, int ms), void *timer) -{ - -} - -int equeue_chain(equeue_t *queue, equeue_t *target) -{ - return 0; -} - -int equeue_call_in(equeue_t *q, int ms, void (*cb)(void *), void *data) -{ - // The stub does not implement the delay mechanism. - return equeue_post(q, cb, data); -} diff --git a/UNITTESTS/stubs/events/mbed_shared_queues_stub.cpp b/UNITTESTS/stubs/events/mbed_shared_queues_stub.cpp deleted file mode 100644 index 7f0474b..0000000 --- a/UNITTESTS/stubs/events/mbed_shared_queues_stub.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "events/mbed_shared_queues.h" - -using namespace events; - -namespace mbed { - -EventQueue *mbed_shared_queue_stub; -EventQueue *mbed_event_queue() -{ - return mbed_shared_queue_stub; -} -} // namespace mbed diff --git a/events/tests/UNITTESTS/CMakeLists.txt b/events/tests/UNITTESTS/CMakeLists.txt index 0c295bb..5b01cbe 100644 --- a/events/tests/UNITTESTS/CMakeLists.txt +++ b/events/tests/UNITTESTS/CMakeLists.txt @@ -1,4 +1,5 @@ # Copyright (c) 2021 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +add_subdirectory(doubles) add_subdirectory(equeue) diff --git a/events/tests/UNITTESTS/doubles/CMakeLists.txt b/events/tests/UNITTESTS/doubles/CMakeLists.txt new file mode 100644 index 0000000..b7bf0ee --- /dev/null +++ b/events/tests/UNITTESTS/doubles/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright (c) 2021 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +add_library(mbed-stubs-events) + +target_sources(mbed-stubs-events + PRIVATE + equeue_stub.c + EqueuePosix_stub.c + EventFlags_stub.cpp + EventQueue_stub.cpp + mbed_shared_queues_stub.cpp +) + +target_link_libraries(mbed-stubs-events + PRIVATE + mbed-headers + mbed-stubs-headers +) diff --git a/events/tests/UNITTESTS/doubles/EqueuePosix_stub.c b/events/tests/UNITTESTS/doubles/EqueuePosix_stub.c new file mode 100644 index 0000000..e3d975e --- /dev/null +++ b/events/tests/UNITTESTS/doubles/EqueuePosix_stub.c @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2019, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "internal/equeue_platform.h" + +#if defined(EQUEUE_PLATFORM_POSIX) + +#include + +/* + * Using global variable as a simulation of passing time. Use of sleep functions may cause problems with thread preempting, which can lead to bad timings. + * This problem does not occur on targets. + * This variable is only increased in equeue_sema_wait function and in functions that simulate pass of time. + */ +unsigned int equeue_global_time = 0; + +// Tick operations +void equeue_tick_init(void) {} + +unsigned equeue_tick(void) +{ + return equeue_global_time; +} + + +// Mutex operations +int equeue_mutex_create(equeue_mutex_t *m) +{ + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + return pthread_mutex_init(m, &attr); +} + +void equeue_mutex_destroy(equeue_mutex_t *m) +{ + pthread_mutex_destroy(m); +} + +void equeue_mutex_lock(equeue_mutex_t *m) +{ + pthread_mutex_lock(m); +} + +void equeue_mutex_unlock(equeue_mutex_t *m) +{ + pthread_mutex_unlock(m); +} + + +// Semaphore operations +int equeue_sema_create(equeue_sema_t *s) +{ + int err = pthread_mutex_init(&s->mutex, 0); + if (err) { + return err; + } + + err = pthread_cond_init(&s->cond, 0); + if (err) { + return err; + } + + s->signal = false; + return 0; +} + +void equeue_sema_destroy(equeue_sema_t *s) +{ + pthread_cond_destroy(&s->cond); + pthread_mutex_destroy(&s->mutex); +} + +void equeue_sema_signal(equeue_sema_t *s) +{ + pthread_mutex_lock(&s->mutex); + s->signal = true; + pthread_cond_signal(&s->cond); + pthread_mutex_unlock(&s->mutex); +} + +bool equeue_sema_wait(equeue_sema_t *s, int ms) +{ + pthread_mutex_lock(&s->mutex); + if (!s->signal) { + if (ms < 0) { + pthread_cond_wait(&s->cond, &s->mutex); + } else { + for (int i = 0; i < ms; i++) { + equeue_global_time++; + } + // If ms == 0 increase time so functions don't get stuck in infinite loops. + if (ms == 0) { + equeue_global_time++; + } + } + } + + bool signal = s->signal; + s->signal = false; + pthread_mutex_unlock(&s->mutex); + + return signal; +} + +#endif diff --git a/events/tests/UNITTESTS/doubles/EventFlags_stub.cpp b/events/tests/UNITTESTS/doubles/EventFlags_stub.cpp new file mode 100644 index 0000000..39ded93 --- /dev/null +++ b/events/tests/UNITTESTS/doubles/EventFlags_stub.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) , Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "rtos/EventFlags.h" +#include + +/** Store the next value to be returned by Event Flags member functions */ +std::list eventFlagsStubNextRetval; + +rtos::EventFlags::EventFlags() {} +rtos::EventFlags::~EventFlags() {} +uint32_t rtos::EventFlags::set(uint32_t flags) +{ + return 0; +} +uint32_t rtos::EventFlags::clear(uint32_t flags) +{ + return 0; +} +uint32_t rtos::EventFlags::get() const +{ + return 0; +} +uint32_t rtos::EventFlags::wait_all(uint32_t flags, uint32_t timeout, bool clear) +{ + return 0; +} +uint32_t rtos::EventFlags::wait_any(uint32_t flags, uint32_t timeout, bool clear) +{ + if (!eventFlagsStubNextRetval.empty()) { + uint32_t ret = eventFlagsStubNextRetval.front(); + eventFlagsStubNextRetval.pop_front(); + return ret; + } + return 0; +} diff --git a/events/tests/UNITTESTS/doubles/EventQueue_stub.cpp b/events/tests/UNITTESTS/doubles/EventQueue_stub.cpp new file mode 100644 index 0000000..e1670d5 --- /dev/null +++ b/events/tests/UNITTESTS/doubles/EventQueue_stub.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) , Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "events/EventQueue.h" +#include "Callback.h" +#include "EventQueue_stub.h" + +using namespace mbed; + +int EventQueue_stub::int_value = 0; +unsigned EventQueue_stub::unsigned_value = 0; + +namespace events { + +EventQueue::EventQueue(unsigned event_size, unsigned char *event_pointer) +{ +} + +EventQueue::~EventQueue() +{ +} + +void EventQueue::dispatch(int ms) +{ +} + +void EventQueue::break_dispatch() +{ +} + +unsigned EventQueue::tick() +{ + return EventQueue_stub::unsigned_value; +} + +bool EventQueue::cancel(int id) +{ + return true; +} + +int EventQueue::time_left(int id) +{ + return EventQueue_stub::int_value; +} + +void EventQueue::background(Callback update) +{ +} + +int EventQueue::chain(EventQueue *target) +{ + return 0; +} + +} // namespace events diff --git a/events/tests/UNITTESTS/doubles/equeue_stub.c b/events/tests/UNITTESTS/doubles/equeue_stub.c new file mode 100644 index 0000000..5b6a068 --- /dev/null +++ b/events/tests/UNITTESTS/doubles/equeue_stub.c @@ -0,0 +1,115 @@ +/* + * Copyright (c) , Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "events/equeue.h" +#include + +#include "equeue_stub.h" + +equeue_stub_def equeue_stub; + +int equeue_create(equeue_t *queue, size_t size) +{ + return 0; +} + +int equeue_create_inplace(equeue_t *queue, size_t size, void *buffer) +{ + return 0; +} + +void equeue_destroy(equeue_t *queue) +{ + +} + +void equeue_dispatch(equeue_t *queue, int ms) +{ + +} + +void equeue_break(equeue_t *queue) +{ + +} + +int equeue_call(equeue_t *queue, void (*cb)(void *), void *data) +{ + return 0; +} + +int equeue_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data) +{ + return 0; +} + +void *equeue_alloc(equeue_t *queue, size_t size) +{ + return equeue_stub.void_ptr; +} + +void equeue_dealloc(equeue_t *queue, void *event) +{ + +} + +void equeue_event_delay(void *event, int ms) +{ + +} + +void equeue_event_period(void *event, int ms) +{ + +} + +void equeue_event_dtor(void *event, void (*dtor)(void *)) +{ + +} + +int equeue_post(equeue_t *queue, void (*cb)(void *), void *event) +{ + struct equeue_event *e = (struct equeue_event *)event - 1; + if (cb && equeue_stub.call_cb_immediately) { + cb(e + 1); + return 1; + } + return 0; +} + +bool equeue_cancel(equeue_t *queue, int id) +{ + return true; +} + +void equeue_background(equeue_t *queue, + void (*update)(void *timer, int ms), void *timer) +{ + +} + +int equeue_chain(equeue_t *queue, equeue_t *target) +{ + return 0; +} + +int equeue_call_in(equeue_t *q, int ms, void (*cb)(void *), void *data) +{ + // The stub does not implement the delay mechanism. + return equeue_post(q, cb, data); +} diff --git a/events/tests/UNITTESTS/doubles/mbed_shared_queues_stub.cpp b/events/tests/UNITTESTS/doubles/mbed_shared_queues_stub.cpp new file mode 100644 index 0000000..7f0474b --- /dev/null +++ b/events/tests/UNITTESTS/doubles/mbed_shared_queues_stub.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2017, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "events/mbed_shared_queues.h" + +using namespace events; + +namespace mbed { + +EventQueue *mbed_shared_queue_stub; +EventQueue *mbed_event_queue() +{ + return mbed_shared_queue_stub; +} +} // namespace mbed diff --git a/events/tests/UNITTESTS/stubs/EqueuePosix_stub.c b/events/tests/UNITTESTS/stubs/EqueuePosix_stub.c deleted file mode 100644 index e3d975e..0000000 --- a/events/tests/UNITTESTS/stubs/EqueuePosix_stub.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2019, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "internal/equeue_platform.h" - -#if defined(EQUEUE_PLATFORM_POSIX) - -#include - -/* - * Using global variable as a simulation of passing time. Use of sleep functions may cause problems with thread preempting, which can lead to bad timings. - * This problem does not occur on targets. - * This variable is only increased in equeue_sema_wait function and in functions that simulate pass of time. - */ -unsigned int equeue_global_time = 0; - -// Tick operations -void equeue_tick_init(void) {} - -unsigned equeue_tick(void) -{ - return equeue_global_time; -} - - -// Mutex operations -int equeue_mutex_create(equeue_mutex_t *m) -{ - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - return pthread_mutex_init(m, &attr); -} - -void equeue_mutex_destroy(equeue_mutex_t *m) -{ - pthread_mutex_destroy(m); -} - -void equeue_mutex_lock(equeue_mutex_t *m) -{ - pthread_mutex_lock(m); -} - -void equeue_mutex_unlock(equeue_mutex_t *m) -{ - pthread_mutex_unlock(m); -} - - -// Semaphore operations -int equeue_sema_create(equeue_sema_t *s) -{ - int err = pthread_mutex_init(&s->mutex, 0); - if (err) { - return err; - } - - err = pthread_cond_init(&s->cond, 0); - if (err) { - return err; - } - - s->signal = false; - return 0; -} - -void equeue_sema_destroy(equeue_sema_t *s) -{ - pthread_cond_destroy(&s->cond); - pthread_mutex_destroy(&s->mutex); -} - -void equeue_sema_signal(equeue_sema_t *s) -{ - pthread_mutex_lock(&s->mutex); - s->signal = true; - pthread_cond_signal(&s->cond); - pthread_mutex_unlock(&s->mutex); -} - -bool equeue_sema_wait(equeue_sema_t *s, int ms) -{ - pthread_mutex_lock(&s->mutex); - if (!s->signal) { - if (ms < 0) { - pthread_cond_wait(&s->cond, &s->mutex); - } else { - for (int i = 0; i < ms; i++) { - equeue_global_time++; - } - // If ms == 0 increase time so functions don't get stuck in infinite loops. - if (ms == 0) { - equeue_global_time++; - } - } - } - - bool signal = s->signal; - s->signal = false; - pthread_mutex_unlock(&s->mutex); - - return signal; -} - -#endif