1 /* 2 * Copyright (c) 2021 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 int (*)(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_BR_SEND, 65 LOOP_TYPE_BR_RECV, 66 LOOP_TYPE_P2P 67 }; 68 69 SoftBusLooper *GetLooper(int looper); 70 71 int LooperInit(void); 72 73 void LooperDeinit(void); 74 75 void DumpLooper(const SoftBusLooper *looper); 76 77 SoftBusLooper *CreateNewLooper(const char *name); 78 79 void DestroyLooper(SoftBusLooper *looper); 80 81 void SetLooperDumpable(SoftBusLooper *loop, bool dumpable); 82 83 #ifdef __cplusplus 84 } 85 #endif 86 87 #endif /* MESSAGE_HANDLER_H */ 88