Add bandwidth internal block.

This commit is contained in:
geommer 2016-03-28 14:49:15 +02:00
parent 83e756dc0c
commit c92baa1bca
2 changed files with 70 additions and 2 deletions

View file

@ -97,7 +97,7 @@ enum {
#define NOT_INHERIT_BLK(blk) (((blk)->attr & BLKA_INHERIT)==0)
#define CMONLEN 16
#define YA_INTERNAL_LEN 4
#define YA_INTERNAL_LEN 5
typedef struct ya_monitor ya_monitor_t;
typedef struct ya_bar ya_bar_t;

View file

@ -13,12 +13,14 @@ void ya_int_uptime(ya_block_t * blk);
void ya_int_memory(ya_block_t * blk);
void ya_int_thermal(ya_block_t *blk);
void ya_int_brightness(ya_block_t *blk);
void ya_int_bandwidth(ya_block_t *blk);
struct reserved_blk ya_reserved_blks[YA_INTERNAL_LEN] = {
{"YA_INT_TIME", ya_int_time},
{"YA_INT_UPTIME", ya_int_uptime},
{"YA_INT_THERMAL", ya_int_thermal},
{"YA_INT_BRIGHTNESS", ya_int_brightness}
{"YA_INT_BRIGHTNESS", ya_int_brightness},
{"YA_INT_BANDWIDTH", ya_int_bandwidth}
//{"YA_INT_MEMORY", ya_int_memory}
};
@ -88,6 +90,10 @@ void ya_int_thermal(ya_block_t *blk) {
tfile = fopen(fpath, "r");
if (tfile == NULL) {
//TODO handle and exit thread
fprintf(stderr, "Error opening file %s\n", fpath);
strncpy(blk->buf, "BLOCK ERROR!", strlen("BLOCK ERROR!"));
ya_draw_pango_text(blk);
pthread_exit(NULL);
}
fclose(tfile);
while (1) {
@ -125,6 +131,10 @@ void ya_int_brightness(ya_block_t *blk) {
tfile = fopen(fpath, "r");
if (tfile == NULL) {
//TODO handle and exit thread
fprintf(stderr, "Error opening file %s\n", fpath);
strncpy(blk->buf, "BLOCK ERROR!", strlen("BLOCK ERROR!"));
ya_draw_pango_text(blk);
pthread_exit(NULL);
}
while(1) {
tfile = fopen(fpath, "r");
@ -136,4 +146,62 @@ void ya_int_brightness(ya_block_t *blk) {
fclose(tfile);
}
}
void ya_int_bandwidth(ya_block_t * blk) {
unsigned long rx, tx, orx, otx;
unsigned int rxrate, txrate;
FILE *rxfile, *txfile;
char rxpath[128];
char txpath[128];
char rxc, txc;
snprintf(rxpath, 128, "/sys/class/net/%s/statistics/rx_bytes", blk->internal->option[0]);
snprintf(txpath, 128, "/sys/class/net/%s/statistics/tx_bytes", blk->internal->option[0]);
rxfile = fopen(rxpath, "r");
txfile = fopen(txpath, "r");
if (rxfile == NULL || txfile == NULL) {
//TODO handle and exit thread
fprintf(stderr, "Error opening file %s or %s\n", rxpath, txpath);
strncpy(blk->buf, "BLOCK \nERROR!", strlen("BLOCK \nERROR!"));
ya_draw_pango_text(blk);
pthread_exit(NULL);
}
else {
fscanf(rxfile, "%lu", &orx);
fscanf(txfile, "%lu", &otx);
}
fclose(rxfile);
fclose(txfile);
while(1) {
txc = rxc = 'K';
rxfile = fopen(rxpath, "r");
txfile = fopen(txpath, "r");
fscanf(rxfile, "%lu", &rx);
fscanf(txfile, "%lu", &tx);
rxrate = (rx - orx)/((blk->sleep)*1024);
txrate = (tx - otx)/((blk->sleep)*1024);
if(rxrate > 1024) {
rxrate/=1024;
rxc = 'M';
}
if(txrate > 1024) {
txrate/=1024;
txc = 'M';
}
orx = rx;
otx = tx;
snprintf(blk->buf, 40, "%4u%c %4u%c", rxrate, rxc, txrate, txc);
ya_draw_pango_text(blk);
fclose(rxfile);
fclose(txfile);
memset(blk->buf, '\0', 40);
sleep(blk->sleep);
}
}
#endif //YA_INTERNAL