ziyan_platform.h 15.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
/**
 ********************************************************************
 * @file    ziyan_platform.h
 * @brief   This is the header file for "ziyan_platform.c", defining the structure and
 * (exported) function prototypes.
 *
 * @copyright (c) 2021 ZIYAN. All rights reserved.
 *
 * All information contained herein is, and remains, the property of ZIYAN.
 * The intellectual and technical concepts contained herein are proprietary
 * to ZIYAN and may be covered by U.S. and foreign patents, patents in process,
 * and protected by trade secret or copyright law.  Dissemination of this
 * information, including but not limited to data and other proprietary
 * material(s) incorporated within the information, in any form, is strictly
 * prohibited without the express written consent of ZIYAN.
 *
 * If you receive this source code without ZIYAN’s authorization, you may not
 * further disseminate the information, and you must immediately remove the
 * source code and notify ZIYAN of its removal. ZIYAN reserves the right to pursue
 * legal actions against you for any loss(es) or damage(s) caused by your
 * failure to do so.
 *
 *********************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef ZIYAN_PLATFORM_H
#define ZIYAN_PLATFORM_H

/* Includes ------------------------------------------------------------------*/
#include "ziyan_typedef.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Exported constants --------------------------------------------------------*/

/* Exported types ------------------------------------------------------------*/
/**
* @brief Platform handle of uart operation.
*/
typedef void *T_ZiyanUartHandle;

/**
* @brief Platform handle of usb bulk operation.
*/
typedef void *T_ZiyanUsbBulkHandle;

/**
* @brief Platform handle of network operation.
*/
typedef void *T_ZiyanNetworkHandle;

/**
* @brief Platform handle of i2c device operation.
*/
typedef void *T_ZiyanI2cHandle;

/**
* @brief Platform handle of thread task operation.
*/
typedef void *T_ZiyanTaskHandle;

/**
* @brief Platform handle of mutex operation.
*/
typedef void *T_ZiyanMutexHandle;

/**
* @brief Platform handle of semaphore operation.
*/
typedef void *T_ZiyanSemaHandle;

/**
* @brief Platform handle of file operation.
*/
typedef void *T_ZiyanFileHandle;

/**
* @brief Platform handle of dir operation.
*/
typedef void *T_ZiyanDirHandle;

/**
* @brief Platform handle of socket operation.
*/
typedef void *T_ZiyanSocketHandle;

typedef enum {
    /**
    * All aircraft type support,users can connect via chip serial port or USB to TTL serial port.
    * Baud rate support list on M300 RTK Payload Port: 115200, 230400, 460800, 921600.
    * Baud rate support list on M300 RTK Extension Port: 115200, 230400, 460800, 921600, 1000000.
    * Baud rate support list on M30/M30T: 115200, 230400, 460800, 921600, 1000000.
    * Baud rate support list on M3E/M3T: 921600.
    * Baud rate support list on M350 RTK Payload Port: 115200, 230400, 460800, 921600.
    * Baud rate support list on M350 RTK Extension Port: 115200, 230400, 460800, 921600, 1000000.
    * */
    ZIYAN_HAL_UART_NUM_0,
    /**
    * Only support on M300/M350 RTK Extension Port by USB virtual serial port, such as /dev/ttyACM0.
    * Baud rate support list on M300 RTK Extension Port: 921600.
    * Baud rate support list on M350 RTK Extension Port: 921600.
    * */
    ZIYAN_HAL_UART_NUM_1,
} E_ZiyanHalUartNum;

typedef enum {
    ZIYAN_HAL_USB_BULK_NUM_0 = 0,
    ZIYAN_HAL_USB_BULK_NUM_1,
    ZIYAN_HAL_USB_BULK_NUM_MAX,
} E_ZiyanHalUsbBulkNum;

typedef enum {
    ZIYAN_SOCKET_MODE_UDP,
    ZIYAN_SOCKET_MODE_TCP,
} E_ZiyanSocketMode;

typedef struct {
    bool isConnect;
} T_ZiyanUartStatus;

typedef struct {
    uint16_t year;
    uint8_t month;
    uint8_t day;
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
} T_ZiyanTime;

typedef struct {
    uint32_t size;
    T_ZiyanTime createTime;
    T_ZiyanTime modifyTime;
    char path[ZIYAN_FILE_PATH_SIZE_MAX];
    bool isDir;
} T_ZiyanFileInfo;

typedef struct {
    T_ZiyanReturnCode (*UartInit)(E_ZiyanHalUartNum uartNum, uint32_t baudRate, T_ZiyanUartHandle *uartHandle);

    T_ZiyanReturnCode (*UartDeInit)(T_ZiyanUartHandle uartHandle);

    T_ZiyanReturnCode (*UartWriteData)(T_ZiyanUartHandle uartHandle, const uint8_t *buf, uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*UartReadData)(T_ZiyanUartHandle uartHandle, uint8_t *buf, uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*UartGetStatus)(E_ZiyanHalUartNum uartNum, T_ZiyanUartStatus *status);
} T_ZiyanHalUartHandler;

typedef struct {
    uint16_t interfaceNum;
    uint16_t endPointIn;
    uint16_t endPointOut;
} T_ZiyanHalUsbBulkChannelInfo;

typedef struct {
    bool isUsbHost;
    // attention: if 'isUsbHost' equals false, the following parameters is not valid.
    uint16_t pid;
    uint16_t vid;
    T_ZiyanHalUsbBulkChannelInfo channelInfo;
} T_ZiyanHalUsbBulkInfo;

typedef struct {
    uint16_t pid;
    uint16_t vid;
    T_ZiyanHalUsbBulkChannelInfo channelInfo[ZIYAN_HAL_USB_BULK_NUM_MAX];
} T_ZiyanHalUsbBulkDeviceInfo;

typedef struct {
    struct {
        uint16_t vid;
        uint16_t pid;
    } usbNetAdapter;
} T_ZiyanHalNetworkDeviceInfo;

typedef struct {
    T_ZiyanReturnCode (*UsbBulkInit)(T_ZiyanHalUsbBulkInfo usbBulkInfo, T_ZiyanUsbBulkHandle *usbBulkHandle);

    T_ZiyanReturnCode (*UsbBulkDeInit)(T_ZiyanUsbBulkHandle usbBulkHandle);

    T_ZiyanReturnCode (*UsbBulkWriteData)(T_ZiyanUsbBulkHandle usbBulkHandle, const uint8_t *buf,
                                        uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*UsbBulkReadData)(T_ZiyanUsbBulkHandle usbBulkHandle, uint8_t *buf,
                                       uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*UsbBulkGetDeviceInfo)(T_ZiyanHalUsbBulkDeviceInfo *deviceInfo);
} T_ZiyanHalUsbBulkHandler;

typedef struct {
    T_ZiyanReturnCode (*NetworkInit)(const char *ipAddr, const char *netMask, T_ZiyanNetworkHandle *networkHandle);

    T_ZiyanReturnCode (*NetworkDeInit)(T_ZiyanNetworkHandle networkHandle);

    T_ZiyanReturnCode (*NetworkGetDeviceInfo)(T_ZiyanHalNetworkDeviceInfo *deviceInfo);
} T_ZiyanHalNetworkHandler;

typedef struct {
    uint32_t i2cSpeed;
    uint16_t devAddress;
} T_ZiyanHalI2cConfig;

typedef struct {
    T_ZiyanReturnCode (*I2cInit)(T_ZiyanHalI2cConfig i2cConfig, T_ZiyanI2cHandle *i2cHandle);

    T_ZiyanReturnCode (*I2cDeInit)(T_ZiyanI2cHandle *i2cHandle);

    T_ZiyanReturnCode (*I2cWriteData)(T_ZiyanI2cHandle *i2cHandle, uint16_t devAddress, const uint8_t *buf,
                                    uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*I2cReadData)(T_ZiyanI2cHandle *i2cHandle, uint16_t devAddress, uint8_t *buf,
                                   uint32_t len, uint32_t *realLen);
} T_ZiyanHalI2cHandler;

typedef struct {
    T_ZiyanReturnCode (*TaskCreate)(const char *name, void *(*taskFunc)(void *),
                                  uint32_t stackSize, void *arg, T_ZiyanTaskHandle *task);

    T_ZiyanReturnCode (*TaskDestroy)(T_ZiyanTaskHandle task);

    T_ZiyanReturnCode (*TaskSleepMs)(uint32_t timeMs);

    T_ZiyanReturnCode (*MutexCreate)(T_ZiyanMutexHandle *mutex);

    T_ZiyanReturnCode (*MutexDestroy)(T_ZiyanMutexHandle mutex);

    T_ZiyanReturnCode (*MutexLock)(T_ZiyanMutexHandle mutex);

    T_ZiyanReturnCode (*MutexUnlock)(T_ZiyanMutexHandle mutex);

    T_ZiyanReturnCode (*SemaphoreCreate)(uint32_t initValue, T_ZiyanSemaHandle *semaphore);

    T_ZiyanReturnCode (*SemaphoreDestroy)(T_ZiyanSemaHandle semaphore);

    T_ZiyanReturnCode (*SemaphoreWait)(T_ZiyanSemaHandle semaphore);

    T_ZiyanReturnCode (*SemaphoreTimedWait)(T_ZiyanSemaHandle semaphore, uint32_t waitTimeMs);

    T_ZiyanReturnCode (*SemaphorePost)(T_ZiyanSemaHandle semaphore);

    T_ZiyanReturnCode (*GetTimeMs)(uint32_t *ms);

    T_ZiyanReturnCode (*GetTimeUs)(uint64_t *us);

    T_ZiyanReturnCode (*GetRandomNum)(uint16_t *randomNum);

    void *(*Malloc)(uint32_t size);

    void (*Free)(void *ptr);
} T_ZiyanOsalHandler;

typedef struct {
    T_ZiyanReturnCode (*FileOpen)(const char *fileName, const char *fileMode, T_ZiyanFileHandle *fileObj);

    T_ZiyanReturnCode (*FileClose)(T_ZiyanFileHandle fileObj);

    T_ZiyanReturnCode (*FileWrite)(T_ZiyanFileHandle fileObj, const uint8_t *buf, uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*FileRead)(T_ZiyanFileHandle fileObj, uint8_t *buf, uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*FileSeek)(T_ZiyanFileHandle fileObj, uint32_t offset);

    T_ZiyanReturnCode (*FileSync)(T_ZiyanFileHandle fileObj);

    T_ZiyanReturnCode (*DirOpen)(const char *filePath, T_ZiyanDirHandle *dirObj);

    T_ZiyanReturnCode (*DirClose)(T_ZiyanDirHandle dirObj);

    T_ZiyanReturnCode (*DirRead)(T_ZiyanDirHandle dirObj, T_ZiyanFileInfo *fileInfo);

    T_ZiyanReturnCode (*Mkdir)(const char *filePath);

    T_ZiyanReturnCode (*Unlink)(const char *filePath);

    T_ZiyanReturnCode (*Rename)(const char *oldFilePath, const char *newFilePath);

    T_ZiyanReturnCode (*Stat)(const char *filePath, T_ZiyanFileInfo *fileInfo);
} T_ZiyanFileSystemHandler;

typedef struct {
    T_ZiyanReturnCode (*Socket)(E_ZiyanSocketMode mode, T_ZiyanSocketHandle *socketHandle);

    T_ZiyanReturnCode (*Close)(T_ZiyanSocketHandle socketHandle);

    T_ZiyanReturnCode (*Bind)(T_ZiyanSocketHandle socketHandle, const char *ipAddr, uint32_t port);

    T_ZiyanReturnCode (*UdpSendData)(T_ZiyanSocketHandle socketHandle, const char *ipAddr, uint32_t port,
                                   const uint8_t *buf, uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*UdpRecvData)(T_ZiyanSocketHandle socketHandle, char *ipAddr, uint32_t *port,
                                   uint8_t *buf, uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*TcpListen)(T_ZiyanSocketHandle socketHandle);

    T_ZiyanReturnCode (*TcpAccept)(T_ZiyanSocketHandle socketHandle, char *ipAddr, uint32_t *port,
                                 T_ZiyanSocketHandle *outSocketHandle);

    T_ZiyanReturnCode (*TcpConnect)(T_ZiyanSocketHandle socketHandle, const char *ipAddr, uint32_t port);

    T_ZiyanReturnCode (*TcpSendData)(T_ZiyanSocketHandle socketHandle,
                                   const uint8_t *buf, uint32_t len, uint32_t *realLen);

    T_ZiyanReturnCode (*TcpRecvData)(T_ZiyanSocketHandle socketHandle,
                                   uint8_t *buf, uint32_t len, uint32_t *realLen);
} T_ZiyanSocketHandler;

/* Exported functions --------------------------------------------------------*/
/**
 * @brief Register the handler for hal uart interfaces by your platform.
 * @note It should be noted that the interface in hal is written and tested well. Users need to implement all the
 * interfaces. Otherwise, the user interface cannot be successfully registered, and then the user interface is registered
 * through the interface. If the registration fails, it needs to be based on the return code. To judge the problem. Make
 * sure that the feature is available after a successful registration. The interface needs to be called at the beginning of
 * the application for registration, otherwise, the subsequent functions will not work properly.
 * @param halUartHandler: pointer to the handler for hal uart interfaces by your platform.
 * @return Execution result.
 */
T_ZiyanReturnCode ZiyanPlatform_RegHalUartHandler(const T_ZiyanHalUartHandler *halUartHandler);

/**
 * @brief Register the handler for usb bulk interfaces by your platform.
 * @param fileSystemHandler: pointer to the handler for usb bulk interfaces by your platform.
 * @return Execution result.
 */
T_ZiyanReturnCode ZiyanPlatform_RegHalUsbBulkHandler(const T_ZiyanHalUsbBulkHandler *halUsbBulkHandler);

/**
 * @brief Register the handler for hal network interfaces by your platform.
 * @note It should be noted that the interface in hal is written and tested well. Users need to implement all the
 * interfaces. Otherwise, the user interface cannot be successfully registered, and then the user interface is registered
 * through the interface. If the registration fails, it needs to be based on the return code. To judge the problem. Make
 * sure that the feature is available after a successful registration.
 * @attention The interface needs to be called at the beginning of the application for registration, otherwise, the
 * subsequent functions will not work properly.
 * @param osalHandler: pointer to the handler for network handler interfaces by your platform.
 * @return Execution result.
 */
T_ZiyanReturnCode ZiyanPlatform_RegHalNetworkHandler(const T_ZiyanHalNetworkHandler *halNetworkHandler);

/**
 * @brief Register the handler for hal i2c master mode interfaces by your platform.
 * @note It should be noted that the interface in hal is written and tested well. Users need to implement all the
 * interfaces. Otherwise, the user interface cannot be successfully registered, and then the user interface is registered
 * through the interface. If the registration fails, it needs to be based on the return code. To judge the problem. Make
 * sure that the feature is available after a successful registration.
 * @attention The interface needs to be called at the beginning of the application for registration, otherwise, the
 * subsequent functions will not work properly.
 * @param halI2cHandler: pointer to the handler for hal i2c handler interfaces by your platform.
 * @return Execution result.
 */
T_ZiyanReturnCode ZiyanPlatform_RegHalI2cHandler(const T_ZiyanHalI2cHandler *halI2cHandler);

/**
 * @brief Register the handler for osal interfaces by your platform.
 * @note It should be noted that the interface in osal is written and tested well. Users need to implement all the
 * interfaces. Otherwise, the user interface cannot be successfully registered, and then the user interface is registered
 * through the interface. If the registration fails, it needs to be based on the return code. To judge the problem. Make
 * sure that the feature is available after a successful registration. The interface needs to be called at the beginning of
 * the application for registration, otherwise, the subsequent functions will not work properly.
 * @param osalHandler: pointer to the handler for osal interfaces by your platform.
 * @return Execution result.
 */
T_ZiyanReturnCode ZiyanPlatform_RegOsalHandler(const T_ZiyanOsalHandler *osalHandler);

/**
 * @brief Register the handler for file-system interfaces by your platform.
 * @param fileSystemHandler: pointer to the handler for file-system interfaces by your platform.
 * @return Execution result.
 */
T_ZiyanReturnCode ZiyanPlatform_RegFileSystemHandler(const T_ZiyanFileSystemHandler *fileSystemHandler);

/**
 * @brief Register the handler for socket interfaces by your platform.
 * @param fileSystemHandler: pointer to the handler for socket interfaces by your platform.
 * @return Execution result.
 */
T_ZiyanReturnCode ZiyanPlatform_RegSocketHandler(const T_ZiyanSocketHandler *socketHandler);

/**
 * @brief Get the handler of osal interfaces.
 * @return Pointer to osal handler.
 */
T_ZiyanOsalHandler *ZiyanPlatform_GetOsalHandler(void);

/**
 * @brief Get the handler of usb bulk interfaces.
 * @return Pointer to usb bulk handler.
 */
T_ZiyanHalUsbBulkHandler *ZiyanPlatform_GetHalUsbBulkHandler(void);

/**
 * @brief Get the handler of network interfaces.
 * @return Pointer to network handler.
 */
T_ZiyanHalNetworkHandler *ZiyanPlatform_GetHalNetworkHandler(void);

/**
 * @brief Get the handler of file-system interfaces.
 * @return Pointer to file-system handler.
 */
T_ZiyanFileSystemHandler *ZiyanPlatform_GetFileSystemHandler(void);

/**
 * @brief Get the handler of socket interfaces.
 * @return Pointer to socket handler.
 */
T_ZiyanSocketHandler *ZiyanPlatform_GetSocketHandler(void);

#ifdef __cplusplus
}
#endif

#endif // ZIYAN_PLATFORM_H
/************************ (C) COPYRIGHT ZIYAN Innovations *******END OF FILE******/