console.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "config.h"
  2. #include "console.h"
  3. QueueHandle_t tx_queue = NULL;
  4. QueueHandle_t rx_queue = NULL;
  5. int initialize_timeout = 0;
  6. bt_at_callback gCallback = NULL;
  7. static void* gCbContex = NULL;
  8. /*
  9. example to demonstrate that:
  10. 1. recv AT response/event from blueware and print out
  11. the application should parse these data according to "Feasycom BT90X AT Command interface_v4.1.pdf"
  12. 2. send AT command to blueware for call/music/phonebook control
  13. note:
  14. the applicaton should process data as soon as possilbe (otherwise the queue may overflow)
  15. */
  16. static void* console_task(void* arg)
  17. {
  18. char buf[AT_CMD_PAYLOAD_LEN];
  19. gCbContex = gCbContex;//make iar happy
  20. for (;;)
  21. {
  22. buf[0] = '\0';
  23. char *response = buf;
  24. if (xQueueReceive(rx_queue, response, portMAX_DELAY) == pdTRUE)
  25. {
  26. response[AT_CMD_PAYLOAD_LEN-1] = '\0';
  27. int response_len = strlen(response);
  28. // response always start/end with \r\n
  29. if((response_len < 4) ||
  30. (response[0] != '\r' || response[1] != '\n') ||
  31. (response[response_len-2] != '\r' || response[response_len-1] != '\n'))
  32. {
  33. printf("invalid response format !!! \n");
  34. continue;
  35. }
  36. response[response_len-2] = '\0';
  37. response += 2;
  38. if (NULL != gCallback)
  39. {
  40. gCallback(response);
  41. }
  42. if(strlen(response) > 4 && !memcmp(response,"+VER",4))
  43. {
  44. console_send_atcmd("AT+NAME\r\n", strlen("AT+NAME\r\n"));
  45. }
  46. }
  47. }
  48. return NULL;
  49. }
  50. int console_init(bt_at_callback cb)
  51. {
  52. pthread_t task;
  53. pthread_attr_t attr;
  54. if (NULL == gCallback)
  55. gCallback = cb;
  56. pthread_attr_init(&attr);
  57. pthread_create(&task, NULL, console_task, NULL);
  58. return 0;
  59. }
  60. void console_register_cb(void *ctx, bt_at_callback cb)
  61. {
  62. gCbContex = ctx;
  63. gCallback = cb;
  64. }
  65. int console_send_atcmd(char* cAtCmd, unsigned short length)
  66. {
  67. if (tx_queue == NULL)
  68. {
  69. printf("console_send_atcmd tx_queue NULL\n");
  70. return -1;
  71. }
  72. char buf[AT_CMD_PAYLOAD_LEN] = {0};
  73. char *command;
  74. if (AT_CMD_PAYLOAD_LEN <= length)
  75. {
  76. printf("console_send_atcmd invalid length. %d\r\n", length);
  77. return -1;
  78. }
  79. command = buf;
  80. memcpy(command,cAtCmd, length);
  81. if (pdTRUE != xQueueSend(tx_queue,command,pdMS_TO_TICKS(200)))
  82. {
  83. printf("console_send_atcmd Send failed.\r\n");
  84. return -1;
  85. }
  86. fscbt_wakeup(0);
  87. return 0;
  88. }
  89. int console_deinit(void)
  90. {
  91. if(rx_queue) vQueueDelete(rx_queue);
  92. if(tx_queue) vQueueDelete(tx_queue);
  93. return 0;
  94. }