main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include <sys/poll.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <assert.h>
  9. #include <time.h>
  10. #include <pthread.h>
  11. #include "hiredis/hiredis.h"
  12. #include "cjson/cJSON.h"
  13. #include "iniparser/iniparser.h"
  14. #define Boolean int
  15. #define TRUE 1
  16. #define FALSE 0
  17. #define MAX_PIPE_BUFSIZE 32
  18. //GPIO_STATUS
  19. #define GPIO_Off 0
  20. #define GPIO_On 1
  21. #define GPIO_Fast 2
  22. #define GPIO_Slow 3
  23. #define GPIO_Simultaneously 4
  24. #define GPIO_Alternately 5
  25. #define SPK_CONF "/etc/speaker.conf"
  26. #define unix_socket_path "/tmp/redis.sock"
  27. #define VAR_IP "${ip}"
  28. #define VAR_MAC "${mac}"
  29. static int DEBUG = FALSE;
  30. static int GPIO67_STATUS = GPIO_Off;
  31. static int GPIO135_STATUS = GPIO_Off;
  32. int GetCmdValue(char *file, char *key, char *value, int lenth)
  33. {
  34. dictionary * ini ;
  35. ini = iniparser_load(file);
  36. if (ini==NULL) {
  37. fprintf(stderr, "cannot parse file: %s\n", file);
  38. return FALSE ;
  39. }
  40. strcpy(value, iniparser_getstring(ini, key, ""));
  41. if(DEBUG)
  42. printf("keystr: \"%s\" ; value: %s.\n", key, value);
  43. iniparser_freedict(ini);
  44. return TRUE;
  45. }
  46. char *strrpl(char *s, const char *s1, const char *s2)
  47. {
  48. char *ptr;
  49. while (ptr = strstr(s, s1)) /* 如果在s中找到s1 */
  50. {
  51. memmove(ptr + strlen(s2) , ptr + strlen(s1), strlen(ptr) - strlen(s1) + 1);
  52. memcpy(ptr, &s2[0], strlen(s2));
  53. }
  54. return s;
  55. }
  56. Boolean get_ip(char * pv)
  57. {
  58. FILE * fp;
  59. char cmdbuf[128], value[32];
  60. char *tmp=NULL;
  61. sprintf(cmdbuf,"/sbin/ifconfig eth0 | grep inet | cut -d\":\" -f2 | cut -d\" \" -f1");
  62. fp = popen(cmdbuf, "r");
  63. if (fp == NULL) {
  64. // printf("Fail to open pipe\n");
  65. return FALSE;
  66. }
  67. memset(value,0,sizeof(value));
  68. if (!feof(fp) && fgets(value,MAX_PIPE_BUFSIZE,fp) != NULL)
  69. ;
  70. // printf("value:%s\n", value);
  71. else {
  72. printf("ip wrong\n");
  73. return FALSE;
  74. }
  75. pclose(fp);
  76. if(strlen(value) < 10)
  77. return FALSE;
  78. if(tmp = strstr(value,"\n"))
  79. {
  80. *tmp = '\0';
  81. }
  82. strcpy(pv,value);
  83. return TRUE;
  84. }
  85. Boolean get_mac(char * pv)
  86. {
  87. FILE * fp;
  88. char cmdbuf[128], value[32];
  89. char *tmp=NULL;
  90. sprintf(cmdbuf,"ifconfig eth0 | grep HWaddr | tr -d : | awk '{print $5}'");
  91. fp = popen(cmdbuf, "r");
  92. if (fp == NULL) {
  93. // printf("Fail to open pipe\n");
  94. return FALSE;
  95. }
  96. memset(value,0,sizeof(value));
  97. if (!feof(fp) && fgets(value,MAX_PIPE_BUFSIZE,fp) != NULL)
  98. ;
  99. // printf("value:%s\n", value);
  100. else {
  101. printf("ip wrong\n");
  102. return FALSE;
  103. }
  104. pclose(fp);
  105. if(strlen(value) < 10)
  106. return FALSE;
  107. if(tmp = strstr(value,"\n"))
  108. {
  109. *tmp = '\0';
  110. }
  111. strcpy(pv,value);
  112. return TRUE;
  113. }
  114. void action_url(char *url)
  115. {
  116. char cmd[320];
  117. sprintf(cmd,"curl --connect-timeout 5 '%s' 2>/dev/null &",url);
  118. if(DEBUG)
  119. printf( "Command: %s\n", cmd);
  120. system(cmd);
  121. }
  122. int get_gpio_type(char *output_type){
  123. if(strcmp(output_type, "On") == 0)
  124. {
  125. return GPIO_On;
  126. }
  127. else if(strcmp(output_type, "FastFlashing") == 0)
  128. {
  129. return GPIO_Fast;
  130. }
  131. else if(strcmp(output_type, "SlowFlashing") == 0)
  132. {
  133. return GPIO_Slow;
  134. }
  135. else if(strcmp(output_type, "Simultaneously") == 0)
  136. {
  137. return GPIO_Simultaneously;
  138. }
  139. else if(strcmp(output_type, "Alternately") == 0)
  140. {
  141. return GPIO_Alternately;
  142. }
  143. return GPIO_Off;
  144. }
  145. void gpio_on_trigger()
  146. {
  147. char enable[8],url[256],ipaddr[16],macaddr[16],cmd[320];
  148. GetCmdValue(SPK_CONF, "relay_action_url:on_enable", enable, sizeof(enable));
  149. if(strcmp(enable,"yes") == 0)
  150. {
  151. GetCmdValue(SPK_CONF, "relay_action_url:on_url", url, sizeof(url));
  152. if(strstr(url,VAR_IP))
  153. {
  154. if(get_ip(ipaddr))
  155. strcpy(url, strrpl(url, VAR_IP, ipaddr));
  156. }
  157. if(strstr(url,VAR_MAC))
  158. {
  159. if(get_mac(macaddr))
  160. strcpy(url, strrpl(url, VAR_MAC, macaddr));
  161. }
  162. action_url(url);
  163. }
  164. }
  165. void gpio_off_trigger()
  166. {
  167. char enable[8],url[256],ipaddr[16],macaddr[16],cmd[320];
  168. GetCmdValue(SPK_CONF, "relay_action_url:off_enable", enable, sizeof(enable));
  169. if(strcmp(enable,"yes") == 0)
  170. {
  171. GetCmdValue(SPK_CONF, "relay_action_url:off_url", url, sizeof(url));
  172. if(strstr(url,VAR_IP))
  173. {
  174. if(get_ip(ipaddr))
  175. strcpy(url, strrpl(url, VAR_IP, ipaddr));
  176. }
  177. if(strstr(url,VAR_MAC))
  178. {
  179. if(get_mac(macaddr))
  180. strcpy(url, strrpl(url, VAR_MAC, macaddr));
  181. }
  182. action_url(url);
  183. }
  184. }
  185. //gpio控制指令处理
  186. void ProcessReply( redisReply * pReply )
  187. {
  188. cJSON *pJson = NULL;
  189. redisReply * pSubReply = NULL;
  190. if ( pReply->elements == 2 )
  191. {
  192. pSubReply = pReply->element[1];
  193. if(DEBUG)
  194. printf( "Msg [%s]\n", pSubReply->str );
  195. pJson = cJSON_Parse(pSubReply->str);
  196. if ( pJson ) {
  197. if(strcmp(cJSON_GetObjectItem(pJson, "id")->valuestring, "gpio67") == 0 || strcmp(cJSON_GetObjectItem(pJson, "id")->valuestring, "gpio135") == 0)
  198. {
  199. GPIO67_STATUS = get_gpio_type(cJSON_GetObjectItem(pJson, "type")->valuestring);
  200. if(GPIO67_STATUS == GPIO_Off)
  201. {
  202. gpio_off_trigger();
  203. }
  204. else
  205. {
  206. gpio_on_trigger();
  207. }
  208. if(DEBUG)
  209. printf("RLEAY: %s\n",cJSON_GetObjectItem(pJson, "type")->valuestring);
  210. }
  211. // else if(strcmp(cJSON_GetObjectItem(pJson, "id")->valuestring, "gpio135") == 0)
  212. // {
  213. // GPIO135_STATUS = get_gpio_type(cJSON_GetObjectItem(pJson, "type")->valuestring);
  214. // if(GPIO135_STATUS == GPIO_Off)
  215. // {
  216. // gpio_off_trigger();
  217. // }
  218. // else
  219. // {
  220. // gpio_on_trigger();
  221. // }
  222. // if(DEBUG)
  223. // printf("gpio135: %s\n",cJSON_GetObjectItem(pJson, "type")->valuestring);
  224. // }
  225. }else{
  226. printf( "parse failed!\n");
  227. }
  228. if(pJson != NULL) cJSON_Delete(pJson);
  229. }
  230. }
  231. /*
  232. * 接收控制指令.
  233. */
  234. void *get_gpio_control()
  235. {
  236. redisContext * pContext = redisConnectUnix(unix_socket_path);
  237. if ( NULL == pContext || pContext->err == 1 )
  238. {
  239. printf( "%s\n", pContext->errstr );
  240. exit( -1 );
  241. }
  242. char * pKey = "output-channel";
  243. redisReply * pReply;
  244. while(1){
  245. pReply = NULL;
  246. pReply = redisCommand( pContext, "BRPOP %s 60", pKey );
  247. if(pReply != NULL && pReply->type == REDIS_REPLY_ARRAY){
  248. ProcessReply( pReply );
  249. }
  250. if(pReply != NULL) freeReplyObject( pReply );
  251. usleep(500*1000);
  252. }
  253. redisFree( pContext );
  254. }
  255. //gpio状态设置
  256. void *gpio_status()
  257. {
  258. int gpioFlashTime = 0,gpioFlashTag1 = 0,gpioFlashTag2 = 0;
  259. while(1)
  260. {
  261. usleep(10 * 1000);
  262. /* gpio flash controller */
  263. gpioFlashTime++;
  264. if(gpioFlashTime == 47 || gpioFlashTime == 143){
  265. // if(GPIO135_STATUS == GPIO_On)
  266. // system("echo 0 > /sys/class/gpio/gpio134/value;echo 0 > /sys/class/gpio/gpio135/value");
  267. // else if(GPIO135_STATUS == GPIO_Off)
  268. // system("echo 1 > /sys/class/gpio/gpio134/value;echo 1 > /sys/class/gpio/gpio135/value");
  269. if(GPIO67_STATUS == GPIO_On)
  270. system("echo 1 > /sys/class/gpio/gpio67/value");
  271. else if(GPIO67_STATUS == GPIO_Off)
  272. system("echo 0 > /sys/class/gpio/gpio67/value");
  273. }
  274. if(gpioFlashTime % 39 == 0){
  275. // if(GPIO135_STATUS == GPIO_Simultaneously){
  276. // gpioFlashTag1 = !gpioFlashTag1;
  277. // if(gpioFlashTag1){
  278. // system("echo 0 > /sys/class/gpio/gpio134/value;echo 0 > /sys/class/gpio/gpio135/value");
  279. // }else{
  280. // system("echo 1 > /sys/class/gpio/gpio134/value;echo 1 > /sys/class/gpio/gpio135/value");
  281. // }
  282. // }
  283. // if(GPIO135_STATUS == GPIO_Alternately){
  284. // gpioFlashTag1 = !gpioFlashTag1;
  285. // if(gpioFlashTag1){
  286. // system("echo 1 > /sys/class/gpio/gpio134/value;echo 0 > /sys/class/gpio/gpio135/value");
  287. // }else{
  288. // system("echo 0 > /sys/class/gpio/gpio134/value;echo 1 > /sys/class/gpio/gpio135/value");
  289. // }
  290. // }
  291. if(GPIO67_STATUS == GPIO_Fast){
  292. gpioFlashTag2 = !gpioFlashTag2;
  293. if(gpioFlashTag2){
  294. system("echo 0 > /sys/class/gpio/gpio67/value");
  295. }else{
  296. system("echo 1 > /sys/class/gpio/gpio67/value");
  297. }
  298. }
  299. }
  300. if(gpioFlashTime % 99 == 0){
  301. // if(GPIO135_STATUS == GPIO_Slow){
  302. // gpioFlashTag1 = !gpioFlashTag1;
  303. // if(gpioFlashTag1){
  304. // system("echo 0 > /sys/class/gpio/gpio134/value;echo 0 > /sys/class/gpio/gpio135/value");
  305. // }else{
  306. // system("echo 1 > /sys/class/gpio/gpio134/value;echo 1 > /sys/class/gpio/gpio135/value");
  307. // }
  308. // }
  309. if(GPIO67_STATUS == GPIO_Slow){
  310. gpioFlashTag2 = !gpioFlashTag2;
  311. if(gpioFlashTag2){
  312. system("echo 0 > /sys/class/gpio/gpio67/value");
  313. }else{
  314. system("echo 1 > /sys/class/gpio/gpio67/value");
  315. }
  316. }
  317. }
  318. /* restart the led flash loop */
  319. if(gpioFlashTime == 200)
  320. gpioFlashTime = 0;
  321. }
  322. }
  323. int main(int argc, char *argv[]){
  324. char model[16];
  325. int ret_thrd1,ret_thrd2;
  326. pthread_t thread1, thread2;
  327. if(argc == 2 && strcmp(argv[1], "debug") == 0)
  328. DEBUG = TRUE;
  329. //启动线程控制gpio状态
  330. ret_thrd2 = pthread_create(&thread2, NULL, gpio_status, NULL);
  331. if (ret_thrd2 != 0) {
  332. printf("create thread2 gpio_status failed\n");
  333. return 0;
  334. }
  335. //启动线程接收gpio控制指令
  336. get_gpio_control();
  337. }