rx tx 速率统计

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <cutils/sockets.h>
#include <ctype.h>
#include <pthread.h>
#include <errno.h>
#include <utils/Log.h>


static void* wlan_speed_monitor_thread(void * pWcndWlan) {
        int server_fd = socket_local_server(WLANSP_ENG_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);

        while(1) {

                sleep(3);
                ALOGD("wlan_speed_monitor_thread running");
        }

        return NULL;


}

 

int start_wlan_speed_monitor(){


        //if(!pWcndManger) return -1;

        pthread_t thread_id;

        if (pthread_create(&thread_id, NULL, wlan_speed_monitor_thread, NULL))
        {
                ALOGE("start_client_listener: pthread_create (%s)", strerror(errno));
                return -1;
        }

        return 0;


}

 

static unsigned long long int get_bytes(const char * filename)
{
  char str_rxbytes[MAX_RXBYTES_LEN + 2]={0};  /* plus one control char and one null char at the tail */
  FILE *fp_rxBytes;

  fp_rxBytes = fopen(filename,"r");
  if(fp_rxBytes == NULL){
    ALOGD("file %s not ready yet\n",filename);
    return 0;   /* return type is unsigned long long int, both operation-failure and no-bytes will return 0 */
  }

  if(fgets(str_rxbytes,MAX_RXBYTES_LEN + 2,fp_rxBytes) == NULL){
    fclose(fp_rxBytes);
    ALOGD("file %s get file content failed\n",filename);
    return -1;
  }
  str_rxbytes[strlen(str_rxbytes)-1] = '\0';  /* to replace the control char */

  fclose(fp_rxBytes);
  return strtoull(str_rxbytes,NULL,10);  /* sysfs node rx_bytes's radix is 10 */

}

 

 

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


          unsigned long long int oldrxbytes = 0;
          unsigned long long int newrxbytes = 0;
          unsigned long long int oldtxbytes = 0;
          unsigned long long int newtxbytes = 0;

         start_wlan_speed_monitor();
        while(1){

                        /*before acurate the speed,we should get rx/tx bytes again, we can't use the byte got lasttime*/
                        oldrxbytes = get_bytes(WLANRXFILE);
                        oldtxbytes = get_bytes(WLANTXFILE);

                        sleep(1);
                        newrxbytes = get_bytes(WLANRXFILE);
                        newtxbytes = get_bytes(WLANTXFILE);
                        speedtx = (newtxbytes - oldtxbytes);
                        speedrx = (newrxbytes - oldrxbytes);
                        ALOGD("speedtx=%f,speedrx=%f \n",speedtx,speedrx);
                        /*sleep for a while */
                        //sleep(1);


        }
        return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值