1 /* 2 * Copyright (C) 2021-2022 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 CRPC_SERIAL_H 17 #define CRPC_SERIAL_H 18 19 #include <stdint.h> 20 #include "context.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 /** 27 * @Description Write the message start flag to the context 28 * 29 * @param context - Communication Context 30 * @param type - 0 normal message and reply message; 1 callback message 31 * @return int - 0 Success; -1 Failed 32 */ 33 int WriteBegin(Context *context, int type); 34 35 /** 36 * @Description Write the function to be invoked by the peer end 37 * 38 * @param context - Communication Context 39 * @param funcName - Name of the peer function to be invoked 40 * @return int - 0 Success; -1 Failed 41 */ 42 int WriteFunc(Context *context, const char *funcName); 43 44 /** 45 * @Description Write data of the int type 46 * 47 * @param context - Communication Context 48 * @param i - integer 49 * @return int - 0 Success; -1 Failed 50 */ 51 int WriteInt(Context *context, int i); 52 53 /** 54 * @Description Write data of the long type 55 * 56 * @param context - Communication Context 57 * @param L - long integer 58 * @return int - 0 Success; -1 Failed 59 */ 60 int WriteLong(Context *context, long L); 61 62 /** 63 * @Description Write data of the int64 type 64 * 65 * @param context - Communication Context 66 * @param i - int64 integer 67 * @return int - 0 Success; -1 Failed 68 */ 69 int WriteInt64(Context *context, int64_t i); 70 71 /** 72 * @Description Write data of the double type 73 * 74 * @param context - Communication Context 75 * @param d - double 76 * @return int - 0 Success; -1 Failed 77 */ 78 int WriteDouble(Context *context, double d); 79 80 /** 81 * @Description Write data of the char type 82 * 83 * @param context - Communication Context 84 * @param c - char 85 * @return int - 0 Success; -1 Failed 86 */ 87 int WriteChar(Context *context, char c); 88 89 /** 90 * @Description Write data of a string type 91 * 92 * @param context - Communication Context 93 * @param str - string 94 * @return int - 0 Success; -1 Failed 95 */ 96 int WriteStr(Context *context, const char *str); 97 98 /** 99 * @Description Write data of the unsigned char* type 100 * 101 * @param context - Communication Context 102 * @param uStr - unsigned char 103 * @param len - String length 104 * @return int - 0 Success; -1 Failed 105 */ 106 int WriteUStr(Context *context, const unsigned char *uStr, unsigned int len); 107 108 /** 109 * @Description Write the message end flag to the context 110 * 111 * @param context - Communication Context 112 * @return int - 0 Success; -1 Failed 113 */ 114 int WriteEnd(Context *context); 115 116 /** 117 * @Description Read the function to be called from the context 118 * 119 * @param context - communication context 120 * @param funcName - the string of the function name 121 * @param count - size of funcName 122 * @return int - 0 success; < 0 read error; > 0 the message is longger than count, return the length 123 */ 124 int ReadFunc(Context *context, char *funcName, int count); 125 126 /** 127 * @Description Reads data of the int type from the context 128 * 129 * @param context - Communication Context 130 * @param i - pointer to an integer 131 * @return int - 0 Success; < 0 read error 132 */ 133 int ReadInt(Context *context, int *i); 134 135 /** 136 * @Description Reads data of the long type from the context 137 * 138 * @param context - Communication Context 139 * @param L - pointer to a long 140 * @return int - 0 Success; < 0 read error 141 */ 142 int ReadLong(Context *context, long *L); 143 144 /** 145 * @Description Reads data of the int64 type from the context 146 * 147 * @param context - Communication Context 148 * @param i - pointer to a int64 149 * @return int - 0 Success; < 0 read error 150 */ 151 int ReadInt64(Context *context, int64_t *i); 152 153 /** 154 * @Description Reads data of the double type from the context 155 * 156 * @param context - Communication Context 157 * @param d - pointer to a double 158 * @return int - 0 Success; < 0 read error 159 */ 160 int ReadDouble(Context *context, double *d); 161 162 /** 163 * @Description Reads data of the char type from the context 164 * 165 * @param context - Communication Context 166 * @param c - pointer to a char 167 * @return int - 0 Success; < 0 read error 168 */ 169 int ReadChar(Context *context, char *c); 170 171 /** 172 * @Description Reads data of the char* type from the context 173 * 174 * @param context - Communication Context 175 * @param str - pointer to the string 176 * @param count - Size of str 177 * @return int - 0 Success; < 0 read error; > 0 the message is longger than count, return the length 178 */ 179 int ReadStr(Context *context, char *str, int count); 180 181 /** 182 * @Description Reads data of the unsigned char* type from the context 183 * 184 * @param context - Communication Context 185 * @param uStr - pointer to the unsigned string 186 * @param count - Size of uStr 187 * @return int - 0 Success; < 0 read error; > 0 the message is longger than count, return the length 188 */ 189 int ReadUStr(Context *context, unsigned char *uStr, int count); 190 191 #ifdef __cplusplus 192 } 193 #endif 194 #endif 195