openHiTLS API openHiTLS 0.1.0-Alpha1
bsl_sal.h
浏览该文件的文档.
1/*---------------------------------------------------------------------------------------------
2 * This file is part of the openHiTLS project.
3 * Copyright © 2023 Huawei Technologies Co.,Ltd. All rights reserved.
4 * Licensed under the openHiTLS Software license agreement 1.0. See LICENSE in the project root
5 * for license information.
6 *---------------------------------------------------------------------------------------------
7 */
8
15#ifndef BSL_SAL_H
16#define BSL_SAL_H
17
18#include <stdint.h>
19#include <stddef.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
30typedef struct MemCallback {
41 void *(*pfMalloc)(uint32_t size);
42
51 void (*pfFree)(void *addr);
53
60
66typedef void *BSL_SAL_ThreadId;
67
73typedef void *BSL_SAL_Mutex;
74
80typedef void *BSL_SAL_CondVar;
81
87typedef struct ThreadCallback {
101
111
124
137
150
159 uint64_t (*pfThreadGetId)(void);
161
175
191
203void *BSL_SAL_Malloc(uint32_t size);
204
217void *BSL_SAL_Calloc(uint32_t num, uint32_t size);
218
228void *BSL_SAL_Dump(const void *src, uint32_t size);
229
239void BSL_SAL_Free(void *value);
240
254void *BSL_SAL_Realloc(void *addr, uint32_t newSize, uint32_t oldSize);
255
263void BSL_SAL_CleanseData(void *ptr, uint32_t size);
264
272void BSL_SAL_ClearFree(void *ptr, uint32_t size);
273
274#define BSL_SAL_FREE(value_) \
275 do { \
276 if ((value_) != NULL) { \
277 BSL_SAL_Free((void *)(value_)); \
278 (value_) = NULL; \
279 } \
280 } while (0)
281
282#define BSL_SAL_ONCE_INIT 0 // equal to PTHREAD_ONCE_INIT, the pthread symbol is masked.
283
298
312
326
340
351
361uint64_t BSL_SAL_ThreadGetId(void);
362
370typedef void (*BSL_SAL_ThreadInitRoutine)(void);
371
385int32_t BSL_SAL_ThreadRunOnce(uint32_t *onceControl, BSL_SAL_ThreadInitRoutine initFunc);
386
401int32_t BSL_SAL_ThreadCreate(BSL_SAL_ThreadId *thread, void *(*startFunc)(void *), void *arg);
402
413
427
441
456int32_t BSL_SAL_CondTimedwaitMs(BSL_SAL_Mutex condMutex, BSL_SAL_CondVar condVar, int32_t timeout);
457
471
472typedef void *bsl_sal_file_handle; // Pointer to file handle
473
488int32_t BSL_SAL_FileOpen(bsl_sal_file_handle *stream, const char *path, const char *mode);
489
501
518int32_t BSL_SAL_FileRead(bsl_sal_file_handle stream, void *buffer, size_t size, size_t num, size_t *len);
519
535int32_t BSL_SAL_FileWrite(bsl_sal_file_handle stream, const void *buffer, size_t size, size_t num);
536
550int32_t BSL_SAL_FileLength(const char *path, size_t *len);
551
556typedef struct {
557 uint16_t year;
558 uint8_t month;
559 uint8_t day;
560 uint8_t hour;
561 uint8_t minute;
562 uint16_t millSec;
563 uint8_t second;
564 uint8_t utcSign;
565 uint8_t utcHour;
566 uint8_t utcMinute;
567 uint32_t microSec;
568} BSL_TIME;
569
574typedef int64_t BslUnixTime;
575
582typedef BslUnixTime (*BslTimeFunc)(void);
583
600
614uint32_t BSL_SAL_DateTimeCompare(const BSL_TIME *dateA, const BSL_TIME *dateB, int64_t *diffSec);
615
628uint32_t BSL_SAL_SysTimeGet(BSL_TIME *sysTime);
629
639
652uint32_t BSL_SAL_DateToUtcTimeConvert(const BSL_TIME *dateTime, int64_t *utcTime);
653
666uint32_t BSL_SAL_UtcTimeToDateConvert(int64_t utcTime, BSL_TIME *sysTime);
667
683uint32_t BSL_SAL_DateTimeCompareByUs(const BSL_TIME *dateA, const BSL_TIME *dateB);
684
694void BSL_SAL_Sleep(uint32_t time);
695
705long BSL_SAL_Tick(void);
706
717
723typedef void *BSL_SAL_SockAddr;
724
738int32_t BSL_SAL_Socket(int32_t af, int32_t type, int32_t protocol);
739
751int32_t BSL_SAL_SockClose(int32_t sockId);
752
768int32_t BSL_SAL_SetSockopt(int32_t sockId, int32_t level, int32_t name, const void *val, uint32_t len);
769
782int32_t BSL_SAL_SockListen(int32_t sockId, int32_t backlog);
783
797int32_t BSL_SAL_SockBind(int32_t sockId, BSL_SAL_SockAddr addr, size_t len);
798
812int32_t BSL_SAL_SockConnect(int32_t sockId, BSL_SAL_SockAddr addr, size_t len);
813
829int32_t BSL_SAL_SockSend(int32_t sockId, const void *msg, size_t len, int32_t flags);
830
846int32_t BSL_SAL_SockRecv(int32_t sockfd, void *buff, size_t len, int32_t flags);
847
864int32_t BSL_SAL_Select(int32_t nfds, void *readfds, void *writefds, void *exceptfds, void *timeout);
865
879int32_t BSL_SAL_Ioctlsocket(int32_t sockId, long cmd, unsigned long *arg);
880
891
905int32_t BSL_SAL_StrcaseCmp(const char *str1, const char *str2);
906
920void *BSL_SAL_Memchr(const char *str, int32_t character, size_t count);
921
933int32_t BSL_SAL_Atoi(const char *str);
934
948uint32_t BSL_SAL_Strnlen(const char *string, uint32_t count);
949
950#ifdef __cplusplus
951}
952#endif
953
954#endif // BSL_SAL_H
void * BSL_SAL_SockAddr
Definition bsl_sal.h:723
void * bsl_sal_file_handle
Definition bsl_sal.h:472
int32_t(* pfThreadUnlock)(BSL_SAL_ThreadLockHandle lock)
Unlock
Definition bsl_sal.h:149
int32_t BSL_SAL_FileRead(bsl_sal_file_handle stream, void *buffer, size_t size, size_t num, size_t *len)
Read the file.
int32_t BSL_SAL_ThreadWriteLock(BSL_SAL_ThreadLockHandle lock)
Lock the write operation.
int32_t BSL_SAL_ThreadCreate(BSL_SAL_ThreadId *thread, void *(*startFunc)(void *), void *arg)
Create a thread.
void BSL_SAL_SysTimeFuncReg(BslTimeFunc func)
Interface for registering the function for obtaining the system time You can use this API to register...
uint32_t BSL_SAL_DateTimeCompareByUs(const BSL_TIME *dateA, const BSL_TIME *dateB)
Compare two dates, accurate to microseconds.
int32_t BSL_SAL_ThreadUnlock(BSL_SAL_ThreadLockHandle lock)
Unlock
int32_t BSL_SAL_SockGetLastSocketError(void)
Obtain the last error corresponding to the socket.
int32_t BSL_SAL_SockBind(int32_t sockId, BSL_SAL_SockAddr addr, size_t len)
Binding a socket
int32_t(* pfThreadReadLock)(BSL_SAL_ThreadLockHandle lock)
Lock the read operation.
Definition bsl_sal.h:123
void * BSL_SAL_CondVar
Definition bsl_sal.h:80
int32_t BSL_SAL_FileLength(const char *path, size_t *len)
Obtain the file length.
int32_t BSL_SAL_SockListen(int32_t sockId, int32_t backlog)
Listening socket
int32_t BSL_SAL_CondSignal(BSL_SAL_CondVar condVar)
The waiting time ends or the signal is obtained.
uint64_t(* pfThreadGetId)(void)
Obtain the thread ID.
Definition bsl_sal.h:159
int32_t BSL_SAL_ThreadRunOnce(uint32_t *onceControl, BSL_SAL_ThreadInitRoutine initFunc)
Execute only once.
int32_t BSL_SAL_ThreadLockNew(BSL_SAL_ThreadLockHandle *lock)
Create a thread lock.
void BSL_SAL_CleanseData(void *ptr, uint32_t size)
Set sensitive information to zero.
int32_t BSL_SAL_DeleteCondVar(BSL_SAL_CondVar condVar)
Delete a condition variable.
int32_t(* pfThreadLockNew)(BSL_SAL_ThreadLockHandle *lock)
Create a thread lock.
Definition bsl_sal.h:100
BslUnixTime(* BslTimeFunc)(void)
Prototype of the callback function for obtaining the time
Definition bsl_sal.h:582
int64_t BslUnixTime
Unix Time structure definition.
Definition bsl_sal.h:574
void BSL_SAL_ThreadLockFree(BSL_SAL_ThreadLockHandle lock)
Release the thread lock.
void BSL_SAL_FileClose(bsl_sal_file_handle stream)
Close the file.
int32_t BSL_SAL_Select(int32_t nfds, void *readfds, void *writefds, void *exceptfds, void *timeout)
Check the socket descriptor.
void * BSL_SAL_Malloc(uint32_t size)
Allocate memory space.
uint32_t BSL_SAL_SysTimeGet(BSL_TIME *sysTime)
Obtain the system time.
int32_t BSL_SAL_SetSockopt(int32_t sockId, int32_t level, int32_t name, const void *val, uint32_t len)
Set the socket
int32_t BSL_SAL_FileOpen(bsl_sal_file_handle *stream, const char *path, const char *mode)
Open a file.
int32_t BSL_SAL_StrcaseCmp(const char *str1, const char *str2)
String comparison
long BSL_SAL_Tick(void)
Obtain the number of ticks that the system has experienced since startup.
void * BSL_SAL_Calloc(uint32_t num, uint32_t size)
Allocate and clear the memory space.
int32_t BSL_SAL_CreateCondVar(BSL_SAL_CondVar *condVar)
Create a condition variable.
int32_t BSL_SAL_SockConnect(int32_t sockId, BSL_SAL_SockAddr addr, size_t len)
Initiate a connection.
int32_t BSL_SAL_RegThreadCallback(BSL_SAL_ThreadCallback *cb)
Interface for registering thread-related callback functions.
uint32_t BSL_SAL_UtcTimeToDateConvert(int64_t utcTime, BSL_TIME *sysTime)
Convert the date in the BslUnixTime format to the BslSysTime format.
void * BSL_SAL_Realloc(void *addr, uint32_t newSize, uint32_t oldSize)
Memory expansion
void BSL_SAL_Free(void *value)
Release the specified memory.
struct MemCallback BSL_SAL_MemCallback
int32_t BSL_SAL_SockSend(int32_t sockId, const void *msg, size_t len, int32_t flags)
Send a message.
void * BSL_SAL_Memchr(const char *str, int32_t character, size_t count)
Search for the corresponding character position in a string.
void BSL_SAL_Sleep(uint32_t time)
Sleep the current thread
int32_t BSL_SAL_Ioctlsocket(int32_t sockId, long cmd, unsigned long *arg)
Device control interface function
int32_t BSL_SAL_Atoi(const char *str)
Convert string to number
uint32_t BSL_SAL_DateTimeCompare(const BSL_TIME *dateA, const BSL_TIME *dateB, int64_t *diffSec)
Compare Two Dates
int32_t BSL_SAL_ThreadReadLock(BSL_SAL_ThreadLockHandle lock)
Lock the read operation.
void BSL_SAL_ThreadClose(BSL_SAL_ThreadId thread)
Close the thread.
void(* BSL_SAL_ThreadInitRoutine)(void)
run once: Use the initialization callback.
Definition bsl_sal.h:370
struct ThreadCallback BSL_SAL_ThreadCallback
int32_t BSL_SAL_SockRecv(int32_t sockfd, void *buff, size_t len, int32_t flags)
Receive the message.
int32_t BSL_SAL_SockClose(int32_t sockId)
Close the socket
uint32_t BSL_SAL_DateToUtcTimeConvert(const BSL_TIME *dateTime, int64_t *utcTime)
Convert the date in the BslSysTime format to the UTC time format.
void * BSL_SAL_ThreadLockHandle
Definition bsl_sal.h:59
uint32_t BSL_SAL_Strnlen(const char *string, uint32_t count)
Obtain the length of a given string.
int32_t BSL_SAL_CondTimedwaitMs(BSL_SAL_Mutex condMutex, BSL_SAL_CondVar condVar, int32_t timeout)
The waiting time ends or the signal is obtained.
int32_t BSL_SAL_RegMemCallback(BSL_SAL_MemCallback *cb)
Interface for registering memory-related callback functions
void * BSL_SAL_Mutex
Definition bsl_sal.h:73
int32_t(* pfThreadWriteLock)(BSL_SAL_ThreadLockHandle lock)
Lock the write operation.
Definition bsl_sal.h:136
uint64_t BSL_SAL_ThreadGetId(void)
Obtain the thread ID.
void(* pfFree)(void *addr)
Reclaim a memory block allocated by pfMalloc.
Definition bsl_sal.h:51
int32_t BSL_SAL_FileWrite(bsl_sal_file_handle stream, const void *buffer, size_t size, size_t num)
Write a file
void BSL_SAL_ClearFree(void *ptr, uint32_t size)
Clear sensitive information and release memory.
int32_t BSL_SAL_Socket(int32_t af, int32_t type, int32_t protocol)
Socket creation interface
long BSL_SAL_TicksPerSec(void)
Obtain the number of system ticks per second.
void * BSL_SAL_Dump(const void *src, uint32_t size)
Duplicate the memory space.
void(* pfThreadLockFree)(BSL_SAL_ThreadLockHandle lock)
Release the thread lock.
Definition bsl_sal.h:110
void * BSL_SAL_ThreadId
Definition bsl_sal.h:66
BslUnixTime BSL_SAL_CurrentSysTimeGet(void)
Obtain the Unix time.
Definition bsl_sal.h:30
Definition bsl_sal.h:87
uint32_t microSec
Definition bsl_sal.h:567
uint8_t month
Definition bsl_sal.h:558
uint8_t utcSign
Definition bsl_sal.h:564
uint8_t utcMinute
Definition bsl_sal.h:566
uint16_t year
Definition bsl_sal.h:557
uint8_t day
Definition bsl_sal.h:559
uint8_t utcHour
Definition bsl_sal.h:565
uint8_t second
Definition bsl_sal.h:563
uint8_t minute
Definition bsl_sal.h:561
uint16_t millSec
Definition bsl_sal.h:562
uint8_t hour
Definition bsl_sal.h:560
Basic time data structure definition.
Definition bsl_sal.h:556