irlap2.c
上传用户:hymould
上传日期:2014-01-20
资源大小:36k
文件大小:7k
源码类别:

单片机开发

开发平台:

C/C++

  1. /*
  2.   Copyright (C) 2002-2003 Gerd Rausch, BlauLogic (http://blaulogic.com)
  3.   All rights reserved.
  4.   Redistribution and use in source and binary forms, with or without
  5.   modification, are permitted provided that the following conditions
  6.   are met:
  7.   1. Redistributions of source code must retain the above copyright
  8.      notice, this list of conditions and the following disclaimer.
  9.   2. Redistributions in binary form must reproduce the above copyright
  10.      notice, this list of conditions and the following disclaimer in the
  11.      documentation and/or other materials provided with the distribution.
  12.   3. Except as contained in this notice, neither the name of BlauLogic
  13.      nor the name(s) of the author(s) may be used to endorse or promote
  14.      products derived from this software without specific prior written
  15.      permission.
  16.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17.   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18.   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19.   IN NO EVENT SHALL THE AUTHOR(S) OR BLAULOGIC BE LIABLE FOR ANY CLAIM,
  20.   DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21.   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  22.   THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <string.h>
  25. #include "irlap.h"
  26. void irlap2_send_i_frame(IrLAP_Context *context_p, IrLAP_Frame *frame_p, uint16_t size)
  27. {
  28.   frame_p->head.addr=context_p->conn_addr;
  29.   frame_p->head.ctrl=IRLAP_CTRL_P_F_MASK | context_p->vr<<IRLAP_CTRL_NR_BIT_POS | context_p->vs<<IRLAP_CTRL_NS_BIT_POS;
  30.   irlap_send_frame(frame_p, size);
  31. }
  32. int16_t irlap2_receive_i_frame(IrLAP_Context *context_p, IrLAP_Frame *frame_p,
  33.        IrLAP_Frame *last_resp_p, uint16_t last_resp_size,
  34.        uint16_t (*get_dev_info_cb)(uint8_t *dev_info_p, uint8_t user_data),
  35.        uint8_t get_dev_info_user_data)
  36. {
  37.   IrLAP_Small_Frame resp;
  38.   int16_t n;
  39.   uint16_t param_len;
  40.   uint8_t old_vs;
  41.   old_vs=context_p->vs;
  42.   for(;;) {
  43.     if((n=irlap_receive_frame(frame_p))<0)
  44.       return -1;
  45.     if(!(frame_p->head.addr & IRLAP_ADDR_C_TEST))
  46.       /* discard response frames in secondary mode */
  47.       continue;
  48.     /* command frame */
  49.     if(frame_p->head.ctrl & IRLAP_CTRL_I_TEST) {
  50.       /* U or S format */
  51.       if(frame_p->head.ctrl & IRLAP_CTRL_S_TEST) {
  52. /* U format */
  53. switch(frame_p->head.ctrl & IRLAP_CTRL_U_MASK) {
  54. case IRLAP_CTRL_U_SNRM_CMD:
  55.   if(frame_p->head.addr==(IRLAP_ADDR_BROADCAST | IRLAP_ADDR_C_TEST) &&
  56.      n>=sizeof(IrLAP_Head)+sizeof(IrLAP_SNRM) &&
  57.      irlap_matching_dest_addr(context_p, frame_p->u.snrm.dest)) {
  58.     context_p->conn_addr=frame_p->u.snrm.conn_addr;
  59.     context_p->vr=0;
  60.     context_p->vs=0;
  61.     resp.head.addr=context_p->conn_addr;
  62.     resp.head.ctrl=IRLAP_CTRL_U_UA_RESP | IRLAP_CTRL_P_F_MASK;
  63.     memcpy(resp.u.ua.source, context_p->dev_addr, sizeof(IrLAP_Device_Addr));
  64.     memcpy(resp.u.ua.dest, frame_p->u.snrm.source, sizeof(IrLAP_Device_Addr));
  65.     param_len=irlap_append_neg_params(resp.u.ua.info);
  66.     irlap_send_frame((IrLAP_Frame *)&resp, sizeof(IrLAP_Head)+sizeof(IrLAP_UA)+param_len);
  67.   }
  68.   break;
  69. case IRLAP_CTRL_U_DISC_CMD:
  70.   if((frame_p->head.addr & ~IRLAP_ADDR_C_TEST)==context_p->conn_addr) {
  71.     resp.head.addr=context_p->conn_addr;
  72.     resp.head.ctrl=IRLAP_CTRL_U_UA_RESP | IRLAP_CTRL_P_F_MASK;
  73.     memcpy(resp.u.ua.source, context_p->dev_addr, sizeof(IrLAP_Device_Addr));
  74.     memcpy(resp.u.ua.dest, frame_p->u.snrm.source, sizeof(IrLAP_Device_Addr));
  75.     irlap_send_frame((IrLAP_Frame *)&resp, sizeof(IrLAP_Head)+sizeof(IrLAP_UA));
  76.   }
  77.   break;
  78. case IRLAP_CTRL_U_UI_CMD:
  79.   break;
  80. case IRLAP_CTRL_U_XID_CMD:
  81.   if(frame_p->head.addr==(IRLAP_ADDR_BROADCAST | IRLAP_ADDR_C_TEST) &&
  82.      n>=sizeof(IrLAP_Head)+sizeof(IrLAP_Discovery) &&
  83.      frame_p->u.discovery.format==IRLAP_XID_FMT_DISCOVERY &&
  84.      irlap_matching_dest_addr(context_p, frame_p->u.discovery.dest)) {
  85.     if(frame_p->u.discovery.slot==((frame_p->u.discovery.flags&IRLAP_XID_FLAGS_SLOTS_MASK)==IRLAP_XID_FLAGS_SLOTS_1 ? 0 : 1)) {
  86.       resp.head.addr=IRLAP_ADDR_BROADCAST;
  87.       resp.head.ctrl=IRLAP_CTRL_U_XID_RESP | IRLAP_CTRL_P_F_MASK;
  88.       resp.u.discovery.format=IRLAP_XID_FMT_DISCOVERY;
  89.       memcpy(resp.u.discovery.source, context_p->dev_addr, sizeof(IrLAP_Device_Addr));
  90.       memcpy(resp.u.discovery.dest, frame_p->u.discovery.source, sizeof(IrLAP_Device_Addr));
  91.       resp.u.discovery.flags=frame_p->u.discovery.flags;
  92.       resp.u.discovery.slot=0;
  93.       resp.u.discovery.version=IRLAP_VERSION;
  94.       if(get_dev_info_cb)
  95. param_len=(*get_dev_info_cb)(resp.u.discovery.info, get_dev_info_user_data);
  96.       else
  97. param_len=0;
  98.       irlap_send_frame((IrLAP_Frame *)&resp, sizeof(IrLAP_Head)+sizeof(IrLAP_Discovery)+param_len);
  99.     }
  100.   }
  101.   break;
  102. case IRLAP_CTRL_U_TEST_CMD:
  103.   if(n>=sizeof(IrLAP_Head)+sizeof(IrLAP_Test) &&
  104.      irlap_matching_dest_addr(context_p, frame_p->u.test.dest)) {
  105.     resp.head.addr=IRLAP_ADDR_NULL;
  106.     resp.head.ctrl=IRLAP_CTRL_U_TEST_RESP | IRLAP_CTRL_P_F_MASK;
  107.     memcpy(resp.u.test.source, context_p->dev_addr, sizeof(IrLAP_Device_Addr));
  108.     memcpy(resp.u.test.dest, frame_p->u.test.source, sizeof(IrLAP_Device_Addr));
  109.     irlap_send_frame((IrLAP_Frame *)&resp, sizeof(IrLAP_Head)+sizeof(IrLAP_Test));
  110.   }
  111.   break;
  112. }
  113.       } else {
  114. /* S format */
  115. switch(frame_p->head.ctrl & IRLAP_CTRL_S_MASK) {
  116. case IRLAP_CTRL_S_RR:
  117.   if((frame_p->head.addr & ~IRLAP_ADDR_C_TEST)==context_p->conn_addr) {
  118.     context_p->vs=(frame_p->head.ctrl>>IRLAP_CTRL_NR_BIT_POS)&IRLAP_CTRL_N_MASK;
  119.     if(last_resp_p && context_p->vs==old_vs) {
  120.       /* resend last response */
  121.       irlap2_send_i_frame(context_p, last_resp_p, last_resp_size);
  122.     } else {
  123.       /* send RR response */
  124.       resp.head.addr=context_p->conn_addr;
  125.       resp.head.ctrl=IRLAP_CTRL_S_RR | IRLAP_CTRL_P_F_MASK | context_p->vr<<IRLAP_CTRL_NR_BIT_POS;
  126.       irlap_send_frame((IrLAP_Frame *)&resp, sizeof(IrLAP_Head));
  127.     }
  128.   }
  129.   break;
  130. case IRLAP_CTRL_S_RNR:
  131.   break;
  132. case IRLAP_CTRL_S_REJ:
  133.   break;
  134. case IRLAP_CTRL_S_SREJ:
  135.   break;
  136. }
  137.       }
  138.     } else {
  139.       /* I format */
  140.       if((frame_p->head.addr & ~IRLAP_ADDR_C_TEST)==context_p->conn_addr &&
  141.  context_p->vr==((frame_p->head.ctrl>>IRLAP_CTRL_NS_BIT_POS)&IRLAP_CTRL_N_MASK)) {
  142. context_p->vs=(frame_p->head.ctrl>>IRLAP_CTRL_NR_BIT_POS)&IRLAP_CTRL_N_MASK;
  143. if(last_resp_p && context_p->vs==old_vs) {
  144.   /* reject received frame */
  145.   resp.head.addr=context_p->conn_addr;
  146.   resp.head.ctrl=IRLAP_CTRL_S_REJ | IRLAP_CTRL_P_F_MASK | context_p->vr<<IRLAP_CTRL_NR_BIT_POS;
  147.   irlap_send_frame((IrLAP_Frame *)&resp, sizeof(IrLAP_Head));
  148. } else {
  149.   context_p->vr=(context_p->vr+1)&IRLAP_CTRL_N_MASK;
  150.   return n;
  151. }
  152.       }
  153.     }
  154.   }
  155. }