[嵌入式开发模块]NTP客户端模块(基于wiz/W5500官方io库)

客官°小女子只卖身不卖艺 2024-04-18 23:13 128阅读 0赞

文章目录

  • 前言
  • 代码
    • NTPClient.h
    • NTPClient.c
    • Debug.h
  • 使用示例

前言

最近因项目需要,把之前写好的NTP模块进一步封装了一下,实现了NTP客户端的逻辑。

就很简单的一个模块,稍微改一改就可以改成任何单次request&respond式的基于UDP的协议。算是一个小的示例吧。

里头的MyOS是自己弄的一个小的抽象OS层。就不放源码了,就把里头的MyOSDly改成自己的OS的休眠函数就好。

先复制黏贴好NTP模块的代码:
https://blog.csdn.net/lin\_strong/article/details/90678838

接下来直接上源码:

代码

NTPClient.h

  1. /*
  2. ******************************************************************************************
  3. *
  4. * NTP CLIENT MODULE
  5. *
  6. * File : NTPClient.h
  7. * By : Lin Shijun(http://blog.csdn.net/lin_strong)
  8. * Date: 2019/09/12
  9. * Version: V1.0
  10. * History:
  11. * NOTE(s): the module is to provide NTP client function based on wiznet io-library.
  12. * the current implementation is SNTP.
  13. ******************************************************************************************
  14. */
  15. #ifndef _NTPCLIENT_H
  16. #define _NTPCLIENT_H
  17. /*
  18. *******************************************************************************************
  19. * INCLUDE
  20. *******************************************************************************************
  21. */
  22. #include "NTP.h"
  23. #include "common.h"
  24. /*
  25. *******************************************************************************************
  26. * CONFIGURE 主配置
  27. *******************************************************************************************
  28. */
  29. // to enable debug message
  30. //#define NTPC_DEBUG
  31. // set the wait time of a request
  32. #ifndef NTPC_TIMEOUT_TIME_MS
  33. #define NTPC_TIMEOUT_TIME_MS 100
  34. #endif
  35. /*
  36. *******************************************************************************************
  37. * PUBLIC INTERFACES
  38. *******************************************************************************************
  39. */
  40. // description: launch a ntp request on given socketNum and return the respond if any.
  41. // parameter : socketNum the socket number for ntp client
  42. // buf the buffer for ntp client
  43. // serverIp ip of the ntp server.
  44. // serverPort udp port of the ntp server. 0 if default port(i.e. NTP_PORT)
  45. // ret to return the result if success
  46. // return : TRUE if success
  47. // FALSE if fail
  48. // note :
  49. BOOL NtpClient_RequestTime(uint8_t socketNum, uint8_t *buf, uint8_t serverIp[4],
  50. uint16_t serverPort, NtpTime *ret);
  51. #endif

NTPClient.c

  1. /*
  2. ******************************************************************************************
  3. *
  4. * NTP CLIENT MODULE
  5. *
  6. * File : NTPClient.c
  7. * By : Lin Shijun(http://blog.csdn.net/lin_strong)
  8. * Date: 2019/09/12
  9. * Version: beta
  10. * History:
  11. * NOTE(s):
  12. ******************************************************************************************
  13. */
  14. /*
  15. *******************************************************************************************
  16. * INCLUDES
  17. *******************************************************************************************
  18. */
  19. #include "NTPClient.h"
  20. #include "socket.h"
  21. #include "MyOS.h"
  22. #ifndef NTPC_DEBUG
  23. #undef _DEBUG
  24. #endif
  25. #include "DebugMsg.h"
  26. /*
  27. *******************************************************************************************
  28. * CONFIGURATION
  29. *******************************************************************************************
  30. */
  31. #ifndef NTPC_POLL_INTERVAL_MS
  32. #define NTPC_POLL_INTERVAL_MS 10
  33. #endif
  34. #define NTPC_POLL_CNT ((NTPC_TIMEOUT_TIME_MS) / (NTPC_POLL_INTERVAL_MS))
  35. /*
  36. *******************************************************************************************
  37. * LOCAL FUNCTION DECLARATION
  38. *******************************************************************************************
  39. */
  40. #define socketNum_valid(socketNum) ((socketNum) < _WIZCHIP_SOCK_NUM_)
  41. /*
  42. *******************************************************************************************
  43. * DEBUG STRINGS
  44. *******************************************************************************************
  45. */
  46. static const char *_str_NTPCErrFormat = "NTPC Err: %s.\r\n";
  47. /*
  48. *******************************************************************************************
  49. * PUBLIC INTERFACE IMPLEMENTATIONS
  50. *******************************************************************************************
  51. */
  52. BOOL NtpClient_RequestTime(uint8_t socketNum, uint8_t *buf, uint8_t serverIp[4],
  53. uint16_t serverPort, NtpTime *ret){
  54. int32_t len;
  55. uint8_t dip[4];
  56. uint16_t dport;
  57. BOOL rst = FALSE;
  58. int remainPollCnt = NTPC_POLL_CNT;
  59. if(serverIp == NULL || ret == NULL || buf == NULL || !socketNum_valid(socketNum)){
  60. _dbg_printf1(_str_NTPCErrFormat, "bad param");
  61. return FALSE;
  62. }
  63. serverPort = (serverPort != 0)? serverPort: NTP_PORT;
  64. if(getSn_SR(socketNum) != SOCK_UDP)
  65. if(socketNum != socket(socketNum, Sn_MR_UDP, 0, 0x00)){
  66. _dbg_printf1(_str_NTPCErrFormat, "Open socket");
  67. goto bail;
  68. };
  69. len = (int32_t)NtpMsg_initRequest(buf, NULL);
  70. len = sendto(socketNum, buf, (uint16_t)len, serverIp, serverPort);
  71. if(len < 0){
  72. _dbg_printf1(_str_NTPCErrFormat, "sendto");
  73. goto bail;
  74. }
  75. _dbg_printf0("NTPC :waiting for respond\r\n");
  76. do{
  77. MyOS_DlyHMSM(0,0,0,NTPC_POLL_INTERVAL_MS);
  78. if(getSn_RX_RSR(socketNum) > 0){
  79. len = recvfrom(socketNum, buf, sizeof(NTPMSG), dip, &dport);
  80. // intentionly only check server ip. considering the situation that respond with another port;
  81. if((*(uint32_t *)dip == *(uint32_t *)serverIp) && len == sizeof(NTPMSG)){
  82. NtpMsg_resolveCurTimeSNTP(buf, ret);
  83. _dbg_printf2("NTPC : get ntptime %lu_%lu.\r\n", (unsigned long)ret->sec, (unsigned long)ret->frac);
  84. rst = TRUE;
  85. goto bail;
  86. }
  87. }
  88. if(getSn_SR(socketNum) != SOCK_UDP){
  89. _dbg_printf1(_str_NTPCErrFormat, "socket mode");
  90. goto bail;
  91. }
  92. }while(--remainPollCnt >= 0);
  93. _dbg_printf1(_str_NTPCErrFormat, "timeout");
  94. bail:
  95. close(socketNum);
  96. return rst;
  97. }

Debug.h

顺带附上我现在用的打印调试用的小模块。因为我用的编译器不支持可变参的宏,所以写成了按参数个数不同名字的写法。

  1. #ifndef _DEBUG_MSG_H
  2. #define _DEBUG_MSG_H
  3. #include <stdio.h>
  4. #ifdef _DEBUG
  5. #define _dbg_printf0(format) ((void)printf(format))
  6. #define _dbg_printf1(format,p1) ((void)printf(format,p1))
  7. #define _dbg_printf2(format,p1,p2) ((void)printf(format,p1,p2))
  8. #define _dbg_printf3(format,p1,p2,p3) ((void)printf(format,p1,p2,p3))
  9. #define _dbg_printf4(format,p1,p2,p3,p4) ((void)printf(format,p1,p2,p3,p4))
  10. #define _dbg_printf5(format,p1,p2,p3,p4,p5) ((void)printf(format,p1,p2,p3,p4,p5))
  11. #define _dbg_printf6(format,p1,p2,p3,p4,p5,p6) ((void)printf(format,p1,p2,p3,p4,p5,p6))
  12. #define _dbg_printf7(format,p1,p2,p3,p4,p5,p6,p7) \
  13. ((void)printf(format,p1,p2,p3,p4,p5,p6,p7))
  14. #define _dbg_printf8(format,p1,p2,p3,p4,p5,p6,p7,p8) \
  15. ((void)printf(format,p1,p2,p3,p4,p5,p6,p7,p8))
  16. #define _dbg_printf9(format,p1,p2,p3,p4,p5,p6,p7,p8,p9) \
  17. ((void)printf(format,p1,p2,p3,p4,p5,p6,p7,p8,p9))
  18. #else
  19. #define _dbg_printf0(format)
  20. #define _dbg_printf1(format,p1)
  21. #define _dbg_printf2(format,p1,p2)
  22. #define _dbg_printf3(format,p1,p2,p3)
  23. #define _dbg_printf4(format,p1,p2,p3,p4)
  24. #define _dbg_printf5(format,p1,p2,p3,p4,p5)
  25. #define _dbg_printf6(format,p1,p2,p3,p4,p5,p6)
  26. #define _dbg_printf7(format,p1,p2,p3,p4,p5,p6,p7)
  27. #define _dbg_printf8(format,p1,p2,p3,p4,p5,p6,p7,p8)
  28. #define _dbg_printf9(format,p1,p2,p3,p4,p5,p6,p7,p8,p9)
  29. #endif
  30. #endif

使用示例

如下就可以从202.112.31.197处的NTP服务器处要到当前时间:

  1. static uint8_t ntpBuf[sizeof(NTPMSG)];
  2. static uint8_t ntpServerIP[] = {
  3. 202,112,31,197};
  4. void NtpTask(void *p_arg){
  5. NtpTime time;
  6. Datetime dt;
  7. // 已省去不重要的代码
  8. ...
  9. printf("launch ntp request\r\n");
  10. if(NtpClient_RequestTime(NTP_SOCKET, ntpBuf, ntpServerIP, NTP_PORT,&time) == TRUE){
  11. NtpTime_toDatetime(time.sec, &dt);
  12. printf("%4u-%2u-%2u %2u:%2u:%2u\r\n",dt.yy,dt.mo,dt.dd,dt.hh,dt.mm,dt.ss);
  13. }else{
  14. printf("request time fail\r\n");
  15. }
  16. ...
  17. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,128人围观)

还没有评论,来说两句吧...

相关阅读