From 7b0c670150d672ea9dbe06673db743ff25ae2830 Mon Sep 17 00:00:00 2001
From: wangk <2865709459@qq.com>
Date: Tue, 26 Nov 2024 13:59:11 +0800
Subject: [PATCH] Add ip modify function by modifying the xml function

---
 kit/kit_xml.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 kit/kit_xml.h |  5 ++--
 test/test.c   |  2 +-
 3 files changed, 77 insertions(+), 4 deletions(-)

diff --git a/kit/kit_xml.c b/kit/kit_xml.c
index 845a24b..db563ef 100644
--- a/kit/kit_xml.c
+++ b/kit/kit_xml.c
@@ -11,6 +11,7 @@ static void set_item(EMS_XML *EMS_XML_item)
     snprintf(EMS_XML_item->can_id_name, NAME_STR_LEN, "%s", "3");
     snprintf(EMS_XML_item->can_recv_dlc, NAME_STR_LEN, "%s", "8");
     snprintf(EMS_XML_item->can_send_dlc, NAME_STR_LEN, "%s", "8");
+    snprintf(EMS_XML_item->ems_local_ip, NAME_STR_LEN, "%s", "192.168.0.111");
 }
  
 //创建节点
@@ -38,6 +39,7 @@ static xmlNodePtr create_node(const EMS_XML *EMS_XML_item)
     xmlNewChild(EMS_XML_node, NULL, BAD_CAST"can_id_name", (xmlChar *)EMS_XML_item->can_id_name);
     xmlNewChild(EMS_XML_node, NULL, BAD_CAST"can_recv_dlc", (xmlChar *)EMS_XML_item->can_recv_dlc);
     xmlNewChild(EMS_XML_node, NULL, BAD_CAST"can_send_dlc", (xmlChar *)EMS_XML_item->can_send_dlc);
+    xmlNewChild(EMS_XML_node, NULL, BAD_CAST"ems_local_ip", (xmlChar *)EMS_XML_item->ems_local_ip);
  
     return EMS_XML_node;
 }
@@ -193,6 +195,76 @@ static int parse_EMS_XML_node(xmlDocPtr doc, xmlNodePtr cur)
         printf("can_send_dlc: %s\n", key);
         xmlFree(key);
     }
+    if ((!xmlStrcmp(cur->name, (const xmlChar *)"ems_local_ip"))) {
+        key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+        int fd;
+        char buffer[1024];
+        ssize_t bytes_read, bytes_written;
+        fd = open("/etc/netplan/01-netcfg.yaml", O_RDWR);
+        if (fd == -1)
+        {
+            perror("open the netcfg failed!");
+            return 1;
+        }
+
+        // 读取文件内容
+        bytes_read = read(fd, buffer, 1024);
+        if (bytes_read == -1) {
+            perror("read file failed!");
+            close(fd);
+            return 1;
+        } else if (bytes_read == 0) {
+            printf("the file is empty\n");
+            close(fd);
+            return 0;
+        }
+        buffer[bytes_read] = '\0';
+
+        // 定位包含 "addresses:" 的行
+        char *addresses_line = strstr(buffer, "addresses:");
+        if (addresses_line!= NULL) {
+            // 定位冒号位置
+            char *colon_pos = strchr(addresses_line, ':');
+            if (colon_pos!= NULL) {
+                colon_pos++;
+                // 跳过可能的空格等字符,找到IP地址起始位置
+                while (*colon_pos!= '\0' && isspace(*colon_pos)) {
+                    colon_pos++;
+                }
+                // 记录IP地址起始位置
+                char *ip_start = colon_pos;
+                // 找到IP地址结束位置(遇到逗号、换行符或右方括号且下一个字符不是数字)
+                char *ip_end = ip_start;
+                while (*ip_end!= '\0' && *ip_end!= ',' && *ip_end!= '\n' &&
+                    ((*ip_end!= ']') || ((*ip_end == ']') && (!isdigit(*(ip_end + 1)))))) {
+                    ip_end++;
+                }
+                // 计算IP地址部分的长度
+                size_t ip_length = ip_end - ip_start;
+                // 替换IP地址
+                memmove(colon_pos, key, strlen(key));
+                // 将剩余部分移动到新位置
+                memmove(colon_pos + strlen(key), ip_end, strlen(ip_end));
+            }
+        }
+
+        // 将文件指针移回文件开头
+        lseek(fd, 0, SEEK_SET);
+
+        // 将修改后的内容写回文件
+        bytes_written = write(fd, buffer, strlen(buffer));
+        if (bytes_written == -1) {
+            perror("write");
+            close(fd);
+            return 1;
+        }
+
+        // 关闭文件
+        close(fd);
+
+        printf("ems_local_ip: %s\n", key);
+        xmlFree(key);
+    }
     cur = cur->next;
     }
     return 0;
@@ -263,7 +335,7 @@ void createXml()
  
     printf("create function:\n");
     if (access(EMS_XML_file, F_OK) == 0) {
-        printf("The %s is exist!\n", EMS_XML_file);
+        printf("The %s is exist!, please modify the file directly!\n", EMS_XML_file);
     }
     else {
         //文件不存在,创建一个
diff --git a/kit/kit_xml.h b/kit/kit_xml.h
index 21a84af..63aad40 100644
--- a/kit/kit_xml.h
+++ b/kit/kit_xml.h
@@ -3,6 +3,7 @@
 
 
 #include <stdio.h>
+#include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -15,14 +16,14 @@
 #define NAME_STR_LEN         32
 #define DEFAULT_XML_FILE "EMS_XML.xml"
  
- 
 typedef struct EMS_XML_t {
     int id;              //编号
     char can_recv_name[NAME_STR_LEN];     
     char can_send_name[NAME_STR_LEN];
     char can_id_name[NAME_STR_LEN];   
     char can_recv_dlc[NAME_STR_LEN]; 
-    char can_send_dlc[NAME_STR_LEN];    
+    char can_send_dlc[NAME_STR_LEN]; 
+    char ems_local_ip[NAME_STR_LEN];   
 }EMS_XML;
 
 void createXml();
diff --git a/test/test.c b/test/test.c
index 5a80b67..7ba91b1 100644
--- a/test/test.c
+++ b/test/test.c
@@ -213,6 +213,6 @@ void *testXMLThread(void *arg)
 {
     logger_level_printf(LOGGER_DEBUG_LEVEL, arg);
     printf("XML test!\n");
-    createXml();
+    // createXml();
     paraXml();
 }