00001
00017 #include "sftd.h"
00018 #include "uip.h"
00019
00020 #include "grlib/grlib.h"
00021 #include "../uart/uart.h"
00022 #include "../cli/debug.h"
00023
00024
00026 #define TAM_BUFF_TEMP 3
00027 #define TOTAL_PIXEL 26136
00028 int contImg, n_buff, flag_buff_temp;
00029 int buff_temp[TAM_BUFF_TEMP];
00030 int break_geral;
00031
00033 int flag_sendu;
00034
00038 void sftd_init(void){
00039 uip_listen(HTONS(SFTD_PORT));
00040 }
00041
00048 void sftd_appcall(void){
00049
00050 struct sftd_state *s = (struct sftd_state *)&(uip_conn->appstate.sftd_st);
00051
00052
00053 if(uip_connected()){
00054 DEBUG_SFT("try connection\n");
00055 DEBUG_SFT("connection .. ok");
00056 s->state = ST_INIT;
00057 flag_sendu=0;
00058 }
00059
00060 if(s->state == ST_CLOSE) {
00061 s->state = ST_INIT;
00062 uip_close();
00063 return;
00064 }
00065
00066 if(uip_closed() || uip_aborted() || uip_timedout()) {
00067 DEBUG_SFT("closed... SFT\n");
00068 sft_closed(s);
00069 }
00070
00071 if(uip_newdata()) {
00072 DEBUG_SFT("new data sft\n");
00073 sft_trata(s);
00074 }
00075
00076 if(uip_rexmit() || uip_newdata() || uip_acked() || uip_connected() || uip_poll()) {
00077 DEBUG_SFT("send data sft\n");
00078 sft_senddata(s);
00079 }
00080
00081 }
00082
00088 void sft_closed(struct sftd_state *s){
00089 memb_free(&s, s->buff);
00090 s->state = ST_CLOSE;
00091 }
00092
00098 void sft_senddata(struct sftd_state *s){
00099 uip_send(s->data_send, s->len_send);
00100 }
00101
00112 int sft_trata(struct sftd_state *s){
00113 char estado[20];
00114 char *dataptr;
00115 char ax[5];
00116 char ax1[5], id[5];
00117 int i;
00118
00119 sprintf(estado,"Estado: %d\n", s->state);
00120 DEBUG_SFT(estado);
00121
00122 dataptr = (char *)uip_appdata;
00123
00124
00125 if(flag_sendu>0){
00126 DEBUG_SFT("FLAG_SENDU");
00127 s->len_buff = strlen(dataptr);
00128 if (s->len_buff == 0){
00129 flag_sendu = 0;
00130 return 0;
00131 }
00132 s->nr_pkt = flag_sendu++;
00133 strcpy(s->buff,dataptr);
00134 carrega_imgu(s);
00135 s->state = ST_RECVU;
00136 return 0;
00137 }
00138
00139
00140 if(strlen(dataptr)>5 && (flag_sendu==0)){
00141
00142
00143 strncpy(ax, dataptr, 2);
00144 s->cmd = atoi(ax);
00145 sprintf(estado, "\ncmd: %d\t", s->cmd);
00146 DEBUG_SFT(estado);
00147
00148
00149
00150 ax[0] = dataptr[2]; ax[1] = dataptr[3]; ax[2] = dataptr[4]; ax[3] = dataptr[5];
00151 s->len_buff = atoi(ax);
00152 sprintf(estado, "len: %d\t", s->len_buff);
00153 DEBUG_SFT(estado);
00154
00155
00156
00157 s->mf = atoi(dataptr[6]);
00158 sprintf(estado, "MF: %d\t", s->mf);
00159 DEBUG_SFT(estado);
00160
00161
00162
00163 ax1[0] = dataptr[7]; ax1[1] = dataptr[8];
00164 s->nr_pkt = atoi(ax1);
00165 sprintf(estado, "NRO PKT: %d\t", s->nr_pkt);
00166 DEBUG_SFT(estado);
00167
00168 if(s->nr_pkt==0) printf("");
00169
00170
00171 for(i=0; i<s->len_buff;i++) s->buff[i] = dataptr[i + 9];
00172
00173 switch (s->cmd){
00174
00175
00176 case CMD_RECV:
00177 s->state = ST_RECV;
00178 carrega_img(s);
00179 break;
00180
00181 case CMD_STOP:
00182 s->state = ST_STOP;
00183 info_host_stop(s);
00184 break;
00185 case CMD_RECV_X:
00186 s->state = ST_RECV;
00187 carrega_img_old(s);
00188 break;
00189 case CMD_RECVU:
00190 s->state = ST_RECVU;
00191 flag_sendu = 1;
00192 carrega_img(s);
00193 default:
00194 info_host_error(s);
00195 break;
00196
00197 }
00198
00199
00200 switch(s->state){
00201 case ST_WAIT_RECV:
00202 info_host_wait(s);
00203 break;
00204
00205 case ST_FINISHED_RECV:
00206 uart_puts("\nSFT enviou uma imagem para o display.");
00207 default: break;
00208
00209 }
00210
00211 if((FIO4PIN & (1<<8))==0){
00212
00213 printf("\nfim do envio de imagens.\n");
00214 s->state = ST_STOP;
00215 for(i=0; i < 26136;){
00216 WriteSpiData(0xff); i++;
00217 WriteSpiData(0xff); i++;
00218 WriteSpiData(0xff); i++;
00219 }
00220 info_host_stop(s);
00221 }
00222 }
00223
00224 return 0;
00225 }
00226
00232 void info_host_wait(struct sftd_state *s){
00233 char bf[100];
00234 int len;
00235
00236
00237 sprintf(bf, "%02d", CMD_RECV_ANS);
00238
00239
00240
00241 strncat(bf, "0002", 4);
00242
00243
00244 strncat(bf, "0",1);
00245
00246
00247 strncat(bf, "00",2);
00248
00249
00250 strncat(bf, "ok", 2);
00251
00252
00253
00254
00255 len = 11;
00256 uip_send(bf, len);
00257 }
00258
00264 void info_host_error(struct sftd_state *s){
00265 char bf[100];
00266 int len;
00267
00268
00269 sprintf(bf, "%02d", CMD_ERROR);
00270
00271
00272
00273 strncat(bf, "0002", 4);
00274
00275
00276 strncat(bf, "0",1);
00277
00278
00279 strncat(bf, "00",2);
00280
00281
00282 strncat(bf, "xx", 2);
00283
00284
00285
00286
00287 len = 11;
00288 uip_send(bf, len);
00289 }
00290
00291
00292
00293
00299 void info_host_stop(struct sftd_state *s){
00300 char bf[100];
00301 int len;
00302
00303
00304 sprintf(bf, "%02d", CMD_STOP);
00305
00306
00307
00308 strncat(bf, "0002", 4);
00309
00310
00311 strncat(bf, "0",1);
00312
00313
00314 strncat(bf, "00",2);
00315
00316
00317 strncat(bf, "ok", 2);
00318
00319
00320
00321
00322 len = 11;
00323 uip_send(bf, len);
00324 }
00325
00332 void carrega_img_old(struct sftd_state *s){
00333 int i;
00334 char a[20];
00335
00336 DEBUG_ENTER;
00337
00338 if(s->nr_pkt == 0){
00339
00340 WriteSpiCommand(0x2b);
00341 WriteSpiData(0);
00342 WriteSpiData(131);
00343 WriteSpiCommand(0x2a);
00344 WriteSpiData(0);
00345 WriteSpiData(131);
00346 WriteSpiCommand(0x2c);
00347
00348 }
00349
00350 for(i=0;i < s->len_buff; i++){
00351
00352
00353 WriteSpiData(s->buff[i]);
00354 }
00355
00356
00357 s->state = (s->mf==1) ? ST_WAIT_RECV : ST_FINISHED_RECV;
00358
00359 }
00360
00369 void carrega_img(struct sftd_state *s){
00370 int i;
00371 char a[20];
00372
00373 DEBUG_ENTER;
00374 if(s->nr_pkt == 0){
00375
00376 WriteSpiCommand(0x2b);
00377 WriteSpiData(0);
00378 WriteSpiData(131);
00379 WriteSpiCommand(0x2a);
00380 WriteSpiData(0);
00381 WriteSpiData(131);
00382 WriteSpiCommand(0x2c);
00383
00384 contImg = 0;
00385 for(i=0;i<TAM_BUFF_TEMP;i++) buff_temp[i]=0;
00386 flag_buff_temp = 0;
00387 n_buff = 0;
00388 break_geral = 0;
00389 }
00390
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 s->state = (s->mf==1) ? ST_WAIT_RECV : ST_FINISHED_RECV;
00443
00444
00445 if(break_geral == 1) flag_sendu = 0;
00446
00447 }
00448
00449
00450
00459 void carrega_imgu(struct sftd_state *s){
00460 int i;
00461 char a[20];
00462
00463 DEBUG_ENTER;
00464
00465 if(contImg < TOTAL_PIXEL && break_geral==0){
00466 for(i=0;i<s->len_buff;){
00467 if(contImg >= TOTAL_PIXEL) break;
00468
00469 if(flag_buff_temp > 0){
00470 while(n_buff--){
00471 if(flag_buff_temp == 3){
00472 WriteSpiData(buff_temp[0]);
00473 WriteSpiData(buff_temp[1]);
00474 WriteSpiData(s->buff[i]);
00475 contImg+=3;
00476 }
00477 else{
00478 WriteSpiData(buff_temp[0]);
00479 WriteSpiData(s->buff[i]);
00480 WriteSpiData(s->buff[i+1]);
00481 contImg+=3;
00482 }
00483 }
00484 i = flag_buff_temp -1;
00485 if(flag_buff_temp!=1) flag_buff_temp = 0;
00486 }
00487
00488
00489 if(flag_buff_temp ==1) flag_buff_temp = 0;
00490 else n_buff = s->buff[i] & 0xff;
00491 if(n_buff == 0xfa) { break_geral = 1; break; }
00492 while(n_buff--){
00493 WriteSpiData(s->buff[i+1]);
00494 WriteSpiData(s->buff[i+2]);
00495 WriteSpiData(s->buff[i+3]);
00496 contImg+=3;
00497 }
00498 i +=4;
00499
00500 if(i > s->len_buff){
00501 if(i == s->len_buff + 4) break;
00502 flag_buff_temp = i - s->len_buff;
00503 n_buff = s->buff[i - 3] & 0xff;
00504 if(n_buff == 0xfa){ break_geral = 1; break; }
00505 if(flag_buff_temp == 3){
00506 buff_temp[0] = s->buff[i-2];
00507 buff_temp[1] = s->buff[i-1];
00508 }
00509 else buff_temp[0] = s->buff[i-1];
00510 }
00511 }
00512
00513 }
00514 else flag_sendu = 0;
00515
00516
00517
00518 if(break_geral == 1) flag_sendu = 0;
00519
00520 }
00521
00522