1 /* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MESSAGE_HANDLER_H 17 #define MESSAGE_HANDLER_H 18 19 #include <stdbool.h> 20 #include <stdint.h> 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 typedef struct SoftBusMessage SoftBusMessage; 27 typedef struct SoftBusHandler SoftBusHandler; 28 typedef struct SoftBusLooperContext SoftBusLooperContext; 29 typedef struct SoftBusLooper SoftBusLooper; 30 31 struct SoftBusLooper { 32 SoftBusLooperContext *context; 33 bool dumpable; 34 void (*PostMessage)(const SoftBusLooper *looper, SoftBusMessage *msg); 35 void (*PostMessageDelay)(const SoftBusLooper *looper, SoftBusMessage *msg, uint64_t delayMillis); 36 void (*RemoveMessage)(const SoftBusLooper *looper, const SoftBusHandler *handler, int32_t what); 37 // customFunc, when match, return 0 38 void (*RemoveMessageCustom)(const SoftBusLooper *looper, const SoftBusHandler *handler, 39 int32_t (*)(const SoftBusMessage*, void*), void *args); 40 }; 41 42 struct SoftBusHandler { 43 char *name; 44 SoftBusLooper *looper; 45 void (*HandleMessage)(SoftBusMessage *msg); 46 }; 47 48 struct SoftBusMessage { 49 int32_t what; 50 uint64_t arg1; 51 uint64_t arg2; 52 int64_t time; 53 void *obj; 54 SoftBusHandler *handler; 55 void (*FreeMessage)(SoftBusMessage *msg); 56 }; 57 58 SoftBusMessage *MallocMessage(void); 59 60 void FreeMessage(SoftBusMessage *msg); 61 62 enum LooperType { 63 LOOP_TYPE_DEFAULT = 1, 64 LOOP_TYPE_CONN, 65 LOOP_TYPE_LNN 66 }; 67 68 SoftBusLooper *GetLooper(int looper); 69 70 int LooperInit(void); 71 72 void SetLooper(int type, SoftBusLooper *looper); 73 74 void LooperDeinit(void); 75 76 void DumpLooper(const SoftBusLooper *looper); 77 78 SoftBusLooper *CreateNewLooper(const char *name); 79 80 void DestroyLooper(SoftBusLooper *looper); 81 82 void SetLooperDumpable(SoftBusLooper *looper, bool dumpable); 83 84 #ifdef __cplusplus 85 } 86 #endif 87 88 #endif /* MESSAGE_HANDLER_H */ 89