Skip to content

Commit

Permalink
Merge pull request #84 from ThomasVon2021/oled
Browse files Browse the repository at this point in the history
Oled
  • Loading branch information
ThomasVon2021 authored Nov 19, 2023
2 parents 7257106 + 05634df commit 2cce1c5
Show file tree
Hide file tree
Showing 22 changed files with 469 additions and 251 deletions.
Binary file modified package/kvmd-web/binary/h616/kvm-link
Binary file not shown.
Binary file modified package/kvmd-web/binary/pi/kvm-link
Binary file not shown.
39 changes: 21 additions & 18 deletions src/blikvm_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "kvmd/blikvm_rtc/blikvm_rtc.h"
#include "kvmd/blikvm_gpio/blikvm_gpio.h"
#include "common/blikvm_log/blikvm_log.h"
#include "config/blikvm_config.h"

#ifdef TEST_HARDWARE
#include "blikvm_test.h"
Expand All @@ -26,14 +25,13 @@
* private methods definition
******************************************************************************/

static blikvm_int8_t g_switch_init_flag = 0;

blikvm_int8_t blikvm_init( blikvm_config_t *config)
{
blikvm_int8_t ret = -1;
do
{
//1. init log module
blikvm_log_init(&config->log);

if(blikvm_gpio_init() == 0)
{
BLILOG_D(TAG,"init gpio success\n");
Expand Down Expand Up @@ -74,19 +72,21 @@ blikvm_int8_t blikvm_init( blikvm_config_t *config)
}

//4. init switch module
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");
}
else
{
BLILOG_E(TAG,"init switch failed\n");
if( config->switch_handle.enable > 0 )
{
if (blikvm_switch_init( &config->switch_handle ) >= 0)
{
g_switch_init_flag = 1;
BLILOG_D(TAG,"init switch success\n");
}
else
{
BLILOG_E(TAG,"init switch failed\n");
}
}

//5. init oled module
if(blikvm_oled_init(config->oled_type) >= 0)
if(blikvm_oled_init(&config->oled) >= 0)
{
BLILOG_D(TAG,"init oled success\n");
}
Expand All @@ -101,7 +101,7 @@ blikvm_int8_t blikvm_init( blikvm_config_t *config)
return ret;
}

blikvm_int8_t blikvm_start()
blikvm_int8_t blikvm_start(blikvm_config_t *config)
{
blikvm_int8_t ret = -1;

Expand All @@ -127,10 +127,13 @@ blikvm_int8_t blikvm_start()
BLILOG_E(TAG,"atx start error\n");
break;
}
if(blikvm_switch_start() < 0)
if(g_switch_init_flag > 0)
{
BLILOG_E(TAG,"switch start error\n");
break;
if(blikvm_switch_start() < 0)
{
BLILOG_E(TAG,"switch start error\n");
break;
}
}
if(blikvm_oled_start() < 0)
{
Expand Down
10 changes: 2 additions & 8 deletions src/blikvm_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@
#include "common/blikvm_type.h"
#include "common/blikvm_log/blikvm_log.h"
#include "kvmd/blikvm_oled/blikvm_oled.h"

typedef struct
{
blikvm_log_t log;
blikvm_oled_type_e oled_type;
blikvm_int8_t switch_device[32];
}blikvm_config_t;
#include "config/blikvm_config.h"

blikvm_int8_t blikvm_init( blikvm_config_t *config);
blikvm_int8_t blikvm_start();
blikvm_int8_t blikvm_start(blikvm_config_t *config);

#endif
12 changes: 12 additions & 0 deletions src/common/blikvm_util/blikvm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ char * GetUptime()
return uptime_string;
}

blikvm_int32_t skdy_get_int_uptime()
{
FILE *fp;
char buffer[256];
fp = fopen("/proc/uptime", "r");
fgets(buffer, 256, fp);
double uptime_seconds = atof(buffer);
blikvm_int32_t uptime_minutes = uptime_seconds / 60;
fclose(fp);
return uptime_minutes;
}

int getWifiSignalStrength(const char* interface, int* signalStrength) {
char command[100];
sprintf(command, "iwconfig %s | awk '/Signal level/ {print $4}'", interface);
Expand Down
6 changes: 6 additions & 0 deletions src/common/blikvm_util/blikvm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ int GetMemUsageShort(char* mem);
blikvm_board_type_e blikvm_get_board_type();
blikvm_int8_t isWifiCardAvailable();

/**
* @brief : get os's up time
* @return: the os's up since the Epoch, unit:min
*/
blikvm_int32_t skdy_get_int_uptime();

#endif
142 changes: 142 additions & 0 deletions src/config/blikvm_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <common/blikvm_util/cJSON.h>

#include "kvmd/blikvm_atx/blikvm_atx.h"
#include "blikvm_config.h"

#define TAG "CFG"

static blikvm_config_t g_config = {0};

static char* jsonFromFile(char* filename);

blikvm_int8_t blikvm_config_init()
{
const char* directory1 = "/mnt/msd/user";
Expand Down Expand Up @@ -50,3 +58,137 @@ blikvm_int8_t blikvm_config_init()

return 0;
}

blikvm_config_t* blikvm_read_config(blikvm_int8_t* file_path)
{
blikvm_config_t* ret = NULL;
do
{
if(access(file_path, R_OK) != 0)
{
BLILOG_E(TAG, "%s config json not exit\n",file_path);
break;
}

char* jsondata = jsonFromFile(file_path);
cJSON* root=cJSON_Parse(jsondata);
if (root == NULL)
{
BLILOG_E(TAG, "read json data is null\n");
break;
}

const cJSON *switch_handle = NULL;
switch_handle = cJSON_GetObjectItemCaseSensitive(root, "switch_handle");
if (cJSON_IsObject(switch_handle))
{
const cJSON *switch_enable = cJSON_GetObjectItemCaseSensitive(switch_handle, "switch_enable");
const cJSON *switch_path = cJSON_GetObjectItemCaseSensitive(switch_handle, "switch_path");

if (!cJSON_IsNumber(switch_enable))
{
BLILOG_E(TAG, "switch function is disable\n");
}
else
{
g_config.switch_handle.enable = switch_enable->valueint;
BLILOG_D(TAG, "switch function is enable\n");
}

if (!cJSON_IsString(switch_path))
{
BLILOG_E(TAG, "switch path is not string\n");
}
else
{
memcpy(g_config.switch_handle.device_path, switch_path->valuestring, strlen(switch_path->valuestring));
BLILOG_D(TAG, "switch device:%s\n",switch_path->valuestring);
}
}

const cJSON *atx = NULL;
atx = cJSON_GetObjectItemCaseSensitive(root, "atx");
if (cJSON_IsObject(atx))
{
const cJSON *power_on_delay = NULL;
power_on_delay = cJSON_GetObjectItemCaseSensitive(atx, "power_on_delay");
if (!cJSON_IsNumber(power_on_delay))
{
BLILOG_E(TAG, "power_on_delay is not number\n");
}
else
{
BLILOG_D(TAG, "power_on_delay:%d\n", power_on_delay->valueint);
blikvm_atx_set_power_on_delay(power_on_delay->valueint);
}

const cJSON *power_off_delay = NULL;
power_off_delay = cJSON_GetObjectItemCaseSensitive(atx, "power_off_delay");
if (!cJSON_IsNumber(power_off_delay))
{
BLILOG_E(TAG, "power_off_delay is not number\n");
}
else
{
BLILOG_D(TAG, "power_off_delay:%d\n",power_off_delay->valueint);
blikvm_atx_set_power_off_delay(power_off_delay->valueint);
}
}

const cJSON *oled = cJSON_GetObjectItemCaseSensitive(root, "oled");
if (cJSON_IsObject(oled))
{
const cJSON *oled_enable = cJSON_GetObjectItemCaseSensitive(oled, "oled_enable");
const cJSON *restart_show_time = cJSON_GetObjectItemCaseSensitive(oled, "restart_show_time");
const cJSON *interval_display_time = cJSON_GetObjectItemCaseSensitive(oled, "interval_display_time");

if (cJSON_IsNumber(oled_enable))
{
BLILOG_I(TAG, "oled_enable: %d\n", oled_enable->valueint);
// Handle oled_type accordingly
}

if (cJSON_IsNumber(restart_show_time))
{
BLILOG_I(TAG,"restart_show_time: %d\n", restart_show_time->valueint);
g_config.oled.restart_show_time = restart_show_time->valueint;
}

if (cJSON_IsNumber(interval_display_time))
{
BLILOG_I(TAG,"interval_display_time: %d\n", interval_display_time->valueint);
g_config.oled.interval_display_time = interval_display_time->valueint;
}
}
else
{
printf("oled section not found or not an object\n");
}
ret = &g_config;

free(jsondata);
cJSON_free(root);
}while(0>1);
return ret;
}

char* jsonFromFile(char* filename)
{
FILE* fp = fopen(filename, "r");
if (!fp)
{
return NULL;
}
fseek(fp, 0, SEEK_END);
long len = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* buf = (char*)calloc(len+1, sizeof(char));
fread(buf, sizeof(char), len, fp);
fclose(fp);
return buf;
}

blikvm_config_t* blikvm_get_config()
{
return &g_config;
}
13 changes: 13 additions & 0 deletions src/config/blikvm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@
#include "common/blikvm_type.h"
#include "common/blikvm_log/blikvm_log.h"
#include "kvmd/blikvm_oled/blikvm_oled.h"
#include "kvmd/blikvm_switch/blikvm_switch.h"


typedef struct
{
blikvm_log_t log;
blikvm_switch_t switch_handle;
blikvm_oled_config_t oled;
}blikvm_config_t;

blikvm_int8_t blikvm_config_init();

blikvm_config_t* blikvm_read_config(blikvm_int8_t* file_path);

blikvm_config_t* blikvm_get_config();

#endif
21 changes: 15 additions & 6 deletions src/config/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
{
"version": "Develop-1.2.5",
"version_int": 125,
"version": "Develop-1.2.7",
"version_int": 127,
"md5value": "",
"switch_handle": "",
"oled_type": 0,
"power_on_dalay":0,
"power_off_dalay":0
"switch_handle":{
"switch_enable": 0,
"switch_path": "/dev/ttyUSB0"
},
"oled":{
"oled_enable": 1,
"restart_show_time": 300,
"interval_display_time": 5
},
"atx":{
"power_on_delay":500,
"power_off_delay":3000
}
}
4 changes: 2 additions & 2 deletions src/config/readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Notes

- oled_type(0:SSD1306 128x32; 1:SSD1306 128x64; )
- power_on_dalay:0 //unit:ms, default time 500ms
- power_off_dalay:0 //uint:ms, default time 3000ms
- power_on_delay:0 //unit:ms, default time 500ms
- power_off_delay:0 //uint:ms, default time 3000ms
Loading

0 comments on commit 2cce1c5

Please sign in to comment.