snmp_api.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /********************************* (C) ROTEK ***********************************
  2. * @module template
  3. * @file template.c
  4. * @version 1.0.0
  5. * @date XX.XX.XXXX
  6. * $brief Template
  7. *******************************************************************************
  8. * @history Version Author Comment
  9. * XX.XX.XXXX 1.0.0 Telenkov D.A. First release.
  10. *******************************************************************************
  11. */
  12. #include "stm32f4xx.h"
  13. #include "snmp_api.h"
  14. #include "trap_api.h"
  15. #include "settings_api.h"
  16. #include "common_config.h"
  17. #include "rtc.h"
  18. #include "megatec.h"
  19. #ifdef PRINTF_STDLIB
  20. #include <stdio.h>
  21. #endif
  22. #ifdef PRINTF_CUSTOM
  23. #include "tinystdio.h"
  24. #endif
  25. #include <stdbool.h>
  26. #include <string.h>
  27. #include "lwip/opt.h"
  28. #include "lwip/api.h"
  29. #include "lwip/sys.h"
  30. #include "lwip/udp.h"
  31. //#include "snmp.h"
  32. //#include "snmp_msg.h"
  33. //#include "private_mib.h"
  34. #include "FreeRTOS.h"
  35. #include "task.h"
  36. #include "queue.h"
  37. /**
  38. * @brief Пул всех возможных трапов устройства
  39. */
  40. extern TRAP_t traps[];
  41. /**
  42. * @brief Общая структура настроек
  43. */
  44. extern SETTINGS_t sSettings;
  45. /**
  46. * @brief Очередь для отправки трапов
  47. */
  48. QueueHandle_t SNMP_TrapQueue;
  49. /**
  50. * @brief Инициализация параметров SNMP
  51. * @retval
  52. */
  53. void SNMP_Init(void)
  54. {
  55. //SETTINGS_SetSnmpDef();
  56. SNMP_AgentInit();
  57. }
  58. /**
  59. * @brief Системный тик SNMP. Таск должен вызываться с частотой 100Гц.
  60. * @retval
  61. */
  62. void SNMP_SysUpTimeTask(void *arg)
  63. {
  64. TickType_t xLastWakeTime;
  65. const TickType_t xFrequency = 10;
  66. xLastWakeTime = xTaskGetTickCount();
  67. while(1)
  68. {
  69. vTaskDelayUntil( &xLastWakeTime, xFrequency );
  70. //snmp_inc_sysuptime();
  71. }
  72. }
  73. /**
  74. * @brief Тестовый таск для проверки отправки трапов
  75. * @retval
  76. */
  77. void snmp_trap_tread(void *arg)
  78. {
  79. uint8_t trapName;
  80. while(1)
  81. {
  82. if (xQueueReceive(SNMP_TrapQueue, &trapName, 0) == pdTRUE)
  83. {
  84. SNMP_SetManagerIP(sSettings.sSnmp.managerIP);
  85. SNMP_SendVarbindTrap(&traps[trapName]);
  86. SNMP_SetManagerIP(sSettings.sSnmp.managerIP2);
  87. SNMP_SendVarbindTrap(&traps[trapName]);
  88. SNMP_SetManagerIP(sSettings.sSnmp.managerIP3);
  89. SNMP_SendVarbindTrap(&traps[trapName]);
  90. SNMP_SetManagerIP(sSettings.sSnmp.managerIP4);
  91. SNMP_SendVarbindTrap(&traps[trapName]);
  92. SNMP_SetManagerIP(sSettings.sSnmp.managerIP5);
  93. SNMP_SendVarbindTrap(&traps[trapName]);
  94. }
  95. }
  96. }
  97. /**
  98. * @brief Инициализация SNMP агента
  99. * @retval
  100. */
  101. void SNMP_AgentInit(void)
  102. {/*
  103. SNMP_SetObjDescr();
  104. SNMP_SetReadCommunity(sSettings.sSnmp.readCommunity);
  105. SNMP_SetWriteCommunity(sSettings.sSnmp.writeCommunity);
  106. SNMP_SetSysContact(sSettings.sSnmp.sysContact);
  107. SNMP_SetSysName(sSettings.sSnmp.sysName);
  108. SNMP_SetSysLocation(sSettings.sSnmp.sysLocation);
  109. SNMP_SetManagerIP(sSettings.sSnmp.managerIP);
  110. SNMP_SetObjID();
  111. SNMP_SetTrapOnOff(1);
  112. SNMP_InitTrapsBase();
  113. snmp_init();
  114. udp_init();
  115. */
  116. SNMP_TrapQueue = xQueueCreate(SNMP_TRAP_QUEUE_SIZE, sizeof(uint8_t));
  117. }
  118. /**
  119. * @brief Пользовательская функция для отправки трапа из массива traps[]
  120. * Трап помещается в очередь. Работа с очередью происходит по принципу
  121. * FIFO буфера.
  122. * Если в настройках трапа отправка отключена, то трап игнорируется.
  123. * @retval
  124. */
  125. void SNMP_SendUserTrap(uint8_t trapName)
  126. {
  127. uint16_t availableSpace;
  128. uint8_t dummyTrap;
  129. if (traps[trapName].trapEnable)
  130. {
  131. availableSpace = uxQueueSpacesAvailable(SNMP_TrapQueue);
  132. if (availableSpace == 0)
  133. xQueueReceive(SNMP_TrapQueue, &dummyTrap, 0);
  134. xQueueSend(SNMP_TrapQueue, &trapName, 0);
  135. }
  136. }
  137. /**
  138. * @brief Установить SNMP Descriptor
  139. * @retval
  140. */
  141. // TODO
  142. void SNMP_SetObjDescr(void)
  143. {
  144. static uint8_t len;
  145. strcpy(sSettings.sSnmp.sysDescr, sSettings.sSnmp.sysName);
  146. strcat(sSettings.sSnmp.sysDescr, " ");
  147. strcat(sSettings.sSnmp.sysDescr, VERSION);
  148. strcat(sSettings.sSnmp.sysDescr, " ");
  149. strcat(sSettings.sSnmp.sysDescr, sSettings.sSnmp.sysContact);
  150. strcat(sSettings.sSnmp.sysDescr, " ");
  151. strcat(sSettings.sSnmp.sysDescr, sSettings.sInfo.serialNumber);
  152. strcat(sSettings.sSnmp.sysDescr, " ");
  153. strcat(sSettings.sSnmp.sysDescr, UPS.model);
  154. len = strlen(sSettings.sSnmp.sysDescr);
  155. //snmp_set_sysdesr((u8_t*)sSettings.sSnmp.sysDescr, &len);
  156. }
  157. /**
  158. * @brief Установить SNMP Community для чтения
  159. */
  160. void SNMP_SetReadCommunity(char *comm)
  161. {
  162. strcpy(sSettings.sSnmp.readCommunity, comm);
  163. }
  164. /**
  165. * @brief Установить SNMP Community для записи
  166. */
  167. void SNMP_SetWriteCommunity(char *comm)
  168. {
  169. strcpy(sSettings.sSnmp.writeCommunity, comm);
  170. }
  171. /**
  172. * @brief Установить SNMP SysContact
  173. * @retval
  174. */
  175. void SNMP_SetSysContact(char *con)
  176. {
  177. static uint8_t len;
  178. len = strlen(con);
  179. //snmp_set_syscontact((u8_t*)con, &len);
  180. }
  181. /**
  182. * @brief Установить SNMP SysName
  183. * @retval
  184. */
  185. void SNMP_SetSysName(char *name)
  186. {
  187. static uint8_t len;
  188. len = strlen(name);
  189. //snmp_set_sysname((u8_t*)name, &len);
  190. }
  191. /**
  192. * @brief Установить SNMP SysLocation
  193. * @retval
  194. */
  195. void SNMP_SetSysLocation(char *loc)
  196. {
  197. static uint8_t len;
  198. len = strlen(loc);
  199. //snmp_set_syslocation((u8_t*)loc, &len);
  200. }
  201. /**
  202. * @brief Установить SNMP SysManagerIP
  203. * @retval
  204. */
  205. void SNMP_SetManagerIP(char *ip)
  206. {/*
  207. static ip_addr_t trap_addr;
  208. ipaddr_aton(ip, &trap_addr);
  209. snmp_trap_dst_ip_set(0, &trap_addr);*/
  210. }
  211. /**
  212. * @brief Установить SNMP Object ID
  213. * @retval
  214. */
  215. void SNMP_SetObjID(void)
  216. {
  217. //static struct snmp_obj_id my_object_id = {9, {1, 3, 6, 1, 4, 1, 41752, 911, 3}};
  218. //snmp_set_sysobjid(&my_object_id);
  219. }
  220. /**
  221. * @brief Вкл/выкл трапы
  222. * @retval
  223. */
  224. void SNMP_SetTrapOnOff(uint8_t state)
  225. {
  226. //snmp_trap_dst_enable(0, (u8_t)state);
  227. }
  228. /********************************* (C) ROTEK **********************************/