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 #include "string.h"
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 cmd[3], id[4], mf[2], len[5], nro[3];
00116 int i;
00117 static struct sft_hdr_uip *sftptr;
00118
00119 sprintf(estado,"Estado: %d\n", s->state);
00120 DEBUG_SFT(estado);
00121
00122 sftptr = (struct sft_hdr_uip *)uip_appdata;
00123
00124
00125
00126
00127
00128 if(flag_sendu>0){
00129 DEBUG_SFT("FLAG_SENDU");
00130 s->len_buff = uip_datalen();
00131 s->nr_pkt = flag_sendu++;
00132 strncpy(s->buff, uip_appdata, s->len_buff);
00133 carrega_img(s);
00134 s->state = ST_RECVU;
00135 return 0;
00136 }
00137
00138
00139 if(uip_datalen()>5){
00140
00141
00142 strncpy(cmd, sftptr->cmd,2);
00143 cmd[3] = '\0';
00144 s->cmd = atoi(cmd);
00145 sprintf(estado, "\ncmd: %02d\t", s->cmd);
00146 DEBUG_SFT(estado);
00147
00148
00149
00150 strncpy(len, sftptr->len, 4);
00151 len[5]='\0';
00152 s->len_buff = atoi(len);
00153 sprintf(estado, "len: %04d\t", s->len_buff);
00154 DEBUG_SFT(estado);
00155
00156
00157
00158 strncpy(mf, sftptr->mf,1);
00159 mf[2]='\0';
00160 s->mf = atoi(mf);
00161 sprintf(estado, "MF: %d\t", s->mf);
00162 DEBUG_SFT(estado);
00163
00164
00165
00166 strncpy(nro, sftptr->nro_pkt,2);
00167 nro[3]='\0';
00168 s->nr_pkt = atoi(nro);
00169 sprintf(estado, "NRO PKT: %02d\t", s->nr_pkt);
00170 DEBUG_SFT(estado);
00171
00172 if(s->nr_pkt==0) printf("");
00173
00174
00175 strncpy(id, sftptr->id,3);
00176 id[4]='\0';
00177 s->id = atoi(id);
00178 sprintf(estado, "ID: %d\t", s->id);
00179 DEBUG_SFT(estado);
00180
00181
00182 fflush(stdout);
00183
00184
00185 for(i=0; i<s->len_buff;i++) s->buff[i] = sftptr->buff_send[i];
00186
00187 switch (s->cmd){
00188
00189
00190 case CMD_RECV:
00191 s->state = ST_RECV;
00192 carrega_img(s);
00193 break;
00194
00195 case CMD_STOP:
00196 s->state = ST_STOP;
00197 info_host_ans(s, CMD_STOP);
00198 break;
00199 case CMD_RECV_X:
00200 s->state = ST_RECV;
00201 carrega_img_old(s);
00202 break;
00203 case CMD_RECVU:
00204 s->state = ST_RECVU;
00205 flag_sendu = 1;
00206 carrega_img(s);
00207 break;
00208 case CMD_MSG:
00209 s->state = ST_RECV;
00210 envia_msg(s);
00211 default: break;
00212
00213 }
00214
00215
00216 switch(s->state){
00217 case ST_WAIT_RECV:
00218 info_host_ans(s, CMD_RECV_ANS);
00219 break;
00220
00221 case ST_FINISHED_RECV:
00222
00223 info_host_ans(s, CMD_FINISHED);
00224 default: break;
00225
00226 }
00227
00228 if((FIO4PIN & (1<<8))==0){
00229
00230 printf("\nfim do envio de imagens.\n");
00231 s->state = ST_STOP;
00232 for(i=0; i < 26136;){
00233 WriteSpiData(0xff); i++;
00234 WriteSpiData(0xff); i++;
00235 WriteSpiData(0xff); i++;
00236 }
00237 info_host_ans(s, CMD_STOP);
00238 }
00239 }
00240
00241 return 0;
00242 }
00243
00244
00250 void info_host_ans(struct sftd_state *s, int cmd){
00251 char bf[100];
00252 int len;
00253 struct sft_hdr_uip *sptr;
00254
00255
00256 sprintf(sptr->cmd, "%02d", cmd);
00257
00258
00259
00260 sprintf(sptr->len, "0002");
00261
00262
00263 sprintf(sptr->mf, "0");
00264
00265
00266 sprintf(sptr->nro_pkt, "00");
00267
00268
00269 sprintf(sptr->id, "%s", s->id);
00270
00271 sprintf(sptr->buff_send, "ok");
00272
00273
00274
00275
00276 uip_send((struct sft_hdr_uip *)sptr, sizeof(sptr));
00277 }
00278
00285 void carrega_img_old(struct sftd_state *s){
00286 int i;
00287 char a[20];
00288
00289 DEBUG_ENTER;
00290
00291 if(s->nr_pkt == 0){
00292
00293 WriteSpiCommand(0x2b);
00294 WriteSpiData(0);
00295 WriteSpiData(131);
00296 WriteSpiCommand(0x2a);
00297 WriteSpiData(0);
00298 WriteSpiData(131);
00299 WriteSpiCommand(0x2c);
00300
00301 }
00302
00303 for(i=0;i < s->len_buff; i++){
00304
00305
00306 WriteSpiData(s->buff[i]);
00307 }
00308
00309
00310 s->state = (s->mf==1) ? ST_WAIT_RECV : ST_FINISHED_RECV;
00311
00312 }
00313
00322 void carrega_img(struct sftd_state *s){
00323 int i;
00324 char a[20];
00325
00326 DEBUG_ENTER;
00327 if(s->nr_pkt == 0){
00328
00329 WriteSpiCommand(0x2b);
00330 WriteSpiData(0);
00331 WriteSpiData(131);
00332 WriteSpiCommand(0x2a);
00333 WriteSpiData(0);
00334 WriteSpiData(131);
00335 WriteSpiCommand(0x2c);
00336
00337 contImg = 0;
00338 for(i=0;i<TAM_BUFF_TEMP;i++) buff_temp[i]=0;
00339 flag_buff_temp = 0;
00340 n_buff = 0;
00341 break_geral = 0;
00342 }
00343
00344 if(contImg < TOTAL_PIXEL && break_geral==0){
00345 for(i=0;i<s->len_buff;){
00346 if(contImg >= TOTAL_PIXEL) break;
00347
00348 if(flag_buff_temp > 0){
00349 while(n_buff--){
00350 if(flag_buff_temp == 3){
00351 WriteSpiData(buff_temp[0]);
00352 WriteSpiData(buff_temp[1]);
00353 WriteSpiData(s->buff[i]);
00354 contImg+=3;
00355 }
00356 else{
00357 WriteSpiData(buff_temp[0]);
00358 WriteSpiData(s->buff[i]);
00359 WriteSpiData(s->buff[i+1]);
00360 contImg+=3;
00361 }
00362 }
00363 i = flag_buff_temp -1;
00364 if(flag_buff_temp!=1) flag_buff_temp = 0;
00365 }
00366
00367
00368 if(flag_buff_temp ==1) flag_buff_temp = 0;
00369 else n_buff = s->buff[i] & 0xff;
00370 if(n_buff == 0xfa) { break_geral = 1; break; }
00371 while(n_buff--){
00372 WriteSpiData(s->buff[i+1]);
00373 WriteSpiData(s->buff[i+2]);
00374 WriteSpiData(s->buff[i+3]);
00375 contImg+=3;
00376 }
00377 i +=4;
00378
00379 if(i > s->len_buff){
00380 if(i == s->len_buff + 4) break;
00381 flag_buff_temp = i - s->len_buff;
00382 n_buff = s->buff[i - 3] & 0xff;
00383 if(n_buff == 0xfa){ break_geral = 1; break; }
00384 if(flag_buff_temp == 3){
00385 buff_temp[0] = s->buff[i-2];
00386 buff_temp[1] = s->buff[i-1];
00387 }
00388 else buff_temp[0] = s->buff[i-1];
00389 }
00390 }
00391
00392 }
00393
00394
00395 s->state = (s->mf==1) ? ST_WAIT_RECV : ST_FINISHED_RECV;
00396
00397
00398 if(break_geral == 1) flag_sendu = 0;
00399
00400 }
00401
00402
00403
00409 void envia_msg(struct sftd_state *s){
00410 int i;
00411 char a[20];
00412 struct txtinfo stxt;
00413
00414 DEBUG_ENTER;
00415
00416
00417 WriteSpiCommand(0x2b);
00418 WriteSpiData(0);
00419 WriteSpiData(131);
00420 WriteSpiCommand(0x2a);
00421 WriteSpiData(0);
00422 WriteSpiData(131);
00423 WriteSpiCommand(0x2c);
00424
00425 for(i=0; i < 26136;){
00426 WriteSpiData(0xff); i++;
00427 WriteSpiData(0xff); i++;
00428 WriteSpiData(0xff); i++;
00429 }
00430
00431
00432
00433 WriteSpiCommand(0x2b);
00434 WriteSpiData(0);
00435 WriteSpiData(131);
00436 WriteSpiCommand(0x2a);
00437 WriteSpiData(0);
00438 WriteSpiData(131);
00439 WriteSpiCommand(0x2c);
00440
00441
00442 stxt.corfundo = 0xff0;
00443 stxt.corfrente = 0x4700;
00444 stxt.linha = 5;
00445 stxt.coluna = 2;
00446 txtattrib(&stxt);
00447
00448 DEBUG_ENTER;
00449
00450 lcdgr_puts(" TCC - MULT UIP ");
00451
00452
00453 stxt.corfundo = 0xff0;
00454 stxt.corfrente = 0x007;
00455 stxt.linha = 30;
00456 stxt.coluna = 1;
00457 txtattrib(&stxt);
00458
00459 lcdgr_puts(s->buff);
00460
00461 s->state = (s->mf==1) ? ST_WAIT_RECV : ST_FINISHED_RECV;
00462
00463 DEBUG_EXIT;
00464
00465 }
00466
00467