fe1aac4c创建于 4月20日历史提交
/****************************************************************************
 * apps/netutils/netlib/netlib_getiobinfo.c
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The
 * ASF licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <netinet/in.h>
#include <net/if.h>

#include "netutils/netlib.h"

#ifdef CONFIG_MM_IOB

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/* Determines the size of an intermediate buffer that must be large enough
 * to handle the longest line generated by this logic.
 */

#define PROCFS_LINELEN   80
#define PROCFS_IOB_PATH "/proc/iobinfo"

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: netlib_get_iobinfo
 *
 * Description:
 *   Get the network iob info.
 *
 * Parameters:
 *   iob   The pointer to return iobinfo.
 *
 * Return:
 *   0 on success; a nagtive on failure.
 *
 ****************************************************************************/

int netlib_get_iobinfo(FAR struct iob_stats_s *iob)
{
  int ret = OK;
  FAR FILE *stream;
  char line[PROCFS_LINELEN];

  stream = fopen(PROCFS_IOB_PATH, "r");
  if (stream == NULL)
    {
      fprintf(stderr, "ERROR: Failed to open path:%s \n", PROCFS_IOB_PATH);
      return -errno;
    }

  /*  Read ioninfo header and next is the data */

  if (fgets(line, sizeof(line), stream) != NULL)
    {
      if (fgets(line, sizeof(line), stream) == NULL)
        {
          ret = -EINVAL;
          fclose(stream);
          return ret;
        }

      if (sscanf(line, "%d %d %d %d", &iob->ntotal, &iob->nfree,
                 &iob->nwait, &iob->nthrottle) <= 0)
        {
          ret = -errno;
          fclose(stream);
          return ret;
        }
    }

  fclose(stream);
  return ret;
}

#endif /* CONFIG_MM_IOB */