From 0704c1a41b4832cc6e839f350c5c3be75297df10 Mon Sep 17 00:00:00 2001 From: ThomasVon2021 <1767893964@qq.com> Date: Sat, 28 Jan 2023 15:42:55 +0000 Subject: [PATCH] fix atx function on PCIe version --- script/install_release.py | 4 +- script/update.py | 13 +-- src/config/package.json | 2 +- src/demo.c | 62 ++++++++++++- src/kvmd/blikvm_atx/blikvm_atx.c | 135 ++++++++++++----------------- src/kvmd/blikvm_oled/blikvm_oled.c | 2 +- 6 files changed, 128 insertions(+), 90 deletions(-) mode change 100644 => 100755 script/install_release.py diff --git a/script/install_release.py b/script/install_release.py old mode 100644 new mode 100755 index f7a5686..ab30b59 --- a/script/install_release.py +++ b/script/install_release.py @@ -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): @@ -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 ) diff --git a/script/update.py b/script/update.py index faba9e0..3adb2ab 100755 --- a/script/update.py +++ b/script/update.py @@ -5,7 +5,7 @@ import sys import subprocess -download_path = "/tmp/" +download_path = "/tmp/kvm_update/" update_result = False def main(): global update_result @@ -13,11 +13,14 @@ def main(): 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 @@ -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: @@ -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() diff --git a/src/config/package.json b/src/config/package.json index f53a31b..30d50fc 100755 --- a/src/config/package.json +++ b/src/config/package.json @@ -1 +1 @@ -{"version": "1.0.5", "version_int": 105, "md5value": "0989d8e835fe3a8a4b669364811ff470"} +{"version": "1.0.6", "version_int": 106, "md5value": "56b0725d3ac9fd6e01ece28a88444b33", "oled_type": 0} \ No newline at end of file diff --git a/src/demo.c b/src/demo.c index 0353e20..53c7cb7 100755 --- a/src/demo.c +++ b/src/demo.c @@ -1,13 +1,20 @@ #include #include #include - +#include +#include #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; @@ -15,7 +22,7 @@ int main(int argc, char *argv[]) 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) @@ -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; } \ No newline at end of file diff --git a/src/kvmd/blikvm_atx/blikvm_atx.c b/src/kvmd/blikvm_atx/blikvm_atx.c index 3016f05..1bd5eb3 100755 --- a/src/kvmd/blikvm_atx/blikvm_atx.c +++ b/src/kvmd/blikvm_atx/blikvm_atx.c @@ -5,6 +5,7 @@ *-----------------------------------------------------------------------------* * 2022-09-10 | 0.1 | Thomasvon | create ******************************************************************************/ + #include #include #include "blikvm_atx.h" @@ -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 { @@ -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"); @@ -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); @@ -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; @@ -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 { @@ -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; } \ No newline at end of file diff --git a/src/kvmd/blikvm_oled/blikvm_oled.c b/src/kvmd/blikvm_oled/blikvm_oled.c index 7387feb..53d0e7d 100755 --- a/src/kvmd/blikvm_oled/blikvm_oled.c +++ b/src/kvmd/blikvm_oled/blikvm_oled.c @@ -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;