cmux_gsm.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-15 xiangxistu the first version
  9. */
  10. #include <cmux.h>
  11. #ifdef PKG_USING_PPP_DEVICE
  12. #include <ppp_chat.h>
  13. #else
  14. #include <cmux_chat.h>
  15. #endif
  16. #define DBG_TAG "cmux.gsm"
  17. #ifdef CMUX_DEBUG
  18. #define DBG_LVL DBG_LOG
  19. #else
  20. #define DBG_LVL DBG_INFO
  21. #endif
  22. #include <rtdbg.h>
  23. #define CMUX_CMD "AT+CMUX=0"
  24. struct cmux *gsm = RT_NULL;
  25. static const struct modem_chat_data cmd[] =
  26. {
  27. {"AT", MODEM_CHAT_RESP_OK, 10, 1, RT_FALSE},
  28. {CMUX_CMD, MODEM_CHAT_RESP_OK, 5, 1, RT_FALSE},
  29. };
  30. static rt_err_t cmux_at_command(struct rt_device *device)
  31. {
  32. /* private control, you can add power control */
  33. // rt_thread_mdelay(5000);
  34. return modem_chat(device, cmd, sizeof(cmd) / sizeof(cmd[0]));
  35. }
  36. static rt_err_t cmux_gsm_start(struct cmux *obj)
  37. {
  38. rt_err_t result = 0;
  39. struct rt_device *device = RT_NULL;
  40. device = obj->dev;
  41. result = rt_device_open(device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  42. if(result != RT_EOK)
  43. {
  44. LOG_E("cmux can't open %s.", device->parent.name);
  45. goto _end;
  46. }
  47. LOG_I("cmux has been control %s.", device->parent.name);
  48. result = cmux_at_command(device);
  49. if(result != RT_EOK)
  50. {
  51. LOG_E("cmux start failed.");
  52. goto _end;
  53. }
  54. _end:
  55. return result;
  56. }
  57. const struct cmux_ops cmux_ops =
  58. {
  59. cmux_gsm_start,
  60. RT_NULL,
  61. RT_NULL
  62. };
  63. int cmux_gsm_init(void)
  64. {
  65. gsm = rt_malloc(sizeof(struct cmux));
  66. rt_memset(gsm, 0, sizeof(struct cmux));
  67. gsm->ops = &cmux_ops;
  68. return cmux_init(gsm, CMUX_DEPEND_NAME, CMUX_PORT_NUMBER, RT_NULL);
  69. }
  70. INIT_COMPONENT_EXPORT(cmux_gsm_init);