Exemple #1
0
int read_message() {
	DWORD rread;
	const struct ble_msg *apimsg;
	struct ble_header apihdr;
	unsigned char data[256]; //enough for BLE

	/* read header */
	if (!ReadFile(serial_handle, (unsigned char*) &apihdr, 4, &rread, NULL )) {
		return GetLastError();
	}
	if (!rread)
		return 0;

	/* read rest if needed */
	if (apihdr.lolen) {
		if (!ReadFile(serial_handle, data, apihdr.lolen, &rread, NULL )) {
			return GetLastError();
		}
	}
	apimsg = ble_get_msg_hdr(apihdr);
	if (!apimsg) {
		printf("ERROR: Message not found:%d:%d\n", (int) apihdr.cls,
				(int) apihdr.command);
		return -1;
	}
	apimsg->handler(data);

	return 0;
}
//*****************************************************************************
//
// input event, response via BGLib
//
//*****************************************************************************
void input() {
	uint8_t data[256]; //enough for BGLib
	const struct ble_msg *apimsg;
	struct ble_header apihdr;

	UART4Receive((uint8_t*) &apihdr, 4);
	//UARTprintf("%d %d %d %d\n", (int)(apihdr.type_hilen), (int)(apihdr.lolen), (int)(apihdr.cls), (int)(apihdr.command));

	if (apihdr.lolen) {
		UART4Receive(data, apihdr.lolen);
	}

	apimsg = ble_get_msg_hdr(apihdr); //Error: sometimes apimsg hdr is wrong
	apimsg->handler(data);
}
Exemple #3
0
// read and parse message from usb dongle over uart
void *read_message()
{
  fprintf(stderr,"reading thread started\n");
  unsigned char data[256]; // enough for BLE
  struct ble_header hdr;
  int r;

  while (state != state_finish) {

    r = uart_rx(sizeof(hdr), (unsigned char *)&hdr, UART_TIMEOUT);
    if (!r) {
      //printf("ERROR: UART timeout. Error code:%d\n", r);
      continue; // return NULL; // timeout
    } else if (r < 0) {
      fprintf(stderr,"ERROR: Reading header failed. Error code:%d\n", r);
      pthread_exit(NULL);
    }

    if (hdr.lolen) {
      r = uart_rx(hdr.lolen, data, UART_TIMEOUT);
      if (r <= 0) {
        fprintf(stderr,"ERROR: Reading data failed. Error code:%d\n", r);
        pthread_exit(NULL);
      }
    }

    const struct ble_msg *msg = ble_get_msg_hdr(hdr);

#ifdef DEBUG
    print_raw_packet(&hdr, data);
#endif

    if (!msg) {
      fprintf(stderr,"ERROR: Unknown message received\n");
      pthread_exit(NULL);
    }
    msg->handler(data);

    //usleep(500);
  }

  pthread_exit(NULL);

  //return 0;
}
Exemple #4
0
int read_message(int timeout_ms)
{
  unsigned char data[256]; // enough for BLE
  struct ble_header hdr;
  int r;

  r = uart_rx(sizeof(hdr), (unsigned char *)&hdr, UART_TIMEOUT);
  if (!r)
  {
    return -1; // timeout
  }
  else if (r < 0)
  {
    printf("ERROR: Reading header failed. Error code: %d\n", r);
    return 1;
  }

  if (hdr.lolen)
  {
    r = uart_rx(hdr.lolen, data, UART_TIMEOUT);
    if (r <= 0)
    {
      printf("ERROR: Reading data failed. Error code: %d\n", r);
      return 1;
    }
  }

  const struct ble_msg *msg = ble_get_msg_hdr(hdr);

#ifdef DEBUG
  print_raw_packet(&hdr, data);
#endif

  if (!msg)
  {
    printf("ERROR: Unknown message received\n");
    exit(1);
  }

  msg->handler(data);

  return 0;
}
Exemple #5
0
/**
 * Receive BGAPI packet using UART interface
 *
 * @param timeout_ms Milliseconds to wait before timing out on the UART RX operation
 */
int read_api_packet(int timeout_ms)
{
  unsigned char data[256]; // enough for BLE
  struct ble_header hdr;
  int r;

  r = uart_rx(sizeof(hdr), (unsigned char *)&hdr, timeout_ms);
  if (!r) {
    return -1; // timeout
  } else if (r < 0) {
    printf("ERROR: Reading header failed. Error code:%d\n", r);
    return 1;
  }

  if (hdr.lolen) {
    r = uart_rx(hdr.lolen, data, timeout_ms);
    if (r <= 0) {
      printf("ERROR: Reading data failed. Error code:%d\n", r);
      return 1;
    }
  }

  // use BGLib function to create correct ble_msg structure based on the header
  // (header contains command class/ID info, used to identify the right structure to apply)
  const struct ble_msg *msg = ble_get_msg_hdr(hdr);

#ifdef DEBUG
  // display incoming BGAPI packet
  print_raw_packet(&hdr, data);
#endif

  if (!msg) {
    printf("ERROR: Unknown message received\n");
    exit(1);
  }

  // call the appropriate handler function with any payload data
  // (this is what triggers the ble_evt_* and ble_rsp_* functions)
  msg -> handler(data);

  return 0;
}