作者 ookk303

临时保存

1 -/**  
2 - ********************************************************************  
3 - * @file main.c  
4 - * @brief  
5 - *  
6 - * @copyright (c) 2021 ZIYAN. All rights reserved.  
7 - *  
8 - * All information contained herein is, and remains, the property of ZIYAN.  
9 - * The intellectual and technical concepts contained herein are proprietary  
10 - * to ZIYAN and may be covered by U.S. and foreign patents, patents in process,  
11 - * and protected by trade secret or copyright law. Dissemination of this  
12 - * information, including but not limited to data and other proprietary  
13 - * material(s) incorporated within the information, in any form, is strictly  
14 - * prohibited without the express written consent of ZIYAN.  
15 - *  
16 - * If you receive this source code without ZIYAN’s authorization, you may not  
17 - * further disseminate the information, and you must immediately remove the  
18 - * source code and notify ZIYAN of its removal. ZIYAN reserves the right to pursue  
19 - * legal actions against you for any loss(es) or damage(s) caused by your  
20 - * failure to do so.  
21 - *  
22 - *********************************************************************  
23 - */  
24 -  
25 -/* Includes ------------------------------------------------------------------*/  
26 -#include <ziyan_platform.h>  
27 -#include <ziyan_logger.h>  
28 -#include <ziyan_core.h>  
29 -#include <utils/util_misc.h>  
30 -#include <errno.h>  
31 -#include <signal.h>  
32 -#include "monitor/sys_monitor.h"  
33 -#include "osal/osal.h"  
34 -#include "osal/osal_fs.h"  
35 -#include "osal/osal_socket.h"  
36 -#include "../hal/hal_uart.h"  
37 -#include "../hal/hal_network.h"  
38 -#include "../hal/hal_usb_bulk.h"  
39 -#include "ziyan_sdk_app_info.h"  
40 -#include "ziyan_aircraft_info.h"  
41 -#include "widget/test_widget.h"  
42 -#include "ziyan_sdk_config.h"  
43 -  
44 -#include "camera_emu/test_payload_cam_emu_base.h"  
45 -  
46 -#include "Zy_Expansion.h"  
47 -#include "BaseConfig.h"  
48 -  
49 -/* Private constants ---------------------------------------------------------*/  
50 -#define ZIYAN_LOG_PATH "Logs/ZIYAN"  
51 -#define ZIYAN_LOG_INDEX_FILE_NAME "Logs/latest"  
52 -#define ZIYAN_LOG_FOLDER_NAME "Logs"  
53 -#define ZIYAN_LOG_PATH_MAX_SIZE (128)  
54 -#define ZIYAN_LOG_FOLDER_NAME_MAX_SIZE (32)  
55 -#define ZIYAN_LOG_MAX_COUNT (10)  
56 -#define ZIYAN_SYSTEM_CMD_STR_MAX_SIZE (64)  
57 -#define ZIYAN_SYSTEM_RESULT_STR_MAX_SIZE (128)  
58 -  
59 -#define ZIYAN_USE_WIDGET_INTERACTION 0  
60 -  
61 -/* Private types -------------------------------------------------------------*/  
62 -typedef struct {  
63 - pid_t tid;  
64 - char name[16];  
65 - float pcpu;  
66 -} T_ThreadAttribute;  
67 -  
68 -/* Private values -------------------------------------------------------------*/  
69 -static FILE *s_djiLogFile;  
70 -static FILE *s_djiLogFileCnt;  
71 -static pthread_t s_monitorThread = 0;  
72 -  
73 -/* Private functions declaration ---------------------------------------------*/  
74 -static T_ZiyanReturnCode ZiyanUser_PrepareSystemEnvironment(void);  
75 -static T_ZiyanReturnCode ZiyanUser_FillInUserInfo(T_ZiyanUserInfo *userInfo);  
76 -static T_ZiyanReturnCode ZiyanUser_PrintConsole(const uint8_t *data, uint16_t dataLen);  
77 -static T_ZiyanReturnCode ZiyanUser_LocalWrite(const uint8_t *data, uint16_t dataLen);  
78 -static T_ZiyanReturnCode ZiyanUser_LocalWriteFsInit(const char *path);  
79 -// static void *ZiyanUser_MonitorTask(void *argument);  
80 -// static T_ZiyanReturnCode ZiyanTest_HighPowerApplyPinInit();  
81 -// static T_ZiyanReturnCode ZiyanTest_WriteHighPowerApplyPin(E_ZiyanPowerManagementPinState pinState);  
82 -static void ZiyanUser_NormalExitHandler(int signalNum);  
83 -  
84 -/* Exported functions definition ---------------------------------------------*/  
85 -int main(int argc, char **argv)  
86 -{  
87 - T_ZiyanReturnCode returnCode;  
88 - T_ZiyanUserInfo userInfo;  
89 - T_ZiyanAircraftInfoBaseInfo aircraftInfoBaseInfo;  
90 - T_ZiyanAircraftVersion aircraftInfoVersion;  
91 - T_ZiyanFirmwareVersion firmwareVersion = {  
92 - .majorVersion = 1,  
93 - .minorVersion = 0,  
94 - .modifyVersion = 0,  
95 - .debugVersion = 0,  
96 - };  
97 -  
98 - USER_UTIL_UNUSED(argc);  
99 - USER_UTIL_UNUSED(argv);  
100 -  
101 - // attention: when the program is hand up ctrl-c will generate the coredump file  
102 - // 注意:当程序挂起时,按下Ctrl-C会生成coredump文件  
103 - signal(SIGTERM, ZiyanUser_NormalExitHandler);  
104 -  
105 - /*!< Step 1: Prepare system environment, such as osal, hal uart, console function and so on. */  
106 - /*!< 步骤1:准备系统环境,例如osal、hal uart、控制台功能等 */  
107 - returnCode = ZiyanUser_PrepareSystemEnvironment();  
108 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
109 - USER_LOG_ERROR("Prepare system environment error");  
110 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
111 - }  
112 -  
113 - USER_LOG_INFO("run main test: %d:%d", 111, __LINE__);  
114 -  
115 - /*!< Step 2: Fill your application information in dji_sdk_app_info.h and use this interface to fill it. */  
116 - /*!< 步骤2:在dji_sdk_app_info.h中填写应用信息,并使用本接口填充信息 */  
117 - returnCode = ZiyanUser_FillInUserInfo(&userInfo);  
118 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
119 - USER_LOG_ERROR("Fill user info error, please check user info config");  
120 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
121 - }  
122 -  
123 - // /*!< Step 3: Initialize the Payload SDK core by your application information. */  
124 - // /*!< 步骤3:通过应用信息初始化Payload SDK核心 */  
125 - returnCode = ZiyanCore_Init(&userInfo);  
126 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
127 - USER_LOG_ERROR("Core init error");  
128 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
129 - }  
130 -  
131 - returnCode = ZiyanAircraftInfo_GetBaseInfo(&aircraftInfoBaseInfo);  
132 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
133 - USER_LOG_ERROR("get aircraft base info error");  
134 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
135 - }  
136 -  
137 - // returnCode = ZiyanAircraftInfo_GetAircraftVersion(&aircraftInfoVersion);  
138 - // if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
139 - // USER_LOG_ERROR("get aircraft version info error");  
140 - // } else {  
141 - // USER_LOG_INFO("Aircraft version is V%02d.%02d.%02d.%02d", aircraftInfoVersion.majorVersion,  
142 - // aircraftInfoVersion.minorVersion, aircraftInfoVersion.modifyVersion,  
143 - // aircraftInfoVersion.debugVersion);  
144 - // }  
145 -  
146 - // returnCode = ZiyanCore_SetAlias("PSDK_APPALIAS");  
147 - // if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
148 - // USER_LOG_ERROR("set alias error");  
149 - // return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
150 - // }  
151 -  
152 - // returnCode = ZiyanCore_SetFirmwareVersion(firmwareVersion);  
153 - // if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
154 - // USER_LOG_ERROR("set firmware version error");  
155 - // return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
156 - // }  
157 -  
158 - // returnCode = ZiyanCore_SetSerialNumber("PSDK12345678XX");  
159 - // if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
160 - // USER_LOG_ERROR("set serial number error");  
161 - // return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
162 - // }  
163 -  
164 -// /*!< Step 4: Initialize the selected modules by macros in dji_sdk_config.h . */  
165 -// /*!< 步骤4:通过dji_sdk_config.h中的宏初始化选中的模块 */  
166 -// #ifdef CONFIG_MODULE_SAMPLE_POWER_MANAGEMENT_ON  
167 -// T_ZiyanTestApplyHighPowerHandler applyHighPowerHandler = {  
168 -// .pinInit = ZiyanTest_HighPowerApplyPinInit,  
169 -// .pinWrite = ZiyanTest_WriteHighPowerApplyPin,  
170 -// };  
171 -  
172 -// returnCode = ZiyanTest_RegApplyHighPowerHandler(&applyHighPowerHandler);  
173 -// if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
174 -// USER_LOG_ERROR("regsiter apply high power handler error");  
175 -// }  
176 -  
177 -// returnCode = ZiyanTest_PowerManagementStartService();  
178 -// if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
179 -// USER_LOG_ERROR("power management init error");  
180 -// }  
181 -// #endif  
182 -  
183 -// #ifdef CONFIG_MODULE_SAMPLE_DATA_TRANSMISSION_ON  
184 -// returnCode = ZiyanTest_DataTransmissionStartService();  
185 -// if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
186 -// USER_LOG_ERROR("widget sample init error");  
187 -// }  
188 -// #endif  
189 -  
190 -// if (aircraftInfoBaseInfo.mountPosition == ZIYAN_MOUNT_POSITION_EXTENSION_PORT &&  
191 -// aircraftInfoBaseInfo.aircraftType == ZIYAN_AIRCRAFT_TYPE_M300_RTK) {  
192 -// returnCode = ZiyanTest_WidgetInteractionStartService();  
193 -// if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
194 -// USER_LOG_ERROR("widget interaction sample init error");  
195 -// }  
196 -  
197 -// returnCode = ZiyanTest_WidgetSpeakerStartService();  
198 -// if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
199 -// USER_LOG_ERROR("widget speaker test init error");  
200 -// }  
201 -// } else {  
202 -#ifdef CONFIG_MODULE_SAMPLE_CAMERA_EMU_ON  
203 - returnCode = ZiyanTest_CameraEmuBaseStartService();  
204 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
205 - USER_LOG_ERROR("camera emu common init error");  
206 - }  
207 -#endif  
208 -  
209 -#ifdef CONFIG_MODULE_SAMPLE_CAMERA_MEDIA_ON  
210 - returnCode = ZiyanTest_CameraEmuMediaStartService();  
211 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
212 - USER_LOG_ERROR("camera emu media init error");  
213 - }  
214 -#endif  
215 -  
216 -#ifdef CONFIG_MODULE_SAMPLE_FC_SUBSCRIPTION_ON  
217 - returnCode = ZiyanTest_FcSubscriptionStartService();  
218 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
219 - USER_LOG_ERROR("data subscription sample init error\n");  
220 - }  
221 -#endif  
222 -  
223 -#ifdef CONFIG_MODULE_SAMPLE_GIMBAL_EMU_ON  
224 - if (aircraftInfoBaseInfo.ziyanAdapterType == ZIYAN_SDK_ADAPTER_TYPE_NONE ||  
225 - aircraftInfoBaseInfo.ziyanAdapterType == ZIYAN_SDK_ADAPTER_TYPE_UNKNOWN) {  
226 -  
227 - if (ZiyanTest_GimbalStartService() != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
228 - USER_LOG_ERROR("psdk gimbal init error");  
229 - }  
230 - }  
231 -#endif  
232 -  
233 -// #ifdef CONFIG_MODULE_SAMPLE_XPORT_ON  
234 -// if (aircraftInfoBaseInfo.djiAdapterType == ZIYAN_SDK_ADAPTER_TYPE_XPORT) {  
235 -// if (ZiyanTest_XPortStartService() != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
236 -// USER_LOG_ERROR("psdk xport init error");  
237 -// }  
238 -// }  
239 -// #endif  
240 -  
241 - //配置拓展接口  
242 - Zy_JZsdkToPsdk_ExpnasionInit();  
243 -  
244 - //自己配置  
245 - Main_APP_Psdk();  
246 -  
247 -  
248 -#ifdef CONFIG_MODULE_SAMPLE_WIDGET_ON  
249 -#if ZIYAN_USE_WIDGET_INTERACTION  
250 - returnCode = ZiyanTest_WidgetInteractionStartService();  
251 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
252 - USER_LOG_ERROR("widget interaction test init error");  
253 - }  
254 -#else  
255 - returnCode = ZiyanTest_WidgetStartService();  
256 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
257 - USER_LOG_ERROR("widget sample init error");  
258 - }  
259 -#endif  
260 -#endif  
261 -  
262 -#ifdef CONFIG_MODULE_SAMPLE_WIDGET_SPEAKER_ON  
263 - returnCode = ZiyanTest_WidgetSpeakerStartService();  
264 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
265 - USER_LOG_ERROR("widget speaker test init error");  
266 - }  
267 -#endif  
268 -  
269 -// #ifdef CONFIG_MODULE_SAMPLE_MOP_CHANNEL_ON  
270 -// returnCode = ZiyanTest_MopChannelStartService();  
271 -// if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
272 -// USER_LOG_ERROR("mop channel sample init error");  
273 -// }  
274 -// #endif  
275 -  
276 -// #ifdef CONFIG_MODULE_SAMPLE_PAYLOAD_COLLABORATION_ON  
277 -// if (aircraftInfoBaseInfo.djiAdapterType == ZIYAN_SDK_ADAPTER_TYPE_SKYPORT_V2 ||  
278 -// aircraftInfoBaseInfo.djiAdapterType == ZIYAN_SDK_ADAPTER_TYPE_XPORT) {  
279 -// returnCode = ZiyanTest_PayloadCollaborationStartService();  
280 -// if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
281 -// USER_LOG_ERROR("Payload collaboration sample init error\n");  
282 -// }  
283 -// }  
284 -// #endif  
285 -  
286 -// #ifdef CONFIG_MODULE_SAMPLE_UPGRADE_ON  
287 -// T_ZiyanTestUpgradePlatformOpt linuxUpgradePlatformOpt = {  
288 -// .rebootSystem = ZiyanUpgradePlatformLinux_RebootSystem,  
289 -// .cleanUpgradeProgramFileStoreArea = ZiyanUpgradePlatformLinux_CleanUpgradeProgramFileStoreArea,  
290 -// .createUpgradeProgramFile = ZiyanUpgradePlatformLinux_CreateUpgradeProgramFile,  
291 -// .writeUpgradeProgramFile = ZiyanUpgradePlatformLinux_WriteUpgradeProgramFile,  
292 -// .readUpgradeProgramFile = ZiyanUpgradePlatformLinux_ReadUpgradeProgramFile,  
293 -// .closeUpgradeProgramFile = ZiyanUpgradePlatformLinux_CloseUpgradeProgramFile,  
294 -// .replaceOldProgram = ZiyanUpgradePlatformLinux_ReplaceOldProgram,  
295 -// .setUpgradeRebootState = ZiyanUpgradePlatformLinux_SetUpgradeRebootState,  
296 -// .getUpgradeRebootState = ZiyanUpgradePlatformLinux_GetUpgradeRebootState,  
297 -// .cleanUpgradeRebootState = ZiyanUpgradePlatformLinux_CleanUpgradeRebootState,  
298 -// };  
299 -// T_ZiyanTestUpgradeConfig testUpgradeConfig = {  
300 -// .firmwareVersion = firmwareVersion,  
301 -// .transferType = ZIYAN_FIRMWARE_TRANSFER_TYPE_DCFTP,  
302 -// .needReplaceProgramBeforeReboot = true  
303 -// };  
304 -// if (ZiyanTest_UpgradeStartService(&linuxUpgradePlatformOpt, testUpgradeConfig) !=  
305 -// ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
306 -// USER_LOG_ERROR("psdk upgrade init error");  
307 -// }  
308 -// #endif  
309 -  
310 -// #ifdef CONFIG_MODULE_SAMPLE_HMS_CUSTOMIZATION_ON  
311 -// returnCode = ZiyanTest_HmsCustomizationStartService();  
312 -// if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
313 -// USER_LOG_ERROR("hms test init error");  
314 -// }  
315 -// #endif  
316 -// }  
317 -  
318 -  
319 - // /*!< Step 5: Tell the ZIYAN Pilot you are ready. */  
320 - returnCode = ZiyanCore_ApplicationStart();  
321 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
322 - USER_LOG_ERROR("start sdk application error");  
323 - }  
324 -  
325 - // if (pthread_create(&s_monitorThread, NULL, ZiyanUser_MonitorTask, NULL) != 0) {  
326 - // USER_LOG_ERROR("create monitor task fail.");  
327 - // }  
328 -  
329 - // if (pthread_setname_np(s_monitorThread, "monitor task") != 0) {  
330 - // USER_LOG_ERROR("set name for monitor task fail.");  
331 - // }  
332 -  
333 - while (1) {  
334 - sleep(1);  
335 - }  
336 -}  
337 -  
338 -/* Private functions definition-----------------------------------------------*/  
339 -static T_ZiyanReturnCode ZiyanUser_PrepareSystemEnvironment(void)  
340 -{  
341 - T_ZiyanReturnCode returnCode;  
342 - T_ZiyanOsalHandler osalHandler = {  
343 - .TaskCreate = Osal_TaskCreate,  
344 - .TaskDestroy = Osal_TaskDestroy,  
345 - .TaskSleepMs = Osal_TaskSleepMs,  
346 - .MutexCreate= Osal_MutexCreate,  
347 - .MutexDestroy = Osal_MutexDestroy,  
348 - .MutexLock = Osal_MutexLock,  
349 - .MutexUnlock = Osal_MutexUnlock,  
350 - .SemaphoreCreate = Osal_SemaphoreCreate,  
351 - .SemaphoreDestroy = Osal_SemaphoreDestroy,  
352 - .SemaphoreWait = Osal_SemaphoreWait,  
353 - .SemaphoreTimedWait = Osal_SemaphoreTimedWait,  
354 - .SemaphorePost = Osal_SemaphorePost,  
355 - .Malloc = Osal_Malloc,  
356 - .Free = Osal_Free,  
357 - .GetRandomNum = Osal_GetRandomNum,  
358 - .GetTimeMs = Osal_GetTimeMs,  
359 - .GetTimeUs = Osal_GetTimeUs,  
360 - };  
361 -  
362 - T_ZiyanLoggerConsole printConsole = {  
363 - .func = ZiyanUser_PrintConsole,  
364 - .consoleLevel = ZIYAN_LOGGER_CONSOLE_LOG_LEVEL_INFO,  
365 - .isSupportColor = true,  
366 - };  
367 -  
368 - T_ZiyanLoggerConsole localRecordConsole = {  
369 - .consoleLevel = ZIYAN_LOGGER_CONSOLE_LOG_LEVEL_DEBUG,  
370 - .func = ZiyanUser_LocalWrite,  
371 - .isSupportColor = true,  
372 - };  
373 -  
374 - T_ZiyanHalUartHandler uartHandler = {  
375 - .UartInit = HalUart_Init,  
376 - .UartDeInit = HalUart_DeInit,  
377 - .UartWriteData = HalUart_WriteData,  
378 - .UartReadData = HalUart_ReadData,  
379 - .UartGetStatus = HalUart_GetStatus,  
380 - };  
381 -  
382 - T_ZiyanHalNetworkHandler networkHandler = {  
383 - .NetworkInit = HalNetWork_Init,  
384 - .NetworkDeInit = HalNetWork_DeInit,  
385 - .NetworkGetDeviceInfo = HalNetWork_GetDeviceInfo,  
386 - };  
387 -  
388 - T_ZiyanHalUsbBulkHandler usbBulkHandler = {  
389 - .UsbBulkInit = HalUsbBulk_Init,  
390 - .UsbBulkDeInit = HalUsbBulk_DeInit,  
391 - .UsbBulkWriteData = HalUsbBulk_WriteData,  
392 - .UsbBulkReadData = HalUsbBulk_ReadData,  
393 - .UsbBulkGetDeviceInfo = HalUsbBulk_GetDeviceInfo,  
394 - };  
395 -  
396 - T_ZiyanFileSystemHandler fileSystemHandler = {  
397 - .FileOpen = Osal_FileOpen,  
398 - .FileClose = Osal_FileClose,  
399 - .FileWrite = Osal_FileWrite,  
400 - .FileRead = Osal_FileRead,  
401 - .FileSync = Osal_FileSync,  
402 - .FileSeek = Osal_FileSeek,  
403 - .DirOpen = Osal_DirOpen,  
404 - .DirClose = Osal_DirClose,  
405 - .DirRead = Osal_DirRead,  
406 - .Mkdir = Osal_Mkdir,  
407 - .Unlink = Osal_Unlink,  
408 - .Rename = Osal_Rename,  
409 - .Stat = Osal_Stat,  
410 - };  
411 -  
412 - T_ZiyanSocketHandler socketHandler = {  
413 - .Socket = Osal_Socket,  
414 - .Bind = Osal_Bind,  
415 - .Close = Osal_Close,  
416 - .UdpSendData = Osal_UdpSendData,  
417 - .UdpRecvData = Osal_UdpRecvData,  
418 - .TcpListen = Osal_TcpListen,  
419 - .TcpAccept = Osal_TcpAccept,  
420 - .TcpConnect = Osal_TcpConnect,  
421 - .TcpSendData = Osal_TcpSendData,  
422 - .TcpRecvData = Osal_TcpRecvData,  
423 - };  
424 -  
425 - returnCode = ZiyanPlatform_RegOsalHandler(&osalHandler);  
426 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
427 - printf("register osal handler error");  
428 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
429 - }  
430 -  
431 - returnCode = ZiyanPlatform_RegHalUartHandler(&uartHandler);  
432 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
433 - printf("register hal uart handler error");  
434 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
435 - }  
436 -  
437 - if (ZiyanUser_LocalWriteFsInit(ZIYAN_LOG_PATH) != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
438 - printf("file system init error");  
439 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_UNKNOWN;  
440 - }  
441 -  
442 - returnCode = ZiyanLogger_AddConsole(&printConsole);  
443 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
444 - printf("add printf console error");  
445 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
446 - }  
447 -  
448 - returnCode = ZiyanLogger_AddConsole(&localRecordConsole);  
449 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
450 - printf("add printf console error");  
451 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
452 - }  
453 -  
454 -  
455 -#if (CONFIG_HARDWARE_CONNECTION == ZIYAN_USE_UART_AND_USB_BULK_DEVICE)  
456 - returnCode = ZiyanPlatform_RegHalUsbBulkHandler(&usbBulkHandler);  
457 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
458 - printf("register hal usb bulk handler error");  
459 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
460 - }  
461 -#elif (CONFIG_HARDWARE_CONNECTION == ZIYAN_USE_UART_AND_NETWORK_DEVICE)  
462 - returnCode = ZiyanPlatform_RegHalNetworkHandler(&networkHandler);  
463 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
464 - printf("register hal network handler error");  
465 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
466 - }  
467 -  
468 - //Attention: if you want to use camera stream view function, please uncomment it.  
469 - returnCode = ZiyanPlatform_RegSocketHandler(&socketHandler);  
470 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
471 - printf("register osal socket handler error");  
472 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
473 - }  
474 -#elif (CONFIG_HARDWARE_CONNECTION == ZIYAN_USE_ONLY_UART)  
475 - /*!< Attention: Only use uart hardware connection.  
476 - */  
477 -#endif  
478 -  
479 - returnCode = ZiyanPlatform_RegFileSystemHandler(&fileSystemHandler);  
480 - if (returnCode != ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS) {  
481 - printf("register osal filesystem handler error");  
482 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
483 - }  
484 -  
485 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
486 -}  
487 -  
488 -static T_ZiyanReturnCode ZiyanUser_FillInUserInfo(T_ZiyanUserInfo *userInfo)  
489 -{  
490 - if (userInfo == NULL) {  
491 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
492 - }  
493 -  
494 - memset(userInfo->appName, 0, sizeof(userInfo->appName));  
495 - memset(userInfo->appId, 0, sizeof(userInfo->appId));  
496 - memset(userInfo->appKey, 0, sizeof(userInfo->appKey));  
497 - memset(userInfo->appLicense, 0, sizeof(userInfo->appLicense));  
498 - memset(userInfo->developerAccount, 0, sizeof(userInfo->developerAccount));  
499 - memset(userInfo->baudRate, 0, sizeof(userInfo->baudRate));  
500 -  
501 - if (strlen(USER_APP_NAME) >= sizeof(userInfo->appName) ||  
502 - strlen(USER_APP_ID) > sizeof(userInfo->appId) ||  
503 - strlen(USER_APP_KEY) > sizeof(userInfo->appKey) ||  
504 - strlen(USER_APP_LICENSE) > sizeof(userInfo->appLicense) ||  
505 - strlen(USER_DEVELOPER_ACCOUNT) >= sizeof(userInfo->developerAccount) ||  
506 - strlen(USER_BAUD_RATE) > sizeof(userInfo->baudRate)) {  
507 - USER_LOG_ERROR("Length of user information string is beyond limit. Please check.");  
508 - sleep(1);  
509 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
510 - }  
511 -  
512 - if (!strcmp(USER_APP_NAME, "your_app_name") ||  
513 - !strcmp(USER_APP_ID, "your_app_id") ||  
514 - !strcmp(USER_APP_KEY, "your_app_key") ||  
515 - !strcmp(USER_BAUD_RATE, "your_app_license") ||  
516 - !strcmp(USER_DEVELOPER_ACCOUNT, "your_developer_account") ||  
517 - !strcmp(USER_BAUD_RATE, "your_baud_rate")) {  
518 - USER_LOG_ERROR(  
519 - "Please fill in correct user information to 'samples/sample_c/platform/linux/manifold2/application/dji_sdk_app_info.h' file.");  
520 - sleep(1);  
521 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
522 - }  
523 -  
524 - strncpy(userInfo->appName, USER_APP_NAME, sizeof(userInfo->appName) - 1);  
525 - memcpy(userInfo->appId, USER_APP_ID, USER_UTIL_MIN(sizeof(userInfo->appId), strlen(USER_APP_ID)));  
526 - memcpy(userInfo->appKey, USER_APP_KEY, USER_UTIL_MIN(sizeof(userInfo->appKey), strlen(USER_APP_KEY)));  
527 - memcpy(userInfo->appLicense, USER_APP_LICENSE,  
528 - USER_UTIL_MIN(sizeof(userInfo->appLicense), strlen(USER_APP_LICENSE)));  
529 - memcpy(userInfo->baudRate, USER_BAUD_RATE, USER_UTIL_MIN(sizeof(userInfo->baudRate), strlen(USER_BAUD_RATE)));  
530 - strncpy(userInfo->developerAccount, USER_DEVELOPER_ACCOUNT, sizeof(userInfo->developerAccount) - 1);  
531 -  
532 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
533 -}  
534 -  
535 -static T_ZiyanReturnCode ZiyanUser_PrintConsole(const uint8_t *data, uint16_t dataLen)  
536 -{  
537 - USER_UTIL_UNUSED(dataLen);  
538 -  
539 - printf("%s", data);  
540 -  
541 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
542 -}  
543 -  
544 -static T_ZiyanReturnCode ZiyanUser_LocalWrite(const uint8_t *data, uint16_t dataLen)  
545 -{  
546 - uint32_t realLen;  
547 -  
548 - if (s_djiLogFile == NULL) {  
549 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_UNKNOWN;  
550 - }  
551 -  
552 - realLen = fwrite(data, 1, dataLen, s_djiLogFile);  
553 - fflush(s_djiLogFile);  
554 - if (realLen == dataLen) {  
555 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
556 - } else {  
557 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_UNKNOWN;  
558 - }  
559 -}  
560 -  
561 -static T_ZiyanReturnCode ZiyanUser_LocalWriteFsInit(const char *path)  
562 -{  
563 - T_ZiyanReturnCode djiReturnCode = ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
564 - char filePath[ZIYAN_LOG_PATH_MAX_SIZE];  
565 - char systemCmd[ZIYAN_SYSTEM_CMD_STR_MAX_SIZE];  
566 - char folderName[ZIYAN_LOG_FOLDER_NAME_MAX_SIZE];  
567 - time_t currentTime = time(NULL);  
568 - struct tm *localTime = localtime(&currentTime);  
569 - uint16_t logFileIndex = 0;  
570 - uint16_t currentLogFileIndex;  
571 - uint8_t ret;  
572 -  
573 - if (localTime == NULL) {  
574 - printf("Get local time error.\r\n");  
575 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
576 - }  
577 -  
578 - if (access(ZIYAN_LOG_FOLDER_NAME, F_OK) != 0) {  
579 - sprintf(folderName, "mkdir %s", ZIYAN_LOG_FOLDER_NAME);  
580 - ret = system(folderName);  
581 - if (ret != 0) {  
582 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
583 - }  
584 - }  
585 -  
586 - s_djiLogFileCnt = fopen(ZIYAN_LOG_INDEX_FILE_NAME, "rb+");  
587 - if (s_djiLogFileCnt == NULL) {  
588 - s_djiLogFileCnt = fopen(ZIYAN_LOG_INDEX_FILE_NAME, "wb+");  
589 - if (s_djiLogFileCnt == NULL) {  
590 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
591 - }  
592 - } else {  
593 - ret = fseek(s_djiLogFileCnt, 0, SEEK_SET);  
594 - if (ret != 0) {  
595 - printf("Seek log count file error, ret: %d, errno: %d.\r\n", ret, errno);  
596 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
597 - }  
598 -  
599 - ret = fread((uint16_t *) &logFileIndex, 1, sizeof(uint16_t), s_djiLogFileCnt);  
600 - if (ret != sizeof(uint16_t)) {  
601 - printf("Read log file index error.\r\n");  
602 - }  
603 - }  
604 -  
605 - currentLogFileIndex = logFileIndex;  
606 - logFileIndex++;  
607 -  
608 - ret = fseek(s_djiLogFileCnt, 0, SEEK_SET);  
609 - if (ret != 0) {  
610 - printf("Seek log file error, ret: %d, errno: %d.\r\n", ret, errno);  
611 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
612 - }  
613 -  
614 - ret = fwrite((uint16_t *) &logFileIndex, 1, sizeof(uint16_t), s_djiLogFileCnt);  
615 - if (ret != sizeof(uint16_t)) {  
616 - printf("Write log file index error.\r\n");  
617 - fclose(s_djiLogFileCnt);  
618 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
619 - }  
620 -  
621 - fclose(s_djiLogFileCnt);  
622 -  
623 - sprintf(filePath, "%s_%04d_%04d%02d%02d_%02d-%02d-%02d.log", path, currentLogFileIndex,  
624 - localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday,  
625 - localTime->tm_hour, localTime->tm_min, localTime->tm_sec);  
626 -  
627 - s_djiLogFile = fopen(filePath, "wb+");  
628 - if (s_djiLogFile == NULL) {  
629 - USER_LOG_ERROR("Open filepath time error.");  
630 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
631 - }  
632 -  
633 - if (logFileIndex >= ZIYAN_LOG_MAX_COUNT) {  
634 - sprintf(systemCmd, "rm -rf %s_%04d*.log", path, currentLogFileIndex - ZIYAN_LOG_MAX_COUNT);  
635 - ret = system(systemCmd);  
636 - if (ret != 0) {  
637 - printf("Remove file error, ret:%d.\r\n", ret);  
638 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
639 - }  
640 - }  
641 -  
642 - return djiReturnCode;  
643 -}  
644 -  
645 -// #pragma GCC diagnostic push  
646 -// #pragma GCC diagnostic ignored "-Wmissing-noreturn"  
647 -// #pragma GCC diagnostic ignored "-Wreturn-type"  
648 -  
649 -// static void *ZiyanUser_MonitorTask(void *argument)  
650 -// {  
651 -// unsigned int i = 0;  
652 -// unsigned int threadCount = 0;  
653 -// pid_t *tidList = NULL;  
654 -// T_ThreadAttribute *threadAttribute = NULL;  
655 -// T_ZiyanOsalHandler *osalHandler = ZiyanPlatform_GetOsalHandler();  
656 -  
657 -// USER_UTIL_UNUSED(argument);  
658 -  
659 -// while (1) {  
660 -// threadCount = Monitor_GetThreadCountOfProcess(getpid());  
661 -// tidList = osalHandler->Malloc(threadCount * sizeof(pid_t));  
662 -// if (tidList == NULL) {  
663 -// USER_LOG_ERROR("malloc fail.");  
664 -// goto delay;  
665 -// }  
666 -// Monitor_GetTidListOfProcess(getpid(), tidList, threadCount);  
667 -  
668 -// threadAttribute = osalHandler->Malloc(threadCount * sizeof(T_ThreadAttribute));  
669 -// if (threadAttribute == NULL) {  
670 -// USER_LOG_ERROR("malloc fail.");  
671 -// goto freeTidList;  
672 -// }  
673 -// for (i = 0; i < threadCount; ++i) {  
674 -// threadAttribute[i].tid = tidList[i];  
675 -// }  
676 -  
677 -// USER_LOG_DEBUG("thread pcpu:");  
678 -// USER_LOG_DEBUG("tid\tname\tpcpu");  
679 -// for (i = 0; i < threadCount; ++i) {  
680 -// threadAttribute[i].pcpu = Monitor_GetPcpuOfThread(getpid(), tidList[i]);  
681 -// Monitor_GetNameOfThread(getpid(), tidList[i], threadAttribute[i].name, sizeof(threadAttribute[i].name));  
682 -// USER_LOG_DEBUG("%d\t%15s\t%f %%.", threadAttribute[i].tid, threadAttribute[i].name,  
683 -// threadAttribute[i].pcpu);  
684 -// }  
685 -  
686 -// USER_LOG_DEBUG("heap used: %d B.", Monitor_GetHeapUsed(getpid()));  
687 -// USER_LOG_DEBUG("stack used: %d B.", Monitor_GetStackUsed(getpid()));  
688 -  
689 -// osalHandler->Free(threadAttribute);  
690 -// freeTidList:  
691 -// osalHandler->Free(tidList);  
692 -  
693 -// delay:  
694 -// sleep(10);  
695 -// }  
696 -// }  
697 -  
698 -// // static T_ZiyanReturnCode ZiyanTest_HighPowerApplyPinInit()  
699 -// // {  
700 -// // return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
701 -// // }  
702 -  
703 -// // static T_ZiyanReturnCode ZiyanTest_WriteHighPowerApplyPin(E_ZiyanPowerManagementPinState pinState)  
704 -// // {  
705 -// // //attention: please pull up the HWPR pin state by hardware.  
706 -// // return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
707 -// // }  
708 -  
709 -static void ZiyanUser_NormalExitHandler(int signalNum)  
710 -{  
711 - USER_UTIL_UNUSED(signalNum);  
712 - exit(0);  
713 -}  
714 -  
715 -#pragma GCC diagnostic pop  
716 -  
717 -/****************** (C) COPYRIGHT ZIYAN Innovations *****END OF FILE****/  
1 -/**  
2 - ********************************************************************  
3 - * @file dji_sdk_app_info.h  
4 - * @brief This is the header file for defining the structure and (exported) function prototypes.  
5 - *  
6 - * @copyright (c) 2018 ZIYAN. All rights reserved.  
7 - *  
8 - * All information contained herein is, and remains, the property of ZIYAN.  
9 - * The intellectual and technical concepts contained herein are proprietary  
10 - * to ZIYAN and may be covered by U.S. and foreign patents, patents in process,  
11 - * and protected by trade secret or copyright law. Dissemination of this  
12 - * information, including but not limited to data and other proprietary  
13 - * material(s) incorporated within the information, in any form, is strictly  
14 - * prohibited without the express written consent of ZIYAN.  
15 - *  
16 - * If you receive this source code without ZIYAN’s authorization, you may not  
17 - * further disseminate the information, and you must immediately remove the  
18 - * source code and notify ZIYAN of its removal. ZIYAN reserves the right to pursue  
19 - * legal actions against you for any loss(es) or damage(s) caused by your  
20 - * failure to do so.  
21 - *  
22 - *********************************************************************  
23 - */  
24 -  
25 -/* Define to prevent recursive inclusion -------------------------------------*/  
26 -#ifndef ZIYAN_SDK_APP_INFO_H  
27 -#define ZIYAN_SDK_APP_INFO_H  
28 -  
29 -/* Includes ------------------------------------------------------------------*/  
30 -  
31 -#ifdef __cplusplus  
32 -extern "C" {  
33 -#endif  
34 -  
35 -/* Exported constants --------------------------------------------------------*/  
36 -// ATTENTION: User must goto https://developer.dji.com/user/apps/#all to create your own dji sdk application, get dji sdk application  
37 -// information then fill in the application information here.  
38 -#define USER_APP_NAME "app_name_ziyan"  
39 -#define USER_APP_ID "app_id_ziyan"  
40 -#define USER_APP_KEY "app_key_ziyan"  
41 -#define USER_APP_LICENSE "app_license_ziyan"  
42 -#define USER_DEVELOPER_ACCOUNT "developer_account_ziyan"  
43 -#define USER_BAUD_RATE "921600" //原为921600  
44 -  
45 -/* Exported types ------------------------------------------------------------*/  
46 -  
47 -/* Exported functions --------------------------------------------------------*/  
48 -  
49 -  
50 -#ifdef __cplusplus  
51 -}  
52 -#endif  
53 -  
54 -#endif // ZIYAN_SDK_APP_INFO_H  
55 -/************************ (C) COPYRIGHT ZIYAN Innovations *******END OF FILE******/  
1 -/**  
2 - ********************************************************************  
3 - * @file dji_sdk_config.h  
4 - * @brief This is the header file for "dji_config.c", defining the structure and  
5 - * (exported) function prototypes.  
6 - *  
7 - * @copyright (c) 2021 ZIYAN. All rights reserved.  
8 - *  
9 - * All information contained herein is, and remains, the property of ZIYAN.  
10 - * The intellectual and technical concepts contained herein are proprietary  
11 - * to ZIYAN and may be covered by U.S. and foreign patents, patents in process,  
12 - * and protected by trade secret or copyright law. Dissemination of this  
13 - * information, including but not limited to data and other proprietary  
14 - * material(s) incorporated within the information, in any form, is strictly  
15 - * prohibited without the express written consent of ZIYAN.  
16 - *  
17 - * If you receive this source code without ZIYAN’s authorization, you may not  
18 - * further disseminate the information, and you must immediately remove the  
19 - * source code and notify ZIYAN of its removal. ZIYAN reserves the right to pursue  
20 - * legal actions against you for any loss(es) or damage(s) caused by your  
21 - * failure to do so.  
22 - *  
23 - *********************************************************************  
24 - */  
25 -  
26 -/* Define to prevent recursive inclusion -------------------------------------*/  
27 -#ifndef ZIYAN_SDK_CONFIG_H  
28 -#define ZIYAN_SDK_CONFIG_H  
29 -  
30 -/* Includes ------------------------------------------------------------------*/  
31 -  
32 -#ifdef __cplusplus  
33 -extern "C" {  
34 -#endif  
35 -  
36 -/* Exported constants --------------------------------------------------------*/  
37 -#define ZIYAN_USE_ONLY_UART (0)  
38 -#define ZIYAN_USE_UART_AND_USB_BULK_DEVICE (1)  
39 -#define ZIYAN_USE_UART_AND_NETWORK_DEVICE (2)  
40 -  
41 -/*!< Attention: Select your hardware connection mode here.  
42 -* */  
43 -#define CONFIG_HARDWARE_CONNECTION ZIYAN_USE_ONLY_UART  
44 -  
45 -/*!< Attention: Select the sample you want to run here.  
46 -* */  
47 -// #define CONFIG_MODULE_SAMPLE_POWER_MANAGEMENT_ON  
48 -  
49 -// #define CONFIG_MODULE_SAMPLE_CAMERA_EMU_ON  
50 -  
51 -// #define CONFIG_MODULE_SAMPLE_CAMERA_MEDIA_ON  
52 -  
53 -// #define CONFIG_MODULE_SAMPLE_GIMBAL_EMU_ON  
54 -  
55 -// #define CONFIG_MODULE_SAMPLE_XPORT_ON  
56 -  
57 -#define CONFIG_MODULE_SAMPLE_WIDGET_ON  
58 -  
59 -#define CONFIG_MODULE_SAMPLE_WIDGET_SPEAKER_ON  
60 -  
61 -// #define CONFIG_MODULE_SAMPLE_DATA_TRANSMISSION_ON  
62 -  
63 -// #define CONFIG_MODULE_SAMPLE_UPGRADE_ON  
64 -  
65 -// #define CONFIG_MODULE_SAMPLE_HMS_CUSTOMIZATION_ON  
66 -  
67 -#define CONFIG_MODULE_SAMPLE_FC_SUBSCRIPTION_ON  
68 -  
69 -/*!< Attention: This function needs to be used together with mobile sdk mop sample.  
70 -* */  
71 -//#define CONFIG_MODULE_SAMPLE_MOP_CHANNEL_ON  
72 -  
73 -/* Exported types ------------------------------------------------------------*/  
74 -  
75 -/* Exported functions --------------------------------------------------------*/  
76 -  
77 -#ifdef __cplusplus  
78 -}  
79 -#endif  
80 -  
81 -#endif // ZIYAN_SDK_CONFIG_H  
82 -/************************ (C) COPYRIGHT ZIYAN Innovations *******END OF FILE******/  
1 -/**  
2 - ********************************************************************  
3 - * @file hal_uart.c  
4 - * @brief  
5 - *  
6 - * @copyright (c) 2021 ZIYAN. All rights reserved.  
7 - *  
8 - * All information contained herein is, and remains, the property of ZIYAN.  
9 - * The intellectual and technical concepts contained herein are proprietary  
10 - * to ZIYAN and may be covered by U.S. and foreign patents, patents in process,  
11 - * and protected by trade secret or copyright law. Dissemination of this  
12 - * information, including but not limited to data and other proprietary  
13 - * material(s) incorporated within the information, in any form, is strictly  
14 - * prohibited without the express written consent of ZIYAN.  
15 - *  
16 - * If you receive this source code without ZIYAN’s authorization, you may not  
17 - * further disseminate the information, and you must immediately remove the  
18 - * source code and notify ZIYAN of its removal. ZIYAN reserves the right to pursue  
19 - * legal actions against you for any loss(es) or damage(s) caused by your  
20 - * failure to do so.  
21 - *  
22 - *********************************************************************  
23 - */  
24 -  
25 -/* Includes ------------------------------------------------------------------*/  
26 -#include <ziyan_logger.h>  
27 -#include "hal_uart.h"  
28 -  
29 -/* Private constants ---------------------------------------------------------*/  
30 -#define UART_DEV_NAME_STR_SIZE (128)  
31 -#define ZIYAN_SYSTEM_CMD_STR_MAX_SIZE (64)  
32 -#define ZIYAN_SYSTEM_RESULT_STR_MAX_SIZE (128)  
33 -  
34 -/* Private types -------------------------------------------------------------*/  
35 -typedef struct {  
36 - int32_t uartFd;  
37 -} T_UartHandleStruct;  
38 -  
39 -/* Private values -------------------------------------------------------------*/  
40 -  
41 -/* Private functions declaration ---------------------------------------------*/  
42 -  
43 -/* Exported functions definition ---------------------------------------------*/  
44 -T_ZiyanReturnCode HalUart_Init(E_ZiyanHalUartNum uartNum, uint32_t baudRate, T_ZiyanUartHandle *uartHandle)  
45 -{  
46 - T_UartHandleStruct *uartHandleStruct = NULL;  
47 - struct termios options;  
48 - struct flock lock;  
49 - T_ZiyanReturnCode returnCode = ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
50 - char uartName[UART_DEV_NAME_STR_SIZE];  
51 - char systemCmd[ZIYAN_SYSTEM_CMD_STR_MAX_SIZE];  
52 - char *ret = NULL;  
53 - char lineBuf[ZIYAN_SYSTEM_RESULT_STR_MAX_SIZE] = {0};  
54 - FILE *fp;  
55 -  
56 - uartHandleStruct = malloc(sizeof(T_UartHandleStruct));  
57 - if (uartHandleStruct == NULL) {  
58 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_MEMORY_ALLOC_FAILED;  
59 - }  
60 -  
61 - if (uartNum == ZIYAN_HAL_UART_NUM_0) {  
62 - strcpy(uartName, LINUX_UART_DEV1);  
63 - } else if (uartNum == ZIYAN_HAL_UART_NUM_1) {  
64 - strcpy(uartName, LINUX_UART_DEV2);  
65 - } else {  
66 - goto free_uart_handle;  
67 - }  
68 -  
69 -#ifdef USE_CLION_DEBUG  
70 - sprintf(systemCmd, "ls -l %s", uartName);  
71 - fp = popen(systemCmd, "r");  
72 - if (fp == NULL) {  
73 - goto free_uart_handle;  
74 - }  
75 -  
76 - ret = fgets(lineBuf, sizeof(lineBuf), fp);  
77 - if (ret == NULL) {  
78 - goto close_fp;  
79 - }  
80 -  
81 - if (strstr(lineBuf, "crwxrwxrwx") == NULL) {  
82 - USER_LOG_ERROR("Can't operation the device. "  
83 - "Probably the device has not operation permission. "  
84 - "Please execute command 'sudo chmod 777 %s' to add permission. ", uartName);  
85 - goto close_fp;  
86 - }  
87 -#else  
88 - sprintf(systemCmd, "chmod 777 %s", uartName);  
89 - fp = popen(systemCmd, "r");  
90 - if (fp == NULL) {  
91 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
92 - }  
93 -#endif  
94 -  
95 - uartHandleStruct->uartFd = open(uartName, (unsigned) O_RDWR | (unsigned) O_NOCTTY | (unsigned) O_NDELAY);  
96 - if (uartHandleStruct->uartFd == -1) {  
97 - goto close_fp;  
98 - }  
99 -  
100 - // Forbid multiple psdk programs to access the serial port  
101 - lock.l_type = F_WRLCK;  
102 - lock.l_pid = getpid();  
103 - lock.l_whence = SEEK_SET;  
104 - lock.l_start = 0;  
105 - lock.l_len = 0;  
106 -  
107 - if (fcntl(uartHandleStruct->uartFd, F_GETLK, &lock) < 0) {  
108 - goto close_uart_fd;  
109 - }  
110 - if (lock.l_type != F_UNLCK) {  
111 - goto close_uart_fd;  
112 - }  
113 - lock.l_type = F_WRLCK;  
114 - lock.l_pid = getpid();  
115 - lock.l_whence = SEEK_SET;  
116 - lock.l_start = 0;  
117 - lock.l_len = 0;  
118 - if (fcntl(uartHandleStruct->uartFd, F_SETLKW, &lock) < 0) {  
119 - goto close_uart_fd;  
120 - }  
121 -  
122 - if (tcgetattr(uartHandleStruct->uartFd, &options) != 0) {  
123 - goto close_uart_fd;  
124 - }  
125 -  
126 - switch (baudRate) {  
127 - case 115200:  
128 - cfsetispeed(&options, B115200);  
129 - cfsetospeed(&options, B115200);  
130 - break;  
131 - case 230400:  
132 - cfsetispeed(&options, B230400);  
133 - cfsetospeed(&options, B230400);  
134 - break;  
135 - case 460800:  
136 - cfsetispeed(&options, B460800);  
137 - cfsetospeed(&options, B460800);  
138 - break;  
139 - case 921600:  
140 - cfsetispeed(&options, B921600);  
141 - cfsetospeed(&options, B921600);  
142 - break;  
143 - case 1000000:  
144 - cfsetispeed(&options, B1000000);  
145 - cfsetospeed(&options, B1000000);  
146 - break;  
147 - default:  
148 - goto close_uart_fd;  
149 - }  
150 -  
151 - options.c_cflag |= (unsigned) CLOCAL;  
152 - options.c_cflag |= (unsigned) CREAD;  
153 - options.c_cflag &= ~(unsigned) CRTSCTS;  
154 - options.c_cflag &= ~(unsigned) CSIZE;  
155 - options.c_cflag |= (unsigned) CS8;  
156 - options.c_cflag &= ~(unsigned) PARENB;  
157 - options.c_iflag &= ~(unsigned) INPCK;  
158 - options.c_cflag &= ~(unsigned) CSTOPB;  
159 - options.c_oflag &= ~(unsigned) OPOST;  
160 - options.c_lflag &= ~((unsigned) ICANON | (unsigned) ECHO | (unsigned) ECHOE | (unsigned) ISIG);  
161 - options.c_iflag &= ~((unsigned) BRKINT | (unsigned) ICRNL | (unsigned) INPCK | (unsigned) ISTRIP | (unsigned) IXON);  
162 - options.c_cc[VTIME] = 0;  
163 - options.c_cc[VMIN] = 0;  
164 -  
165 - tcflush(uartHandleStruct->uartFd, TCIFLUSH);  
166 -  
167 - if (tcsetattr(uartHandleStruct->uartFd, TCSANOW, &options) != 0) {  
168 - goto close_uart_fd;  
169 - }  
170 -  
171 - *uartHandle = uartHandleStruct;  
172 - pclose(fp);  
173 -  
174 - return returnCode;  
175 -  
176 -close_uart_fd:  
177 - close(uartHandleStruct->uartFd);  
178 -  
179 -close_fp:  
180 - pclose(fp);  
181 -  
182 -free_uart_handle:  
183 - free(uartHandleStruct);  
184 -  
185 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
186 -}  
187 -  
188 -T_ZiyanReturnCode HalUart_DeInit(T_ZiyanUartHandle uartHandle)  
189 -{  
190 - int32_t ret;  
191 - T_UartHandleStruct *uartHandleStruct = (T_UartHandleStruct *) uartHandle;  
192 -  
193 - if (uartHandle == NULL) {  
194 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_UNKNOWN;  
195 - }  
196 -  
197 - ret = close(uartHandleStruct->uartFd);  
198 - if (ret < 0) {  
199 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
200 - }  
201 -  
202 - free(uartHandleStruct);  
203 -  
204 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
205 -}  
206 -  
207 -T_ZiyanReturnCode HalUart_WriteData(T_ZiyanUartHandle uartHandle, const uint8_t *buf, uint32_t len, uint32_t *realLen)  
208 -{  
209 - int32_t ret;  
210 - T_UartHandleStruct *uartHandleStruct = (T_UartHandleStruct *) uartHandle;  
211 -  
212 - if (uartHandle == NULL || buf == NULL || len == 0 || realLen == NULL) {  
213 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
214 - }  
215 -  
216 - ret = write(uartHandleStruct->uartFd, buf, len);  
217 - if (ret >= 0) {  
218 - *realLen = ret;  
219 - } else {  
220 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
221 - }  
222 -  
223 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
224 -}  
225 -  
226 -T_ZiyanReturnCode HalUart_ReadData(T_ZiyanUartHandle uartHandle, uint8_t *buf, uint32_t len, uint32_t *realLen)  
227 -{  
228 - int32_t ret;  
229 - T_UartHandleStruct *uartHandleStruct = (T_UartHandleStruct *) uartHandle;  
230 -  
231 - if (uartHandle == NULL || buf == NULL || len == 0 || realLen == NULL) {  
232 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
233 - }  
234 -  
235 - ret = read(uartHandleStruct->uartFd, buf, len);  
236 - if (ret >= 0) {  
237 - *realLen = ret;  
238 - } else {  
239 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SYSTEM_ERROR;  
240 - }  
241 -  
242 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
243 -}  
244 -  
245 -T_ZiyanReturnCode HalUart_GetStatus(E_ZiyanHalUartNum uartNum, T_ZiyanUartStatus *status)  
246 -{  
247 - if (uartNum == ZIYAN_HAL_UART_NUM_0) {  
248 - status->isConnect = true;  
249 - } else if (uartNum == ZIYAN_HAL_UART_NUM_1) {  
250 - status->isConnect = true;  
251 - } else {  
252 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
253 - }  
254 -  
255 - return ZIYAN_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
256 -}  
257 -  
258 -/* Private functions definition-----------------------------------------------*/  
259 -  
260 -/****************** (C) COPYRIGHT ZIYAN Innovations *****END OF FILE****/