main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "iniparser/iniparser.h"
  13. #define Boolean int
  14. #define TRUE 1
  15. #define FALSE 0
  16. #define MAX_PIPE_BUFSIZE 256
  17. //BARESIP_STATUS
  18. #define IDLE 0
  19. #define INUSE 1
  20. #define RING 2
  21. #define RINGING 3
  22. #define HOLD 4
  23. #define OFFLINE 5
  24. #define DIALING 6
  25. //BARESIP_REG
  26. #define REGISTER_FAIL 0
  27. #define REGISTER_OK 1
  28. //DEV_TYPE
  29. #define SPEAKER 0
  30. #define SPEAKER2 1
  31. #define SPEAKER3 1
  32. #define INTERCOM 2
  33. #define GATEWAYE 3
  34. //LED_STATUS
  35. #define Led_Off 0
  36. #define Led_On 1
  37. #define Led_Fast 2
  38. #define Led_Slow 3
  39. //REDIS_CHANNEL
  40. #define BARESIP_CHAN 0
  41. #define GPIO_CHAN 1
  42. //GPIO_STATUS
  43. #define GPIO_OFF 0
  44. #define GPIO_ON 1
  45. //DIN
  46. #define DOWN 0
  47. #define UP 1
  48. #define SPK_CONF "/etc/speaker.conf"
  49. #define VOL_CONF "/oem/etc/volctrl.conf"
  50. //operator log type
  51. #define BTN "BUTTON"
  52. #define FUN "FUNCTION"
  53. #define STA "SIP STATE"
  54. static int BARESIP_STATUS = OFFLINE;
  55. static int keyLedStatus = Led_On;
  56. static int DEBUG = FALSE;
  57. static int ACCOUNT1_REG = REGISTER_FAIL;
  58. static int ACCOUNT2_REG = REGISTER_FAIL;
  59. static int ACCOUNT3_REG = REGISTER_FAIL;
  60. static int ACCOUNTP2P_REG = REGISTER_FAIL;
  61. static int RELAY_STATE = GPIO_OFF;
  62. static int press_down = FALSE;
  63. static int DEV_TYPE = SPEAKER;
  64. static int CURRENT_CALLS = 0;
  65. static int regcount=0;
  66. static char DTMF_STRING[32] = "\0";
  67. /*
  68. * @cmd 执行命令
  69. * @value 命令结果指针
  70. * @lenth 存储结果长度
  71. int GetCmdValue(char *cmd, char *value, int lenth)
  72. {
  73. bzero(value, lenth);
  74. FILE *fp = popen(cmd,"r");
  75. if(fp != NULL){
  76. fread(value, 1, lenth, fp);
  77. pclose(fp);
  78. } else {
  79. printf("Handle \"%s\" failed.\n", cmd);
  80. return FALSE;
  81. }
  82. return TRUE;
  83. }
  84. */
  85. int GetCmdValue(char *file, char *key, char *value, int lenth)
  86. {
  87. dictionary * ini ;
  88. ini = iniparser_load(file);
  89. if (ini==NULL) {
  90. fprintf(stderr, "cannot parse file: %s\n", file);
  91. return FALSE ;
  92. }
  93. strcpy(value, iniparser_getstring(ini, key, ""));
  94. if(DEBUG)
  95. printf("keystr: \"%s\" ; value: %s.\n", key, value);
  96. iniparser_freedict(ini);
  97. return TRUE;
  98. }
  99. //控制功放的开启/关闭,TRUE/FALSE
  100. // void amplifier_switch(Boolean enable){
  101. // if(enable){
  102. // system("/etc/scripts/pa_mute.sh 0");
  103. // }
  104. // else
  105. // {
  106. // system("/etc/scripts/pa_mute.sh 1");
  107. // }
  108. // }
  109. Boolean get_ip(char * pv)
  110. {
  111. FILE * fp;
  112. char cmdbuf[128], value[32];
  113. char *tmp=NULL;
  114. sprintf(cmdbuf,"/sbin/ifconfig eth1 | grep 'inet ' | awk '{print $2}'");
  115. fp = popen(cmdbuf, "r");
  116. if (fp == NULL) {
  117. // printf("Fail to open pipe\n");
  118. return FALSE;
  119. }
  120. memset(value,0,sizeof(value));
  121. if (!feof(fp) && fgets(value,32,fp) != NULL)
  122. ;
  123. // printf("value:%s\n", value);
  124. else {
  125. printf("ip wrong\n");
  126. return FALSE;
  127. }
  128. pclose(fp);
  129. if(strlen(value) < 10)
  130. return FALSE;
  131. if(tmp = strstr(value,"\n"))
  132. {
  133. *tmp = '\0';
  134. }
  135. strcpy(pv,value);
  136. return TRUE;
  137. }
  138. // void writeLog(char *type,char *action, char *remark)
  139. // {
  140. // char logstr[256];
  141. // redisContext *pContext = redisConnect("127.0.0.1", 6379);
  142. // if (NULL == pContext || pContext->err == 1)
  143. // {
  144. // printf("%s\n", pContext->errstr);
  145. // return;
  146. // }
  147. // sprintf(logstr, "%s,%s,%s", type, action, remark);
  148. // redisReply *pReply;
  149. // pReply = redisCommand(pContext, "LPUSH writelog-channel %s", logstr);
  150. // freeReplyObject(pReply);
  151. // redisFree(pContext);
  152. // }
  153. int PlayIP()
  154. {
  155. char language[32];
  156. GetCmdValue(SPK_CONF, "system:language", language, sizeof(language));
  157. char ipaddr[16],cmd_line[240];
  158. // system("/usr/bin/amixer -q sset 'Master',0 29");
  159. // amplifier_switch(TRUE); //开启功放
  160. if(!get_ip(ipaddr)) {
  161. if (strcmp(language, "en") == 0){
  162. system("mpg123 /usr/share/sounds/en/get_ip_failed.mp3");
  163. }else{
  164. system("mpg123 /usr/share/sounds/cn/get_ip_failed.mp3");
  165. }
  166. return 0;
  167. }
  168. if (strcmp(language, "en") == 0){
  169. strcpy(cmd_line,"cd /usr/share/sounds/en/;mpg123 ");
  170. }else{
  171. strcpy(cmd_line,"cd /usr/share/sounds/cn/;mpg123 ");
  172. }
  173. int i;
  174. for (i=0;i<strlen(ipaddr);i++){
  175. switch(ipaddr[i])
  176. {
  177. case '0':
  178. strcat(cmd_line,"digit-0.mp3 ");
  179. break;
  180. case '1':
  181. strcat(cmd_line,"digit-1.mp3 ");
  182. break;
  183. case '2':
  184. strcat(cmd_line,"digit-2.mp3 ");
  185. break;
  186. case '3':
  187. strcat(cmd_line,"digit-3.mp3 ");
  188. break;
  189. case '4':
  190. strcat(cmd_line,"digit-4.mp3 ");
  191. break;
  192. case '5':
  193. strcat(cmd_line,"digit-5.mp3 ");
  194. break;
  195. case '6':
  196. strcat(cmd_line,"digit-6.mp3 ");
  197. break;
  198. case '7':
  199. strcat(cmd_line,"digit-7.mp3 ");
  200. break;
  201. case '8':
  202. strcat(cmd_line,"digit-8.mp3 ");
  203. break;
  204. case '9':
  205. strcat(cmd_line,"digit-9.mp3 ");
  206. break;
  207. case '.':
  208. strcat(cmd_line,"dian.mp3 ");
  209. break;
  210. }
  211. }
  212. system(cmd_line);
  213. // amplifier_switch(FALSE); //关闭功放
  214. system("/etc/scripts/set_volume.sh resume");
  215. // writeLog( FUN, "Play IP Address", ipaddr );
  216. return 0;
  217. }
  218. int main(int argc, char *argv[]){
  219. dictionary * ini ;
  220. char alwaysPlayIP[8];
  221. // sleep(2);
  222. GetCmdValue(VOL_CONF, "volume:always_play_ip", alwaysPlayIP, sizeof(alwaysPlayIP));
  223. if (strcmp(alwaysPlayIP, "yes") == 0)
  224. {
  225. system("/etc/scripts/set_volume.sh 7 7");
  226. PlayIP();
  227. return TRUE;
  228. }
  229. ini = iniparser_load(SPK_CONF);
  230. if (ini==NULL) {
  231. fprintf(stderr, "cannot parse file: %s\n", SPK_CONF);
  232. return FALSE ;
  233. }
  234. if(strcmp(iniparser_getstring(ini, "account_info_1:enable", "no"),"yes") == 0)
  235. {
  236. goto end;
  237. }
  238. else if(strcmp(iniparser_getstring(ini, "account_info_2:enable", "no"),"yes") == 0)
  239. {
  240. goto end;
  241. }
  242. else if(strcmp(iniparser_getstring(ini, "account_info_3:enable", "no"),"yes") == 0)
  243. {
  244. goto end;
  245. }
  246. else if(strcmp(iniparser_getstring(ini, "account_info_p2p:enable", "no"),"yes") == 0)
  247. {
  248. goto end;
  249. }
  250. else if(strcmp(iniparser_getstring(ini, "multicast_player:enable", "no"),"yes") == 0)
  251. {
  252. goto end;
  253. }
  254. else if(strcmp(iniparser_getstring(ini, "onvif:enable", "no"),"yes") == 0)
  255. {
  256. goto end;
  257. }
  258. system("/etc/scripts/set_volume.sh 7 7");
  259. PlayIP();
  260. end:
  261. iniparser_freedict(ini);
  262. return TRUE;
  263. }