|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may 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, |
| 13 | + * WITHOUT 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 | + |
| 18 | +#include <inet/EndPointStateLwIP.h> |
| 19 | + |
| 20 | +#include <lwip/sys.h> |
| 21 | +#include <lwip/tcpip.h> |
| 22 | + |
| 23 | +#include <platform/LockTracker.h> |
| 24 | + |
| 25 | +namespace chip { |
| 26 | +namespace Inet { |
| 27 | + |
| 28 | +err_t EndPointStateLwIP::RunOnTCPIPRet(std::function<err_t()> fn) |
| 29 | +{ |
| 30 | + assertChipStackLockedByCurrentThread(); |
| 31 | +#if LWIP_TCPIP_CORE_LOCKING |
| 32 | + err_t err; |
| 33 | + LOCK_TCPIP_CORE(); |
| 34 | + err = fn(); |
| 35 | + UNLOCK_TCPIP_CORE(); |
| 36 | + return err; |
| 37 | +#else |
| 38 | + // Post a message to the TCPIP task and wait for it to run. |
| 39 | + static sys_sem_t sTCPIPSem; |
| 40 | + static bool sTCPIPSemInited = false; |
| 41 | + if (!sTCPIPSemInited) |
| 42 | + { |
| 43 | + err_t err = sys_sem_new(&sTCPIPSem, 0); |
| 44 | + if (err != ERR_OK) |
| 45 | + { |
| 46 | + return err; |
| 47 | + } |
| 48 | + sTCPIPSemInited = true; |
| 49 | + } |
| 50 | + |
| 51 | + // tcpip_callback takes a C function pointer, so we can't pass a capturing lambda to it. |
| 52 | + // Just store the state the function we pass to it needs in statics. |
| 53 | + // This should be safe, since that function will execute before we return and there is no |
| 54 | + // re-entry into this method. |
| 55 | + static std::function<err_t()> sTCPIPFunction; |
| 56 | + static err_t sTCPIPFunctionResult; |
| 57 | + VerifyOrDie(sTCPIPFunction == nullptr); |
| 58 | + |
| 59 | + sTCPIPFunction = fn; |
| 60 | + const err_t err = tcpip_callback( |
| 61 | + [](void * aCtx) { |
| 62 | + sTCPIPFunctionResult = sTCPIPFunction(); |
| 63 | + sys_sem_signal(&sTCPIPSem); |
| 64 | + }, |
| 65 | + nullptr); |
| 66 | + if (err != ERR_OK) |
| 67 | + { |
| 68 | + return err; |
| 69 | + } |
| 70 | + sys_arch_sem_wait(&sTCPIPSem, 0); |
| 71 | + sTCPIPFunction = nullptr; |
| 72 | + return sTCPIPFunctionResult; |
| 73 | +#endif |
| 74 | +} |
| 75 | + |
| 76 | +void EndPointStateLwIP::RunOnTCPIP(std::function<void()> fn) |
| 77 | +{ |
| 78 | + VerifyOrDie(RunOnTCPIPRet([&fn]() { |
| 79 | + fn(); |
| 80 | + return ERR_OK; |
| 81 | + }) == ERR_OK); |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace Inet |
| 85 | +} // namespace chip |
0 commit comments