1、操作遥控器按钮,psdk进程通过socket发送命令给相机进程 来控制高光谱采集系统;

2、通过FFmpeg采集摄像头视频流并编码为h264,利用psdk推流到遥控器;
This commit is contained in:
tangchao0503
2022-06-22 16:48:50 +08:00
commit 109e0a8f2b
89 changed files with 17079 additions and 0 deletions

View File

@ -0,0 +1,484 @@
/**
********************************************************************
* @file sys_monitor.c
* @version V2.0.0
* @date 2019/11/10
* @brief
*
* @copyright (c) 2018-2019 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 "psdk_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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("fp is null.");
return 0;
}
while (fgets(lineBuf, sizeof(lineBuf), fp) != NULL) {
q = (char *) Monitor_GetItems(lineBuf, 1);
if (q == NULL) {
PsdkLogger_UserLogError("get item fail.");
goto out;
}
sscanf(q, "%u", (unsigned int *) &tidInCommandLine);
if (tidInCommandLine == tid) {
q = (char *) Monitor_GetItems(lineBuf, 2);
if (q == NULL) {
PsdkLogger_UserLogError("get item fail.");
goto out;
}
ret = sscanf(q, "%f", &pcpuInCommandLine);
if (ret <= 0) {
PsdkLogger_UserLogError("get pcpu error.");
pcpuInCommandLine = 0;
}
goto out;
}
}
PsdkLogger_UserLogDebug("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) {
PsdkLogger_UserLogError("fp is null.");
return 0;
}
ret = fscanf(fp, "%u", &count);
if (ret <= 0) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogDebug("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("get item fail.");
goto out;
}
ret = sscanf(q, "%u", &heapUsed);
if (ret <= 0) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("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) {
PsdkLogger_UserLogError("get item fail.");
goto out;
}
ret = sscanf(q, "%u", &stackUsed);
if (ret <= 0) {
PsdkLogger_UserLogError("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,78 @@
/**
********************************************************************
* @file sys_monitor.h
* @version V2.0.0
* @date 2019/11/10
* @brief This is the header file for "sys_monitor.c", defining the structure and
* (exported) function prototypes.
*
* @copyright (c) 2018-2019 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 "psdk_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,294 @@
/**
********************************************************************
* @file psdk_osal.c
* @version V2.0.0
* @date 2019/07/01
* @brief
*
* @copyright (c) 2018-2019 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 "utils/util_misc.h"
#include "psdk_logger.h"
/* Private constants ---------------------------------------------------------*/
/* Private types -------------------------------------------------------------*/
/* Private functions declaration ---------------------------------------------*/
/* Exported functions definition ---------------------------------------------*/
/* Private functions definition-----------------------------------------------*/
/* Private functions declaration ---------------------------------------------*/
/* Exported functions definition ---------------------------------------------*/
T_PsdkReturnCode
Osal_TaskCreate(T_PsdkTaskHandle *task, void *(*taskFunc)(void *), const char *name, uint32_t stackSize, void *arg)
{
int result;
char nameDealed[16] = {0};
USER_UTIL_UNUSED(stackSize);
*task = malloc(sizeof(pthread_t));
if (*task == NULL) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
result = pthread_create(*task, NULL, taskFunc, arg);
if (result != 0) {
return PSDK_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 PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_PsdkReturnCode Osal_TaskDestroy(T_PsdkTaskHandle task)
{
pthread_cancel(*(pthread_t *) task);
free(task);
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_PsdkReturnCode Osal_TaskSleepMs(uint32_t timeMs)
{
usleep(1000 * timeMs);
return PSDK_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_PsdkReturnCode Osal_MutexCreate(T_PsdkMutexHandle *mutex)
{
int result;
*mutex = malloc(sizeof(pthread_mutex_t));
if (*mutex == NULL) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
result = pthread_mutex_init(*mutex, NULL);
if (result != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_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_PsdkReturnCode Osal_MutexDestroy(T_PsdkMutexHandle mutex)
{
int result;
result = pthread_mutex_destroy(mutex);
if (result != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
free(mutex);
return PSDK_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_PsdkReturnCode Osal_MutexLock(T_PsdkMutexHandle mutex)
{
int result = pthread_mutex_lock(mutex);
if (result != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_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_PsdkReturnCode Osal_MutexUnlock(T_PsdkMutexHandle mutex)
{
int result = pthread_mutex_unlock(mutex);
if (result != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_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_PsdkReturnCode Osal_SemaphoreCreate(T_PsdkSemHandle *semaphore, uint32_t initValue)
{
int result;
*semaphore = malloc(sizeof(sem_t));
if (*semaphore == NULL) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;
}
result = sem_init(*semaphore, 0, (unsigned int) initValue);
if (result != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_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_PsdkReturnCode Osal_SemaphoreDestroy(T_PsdkSemHandle semaphore)
{
int result;
result = sem_destroy((sem_t *) semaphore);
if (result != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
free(semaphore);
return PSDK_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_PsdkReturnCode Osal_SemaphoreWait(T_PsdkSemHandle semaphore)
{
int result;
result = sem_wait(semaphore);
if (result != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_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_PsdkReturnCode Osal_SemaphoreTimedWait(T_PsdkSemHandle 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 PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_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_PsdkReturnCode Osal_SemaphorePost(T_PsdkSemHandle semaphore)
{
int result;
result = sem_post(semaphore);
if (result != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/**
* @brief Get the system time for ms.
* @return an uint32 that the time of system, uint:ms
*/
T_PsdkReturnCode Osal_GetTimeMs(uint32_t *ms)
{
struct timeval time;
gettimeofday(&time, NULL);
*ms = (time.tv_sec * 1000 + time.tv_usec / 1000);
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
void *Osal_Malloc(uint32_t size)
{
return malloc(size);
}
void Osal_Free(void *ptr)
{
free(ptr);
}
/****************** (C) COPYRIGHT DJI Innovations *****END OF FILE****/

View File

@ -0,0 +1,77 @@
/**
********************************************************************
* @file osal.h
* @version V2.0.0
* @date 2019/8/28
* @brief This is the header file for "osal.c", defining the structure and
* (exported) function prototypes.
*
* @copyright (c) 2018-2019 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 "psdk_typedef.h"
#include "psdk_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Exported constants --------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
T_PsdkReturnCode
Osal_TaskCreate(T_PsdkTaskHandle *task, void *(*taskFunc)(void *), const char *name, uint32_t stackSize, void *arg);
T_PsdkReturnCode Osal_TaskDestroy(T_PsdkTaskHandle task);
T_PsdkReturnCode Osal_TaskSleepMs(uint32_t timeMs);
T_PsdkReturnCode Osal_MutexCreate(T_PsdkMutexHandle *mutex);
T_PsdkReturnCode Osal_MutexDestroy(T_PsdkMutexHandle mutex);
T_PsdkReturnCode Osal_MutexLock(T_PsdkMutexHandle mutex);
T_PsdkReturnCode Osal_MutexUnlock(T_PsdkMutexHandle mutex);
T_PsdkReturnCode Osal_SemaphoreCreate(T_PsdkSemHandle *semaphore, uint32_t initValue);
T_PsdkReturnCode Osal_SemaphoreDestroy(T_PsdkSemHandle semaphore);
T_PsdkReturnCode Osal_SemaphoreWait(T_PsdkSemHandle semaphore);
T_PsdkReturnCode Osal_SemaphoreTimedWait(T_PsdkSemHandle semaphore, uint32_t waitTime);
T_PsdkReturnCode Osal_SemaphorePost(T_PsdkSemHandle semaphore);
T_PsdkReturnCode Osal_GetTimeMs(uint32_t *ms);
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,219 @@
/**
********************************************************************
* @file upgrade_platform_opt_linux.c
* @version V2.0.0
* @date 3/25/20
* @brief
*
* @copyright (c) 2018-2019 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 <psdk_logger.h>
#include <psdk_upgrade.h>
/* Private constants ---------------------------------------------------------*/
#define PSDK_TEST_CMD_CALL_MAX_LEN (PSDK_FILE_PATH_SIZE_MAX + 256)
#define PSDK_REBOOT_STATE_FILE_NAME "reboot_state"
/* Private types -------------------------------------------------------------*/
/* Private values -------------------------------------------------------------*/
static FILE *s_upgradeProgramFile = NULL;
/* Private functions declaration ---------------------------------------------*/
static T_PsdkReturnCode PsdkTest_RunSystemCmd(char *systemCmdStr);
/* Exported functions definition ---------------------------------------------*/
T_PsdkReturnCode PsdkUpgradePlatformLinux_RebootSystem(void)
{
// attention: you need su permission to reboot system
return PsdkTest_RunSystemCmd("reboot -h now");
}
T_PsdkReturnCode PsdkUpgradePlatformLinux_CleanUpgradeProgramFileStoreArea(void)
{
char cmdBuffer[PSDK_TEST_CMD_CALL_MAX_LEN];
snprintf(cmdBuffer, PSDK_TEST_CMD_CALL_MAX_LEN, "rm -rf %s*", PSDK_TEST_UPGRADE_FILE_DIR);
return PsdkTest_RunSystemCmd(cmdBuffer);
}
T_PsdkReturnCode PsdkUpgradePlatformLinux_ReplaceOldProgram(void)
{
char cmdBuffer[PSDK_TEST_CMD_CALL_MAX_LEN];
snprintf(cmdBuffer, PSDK_TEST_CMD_CALL_MAX_LEN, "cp -f %s*_V*.*.*.bin %s", PSDK_TEST_UPGRADE_FILE_DIR,
PSDK_TEST_UPGRADE_OLD_FIRMWARE_PATH);
return PsdkTest_RunSystemCmd(cmdBuffer);
}
T_PsdkReturnCode PsdkUpgradePlatformLinux_SetUpgradeRebootState(const T_PsdkUpgradeEndInfo *upgradeEndInfo)
{
FILE *rebootStateFile;
size_t res;
T_PsdkReturnCode returnCode;
rebootStateFile = fopen(PSDK_REBOOT_STATE_FILE_NAME, "w+");
if (rebootStateFile == NULL) {
PsdkLogger_UserLogError("Create reboot state file error");
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
res = fwrite((uint8_t *) upgradeEndInfo, 1, sizeof(T_PsdkUpgradeEndInfo), rebootStateFile);
if (res != sizeof(T_PsdkUpgradeEndInfo)) {
PsdkLogger_UserLogError("Write data len is not equal");
returnCode = PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
goto out;
}
returnCode = PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
out:
fclose(rebootStateFile);
return returnCode;
}
T_PsdkReturnCode PsdkUpgradePlatformLinux_GetUpgradeRebootState(bool *isUpgradeReboot,
T_PsdkUpgradeEndInfo *upgradeEndInfo)
{
FILE *rebootStateFile;
size_t res;
rebootStateFile = fopen(PSDK_REBOOT_STATE_FILE_NAME, "r");
if (rebootStateFile == NULL) {
*isUpgradeReboot = false;
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
res = fread(upgradeEndInfo, 1, sizeof(T_PsdkUpgradeEndInfo), rebootStateFile);
if (res != sizeof(T_PsdkUpgradeEndInfo)) {
PsdkLogger_UserLogError("Read data len is not equal");
*isUpgradeReboot = false;
goto out;
}
*isUpgradeReboot = true;
out:
fclose(rebootStateFile);
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_PsdkReturnCode PsdkUpgradePlatformLinux_CleanUpgradeRebootState(void)
{
return PsdkTest_RunSystemCmd("rm -f "PSDK_REBOOT_STATE_FILE_NAME);
}
T_PsdkReturnCode PsdkUpgradePlatformLinux_CreateUpgradeProgramFile(const T_PsdkUpgradeFileInfo *fileInfo)
{
char filePath[PSDK_FILE_PATH_SIZE_MAX];
s_upgradeProgramFile = NULL;
snprintf(filePath, PSDK_FILE_PATH_SIZE_MAX, "%s%s", PSDK_TEST_UPGRADE_FILE_DIR, fileInfo->fileName);
s_upgradeProgramFile = fopen(filePath, "w+");
if (s_upgradeProgramFile == NULL) {
PsdkLogger_UserLogError("Upgrade program file can't create");
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_PsdkReturnCode
PsdkUpgradePlatformLinux_WriteUpgradeProgramFile(uint32_t offset, const uint8_t *data, uint16_t dataLen)
{
size_t writeLen;
if (s_upgradeProgramFile == NULL) {
PsdkLogger_UserLogError("upgrade program file can't be NULL");
return PSDK_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
if (fseek(s_upgradeProgramFile, offset, SEEK_SET) != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
writeLen = fwrite(data, 1, dataLen, s_upgradeProgramFile);
if (writeLen != dataLen) {
PsdkLogger_UserLogError("Write upgrade program file error, writeLen = %d, dataLen = %d", writeLen);
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_PsdkReturnCode PsdkUpgradePlatformLinux_ReadUpgradeProgramFile(uint32_t offset, uint16_t readDataLen, uint8_t *data,
uint16_t *realLen)
{
uint32_t readRtn;
if (s_upgradeProgramFile == NULL) {
PsdkLogger_UserLogError("upgrade program file can't be NULL");
return PSDK_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
if (fseek(s_upgradeProgramFile, offset, SEEK_SET) != 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
readRtn = fread(data, 1, readDataLen, s_upgradeProgramFile);
if (readRtn == 0 || readRtn > readDataLen) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
*realLen = readRtn;
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
T_PsdkReturnCode PsdkUpgradePlatformLinux_CloseUpgradeProgramFile(void)
{
if (s_upgradeProgramFile == NULL) {
PsdkLogger_UserLogError("upgrade program file can't be NULL");
return PSDK_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
fclose(s_upgradeProgramFile);
s_upgradeProgramFile = NULL;
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}
/* Private functions definition-----------------------------------------------*/
static T_PsdkReturnCode PsdkTest_RunSystemCmd(char *systemCmdStr)
{
int status;
status = system(systemCmdStr);
if (status == -1 || WIFEXITED(status) == false) {
PsdkLogger_UserLogError("Call %s error, status = %d", systemCmdStr, status);
return PSDK_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;
}
if (WEXITSTATUS(status) == 0) {
return PSDK_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
} else {
PsdkLogger_UserLogError("Exit status is = %d", WEXITSTATUS(status));
return PSDK_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
}
}
/****************** (C) COPYRIGHT DJI Innovations *****END OF FILE****/

View File

@ -0,0 +1,71 @@
/**
********************************************************************
* @file upgrade_platform_opt_linux.h
* @version V2.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) 2018-2019 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 <psdk_typedef.h>
#include <psdk_upgrade.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Exported constants --------------------------------------------------------*/
#define PSDK_TEST_UPGRADE_OLD_FIRMWARE_PATH "/usr/local/bin/psdk_demo"
#define PSDK_TEST_UPGRADE_FILE_DIR "/upgrade/"
/* Exported types ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
T_PsdkReturnCode PsdkUpgradePlatformLinux_RebootSystem(void);
T_PsdkReturnCode PsdkUpgradePlatformLinux_CleanUpgradeProgramFileStoreArea(void);
T_PsdkReturnCode PsdkUpgradePlatformLinux_CreateUpgradeProgramFile(const T_PsdkUpgradeFileInfo *fileInfo);
T_PsdkReturnCode PsdkUpgradePlatformLinux_WriteUpgradeProgramFile(uint32_t offset, const uint8_t *data,
uint16_t dataLen);
T_PsdkReturnCode PsdkUpgradePlatformLinux_ReadUpgradeProgramFile(uint32_t offset, uint16_t readDataLen, uint8_t *data,
uint16_t *realLen);
T_PsdkReturnCode PsdkUpgradePlatformLinux_CloseUpgradeProgramFile(void);
T_PsdkReturnCode PsdkUpgradePlatformLinux_ReplaceOldProgram(void);
T_PsdkReturnCode PsdkUpgradePlatformLinux_SetUpgradeRebootState(const T_PsdkUpgradeEndInfo *upgradeEndInfo);
T_PsdkReturnCode PsdkUpgradePlatformLinux_GetUpgradeRebootState(bool *isUpgradeReboot,
T_PsdkUpgradeEndInfo *upgradeEndInfo);
T_PsdkReturnCode PsdkUpgradePlatformLinux_CleanUpgradeRebootState(void);
#ifdef __cplusplus
}
#endif
#endif // UPGRADE_PLATFORM_OPT_LINUX_H
/************************ (C) COPYRIGHT DJI Innovations *******END OF FILE******/