Skip to content

Commit

Permalink
add test hardware v4 code
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVon2021 authored and shangbinbin committed Jun 4, 2023
1 parent 3c33342 commit d4dd60b
Show file tree
Hide file tree
Showing 25 changed files with 1,033 additions and 83 deletions.
7 changes: 7 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ endif
ifeq ($(H616),1)
CFLAGS += -DH616
CFLAGS += -DVER4
ifeq ($(TEST_HARDWARE),1)
INCLUDE_PATH += ../test/hardware/
SOURCE_FILES += ../test/hardware/blikvm_test_v4.c
CFLAGS += -DTEST_HARDWARE
endif
endif

ifeq ($(SSD1306),1)
Expand All @@ -25,6 +30,7 @@ INCLUDE_PATH += ../third_lib/ssd1306/lib/Config/
INCLUDE_PATH += ../third_lib/ssd1306/lib/Fonts/
INCLUDE_PATH += ../third_lib/ssd1306/lib/GUI/
INCLUDE_PATH += ../third_lib/ssd1306/lib/OLED/

SOURCE_FILES += ../third_lib/ssd1306/examples/OLED_0in91_test.c
SOURCE_FILES += ../third_lib/ssd1306/examples/OLED_0in96_test.c
SOURCE_FILES += ../third_lib/ssd1306/lib/Config/*.c
Expand Down Expand Up @@ -57,6 +63,7 @@ SOURCE_FILES += ../third_lib/GPIO/armbianio.c
SOURCE_FILES += ../third_lib/GPIO/softPwm.c
SOURCE_FILES += ./kvmd/blikvm_oled/blikvm_oled.c
SOURCE_FILES += ./kvmd/blikvm_gpio/blikvm_gpio.c
SOURCE_FILES += ./kvmd/blikvm_rtc/blikvm_rtc.c

CC = gcc

Expand Down
15 changes: 13 additions & 2 deletions src/blikvm_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
*-----------------------------------------------------------------------------*
* 2022-09-02 | 0.1 | Thmoasvon | create
******************************************************************************/
#include <string.h>

#include "blikvm_server.h"
#include "kvmd/blikvm_fan/blikvm_fan.h"
#include "kvmd/blikvm_atx/blikvm_atx.h"
#include "kvmd/blikvm_oled/blikvm_oled.h"
#include "kvmd/blikvm_switch/blikvm_switch.h"
#include "kvmd/blikvm_rtc/blikvm_rtc.h"
#include "kvmd/blikvm_gpio/blikvm_gpio.h"
#include "common/blikvm_log/blikvm_log.h"

#ifdef TEST_HARDWARE
#include "blikvm_test.h"
#endif

#define TAG "SERVER"
/*******************************************************************************
Expand Down Expand Up @@ -59,7 +64,9 @@ blikvm_int8_t blikvm_init( blikvm_config_t *config)
}

//4. init switch module
if (blikvm_switch_init() >= 0)
blikvm_switch_t switch_config = {0};
memcpy(switch_config.device_path, config->switch_device, strlen(config->switch_device));
if (blikvm_switch_init(&switch_config) >= 0)
{
BLILOG_D(TAG,"init switch success\n");
}
Expand All @@ -77,7 +84,8 @@ blikvm_int8_t blikvm_init( blikvm_config_t *config)
{
BLILOG_E(TAG,"init oled failed\n");
}
//5. init dtc module
//5. init rtc module
blikvm_rtc_init();

} while (0>1);
return ret;
Expand Down Expand Up @@ -123,6 +131,9 @@ blikvm_int8_t blikvm_start()
{
BLILOG_D(TAG,"oled start ok\n");
}
#ifdef TEST_HARDWARE
blikvm_hardware_test();
#endif
ret =0;
}while(0>1);

Expand Down
1 change: 1 addition & 0 deletions src/blikvm_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct
{
blikvm_log_t log;
blikvm_oled_type_e oled_type;
blikvm_int8_t switch_device[32];
}blikvm_config_t;

blikvm_int8_t blikvm_init( blikvm_config_t *config);
Expand Down
2 changes: 1 addition & 1 deletion src/common/blikvm_log/blikvm_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define BLI_MAX_STATIC_CACHE 10*1024
#define BLI_LOG_FILE_PATH 256U
#define BLI_LOG_DEVICE_PATH 128U
#define BLI_MAX_FILE_NAME 64U
#define BLI_MAX_FILE_NAME 512U
#define BLI_LOG_MAX_PREFIX_LEN 32U
#define BLI_MAX_ROTATE_CNT 4U
#define BLI_LOG_FILE_NUM 1U
Expand Down
7 changes: 5 additions & 2 deletions src/common/blikvm_uart/blikvm_uart.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#define SERIAL_TIME_OUT_MS (5050)

blikvm_int8_t g_fd = 0;
static blikvm_int32_t set_serial_opt(blikvm_int32_t fd, blikvm_int32_t nSpeed, blikvm_int32_t nBits, blikvm_int8_t nEvent, blikvm_int32_t nStop);

/**
* @brief open serial device and set baut
Expand Down Expand Up @@ -62,7 +61,7 @@ blikvm_int32_t open_serial_dev(const blikvm_int8_t* serial_dev, blikvm_int32_t b
/**
* change baut rate and set flags.
*/
static blikvm_int32_t
blikvm_int32_t
set_serial_opt(blikvm_int32_t fd, blikvm_int32_t nSpeed, blikvm_int32_t nBits, blikvm_int8_t nEvent, blikvm_int32_t nStop)
{
struct termios newtio = { 0 };
Expand Down Expand Up @@ -113,6 +112,10 @@ set_serial_opt(blikvm_int32_t fd, blikvm_int32_t nSpeed, blikvm_int32_t nBits, b
cfsetispeed(&newtio, B19200);
cfsetospeed(&newtio, B19200);
break;
case 38400:
cfsetispeed(&newtio, B38400);
cfsetospeed(&newtio, B38400);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
Expand Down
2 changes: 2 additions & 0 deletions src/common/blikvm_uart/blikvm_uart.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "common/blikvm_log/blikvm_log.h"

blikvm_int32_t set_serial_opt(blikvm_int32_t fd, blikvm_int32_t nSpeed, blikvm_int32_t nBits, blikvm_int8_t nEvent, blikvm_int32_t nStop);

blikvm_int32_t open_serial_dev(const blikvm_int8_t* serial_dev, blikvm_int32_t baut);

#endif
74 changes: 69 additions & 5 deletions src/common/blikvm_util/blikvm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
#include <time.h>
#include <stdlib.h>

#include "blikvm_util.h"
#include "blikvm_util.h"

#define MAX_BUFFER_SIZE 1024

static blikvm_int8_t *pi4b_board = "Raspberry Pi 4 Model B";
static blikvm_int8_t *cm4b_board = "Raspberry Pi Compute Module 4";
static blikvm_int8_t *h616_board = "Mango Pi Mcore";



blikvm_int32_t execmd(blikvm_int8_t* cmd, blikvm_int8_t* result)
{
blikvm_int8_t buffer[128]; //定义缓冲区
Expand Down Expand Up @@ -153,15 +153,13 @@ int GetMemUsage(char* mem)
{
char* cmd = "df -h | awk '$NF==\"/\"{printf \"%d/%dGB %s\", $3,$2,$5}'";
execterminal(cmd,mem);
//printf("mem:%s\n",mem);
return 0;
}

int GetMemUsageShort(char* mem)
{
char* cmd = "df -h | awk '$NF==\"/\"{printf \"%d/%dG\", $3,$2}'";
execterminal(cmd,mem);
//printf("mem:%s\n",mem);
return 0;
}

Expand Down Expand Up @@ -203,4 +201,70 @@ char * GetUptime()
asprintf(&uptime_string, "%dd %dh %dm", uptime_days, uptime_hours, uptime_minutes);
fclose(fp);
return uptime_string;
}

int getWifiSignalStrength(const char* interface, int* signalStrength) {
char command[100];
sprintf(command, "iwconfig %s | awk '/Signal level/ {print $4}'", interface);

FILE* fp = popen(command, "r");
if (fp == NULL) {
perror("Failed to execute command");
return -1;
}

char output[32];
if (fgets(output, sizeof(output), fp) == NULL) {
perror("Failed to read command output");
pclose(fp);
return -1;
}

pclose(fp);
int strength = atoi(&output[6]);
*signalStrength = strength;
return 0;
}

blikvm_int8_t isWifiCardAvailable()
{
blikvm_int8_t ret = -1;
FILE* fp;
char buffer[MAX_BUFFER_SIZE];
blikvm_int8_t wifiCardAvailable = 0;

// 执行 iwconfig 命令
fp = popen("ifconfig wlan0 | grep UP", "r");
if (fp == NULL) {
perror("Failed to execute iwconfig");
return -1;
}

// 读取命令输出并解析
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
// 检查是否存在无线网卡
if (strstr(buffer, "UP") != NULL)
{
wifiCardAvailable = 1; // 网卡正常
break;
}
}
pclose(fp);
if( wifiCardAvailable == 1)
{
blikvm_int32_t strength;
if(getWifiSignalStrength("wlan0", &strength) == 0)
{
if( strength > 40)
{
ret = 0;
}
}
else
{
printf("get wifi strength failed\n");
}
}
return ret; // 返回0表示正常,返回-1表示不正常
}
1 change: 1 addition & 0 deletions src/common/blikvm_util/blikvm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ int GetIP(char* ip);
char* GetUptime();
int GetMemUsageShort(char* mem);
blikvm_board_type_e blikvm_get_board_type();
blikvm_int8_t isWifiCardAvailable();

#endif
5 changes: 4 additions & 1 deletion src/config/package.json
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{"version": "Develop-1.1.3", "version_int": 113, "md5value": "846ecb87ff42fccd22d334bd3d58423c", "oled_type": 0}
{"version": "Develop-1.1.3", "version_int": 113, "md5value": "846ecb87ff42fccd22d334bd3d58423c",
"switch_handle": "",
"oled_type": 0
}
40 changes: 27 additions & 13 deletions src/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
#define TAG "MAIN"


static char* config_json_path= (char*)"/usr/bin/package.json";
static char* config_json_path= (char*)"/usr/bin/blikvm/package.json";
static char* jsonFromFile(char* filename);
static blikvm_int8_t blikvm_parse_config(blikvm_int8_t* file_path);
static blikvm_config_t config;
static blikvm_config_t g_config;

int main(int argc, char *argv[])
{

blikvm_uint8_t log_path[] = "/opt/bin/blilog";
config.log.enable[0] = 1;
config.log.level = 4U;
config.log.channel = BLI_LOG_CHANNEL_CONSOLE | BLI_LOG_CHANNEL_FILE;
memcpy(config.log.log_out.file.path, log_path, strnlen((blikvm_int8_t *)log_path, 256));
config.log.log_out.file.max_size[0] = 5 * 1024U;
config.oled_type = OLED_SSD1306_128_64;
g_config.log.enable[0] = 1;
g_config.log.level = 4U;
g_config.log.channel = BLI_LOG_CHANNEL_CONSOLE | BLI_LOG_CHANNEL_FILE;
memcpy(g_config.log.log_out.file.path, log_path, strnlen((blikvm_int8_t *)log_path, 256));
g_config.log.log_out.file.max_size[0] = 5 * 1024U;
g_config.oled_type = OLED_SSD1306_128_64;
blikvm_parse_config(config_json_path);
blikvm_init(&config);
blikvm_init(&g_config);
blikvm_start();
while (1)
{
Expand All @@ -40,12 +40,12 @@ blikvm_int8_t blikvm_parse_config(blikvm_int8_t* file_path)
{
if(access(file_path, R_OK) != 0)
{
BLILOG_E(TAG,"%s config json not exit\n",file_path);
printf("%s config json not exit\n",file_path);
break;
}
const cJSON *oled_type = NULL;
const cJSON *switch_device = NULL;
char* jsondata = jsonFromFile(file_path);
printf("%s\n", jsondata);
cJSON* root=cJSON_Parse(jsondata);
if (root == NULL)
{
Expand All @@ -56,9 +56,23 @@ blikvm_int8_t blikvm_parse_config(blikvm_int8_t* file_path)
if (!cJSON_IsNumber(oled_type))
{
printf("oled_type is not number\n");
break;
}
printf("oled type:%d\n",oled_type->valueint);
else
{
printf("oled type:%d\n",oled_type->valueint);
}


switch_device = cJSON_GetObjectItemCaseSensitive(root, "switch_device");
if (!cJSON_IsString(switch_device))
{
printf("switch_device is not string\n");
}
else
{
memcpy(g_config.switch_device,switch_device->valuestring,strlen(switch_device->valuestring));
printf("switch device:%s\n",switch_device->valuestring);
}
//TODO
free(jsondata);
cJSON_free(root);
Expand Down
36 changes: 36 additions & 0 deletions src/kvmd/blikvm_atx/blikvm_atx.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,40 @@ static blikvm_uint8_t blikvm_read_atx_state()
} while (0>1);
//BLILOG_D(TAG,"atx state ret:%02X\n",ret);
return ret;
}

blikvm_int8_t blikvm_atx_test()
{
blikvm_uint8_t ret = -1;
do
{
if(g_atx.init != 1U)
{
BLILOG_E(TAG,"not init\n");
break;
}
AIOWriteGPIO(PIN_POWER, GPIO_LOW);
AIOWriteGPIO(PIN_RESET, GPIO_LOW);
usleep(10*1000);
blikvm_int32_t value1 = AIOReadGPIO(PIN_LED_PWR);
blikvm_int32_t value2 = AIOReadGPIO(PIN_LED_HDD);
if(value1 != GPIO_LOW || value2 != GPIO_LOW)
{
BLILOG_E(TAG,"read gpio is not low %d %d\n",value1,value2);
break;
}
AIOWriteGPIO(PIN_POWER, GPIO_HIGH);
AIOWriteGPIO(PIN_RESET, GPIO_HIGH);
usleep(10*1000);
value1 = AIOReadGPIO(PIN_LED_PWR);
value2 = AIOReadGPIO(PIN_LED_HDD);
if(value1 != GPIO_HIGH || value2 != GPIO_HIGH)
{
BLILOG_E(TAG,"read gpio is not high %d %d\n", value1,value2);
break;
}
ret = 0;

}while(0>1);
return ret;
}
1 change: 1 addition & 0 deletions src/kvmd/blikvm_atx/blikvm_atx.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

blikvm_int8_t blikvm_atx_init();
blikvm_int8_t blikvm_atx_start();
blikvm_int8_t blikvm_atx_test();
#endif
1 change: 0 additions & 1 deletion src/kvmd/blikvm_fan/blikvm_fan.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ static blikvm_void_t *blikvm_fan_loop(void *_)
blikvm_uint8_t state[1];
blikvm_uint8_t last_state[1];
blikvm_int32_t ret_len;
// printf("fan open:%u speed:%u\n",fan_open,fan_speed);
if(state != last_state)
{
g_fan.fp = fopen("/dev/shm/blikvm/fan","wb+");
Expand Down
Loading

0 comments on commit d4dd60b

Please sign in to comment.