Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. //receiver -- application layer
  2.  
  3. #include "logic.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8.  
  9. //states
  10. #define START 0
  11. #define PACKAGE 1
  12. #define END 2
  13. //----
  14.  
  15. int state;
  16. FILE *file;
  17. size_t sizeFile;
  18.  
  19. void handleRead(char *buffer, int size);
  20.  
  21. int main(int argc, char** argv) {
  22. if ( (argc < 2) ||
  23. ((strcmp("/dev/ttyS0", argv[1])!=0) &&
  24. (strcmp("/dev/ttyS1", argv[1])!=0))) {
  25. printf("Usage:\tnserial SerialPort\n\tex: nserial /dev/ttyS1\n");
  26. exit(1);
  27. }
  28.  
  29. if(setup(argv[1]) == ERROR) {
  30. printf("Error: Could not setup serial port.\n");
  31. exit(1);
  32. }
  33.  
  34. if(llopen(RECEIVER) == ERROR){
  35. printf("Unable to connect to the sender!\n");
  36. exit(-1);
  37. }
  38. else {
  39. printf("Connection Successful\n");
  40. }
  41.  
  42. // int readResult;
  43.  
  44. // do {
  45. // char buffer[PACKAGE_DATA_SIZE+4];
  46. // readResult = llread(buffer);
  47.  
  48. // handleRead(buffer, readResult);
  49.  
  50. // } while(readResult > 0);
  51.  
  52.  
  53.  
  54. return 0;
  55. }
  56.  
  57. int handleStartPkg(char *buffer, int size){
  58. // esta a receber pacote start, extrai informacao e abre ficheiro em disco em modo de escrita.
  59. int i;
  60. size_t L1, L2;
  61. char * nameFile;
  62.  
  63. for (i = 0; i < size; i++){
  64. if(buffer[i] == 0x00){
  65. L1 = buffer[i+1];
  66. memcpy(buffer+(i+2), sizeFile, L1);
  67. i = i + L1 + 1;
  68. }
  69. if(buffer[i] == 0x01){
  70. L2 = buffer[i+1];
  71. i++;
  72. nameFile = malloc(L2);
  73. for( ; i < L2; i++){
  74. if(i >= size)
  75. return ERROR;
  76. nameFile[i] = buffer[i];
  77. }
  78. }
  79.  
  80. }
  81.  
  82. file = fopen(nameFile, "wb");
  83.  
  84. if (file == NULL){
  85. printf("Error opening file!\n");
  86. exit(1);
  87. }
  88.  
  89. return 0;
  90. }
  91.  
  92. void handleIPkg(char *buffer, int size){
  93. // percebe que o pacote comtem dados do ficheiro e armazena em disco.);
  94. printf("\nData package received: \n");
  95. // TODO: extrair N
  96. printf("N: %d\n", buffer[1]);
  97. printf("L1: %d\n", buffer[2]);
  98. printf("L2: %d\n", buffer[3]);
  99.  
  100. // parse L1 e L2
  101. // int length = 256 * buffer[2] + buffer[3];
  102. // printf("Tamanho dos dados: %d\n", length);
  103. // printf("Buffer size: %d\n", size);
  104.  
  105. if (file == NULL){
  106. printf("Error: No file opened.\n");
  107. exit(1);
  108. }
  109.  
  110. int length = size-4;
  111. char * temp;
  112. temp = malloc(length);
  113. int i;
  114. for(i = 0; i < length; i++) {
  115. temp[i] = buffer[i+4];
  116. }
  117.  
  118. printBuffer(temp, length);
  119.  
  120. write(fileno(file), temp, length);
  121.  
  122. }
  123.  
  124. void handleEndPkg(char *buffer, int size){
  125. // percebe que e o pacote end e termina.
  126. printf("Finished writing, closing file!\n");
  127.  
  128. fclose(file);
  129.  
  130. // TODO: llclose se for pra fechar apos 1 transferencia.
  131. }
  132.  
  133. void handleRead(char *buffer, int size){
  134.  
  135. //lidar com o c2 do pacote de comando
  136. char c2 = buffer[0];
  137.  
  138. switch(c2){
  139. case 0x02:
  140. handleStartPkg(buffer, size);
  141. break;
  142. case 0x01:
  143. handleIPkg(buffer, size);
  144. break;
  145. case 0x03:
  146. handleEndPkg(buffer, size);
  147. break;
  148. default:
  149. break;
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement