www.pudn.com > IrDA.rar > main.c


/*
  Copyright (C) 2002-2003 Gerd Rausch, BlauLogic (http://blaulogic.com)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  3. Except as contained in this notice, neither the name of BlauLogic
     nor the name(s) of the author(s) may be used to endorse or promote
     products derived from this software without specific prior written
     permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHOR(S) OR BLAULOGIC BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include 
#include 
#include 
#include 

#include "irphy.h"
#include "irobex.h"
#include "ircomm.h"

static int16_t get_name_cb(char *name, uint16_t size, void *user_data)
{
  strncpy(name, user_data, size);
  name[size-1]=0;

  return strlen(name);
}

static int16_t get_data_cb(uint8_t *data, uint16_t size, void *user_data)
{
  return fread(data, 1, size, stdin);
}

static uint8_t put_name_cb(char *name, void *user_data)
{
  fprintf(stderr, "file_name=%s\n", name);

  return 1;
}

static uint8_t put_data_cb(uint8_t *data, uint16_t size, void *user_data)
{
  fwrite(data, 1, size, stdout);

  return 1;
}

static int16_t put_ircomm_cb(uint8_t *data, uint16_t size,
			     uint8_t *resp_buf, uint16_t resp_buf_size,
			     uint8_t *terminate_p,
			     void *user_data)
{
  int16_t n;

  fprintf(stderr, "ircomm=>");
  fwrite(data, 1, size, stderr);
  fprintf(stderr, "<\n");

  n=size;
  if(n>resp_buf_size)
    n=resp_buf_size;

  memcpy(resp_buf, data, n);

  return n;
}

int main(int argc, char **argv)
{
  IrLAP_Context ctx;
  IrIAS_Node *ias_nodes[3];
  int16_t dlsap_sel;

  irphy_reset();
  irlap_init_context(&ctx);

  if(argc>1) {
    fprintf(stderr, "%s: primary mode\n", argv[0]);

    if(!irobex_send(&ctx, 0, get_name_cb, get_data_cb, argv[1])) {
      fprintf(stderr, "%s: error sending data\n", argv[0]);
      exit(1);
    }
  } else {
    fprintf(stderr, "%s: secondary mode\n", argv[0]);

    ias_nodes[0]=&irobex_ias_node;
    ias_nodes[1]=&ircomm_ias_node;
    ias_nodes[2]=0;

    for(;;) {
      irphy_wait(-1);
      if((dlsap_sel=irttp2_accept(&ctx, IRLMP_HINT1_OBEX | IRLMP_HINT1_COMM,
				  ias_nodes))<0)
	continue;
      if(dlsap_sel==IRCOMM_LSAP_SEL_VAL) {
	ircomm2_serve(&ctx, put_ircomm_cb, 0);
      } else
	if(irobex_receive(&ctx, put_name_cb, put_data_cb, 0))
	  break;
    }

    fflush(stdout);
    fputc('\n', stderr);
  }

  return 0;
}