server_GPIO.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include <string>
  2. #include "string.h"
  3. #include <string.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_system.h"
  7. #include "esp_event.h"
  8. #include "esp_log.h"
  9. #include "driver/gpio.h"
  10. //#include "errno.h"
  11. #include <sys/stat.h>
  12. #include <vector>
  13. //#include <regex>
  14. #include "server_GPIO.h"
  15. #include "ClassLogFile.h"
  16. #include "configFile.h"
  17. #include "Helper.h"
  18. // #define DEBUG_DETAIL_ON
  19. GpioPin::GpioPin(gpio_num_t gpio, const char* name, gpio_pin_mode_t mode, gpio_int_type_t interruptType, uint8_t dutyResolution, bool mqttEnable, bool httpEnable)
  20. {
  21. _gpio = gpio;
  22. _name = name;
  23. _mode = mode;
  24. _interruptType = interruptType;
  25. //initGPIO();
  26. }
  27. GpioPin::~GpioPin()
  28. {
  29. printf("reset GPIO pin %d", _gpio);
  30. gpio_reset_pin(_gpio);
  31. }
  32. void GpioPin::init()
  33. {
  34. gpio_config_t io_conf;
  35. //set interrupt
  36. io_conf.intr_type = _interruptType;
  37. //set as output mode
  38. io_conf.mode = _mode == gpio_pin_mode_t::GPIO_PIN_MODE_OUTPUT ? gpio_mode_t::GPIO_MODE_OUTPUT : gpio_mode_t::GPIO_MODE_INPUT;
  39. //bit mask of the pins that you want to set,e.g.GPIO18/19
  40. io_conf.pin_bit_mask = (1ULL << _gpio);
  41. //set pull-down mode
  42. io_conf.pull_down_en = _mode == gpio_pin_mode_t::GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pulldown_t::GPIO_PULLDOWN_ENABLE : gpio_pulldown_t::GPIO_PULLDOWN_DISABLE;
  43. //set pull-up mode
  44. io_conf.pull_up_en = _mode == gpio_pin_mode_t::GPIO_PIN_MODE_INPUT_PULLDOWN ? gpio_pullup_t::GPIO_PULLUP_ENABLE : gpio_pullup_t::GPIO_PULLUP_DISABLE;
  45. //configure GPIO with the given settings
  46. gpio_config(&io_conf);
  47. }
  48. bool GpioPin::getValue(std::string* errorText)
  49. {
  50. if ((_mode != GPIO_PIN_MODE_INPUT) && (_mode != GPIO_PIN_MODE_INPUT_PULLUP) && (_mode != GPIO_PIN_MODE_INPUT_PULLDOWN)) {
  51. (*errorText) = "GPIO is not in input mode";
  52. }
  53. return gpio_get_level(_gpio) == 1;
  54. }
  55. void GpioPin::setValue(bool value, std::string* errorText)
  56. {
  57. if ((_mode != GPIO_PIN_MODE_OUTPUT) && (_mode != GPIO_PIN_MODE_OUTPUT_PWM)) {
  58. (*errorText) = "GPIO is not in output mode";
  59. }
  60. gpio_set_level(_gpio, value);
  61. }
  62. esp_err_t callHandleHttpRequest(httpd_req_t *req)
  63. {
  64. printf("callHandleHttpRequest\n");
  65. GpioHandler *gpioHandler = (GpioHandler*)req->user_ctx;
  66. return gpioHandler->handleHttpRequest(req);
  67. }
  68. void taskGpioHandler(void *pvParameter)
  69. {
  70. printf("taskGpioHandler\n");
  71. ((GpioHandler*)pvParameter)->init();
  72. }
  73. GpioHandler::GpioHandler(std::string configFile, httpd_handle_t httpServer)
  74. {
  75. printf("---- start GpioHandler ----\n");
  76. _configFile = configFile;
  77. printf("-1-\n");
  78. _httpServer = httpServer;
  79. printf("-2-\n");
  80. printf("register GPIO Uri\n");
  81. registerGpioUri();
  82. //xTaskCreate((TaskFunction_t)&taskGpioHandler, "taskGpioHandler", configMINIMAL_STACK_SIZE * 64, this, tskIDLE_PRIORITY+1, &xHandletaskGpioHandler);
  83. }
  84. GpioHandler::~GpioHandler() {
  85. if (gpioMap != NULL) {
  86. clear();
  87. delete gpioMap;
  88. }
  89. }
  90. void GpioHandler::init()
  91. {
  92. printf("freemem -3.1-: %u\n", esp_get_free_heap_size());
  93. // TickType_t xDelay = 60000 / portTICK_PERIOD_MS;
  94. // printf("wait before start %ldms\n", (long) xDelay);
  95. // vTaskDelay( xDelay );
  96. if (gpioMap == NULL) {
  97. gpioMap = new std::map<gpio_num_t, GpioPin*>();
  98. } else {
  99. clear();
  100. }
  101. printf("read GPIO config and init GPIO\n");
  102. printf("freemem -3.2-: %u\n", esp_get_free_heap_size());
  103. readConfig();
  104. printf("freemem -3.3-: %u\n", esp_get_free_heap_size());
  105. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  106. it->second->init();
  107. }
  108. printf("freemem -3.4-: %u\n", esp_get_free_heap_size());
  109. printf("GPIO init comleted\n");
  110. }
  111. bool GpioHandler::readConfig()
  112. {
  113. if (!gpioMap->empty())
  114. clear();
  115. ConfigFile configFile = ConfigFile(_configFile);
  116. std::vector<std::string> zerlegt;
  117. std::string line = "";
  118. bool disabledLine = false;
  119. bool eof = false;
  120. while ((!configFile.GetNextParagraph(line, disabledLine, eof) || (line.compare("[GPIO]") != 0)) && !disabledLine && !eof) {}
  121. if (eof)
  122. return false;
  123. while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
  124. {
  125. zerlegt = configFile.ZerlegeZeile(line);
  126. // const std::regex pieces_regex("IO([0-9]{1,2})");
  127. // std::smatch pieces_match;
  128. // if (std::regex_match(zerlegt[0], pieces_match, pieces_regex) && (pieces_match.size() == 2))
  129. // {
  130. // std::string gpioStr = pieces_match[1];
  131. if (zerlegt[0].rfind("IO", 0) == 0)
  132. {
  133. printf("Enable GP%s in %s mode\n", zerlegt[0].c_str(), zerlegt[1].c_str());
  134. std::string gpioStr = zerlegt[0].substr(2, 2);
  135. gpio_num_t gpioNr = (gpio_num_t)atoi(gpioStr.c_str());
  136. gpio_pin_mode_t pinMode = resolvePinMode(toLower(zerlegt[1]));
  137. gpio_int_type_t intType = resolveIntType(toLower(zerlegt[2]));
  138. uint16_t dutyResolution = (uint8_t)atoi(zerlegt[3].c_str());
  139. bool mqttEnabled = toLower(zerlegt[4]) == "true";
  140. bool httpEnabled = toLower(zerlegt[5]) == "true";
  141. (*gpioMap)[gpioNr] = new GpioPin(gpioNr, zerlegt[6].c_str(), pinMode, intType,dutyResolution, mqttEnabled, httpEnabled);
  142. }
  143. }
  144. return true;
  145. }
  146. void GpioHandler::clear()
  147. {
  148. for(std::map<gpio_num_t, GpioPin*>::iterator it = gpioMap->begin(); it != gpioMap->end(); ++it) {
  149. delete it->second;
  150. }
  151. gpioMap->clear();
  152. }
  153. void GpioHandler::registerGpioUri()
  154. {
  155. ESP_LOGI(TAGPARTGPIO, "server_GPIO - Registering URI handlers");
  156. httpd_uri_t camuri = { };
  157. camuri.method = HTTP_GET;
  158. camuri.uri = "/GPIO";
  159. camuri.handler = callHandleHttpRequest;
  160. camuri.user_ctx = (void*)this;
  161. httpd_register_uri_handler(_httpServer, &camuri);
  162. }
  163. esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
  164. {
  165. printf("handleHttpRequest\n");
  166. #ifdef DEBUG_DETAIL_ON
  167. LogFile.WriteHeapInfo("handler_switch_GPIO - Start");
  168. #endif
  169. LogFile.WriteToFile("handler_switch_GPIO");
  170. char _query[200];
  171. char _valueGPIO[30];
  172. char _valueStatus[30];
  173. std::string gpio, status;
  174. printf("-1-\n");
  175. if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK) {
  176. printf("Query: "); printf(_query); printf("\n");
  177. if (httpd_query_key_value(_query, "GPIO", _valueGPIO, 30) == ESP_OK)
  178. {
  179. printf("GPIO is found "); printf(_valueGPIO); printf("\n");
  180. gpio = std::string(_valueGPIO);
  181. } else {
  182. std::string resp_str = "GPIO No is not defined";
  183. httpd_resp_send(req, resp_str.c_str(), resp_str.length());
  184. return ESP_OK;
  185. }
  186. if (httpd_query_key_value(_query, "Status", _valueStatus, 30) == ESP_OK)
  187. {
  188. printf("Status is found "); printf(_valueStatus); printf("\n");
  189. status = std::string(_valueStatus);
  190. }
  191. } else {
  192. char* resp_str = "Error in call. Use /GPIO?GPIO=12&Status=high";
  193. httpd_resp_send(req, resp_str, strlen(resp_str));
  194. return ESP_OK;
  195. }
  196. printf("-2-\n");
  197. status = toUpper(status);
  198. if ((status != "HIGH") && (status != "LOW") && (status != "TRUE") && (status != "FALSE") && (status != "0") && (status != "1") && (status != ""))
  199. {
  200. std::string zw = "Status not valid: " + status;
  201. httpd_resp_sendstr_chunk(req, zw.c_str());
  202. httpd_resp_sendstr_chunk(req, NULL);
  203. return ESP_OK;
  204. }
  205. printf("-3-\n");
  206. int gpionum = stoi(gpio);
  207. // frei: 16; 12-15; 2; 4 // nur 12 und 13 funktionieren 2: reboot, 4: BlitzLED, 15: PSRAM, 14/15: DMA für SDKarte ???
  208. gpio_num_t gpio_num = resolvePinNr(gpionum);
  209. if (gpio_num == GPIO_NUM_NC)
  210. {
  211. std::string zw = "GPIO" + std::to_string(gpionum) + " not support - only 12 & 13 free";
  212. httpd_resp_sendstr_chunk(req, zw.c_str());
  213. httpd_resp_sendstr_chunk(req, NULL);
  214. return ESP_OK;
  215. }
  216. if (gpioMap->count(gpio_num) == 0) {
  217. char resp_str [30];
  218. sprintf(resp_str, "GPIO%d is not registred", gpio_num);
  219. httpd_resp_send(req, resp_str, strlen(resp_str));
  220. return ESP_OK;
  221. }
  222. printf("-4-\n");
  223. if (status == "")
  224. {
  225. std::string resp_str = "";
  226. status = (*gpioMap)[gpio_num]->getValue(&resp_str) ? "HIGH" : "LOW";
  227. if (resp_str == "") {
  228. resp_str = status;
  229. }
  230. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  231. httpd_resp_sendstr_chunk(req, NULL);
  232. }
  233. else
  234. {
  235. std::string resp_str = "";
  236. (*gpioMap)[gpio_num]->setValue((status == "HIGH") || (status == "TRUE") || (status == "1"), &resp_str);
  237. if (resp_str == "") {
  238. resp_str = "GPIO" + std::to_string(gpionum) + " switched to " + status;
  239. }
  240. httpd_resp_sendstr_chunk(req, resp_str.c_str());
  241. httpd_resp_sendstr_chunk(req, NULL);
  242. }
  243. printf("-5-\n");
  244. return ESP_OK;
  245. };
  246. gpio_num_t GpioHandler::resolvePinNr(uint8_t pinNr)
  247. {
  248. switch(pinNr) {
  249. case 0:
  250. return GPIO_NUM_0;
  251. case 1:
  252. return GPIO_NUM_1;
  253. case 3:
  254. return GPIO_NUM_3;
  255. case 4:
  256. return GPIO_NUM_4;
  257. case 12:
  258. return GPIO_NUM_12;
  259. case 13:
  260. return GPIO_NUM_13;
  261. default:
  262. return GPIO_NUM_NC;
  263. }
  264. }
  265. gpio_pin_mode_t GpioHandler::resolvePinMode(std::string input)
  266. {
  267. if( input == "disabled" ) return GPIO_PIN_MODE_DISABLED;
  268. if( input == "input" ) return GPIO_PIN_MODE_INPUT;
  269. if( input == "input-pullup" ) return GPIO_PIN_MODE_INPUT_PULLUP;
  270. if( input == "input-pulldown" ) return GPIO_PIN_MODE_INPUT_PULLDOWN;
  271. if( input == "output" ) return GPIO_PIN_MODE_OUTPUT;
  272. if( input == "output-pwm" ) return GPIO_PIN_MODE_OUTPUT_PWM;
  273. if( input == "external-flash-pwm" ) return GPIO_PIN_MODE_EXTERNAL_FLASH_PWM;
  274. if( input == "external-flash-ws281x" ) return GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X;
  275. return GPIO_PIN_MODE_DISABLED;
  276. }
  277. gpio_int_type_t GpioHandler::resolveIntType(std::string input)
  278. {
  279. if( input == "disabled" ) return GPIO_INTR_DISABLE;
  280. if( input == "rising-edge" ) return GPIO_INTR_POSEDGE;
  281. if( input == "falling-edge" ) return GPIO_INTR_NEGEDGE;
  282. if( input == "rising-and-falling" ) return GPIO_INTR_ANYEDGE ;
  283. if( input == "low-level-trigger" ) return GPIO_INTR_LOW_LEVEL;
  284. if( input == "high-level-trigger" ) return GPIO_INTR_HIGH_LEVEL;
  285. return GPIO_INTR_DISABLE;
  286. }