You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/12/29 03:55:42 UTC

[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on a change in pull request #944: Add a system tool to get/set hostname

xiaoxiang781216 commented on a change in pull request #944:
URL: https://github.com/apache/incubator-nuttx-apps/pull/944#discussion_r776143843



##########
File path: system/hostname/hostname_main.c
##########
@@ -0,0 +1,173 @@
+/****************************************************************************
+ * apps/system/hostname/hostname_main.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 <stdio.h>
+#include <stdlib.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+
+#include <net/if.h>
+#include <arpa/inet.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_HELLO_IPADDR     0xc0a8eacd
+#define CONFIG_HELLO_PORT       81
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Socket related variables */
+
+static int g_listen_fd;
+
+static struct sockaddr_in g_sa;
+
+int initialize_socket(void)
+{
+  /* Prepare listen address */
+
+  g_sa.sin_family      = AF_INET;
+  g_sa.sin_port        = HTONS(CONFIG_HELLO_PORT);
+  g_sa.sin_addr.s_addr = HTONL(CONFIG_HELLO_IPADDR);
+
+  /* Open listening socket */
+
+  g_listen_fd = socket(g_sa.sin_family, SOCK_DGRAM, 0);
+
+  /* Verify socket creation */
+
+  if (!(g_listen_fd > 0))
+    {
+      return -1;
+    }
+
+  return 0;
+}
+
+int get_phy_id(void)
+{
+  int ret;
+  uint16_t phy_id;
+  struct ifreq ifr;
+
+  /* Prepare ifreq */
+
+  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
+
+  ret = ioctl(g_listen_fd, SIOCGMIIPHY, (unsigned long) &ifr);
+
+  printf("call to %s() ", "ioctl");
+
+  if (ret != 0)
+    {
+      printf("failed! (value %d, errno %d)\n", ret, errno);
+      return -1;
+    }
+
+  phy_id = ifr.ifr_mii_phy_id;
+
+  return phy_id;
+}
+
+int get_phy_reg(uint16_t phy_id, uint16_t reg_num, uint16_t *val)
+{
+  int ret;
+  struct ifreq ifr;
+
+  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
+
+  ifr.ifr_mii_phy_id = phy_id;
+  ifr.ifr_mii_reg_num = reg_num;
+
+  ret = ioctl(g_listen_fd, SIOCGMIIREG, (unsigned long) &ifr);
+
+  if (ret == OK)
+    {
+      *val = ifr.ifr_mii_val_out;
+    }
+
+  return ret;
+}
+
+int set_phy_reg(uint16_t phy_id, uint16_t reg_num, uint16_t val)

Review comment:
       should we remove all unrelated function and header file?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org