313 lines
10 KiB
C
313 lines
10 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdint.h>
|
||
|
#include "lfs.h"
|
||
|
#include "charge.h"
|
||
|
#include "status.h"
|
||
|
#include "audio.h"
|
||
|
#include "app.h"
|
||
|
#include "storage.h"
|
||
|
#include "cmsis_os2.h"
|
||
|
#include "opensdk_api.h"
|
||
|
#include "export.h"
|
||
|
|
||
|
|
||
|
#define TEST_API 0
|
||
|
#define THREAD_STACK_SIZE_API_TEST (10 * 1024)
|
||
|
#define DEFAULT_INFO "C:/default.info"
|
||
|
|
||
|
|
||
|
enum GlobalmapNameT
|
||
|
{
|
||
|
GLOBALMAP_SYSCALL_TABLE = 0,
|
||
|
GLOBALMAP_SPEAKER_APP_INFO,
|
||
|
GLOBALMAP_SPEAKER_APP_WND,
|
||
|
GLOBALMAP_SPEAKER_APP,
|
||
|
GLOBALMAP_BATTERY_CHARGE,
|
||
|
GLOBALMAP_BATTERY_LEVEL,
|
||
|
GLOBALMAP_BATTERY_LOW,
|
||
|
GLOBALMAP_SIM_STATUS,
|
||
|
GLOBALMAP_NW_READY,
|
||
|
GLOBALMAP_SERVER_READY,
|
||
|
GOBALMAP_MAX,
|
||
|
};
|
||
|
|
||
|
|
||
|
int32_t speakerAppInit(AppInfoT *appInfo, uint32_t reserved1, uint32_t reserved2, uint32_t syscallTable)
|
||
|
{
|
||
|
printf("speakerAppInit\r\n");
|
||
|
appInfo->initStatus = 1;
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int32_t speakerAppPreDraw()
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int32_t speakerAppMsgProc(AppInfoT *appInfo, AppMsgT *msg, uint32_t reserved2, uint32_t syscallTable)
|
||
|
{
|
||
|
#ifdef FEATURE_SUBSYS_AUDIO_ENABLE
|
||
|
uint8_t batteryLevel = *((uint8_t *)(globalItemGet(GLOBALMAP_BATTERY_LEVEL)));
|
||
|
uint8_t batteryCharge = *((uint8_t *)(globalItemGet(GLOBALMAP_BATTERY_CHARGE)));
|
||
|
bool batteryLow = *((bool *)(globalItemGet(GLOBALMAP_BATTERY_LOW)));
|
||
|
uint8_t simStatus = *((uint8_t *)(globalItemGet(GLOBALMAP_SIM_STATUS)));
|
||
|
bool nwReady = *((bool *)(globalItemGet(GLOBALMAP_NW_READY)));
|
||
|
bool serverReady = *((bool *)(globalItemGet(GLOBALMAP_SERVER_READY)));
|
||
|
StatusT status = {0};
|
||
|
|
||
|
switch(msg->msgType)
|
||
|
{
|
||
|
case APP_KEY_MSG:
|
||
|
switch(msg->param1)
|
||
|
{
|
||
|
case ACTION_VOLUME_MINUS_SHORT:
|
||
|
audioAdjustVolume(ACTION_VOLUME_MINUS_SHORT);
|
||
|
break;
|
||
|
|
||
|
case ACTION_VOLUME_PLUS_SHORT:
|
||
|
audioAdjustVolume(ACTION_VOLUME_PLUS_SHORT);
|
||
|
break;
|
||
|
|
||
|
case ACTION_VOLUME_MINUS_LONG:
|
||
|
audioAdjustVolume(ACTION_VOLUME_MINUS_LONG);
|
||
|
break;
|
||
|
|
||
|
case ACTION_VOLUME_PLUS_LONG:
|
||
|
audioAdjustVolume(ACTION_VOLUME_PLUS_LONG);
|
||
|
break;
|
||
|
|
||
|
case ACTION_MENU_SHORT:
|
||
|
if (batteryLevel == 100) {audioPlayMp3(BATTRY_SOUND_100, NULL, true, true);}
|
||
|
else if (batteryLevel == 75) {audioPlayMp3(BATTRY_SOUND_75, NULL, true, true);}
|
||
|
else if (batteryLevel == 50) {audioPlayMp3(BATTRY_SOUND_50, NULL, true, true);}
|
||
|
else if (batteryLevel == 25) {audioPlayMp3(BATTRY_SOUND_25, NULL, true, true);}
|
||
|
else if (batteryLevel == 0) {audioPlayMp3(BATTRY_SOUND_LOW, NULL, true, true);}
|
||
|
|
||
|
if (nwReady == true)
|
||
|
{
|
||
|
audioPlayMp3(NW_SOUND_READY, NULL, true, true);
|
||
|
if (serverReady == true)
|
||
|
{
|
||
|
audioPlayMp3(SERVER_SOUND_READY, NULL, true, true);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
audioPlayMp3(SERVER_SOUND_UNREADY, NULL, true, true);
|
||
|
}
|
||
|
}
|
||
|
else if (simStatus == SIM_REMOVED)
|
||
|
{
|
||
|
audioPlayMp3(SIM_SOUND_UNREADY, NULL, true, true);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
audioPlayMp3(NW_SOUND_UNREADY, NULL, true, true);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case ACTION_MENU_LONG:
|
||
|
#ifdef FEATURE_SUBSYS_PCM_RECORD_ENABLE
|
||
|
audioRecordG726(RECORD_SOUND);
|
||
|
#endif
|
||
|
break;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case APP_STAT_MSG:
|
||
|
if (msg != NULL)
|
||
|
{
|
||
|
((uint32_t *)(&status))[0] = msg->param1;
|
||
|
((uint32_t *)(&status))[1] = msg->param2;
|
||
|
}
|
||
|
batteryLevel = status.batteryLevel;
|
||
|
simStatus = status.simStatus;
|
||
|
nwReady = status.nwReady;
|
||
|
serverReady = status.serverReady;
|
||
|
globalItemSet(GLOBALMAP_BATTERY_LEVEL, &batteryLevel);
|
||
|
globalItemSet(GLOBALMAP_SIM_STATUS, &simStatus);
|
||
|
globalItemSet(GLOBALMAP_NW_READY, &nwReady);
|
||
|
globalItemSet(GLOBALMAP_SERVER_READY, &serverReady);
|
||
|
|
||
|
if ((batteryCharge == CHARGE_STATUS_DISCONNECT) && (status.batteryCharge == CHARGE_STATUS_CHARGING))
|
||
|
{
|
||
|
batteryCharge = status.batteryCharge;
|
||
|
globalItemSet(GLOBALMAP_BATTERY_CHARGE, &batteryCharge);
|
||
|
audioPlayMp3(CHARGE_SOUND_BEGIN, NULL, true, true);
|
||
|
}
|
||
|
else if ((batteryCharge != CHARGE_STATUS_DISCONNECT) && (status.batteryCharge == CHARGE_STATUS_DISCONNECT))
|
||
|
{
|
||
|
batteryLow = false;
|
||
|
batteryCharge = status.batteryCharge;
|
||
|
globalItemSet(GLOBALMAP_BATTERY_CHARGE, &batteryCharge);
|
||
|
globalItemSet(GLOBALMAP_BATTERY_LEVEL, &batteryLow);
|
||
|
audioPlayMp3(CHARGE_SOUND_END, NULL, true, true);
|
||
|
}
|
||
|
else if ((batteryCharge == CHARGE_STATUS_CHARGING) && (status.batteryCharge == CHARGE_STATUS_FINISH))
|
||
|
{
|
||
|
batteryCharge = status.batteryCharge;
|
||
|
globalItemSet(GLOBALMAP_BATTERY_CHARGE, &batteryCharge);
|
||
|
audioPlayMp3(BATTRY_SOUND_100, NULL, true, true);
|
||
|
}
|
||
|
|
||
|
if ((status.batteryLevel == 0) && (status.batteryCharge == CHARGE_STATUS_DISCONNECT) && (batteryLow == false))
|
||
|
{
|
||
|
batteryLow = true;
|
||
|
globalItemSet(GLOBALMAP_BATTERY_LEVEL, &batteryLow);
|
||
|
audioPlayMp3(BATTRY_SOUND_LOW, NULL, true, true);
|
||
|
audioPlayMp3(POWER_SOUND_OFF, pwrKeyStartPowerOff, true, true);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int32_t speakerAppAfterDraw()
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int32_t speakerAppDestory()
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void storageTest(uint32_t syscallTable)
|
||
|
{
|
||
|
FILE *file = NULL;
|
||
|
struct stat buf = {0};
|
||
|
char buffer[32] = {0};
|
||
|
|
||
|
printf("storageTest\r\n");
|
||
|
file = file_fopen(DEFAULT_INFO, "r");
|
||
|
if (file == NULL)
|
||
|
{
|
||
|
printf("Failed to open the file %s.\r\n", DEFAULT_INFO);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
file_fstat((int)file, &buf);
|
||
|
if (buf.st_size == 0)
|
||
|
{
|
||
|
printf("File is empty.\r\n");
|
||
|
goto labelEnd;
|
||
|
}
|
||
|
|
||
|
if (buf.st_size > (sizeof(buffer) - 1))
|
||
|
{
|
||
|
printf("Buffer is too small.\r\n");
|
||
|
goto labelEnd;
|
||
|
}
|
||
|
|
||
|
file_fread((void *)buffer, buf.st_size, 1, file);
|
||
|
buffer[buf.st_size] = 0;
|
||
|
printf("Below is the content of the file "DEFAULT_INFO".\r\n%s\r\n", buffer);
|
||
|
|
||
|
labelEnd:
|
||
|
file_fclose(file);
|
||
|
}
|
||
|
|
||
|
void semaphoreTest(uint32_t syscallTable)
|
||
|
{
|
||
|
osSemaphoreId_t semaphoreId;
|
||
|
|
||
|
printf("semaphoreTest\r\n");
|
||
|
semaphoreId = osSemaphoreNew(1, 0, NULL);
|
||
|
printf("Release semaphore.\r\n");
|
||
|
osSemaphoreRelease(semaphoreId);
|
||
|
printf("Wait semaphore.\r\n");
|
||
|
osSemaphoreAcquire(semaphoreId, osWaitForever);
|
||
|
printf("Received semaphore.\r\n\r\n");
|
||
|
}
|
||
|
|
||
|
void messageQueueTest(uint32_t syscallTable)
|
||
|
{
|
||
|
osMessageQueueId_t messageQueueId;
|
||
|
uint32_t testData = 0x12;
|
||
|
uint32_t testData2 = 0;
|
||
|
|
||
|
printf("messageQueueTest\r\n");
|
||
|
messageQueueId = osMessageQueueNew(1, 4, NULL);
|
||
|
printf("Put queue: 0x%X\r\n", testData);
|
||
|
osMessageQueuePut(messageQueueId, &testData, 0, 0);
|
||
|
printf("Wait queue.\r\n");
|
||
|
osMessageQueueGet(messageQueueId, &testData2, 0, osWaitForever);
|
||
|
printf("Received queue: 0x%X\r\n\r\n", testData2);
|
||
|
}
|
||
|
|
||
|
void ThreadApiTest(void *argument)
|
||
|
{
|
||
|
uint32_t syscallTable = *((uint32_t *)argument);
|
||
|
#if (TEST_API == 1)
|
||
|
uint32_t count = 0;
|
||
|
|
||
|
osDelay(2000);
|
||
|
storageTest(syscallTable);
|
||
|
semaphoreTest(syscallTable);
|
||
|
messageQueueTest(syscallTable);
|
||
|
|
||
|
while (1)
|
||
|
{
|
||
|
printf("osDelay %ds\r\n", count++);
|
||
|
osDelay(1000 * count);
|
||
|
}
|
||
|
#else
|
||
|
osThreadExit();
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
void apiTestInit(uint32_t syscallTable, uint32_t appImageLocation)
|
||
|
{
|
||
|
osThreadAttr_t threadAttr;
|
||
|
|
||
|
threadAttr.name = "ThreadApiTest";
|
||
|
threadAttr.stack_mem = NULL;
|
||
|
threadAttr.stack_size = THREAD_STACK_SIZE_API_TEST;
|
||
|
threadAttr.priority = osPriorityNormal;
|
||
|
osThreadNew(appImageLocation + app_ThreadApiTest_addr, globalItemGet(GLOBALMAP_SYSCALL_TABLE), &threadAttr);
|
||
|
}
|
||
|
|
||
|
void main(uint32_t syscallTable, uint32_t appImageLocation, uint32_t reserved2, uint32_t reserved3)
|
||
|
{
|
||
|
char version[5];
|
||
|
|
||
|
*((uint32_t *)version) = *apiVersion;
|
||
|
version[4] = 0;
|
||
|
printf("OpenSDK API Version: %s\r\n", version);
|
||
|
printf("A:Hello world\r\n");
|
||
|
|
||
|
globalmapInit(GLOBALMAP_SYSCALL_TABLE, &syscallTable, sizeof(syscallTable));
|
||
|
|
||
|
globalmapInit(GLOBALMAP_SPEAKER_APP_INFO, NULL, sizeof(AppInfoT));
|
||
|
globalmapInit(GLOBALMAP_SPEAKER_APP_WND, NULL, sizeof(uint32_t *));
|
||
|
|
||
|
AppT speakerApp =
|
||
|
{
|
||
|
globalItemGet(GLOBALMAP_SPEAKER_APP_INFO),
|
||
|
globalItemGet(GLOBALMAP_SPEAKER_APP_WND),
|
||
|
appImageLocation + app_speakerAppInit_addr, //speakerAppInit
|
||
|
appImageLocation + app_speakerAppPreDraw_addr, //speakerAppPreDraw
|
||
|
appImageLocation + app_speakerAppMsgProc_addr, //speakerAppMsgProc
|
||
|
appImageLocation + app_speakerAppAfterDraw_addr, //speakerAppAfterDraw
|
||
|
appImageLocation + app_speakerAppDestory_addr, //speakerAppDestory
|
||
|
};
|
||
|
globalmapInit(GLOBALMAP_SPEAKER_APP, &speakerApp, sizeof(speakerApp));
|
||
|
|
||
|
mountApp(globalItemGet(GLOBALMAP_SPEAKER_APP), 5);
|
||
|
setActiveApp(5);
|
||
|
|
||
|
globalmapInit(GLOBALMAP_BATTERY_LEVEL, NULL, sizeof(uint8_t));
|
||
|
globalmapInit(GLOBALMAP_BATTERY_CHARGE, NULL, sizeof(bool));
|
||
|
globalmapInit(GLOBALMAP_BATTERY_LOW, NULL, sizeof(bool));
|
||
|
globalmapInit(GLOBALMAP_SIM_STATUS, NULL, sizeof(uint8_t));
|
||
|
globalmapInit(GLOBALMAP_NW_READY, NULL, sizeof(bool));
|
||
|
globalmapInit(GLOBALMAP_SERVER_READY, NULL, sizeof(bool));
|
||
|
|
||
|
apiTestInit(syscallTable, appImageLocation);
|
||
|
}
|