main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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{
  212. printf( "parse failed!\n");
  213. }
  214. if(pJson != NULL) cJSON_Delete(pJson);
  215. }
  216. }
  217. /*
  218. * 接收控制指令.
  219. */
  220. void *get_gpio_control()
  221. {
  222. redisContext * pContext = redisConnectUnix(unix_socket_path);
  223. if ( NULL == pContext || pContext->err == 1 )
  224. {
  225. printf( "%s\n", pContext->errstr );
  226. exit( -1 );
  227. }
  228. char * pKey = "output-channel";
  229. redisReply * pReply;
  230. while(1){
  231. pReply = NULL;
  232. pReply = redisCommand( pContext, "BRPOP %s 60", pKey );
  233. if(pReply != NULL && pReply->type == REDIS_REPLY_ARRAY){
  234. ProcessReply( pReply );
  235. }
  236. if(pReply != NULL) freeReplyObject( pReply );
  237. usleep(500*1000);
  238. }
  239. redisFree( pContext );
  240. }
  241. //gpio状态设置
  242. void *gpio_status()
  243. {
  244. int gpioFlashTime = 0,gpioFlashTag1 = 0,gpioFlashTag2 = 0;
  245. while(1)
  246. {
  247. usleep(10 * 1000);
  248. /* gpio flash controller */
  249. gpioFlashTime++;
  250. if(gpioFlashTime == 47 || gpioFlashTime == 143){
  251. if(GPIO67_STATUS == GPIO_On)
  252. system("echo 1 > /sys/class/gpio/gpio112/value");
  253. else if(GPIO67_STATUS == GPIO_Off)
  254. system("echo 0 > /sys/class/gpio/gpio112/value");
  255. }
  256. if(gpioFlashTime % 39 == 0){
  257. if(GPIO67_STATUS == GPIO_Fast){
  258. gpioFlashTag2 = !gpioFlashTag2;
  259. if(gpioFlashTag2){
  260. system("echo 0 > /sys/class/gpio/gpio112/value");
  261. }else{
  262. system("echo 1 > /sys/class/gpio/gpio112/value");
  263. }
  264. }
  265. }
  266. if(gpioFlashTime % 99 == 0){
  267. if(GPIO67_STATUS == GPIO_Slow){
  268. gpioFlashTag2 = !gpioFlashTag2;
  269. if(gpioFlashTag2){
  270. system("echo 0 > /sys/class/gpio/gpio112/value");
  271. }else{
  272. system("echo 1 > /sys/class/gpio/gpio112/value");
  273. }
  274. }
  275. }
  276. /* restart the led flash loop */
  277. if(gpioFlashTime == 200)
  278. gpioFlashTime = 0;
  279. }
  280. }
  281. int main(int argc, char *argv[]){
  282. char model[16];
  283. int ret_thrd1,ret_thrd2;
  284. pthread_t thread1, thread2;
  285. if(argc == 2 && strcmp(argv[1], "debug") == 0)
  286. DEBUG = TRUE;
  287. //启动线程控制gpio状态
  288. ret_thrd2 = pthread_create(&thread2, NULL, gpio_status, NULL);
  289. if (ret_thrd2 != 0) {
  290. printf("create thread2 gpio_status failed\n");
  291. return 0;
  292. }
  293. //启动线程接收gpio控制指令
  294. get_gpio_control();
  295. }