Logo Search packages:      
Sourcecode: cafix version File versions  Download package

receive.c

#include "custom.h"
extern int promptfilenames; //, fdev;
char nompc[20], *x;
int a;
int receivePG(char *argv[])
{
    cafixdata *list = NULL;
    cafixdata *data;
   int fdev;
    printf("Opening /dev/casio\n");
    if ((fdev = cafix_open("/dev/casio", 0)) < 0) {
      perror("cafix_open");
      exit(-1);
    }
//    openio();

    data = calloc(1, sizeof(cafixdata));
    printf("Handshaking...");fflush(stdout);
    while (cafix_handshake_receive(fdev, data)) {
      printf("retry...");fflush(stdout);
    }
    printf("done!\n");

    list = calloc(1, sizeof(cafixdata));
    while ((!cafix_receive(fdev, data, &show_status)) && (data->format != FMT_END)) {
      printf("\nReceived data (format=%i, datatype=%i, size=%i, name=%s, unstackable=%i)\n", data->format, data->datatype, data->length, data->name, data->unstackable);
      if (data->datatype == DATA_UNKNOWN && data->format != FMT_END)
          printf("Unknown datatype: %c%c\n", data->rawheader[4], data->rawheader[5]);

      if (data->format == FMT_REQUEST) {
          /* Reply to Receive().
             Example program: 0->X:Receive(X):X */
          double grilled;
          cafixdata *reply;

          printf("Responding to REQ...\n");
          grilled = 42;
          reply = calloc(1, sizeof(cafixdata));
          memcpy(reply, data, sizeof(cafixdata));
          reply->format = FMT_VARIABLE;
          cafix_data_generate(reply, (void*) &grilled);
          if (cafix_handshake_send(fdev, reply))
            printf("Handshaking interactive response failed (status=%i)!\n", reply->status);
          if (cafix_send(fdev, reply, &show_status))
            printf("Couldn't respond to request (status=%i)!\n", reply->status);
          free(reply->data);
          memset(reply, 0, sizeof(cafixdata));
          reply->format = FMT_END;
          cafix_send(fdev, reply, &show_status);
          printf("\nResponded with 42.\n");
          free(reply);
//        data->unstackable = 1;
      }

      cafix_list_append(list, data);
      if (data->unstackable == 1) {
          data = NULL;
          break;
      }
      data = calloc(1, sizeof(cafixdata));
    }
    if (data != NULL)
      free(data);
    printf("No more data. Here's a list of all I got:\n");
    data = list->next;
    while (data != NULL) {
      if (data->format != FMT_END) {
          FILE *f;
          char *fname;
          char *ftype;
          fname=malloc(100);
          switch(data->datatype) {
                                   case 1:
                                     ftype="backup";
                                     break;
            case 8:
               ftype="prog";
               break;
                                  case 9:
                                      ftype="Y=data";
                                      break;
            case 13:
               ftype="var";
               break;
            default:
               ftype="unknown";
               break; }

                     if ( promptfilenames ==0) { sprintf(fname, "%s-%s.cafix", ftype, data->name);
          f = fopen(fname, "w");
          fwrite(data->data, data->length, 1, f);
          fclose(f);
          free(fname); }
                      if ( promptfilenames ==1) {
                        printf("Input filename (7 characters):");
                        for(a=0;a<20;a++){
                        nompc[a]=getchar();
                        if (nompc[a]=='\n'){ nompc[a]='\0'; break;}}
                        //nompc=nomcasio;
                      sprintf(fname, "%s-%s.cafix", ftype, nompc); //data->name);
                     f = fopen(fname, "w");
          fwrite(data->data, data->length, 1, f);
          fclose(f);
          free(fname); }}
      if (data->format != FMT_END)
          printf("%-11s:% 6i\n", data->name, data->length);
      data = data->next;
    }
    cafix_list_free_all(list);



    cafix_close(fdev);
    return EXIT_SUCCESS;
}


Generated by  Doxygen 1.6.0   Back to index