1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef SIDECAR_H 10 #define SIDECAR_H 11 #include "hdf_base.h" 12 #include "hdf_device_desc.h" 13 #include "osal/osal_sem.h" 14 #include "message_types.h" 15 #include "hdf_sbuf.h" 16 17 enum MessageType { 18 MESSAGE_REQ_START = 0, 19 MESSAGE_TYPE_SYNC_REQ, 20 MESSAGE_TYPE_ASYNC_REQ, 21 22 MESSAGE_RSP_START, 23 MESSAGE_TYPE_SYNC_RSP, 24 MESSAGE_TYPE_ASYNC_RSP, 25 }; 26 27 #define INHERT_REQUEST_CONTEXT \ 28 uint32_t commandId; \ 29 uint8_t senderId; \ 30 uint8_t receiverId; \ 31 uint8_t requestType; \ 32 bool crossNode; \ 33 struct HdfDeviceIoClient *client 34 35 #define RESERVED_SERVICE_ID 0 36 #define BAD_SERVICE_ID 254 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 int32_t DispatchToMessage(struct HdfDeviceIoClient *client, int id, struct HdfSBuf *reqData, struct HdfSBuf *rspData); 43 44 typedef struct { 45 INHERT_REQUEST_CONTEXT; 46 } RequestContext; 47 48 typedef void (*MessageCallBack)(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData, 49 ErrorCode responseStatus); 50 51 typedef ErrorCode (*MessageHandler)(const RequestContext *context, struct HdfSBuf *reqData, struct HdfSBuf *rspData); 52 53 struct MessageContext { 54 INHERT_REQUEST_CONTEXT; 55 #if defined(KERNEL_SERVER_SUPPORT) || defined(USERSPACE_CLIENT_SUPPORT) 56 uint16_t messageID; 57 #endif 58 ErrorCode responseStatus; 59 struct HdfSBuf *reqData; 60 struct HdfSBuf *rspData; 61 union { 62 MessageCallBack callback; 63 OSAL_DECLARE_SEMAPHORE(rspSemaphore); 64 }; 65 }; 66 typedef struct MessageContext MessageContext; 67 68 typedef struct SideCar_ { 69 ErrorCode (*SendOneWayMessage)(const struct SideCar_ *sideCar, ServiceId receiver, uint32_t commandId, 70 struct HdfSBuf *reqData); 71 72 ErrorCode (*SendSyncMessage)(const struct SideCar_ *sideCar, ServiceId receiver, uint32_t commandId, 73 struct HdfSBuf *sendData, struct HdfSBuf *recvData); 74 75 ErrorCode (*SendAsyncMessage)(const struct SideCar_ *sideCar, ServiceId receiver, uint32_t commandId, 76 struct HdfSBuf *reqData, MessageCallBack callback); 77 78 ErrorCode (*Destroy)(struct SideCar_ *sideCar); 79 80 void *privateData; 81 } Service; 82 83 typedef struct { 84 DispatcherId dispatcherId; 85 } ServiceCfg; 86 87 struct MessageDef { 88 MessageHandler handler; 89 uint8_t pri; 90 }; 91 92 struct ServiceDef { 93 ServiceId serviceId; 94 uint8_t messagesLength; 95 struct MessageDef *messages; 96 }; 97 98 Service *InitService(struct ServiceDef *def, const ServiceCfg *cfg); 99 100 #define DUEMessage(CMDID, HANDLER, PRI) \ 101 [CMDID] = { \ 102 .handler = HANDLER, \ 103 .pri = PRI \ 104 } 105 106 #define ServiceDefine(ServiceName, SERVICE_ID, ServiceMap) \ 107 Service *CreateService##ServiceName(const ServiceCfg *cfg) \ 108 { \ 109 static struct ServiceDef serviceDef = { \ 110 .serviceId = SERVICE_ID, \ 111 .messagesLength = sizeof(ServiceMap) / sizeof(struct MessageDef), \ 112 .messages = ServiceMap \ 113 }; \ 114 return InitService(&serviceDef, cfg); \ 115 } 116 117 #define CreateService(ServiceName, cfg) CreateService##ServiceName(cfg) 118 119 120 #ifdef __cplusplus 121 } 122 #endif 123 124 #endif