00001
00021 #include "sftd.h"
00022 #include "uip.h"
00023
00024 #include "grlib/grlib.h"
00025 #include "../uart/uart.h"
00026 #include "../cli/debug.h"
00027
00028
00030 #define TAM_BUFF_TEMP 3
00031 #define TOTAL_PIXEL 26136
00032 int contImg, n_buff, flag_buff_temp;
00033 int buff_temp[TAM_BUFF_TEMP];
00034 int break_geral;
00035 int old_id;
00036
00038 int flag_sendu;
00039
00043 void sftd_init(void){
00044 uip_listen(HTONS(SFTD_PORT));
00045 }
00046
00053 void sftd_appcall(void){
00054
00055 struct sftd_state *s = (struct sftd_state *)&(uip_conn->appstate.sftd_st);
00056
00057
00058 if(uip_connected()){
00059 DEBUG_SFT("try connection\n");
00060 DEBUG_SFT("connection .. ok");
00061 s->state = ST_INIT;
00062 flag_sendu=0;
00063 }
00064
00065 if(s->state == ST_CLOSE) {
00066 s->state = ST_INIT;
00067 uip_close();
00068 return;
00069 }
00070
00071 if(uip_closed() || uip_aborted() || uip_timedout()) {
00072 DEBUG_SFT("closed... SFT\n");
00073 sft_closed(s);
00074 }
00075
00076 if(uip_newdata()) {
00077 DEBUG_SFT("new data sft\n");
00078 sft_trata(s);
00079 }
00080
00081 if(uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll()) {
00082 DEBUG_SFT("send data sft\n");
00083 sft_senddata(s);
00084 }
00085
00086 }
00087
00093 void sft_closed(struct sftd_state *s){
00094 memb_free(&s, s->buff);
00095 s->state = ST_CLOSE;
00096 }
00097
00103 void sft_senddata(struct sftd_state *s){
00104 uip_send(s->data_send, s->len_send);
00105 }
00106
00117 int sft_trata(struct sftd_state *s){
00118 char estado[50];
00119 char *dataptr;
00120 char cmd[3];
00121 char len[5];
00122 char nro[4];
00123 char id[5];
00124 char *dt='\0';
00125 int i;
00126 struct sft_hdr_uip *sftptr;
00127
00128 sprintf(estado,"Estado: %d\n", s->state);
00129 DEBUG_SFT(estado);
00130
00131 dataptr = (char *)uip_appdata;
00132
00133
00134
00135
00137
00138
00139
00140
00141
00142
00143 if(flag_sendu>0){
00144 DEBUG_SFT("FLAG_SENDU");
00145 s->len_buff = strlen(dataptr);
00146 if(s->len_buff == 0){
00147 flag_sendu = 0;
00148 return 0;
00149 }
00150 sprintf(estado, "s->len_buff == %d", s->len_buff);
00151 DEBUG_SFT(estado);
00152 s->nr_pkt = flag_sendu;
00153 for(i=0; i<s->len_buff;i++) s->buff[i] = dataptr[i];
00154 carrega_imgu(s);
00155 flag_sendu++;
00156 s->state = ST_RECVU;
00157 return 0;
00158 }
00159
00160
00161 if(strlen(dataptr)>5 && flag_sendu==0){
00162
00163
00164 strncpy(cmd, dataptr, 2);
00165 s->cmd = atoi(cmd);
00166 sprintf(estado, "cmd: %d\n", s->cmd);
00167 DEBUG_SFT(estado);
00168 printf(estado);
00169
00170
00171 len[0] = dataptr[2]; len[1] = dataptr[3]; len[2] = dataptr[4]; len[3] = dataptr[5];
00172 s->len_buff = atoi(len);
00173 sprintf(estado, "len: %d\t", s->len_buff);
00174 DEBUG_SFT(estado);
00175 printf(estado);
00176
00177
00178 s->mf = atoi(dataptr[6]);
00179 sprintf(estado, "MF: %d\n", s->mf);
00180 DEBUG_SFT(estado);
00181 printf(estado);
00182
00183
00184 nro[0] = dataptr[7]; nro[1] = dataptr[8];
00185 s->nr_pkt = atoi(nro);
00186 sprintf(estado, "NRO PKT: %d\n", s->nr_pkt);
00187 printf(estado);
00188
00189
00190 id[0] = dataptr[9]; id[1] = dataptr[10]; id[2] = dataptr[11];
00191 s->id = atoi(id);
00192 sprintf(estado, "NRO ID: %03d", s->id);
00193 DEBUG_SFT(estado);
00194 printf(estado);
00195
00196
00197
00198
00199
00200 switch (s->cmd){
00201
00202
00203 case CMD_RECV:
00204 s->state = ST_RECV;
00205 carrega_img(s);
00206 break;
00207
00208 case CMD_STOP:
00209 s->state = ST_STOP;
00210 info_host_stop(s);
00211 break;
00212 case CMD_RECV_X:
00213 s->state = ST_RECV;
00214 carrega_img_old(s);
00215 break;
00216 case CMD_RECVU:
00217 DEBUG_SFT("CMD SENDU.");
00218 s->state = ST_RECVU;
00219 flag_sendu = 1;
00220 carrega_img(s);
00221 default: break;
00222
00223 }
00224
00225
00226 switch(s->state){
00227 case ST_WAIT_RECV:
00228 info_host_wait(s);
00229 break;
00230
00231 case ST_FINISHED_RECV:
00232 uart_puts("\nSFT enviou uma imagem para o display.");
00233 default: break;
00234
00235 }
00236
00237 if((FIO4PIN & (1<<8))==0){
00238
00239 printf("\nfim do envio de imagens.\n");
00240 s->state = ST_STOP;
00241 for(i=0; i < 26136;){
00242 WriteSpiData(0xff); i++;
00243 WriteSpiData(0xff); i++;
00244 WriteSpiData(0xff); i++;
00245 }
00246 info_host_stop(s);
00247 }
00248 }
00249
00250 return 0;
00251 }
00252
00258 void info_host_wait(struct sftd_state *s){
00259 char bf[100];
00260 int len;
00261
00262
00263 sprintf(bf, "%02d", CMD_RECV_ANS);
00264
00265
00266
00267 strncat(bf, "0002", 4);
00268
00269
00270 strncat(bf, "0",1);
00271
00272
00273 strncat(bf, "00",2);
00274
00275
00276 strncat(bf, "%03d", s->id);
00277
00278
00279 strncat(bf, "ok", 2);
00280
00281 len = 14;
00282
00283 DEBUG_SFT("SEND*RECV OK - HOST_WAIT");
00284 uip_send(bf, len);
00285 }
00286
00287
00293 void info_host_stop(struct sftd_state *s){
00294 char bf[100];
00295 int len;
00296
00297
00298 sprintf(bf, "%02d", CMD_STOP);
00299
00300
00301
00302 strncat(bf, "0002", 4);
00303
00304
00305 strncat(bf, "0",1);
00306
00307
00308 strncat(bf, "00",2);
00309
00310
00311 strncat(bf, "%03d", s->id);
00312
00313
00314 strncat(bf, "ok", 2);
00315
00316 len = 14;
00317 uip_send(bf, len);
00318 }
00319
00326 void carrega_img_old(struct sftd_state *s){
00327 int i;
00328 char a[20];
00329
00330 DEBUG_ENTER;
00331
00332 if(s->nr_pkt == 0){
00333
00334 WriteSpiCommand(0x2b);
00335 WriteSpiData(0);
00336 WriteSpiData(131);
00337 WriteSpiCommand(0x2a);
00338 WriteSpiData(0);
00339 WriteSpiData(131);
00340 WriteSpiCommand(0x2c);
00341
00342 }
00343
00344 for(i=0;i < s->len_buff; i++){
00345
00346
00347 WriteSpiData(s->buff[i]);
00348 }
00349
00350
00351 s->state = (s->mf==1) ? ST_WAIT_RECV : ST_FINISHED_RECV;
00352
00353 }
00354
00363 void carrega_img(struct sftd_state *s){
00364 int i;
00365 char a[20];
00366
00367 DEBUG_ENTER;
00368 if(s->nr_pkt == 0){
00369
00370 WriteSpiCommand(0x2b);
00371 WriteSpiData(0);
00372 WriteSpiData(131);
00373 WriteSpiCommand(0x2a);
00374 WriteSpiData(0);
00375 WriteSpiData(131);
00376 WriteSpiCommand(0x2c);
00377
00378 contImg = 0;
00379 for(i=0;i<TAM_BUFF_TEMP;i++) buff_temp[i]=0;
00380 flag_buff_temp = 0;
00381 n_buff = 0;
00382 break_geral = 0;
00383 old_id = s->id;
00384 printf("Novo id: %d - Antigo ID %d\n", s->id, old_id);
00385 }
00386
00387 printf("M");
00388
00389 if(s->id == old_id){
00390 printf(".");
00391 if(contImg < TOTAL_PIXEL && break_geral==0){
00392 for(i=0;i<s->len_buff;){
00393 if(contImg >= TOTAL_PIXEL) break;
00394
00395 if(flag_buff_temp > 0){
00396 while(n_buff--){
00397 if(flag_buff_temp == 3){
00398 WriteSpiData(buff_temp[0]);
00399 WriteSpiData(buff_temp[1]);
00400 WriteSpiData(s->buff[i]);
00401 contImg+=3;
00402 }
00403 else{
00404 WriteSpiData(buff_temp[0]);
00405 WriteSpiData(s->buff[i]);
00406 WriteSpiData(s->buff[i+1]);
00407 contImg+=3;
00408 }
00409 }
00410 i = flag_buff_temp -1;
00411 if(flag_buff_temp!=1) flag_buff_temp = 0;
00412 }
00413
00414
00415 if(flag_buff_temp ==1) flag_buff_temp = 0;
00416 else n_buff = s->buff[i] & 0xff;
00417 if(n_buff == 0xfa) { break_geral = 1; break; }
00418 while(n_buff--){
00419 WriteSpiData(s->buff[i+1]);
00420 WriteSpiData(s->buff[i+2]);
00421 WriteSpiData(s->buff[i+3]);
00422 contImg+=3;
00423 }
00424 i +=4;
00425
00426 if(i > s->len_buff){
00427 if(i == s->len_buff + 4) break;
00428 flag_buff_temp = i - s->len_buff;
00429 n_buff = s->buff[i - 3] & 0xff;
00430 if(n_buff == 0xfa){ break_geral = 1; break; }
00431 if(flag_buff_temp == 3){
00432 buff_temp[0] = s->buff[i-2];
00433 buff_temp[1] = s->buff[i-1];
00434 }
00435 else buff_temp[0] = s->buff[i-1];
00436 }
00437 }
00438
00439 }
00440 }
00441
00442
00443 s->state = (s->mf==1) ? ST_WAIT_RECV : ST_FINISHED_RECV;
00444
00445
00446 if(break_geral == 1 && s->cmd == CMD_RECVU) flag_sendu = 0;
00447
00448 }
00449
00450
00451
00460 void carrega_imgu(struct sftd_state *s){
00461 int i;
00462 char a[20];
00463
00464 DEBUG_ENTER;
00465 DEBUG_SFT(s->buff);
00466
00467 for(i=0;i<s->len_buff;){
00468 if(contImg >= TOTAL_PIXEL) break;
00469
00470 n_buff = s->buff[i] & 0xff;
00471 if(n_buff == 0xfa) { break_geral = 1; break; }
00472 while(n_buff--){
00473 WriteSpiData(s->buff[i+1]);
00474 WriteSpiData(s->buff[i+2]);
00475 WriteSpiData(s->buff[i+3]);
00476 contImg+=3;
00477 }
00478 i +=4;
00479 }
00480
00481
00482 s->state = (s->mf==1) ? ST_WAIT_RECV : ST_FINISHED_RECV;
00483
00484
00485 if(break_geral == 1) flag_sendu = 0;
00486
00487 }
00488
00489