📄 setip.c
字号:
#include "socket_base.h"#define SERVER_IP "1.1.1.1"#define CLIENT_IP "1.1.1.2"char *next_cmd = "reboot";int main(int argc, char *argv[]){ //open network lclient COMMANDPAR par1; HTRANSINTERFACE cmdFile = NULL; int result; int ret = 0; char cmdline[256]; par1.opMode=SOCKET_OP; par1.socketAddr = malloc(30); par1.socketAddr = "192.168.1.100:10000"; par1.endPoint = CLIENT; if (par1.opMode == FILE_OP){ }else if (par1.opMode == SOCKET_OP){ cmdFile = OpenTransInterface(&par1); if(cmdFile == NULL){ fprintf(stderr, "Can not connect the server socket!\n"); return -1; } } printf("send commad:%s\n",next_cmd); result = SendPackage(next_cmd, 6, cmdFile); if(result != 6){ printf("send problem\n"); ret = -1; goto out1; } result = ReadPackage(cmdline, 4, cmdFile);//get the cmd if(result != 4){ printf("read problem\n"); ret = -1; goto out1; } printf("readpackage: %s\n", cmdline); if (strncmp(cmdline, "Done",4) == 0) { printf("reboot success\n"); } //finish reboot platform COMMANDPAR par; HTRANSINTERFACE handle; int last_result; int i; char rbuf[4096], rbuf_save[256]; int *log_fd; char *p_buf; char *mac_buf; char *wbuf = malloc(256); char *macaddr = malloc(20); memset(macaddr, 0,20); mac_buf = macaddr; memset(rbuf, 0, 4096); memset(rbuf_save, 0, 256); par.opMode = COMM_OP; par.commAddr = malloc(30); par.commAddr = "/dev/ttyS0"; par.baudrate = malloc(15); par.baudrate = "B38400"; par.endPoint = SERVER; handle = OpenTransInterface(&par); if(handle == NULL){ printf("Open Transfer interface failed\n"); return -1; } sleep(5); printf("----Test platform can enter blob control interface----\n"); /* wait to enter blob command control interface*/ for (i=0; i<6; i++) { wbuf = "\n"; result = SendPackage(wbuf, 1, handle); if(result != 1){ printf("Send problem\n"); ret = -1; goto out; } sleep(1); } printf("echo anykey successed\n"); result = ReadPackage(rbuf, 1024, handle, 1, 1); printf("read %d bytes\n", result); printf("read: %s\n", rbuf); p_buf = rbuf; for (i=0; i<result; i++) { if (strncmp(p_buf, "blob>",5) == 0) { printf("enter blob command control\n"); break; } p_buf++; if(i >= result-1) { printf("can not enter blob command control\n"); ret = -1; goto out; } } printf("----set ethernet server ip and client ip----\n"); /* test download zImage to nandflash*//* p_buf = wbuf; p_buf = "setmac "; p_buf += 7; p_buf = MAC_ADDR; p_buf += 17; p_buf = "\n";*/ wbuf = "setip server 1.1.1.1\n"; result = SendPackage(wbuf, 21, handle); if(result != 21){ printf("Send problem\n"); ret = -1; goto out; } sleep(3); wbuf = "setip client 1.1.1.2\n"; result = SendPackage(wbuf, 21, handle); if(result != 21){ printf("Send problem\n"); ret = -1; goto out; } sleep(3); wbuf = "setip\n"; result = SendPackage(wbuf, 6, handle); if(result != 6){ printf("Send problem\n"); ret = -1; goto out; } sleep(3); result = ReadPackage(rbuf, 4096, handle, 1, 10); printf("read %d bytes\n", result); printf("read: %s\n", rbuf); p_buf = rbuf; for (i=0; i<result; i++) { if (strncmp(p_buf, "Our server IP : ",16) == 0) { int j; printf("set server ip\n"); p_buf += 16; for (j=0; j < 7; j++) { *mac_buf = *p_buf; mac_buf++; p_buf++; } if(strncmp(macaddr,SERVER_IP,7) == 0) { printf("set server ip via blob\n"); } else { printf("Fail to set server ip via blob\n"); goto out; } break; } p_buf++; if(i == result-1) { printf("failed to set mac\n"); ret = -1; goto out; } } mac_buf = macaddr; for (; i<result; i++) { if (strncmp(p_buf, "Our client IP : ",16) == 0) { int j; printf("set client ip\n"); p_buf += 16; for (j=0; j < 7; j++) { *mac_buf = *p_buf; mac_buf++; p_buf++; } if(strncmp(macaddr,CLIENT_IP,7) == 0) { printf("set client ip via blob\n"); } else { printf("Fail to set server ip via blob\n"); goto out; } break; } p_buf++; if(i == result-1) { printf("failed to set mac\n"); ret = -1; goto out; } } //config server ip on pc system("ifconfig eth0 1.1.1.1"); system("ifconfig eth0 >> log"); log_fd = open("log", O_RDONLY); if(log_fd < 0) { print("open log file fialed\n"); goto out; } int len; len = read(log_fd,(void*)rbuf, 4096); p_buf = rbuf; close(log_fd); if(len<0) { printf("fail to read server ip config log.\n"); goto out; } if(len=0) { goto out; } for (i=0; i<len; i++) { printf("read server ip config log.\n"); if (strncmp(p_buf, "inet addr:",10) == 0) { int j; printf("set server ip\n"); p_buf += 10; if(strncmp(p_buf,SERVER_IP, 7) == 0) { printf("set mac via blob successfully\n"); break; } else { printf("set mac via blob failed. get hw addr is not equal to the mac set.\n"); goto out; } } p_buf++; if(i == result-1) { printf("failed to set mac\n"); ret = -1; goto out; } } printf("----download zImage------\n"); wbuf = "tftp zImage\n"; result = SendPackage(wbuf, 12, handle); if(result != 12){ printf("Send problem\n"); ret = -1; goto out; } sleep(10); result = ReadPackage(rbuf, 2056, handle, 1, 5); printf("read %d bytes\n", result); printf("read: %s\n", rbuf); p_buf = rbuf; for (i=0; i<result; i++) { if (strncmp(p_buf, "tftp_cmd: file 'zImage' loaded",30) == 0) { printf("download zImage to sram though ethernet successfully\n"); printf("set ethernet ip via blob succuss\n"); break; } p_buf++; if (i >= result - 1) { printf("failed to download zImage to sram through thernet\n"); printf("set ethernet ip via blob failed\n"); ret = -1; goto out; } } printf("------------------------------------------------------------------\n"); printf("------------------------------------------------------------------\n"); printf("------------------------------------------------------------------\n"); printf("------------------------------------------------------------------\n"); out: if(handle != NULL){ CloseTransInterface(handle); } out1: if(cmdFile != NULL) CloseTransInterface(cmdFile); system("ifconfig eth0 192.168.1.100"); return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -