rx tx 速率统计

来源:互联网 发布:有个搞笑的淘宝真人秀 编辑:程序博客网 时间:2024/05/16 18:05

#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;

}

 

0 0
原创粉丝点击