NEW: release DJI Payload-SDK version 3.0

Signed-off-by: DJI-Martin <DJI-Martin@dji.com>
This commit is contained in:
DJI-Martin
2021-11-12 16:27:03 +08:00
parent 2cce11a80b
commit e478e9a7a5
541 changed files with 2192778 additions and 2 deletions

View File

@ -0,0 +1,181 @@
#
# Find the native FFMPEG includes and library
#
# This module defines
# FFMPEG_INCLUDE_DIR, where to find avcodec.h, avformat.h ...
# FFMPEG_LIBRARIES, the libraries to link against to use FFMPEG.
# FFMPEG_FOUND, If false, do not try to use FFMPEG.
# also defined, but not for general use are
# FFMPEG_avformat_LIBRARY and FFMPEG_avcodec_LIBRARY, where to find the FFMPEG library.
# This is useful to do it this way so that we can always add more libraries
# if needed to FFMPEG_LIBRARIES if ffmpeg ever changes...
# if ffmpeg headers are all in one directory
FIND_PATH(FFMPEG_INCLUDE_DIR avformat.h
PATHS
$ENV{FFMPEG_DIR}/include
$ENV{OSGDIR}/include
$ENV{OSG_ROOT}/include
~/Library/Frameworks
/Library/Frameworks
/usr/local/include
/usr/include
/sw/include # Fink
/opt/local/include # DarwinPorts
/opt/csw/include # Blastwave
/opt/include
/usr/freeware/include
PATH_SUFFIXES ffmpeg
DOC "Location of FFMPEG Headers"
)
# if ffmpeg headers are seperated to each of libavformat, libavcodec etc..
IF( NOT FFMPEG_INCLUDE_DIR )
FIND_PATH(FFMPEG_INCLUDE_DIR libavformat/avformat.h
PATHS
$ENV{FFMPEG_DIR}/include
$ENV{OSGDIR}/include
$ENV{OSG_ROOT}/include
~/Library/Frameworks
/Library/Frameworks
/usr/local/include
/usr/include
/sw/include # Fink
/opt/local/include # DarwinPorts
/opt/csw/include # Blastwave
/opt/include
/usr/freeware/include
PATH_SUFFIXES ffmpeg
DOC "Location of FFMPEG Headers"
)
ENDIF( NOT FFMPEG_INCLUDE_DIR )
# we want the -I include line to use the parent directory of ffmpeg as
# ffmpeg uses relative includes such as <ffmpeg/avformat.h> or <libavcodec/avformat.h>
get_filename_component(FFMPEG_INCLUDE_DIR ${FFMPEG_INCLUDE_DIR} ABSOLUTE)
FIND_LIBRARY(FFMPEG_avformat_LIBRARY avformat
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_avcodec_LIBRARY avcodec
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_avutil_LIBRARY avutil
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_vorbis_LIBRARY vorbis
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_dc1394_LIBRARY dc1394_control
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_vorbisenc_LIBRARY vorbisenc
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_theora_LIBRARY theora
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_dts_LIBRARY dts
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_gsm_LIBRARY gsm
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_swscale_LIBRARY swscale
/usr/local/lib
/usr/lib
)
FIND_LIBRARY(FFMPEG_z_LIBRARY z
/usr/local/lib
/usr/lib
)
SET(FFMPEG_LIBRARIES)
IF(FFMPEG_INCLUDE_DIR)
IF(FFMPEG_avformat_LIBRARY)
IF(FFMPEG_avcodec_LIBRARY)
IF(FFMPEG_avutil_LIBRARY)
SET( FFMPEG_FOUND "YES" )
SET( FFMPEG_BASIC_LIBRARIES
${FFMPEG_avcodec_LIBRARY}
${FFMPEG_avformat_LIBRARY}
${FFMPEG_avutil_LIBRARY}
)
# swscale is always a part of newer ffmpeg distros
IF(FFMPEG_swscale_LIBRARY)
LIST(APPEND FFMPEG_BASIC_LIBRARIES ${FFMPEG_swscale_LIBRARY})
ENDIF(FFMPEG_swscale_LIBRARY)
SET(FFMPEG_LIBRARIES ${FFMPEG_BASIC_LIBRARIES})
IF(FFMPEG_vorbis_LIBRARY)
LIST(APPEND FFMPEG_LIBRARIES ${FFMPEG_vorbis_LIBRARY})
ENDIF(FFMPEG_vorbis_LIBRARY)
IF(FFMPEG_dc1394_LIBRARY)
LIST(APPEND FFMPEG_LIBRARIES ${FFMPEG_dc1394_LIBRARY})
ENDIF(FFMPEG_dc1394_LIBRARY)
IF(FFMPEG_vorbisenc_LIBRARY)
LIST(APPEND FFMPEG_LIBRARIES ${FFMPEG_vorbisenc_LIBRARY})
ENDIF(FFMPEG_vorbisenc_LIBRARY)
IF(FFMPEG_theora_LIBRARY)
LIST(APPEND FFMPEG_LIBRARIES ${FFMPEG_theora_LIBRARY})
ENDIF(FFMPEG_theora_LIBRARY)
IF(FFMPEG_dts_LIBRARY)
LIST(APPEND FFMPEG_LIBRARIES ${FFMPEG_dts_LIBRARY})
ENDIF(FFMPEG_dts_LIBRARY)
IF(FFMPEG_gsm_LIBRARY)
LIST(APPEND FFMPEG_LIBRARIES ${FFMPEG_gsm_LIBRARY})
ENDIF(FFMPEG_gsm_LIBRARY)
IF(FFMPEG_z_LIBRARY)
LIST(APPEND FFMPEG_LIBRARIES ${FFMPEG_z_LIBRARY})
ENDIF(FFMPEG_z_LIBRARY)
SET(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE INTERNAL "All presently found FFMPEG libraries.")
ENDIF(FFMPEG_avutil_LIBRARY)
ENDIF(FFMPEG_avcodec_LIBRARY)
ENDIF(FFMPEG_avformat_LIBRARY)
ENDIF(FFMPEG_INCLUDE_DIR)
MARK_AS_ADVANCED(
FFMPEG_INCLUDE_DIR
FFMPEG_avformat_LIBRARY
FFMPEG_avcodec_LIBRARY
FFMPEG_avutil_LIBRARY
FFMPEG_vorbis_LIBRARY
FFMPEG_dc1394_LIBRARY
FFMPEG_vorbisenc_LIBRARY
FFMPEG_theora_LIBRARY
FFMPEG_dts_LIBRARY
FFMPEG_gsm_LIBRARY
FFMPEG_swscale_LIBRARY
FFMPEG_z_LIBRARY
)

View File

@ -0,0 +1,482 @@
/**
********************************************************************
* @file sys_monitor.c
* @brief
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI reserves the right to pursue
* legal actions against you for any loss(es) or damage(s) caused by your
* failure to do so.
*
*********************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include "sys_monitor.h"
#include "dji_logger.h"
#include "utils/util_misc.h"
/* Private constants ---------------------------------------------------------*/
#define MONITOR_VMRSS_LINE 15
#define MONITOR_PROCESS_ITEM 14
#define MONITOR_CMD_BUF_SIZE 512
/* Private types -------------------------------------------------------------*/
/* Private functions declaration ---------------------------------------------*/
static const char *Monitor_GetItems(const char *buffer, int ie);
/* Private variables ---------------------------------------------------------*/
/* Exported functions definition ---------------------------------------------*/
int Monitor_GetPhyMem(pid_t p)
{
int i;
char file[64] = {0};
FILE *fd;
char lineBuf[256] = {0};
char name[32];
int vmrss = 0; //memory peak
char *ret = NULL;
sprintf(file, "/proc/%d/status", (int) p);
fd = fopen(file, "r");
if (fd == NULL) {
USER_LOG_ERROR("open file fail.");
return 0;
}
for (i = 0; i < MONITOR_VMRSS_LINE - 1; i++) {
ret = fgets(lineBuf, sizeof(lineBuf), fd);
USER_UTIL_UNUSED(ret);
}
ret = fgets(lineBuf, sizeof(lineBuf), fd);
if (ret == NULL)
goto out;
sscanf(lineBuf, "%31s %d", name, &vmrss);
out:
fclose(fd);
return vmrss;
}
int Monitor_GetTotalMem(void)
{
char *file = "/proc/meminfo";
FILE *fd;
char lineBuf[256] = {0};
char name[32];
int memtotal = 0;
char *ret = NULL;
fd = fopen(file, "r");
if (fd == NULL) {
USER_LOG_ERROR("open file fail.");
return 0;
}
ret = fgets(lineBuf, sizeof(lineBuf), fd);
if (ret == NULL)
goto out;
sscanf(lineBuf, "%31s %d", name, &memtotal);
out:
fclose(fd);
return memtotal;
}
float Monitor_GetPmem(pid_t p)
{
int phy = Monitor_GetPhyMem(p);
int total = Monitor_GetTotalMem();
float occupy = (float) ((phy * 1.0) / (total * 1.0));
return occupy;
}
unsigned int Monitor_GetCpuOccupyOfProcess(pid_t pid)
{
char file[64] = {0};
T_MonitorProcessCpuOccupy t = {0};
FILE *fd;
char lineBuf[1024] = {0};
char *q = NULL;
char *ret = NULL;
sprintf(file, "/proc/%d/stat", (int) pid);
fd = fopen(file, "r");
if (fd == NULL) {
USER_LOG_ERROR("open file fail.");
return 0;
}
ret = fgets(lineBuf, sizeof(lineBuf), fd);
if (ret == NULL)
goto out;
sscanf(lineBuf, "%u", (unsigned int *) &t.pid);
q = (char *) Monitor_GetItems(lineBuf, MONITOR_PROCESS_ITEM);
if (q == NULL) {
USER_LOG_ERROR("get item fail.");
goto out;
}
sscanf(q, "%u %u %u %u", &t.utime, &t.stime, &t.cutime, &t.cstime);
out:
fclose(fd);
return (t.utime + t.stime + t.cutime + t.cstime);
}
unsigned int Monitor_GetCpuOccupyOfThread(pid_t pid, pid_t tid)
{
char file[64] = {0};
T_MonitorProcessCpuOccupy t = {0};
FILE *fd;
char lineBuf[1024] = {0};
char *q = NULL;
char *ret = NULL;
sprintf(file, "/proc/%d/task/%d/stat", (int) pid, (int) tid);
fd = fopen(file, "r");
if (fd == NULL) {
USER_LOG_ERROR("open file fail.");
return 0;
}
ret = fgets(lineBuf, sizeof(lineBuf), fd);
if (ret == NULL)
goto out;
sscanf(lineBuf, "%u", (unsigned int *) &t.pid);
q = (char *) Monitor_GetItems(lineBuf, MONITOR_PROCESS_ITEM);
if (q == NULL) {
USER_LOG_ERROR("get item fail.");
goto out;
}
sscanf(q, "%u %u %u %u", &t.utime, &t.stime, &t.cutime, &t.cstime);
out:
fclose(fd);
return (t.utime + t.stime + t.cutime + t.cstime);
}
unsigned int Monitor_GetCpuTotalOccupy(void)
{
FILE *fd;
char buff[1024] = {0};
T_MonitorTotalCpuOccupy t = {0};
char name[16];
char *ret = NULL;
fd = fopen("/proc/stat", "r");
if (fd == NULL) {
USER_LOG_ERROR("open file fail.");
return 0;
}
ret = fgets(buff, sizeof(buff), fd);
if (ret == NULL)
goto out;
sscanf(buff, "%15s %u %u %u %u", name, &t.user, &t.nice, &t.system, &t.idle);
out:
fclose(fd);
return (t.user + t.nice + t.system + t.idle);
}
float Monitor_GetPcpuOfThread(pid_t pid, pid_t tid)
{
FILE *fp;
char cmdStr[MONITOR_CMD_BUF_SIZE];
char lineBuf[256] = {0};
pid_t tidInCommandLine = 0;
float pcpuInCommandLine = 0.0f;
int ret;
char *q = NULL;
snprintf(cmdStr, MONITOR_CMD_BUF_SIZE, "ps -mp %d -o tid,pcpu", (int) pid);
fp = popen(cmdStr, "r");
if (fp == NULL) {
USER_LOG_ERROR("fp is null.");
return 0;
}
while (fgets(lineBuf, sizeof(lineBuf), fp) != NULL) {
q = (char *) Monitor_GetItems(lineBuf, 1);
if (q == NULL) {
USER_LOG_ERROR("get item fail.");
goto out;
}
sscanf(q, "%u", (unsigned int *) &tidInCommandLine);
if (tidInCommandLine == tid) {
q = (char *) Monitor_GetItems(lineBuf, 2);
if (q == NULL) {
USER_LOG_ERROR("get item fail.");
goto out;
}
ret = sscanf(q, "%f", &pcpuInCommandLine);
if (ret <= 0) {
USER_LOG_ERROR("get pcpu error.");
pcpuInCommandLine = 0;
}
goto out;
}
}
USER_LOG_DEBUG("not found thread.");
out:
pclose(fp);
return pcpuInCommandLine;
}
unsigned int Monitor_GetThreadCountOfProcess(pid_t pid)
{
FILE *fp;
char cmdStr[MONITOR_CMD_BUF_SIZE];
unsigned int count;
int ret;
snprintf(cmdStr, MONITOR_CMD_BUF_SIZE, "ps -T -p %d | wc -l", (int) pid);
fp = popen(cmdStr, "r");
if (fp == NULL) {
USER_LOG_ERROR("fp is null.");
return 0;
}
ret = fscanf(fp, "%u", &count);
if (ret <= 0) {
USER_LOG_ERROR("get count error.");
count = 0;
goto out;
}
count--;
out:
pclose(fp);
return count;
}
void Monitor_GetTidListOfProcess(pid_t pid, pid_t *tidList, unsigned int size)
{
int i = 0;
FILE *fp;
char cmdStr[MONITOR_CMD_BUF_SIZE];
char lineBuf[256] = {0};
int ret = 0;
if (Monitor_GetThreadCountOfProcess(pid) > size) {
USER_LOG_ERROR("size is too small.");
return;
}
snprintf(cmdStr, MONITOR_CMD_BUF_SIZE, "ps -mp %d -o tid", (int) pid);
fp = popen(cmdStr, "r");
if (fp == NULL) {
USER_LOG_ERROR("fp is null.");
return;
}
while (fgets(lineBuf, sizeof(lineBuf), fp) != NULL && i <= size) {
ret = sscanf(lineBuf, "%u", (unsigned int *) &tidList[i]);
if (ret > 0)
i++;
}
pclose(fp);
}
void Monitor_GetNameOfThread(pid_t pid, pid_t tid, char *name, unsigned int size)
{
char file[64] = {0};
FILE *fd;
char lineBuf[32] = {0};
char *ret = NULL;
memset(name, 0, size);
sprintf(file, "/proc/%d/task/%d/comm", (int) pid, (int) tid);
fd = fopen(file, "r");
if (fd == NULL) {
USER_LOG_DEBUG("open file fail.");
return;
}
ret = fgets(lineBuf, sizeof(lineBuf), fd);
if (ret == NULL)
goto out;
if (lineBuf[strlen(lineBuf) - 1] == '\n')
lineBuf[strlen(lineBuf) - 1] = '\0';
strncpy(name, lineBuf, USER_UTIL_MIN(size - 1, sizeof(lineBuf)));
out:
fclose(fd);
}
/**
* @brief
* @param pid
* @return Unit: B.
*/
unsigned int Monitor_GetHeapUsed(pid_t pid)
{
FILE *fp;
char cmdStr[MONITOR_CMD_BUF_SIZE];
char lineBuf[256] = {0};
int ret = 0;
unsigned int heapUsed = 0;
char *q = NULL;
char *rett = NULL;
snprintf(cmdStr, MONITOR_CMD_BUF_SIZE, "cat /proc/%d/smaps | grep -A 18 heap | grep Private_Dirty", (int) pid);
fp = popen(cmdStr, "r");
if (fp == NULL) {
USER_LOG_ERROR("fp is null.");
return 0;
}
rett = fgets(lineBuf, sizeof(lineBuf), fp);
if (rett == NULL) {
goto out;
}
q = (char *) Monitor_GetItems(lineBuf, 2);
if (q == NULL) {
USER_LOG_ERROR("get item fail.");
goto out;
}
ret = sscanf(q, "%u", &heapUsed);
if (ret <= 0) {
USER_LOG_ERROR("can not find heapUsed.");
heapUsed = 0;
goto out;
}
heapUsed *= 1024;
out:
pclose(fp);
return heapUsed;
}
/**
* @brief
* @param pid
* @return Unit: B.
*/
unsigned int Monitor_GetStackUsed(pid_t pid)
{
FILE *fp;
char cmdStr[MONITOR_CMD_BUF_SIZE];
char lineBuf[256] = {0};
int ret = 0;
unsigned int stackUsed = 0;
char *q = NULL;
char *rett = NULL;
snprintf(cmdStr, MONITOR_CMD_BUF_SIZE, "cat /proc/%d/smaps | grep -A 18 stack | grep Private_Dirty", (int) pid);
fp = popen(cmdStr, "r");
if (fp == NULL) {
USER_LOG_ERROR("fp is null.");
return 0;
}
rett = fgets(lineBuf, sizeof(lineBuf), fp);
if (rett == NULL)
goto out;
q = (char *) Monitor_GetItems(lineBuf, 2);
if (q == NULL) {
USER_LOG_ERROR("get item fail.");
goto out;
}
ret = sscanf(q, "%u", &stackUsed);
if (ret <= 0) {
USER_LOG_ERROR("can not find stackUsed.");
stackUsed = 0;
goto out;
}
stackUsed *= 1024;
out:
pclose(fp);
return stackUsed;
}
/* Private functions definition-----------------------------------------------*/
static const char *Monitor_GetItems(const char *buffer, int ie)
{
int i = 0;
int j = 0;
char *p = (char *) buffer;
int len = (int) strlen(buffer);
int count = 0;
if (1 == ie || ie < 1) {
return p;
}
while (1) {
for (i = j; i < len; i++) {
if (*(buffer + i) != ' ') {
count++;
if (count == ie)
return buffer + i;
break;
}
}
for (j = i; j < len; ++j) {
if (*(buffer + j) == ' ')
break;
}
if (i == len || j == len)
break;
}
return NULL;
}
/****************** (C) COPYRIGHT DJI Innovations *****END OF FILE****/

View File

@ -0,0 +1,76 @@
/**
********************************************************************
* @file sys_monitor.h
* @brief This is the header file for "sys_monitor.c", defining the structure and
* (exported) function prototypes.
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI 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 SYS_MONITOR_H
#define SYS_MONITOR_H
/* Includes ------------------------------------------------------------------*/
#include "dji_typedef.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Exported constants --------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
typedef struct {
unsigned int user;
unsigned int nice;
unsigned int system;
unsigned int idle;
} T_MonitorTotalCpuOccupy;
typedef struct {
pid_t pid;
unsigned int utime;
unsigned int stime;
unsigned int cutime;
unsigned int cstime;
} T_MonitorProcessCpuOccupy;
/* Exported functions --------------------------------------------------------*/
int Monitor_GetPhyMem(pid_t p);
int Monitor_GetTotalMem();
unsigned int Monitor_GetCpuTotalOccupy();
unsigned int Monitor_GetCpuOccupyOfProcess(pid_t pid);
unsigned int Monitor_GetCpuOccupyOfThread(pid_t pid, pid_t tid);
float Monitor_GetPcpuOfThread(pid_t pid, pid_t tid);
float Monitor_GetPmem(pid_t p);
unsigned int Monitor_GetThreadCountOfProcess(pid_t pid);
void Monitor_GetTidListOfProcess(pid_t pid, pid_t *tidList, unsigned int size);
void Monitor_GetNameOfThread(pid_t pid, pid_t tid, char *name, unsigned int size);
unsigned int Monitor_GetHeapUsed(pid_t pid);
unsigned int Monitor_GetStackUsed(pid_t pid);
#ifdef __cplusplus
}
#endif
#endif //SYS_MONITOR_H
/************************ (C) COPYRIGHT DJI Innovations *******END OF FILE******/

View File

@ -0,0 +1,309 @@
/**
********************************************************************
* @file osal.c
* @brief
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI reserves the right to pursue
* legal actions against you for any loss(es) or damage(s) caused by your
* failure to do so.
*
*********************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "osal.h"
#include "dji_typedef.h"
/* Private constants ---------------------------------------------------------*/
/* Private types -------------------------------------------------------------*/
/* Private functions declaration ---------------------------------------------*/
/* Exported functions definition ---------------------------------------------*/
/* Private functions definition-----------------------------------------------*/
/* Private functions declaration ---------------------------------------------*/
/* Exported functions definition ---------------------------------------------*/
T_DjiReturnCode Osal_TaskCreate(const char *name, void *(*taskFunc)(void *), uint32_t stackSize, void *arg,
T_DjiTaskHandle *task)
{
int result;
char nameDealed[16] = {0};
*task = malloc(sizeof(pthread_t));
if (*task == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
result = pthread_create(*task, NULL, taskFunc, arg);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
if (name != NULL)
strncpy(nameDealed, name, sizeof(nameDealed) - 1);
result = pthread_setname_np(*(pthread_t *) *task, nameDealed);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_TaskDestroy(T_DjiTaskHandle task)
{
pthread_cancel(*(pthread_t *) task);
free(task);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_TaskSleepMs(uint32_t timeMs)
{
usleep(1000 * timeMs);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Declare the mutex container, initialize the mutex, and
* create mutex ID.
* @param mutex: pointer to the created mutex ID.
* @return an enum that represents a status of PSDK
*/
T_DjiReturnCode Osal_MutexCreate(T_DjiMutexHandle *mutex)
{
int result;
*mutex = malloc(sizeof(pthread_mutex_t));
if (*mutex == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
result = pthread_mutex_init(*mutex, NULL);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Delete the created mutex.
* @param mutex: pointer to the created mutex ID.
* @return an enum that represents a status of PSDK
*/
T_DjiReturnCode Osal_MutexDestroy(T_DjiMutexHandle mutex)
{
int result;
result = pthread_mutex_destroy(mutex);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
free(mutex);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Acquire and lock the mutex when peripheral access is required
* @param mutex: pointer to the created mutex ID.
* @return an enum that represents a status of PSDK
*/
T_DjiReturnCode Osal_MutexLock(T_DjiMutexHandle mutex)
{
int result = pthread_mutex_lock(mutex);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Unlock and release the mutex, when done with the peripheral access.
* @param mutex: pointer to the created mutex ID.
* @return an enum that represents a status of PSDK
*/
T_DjiReturnCode Osal_MutexUnlock(T_DjiMutexHandle mutex)
{
int result = pthread_mutex_unlock(mutex);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Declare the semaphore container, initialize the semaphore, and
* create semaphore ID.
* @param semaphore: pointer to the created semaphore ID.
* @param initValue: initial value of semaphore.
* @return an enum that represents a status of PSDK
*/
T_DjiReturnCode Osal_SemaphoreCreate(uint32_t initValue, T_DjiSemaHandle *semaphore)
{
int result;
*semaphore = malloc(sizeof(sem_t));
if (*semaphore == NULL) {
return
DJI_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
result = sem_init(*semaphore, 0, (unsigned int) initValue);
if (result != 0) {
return
DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return
DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Delete the created semaphore.
* @param semaphore: pointer to the created semaphore ID.
* @return an enum that represents a status of PSDK
*/
T_DjiReturnCode Osal_SemaphoreDestroy(T_DjiSemaHandle semaphore)
{
int result;
result = sem_destroy((sem_t *) semaphore);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
free(semaphore);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Wait the semaphore until token becomes available.
* @param semaphore: pointer to the created semaphore ID.
* @return an enum that represents a status of PSDK
*/
T_DjiReturnCode Osal_SemaphoreWait(T_DjiSemaHandle semaphore)
{
int result;
result = sem_wait(semaphore);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Wait the semaphore until token becomes available.
* @param semaphore: pointer to the created semaphore ID.
* @param waitTime: timeout value of waiting semaphore, unit: millisecond.
* @return an enum that represents a status of PSDK
*/
T_DjiReturnCode Osal_SemaphoreTimedWait(T_DjiSemaHandle semaphore, uint32_t waitTime)
{
int result;
struct timespec semaphoreWaitTime;
struct timeval systemTime;
gettimeofday(&systemTime, NULL);
systemTime.tv_usec += waitTime * 1000;
if (systemTime.tv_usec >= 1000000) {
systemTime.tv_sec += systemTime.tv_usec / 1000000;
systemTime.tv_usec %= 1000000;
}
semaphoreWaitTime.tv_sec = systemTime.tv_sec;
semaphoreWaitTime.tv_nsec = systemTime.tv_usec * 1000;
result = sem_timedwait(semaphore, &semaphoreWaitTime);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Release the semaphore token.
* @param semaphore: pointer to the created semaphore ID.
* @return an enum that represents a status of PSDK
*/
T_DjiReturnCode Osal_SemaphorePost(T_DjiSemaHandle semaphore)
{
int result;
result = sem_post(semaphore);
if (result != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Get the system time for ms.
* @return an uint32 that the time of system, uint:ms
*/
T_DjiReturnCode Osal_GetTimeMs(uint32_t *ms)
{
struct timeval time;
gettimeofday(&time, NULL);
*ms = (time.tv_sec * 1000 + time.tv_usec / 1000);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_GetTimeUs(uint64_t *us)
{
struct timeval time;
gettimeofday(&time, NULL);
*us = (time.tv_sec * 1000000 + time.tv_usec);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
void *Osal_Malloc(uint32_t size)
{
if (size == 0) {
return NULL;
}
return malloc(size);
}
void Osal_Free(void *ptr)
{
free(ptr);
}
/****************** (C) COPYRIGHT DJI Innovations *****END OF FILE****/

View File

@ -0,0 +1,75 @@
/**
********************************************************************
* @file osal.h
* @brief This is the header file for "osal.c", defining the structure and
* (exported) function prototypes.
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI 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 OSAL_H
#define OSAL_H
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/time.h>
#include <unistd.h>
#include "dji_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Exported constants --------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
T_DjiReturnCode Osal_TaskCreate(const char *name, void *(*taskFunc)(void *),
uint32_t stackSize, void *arg, T_DjiTaskHandle *task);
T_DjiReturnCode Osal_TaskDestroy(T_DjiTaskHandle task);
T_DjiReturnCode Osal_TaskSleepMs(uint32_t timeMs);
T_DjiReturnCode Osal_MutexCreate(T_DjiMutexHandle *mutex);
T_DjiReturnCode Osal_MutexDestroy(T_DjiMutexHandle mutex);
T_DjiReturnCode Osal_MutexLock(T_DjiMutexHandle mutex);
T_DjiReturnCode Osal_MutexUnlock(T_DjiMutexHandle mutex);
T_DjiReturnCode Osal_SemaphoreCreate(uint32_t initValue, T_DjiSemaHandle *semaphore);
T_DjiReturnCode Osal_SemaphoreDestroy(T_DjiSemaHandle semaphore);
T_DjiReturnCode Osal_SemaphoreWait(T_DjiSemaHandle semaphore);
T_DjiReturnCode Osal_SemaphoreTimedWait(T_DjiSemaHandle semaphore, uint32_t waitTime);
T_DjiReturnCode Osal_SemaphorePost(T_DjiSemaHandle semaphore);
T_DjiReturnCode Osal_GetTimeMs(uint32_t *ms);
T_DjiReturnCode Osal_GetTimeUs(uint64_t *us);
void *Osal_Malloc(uint32_t size);
void Osal_Free(void *ptr);
#ifdef __cplusplus
}
#endif
#endif // OSAL_H
/************************ (C) COPYRIGHT DJI Innovations *******END OF FILE******/

View File

@ -0,0 +1,293 @@
/**
********************************************************************
* @file osal_fs.c
* @brief
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI reserves the right to pursue
* legal actions against you for any loss(es) or damage(s) caused by your
* failure to do so.
*
*********************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "osal_fs.h"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include <sys/stat.h>
#include <dirent.h>
#include "time.h"
/* Private constants ---------------------------------------------------------*/
/* Private types -------------------------------------------------------------*/
/* Private values -------------------------------------------------------------*/
/* Private functions declaration ---------------------------------------------*/
/* Exported functions definition ---------------------------------------------*/
T_DjiReturnCode Osal_FileOpen(const char *fileName, const char *fileMode, T_DjiFileHandle *fileObj)
{
if (fileName == NULL || fileMode == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
*fileObj = malloc(sizeof(FILE));
if (*fileObj == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
*fileObj = fopen(fileName, fileMode);
if (*fileObj == NULL) {
goto out;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
out:
free(*fileObj);
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
T_DjiReturnCode Osal_FileClose(T_DjiFileHandle fileObj)
{
int32_t ret;
if (fileObj == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = fclose(fileObj);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_FileWrite(T_DjiFileHandle fileObj, const uint8_t *buf, uint32_t len, uint32_t *realLen)
{
int32_t ret;
if (fileObj == NULL || buf == NULL || len == 0 || realLen == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = fwrite(buf, 1, len, fileObj);
if (ret >= 0) {
*realLen = ret;
} else {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_FileRead(T_DjiFileHandle fileObj, uint8_t *buf, uint32_t len, uint32_t *realLen)
{
int32_t ret;
if (fileObj == NULL || buf == NULL || len == 0 || realLen == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = fread(buf, 1, len, fileObj);
if (ret >= 0) {
*realLen = ret;
} else {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_FileSeek(T_DjiFileHandle fileObj, uint32_t offset)
{
int32_t ret;
if (fileObj == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = fseek(fileObj, offset, SEEK_SET);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_FileSync(T_DjiFileHandle fileObj)
{
int32_t ret;
if (fileObj == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = fflush(fileObj);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_DirOpen(const char *filePath, T_DjiDirHandle *dirObj)
{
if (filePath == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
*dirObj = opendir(filePath);
if (*dirObj == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_DirClose(T_DjiDirHandle dirObj)
{
int32_t ret;
if (dirObj == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = closedir((DIR *) dirObj);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_DirRead(T_DjiDirHandle dirObj, T_DjiFileInfo *fileInfo)
{
struct dirent *dirent;
if (dirObj == NULL || fileInfo == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
dirent = readdir((DIR *) dirObj);
if (!dirent) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
if (dirent->d_type == DT_DIR) {
fileInfo->isDir = true;
} else {
fileInfo->isDir = false;
}
strcpy(fileInfo->path, dirent->d_name);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_Mkdir(const char *filePath)
{
int32_t ret;
if (filePath == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = mkdir(filePath, S_IRWXU);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_Unlink(const char *filePath)
{
int32_t ret;
if (filePath == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
if (filePath[strlen(filePath) - 1] == '/') {
ret = rmdir(filePath);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
} else {
ret = unlink(filePath);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_Rename(const char *oldFilePath, const char *newFilePath)
{
int32_t ret;
if (oldFilePath == NULL || newFilePath == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = rename(oldFilePath, newFilePath);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_Stat(const char *filePath, T_DjiFileInfo *fileInfo)
{
struct stat st;
int32_t ret;
struct tm *fileTm;
if (filePath == NULL || fileInfo == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = stat(filePath, &st);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
fileTm = localtime(&(st.st_ctime));
if (fileTm == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
fileInfo->size = st.st_size;
fileInfo->createTime.year = fileTm->tm_year + 1900 - 1980;
fileInfo->createTime.month = fileTm->tm_mon;
fileInfo->createTime.day = fileTm->tm_mday;
fileInfo->createTime.hour = fileTm->tm_hour;
fileInfo->createTime.minute = fileTm->tm_min;
fileInfo->createTime.second = fileTm->tm_sec;
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/* Private functions definition-----------------------------------------------*/
/****************** (C) COPYRIGHT DJI Innovations *****END OF FILE****/

View File

@ -0,0 +1,73 @@
/**
********************************************************************
* @file osal_fs.h
* @brief This is the header file for "osal_fs.c", defining the structure and
* (exported) function prototypes.
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI 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 OSAL_FS_H
#define OSAL_FS_H
/* Includes ------------------------------------------------------------------*/
#include "dji_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Exported constants --------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
T_DjiReturnCode Osal_FileOpen(const char *fileName, const char *fileMode, T_DjiFileHandle *fileObj);
T_DjiReturnCode Osal_FileClose(T_DjiFileHandle fileObj);
T_DjiReturnCode Osal_FileWrite(T_DjiFileHandle fileObj, const uint8_t *buf, uint32_t len, uint32_t *realLen);
T_DjiReturnCode Osal_FileRead(T_DjiFileHandle fileObj, uint8_t *buf, uint32_t len, uint32_t *realLen);
T_DjiReturnCode Osal_FileSeek(T_DjiFileHandle fileObj, uint32_t offset);
T_DjiReturnCode Osal_FileSync(T_DjiFileHandle fileObj);
T_DjiReturnCode Osal_DirOpen(const char *filePath, T_DjiDirHandle *dirObj);
T_DjiReturnCode Osal_DirClose(T_DjiDirHandle dirObj);
T_DjiReturnCode Osal_DirRead(T_DjiDirHandle dirObj, T_DjiFileInfo *fileInfo);
T_DjiReturnCode Osal_Mkdir(const char *filePath);
T_DjiReturnCode Osal_Unlink(const char *filePath);
T_DjiReturnCode Osal_Rename(const char *oldFilePath, const char *newFilePath);
T_DjiReturnCode Osal_Stat(const char *filePath, T_DjiFileInfo *fileInfo);
#ifdef __cplusplus
}
#endif
#endif // OSAL_FS_H
/************************ (C) COPYRIGHT DJI Innovations *******END OF FILE******/

View File

@ -0,0 +1,272 @@
/**
********************************************************************
* @file osal_socket.c
* @brief
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI reserves the right to pursue
* legal actions against you for any loss(es) or damage(s) caused by your
* failure to do so.
*
*********************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "osal_socket.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "stdlib.h"
/* Private constants ---------------------------------------------------------*/
typedef struct {
int socketFd;
} T_SocketHandleStruct;
/* Private types -------------------------------------------------------------*/
/* Private values -------------------------------------------------------------*/
/* Private functions declaration ---------------------------------------------*/
/* Exported functions definition ---------------------------------------------*/
T_DjiReturnCode Osal_Socket(E_DjiSocketMode mode, T_DjiSocketHandle *socketHandle)
{
T_SocketHandleStruct *socketHandleStruct;
if (socketHandle == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
socketHandleStruct = malloc(sizeof(T_SocketHandleStruct));
if (socketHandleStruct == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
if (mode == DJI_SOCKET_MODE_UDP) {
socketHandleStruct->socketFd = socket(PF_INET, SOCK_DGRAM, 0);
} else if (mode == DJI_SOCKET_MODE_TCP) {
socketHandleStruct->socketFd = socket(PF_INET, SOCK_STREAM, 0);
} else {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
*socketHandle = socketHandleStruct;
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_Close(T_DjiSocketHandle socketHandle)
{
T_SocketHandleStruct *socketHandleStruct = (T_SocketHandleStruct *) socketHandle;
int32_t ret;
if (socketHandleStruct->socketFd <= 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = close(socketHandleStruct->socketFd);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_Bind(T_DjiSocketHandle socketHandle, const char *ipAddr, uint32_t port)
{
T_SocketHandleStruct *socketHandleStruct = (T_SocketHandleStruct *) socketHandle;
struct sockaddr_in addr;
int32_t ret;
if (socketHandle == NULL || ipAddr == NULL || port == 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ipAddr);
ret = bind(socketHandleStruct->socketFd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_UdpSendData(T_DjiSocketHandle socketHandle, const char *ipAddr, uint32_t port,
const uint8_t *buf, uint32_t len, uint32_t *realLen)
{
struct sockaddr_in addr;
T_SocketHandleStruct *socketHandleStruct = (T_SocketHandleStruct *) socketHandle;
int32_t ret;
if (socketHandle <= 0 || ipAddr == NULL || port == 0 || buf == NULL || len == 0 || realLen == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ipAddr);
ret = sendto(socketHandleStruct->socketFd, buf, len, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
if (ret >= 0) {
*realLen = ret;
} else {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_UdpRecvData(T_DjiSocketHandle socketHandle, char *ipAddr, uint32_t *port,
uint8_t *buf, uint32_t len, uint32_t *realLen)
{
struct sockaddr_in addr;
T_SocketHandleStruct *socketHandleStruct = (T_SocketHandleStruct *) socketHandle;
uint32_t addrLen = 0;
int32_t ret;
if (socketHandle == NULL || ipAddr == NULL || port == 0 || buf == NULL || len == 0 || realLen == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = recvfrom(socketHandleStruct->socketFd, buf, len, 0, (struct sockaddr *) &addr, &addrLen);
if (ret >= 0) {
*realLen = ret;
strcpy(ipAddr, inet_ntoa(addr.sin_addr));
*port = ntohs(addr.sin_port);
} else {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_TcpListen(T_DjiSocketHandle socketHandle)
{
int32_t ret;
T_SocketHandleStruct *socketHandleStruct = (T_SocketHandleStruct *) socketHandle;
if (socketHandle == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = listen(socketHandleStruct->socketFd, 5);
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_TcpAccept(T_DjiSocketHandle socketHandle, char *ipAddr, uint32_t *port,
T_DjiSocketHandle *outSocketHandle)
{
T_SocketHandleStruct *socketHandleStruct = (T_SocketHandleStruct *) socketHandle;
T_SocketHandleStruct *outSocketHandleStruct;
struct sockaddr_in addr;
uint32_t addrLen = 0;
if (socketHandle == NULL || ipAddr == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
outSocketHandleStruct = malloc(sizeof(T_SocketHandleStruct));
if (outSocketHandleStruct == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
outSocketHandleStruct->socketFd = accept(socketHandleStruct->socketFd, (struct sockaddr *) &addr, &addrLen);
if (outSocketHandleStruct->socketFd < 0) {
free(outSocketHandleStruct);
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
*port = ntohs(addr.sin_port);
*outSocketHandle = outSocketHandleStruct;
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_TcpConnect(T_DjiSocketHandle socketHandle, const char *ipAddr, uint32_t port)
{
struct sockaddr_in addr;
T_SocketHandleStruct *socketHandleStruct = (T_SocketHandleStruct *) socketHandle;
int32_t ret;
if (socketHandle == NULL || ipAddr == NULL || port == 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ipAddr);
ret = connect(socketHandleStruct->socketFd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
if (ret < 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_TcpSendData(T_DjiSocketHandle socketHandle,
const uint8_t *buf, uint32_t len, uint32_t *realLen)
{
T_SocketHandleStruct *socketHandleStruct = (T_SocketHandleStruct *) socketHandle;
int32_t ret;
if (socketHandle == NULL || buf == NULL || len == 0 || realLen == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = send(socketHandleStruct->socketFd, buf, len, 0);
if (ret >= 0) {
*realLen = ret;
} else {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode Osal_TcpRecvData(T_DjiSocketHandle socketHandle,
uint8_t *buf, uint32_t len, uint32_t *realLen)
{
T_SocketHandleStruct *socketHandleStruct = (T_SocketHandleStruct *) socketHandle;
int32_t ret;
if (socketHandle == NULL || buf == NULL || len == 0 || realLen == NULL) {
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
ret = recv(socketHandleStruct->socketFd, buf, len, 0);
if (ret >= 0) {
*realLen = ret;
} else {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/* Private functions definition-----------------------------------------------*/
/****************** (C) COPYRIGHT DJI Innovations *****END OF FILE****/

View File

@ -0,0 +1,72 @@
/**
********************************************************************
* @file osal_socket.h
* @brief This is the header file for "osal_socket.c", defining the structure and
* (exported) function prototypes.
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI 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 OSAL_SOCKET_H
#define OSAL_SOCKET_H
/* Includes ------------------------------------------------------------------*/
#include "dji_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Exported constants --------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
T_DjiReturnCode Osal_Socket(E_DjiSocketMode mode, T_DjiSocketHandle *socketHandle);
T_DjiReturnCode Osal_Close(T_DjiSocketHandle socketHandle);
T_DjiReturnCode Osal_Bind(T_DjiSocketHandle socketHandle, const char *ipAddr, uint32_t port);
T_DjiReturnCode Osal_UdpSendData(T_DjiSocketHandle socketHandle, const char *ipAddr, uint32_t port,
const uint8_t *buf, uint32_t len, uint32_t *realLen);
T_DjiReturnCode Osal_UdpRecvData(T_DjiSocketHandle socketHandle, char *ipAddr, uint32_t *port,
uint8_t *buf, uint32_t len, uint32_t *realLen);
T_DjiReturnCode Osal_TcpListen(T_DjiSocketHandle socketHandle);
T_DjiReturnCode Osal_TcpAccept(T_DjiSocketHandle socketHandle, char *ipAddr, uint32_t *port,
T_DjiSocketHandle *outSocketHandle);
T_DjiReturnCode Osal_TcpConnect(T_DjiSocketHandle socketHandle, const char *ipAddr, uint32_t port);
T_DjiReturnCode Osal_TcpSendData(T_DjiSocketHandle socketHandle,
const uint8_t *buf, uint32_t len, uint32_t *realLen);
T_DjiReturnCode Osal_TcpRecvData(T_DjiSocketHandle socketHandle,
uint8_t *buf, uint32_t len, uint32_t *realLen);
#ifdef __cplusplus
}
#endif
#endif // OSAL_SOCKET_H
/************************ (C) COPYRIGHT DJI Innovations *******END OF FILE******/

View File

@ -0,0 +1,218 @@
/**
********************************************************************
* @file upgrade_platform_opt_linux.c
* @version V2.0.0
* @date 3/25/20
* @brief
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI reserves the right to pursue
* legal actions against you for any loss(es) or damage(s) caused by your
* failure to do so.
*
*********************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "upgrade_platform_opt_linux.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dji_logger.h>
#include <dji_upgrade.h>
/* Private constants ---------------------------------------------------------*/
#define DJI_TEST_CMD_CALL_MAX_LEN (DJI_FILE_PATH_SIZE_MAX + 256)
#define DJI_REBOOT_STATE_FILE_NAME "reboot_state"
/* Private types -------------------------------------------------------------*/
/* Private values -------------------------------------------------------------*/
static FILE *s_upgradeProgramFile = NULL;
/* Private functions declaration ---------------------------------------------*/
static T_DjiReturnCode DjiTest_RunSystemCmd(char *systemCmdStr);
/* Exported functions definition ---------------------------------------------*/
T_DjiReturnCode DjiUpgradePlatformLinux_RebootSystem(void)
{
// attention: you need su permission to reboot system
return DjiTest_RunSystemCmd("reboot -h now");
}
T_DjiReturnCode DjiUpgradePlatformLinux_CleanUpgradeProgramFileStoreArea(void)
{
char cmdBuffer[DJI_TEST_CMD_CALL_MAX_LEN];
snprintf(cmdBuffer, DJI_TEST_CMD_CALL_MAX_LEN, "rm -rf %s*", DJI_TEST_UPGRADE_FILE_DIR);
return DjiTest_RunSystemCmd(cmdBuffer);
}
T_DjiReturnCode DjiUpgradePlatformLinux_ReplaceOldProgram(void)
{
char cmdBuffer[DJI_TEST_CMD_CALL_MAX_LEN];
snprintf(cmdBuffer, DJI_TEST_CMD_CALL_MAX_LEN, "cp -f %s*_V*.*.*.bin %s", DJI_TEST_UPGRADE_FILE_DIR,
DJI_TEST_UPGRADE_OLD_FIRMWARE_PATH);
return DjiTest_RunSystemCmd(cmdBuffer);
}
T_DjiReturnCode DjiUpgradePlatformLinux_SetUpgradeRebootState(const T_DjiUpgradeEndInfo *upgradeEndInfo)
{
FILE *rebootStateFile;
size_t res;
T_DjiReturnCode returnCode;
rebootStateFile = fopen(DJI_REBOOT_STATE_FILE_NAME, "w+");
if (rebootStateFile == NULL) {
USER_LOG_ERROR("Create reboot state file error");
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
res = fwrite((uint8_t *) upgradeEndInfo, 1, sizeof(T_DjiUpgradeEndInfo), rebootStateFile);
if (res != sizeof(T_DjiUpgradeEndInfo)) {
USER_LOG_ERROR("Write data len is not equal");
returnCode = DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
goto out;
}
returnCode = DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
out:
fclose(rebootStateFile);
return returnCode;
}
T_DjiReturnCode DjiUpgradePlatformLinux_GetUpgradeRebootState(bool *isUpgradeReboot,
T_DjiUpgradeEndInfo *upgradeEndInfo)
{
FILE *rebootStateFile;
size_t res;
rebootStateFile = fopen(DJI_REBOOT_STATE_FILE_NAME, "r");
if (rebootStateFile == NULL) {
*isUpgradeReboot = false;
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
res = fread(upgradeEndInfo, 1, sizeof(T_DjiUpgradeEndInfo), rebootStateFile);
if (res != sizeof(T_DjiUpgradeEndInfo)) {
USER_LOG_ERROR("Read data len is not equal");
*isUpgradeReboot = false;
goto out;
}
*isUpgradeReboot = true;
out:
fclose(rebootStateFile);
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode DjiUpgradePlatformLinux_CleanUpgradeRebootState(void)
{
return DjiTest_RunSystemCmd("rm -f "DJI_REBOOT_STATE_FILE_NAME);
}
T_DjiReturnCode DjiUpgradePlatformLinux_CreateUpgradeProgramFile(const T_DjiUpgradeFileInfo *fileInfo)
{
char filePath[DJI_FILE_PATH_SIZE_MAX];
s_upgradeProgramFile = NULL;
snprintf(filePath, DJI_FILE_PATH_SIZE_MAX, "%s%s", DJI_TEST_UPGRADE_FILE_DIR, fileInfo->fileName);
s_upgradeProgramFile = fopen(filePath, "w+");
if (s_upgradeProgramFile == NULL) {
USER_LOG_ERROR("Upgrade program file can't create");
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode
DjiUpgradePlatformLinux_WriteUpgradeProgramFile(uint32_t offset, const uint8_t *data, uint16_t dataLen)
{
size_t writeLen;
if (s_upgradeProgramFile == NULL) {
USER_LOG_ERROR("upgrade program file can't be NULL");
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
if (fseek(s_upgradeProgramFile, offset, SEEK_SET) != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
writeLen = fwrite(data, 1, dataLen, s_upgradeProgramFile);
if (writeLen != dataLen) {
USER_LOG_ERROR("Write upgrade program file error, writeLen = %d, dataLen = %d", writeLen);
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode DjiUpgradePlatformLinux_ReadUpgradeProgramFile(uint32_t offset, uint16_t readDataLen, uint8_t *data,
uint16_t *realLen)
{
uint32_t readRtn;
if (s_upgradeProgramFile == NULL) {
USER_LOG_ERROR("upgrade program file can't be NULL");
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
if (fseek(s_upgradeProgramFile, offset, SEEK_SET) != 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
readRtn = fread(data, 1, readDataLen, s_upgradeProgramFile);
if (readRtn == 0 || readRtn > readDataLen) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
*realLen = readRtn;
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_DjiReturnCode DjiUpgradePlatformLinux_CloseUpgradeProgramFile(void)
{
if (s_upgradeProgramFile == NULL) {
USER_LOG_ERROR("upgrade program file can't be NULL");
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
fclose(s_upgradeProgramFile);
s_upgradeProgramFile = NULL;
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/* Private functions definition-----------------------------------------------*/
static T_DjiReturnCode DjiTest_RunSystemCmd(char *systemCmdStr)
{
int status;
status = system(systemCmdStr);
if (status == -1 || WIFEXITED(status) == false) {
USER_LOG_ERROR("Call %s error, status = %d", systemCmdStr, status);
return DJI_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
if (WEXITSTATUS(status) == 0) {
return DJI_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
} else {
USER_LOG_ERROR("Exit status is = %d", WEXITSTATUS(status));
return DJI_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
}
/****************** (C) COPYRIGHT DJI Innovations *****END OF FILE****/

View File

@ -0,0 +1,70 @@
/**
********************************************************************
* @file upgrade_platform_opt_linux.h
* @version V1.0.0
* @date 3/25/20
* @brief This is the header file for "upgrade_platform_opt_linux.c", defining the structure and
* (exported) function prototypes.
*
* @copyright (c) 2021 DJI. All rights reserved.
*
* All information contained herein is, and remains, the property of DJI.
* The intellectual and technical concepts contained herein are proprietary
* to DJI 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 DJI.
*
* If you receive this source code without DJIs authorization, you may not
* further disseminate the information, and you must immediately remove the
* source code and notify DJI of its removal. DJI 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 UPGRADE_PLATFORM_OPT_LINUX_H
#define UPGRADE_PLATFORM_OPT_LINUX_H
/* Includes ------------------------------------------------------------------*/
#include <dji_typedef.h>
#include <dji_upgrade.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Exported constants --------------------------------------------------------*/
#define DJI_TEST_UPGRADE_OLD_FIRMWARE_PATH "/usr/local/bin/dji_sdk_demo"
#define DJI_TEST_UPGRADE_FILE_DIR "/upgrade/"
/* Exported types ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
T_DjiReturnCode DjiUpgradePlatformLinux_RebootSystem(void);
T_DjiReturnCode DjiUpgradePlatformLinux_CleanUpgradeProgramFileStoreArea(void);
T_DjiReturnCode DjiUpgradePlatformLinux_CreateUpgradeProgramFile(const T_DjiUpgradeFileInfo *fileInfo);
T_DjiReturnCode DjiUpgradePlatformLinux_WriteUpgradeProgramFile(uint32_t offset, const uint8_t *data,
uint16_t dataLen);
T_DjiReturnCode DjiUpgradePlatformLinux_ReadUpgradeProgramFile(uint32_t offset, uint16_t readDataLen, uint8_t *data,
uint16_t *realLen);
T_DjiReturnCode DjiUpgradePlatformLinux_CloseUpgradeProgramFile(void);
T_DjiReturnCode DjiUpgradePlatformLinux_ReplaceOldProgram(void);
T_DjiReturnCode DjiUpgradePlatformLinux_SetUpgradeRebootState(const T_DjiUpgradeEndInfo *upgradeEndInfo);
T_DjiReturnCode DjiUpgradePlatformLinux_GetUpgradeRebootState(bool *isUpgradeReboot,
T_DjiUpgradeEndInfo *upgradeEndInfo);
T_DjiReturnCode DjiUpgradePlatformLinux_CleanUpgradeRebootState(void);
#ifdef __cplusplus
}
#endif
#endif // UPGRADE_PLATFORM_OPT_LINUX_H
/************************ (C) COPYRIGHT DJI Innovations *******END OF FILE******/