Skip to content

Commit

Permalink
fix atx function on PCIe version
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVon2021 committed Jan 28, 2023
1 parent e5ffd47 commit 0704c1a
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 90 deletions.
4 changes: 2 additions & 2 deletions script/install_release.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def doArgParse():
global gArgs
optParser = argparse.ArgumentParser(description='build and tar file')
optParser.add_argument('--releasepath', nargs='?', default='/tmp/release/', type=str, help='release path')
optParser.add_argument('--releasepath', nargs='?', default='/tmp/kvm_update/release/', type=str, help='release path')
gArgs = optParser.parse_args()

def execute_cmd(cmd,sh_path):
Expand Down Expand Up @@ -55,7 +55,7 @@ def main():
cmd = "bash install-kvmd-main.sh && bash install-ustreamer.sh && bash install-kvmd-oled.sh && bash install-kvmd-hid.sh \
&& bash install-kvmd-web.sh"
subprocess.check_output(cmd, shell = True, cwd=gArgs.releasepath )
cmd = "cp package.json /usr/bin/package.json"
cmd = "cp package.json /usr/bin/kvm_json/package.json"
subprocess.check_output(cmd, shell = True, cwd=gArgs.releasepath )
# cmd = "reboot"
# subprocess.check_output(cmd, shell = True, cwd=gArgs.releasepath )
Expand Down
13 changes: 8 additions & 5 deletions script/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
import sys
import subprocess

download_path = "/tmp/"
download_path = "/tmp/kvm_update/"
update_result = False
def main():
global update_result
sh_path = os.path.split(os.path.realpath(__file__))[0]
make_path = sh_path + '/src'

# clear download_path file
cmd = "rm -rf /tmp/*"
cmd = "rm -rf /tmp/kvm_update"
output = subprocess.check_output(cmd, shell = True, cwd=sh_path )

cmd = "mkdir /tmp/kvm_update"
output = subprocess.check_output(cmd, shell = True, cwd=sh_path )

#start update
file = open('/tmp/update_status.json','w')
file = open('/tmp/kvm_update/update_status.json','w')
file.write('{\"update_status\": 0}')
file.close()
a=1
Expand All @@ -41,7 +44,7 @@ def main():

# compare download packjson version and run packjson version
download_json = download_path + "package.json"
run_json = '/usr/bin/package.json'
run_json = '/usr/bin/kvm_json/package.json'
last_version = ''
run_version = ''
with open(download_json,'r',encoding='utf8')as fp_r:
Expand Down Expand Up @@ -76,7 +79,7 @@ def main():
else:
result_cnt = "{\"update_status\": 2}"

file = open('/tmp/update_status.json','w')
file = open('/tmp/kvm_update/update_status.json','w')
file.write(result_cnt)
file.close()

Expand Down
2 changes: 1 addition & 1 deletion src/config/package.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version": "1.0.5", "version_int": 105, "md5value": "0989d8e835fe3a8a4b669364811ff470"}
{"version": "1.0.6", "version_int": 106, "md5value": "56b0725d3ac9fd6e01ece28a88444b33", "oled_type": 0}
62 changes: 59 additions & 3 deletions src/demo.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <stdlib.h>
#include <common/blikvm_util/cJSON.h>
#include "blikvm_server.h"
#define TAG "MAIN"


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

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

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;

blikvm_parse_config(config_json_path);
blikvm_init(&config);
blikvm_start();
while (1)
Expand All @@ -24,4 +31,53 @@ int main(int argc, char *argv[])
}

return 0;
}

blikvm_int8_t blikvm_parse_config(blikvm_int8_t* file_path)
{
blikvm_int8_t ret = -1;
do
{
if(access(file_path, R_OK) != 0)
{
BLILOG_E(TAG,"%s config json not exit\n",file_path);
break;
}
const cJSON *oled_type = NULL;
char* jsondata = jsonFromFile(file_path);
printf("%s\n", jsondata);
cJSON* root=cJSON_Parse(jsondata);
if (root == NULL)
{
printf("read json data is null\n");
break;
}
oled_type = cJSON_GetObjectItemCaseSensitive(root, "oled_type");
if (!cJSON_IsNumber(oled_type))
{
printf("oled_type is not number\n");
break;
}
printf("oled type:%d\n",oled_type->valueint);
//TODO
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;
}
135 changes: 57 additions & 78 deletions src/kvmd/blikvm_atx/blikvm_atx.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*-----------------------------------------------------------------------------*
* 2022-09-10 | 0.1 | Thomasvon | create
******************************************************************************/

#include <pthread.h>
#include <stdlib.h>
#include "blikvm_atx.h"
Expand All @@ -14,6 +15,12 @@
#include "softPwm.h" //添加库文件

#define TAG "ATX"
#define PIN_POWER 23 //BCM23
#define PIN_RESET 27 //BCM27
#define PIN_LED_PWR 24 //BCM24
#define PIN_LED_HDD 22 //BCM22
#define ATX_CYCLE 500 //unit:ms


typedef struct
{
Expand All @@ -33,13 +40,18 @@ typedef enum
static blikvm_atx_t g_atx = {0};
static blikvm_domainsocker_rev_t g_rev_buff = {0};
static blikvm_void_t *blikvm_atx_loop(void *_);
static blikvm_int8_t blikvm_read_atx_state();
static blikvm_void_t *blikvm_atx_monitor(void *_);
static blikvm_uint8_t blikvm_read_atx_state();

blikvm_int8_t blikvm_atx_init()
{
blikvm_int8_t ret = -1;
do
{
if(wiringPiSetupGpio() == -1)
{
BLILOG_E(TAG,"atx init gpio failed\n");
}
if(access("/dev/shm/blikvm/",R_OK) != 0)
{
BLILOG_E(TAG,"not exit /dev/shm/blikvm/ will creat this dir\n");
Expand Down Expand Up @@ -97,8 +109,10 @@ blikvm_int8_t blikvm_atx_init()
}
g_atx.socket_addr.send_addr_len = sizeof(g_atx.socket_addr.send_addr);

pinMode(4, OUTPUT); // wpi
pinMode(2, OUTPUT); //wpi
pinMode(PIN_POWER, OUTPUT);
pinMode(PIN_RESET, OUTPUT);
pinMode(PIN_LED_PWR, INPUT);
pinMode(PIN_LED_HDD, INPUT);
g_atx.init = 1;
ret =0;
} while (0>1);
Expand All @@ -110,12 +124,19 @@ blikvm_int8_t blikvm_atx_start()
{
blikvm_int8_t ret = -1;
pthread_t blikvm_atx_thread;
pthread_t blikvm_monitor_thread;
do
{
blikvm_int8_t thread_ret = pthread_create(&blikvm_atx_thread, NULL, blikvm_atx_loop, NULL);
if(thread_ret != 0)
{
BLILOG_E(TAG,"creat thread failed\n");
BLILOG_E(TAG,"creat loop thread failed\n");
break;
}
ret = pthread_create(&blikvm_monitor_thread, NULL, blikvm_atx_monitor, NULL);
if(thread_ret != 0)
{
BLILOG_E(TAG,"creat monitor thread failed\n");
break;
}
ret = 0;
Expand All @@ -137,48 +158,33 @@ static blikvm_void_t *blikvm_atx_loop(void *_)
blikvm_int32_t ret = recvfrom(g_atx.socket,(void *)g_rev_buff.recvBuf,DEFAULT_BUF_LEN,
0,(struct sockaddr *)&(g_atx.socket_addr.send_addr), &(g_atx.socket_addr.send_addr_len));

blikvm_uint8_t state[1];
if( ret == 1U)
{
g_atx.fp = fopen("/dev/shm/blikvm/atx","wb+");
blikvm_int32_t ret_len;
BLILOG_D(TAG,"atx get:%d\n",g_rev_buff.recvBuf[0]);
switch(g_rev_buff.recvBuf[0])
{
case blikvm_int32_t(ATX_SHORT):
digitalWrite(4, HIGH);
digitalWrite(PIN_POWER, HIGH);
usleep(500*1000);
digitalWrite(4, LOW);
digitalWrite(PIN_POWER, LOW);
BLILOG_D(TAG,"atx power on\n");
state[0] = 0b01000000 | blikvm_read_atx_state();
break;
case blikvm_int32_t(ATX_LONG):
digitalWrite(4, HIGH);
digitalWrite(PIN_POWER, HIGH);
usleep(2000*1000);
digitalWrite(4, LOW);
digitalWrite(PIN_POWER, LOW);
BLILOG_D(TAG,"atx power off\n");
state[0] = 0b00000000 | blikvm_read_atx_state();
break;
case blikvm_int32_t(ATX_RESET):
digitalWrite(16, HIGH);
digitalWrite(PIN_RESET, HIGH);
usleep(500*1000);
digitalWrite(2, LOW);
state[0] = 0b01000000 | blikvm_read_atx_state();
digitalWrite(PIN_RESET, LOW);
BLILOG_D(TAG,"atx restart\n");
break;
default:
BLILOG_E(TAG,"atx get error command:%d\n",g_rev_buff.recvBuf[0]);
break;
}

ret_len = fwrite(state, sizeof(state) , 1, g_atx.fp);
if(ret_len > 0)
{
BLILOG_D(TAG,"write ok: %d sizeof state:%d\n",ret_len,sizeof(state));
}
fflush(g_atx.fp);
fclose(g_atx.fp);

}
else
{
Expand All @@ -190,73 +196,46 @@ static blikvm_void_t *blikvm_atx_loop(void *_)
return NULL;
}

static blikvm_int8_t blikvm_read_atx_state()
static blikvm_void_t *blikvm_atx_monitor(void *_)
{
blikvm_int8_t ret = 0;

do
{
// led_pwr = os.popen('gpio -g read 24').read()
// led_hdd = os.popen('gpio -g read 22').read()
FILE *fp;
fp = popen("gpio -g read 24","r");
blikvm_int8_t result_buf[1024];
if( fp == NULL)
{
BLILOG_E(TAG,"read pwr led gpio error\n");
break;
}
blikvm_int32_t len = fread(result_buf,1,sizeof(result_buf),fp);

if( len > 0)
{
blikvm_int32_t gpio = atoi(result_buf);
if( gpio == 1 )
{
ret = 0b10000000;
}
}
else
blikvm_uint8_t state[1];
state[0] = blikvm_read_atx_state();
g_atx.fp = fopen("/dev/shm/blikvm/atx","wb+");
if(NULL == g_atx.fp)
{
BLILOG_E(TAG,"read pwr led buff error len:%d conten:%s c0:%d c1:%d\n",len,result_buf,result_buf[0],result_buf[1]);
BLILOG_E(TAG,"open /dev/shm/blikvm/atx error\n");
break;
}
//关闭文件指针
if(-1 == pclose(fp))
blikvm_int32_t ret_len = fwrite(state, sizeof(state) , 1, g_atx.fp);
if(ret_len < 0)
{
BLILOG_E(TAG,"close file point failure.\n");
ret = 0;
break;
BLILOG_E(TAG,"write error: %d sizeof state:%d\n",ret_len,sizeof(state));
}
fflush(g_atx.fp);
fclose(g_atx.fp);
usleep(ATX_CYCLE*1000);
}while(1);
return NULL;
}

fp = popen("gpio -g read 22","r");
if( fp == NULL)
{
BLILOG_E(TAG,"read hdd led gpio error\n");
break;
}
if(fread(result_buf,1,sizeof(result_buf),fp) > 0)
{
blikvm_int32_t gpio_hdd = atoi(result_buf);
if( gpio_hdd == 1 )
{
ret = ret | 0b00001000;
}
}
else
static blikvm_uint8_t blikvm_read_atx_state()
{
blikvm_uint8_t ret = 0;

do
{
if(digitalRead(PIN_LED_PWR) == 1 )
{
BLILOG_E(TAG,"read hdd led buff error\n");
break;
ret = 0b01000000;
}
//关闭文件指针
if(-1 == pclose(fp))
if(digitalRead(PIN_LED_HDD) == 1 )
{
BLILOG_E(TAG,"close file point failure.\n");
ret = 0;
break;
ret = ret | 0b00001000;
}

} while (0>1);
BLILOG_D(TAG,"atx state ret:%d\n",ret);
BLILOG_D(TAG,"atx state ret:%02X\n",ret);
return ret;
}
2 changes: 1 addition & 1 deletion src/kvmd/blikvm_oled/blikvm_oled.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ blikvm_int8_t blikvm_oled_start()
blikvm_int8_t ret = -1;
do
{
if( popen("python kvmd/blikvm_oled/blikvm-oled.py &","r") == NULL)
if( popen("python /usr/bin/blikvm-oled.py &","r") == NULL)
{
BLILOG_E(TAG,"don't find oled py\n");
break;
Expand Down

0 comments on commit 0704c1a

Please sign in to comment.