Commit 97cc6953ec86efb667bf8c788dcdc8864265e3e0
0 parents
Exists in
master
first commit
Showing
2 changed files
with
617 additions
and
0 deletions
Show diff stats
index.php
| File was created | 1 | <?php | |
| 2 | date_default_timezone_set("Asia/Taipei"); | ||
| 3 | $date = date_create(); | ||
| 4 | $time = date_format($date, 'Y-m-d H:i:s'); | ||
| 5 | |||
| 6 | $link = mysqli_connect("10.10.10.237", "root", "SANlien<03051501>", "practice"); | ||
| 7 | |||
| 8 | if (mysqli_connect_errno()) { | ||
| 9 | //printf("Connect failed: %s\n", mysqli_connect_error()); | ||
| 10 | exit(); | ||
| 11 | } | ||
| 12 | |||
| 13 | //printf("#connect successfully<br><br>\n"); | ||
| 14 | |||
| 15 | if(isset($_GET["inst"])) | ||
| 16 | { | ||
| 17 | $serial = "\"".$_GET["inst"]."\""; | ||
| 18 | if( !(isset($_GET["code"]))) | ||
| 19 | { | ||
| 20 | //echo "not API<br>"; | ||
| 21 | $query = "SELECT OpCode FROM status WHERE SerialNo = $serial"; | ||
| 22 | //echo "#query <br> => <br> $query<br><br>"; | ||
| 23 | |||
| 24 | if ($result = mysqli_query($link, $query)) | ||
| 25 | { | ||
| 26 | if ($row = mysqli_fetch_row($result)) | ||
| 27 | { | ||
| 28 | $service_code = "\"$row[0]\""; | ||
| 29 | echo "service_code:$row[0]"; | ||
| 30 | |||
| 31 | //echo "<br><br>insert service_code to log<br>"; | ||
| 32 | $query = "INSERT INTO log(Time,SerialNo,OpCode) VALUES (\"$time\", $serial, $service_code)"; | ||
| 33 | //echo "#query <br> => <br> $query<br><br>"; | ||
| 34 | mysqli_query($link, $query); | ||
| 35 | //printf("Insert New Record sucessfully<br>"); | ||
| 36 | } | ||
| 37 | else | ||
| 38 | { | ||
| 39 | //echo "<br><br>insert wrong service_code to log<br>"; | ||
| 40 | $query = "INSERT INTO log(Time,SerialNo,OpCode,msg) VALUES (\"$time\", $serial, \"\",\"wrong serial\")"; | ||
| 41 | mysqli_query($link, $query); | ||
| 42 | |||
| 43 | //printf("Insert New wrong record sucessfully<br><br>"); | ||
| 44 | } | ||
| 45 | mysqli_free_result($result); | ||
| 46 | mysqli_free_result($row); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | else | ||
| 50 | { | ||
| 51 | //echo "is API<br>"; | ||
| 52 | $service_code = "\"".$_GET["code"]."\""; | ||
| 53 | |||
| 54 | if($service_code == "\"666\"") | ||
| 55 | { | ||
| 56 | //echo "<br>insert API's service code to log<br>"; | ||
| 57 | $query = "INSERT INTO log(Time,SerialNo,OpCode) VALUES (\"$time\", $serial, $service_code)"; | ||
| 58 | mysqli_query($link, $query); | ||
| 59 | //printf("Insert into log sucessfully<br><br>"); | ||
| 60 | |||
| 61 | //printf("update status to 666<br>"); | ||
| 62 | $query="UPDATE status SET OpCode = $service_code WHERE SerialNo = $serial"; | ||
| 63 | mysqli_query($link, $query); | ||
| 64 | //printf("update successfully.<br>Affected rows (UPDATE): %d<br>", mysqli_affected_rows($link)); | ||
| 65 | } | ||
| 66 | else if($service_code == "\"400\"") | ||
| 67 | { | ||
| 68 | if(isset($_GET["msg"])) | ||
| 69 | { | ||
| 70 | $msg="\"".$_GET["msg"]."\""; | ||
| 71 | $query = "INSERT INTO log(Time,SerialNo,OpCode,msg) VALUES (\"$time\", $serial, $service_code,$msg)"; | ||
| 72 | mysqli_query($link, $query); | ||
| 73 | //printf("Insert 400 sucessfully<br><br>"); | ||
| 74 | |||
| 75 | //printf("update status to 000<br>"); | ||
| 76 | $query="UPDATE status SET OpCode = \"000\" WHERE SerialNo = $serial"; | ||
| 77 | mysqli_query($link, $query); | ||
| 78 | //printf("update successfully.<br>Affected rows (UPDATE): %d<br>", mysqli_affected_rows($link)); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | else if($service_code == "\"777\"") | ||
| 82 | { | ||
| 83 | //echo "<br>insert API's service code to log<br>"; | ||
| 84 | $query = "INSERT INTO log(Time,SerialNo,OpCode) VALUES (\"$time\", $serial, $service_code)"; | ||
| 85 | mysqli_query($link, $query); | ||
| 86 | //printf("Insert into log sucessfully<br><br>"); | ||
| 87 | |||
| 88 | //printf("update status to 000<br>"); | ||
| 89 | $query="UPDATE status SET OpCode = \"000\" WHERE SerialNo = $serial"; | ||
| 90 | mysqli_query($link, $query); | ||
| 91 | //printf("update successfully.<br>Affected rows (UPDATE): %d<br>", mysqli_affected_rows($link)); | ||
| 92 | } | ||
| 93 | |||
| 94 | } | ||
| 95 | } | ||
| 96 | else | ||
| 97 | { | ||
| 98 | //printf("no serial"); | ||
| 99 | } | ||
| 100 | |||
| 101 | mysqli_close($link); | ||
| 102 | ?> |
serviceApi.c
| File was created | 1 | #include <stdio.h> | |
| 2 | #include <string.h> //memcmp 要 include 的 header | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <unistd.h> //getpid() | ||
| 5 | #include <errno.h> | ||
| 6 | #include <netdb.h> | ||
| 7 | #include <sys/types.h> | ||
| 8 | #include <netinet/in.h> //sockaddr_in | ||
| 9 | #include <sys/socket.h> //socket() | ||
| 10 | |||
| 11 | #define PORT 80 // Client 所要連線的 port | ||
| 12 | #define MAXDATASIZE 500 //最大可收的 bytes 大小 | ||
| 13 | |||
| 14 | char ip[50]; | ||
| 15 | char port[50]; | ||
| 16 | char user[50]; | ||
| 17 | char password[50]; | ||
| 18 | char folder[50]; | ||
| 19 | char command[500]; | ||
| 20 | char serial[50]; | ||
| 21 | int checkT = 0, checkR = 0; //0為complete; -1回傳開檔失敗; -2回傳 傳檔/收檔失敗 的錯誤 Message | ||
| 22 | int errorCountR = 0, errorCountT = 0; | ||
| 23 | |||
| 24 | void readConfig() //用來存取 ip, port, password 等等變數的 function | ||
| 25 | { | ||
| 26 | FILE *fp; | ||
| 27 | fp = fopen("service.cfg", "r"); //開檔,讀取service.cfg的檔案 | ||
| 28 | if(fp == NULL) | ||
| 29 | printf("open failure\n"); | ||
| 30 | else | ||
| 31 | { | ||
| 32 | while(1) | ||
| 33 | { | ||
| 34 | fgets(command, 500, fp); //從fp中一次讀取一行指令(500字元)到common中 | ||
| 35 | |||
| 36 | if(feof(fp)) //如讀到底則 Break | ||
| 37 | { | ||
| 38 | break; | ||
| 39 | } | ||
| 40 | else | ||
| 41 | { | ||
| 42 | char check[500]; | ||
| 43 | sscanf(command, "%s %*s", check); //從command中讀取前面的字元存到 check 中,後面的字元省略 | ||
| 44 | |||
| 45 | if(memcmp(check, "IP", 2) == 0) //如 (check == "IP") 則從 command 中讀取後面的字元存到 ip 變數中,前面的字元省略 | ||
| 46 | { | ||
| 47 | sscanf(command, "%*s %s", ip); | ||
| 48 | } | ||
| 49 | else if(memcmp(check, "PORT", 4) == 0) | ||
| 50 | { | ||
| 51 | sscanf(command, "%*s %s", port); | ||
| 52 | } | ||
| 53 | else if(memcmp(check, "PASSWORD", 8) == 0) | ||
| 54 | { | ||
| 55 | sscanf(command, "%*s %s", password); | ||
| 56 | } | ||
| 57 | else if(memcmp(check, "USER", 4) == 0) | ||
| 58 | { | ||
| 59 | sscanf(command, "%*s %s", user); | ||
| 60 | } | ||
| 61 | else if(memcmp(check, "FOLDER", 6) == 0) | ||
| 62 | { | ||
| 63 | sscanf(command, "%*s %s", folder); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | fclose(fp); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | /*void readSerialNo() //用來讀取序號用的 function | ||
| 72 | { | ||
| 73 | FILE *fp2; | ||
| 74 | fp2 = fopen("/proc/cpuinfo", "r"); | ||
| 75 | |||
| 76 | if(fp2 == NULL) | ||
| 77 | { | ||
| 78 | printf("open failure\n"); | ||
| 79 | } | ||
| 80 | else | ||
| 81 | { | ||
| 82 | while(1) | ||
| 83 | { | ||
| 84 | fgets(command, 500, fp2); | ||
| 85 | if(feof(fp2)) | ||
| 86 | { | ||
| 87 | break; | ||
| 88 | } | ||
| 89 | else | ||
| 90 | { | ||
| 91 | char check[500]; | ||
| 92 | sscanf(command, "%s %*[]", check); | ||
| 93 | if(memcmp(check, "Serial", 6) == 0) | ||
| 94 | { | ||
| 95 | sscanf(command, "%*[^:] %*s %s", serial); //從command中,前面到:為止都省略掉,再省略一個空格,讀取最後一個字元存到serial | ||
| 96 | //printf("%s\n", serial); | ||
| 97 | sprintf(serial, "%s", serial); //把serial存成指定格式(%s) | ||
| 98 | //printf("%d\n", strlen(serial)); //測試serial有幾個字元 | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | fclose(fp2); //記得關檔 | ||
| 103 | } | ||
| 104 | }*/ | ||
| 105 | |||
| 106 | void FileRevOk() //查看是否成功下載檔案,如成功跳出"file recv successfully" | ||
| 107 | { //如失敗跳出 "file not recv" | ||
| 108 | char buf[500]; | ||
| 109 | FILE *pp; | ||
| 110 | |||
| 111 | printf("Befor receive, 以上正常\n"); | ||
| 112 | |||
| 113 | if((pp = popen(command, "r")) == NULL) | ||
| 114 | { | ||
| 115 | printf("popen() error!\n"); | ||
| 116 | checkR = -1; | ||
| 117 | return; //回傳 not found | ||
| 118 | } | ||
| 119 | else | ||
| 120 | { | ||
| 121 | while(fgets(buf, sizeof(buf), pp)) | ||
| 122 | { | ||
| 123 | //printf("%s", buf); | ||
| 124 | if(memcmp(buf, "file recv ok", 12) == 0) | ||
| 125 | { | ||
| 126 | printf("file recv successfully\n"); | ||
| 127 | pclose(pp); | ||
| 128 | checkR = 0; | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | pclose(pp); | ||
| 134 | printf("file not recv\n"); | ||
| 135 | checkR = -2; | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | void FileTransferOk() | ||
| 141 | { | ||
| 142 | char buf[500]; | ||
| 143 | FILE *pp; | ||
| 144 | |||
| 145 | puts("Befor transfer, 以上正常\n"); | ||
| 146 | |||
| 147 | if((pp = popen(command, "r")) == NULL) | ||
| 148 | { | ||
| 149 | printf("popen() error!\n"); | ||
| 150 | checkT = -1; | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | else | ||
| 154 | { | ||
| 155 | while(fgets(buf, sizeof(buf), pp)) | ||
| 156 | { | ||
| 157 | if(memcmp(buf, "Transfer OK.", 12) == 0) | ||
| 158 | { | ||
| 159 | printf("file transfer successfully\n"); | ||
| 160 | pclose(pp); | ||
| 161 | checkT = 0; | ||
| 162 | return; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | pclose(pp); | ||
| 167 | printf("file not transfer\n"); | ||
| 168 | checkT = -2; | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | //////////////////////////////////////////////////////////////////////////////// | ||
| 174 | // // | ||
| 175 | // tcpConnect // | ||
| 176 | // // | ||
| 177 | //////////////////////////////////////////////////////////////////////////////// | ||
| 178 | |||
| 179 | int tcpConnect(int return_code, char msg[50]) | ||
| 180 | { | ||
| 181 | int sockfd; //socket的描述 | ||
| 182 | int i; | ||
| 183 | int numbytes; | ||
| 184 | int service_code; | ||
| 185 | char buf[MAXDATASIZE]; | ||
| 186 | struct hostent *host; | ||
| 187 | struct sockaddr_in info; | ||
| 188 | struct timeval tv; | ||
| 189 | |||
| 190 | sprintf(buf, "GET /index.php?inst=%s&&code=%d&&msg=%s HTTP/1.1\r\n", serial, return_code, msg); | ||
| 191 | i = strlen(buf); | ||
| 192 | |||
| 193 | if(return_code == 400) | ||
| 194 | { | ||
| 195 | //printf("Failure, Error message : %s\n", message); | ||
| 196 | sprintf(&buf[i], "Host: %s\r\nConnection: Failure\r\n\r\n\r\n\0\0\0\0\0\0\0", ip); | ||
| 197 | } | ||
| 198 | else if(return_code == 777) | ||
| 199 | { | ||
| 200 | //printf("complete\n"); | ||
| 201 | sprintf(&buf[i], "Host: %s\r\nConnection: Complete\r\n\r\n\r\n\0\0\0\0\0\0\0", ip); | ||
| 202 | } | ||
| 203 | else if(return_code == 666) | ||
| 204 | { | ||
| 205 | //printf("under processing\n"); | ||
| 206 | sprintf(&buf[i], "Host: %s\r\nConnection: Keep-Alive\r\n\r\n\r\n\0\0\0\0\0\0\0", ip); | ||
| 207 | } | ||
| 208 | else | ||
| 209 | { | ||
| 210 | puts("your return_code is wrong.\n"); | ||
| 211 | } | ||
| 212 | |||
| 213 | sockfd = socket(AF_INET, SOCK_STREAM, 0); //socket的描述浮 | ||
| 214 | if(sockfd == -1) | ||
| 215 | { | ||
| 216 | perror("socket"); | ||
| 217 | exit(1); | ||
| 218 | } | ||
| 219 | |||
| 220 | |||
| 221 | bzero(&info, sizeof(info)); //初始化,將struct涵蓋的bits設為0 | ||
| 222 | info.sin_family = AF_INET; //使用IPv4協定的地址 | ||
| 223 | info.sin_addr.s_addr = inet_addr(ip); //IP address, inet_addr()可將字串IP變成 binary's IP | ||
| 224 | //info.sin_addr.s_addr = inet_addr("10.0.0.49");//先指向自己的 IP 練習, 可以把防火牆打開測試connected的timeout | ||
| 225 | info.sin_port = htons(PORT); //埠號,Host TO Network Short integer的縮寫,它將本機端的字節序(endian)轉換成了網路端的字節序 | ||
| 226 | //printf("test %x\n", info.sin_addr.s_addr); | ||
| 227 | |||
| 228 | //socket的連線 | ||
| 229 | //沒有timeout版本的connect | ||
| 230 | if((connect(sockfd, (struct sockaddr *)&info, sizeof(info))) == -1) | ||
| 231 | { | ||
| 232 | perror("connect"); | ||
| 233 | exit(1); | ||
| 234 | } | ||
| 235 | printf("connect successfully\n"); | ||
| 236 | //沒有timeout版本的connect | ||
| 237 | |||
| 238 | |||
| 239 | //有timeout 版本的connect | ||
| 240 | /*int res = connect(sockfd, (struct sockaddr *)&info, sizeof(info)); | ||
| 241 | if(res < 0) | ||
| 242 | { | ||
| 243 | if(errno == EINPROGRESS) //connect 還在進行中 | ||
| 244 | { | ||
| 245 | perror("EINPROGRESS in connect"); | ||
| 246 | |||
| 247 | fd_set myset; | ||
| 248 | do | ||
| 249 | { | ||
| 250 | tv.tv_sec = 10; | ||
| 251 | tv.tv_usec = 0; | ||
| 252 | FD_ZERO(&myset); | ||
| 253 | FD_SET(sockfd, &myset); | ||
| 254 | res = select(sockfd+1, NULL, &myset, NULL, &tv); | ||
| 255 | if(res < 0 && errno != EINTR) | ||
| 256 | { | ||
| 257 | perror("connect error"); | ||
| 258 | exit(1); | ||
| 259 | } | ||
| 260 | else if(res >0) | ||
| 261 | { | ||
| 262 | socklen_t len; | ||
| 263 | int valopt; | ||
| 264 | |||
| 265 | len = sizeof(int); | ||
| 266 | if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &len) < 0) //getsockopt can determine if the socket is connected (=0, connected) | ||
| 267 | { | ||
| 268 | perror("Error in getsockopt()"); | ||
| 269 | exit(1); | ||
| 270 | } | ||
| 271 | |||
| 272 | if(valopt) | ||
| 273 | { | ||
| 274 | perror("Error in delay"); | ||
| 275 | exit(1); | ||
| 276 | } | ||
| 277 | break; | ||
| 278 | } | ||
| 279 | else | ||
| 280 | { | ||
| 281 | perror("Timeout"); | ||
| 282 | exit(1); | ||
| 283 | } | ||
| 284 | }while(1); | ||
| 285 | } | ||
| 286 | else | ||
| 287 | { | ||
| 288 | perror("connect error"); | ||
| 289 | exit(1); | ||
| 290 | } | ||
| 291 | }//有timeout版的connect | ||
| 292 | */ | ||
| 293 | |||
| 294 | //傳送資料(send a message on a socket),如正確 send 會回傳實際送出的 Byte 數 | ||
| 295 | if((send(sockfd, buf, sizeof(buf), 0)) == -1) | ||
| 296 | { | ||
| 297 | perror("send"); | ||
| 298 | exit(1); | ||
| 299 | } | ||
| 300 | //printf("send:\n%s\n", buf); | ||
| 301 | printf("After the send function \n\n"); | ||
| 302 | |||
| 303 | |||
| 304 | //接收資料,如正確 recv 會回傳實際讀到並寫入到 buffer 的 Byte 數 | ||
| 305 | numbytes = recv(sockfd, buf, sizeof(buf), 0); | ||
| 306 | //printf("numbytes = %d\n", numbytes); | ||
| 307 | if(numbytes == -1) | ||
| 308 | { | ||
| 309 | perror("recv"); | ||
| 310 | exit(1); | ||
| 311 | } | ||
| 312 | |||
| 313 | close(sockfd); | ||
| 314 | |||
| 315 | buf[numbytes] = '\0'; | ||
| 316 | printf("Received in pid = %d, text = %s \n", getpid(), buf); | ||
| 317 | |||
| 318 | return 0; | ||
| 319 | } | ||
| 320 | |||
| 321 | int updateFirmware() | ||
| 322 | { | ||
| 323 | int sockfd; //socket的描述 | ||
| 324 | int i; | ||
| 325 | int numbytes; | ||
| 326 | int service_code; | ||
| 327 | char buf[12] = {1,2,0,0,0,6,1,6,0,0x70,1,0x81}; | ||
| 328 | struct hostent *host; | ||
| 329 | struct sockaddr_in info; | ||
| 330 | |||
| 331 | i = strlen(buf); | ||
| 332 | |||
| 333 | sockfd = socket(AF_INET, SOCK_STREAM, 0); //socket的描述浮 | ||
| 334 | if(sockfd == -1) | ||
| 335 | { | ||
| 336 | perror("socket"); | ||
| 337 | exit(1); | ||
| 338 | } | ||
| 339 | |||
| 340 | bzero(&info, sizeof(info)); //初始化,將struct涵蓋的bits設為0 | ||
| 341 | info.sin_family = AF_INET; //使用IPv4協定的地址 | ||
| 342 | //info.sin_addr.s_addr = inet_addr(ip); //IP address, inet_addr()可將字串IP變成 binary's IP | ||
| 343 | info.sin_addr.s_addr = inet_addr("127.0.0.1");//先指向自己的 IP 練習, 可以把防火牆打開測試connected的timeout | ||
| 344 | info.sin_port = htons(502); //埠號,Host TO Network Short integer的縮寫,它將本機端的字節序(endian)轉換成了網路端的字節序 | ||
| 345 | |||
| 346 | //socket的連線 | ||
| 347 | //沒有timeout版本的connect | ||
| 348 | if((connect(sockfd, (struct sockaddr *)&info, sizeof(info))) == -1) | ||
| 349 | { | ||
| 350 | perror("connect"); | ||
| 351 | exit(1); | ||
| 352 | } | ||
| 353 | |||
| 354 | //傳送資料(send a message on a socket),如正確 send 會回傳實際送出的 Byte 數 | ||
| 355 | if((send(sockfd, buf, sizeof(buf), 0)) == -1) | ||
| 356 | { | ||
| 357 | perror("send"); | ||
| 358 | exit(1); | ||
| 359 | } | ||
| 360 | printf("After the send function \n\n"); | ||
| 361 | |||
| 362 | //接收資料,如正確 recv 會回傳實際讀到並寫入到 buffer 的 Byte 數 | ||
| 363 | numbytes = recv(sockfd, buf, sizeof(buf), 0); | ||
| 364 | if(numbytes == -1) | ||
| 365 | { | ||
| 366 | perror("recv"); | ||
| 367 | exit(1); | ||
| 368 | } | ||
| 369 | |||
| 370 | close(sockfd); | ||
| 371 | |||
| 372 | buf[numbytes] = '\0'; | ||
| 373 | |||
| 374 | return 0; | ||
| 375 | |||
| 376 | } | ||
| 377 | ///////////////////// main /////////////////////////////////////////// | ||
| 378 | |||
| 379 | int main(int argc, char *argv[]) | ||
| 380 | { | ||
| 381 | int return_code; | ||
| 382 | int version; | ||
| 383 | int date; | ||
| 384 | |||
| 385 | return_code = atoi(argv[4]); | ||
| 386 | version = atoi(argv[6]); | ||
| 387 | date = atoi(argv[8]); | ||
| 388 | sprintf(serial, "%s", argv[9]); | ||
| 389 | |||
| 390 | readConfig(); //呼叫存取ip,password等等變數的function | ||
| 391 | if(return_code == 0) //如return_code為0,則輸出0且結束 | ||
| 392 | printf("0\n"); //do nothing | ||
| 393 | else if(return_code == 400) | ||
| 394 | puts("工作失敗\n"); | ||
| 395 | else if(return_code == 666) | ||
| 396 | puts("上一個程式還在執行中\n"); | ||
| 397 | else if(return_code == 777) | ||
| 398 | puts("已完成\n"); | ||
| 399 | else if(return_code == 10|| return_code == 110 || return_code == 20 || return_code == 120 || | ||
| 400 | return_code == 1 || return_code == 101 || return_code == 200 || return_code == 300) | ||
| 401 | { | ||
| 402 | tcpConnect(666, ""); //呼叫tcp function會回處理中(參數666) | ||
| 403 | errorCountPlus: //如errorCountR or errorCountT <3會用goto回到這裡再執行一次 | ||
| 404 | switch(return_code) | ||
| 405 | { | ||
| 406 | case 10: //如return_code為 010 則上傳 vAlert8.cfg | ||
| 407 | sprintf(command, "./ftpUpload.exe %s %s %s %s %s vAlert8.cfg STOR", ip, port, user, password, folder); | ||
| 408 | //printf("test command %s\n", command); //sprintf 為,把這些格式的值寫到command中 | ||
| 409 | system(command); //叫 system 去執行command(自己設的變數) | ||
| 410 | FileTransferOk(); | ||
| 411 | break; | ||
| 412 | case 110: //如return_code為 110 則下載 vAlert8.cfg | ||
| 413 | sprintf(command, "./ftpClient.exe %s %s %s %s vAlert8.cfg reset\n", ip, port, user, password); | ||
| 414 | //printf("%s\n", command); | ||
| 415 | system(command); | ||
| 416 | FileRevOk(); | ||
| 417 | break; | ||
| 418 | case 20: //如return_code為 020 則上傳 vAlert8Common.cfg | ||
| 419 | sprintf(command, "./ftpUpload.exe %s %s %s %s %s vAlert8Common.cfg STOR", ip, port, user, password, folder); | ||
| 420 | //printf("%s\n", command); | ||
| 421 | system(command); | ||
| 422 | FileTransferOk(); | ||
| 423 | break; | ||
| 424 | case 120: //如return_code為 120 則下載 vAlert8Common.cfg | ||
| 425 | sprintf(command, "./ftpClient.exe %s %s %s %s vAlert8Common.cfg reset", ip, port, user, password); | ||
| 426 | //printf("%s\n",command); | ||
| 427 | system(command); | ||
| 428 | FileRevOk(); | ||
| 429 | break; | ||
| 430 | case 1: //如return_code為 001 則上傳清單上的檔案,需先下載再上傳檔案清單 | ||
| 431 | sprintf(command, "./ftpClient.exe %s %s %s %s uploadFileList_%s reset", ip, port, user, password, serial); | ||
| 432 | system(command); //先下載檔案 | ||
| 433 | FileRevOk(); | ||
| 434 | |||
| 435 | FILE *fp3; | ||
| 436 | fp3 = fopen("uploadFileList.txt","r"); | ||
| 437 | if(fp3 == NULL) | ||
| 438 | { | ||
| 439 | printf("open failure\n"); | ||
| 440 | } | ||
| 441 | else //再上傳檔案清單 | ||
| 442 | { | ||
| 443 | char temp[500]; | ||
| 444 | fgets(temp, 500, fp3); | ||
| 445 | sprintf(command,"./ftpUpload.exe %s %s %s %s %s/%s uploadFileList_%s STOR", ip, port, user, password, folder, temp, serial); | ||
| 446 | //printf("%s\n", command); | ||
| 447 | system(command); | ||
| 448 | FileTransferOk(); | ||
| 449 | } | ||
| 450 | fclose(fp3); | ||
| 451 | break; | ||
| 452 | case 101: //如return_code為 101 則單純下載檔案清單 | ||
| 453 | sprintf(command, "./ftpClients.exe %s %s %s %s uploadFileList_%s reset", ip, port, user, password, serial); | ||
| 454 | //printf("%s\n", command); | ||
| 455 | system(command); | ||
| 456 | FileRevOk(); | ||
| 457 | break; | ||
| 458 | case 200: | ||
| 459 | puts("更新韌體\n"); | ||
| 460 | updateFirmware(); | ||
| 461 | break; | ||
| 462 | case 300: //如return_code為 300 則先下載 run_serial.sh,並執行排程 | ||
| 463 | sprintf(command, "./ftpClient.exe %s %s %s %s run_%s.sh reset", ip, port, user, password, serial); | ||
| 464 | //printf("%s\n", command); | ||
| 465 | system(command); | ||
| 466 | FileRevOk(); | ||
| 467 | |||
| 468 | sprintf(command, "sudo chmod +x run_%s.sh", serial);//改成可執行檔 | ||
| 469 | //printf("first %s\n", command); | ||
| 470 | system(command); | ||
| 471 | |||
| 472 | sprintf(command, "sudo ./run_%s.sh", serial); //執行排程 | ||
| 473 | //printf("second %s\n", command); | ||
| 474 | system(command); | ||
| 475 | break; | ||
| 476 | }//end switch | ||
| 477 | }//end else if | ||
| 478 | else | ||
| 479 | { | ||
| 480 | puts("wrong code\n"); | ||
| 481 | } | ||
| 482 | if(checkR == 0 && checkT == 0) //FileRevOk && FileTransferOk 的結果都收到OK回傳777 | ||
| 483 | { | ||
| 484 | tcpConnect(777, ""); | ||
| 485 | } | ||
| 486 | else //Received 沒收到三次會回傳400 | ||
| 487 | { | ||
| 488 | if(checkR != 0 && errorCountR < 3) | ||
| 489 | { | ||
| 490 | errorCountR++; | ||
| 491 | printf("Received fail %d time\n", errorCountR); | ||
| 492 | checkR = 0; | ||
| 493 | goto errorCountPlus; | ||
| 494 | } | ||
| 495 | else if(checkR == -1) | ||
| 496 | tcpConnect(400, "popen()_error"); | ||
| 497 | else if(checkR == -2) | ||
| 498 | tcpConnect(400, "file_not_recv"); | ||
| 499 | |||
| 500 | |||
| 501 | if(checkT != 0 && errorCountT < 3) //Transfer 沒收到三次會回傳400 | ||
| 502 | { | ||
| 503 | errorCountT++; | ||
| 504 | printf("Transfer fail %d time\n", errorCountT); | ||
| 505 | checkT = 0; | ||
| 506 | goto errorCountPlus; | ||
| 507 | } | ||
| 508 | else if(checkT == -1) | ||
| 509 | tcpConnect(400, "popen()_error"); | ||
| 510 | else if(checkT == -2) | ||
| 511 | tcpConnect(400, "file_not_transfer"); | ||
| 512 | } | ||
| 513 | |||
| 514 | return 0; | ||
| 515 | } |