From b22c2f7976b61cb36a9ac7338876944c51f5e507 Mon Sep 17 00:00:00 2001 From: hqm <13720409820@163.com> Date: Fri, 28 Jan 2022 16:32:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=BA=90=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84=EF=BC=8C=E4=BF=AE=E5=A4=8D=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/linux/Makefile | 4 +- example/linux/linux_main.c | 3 +- .../ex_list/ex_list.c | 101 ++++++++---------- extensions/ex_list/ex_list.h | 14 +++ {inc => extensions/ex_list}/list.h | 0 extensions/net_cmds/socket_cmds.c | 12 ++- 6 files changed, 71 insertions(+), 63 deletions(-) rename example/linux/xcmd_platform.c => extensions/ex_list/ex_list.c (83%) create mode 100644 extensions/ex_list/ex_list.h rename {inc => extensions/ex_list}/list.h (100%) diff --git a/example/linux/Makefile b/example/linux/Makefile index 58600b2..2fba5d9 100755 --- a/example/linux/Makefile +++ b/example/linux/Makefile @@ -3,10 +3,10 @@ BIN = xcmder OBJ += xcmd.o \ xcmd_default_keys.o \ xcmd_default_cmds.o \ - xcmd_platform.o \ linux_main.o \ test.o \ ex_keys.o \ + ex_list.o \ ex_cmds.o \ socket_cmds.o \ diskio.o \ @@ -18,6 +18,7 @@ OBJ += xcmd.o \ VPATH := ../../src \ ../../extensions/test \ ../../extensions/ex_keys \ + ../../extensions/ex_list \ ../../extensions/ex_cmds \ ../../extensions/net_cmds \ ./FatFs/source \ @@ -27,6 +28,7 @@ INC += -I./ \ -I../../inc \ -I../../extensions/test \ -I../../extensions/ex_keys \ + -I../../extensions/ex_list \ -I../../extensions/ex_cmds \ -I../../extensions/net_cmds \ -IFatFs/source \ diff --git a/example/linux/linux_main.c b/example/linux/linux_main.c index ced16c3..840a2c8 100755 --- a/example/linux/linux_main.c +++ b/example/linux/linux_main.c @@ -14,6 +14,7 @@ #include "test.h" #include "ex_keys.h" #include "ex_cmds.h" +#include "ex_list.h" #include "fs_cmds.h" #include "socket_cmds.h" #include "ff.h" @@ -154,7 +155,7 @@ int main(void) ex_cmds_init(); socket_cmds_init(); fs_cmds_init(); - linux_cmd_init(); + ex_list_init(); while (1) { xcmd_task(); diff --git a/example/linux/xcmd_platform.c b/extensions/ex_list/ex_list.c similarity index 83% rename from example/linux/xcmd_platform.c rename to extensions/ex_list/ex_list.c index ef3a6c4..73655c6 100644 --- a/example/linux/xcmd_platform.c +++ b/extensions/ex_list/ex_list.c @@ -1,7 +1,6 @@ #include #include #include -#include "xcmd_platform.h" #include "xcmd.h" #include "list.h" struct student @@ -13,22 +12,21 @@ struct student static int list_test(int argc, char* argv[]) { - struct student *p; - struct student *q; + struct student *p; struct student stu1; struct student stu2; struct list_head *pos; - //链表的初始化 + //閾捐〃鐨勫垵濮嬪寲 INIT_LIST_HEAD(&stu1.stu_list); INIT_LIST_HEAD(&stu2.stu_list); - //头插法创建stu stu1链表 + //澶存彃娉曞垱寤簊tu stu1閾捐〃 for (int i = 0;i < 6;i++) { p = (struct student *)malloc(sizeof(struct student)); p->ID=i; p->math = i+80; - //头插法 + //澶存彃娉 list_add(&p->stu_list,&stu1.stu_list); - //尾插法 + //灏炬彃娉 //list_add_tail(&p->list,&stu.list); } @@ -37,14 +35,14 @@ static int list_test(int argc, char* argv[]) printf("ID = %d,math = %d\n",((struct student*)pos)->ID,((struct student*)pos)->math); } - //尾插法创建stu stu1链表 + //灏炬彃娉曞垱寤簊tu stu1閾捐〃 for (int i = 0;i < 6;i++) { p = (struct student *)malloc(sizeof(struct student)); p->ID=i; p->math = i+80; - //头插法 + //澶存彃娉 //list_add(&p->stu_list,&stu1.stu_list); - //尾插法 + //灏炬彃娉 list_add_tail(&p->stu_list,&stu2.stu_list); } @@ -59,25 +57,24 @@ static int list_test(int argc, char* argv[]) static int list_note_del(int argc, char* argv[]) { struct student *p; - struct student *q; struct student stu1; struct student stu2; struct list_head *pos1; - //注意这里的pos2,后面会解释为什么定义为 + //娉ㄦ剰杩欓噷鐨刾os2锛屽悗闈細瑙i噴涓轰粈涔堝畾涔変负 struct student *pos2; //stu = (struct student*)malloc(sizeof(struct student)); - //链表的初始化 + //閾捐〃鐨勫垵濮嬪寲 INIT_LIST_HEAD(&stu1.stu_list); INIT_LIST_HEAD(&stu2.stu_list); LIST_HEAD(stu); - //头插法创建stu stu1链表 + //澶存彃娉曞垱寤簊tu stu1閾捐〃 for (int i = 0;i < 6;i++) { p = (struct student *)malloc(sizeof(struct student)); p->ID=i; p->math = i+80; - //头插法 + //澶存彃娉 list_add(&p->stu_list,&stu1.stu_list); - //尾插法 + //灏炬彃娉 //list_add_tail(&p->list,&stu.list); } @@ -86,7 +83,7 @@ static int list_note_del(int argc, char* argv[]) printf("ID = %d,math = %d\n",((struct student*)pos1)->ID,((struct student*)pos1)->math); } - //删除 + //鍒犻櫎 list_for_each_entry(pos2,&stu1.stu_list,stu_list) { if (pos2->ID == 4) { list_del(&pos2->stu_list); @@ -104,25 +101,24 @@ static int list_note_del(int argc, char* argv[]) static int list_note_rep(int argc, char* argv[]) { struct student *p; - struct student *q; struct student stu1; struct student stu2; struct list_head *pos1; struct student *pos2; struct student new_obj={.ID=100,.math=100}; //stu = (struct student*)malloc(sizeof(struct student)); - //链表的初始化 + //閾捐〃鐨勫垵濮嬪寲 INIT_LIST_HEAD(&stu1.stu_list); INIT_LIST_HEAD(&stu2.stu_list); LIST_HEAD(stu); - //头插法创建stu stu1链表 + //澶存彃娉曞垱寤簊tu stu1閾捐〃 for (int i = 0;i < 6;i++) { p = (struct student *)malloc(sizeof(struct student)); p->ID=i; p->math = i+80; - //头插法 + //澶存彃娉 list_add(&p->stu_list,&stu1.stu_list); - //尾插法 + //灏炬彃娉 //list_add_tail(&p->list,&stu.list); } printf("list_add: \r\n"); @@ -130,7 +126,7 @@ static int list_note_rep(int argc, char* argv[]) printf("ID = %d,math = %d\n",((struct student*)pos1)->ID,((struct student*)pos1)->math); } - //替换 + //鏇挎崲 list_for_each_entry(pos2,&stu1.stu_list,stu_list) { if (pos2->ID == 4) { list_replace(&pos2->stu_list,&new_obj.stu_list); @@ -147,26 +143,24 @@ static int list_note_rep(int argc, char* argv[]) static int list_note_del_insert(int argc, char* argv[]) { - struct student *p; - struct student *q; + struct student *p; struct student stu1; struct student stu2; struct list_head *pos1; struct student *pos2; - struct student new_obj={.ID=100,.math=100}; //stu = (struct student*)malloc(sizeof(struct student)); - //链表的初始化 + //閾捐〃鐨勫垵濮嬪寲 INIT_LIST_HEAD(&stu1.stu_list); INIT_LIST_HEAD(&stu2.stu_list); LIST_HEAD(stu); - //头插法创建stu stu1链表 + //澶存彃娉曞垱寤簊tu stu1閾捐〃 for (int i = 0;i < 6;i++) { p = (struct student *)malloc(sizeof(struct student)); p->ID=i; p->math = i+80; - //头插法 + //澶存彃娉 list_add(&p->stu_list,&stu1.stu_list); - //尾插法 + //灏炬彃娉 //list_add_tail(&p->list,&stu.list); } printf("list_add: \r\n"); @@ -174,7 +168,7 @@ static int list_note_del_insert(int argc, char* argv[]) printf("ID = %d,math = %d\n",((struct student*)pos1)->ID,((struct student*)pos1)->math); } - //移位替换 + //绉讳綅鏇挎崲 list_for_each_entry(pos2,&stu1.stu_list,stu_list) { if (pos2->ID == 0) { list_move(&pos2->stu_list,&stu1.stu_list); @@ -190,40 +184,38 @@ static int list_note_del_insert(int argc, char* argv[]) static int List_to_merge(int argc, char* argv[]) { - struct student *p; + struct student *p; struct student *q; struct student stu1; struct student stu2; struct list_head *pos1; - struct student *pos2; - struct student new_obj={.ID=100,.math=100}; //stu = (struct student*)malloc(sizeof(struct student)); - //链表的初始化 + //閾捐〃鐨勫垵濮嬪寲 INIT_LIST_HEAD(&stu1.stu_list); INIT_LIST_HEAD(&stu2.stu_list); LIST_HEAD(stu); - //头插法创建stu1 list链表 + //澶存彃娉曞垱寤簊tu1 list閾捐〃 for (int i = 0;i < 6;i++) { p = (struct student *)malloc(sizeof(struct student)); p->ID=i; p->math = i+80; - //头插法 + //澶存彃娉 list_add(&p->stu_list,&stu1.stu_list); - //尾插法 + //灏炬彃娉 //list_add_tail(&p->list,&stu.list); } printf("stu1: \r\n"); list_for_each(pos1, &stu1.stu_list) { printf("ID = %d,math = %d\n",((struct student*)pos1)->ID,((struct student*)pos1)->math); } - //头插法创建stu2 list 链表 + //澶存彃娉曞垱寤簊tu2 list 閾捐〃 for (int i = 0;i < 3;i++) { q = (struct student *)malloc(sizeof(struct student)); q->ID=i; q->math = i+80; - //头插法 + //澶存彃娉 list_add(&q->stu_list,&stu2.stu_list); - //尾插法 + //灏炬彃娉 //list_add_tail(&p->list,&stu.list); } printf("stu2: \r\n"); @@ -231,7 +223,7 @@ static int List_to_merge(int argc, char* argv[]) printf("ID = %d,math = %d\n",((struct student*)pos1)->ID,((struct student*)pos1)->math); } - //合并 + //鍚堝苟 list_splice(&stu1.stu_list,&stu2.stu_list); printf("list_splice\r\n"); list_for_each(pos1, &stu2.stu_list) { @@ -245,25 +237,22 @@ static int List_to_merge(int argc, char* argv[]) static int List_traverse(int argc, char* argv[]) { struct student *p; - struct student *q; struct student stu1; struct student stu2; struct list_head *pos1; - struct student *pos2; - struct student new_obj={.ID=100,.math=100}; //stu = (struct student*)malloc(sizeof(struct student)); - //链表的初始化 + //閾捐〃鐨勫垵濮嬪寲 INIT_LIST_HEAD(&stu1.stu_list); INIT_LIST_HEAD(&stu2.stu_list); LIST_HEAD(stu); - //头插法创建stu stu1链表 + //澶存彃娉曞垱寤簊tu stu1閾捐〃 for (int i = 0;i < 6;i++) { p = (struct student *)malloc(sizeof(struct student)); p->ID=i; p->math = i+80; - //头插法 + //澶存彃娉 list_add(&p->stu_list,&stu1.stu_list); - //尾插法 + //灏炬彃娉 //list_add_tail(&p->list,&stu.list); } printf("stu1: \r\n"); @@ -281,16 +270,16 @@ static int List_traverse(int argc, char* argv[]) static xcmd_t cmds[] = { - {"list", list_test, "list test demo", NULL}, //链表增加节点 - {"list1", list_note_del, "list note del demo", NULL}, //链表删除节点 - {"list2", list_note_rep, "list note replace demo", NULL}, //链表替换节点 - {"list3", list_note_del_insert, "list note del and insert demo", NULL}, //链表删除并插入节点 - {"list4", List_to_merge, "List to merge", NULL}, //链表的合并 - {"list5", List_traverse, "List to merge", NULL}, //链表的遍历 + {"list", list_test, "list test demo", NULL}, //閾捐〃澧炲姞鑺傜偣 + {"list1", list_note_del, "list note del demo", NULL}, //閾捐〃鍒犻櫎鑺傜偣 + {"list2", list_note_rep, "list note replace demo", NULL}, //閾捐〃鏇挎崲鑺傜偣 + {"list3", list_note_del_insert, "list note del and insert demo", NULL}, //閾捐〃鍒犻櫎骞舵彃鍏ヨ妭鐐 + {"list4", List_to_merge, "List to merge", NULL}, //閾捐〃鐨勫悎骞 + {"list5", List_traverse, "List to merge", NULL}, //閾捐〃鐨勯亶鍘 }; -void linux_cmd_init(void) +void ex_list_init(void) { xcmd_cmd_register(cmds, sizeof(cmds)/sizeof(xcmd_t)); } diff --git a/extensions/ex_list/ex_list.h b/extensions/ex_list/ex_list.h new file mode 100644 index 0000000..9e01dca --- /dev/null +++ b/extensions/ex_list/ex_list.h @@ -0,0 +1,14 @@ +#ifndef EX_LIST_H +#define EX_LIST_H + +#ifdef __cplusplus +extern "C" { +#endif + +void ex_list_init(void); + +#ifdef __cplusplus + } +#endif + +#endif \ No newline at end of file diff --git a/inc/list.h b/extensions/ex_list/list.h similarity index 100% rename from inc/list.h rename to extensions/ex_list/list.h diff --git a/extensions/net_cmds/socket_cmds.c b/extensions/net_cmds/socket_cmds.c index 2991e1d..16b4d64 100644 --- a/extensions/net_cmds/socket_cmds.c +++ b/extensions/net_cmds/socket_cmds.c @@ -18,7 +18,7 @@ static int x_inet_send_to(int fd, char* ip, uint16_t port, void* data, uint16_t return ret; } -static void xcmd_udp_client(int argc ,char**argv) +static int xcmd_udp_client(int argc ,char**argv) { if(argc >= 4) { @@ -26,7 +26,7 @@ static void xcmd_udp_client(int argc ,char**argv) if(udp<0) { xcmd_print("Open socket error!!\r\n"); - return; + return -1; } char* ip = argv[1]; uint16_t port = atoi(argv[2]); @@ -36,7 +36,7 @@ static void xcmd_udp_client(int argc ,char**argv) { xcmd_print("Send msg error\r\n"); close(udp); - return; + return -1; } close(udp); } @@ -44,9 +44,10 @@ static void xcmd_udp_client(int argc ,char**argv) { xcmd_print("Usage: udp_client ip port msg"); } + return 0; } -void xcmd_udp_service(int argc, char** argv) +static int xcmd_udp_service(int argc, char** argv) { struct sockaddr_in addrin; addrin.sin_family = AF_INET; @@ -75,7 +76,7 @@ void xcmd_udp_service(int argc, char** argv) { xcmd_print("Bind error\r\n"); close(udp); - return; + return -1; } struct sockaddr_in client_addr; @@ -90,6 +91,7 @@ void xcmd_udp_service(int argc, char** argv) xcmd_print(rcv_buf); } close(udp); + return 0; } static xcmd_t cmds[] =